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

fix block hash #790

Merged
merged 10 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 10 additions & 6 deletions packages/core/src/blockchain/block-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { HexString } from '@polkadot/util/types'
import { InherentProvider } from './inherent/index.js'
import { StorageLayer, StorageValueKind } from './storage-layer.js'
import { TaskCallResponse } from '../wasm-executor/index.js'
import { blake2AsU8a } from '@polkadot/util-crypto'
import { compactAddLength, hexToU8a, stringToHex, u8aConcat } from '@polkadot/util'
import { compactHex, getCurrentSlot } from '../utils/index.js'
import { defaultLogger, truncate } from '../logger.js'
Expand Down Expand Up @@ -341,9 +342,12 @@ export const buildBlock = async (
callbacks?.onPhaseApplied?.('finalize', resp)
}

const blockData = registry.createType('Block', {
header,
extrinsics: includedExtrinsic,
const allExtrinsics = [...inherents, ...includedExtrinsic]

const mockExtrinsicRoot = blake2AsU8a(u8aConcat(...allExtrinsics), 256)
const finalHeader = registry.createType<Header>('Header', {
...header.toJSON(),
extrinsicsRoot: mockExtrinsicRoot,
})

const storageDiff = await newBlock.storageDiff()
Expand All @@ -355,9 +359,9 @@ export const buildBlock = async (
)
}

const finalBlock = new Block(head.chain, newBlock.number, blockData.hash.toHex(), head, {
header,
extrinsics: [...inherents, ...includedExtrinsic],
const finalBlock = new Block(head.chain, newBlock.number, finalHeader.hash.toHex(), head, {
header: finalHeader,
extrinsics: allExtrinsics,
storage: head.storage,
storageDiff,
})
Expand Down
46 changes: 23 additions & 23 deletions packages/e2e/src/__snapshots__/author.test.ts.snap

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions packages/e2e/src/__snapshots__/chain.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ exports[`chain rpc > getXXX 8`] = `
}
`;

exports[`chain rpc > getXXX 9`] = `"0xa08ebd83c5b4d941bf9c6853c0af2bc6620a1878a5d2ce302d09f40feea8ef98"`;
exports[`chain rpc > getXXX 9`] = `"0x3ee18ba950984f5ed8c6c6d407784c551aea7471ba1000f74998ebd5a3fbc9bf"`;

exports[`chain rpc > getXXX 10`] = `"0xa08ebd83c5b4d941bf9c6853c0af2bc6620a1878a5d2ce302d09f40feea8ef98"`;
exports[`chain rpc > getXXX 10`] = `"0x3ee18ba950984f5ed8c6c6d407784c551aea7471ba1000f74998ebd5a3fbc9bf"`;

exports[`chain rpc > getXXX 11`] = `
{
Expand All @@ -231,7 +231,7 @@ exports[`chain rpc > getXXX 11`] = `
},
],
},
"extrinsicsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extrinsicsRoot": "0xef16718ba6ed955c1c258867f31167879ec11fd68312418c4e92fc5cd1dbacd4",
"number": 3800001,
"parentHash": "0x0df086f32a9c3399f7fa158d3d77a1790830bd309134c5853718141c969299c7",
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down Expand Up @@ -262,7 +262,7 @@ exports[`chain rpc > getXXX 12`] = `
},
],
},
"extrinsicsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extrinsicsRoot": "0xef16718ba6ed955c1c258867f31167879ec11fd68312418c4e92fc5cd1dbacd4",
"number": 3800001,
"parentHash": "0x0df086f32a9c3399f7fa158d3d77a1790830bd309134c5853718141c969299c7",
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down Expand Up @@ -301,7 +301,7 @@ exports[`chain rpc > subscribeNewHeads 1`] = `
]
`;

exports[`chain rpc > subscribeNewHeads 2`] = `"0xa08ebd83c5b4d941bf9c6853c0af2bc6620a1878a5d2ce302d09f40feea8ef98"`;
exports[`chain rpc > subscribeNewHeads 2`] = `"0x3ee18ba950984f5ed8c6c6d407784c551aea7471ba1000f74998ebd5a3fbc9bf"`;

exports[`chain rpc > subscribeNewHeads 3`] = `
[
Expand All @@ -323,7 +323,7 @@ exports[`chain rpc > subscribeNewHeads 3`] = `
},
],
},
"extrinsicsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extrinsicsRoot": "0xef16718ba6ed955c1c258867f31167879ec11fd68312418c4e92fc5cd1dbacd4",
"number": 3800001,
"parentHash": "0x0df086f32a9c3399f7fa158d3d77a1790830bd309134c5853718141c969299c7",
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand All @@ -332,4 +332,4 @@ exports[`chain rpc > subscribeNewHeads 3`] = `
]
`;

exports[`chain rpc > subscribeNewHeads 4`] = `"0x018ba9714f720e785ed94f4c86cee723fe3f16eb53598f6725fac0b4f4ae5955"`;
exports[`chain rpc > subscribeNewHeads 4`] = `"0x1950829c5687d921d786c184dae9eb20ae55d2020b07adafcb1cfb1a5be4212f"`;
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ exports[`chopsticks provider works > handles tx 1`] = `
},
],
},
"extrinsicsRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extrinsicsRoot": "0xb55115c9307159fe4dcd399140b118833bacc31f6a81a0bd4d6531a86ee1b2ff",
"number": 3800001,
"parentHash": "0x0df086f32a9c3399f7fa158d3d77a1790830bd309134c5853718141c969299c7",
"stateRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
Expand Down
22 changes: 20 additions & 2 deletions packages/e2e/src/block.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { afterAll, describe, expect, it } from 'vitest'

