Skip to content

Commit

Permalink
refactor(cu): use SU to load all process meta in lieu of gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Nov 8, 2023
1 parent 00ee9f3 commit 8234f46
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 59 deletions.
4 changes: 3 additions & 1 deletion servers/cu/src/domain/client/ao-su.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,13 @@ export const loadMessagesWith = ({ fetch, SU_URL, logger: _logger, pageSize }) =
.toPromise()
}

export const loadProcessBlockWith = ({ fetch, SU_URL }) => {
export const loadProcessWith = ({ fetch, SU_URL }) => {
return async (processId) => {
return fetch(`${SU_URL}/processes/${processId}`, { method: 'GET' })
.then(res => res.json())
.then(applySpec({
owner: path(['owner', 'address']),
tags: path(['tags']),
block: applySpec({
height: path(['block', 'height']),
/**
Expand Down
10 changes: 8 additions & 2 deletions servers/cu/src/domain/dal.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,15 @@ export const loadMessagesSchema = z.function()
}, { message: 'Value must implement the iteration protocol' })
))

export const loadProcessBlockSchema = z.function()
export const loadProcessSchema = z.function()
.args(z.string().min(1))
.returns(z.promise(z.object({ block: rawBlockSchema })))
.returns(z.promise(
z.object({
owner: z.string().min(1),
tags: z.array(rawTagSchema),
block: rawBlockSchema
})
))

export const loadTimestampSchema = z.function()
.returns(z.promise(z.object({
Expand Down
2 changes: 1 addition & 1 deletion servers/cu/src/domain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const createApis = (ctx) => {
findLatestEvaluation: PouchDbClient.findLatestEvaluationWith({ pouchDb, logger }),
saveEvaluation: PouchDbClient.saveEvaluationWith({ pouchDb, logger }),
loadTimestamp: AoSuClient.loadTimestampWith({ fetch: ctx.fetch, SU_URL: ctx.SEQUENCER_URL, logger }),
loadProcessBlock: AoSuClient.loadProcessBlockWith({ fetch: ctx.fetch, SU_URL: ctx.SEQUENCER_URL, logger }),
loadProcess: AoSuClient.loadProcessWith({ fetch: ctx.fetch, SU_URL: ctx.SEQUENCER_URL, logger }),
loadMessages: AoSuClient.loadMessagesWith({ fetch: ctx.fetch, SU_URL: ctx.SEQUENCER_URL, pageSize: 50, logger }),
logger
})
Expand Down
23 changes: 5 additions & 18 deletions servers/cu/src/domain/lib/loadProcess.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { Rejected, Resolved, fromPromise, of } from 'hyper-async'
import { F, T, always, applySpec, assoc, cond, equals, includes, is, isNotNil, mergeRight, path, reduce } from 'ramda'
import { F, T, always, assoc, cond, equals, includes, is, isNotNil, mergeRight, reduce } from 'ramda'
import { z } from 'zod'

import { findLatestEvaluationSchema, findProcessSchema, loadProcessBlockSchema, loadTransactionMetaSchema, saveProcessSchema } from '../dal.js'
import { findLatestEvaluationSchema, findProcessSchema, loadProcessSchema, saveProcessSchema } from '../dal.js'
import { rawBlockSchema, rawTagSchema } from '../model.js'
import { parseTags } from './utils.js'

function getProcessMetaWith ({ loadTransactionMeta, loadProcessBlock, findProcess, saveProcess, logger }) {
loadTransactionMeta = fromPromise(loadTransactionMetaSchema.implement(loadTransactionMeta))
function getProcessMetaWith ({ loadProcess, findProcess, saveProcess, logger }) {
findProcess = fromPromise(findProcessSchema.implement(findProcess))
saveProcess = fromPromise(saveProcessSchema.implement(saveProcess))
loadProcessBlock = fromPromise(loadProcessBlockSchema.implement(loadProcessBlock))
loadProcess = fromPromise(loadProcessSchema.implement(loadProcess))

const checkTag = (name, pred) => (tags) => pred(tags[name])
? Resolved(tags)
Expand All @@ -24,11 +23,7 @@ function getProcessMetaWith ({ loadTransactionMeta, loadProcessBlock, findProces
* for now, just loading Block, since that's the only bit that doesn't finalize
*/
function loadFromChainAndSu (processId) {
return loadTransactionMeta(processId)
.map(applySpec({
owner: path(['owner', 'address']),
tags: path(['tags'])
}))
return loadProcess(processId)
/**
* Verify the process by examining the tags
*/
Expand All @@ -52,14 +47,6 @@ function getProcessMetaWith ({ loadTransactionMeta, loadProcessBlock, findProces
logger.tap('Verified process. Saving to db...')
)
)
/**
* Fetch block metadata from SU
* and merge with the process meta from the gateway
*/
.chain(process =>
loadProcessBlock(processId)
.map(mergeRight(process))
)
/**
* Attempt to save to the db
*/
Expand Down
59 changes: 22 additions & 37 deletions servers/cu/src/domain/lib/loadProcess.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ describe('loadProcess', () => {
findProcess: async () => { throw { status: 404 } },
saveProcess: async () => PROCESS,
findLatestEvaluation: async () => { throw { status: 404 } },
loadTransactionMeta: async (id) => ({
owner: { address: 'woohoo' },
tags
}),
loadProcessBlock: async (id) => {
loadProcess: async (id) => {
assert.equal(id, PROCESS)
return {
owner: 'woohoo',
tags,
block: { height: 123, timestamp: 1697574792000 }
}
},
Expand Down Expand Up @@ -75,8 +73,7 @@ describe('loadProcess', () => {
}),
saveProcess: async () => assert.fail('should not save if found in db'),
findLatestEvaluation: async () => { throw { status: 404 } },
loadTransactionMeta: async (_id) => assert.fail('should not load transaction meta if found in db'),
loadProcessBlock: async (_id) => assert.fail('should not load process block if found in db'),
loadProcess: async (_id) => assert.fail('should not load process block if found in db'),
logger
})

Expand Down Expand Up @@ -128,11 +125,9 @@ describe('loadProcess', () => {
assert.equal(to, 'sortkey-123')
return cachedEvaluation
},
loadTransactionMeta: async (_id) => ({
owner: { address: 'woohoo' },
tags
}),
loadProcessBlock: async (id) => ({
loadProcess: async (id) => ({
owner: 'woohoo',
tags,
block: { height: 123, timestamp: 1697574792000 }
}),
logger
Expand Down Expand Up @@ -165,11 +160,9 @@ describe('loadProcess', () => {
return PROCESS
},
findLatestEvaluation: async () => { throw { status: 404 } },
loadTransactionMeta: async (_id) => ({
owner: { address: 'woohoo' },
tags
}),
loadProcessBlock: async (id) => ({
loadProcess: async (id) => ({
owner: 'woohoo',
tags,
block: { height: 123, timestamp: 1697574792000 }
}),
logger
Expand All @@ -189,11 +182,9 @@ describe('loadProcess', () => {
findProcess: async () => { throw { status: 404 } },
saveProcess: async () => { throw { status: 409 } },
findLatestEvaluation: async () => { throw { status: 404 } },
loadTransactionMeta: async (_id) => ({
owner: { address: 'woohoo' },
tags
}),
loadProcessBlock: async (id) => ({
loadProcess: async (id) => ({
owner: 'woohoo',
tags,
block: { height: 123, timestamp: 1697574792000 }
}),
logger
Expand All @@ -211,15 +202,13 @@ describe('loadProcess', () => {
findProcess: async () => { throw { status: 404 } },
saveProcess: async () => PROCESS,
findLatestEvaluation: async () => { throw { status: 404 } },
loadTransactionMeta: async (_id) => ({
owner: { address: 'woohoo' },
loadProcess: async (id) => ({
owner: 'woohoo',
tags: [
{ name: 'Not-Contract-Src', value: 'foobar' },
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'ao-type', value: 'process' }
]
}),
loadProcessBlock: async (id) => ({
],
block: { height: 123, timestamp: 1697574792000 }
}),
logger
Expand All @@ -235,15 +224,13 @@ describe('loadProcess', () => {
findProcess: async () => { throw { status: 404 } },
saveProcess: async () => PROCESS,
findLatestEvaluation: async () => { throw { status: 404 } },
loadTransactionMeta: async (_id) => ({
owner: { address: 'woohoo' },
loadProcess: async (id) => ({
owner: 'woohoo',
tags: [
{ name: 'Contract-Src', value: 'foobar' },
{ name: 'Data-Protocol', value: 'not_ao' },
{ name: 'ao-type', value: 'process' }
]
}),
loadProcessBlock: async (id) => ({
],
block: { height: 123, timestamp: 1697574792000 }
}),
logger
Expand All @@ -259,15 +246,13 @@ describe('loadProcess', () => {
findProcess: async () => { throw { status: 404 } },
saveProcess: async () => PROCESS,
findLatestEvaluation: async () => { throw { status: 404 } },
loadTransactionMeta: async (_id) => ({
owner: { address: 'woohoo' },
loadProcess: async (id) => ({
owner: 'woohoo',
tags: [
{ name: 'Contract-Src', value: 'foobar' },
{ name: 'Data-Protocol', value: 'ao' },
{ name: 'ao-type', value: 'message' }
]
}),
loadProcessBlock: async (id) => ({
],
block: { height: 123, timestamp: 1697574792000 }
}),
logger
Expand Down

0 comments on commit 8234f46

Please sign in to comment.