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 storage import encode #606

Merged
merged 4 commits into from
Dec 24, 2023
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
20 changes: 16 additions & 4 deletions packages/core/src/utils/set-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,25 @@ function objectToStorageItems(meta: DecoratedMeta, storage: StorageConfig): RawS

if (storageEntry.meta.type.isPlain) {
const key = new StorageKey(meta.registry, [storageEntry])
const type = storageEntry.meta.modifier.isOptional ? `Option<${key.outputType}>` : key.outputType
storageItems.push([key.toHex(), storage ? meta.registry.createType(type, storage).toHex() : null])
if (storageEntry.meta.modifier.isOptional && storage === '0x') {
storageItems.push([key.toHex(), '0x'])
} else {
storageItems.push([
key.toHex(),
storage ? u8aToHex(meta.registry.createType(key.outputType, storage).toU8a()) : null,
])
}
} else {
for (const [keys, value] of storage) {
const key = new StorageKey(meta.registry, [storageEntry, keys])
const type = storageEntry.meta.modifier.isOptional ? `Option<${key.outputType}>` : key.outputType
storageItems.push([key.toHex(), value ? meta.registry.createType(type, value).toHex() : null])
if (storageEntry.meta.modifier.isOptional && value === '0x') {
storageItems.push([key.toHex(), '0x'])
} else {
storageItems.push([
key.toHex(),
value ? u8aToHex(meta.registry.createType(key.outputType, value).toU8a()) : null,
])
}
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions packages/e2e/src/import-storage/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest'
import path from 'path'

import { api, chain, setupApi } from '../helper.js'
import { compactHex } from '@acala-network/chopsticks'
import { overrideStorage, overrideWasm } from '@acala-network/chopsticks/utils/override.js'

setupApi({
Expand All @@ -14,12 +15,21 @@ const sudoKey = '0x5c0d1176a568c1f92944340dbfed9e9c530ebca703c85910e7164cb7d1c9e
describe('import-storage', () => {
it('works', async () => {
const block = await chain.getBlock()
if (!block) throw new Error('block not found')

expect(await block?.get(sudoKey)).toBe('0x6d6f646c6163612f747273790000000000000000000000000000000000000000')
expect(await block.get(sudoKey)).toBe('0x6d6f646c6163612f747273790000000000000000000000000000000000000000')

await overrideStorage(chain, path.join(__dirname, './storage.ok.yml'))

expect(await block?.get(sudoKey)).toBeUndefined()
expect(await block.get(sudoKey)).toBe('0x') // None

{
const key = compactHex((await block.meta).query.stableAsset.pools(0))
expect(await block.get(key)).toBe('0x') // None
}

const key = compactHex((await block.meta).query.timestamp.now())
expect(await block.get(key)).toBe('0x0100000000000000')
})

it('handle errors', async () => {
Expand Down
12 changes: 11 additions & 1 deletion packages/e2e/src/import-storage/storage.ok.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Sudo:
Key: null
Key: 0x

Timestamp:
Now: 1

StableAsset:
Pools:
-
-
- 0
- 0x

TechnicalCommittee:
Members:
Expand Down