forked from Joystream/hydra
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hydra-processor): string-based ranges in manifest (Joystream#347)
* refactor(hydra-processor): refactor event queue for processing affects: @dzlzv/hydra-processor, sample * refactor(hydra-processor): block-based processing of events affects: @dzlzv/hydra-processor, sample, sample-mappings events are processed in blocks, so that the transaction granularity always corresponds to the block-level * feat(hydra-processor): support block ranges for mappings affects: @dzlzv/hydra-processor * test(sample): add hooks to sample affects: sample, sample-mappings * test(hydra-processor): add validation and tests for handlers in manifest * test(hydra-e2e-tests): add e2e-tests for ranges and block hooks affects: hydra-e2e-tests * feat(hydra-processor): support semiopen ranges affects: hydra-e2e-tests, @dzlzv/hydra-processor support mapping ranges defined as strings, e.g. (3,] or (3, 34]
- Loading branch information
Showing
55 changed files
with
2,482 additions
and
949 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Export here all the event handler functions | ||
// so that the indexer picks them up | ||
// export { balancesTransfer as balances_Transfer } from './transfer' | ||
export { balancesTransfer, timestampCall, preHook, postHook } from './mappings' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { DatabaseManager } from '@dzlzv/hydra-db-utils' | ||
import BN from 'bn.js' | ||
import { SubstrateEvent } from '@dzlzv/hydra-common' | ||
|
||
import { Transfer } from '../generated/graphql-server/src/modules/transfer/transfer.model' | ||
import { BlockTimestamp } from '../generated/graphql-server/src/modules/block-timestamp/block-timestamp.model' | ||
import { | ||
BlockHook, | ||
HookType, | ||
} from '../generated/graphql-server/src/modules/block-hook/block-hook.model' | ||
|
||
// run 'NODE_URL=<RPC_ENDPOINT> EVENTS=<comma separated list of events> yarn codegen:mappings-types' | ||
// to genenerate typescript classes for events, such as Balances.TransferEvent | ||
import { Balances, Timestamp } from './generated/types' | ||
|
||
// positional arguments | ||
export async function balancesTransfer( | ||
store: DatabaseManager, | ||
event: Balances.TransferEvent | ||
) { | ||
const transfer = new Transfer() | ||
transfer.from = Buffer.from(event.data.accountIds[0].toHex()) | ||
transfer.to = Buffer.from(event.data.accountIds[1].toHex()) | ||
transfer.value = event.data.balance.toBn() | ||
transfer.block = event.ctx.blockNumber | ||
transfer.comment = `Transferred ${transfer.value} from ${transfer.from} to ${transfer.to}` | ||
transfer.insertedAt = new Date() | ||
await store.save<Transfer>(transfer) | ||
} | ||
|
||
// context argument | ||
export async function timestampCall({ | ||
store, | ||
event, | ||
}: { | ||
store: DatabaseManager | ||
event: SubstrateEvent | ||
}) { | ||
const call = new Timestamp.SetCall(event) | ||
const block = new BlockTimestamp() | ||
block.timestamp = call.args.now.toBn() | ||
block.blockNumber = new BN(call.ctx.blockNumber) | ||
|
||
await store.save<BlockTimestamp>(block) | ||
} | ||
|
||
export async function preHook({ | ||
block: { blockNumber }, | ||
store, | ||
}: { | ||
block: { blockNumber: BN } | ||
store: DatabaseManager | ||
}) { | ||
const hook = new BlockHook() | ||
hook.blockNumber = blockNumber | ||
hook.type = HookType.PRE | ||
await store.save<BlockHook>(hook) | ||
} | ||
|
||
export async function postHook({ | ||
block: { blockNumber }, | ||
store, | ||
}: { | ||
block: { blockNumber: BN } | ||
store: DatabaseManager | ||
}) { | ||
const hook = new BlockHook() | ||
hook.blockNumber = blockNumber | ||
hook.type = HookType.POST | ||
await store.save<BlockHook>(hook) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pWaitFor from 'p-wait-for' | ||
import { expect } from 'chai' | ||
import { HOOKS } from './api/graphql-queries' | ||
import { getGQLClient, getProcessorStatus } from './api/processor-api' | ||
|
||
const hooksTo = 4 | ||
|
||
describe('end-to-end hook tests', () => { | ||
let hooks: { type: string; blockNumber: number }[] | ||
|
||
before(async () => { | ||
// wait until the indexer indexes the block and the processor picks it up | ||
await pWaitFor( | ||
async () => { | ||
return (await getProcessorStatus()).lastCompleteBlock > hooksTo | ||
}, | ||
{ interval: 50 } | ||
) | ||
hooks = ( | ||
await getGQLClient().request<{ | ||
blockHooks: { type: string; blockNumber: number }[] | ||
}>(HOOKS) | ||
).blockHooks | ||
console.log(`Executed hooks: ${JSON.stringify(hooks, null, 2)}`) | ||
}) | ||
|
||
it('finds pre hooks', async () => { | ||
const preHooks = hooks | ||
.filter((h) => h.type === 'PRE') | ||
.map((h) => h.blockNumber) | ||
expect(preHooks).to.have.members(['1', '2']) | ||
}) | ||
|
||
it('finds post hooks', async () => { | ||
const postHooks = hooks | ||
.filter((h) => h.type === 'POST') | ||
.map((h) => h.blockNumber) | ||
expect(postHooks).to.have.members(['2', '3']) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { BlockContext } from '../queue' | ||
|
||
export interface IMappingExecutor { | ||
executeBlock( | ||
blockCtx: BlockContext, | ||
onSuccess: (ctx: BlockContext) => Promise<void> | ||
): Promise<void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { DatabaseManager } from '@dzlzv/hydra-db-utils' | ||
import { BlockContext, MappingContext } from '../queue' | ||
import { MappingHandler } from '../start/manifest' | ||
|
||
export interface StoreContext { | ||
store: DatabaseManager | ||
} | ||
|
||
export type BlockHookContext = StoreContext & BlockContext | ||
|
||
export type EventContext = StoreContext & MappingContext | ||
|
||
export type ExecContext = BlockHookContext | EventContext | ||
|
||
export interface BlockMappings { | ||
pre: MappingHandler[] | ||
post: MappingHandler[] | ||
mappings: MappingHandler[] | ||
} | ||
|
||
export interface IMappingsLookup { | ||
lookupHandlers(ctx: BlockContext): BlockMappings | ||
|
||
call(handler: MappingHandler, ctx: ExecContext): Promise<void> | ||
} |
Oops, something went wrong.