Skip to content

Commit

Permalink
Test wasm override (#69)
Browse files Browse the repository at this point in the history
* wasm-override minor fix and test

* rename runtime
  • Loading branch information
ermalkaleci authored Dec 3, 2022
1 parent e140ee3 commit 3db7f76
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
1 change: 1 addition & 0 deletions e2e/blobs/acala-runtime-2101.txt

Large diffs are not rendered by default.

30 changes: 22 additions & 8 deletions e2e/import-storage/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
import { describe, expect, it } from 'vitest'
import path from 'path'

import { chain, env, setupApi } from '../helper'
import { importStorage } from '../../src/utils/import-storage'
import { chain, setupApi } from '../helper'
import { importStorage, overrideWasm } from '../../src/utils/import-storage'

setupApi(env.mandala)
setupApi({
endpoint: 'wss://acala-rpc-1.aca-api.network',
blockHash: '0x663c25dc86521f4b7f74dcbc26224bb0fac40e316e6b0bcf6a51de373f37afac', // 2_000_000
})

const sudoKey = '0x5c0d1176a568c1f92944340dbfed9e9c530ebca703c85910e7164cb7d1c9e47b'

describe('import-storage', () => {
it('works', async () => {
const block = await chain.getBlock()

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

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

expect(await block?.get(sudoKey)).toBeUndefined
})

it('handle errors', async () => {
const notExist = path.join(__dirname, 'does_not_exist.yml')
await expect(importStorage(notExist, chain)).rejects.toThrowError(`File ${notExist} does not exist`)
await expect(importStorage(path.join(__dirname, 'storage.error.pallet.yml'), chain)).rejects.toThrowError(
await expect(importStorage(chain, notExist)).rejects.toThrowError(`File ${notExist} does not exist`)
await expect(importStorage(chain, path.join(__dirname, 'storage.error.pallet.yml'))).rejects.toThrowError(
'Cannot find pallet TTechnicalCommittee'
)
await expect(importStorage(path.join(__dirname, 'storage.error.storage.yml'), chain)).rejects.toThrowError(
await expect(importStorage(chain, path.join(__dirname, 'storage.error.storage.yml'))).rejects.toThrowError(
'Cannot find storage MMembers in pallet TechnicalCommittee'
)
})

it('wasm override works', async () => {
expect(await chain.head.runtimeVersion).toContain({ specVersion: 2096 })
const oldWasm = await chain.head.wasm
await overrideWasm(chain, path.join(__dirname, '../blobs/acala-runtime-2101.txt'))
expect(await chain.head.wasm).not.eq(oldWasm)
expect(await chain.head.runtimeVersion).toContain({ specVersion: 2101 })
const blockNumber = chain.head.number
// can produce blocks
await expect(chain.newBlock()).resolves.toContain({ number: blockNumber + 1 })
})
})
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ const setup = async (argv: Config) => {

tasks.updateListeningPort(listeningPort)

await importStorage(argv['import-storage'], chain)
overrideWasm(argv['wasm-override'], chain)
await importStorage(chain, argv['import-storage'])
await overrideWasm(chain, argv['wasm-override'])

if (argv.genesis) {
// mine 1st block when starting from genesis to set some mock validation data
Expand Down
12 changes: 6 additions & 6 deletions src/utils/import-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ import { existsSync, readFileSync } from 'fs'
import yaml from 'js-yaml'

import { Blockchain } from '../blockchain'
import { StorageValues, setStorage } from './set-storage'
import { defaultLogger } from '../logger'
import { setStorage } from './set-storage'

export const importStorage = async (storage: any, chain: Blockchain) => {
let storageValue
export const importStorage = async (chain: Blockchain, storage?: string | StorageValues) => {
if (storage == null) {
return
}
let storageValue: StorageValues
if (typeof storage === 'string') {
if (!existsSync(storage)) throw Error(`File ${storage} does not exist`)
storageValue = yaml.load(String(readFileSync(storage)))
storageValue = yaml.load(String(readFileSync(storage))) as StorageValues
} else {
storageValue = storage
}
const blockHash = await setStorage(chain, storageValue)
defaultLogger.trace({ blockHash, storage }, 'ImportStorage')
}

export const overrideWasm = async (wasmPath: string | undefined, chain: Blockchain) => {
export const overrideWasm = async (chain: Blockchain, wasmPath?: string) => {
if (wasmPath == null) {
return
}
const wasm = readFileSync(wasmPath)
let wasmHex: string
if (wasm.at(0) === 0x30 && wasm.at(1) === 0x78) {
// starts with 0x
wasmHex = wasm.toString()
wasmHex = wasm.toString().trim()
} else {
wasmHex = '0x' + wasm.toString('hex')
}
Expand Down

0 comments on commit 3db7f76

Please sign in to comment.