Skip to content

Commit

Permalink
fix(mu): get spawn working #244
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 19, 2023
1 parent cc4436c commit b1308fd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 26 deletions.
4 changes: 3 additions & 1 deletion servers/mu/src/domain/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function sendDataItemWith ({
of({ ...rest, tracer })
.chain(writeMessage)
.map(res => ({
...res,
/**
* An opaque method to fetch the result of the message just forwarded
* and then crank its results
Expand All @@ -100,7 +101,8 @@ export function sendDataItemWith ({
*/
const sendProcess = (ctx) => of(ctx)
.chain(writeProcess)
.map(() => ({
.map((res) => ({
...res,
/**
* There is nothing to crank for a process sent to the MU,
*
Expand Down
6 changes: 5 additions & 1 deletion servers/mu/src/domain/lib/processDataItem/parse-data-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export function parseDataItemWith ({ createDataItem, logger }) {
async (dataItem) => ({
tx: { id: await dataItem.id, processId: dataItem.target, data: ctx.raw },
dataItem: {
...dataItem.toJSON(),
id: await dataItem.id,
signature: dataItem.signature,
owner: dataItem.owner,
target: dataItem.target,
tags: dataItem.tags,
/**
* For some reason, anchor is not included in the toJSON api on DataItem,
* so we make sure to include it here
Expand Down
16 changes: 12 additions & 4 deletions servers/mu/src/domain/lib/processDataItem/parse-data-item.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import * as assert from 'node:assert'

import { createLogger } from '../../logger.js'
import { parseDataItemWith } from './parse-data-item.js'
import { omit } from 'ramda'

const logger = createLogger('ao-mu:processMsg')

describe('parseDataItem', () => {
test('parse data item into tx object', async () => {
const raw1 = Buffer.alloc(0)
const item1 = {
id: 'id-1',
id: Promise.resolve('id-1'),
signature: 'foobar',
owner: 'owner-1',
target: 'target-1',
anchor: 'foobar',
toJSON: () => omit(['toJSON'], item1)
tags: [
{ name: 'Foo', value: 'Bar' }
]
}
const parseDataItem = parseDataItemWith({
createDataItem: (raw) => {
Expand All @@ -34,8 +37,13 @@ describe('parseDataItem', () => {

assert.deepStrictEqual(result.dataItem, {
id: 'id-1',
signature: 'foobar',
owner: 'owner-1',
target: 'target-1',
anchor: 'foobar'
anchor: 'foobar',
tags: [
{ name: 'Foo', value: 'Bar' }
]
})
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { anyPass, findIndex, propOr } from 'ramda'
import { anyPass, propOr, tap } from 'ramda'
import { Rejected, Resolved, of } from 'hyper-async'

import { eqOrIncludes, parseTags } from '../../utils.js'
Expand All @@ -10,26 +10,32 @@ export function verifyParsedDataItemWith () {

const isProcessOrMessage = anyPass([eqOrIncludes('Process'), eqOrIncludes('Message')])

const isTagEqualTo = ({ name, value }) => (tag) => tag.name === name && tag.value === value

return (dataItem) => of(dataItem)
.map(tap(console.log))
.map(propOr([], 'tags'))
.map(parseTags)
.chain(checkTag('Data-Protocol', eqOrIncludes('ao'), 'must contain \'ao\''))
.chain(checkTag('Type', isProcessOrMessage, 'must be either \'Process\' or \'Message\''))
/**
* At this point, we know Type will contain 'Process' 'Message'
* OR both.
*
* So let's find the earliest occurring Type and use
* that to determine whether or not this data item is an ao Message or Process
*/
.map((parsedTags) => {
const [processIdx, messageIdx] = [
findIndex(eqOrIncludes('Process'), parsedTags.Type),
findIndex(eqOrIncludes('Message'), parsedTags.Type)
]
.chain((rawTags) =>
of(rawTags)
.map(parseTags)
.chain(checkTag('Data-Protocol', eqOrIncludes('ao'), 'must contain \'ao\''))
.chain(checkTag('Type', isProcessOrMessage, 'must be either \'Process\' or \'Message\''))
/**
* At this point, we know Type will contain 'Process' 'Message'
* OR both.
*
* So let's find the earliest occurring Type and use
* that to determine whether or not this data item is an ao Message or Process
*/
.map(() => {
const [processIdx, messageIdx] = [
rawTags.findIndex(isTagEqualTo({ name: 'Type', value: 'Process' })),
rawTags.findIndex(isTagEqualTo({ name: 'Type', value: 'Message' }))
]

if (processIdx === -1) return { isMessage: true }
if (messageIdx === -1) return { isMessage: false }
return { isMessage: messageIdx < processIdx }
})
if (processIdx === -1) return { isMessage: true }
if (messageIdx === -1) return { isMessage: false }
return { isMessage: messageIdx < processIdx }
})
)
}

0 comments on commit b1308fd

Please sign in to comment.