Skip to content

Commit

Permalink
fix storage import encode (#606)
Browse files Browse the repository at this point in the history
* fix storage import encode

* fix

* handle null

* cleanup
  • Loading branch information
ermalkaleci authored Dec 24, 2023
1 parent a45e5d0 commit 78d11c8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
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

0 comments on commit 78d11c8

Please sign in to comment.