import { delay } from './helper.js'
import { delay, testingPairs } from './helper.js'
import networks from './networks.js'

describe('block', async () => {
const { chain, dev, teardown } = await networks.acala()
const { chain, dev, teardown, api } = await networks.acala()

afterAll(async () => {
await teardown()
Expand All @@ -24,4 +24,22 @@ describe('block', async () => {

await Promise.all(promises)
})

it('block hash are unique', async () => {
const { alice } = testingPairs()
await dev.setStorage({
System: {
Account: [[[alice.address], { providers: 1, data: { free: 10 * 1e12 } }]],
},
})

const head = chain.head
const newBlock = await chain.newBlock()
expect(newBlock.hash).toMatchInlineSnapshot(`"0xfca2b29b2ef3e018b87ce56dfa6200d973201222cbb67dde3aca6db905b440cc"`)

await chain.setHead(head)
await api.tx.system.remark('test').signAndSend(alice)
const newBlock2 = await chain.newBlock()
expect(newBlock2.hash).toMatchInlineSnapshot(`"0xbf570d4c473241efbc4ffe08145e074e3f22b1f40dc2fac8108912bec8ac845e"`)
})
})
20 changes: 8 additions & 12 deletions packages/e2e/src/build-block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,14 @@ describe.runIf(process.env.CI || process.env.RUN_ALL).each([
await teardownAll()
})

it(
'build blocks',
async () => {
const { chain, ws, teardown } = await setup()
storage && (await ws.send('dev_setStorage', [storage]))
const blockNumber = chain.head.number
await ws.send('dev_newBlock', [{ count: 2 }])
expect(chain.head.number).eq(blockNumber + 2)
await teardown()
},
{ timeout: 300_000, retry: 1 },
)
it('build blocks', { timeout: 300_000, retry: 1 }, async () => {
const { chain, ws, teardown } = await setup()
storage && (await ws.send('dev_setStorage', [storage]))
const blockNumber = chain.head.number
await ws.send('dev_newBlock', [{ count: 2 }])
expect(chain.head.number).eq(blockNumber + 2)
await teardown()
})

it('build block using unsafeBlockHeight', async () => {
const { chain, ws, teardown } = await setup()
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/src/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ describe('dev rpc', async () => {
expect((await api.rpc.chain.getBlockHash()).toHex()).toBe(hash)
await dev.setHead(blockNumber - 3)
expect((await api.rpc.chain.getBlockHash()).toHex()).toMatchInlineSnapshot(
'"0xfab81f03d3275189a7dc02b0e4fabfab3916ff9a729ba3ec6ad84e029f0a74e7"',
`"0xc67fac55f8f2e1ba8f3b7d277d3097fd796216998bd6b1bfd8fd9aed87da579d"`,
)
await dev.setHead(-3)
expect((await api.rpc.chain.getBlockHash()).toHex()).toMatchInlineSnapshot(
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { config as dotenvConfig } from 'dotenv'

import { SetupOption, setupContext } from '@acala-network/chopsticks-testing'

dotenvConfig
dotenvConfig()

const endpoints = {
polkadot: ['wss://rpc-polkadot.luckyfriday.io'],
Expand Down
6 changes: 3 additions & 3 deletions packages/e2e/src/relaychain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ describe('relaychain dev rpc', async () => {

it('build blocks', async () => {
expect(await dev.newBlock()).toMatchInlineSnapshot(
'"0xbc578eb38288c517a8b9d733af1a0a5283698b24c2360a15bbade3a325151ab6"',
`"0x944830e4d3cf6b4c803c23d2ddd2dc9efa5cdaec9d3289107a828638eafdc58f"`,
)
expect(await dev.newBlock()).toMatchInlineSnapshot(
'"0x25bbf7510c57d5e7e26c1dd68c77cbcf25e4c08f5e51fd18d9c3df98507df0fc"',
`"0xb823303eb5c79ef4e865f49adbd39f8607d699ac03b30d256441989212de866e"`,
)
expect(await dev.newBlock()).toMatchInlineSnapshot(
'"0xd580fd6d3c17389c2572ef81a21c9be79abb833d228896a7c676799a9def5b58"',
`"0xd37527c814d86869b308f16f3eb6126f95da3b8f5a1a782f41e356454215d916"`,
)
})
})
4 changes: 2 additions & 2 deletions packages/e2e/src/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('storage', () => {

tick = next()
expect(await dev.newBlock()).toMatchInlineSnapshot(
'"0xa08ebd83c5b4d941bf9c6853c0af2bc6620a1878a5d2ce302d09f40feea8ef98"',
`"0x3ee18ba950984f5ed8c6c6d407784c551aea7471ba1000f74998ebd5a3fbc9bf"`,
)
await tick

Expand All @@ -84,7 +84,7 @@ describe('storage', () => {
unsub()

expect(await dev.newBlock()).toMatchInlineSnapshot(
'"0x018ba9714f720e785ed94f4c86cee723fe3f16eb53598f6725fac0b4f4ae5955"',
`"0x1950829c5687d921d786c184dae9eb20ae55d2020b07adafcb1cfb1a5be4212f"`,
)

await delay(100)
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export const sendTransaction = async (tx: Promise<SubmittableExtrinsic<'promise'
const signed = await tx
const deferred = defer<Codec[]>()
await signed.send((status) => {
logger.debug('tranaction status: ', status.status.toHuman())
logger.debug({ status: status.status.toHuman() }, 'Transaction status')
if (status.isInBlock || status.isFinalized) {
deferred.resolve(status.events)
}
Expand Down
8 changes: 4 additions & 4 deletions packages/web-test/tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test.describe('index', () => {
})
return block.result.hash
})
expect(savedHash).toBe('0x6b81a9a7fabbe32c1e685b944c8f1afd06be7e58ae48bb8d5ac50cc761d9bb77')
expect(savedHash).toBe('0x7403a6c53702bfbace9737a5b3cc84fd02d5f8b30259e718455738ae841d992b')
})

test('dry run extrinsic', async ({ page }) => {
Expand Down Expand Up @@ -63,14 +63,14 @@ test.describe('index', () => {
expect(hightestBlock).toEqual(
expect.objectContaining({
number: 4_000_002,
hash: '0xdd1d5206ce64d643e262f0bdc351147e2ba9e20846fdf78c9c5855ab6e2bc0ca',
hash: '0x9db1dc9525d7574ad6aed6b4302c9fb5069c5cd35288d97ebc78a58782f57b0d',
}),
)
const blockByNumber = await page.evaluate(() => globalThis.chain.db?.queryBlockByNumber(4_000_001))
expect(blockByNumber).toEqual(
expect.objectContaining({
number: 4_000_001,
hash: '0x6b81a9a7fabbe32c1e685b944c8f1afd06be7e58ae48bb8d5ac50cc761d9bb77',
hash: '0x7403a6c53702bfbace9737a5b3cc84fd02d5f8b30259e718455738ae841d992b',
}),
)
const blocksCount = await page.evaluate(() => globalThis.chain.db?.blocksCount())
Expand All @@ -87,7 +87,7 @@ test.describe('index', () => {
expect(hightestBlock).toEqual(
expect.objectContaining({
number: 4_000_001,
hash: '0x6b81a9a7fabbe32c1e685b944c8f1afd06be7e58ae48bb8d5ac50cc761d9bb77',
hash: '0x7403a6c53702bfbace9737a5b3cc84fd02d5f8b30259e718455738ae841d992b',
}),
)
}
Expand Down
Loading