Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: load blocks from db #392

Merged
merged 8 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/chopsticks/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const commands = yargs(hideBin(process.argv))
desc: 'Max memory block count',
number: true,
},
resume: {
qiweiii marked this conversation as resolved.
Show resolved Hide resolved
qiweiii marked this conversation as resolved.
Show resolved Hide resolved
desc: 'Resume from the lastest block saved in the db, note this will override the block option',
},
}),
async (argv) => {
await setupWithServer(argv as Config)
Expand Down
11 changes: 11 additions & 0 deletions packages/chopsticks/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './utils/tunnel'
import { BlockEntity } from '@acala-network/chopsticks-core/db/entities'
import { Config } from './schema'
import { HexString } from '@polkadot/util/types'
import { overrideStorage, overrideWasm } from './utils/override'
Expand Down Expand Up @@ -34,5 +35,15 @@ export const setupContext = async (argv: Config, overrideParent = false) => {
await overrideStorage(chain, argv['import-storage'], at)
await overrideWasm(chain, argv['wasm-override'], at)

// load blocks from db
qiweiii marked this conversation as resolved.
Show resolved Hide resolved
if (chain.db && argv.resume) {
const blocks = await chain.db.getRepository(BlockEntity).find({ where: {}, order: { number: 'asc' } })
qiweiii marked this conversation as resolved.
Show resolved Hide resolved
let head
for (const block of blocks) {
head = await chain.loadBlockFromDB(block.number)
}
await chain.setHead(head)
}

return { chain }
}
1 change: 1 addition & 0 deletions packages/chopsticks/src/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const configSchema = z
'registered-types': z.any().optional(),
'runtime-log-level': z.number().min(0).max(5).optional(),
'offchain-worker': z.boolean().optional(),
resume: z.boolean().optional(),
})
.strict()

Expand Down
29 changes: 21 additions & 8 deletions packages/e2e/src/blocks-save.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { afterAll, assert, describe, expect, it } from 'vitest'
import { assert, describe, expect, it } from 'vitest'
import { resolve } from 'node:path'
import { tmpdir } from 'node:os'
import networks from './networks'

describe('block-save', async () => {
const acala = await networks.acala({ db: resolve(tmpdir(), 'db.sqlite') })
const { chain, dev } = acala

afterAll(async () => {
await acala.teardown()
})
let savedBlockHash: string

it('saved blocks data', async () => {
await dev.newBlock({ count: 2 })
const acala = await networks.acala({ db: resolve(tmpdir(), 'testdb.sqlite') })
const { chain, dev } = acala
const blockNumber = chain.head.number

await dev.newBlock({ count: 2 })
expect(chain.head.number).eq(blockNumber + 2)
const numberOfBlocks = await chain.db!.getRepository('Block').count()
expect(numberOfBlocks).toEqual(2)

Expand All @@ -25,5 +24,19 @@ describe('block-save', async () => {
expect(blockData.parentHash).toEqual((await block.parentBlock)!.hash)
expect(JSON.stringify(blockData.extrinsics)).toEqual(JSON.stringify(await block.extrinsics))
expect(JSON.stringify(blockData.storageDiff)).toEqual(JSON.stringify(await block.storageDiff()))

savedBlockHash = block.hash

await acala.teardown()
})

it('load chain from the saved blocks', async () => {
const acala = await networks.acala({ db: resolve(tmpdir(), 'testdb.sqlite'), resume: true })
qiweiii marked this conversation as resolved.
Show resolved Hide resolved
const { chain } = acala
const head = await chain.getBlockAt(chain.head.number)

expect(head?.hash).toEqual(savedBlockHash)

await acala.teardown()
})
})
3 changes: 3 additions & 0 deletions packages/testing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type SetupOption = {
timeout?: number
port?: number
maxMemoryBlockCount?: number
resume?: boolean
}

export type SetupConfig = Config & {
Expand All @@ -40,6 +41,7 @@ export const createConfig = ({
timeout,
port,
maxMemoryBlockCount,
resume,
}: SetupOption): SetupConfig => {
// random port if not specified
port = port ?? Math.floor(Math.random() * 10000) + 10000
Expand All @@ -53,6 +55,7 @@ export const createConfig = ({
db,
'wasm-override': wasmOverride,
timeout,
resume: resume ?? false,
}
return config
}
Expand Down