Skip to content

Commit

Permalink
fix decode cache (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc authored Dec 13, 2023
1 parent 1caee75 commit 8059dbc
Show file tree
Hide file tree
Showing 6 changed files with 3,355 additions and 34 deletions.
6 changes: 1 addition & 5 deletions packages/chopsticks/src/plugins/decode-key/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ export const cli = (y: Argv) => {
}),
async (argv) => {
const context = await setupContext(configSchema.parse(argv))
const { storage, decodedKey } = decodeKey(
await context.chain.head.meta,
context.chain.head,
argv.key as HexString,
)
const { storage, decodedKey } = decodeKey(await context.chain.head.meta, argv.key as HexString)
if (storage && decodedKey) {
console.log(
`${storage.section}.${storage.method}`,
Expand Down
3,301 changes: 3,301 additions & 0 deletions packages/chopsticks/src/plugins/run-block/__snapshots__/index.test.ts.snap

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions packages/chopsticks/src/plugins/run-block/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,38 @@ describe('run-block', () => {

await chain.close()
}, 90000)

it('should work for acala', async () => {
const chain = await setup({
endpoint: 'wss://acala-rpc.aca-api.network',
block: 4851233,
db: !process.env.RUN_TESTS_WITHOUT_DB ? new SqliteDatabase('e2e-tests-db.sqlite') : undefined,
})

const parseBlock = async (n) => {
const block = (await chain.getBlockAt(n))!
const header = await block.header
const parent = header.parentHash.toHex()

const result = await rpc({ chain }, [
{
includeRaw: true,
includeParsed: true,
includeBlockDetails: true,
parent,
block: {
header: header.toJSON(),
extrinsics: await block.extrinsics,
},
},
])
expect(result).toMatchSnapshot()
}

await parseBlock(4851229)
await parseBlock(4851230)
await parseBlock(4851231)

await chain.close()
}, 90000)
})
4 changes: 3 additions & 1 deletion packages/chopsticks/src/plugins/run-block/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ export const rpc = async ({ chain }: Context, [params]: [RunBlockParams]): Promi
const previousLayer = newBlock.storage
newBlock.pushStorageLayer().setAll(raw)

const newBlockMeta = await newBlock.meta

for (const [key, value] of raw) {
if (key === systemEventsKey) {
continue
Expand All @@ -201,7 +203,7 @@ export const rpc = async ({ chain }: Context, [params]: [RunBlockParams]): Promi
obj.raw = { key, value }
}
if (includeParsed) {
const decoded = decodeKeyValue(await newBlock.meta, newBlock, key, value, false)
const decoded = decodeKeyValue(newBlockMeta, key, value, false)
if (decoded) {
obj.parsed = {
section: decoded.section,
Expand Down
26 changes: 8 additions & 18 deletions packages/core/src/utils/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const getCache = (uid: string): LRUCache<HexString, StorageEntry> => {
return _CACHE[uid]
}

const getStorageEntry = (meta: DecoratedMeta, block: Block, key: HexString) => {
const cache = getCache(block.chain.uid)
const getStorageEntry = (meta: DecoratedMeta, key: HexString) => {
const cache = getCache(meta.registry.metadata.hash.toHex())
for (const prefix of cache.keys()) {
if (key.startsWith(prefix))
// update the recency of the cache entry
Expand All @@ -47,12 +47,8 @@ const getStorageEntry = (meta: DecoratedMeta, block: Block, key: HexString) => {
return undefined
}

export const decodeKey = (
meta: DecoratedMeta,
block: Block,
key: HexString,
): { storage?: StorageEntry; decodedKey?: StorageKey } => {
const storage = getStorageEntry(meta, block, key)
export const decodeKey = (meta: DecoratedMeta, key: HexString): { storage?: StorageEntry; decodedKey?: StorageKey } => {
const storage = getStorageEntry(meta, key)
const decodedKey = meta.registry.createType('StorageKey', key)
if (storage) {
decodedKey.setMeta(storage.meta)
Expand All @@ -61,13 +57,7 @@ export const decodeKey = (
return {}
}

export const decodeKeyValue = (
meta: DecoratedMeta,
block: Block,
key: HexString,
value?: HexString | null,
toHuman = true,
) => {
export const decodeKeyValue = (meta: DecoratedMeta, key: HexString, value?: HexString | null, toHuman = true) => {
const res = decodeWellKnownKey(meta.registry, key, value)
if (res) {
return {
Expand All @@ -78,7 +68,7 @@ export const decodeKeyValue = (
}
}

const { storage, decodedKey } = decodeKey(meta, block, key)
const { storage, decodedKey } = decodeKey(meta, key)

if (!storage || !decodedKey) {
logger.warn({ key, value }, 'Failed to decode storage key')
Expand Down Expand Up @@ -144,10 +134,10 @@ export const decodeBlockStorageDiff = async (block: Block, diff: [HexString, Hex
const meta = await block.meta
for (const [key, value] of diff) {
const oldValue = await block.get(key)
const oldDecoded = toStorageObject(decodeKeyValue(meta, block, key, oldValue)) ?? { [key]: oldValue }
const oldDecoded = toStorageObject(decodeKeyValue(meta, key, oldValue)) ?? { [key]: oldValue }
_.merge(oldState, oldDecoded)

const newDecoded = toStorageObject(decodeKeyValue(meta, block, key, value)) ?? { [key]: value }
const newDecoded = toStorageObject(decodeKeyValue(meta, key, value)) ?? { [key]: value }
_.merge(newState, newDecoded)
}
return [oldState, newState]
Expand Down
18 changes: 8 additions & 10 deletions packages/e2e/src/decoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,43 @@ describe('decoder', async () => {
})

it('decode keys', async () => {
expect(decodeKey(await chain.head.meta, chain.head, SYSTEM_ACCOUNT)).toMatchSnapshot()
expect(decodeKey(await chain.head.meta, SYSTEM_ACCOUNT)).toMatchSnapshot()
})

it('decode key-value', async () => {
const meta = await chain.head.meta
const data = { data: { free: 10000000000 } }
const value = meta.registry.createType('AccountInfo', data)
const decoded = decodeKeyValue(meta, chain.head, SYSTEM_ACCOUNT, value.toHex())
const decoded = decodeKeyValue(meta, SYSTEM_ACCOUNT, value.toHex())
expect(decoded).toMatchSnapshot()
expect(toStorageObject(decoded)).toMatchSnapshot()

const ormlAccountData = meta.registry.createType('AccountData', data.data)
const decoded2 = decodeKeyValue(meta, chain.head, TOKENS_ACCOUNTS, ormlAccountData.toHex())
const decoded2 = decodeKeyValue(meta, TOKENS_ACCOUNTS, ormlAccountData.toHex())
expect(decoded2).toMatchSnapshot()
expect(toStorageObject(decoded2)).toMatchSnapshot()

const timestampNow = meta.registry.createType('Moment', data.data)
const decoded3 = decodeKeyValue(meta, chain.head, TIMESTAMPE_NOW, timestampNow.toHex())
const decoded3 = decodeKeyValue(meta, TIMESTAMPE_NOW, timestampNow.toHex())
expect(decoded3).toMatchSnapshot()
expect(toStorageObject(decoded3)).toMatchSnapshot()
})

it('works with well known keys', async () => {
const meta = await chain.head.meta
expect(decodeKeyValue(meta, chain.head, '0x3a636f6465', '0x12345678')).toMatchSnapshot()
expect(decodeKeyValue(meta, '0x3a636f6465', '0x12345678')).toMatchSnapshot()
expect(
decodeKeyValue(
meta,
chain.head,
'0x3a72656c61795f64697370617463685f71756575655f72656d61696e696e675f63617061636974790c0d0000',
'0xaaaa020000001000',
),
).toMatchSnapshot()
expect(decodeKeyValue(meta, chain.head, '0x3a7472616e73616374696f6e5f6c6576656c3a')).toMatchSnapshot()
expect(decodeKeyValue(meta, chain.head, '0x3a65787472696e7369635f696e646578', '0x02000000')).toMatchSnapshot()
expect(decodeKeyValue(meta, '0x3a7472616e73616374696f6e5f6c6576656c3a')).toMatchSnapshot()
expect(decodeKeyValue(meta, '0x3a65787472696e7369635f696e646578', '0x02000000')).toMatchSnapshot()
expect(
decodeKeyValue(
meta,
chain.head,
'0xf5207f03cfdce586301014700e2c2593fad157e461d71fd4c1f936839a5f1f3e63f5a4efb16ffa83d0070000',
'0x0100000043000000',
),
Expand All @@ -70,7 +68,7 @@ describe('decoder', async () => {
const meta = await chain.head.meta
const data = { data: { free: 10000000000 } }
const value = meta.registry.createType('AccountInfo', data)
const decoded = decodeKeyValue(meta, chain.head, SYSTEM_ACCOUNT, value.toHex())
const decoded = decodeKeyValue(meta, SYSTEM_ACCOUNT, value.toHex())
expect(decoded).toMatchSnapshot()
expect(toStorageObject(decoded)).toMatchSnapshot()
await teardown()
Expand Down

0 comments on commit 8059dbc

Please sign in to comment.