-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: load blocks from db * fix load blocks * fix: make tests independent * fix: override storage after load blocks * fix: storage * fix: load latest block only * feat: resume using block number hash
- Loading branch information
Showing
6 changed files
with
91 additions
and
8 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
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 |
---|---|---|
@@ -1,29 +1,74 @@ | ||
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 () => { | ||
const buildBlocks = async () => { | ||
// save blocks | ||
const acala = await networks.acala({ db: resolve(tmpdir(), 'db.sqlite') }) | ||
const { chain, dev } = acala | ||
await dev.newBlock({ count: 2 }) | ||
const head = await chain.getBlockAt(chain.head.number) | ||
const savedHeadHash = head?.hash | ||
await acala.teardown() | ||
}) | ||
|
||
return savedHeadHash | ||
} | ||
|
||
it('saved blocks data', async () => { | ||
const acala = await networks.acala({ db: resolve(tmpdir(), 'db.sqlite') }) | ||
const { chain, dev } = acala | ||
await dev.newBlock({ count: 2 }) | ||
|
||
const numberOfBlocks = await chain.db!.getRepository('Block').count() | ||
expect(numberOfBlocks).toEqual(2) | ||
|
||
const block = await chain.getBlockAt(chain.head.number) | ||
const blockData = await chain.db!.getRepository('Block').findOne({ where: { number: chain.head.number } }) | ||
|
||
assert(block && blockData, 'block and blockData should be defined') | ||
expect(blockData.hash).toEqual(block.hash) | ||
expect(JSON.stringify(blockData.header)).toEqual(JSON.stringify(block.header)) | ||
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())) | ||
|
||
await acala.teardown() | ||
}) | ||
|
||
it('load chain using the latest saved block', async () => { | ||
const savedHeadHash = await buildBlocks() | ||
|
||
// load block | ||
const newAcala = await networks.acala({ db: resolve(tmpdir(), 'db.sqlite'), resume: true }) | ||
const newHeadNumber = newAcala.chain.head.number | ||
const loadedHead = await newAcala.chain.getBlockAt(newHeadNumber) | ||
|
||
expect(loadedHead?.hash).toEqual(savedHeadHash) | ||
await newAcala.teardown() | ||
}) | ||
|
||
it('load chain using a block number', async () => { | ||
await buildBlocks() | ||
|
||
// load blocks | ||
const newAcala = await networks.acala({ db: resolve(tmpdir(), 'db.sqlite'), resume: 3000001 }) | ||
const newHeadNumber = newAcala.chain.head.number | ||
|
||
expect(newHeadNumber).toEqual(3000001) | ||
await newAcala.teardown() | ||
}) | ||
|
||
it('load chain using a block hash', async () => { | ||
const savedHeadHash = await buildBlocks() | ||
|
||
// load blocks | ||
const newAcala = await networks.acala({ db: resolve(tmpdir(), 'db.sqlite'), resume: savedHeadHash }) | ||
const newHeadNumber = newAcala.chain.head.number | ||
const loadedHead = await newAcala.chain.getBlockAt(newHeadNumber) | ||
|
||
expect(loadedHead?.hash).toEqual(savedHeadHash) | ||
await newAcala.teardown() | ||
}) | ||
}) |
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