-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add feature to dry-run preimage (#179)
- Loading branch information
1 parent
daed180
commit 3b20760
Showing
5 changed files
with
142 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { HexString } from '@polkadot/util/types' | ||
import { blake2AsHex } from '@polkadot/util-crypto' | ||
import { hexToU8a } from '@polkadot/util' | ||
|
||
import { Config } from './schema' | ||
import { defaultLogger } from './logger' | ||
import { generateHtmlDiffPreviewFile } from './utils/generate-html-diff' | ||
import { newHeader } from './blockchain/block-builder' | ||
import { openHtml } from './utils/open-html' | ||
import { runTask, taskHandler } from './executor' | ||
import { setStorage } from './utils/set-storage' | ||
import { setup } from './setup' | ||
|
||
export const dryRunPreimage = async (argv: Config) => { | ||
const context = await setup(argv) | ||
|
||
const extrinsic = argv['preimage'] | ||
|
||
const block = context.chain.head | ||
const registry = await block.registry | ||
|
||
const header = await newHeader(block) | ||
|
||
const data = hexToU8a(extrinsic) | ||
const hash = blake2AsHex(data, 256) | ||
|
||
await setStorage(context.chain, { | ||
Preimage: { | ||
PreimageFor: [[[[hash, data.byteLength]], extrinsic]], | ||
StatusFor: [ | ||
[ | ||
[hash], | ||
{ | ||
Requested: { | ||
count: 1, | ||
len: data.byteLength, | ||
}, | ||
}, | ||
], | ||
], | ||
}, | ||
Scheduler: { | ||
Agenda: [ | ||
[ | ||
[block.number + 1], | ||
[ | ||
{ | ||
maybeId: '0x64656d6f637261633a0000000000000000000000000000000000000000000000', | ||
priority: 63, | ||
call: { | ||
Lookup: { | ||
hash: hash, | ||
len: data.byteLength, | ||
}, | ||
}, | ||
origin: { system: { Root: null } }, | ||
}, | ||
], | ||
], | ||
], | ||
Lookup: [[['0x64656d6f637261633a0000000000000000000000000000000000000000000000'], [block.number + 1, 0]]], | ||
}, | ||
}) | ||
|
||
const calls: [string, HexString[]][] = [['Core_initialize_block', [header.toHex()]]] | ||
|
||
for (const inherent of await block.chain.getInherents()) { | ||
calls.push(['BlockBuilder_apply_extrinsic', [inherent]]) | ||
} | ||
|
||
calls.push(['BlockBuilder_finalize_block', []]) | ||
|
||
defaultLogger.info({ preimage: registry.createType('Call', data).toHuman() }, 'Dry run preimage') | ||
|
||
const result = await runTask( | ||
{ | ||
wasm: await block.wasm, | ||
calls, | ||
storage: [], | ||
mockSignatureHost: false, | ||
allowUnresolvedImports: false, | ||
}, | ||
taskHandler(block) | ||
) | ||
|
||
if (result.Error) { | ||
throw new Error(result.Error) | ||
} | ||
|
||
const filePath = await generateHtmlDiffPreviewFile(block, result.Call.storageDiff, hash) | ||
console.log(`Generated preview ${filePath}`) | ||
if (argv['open']) { | ||
openHtml(filePath) | ||
} | ||
|
||
// if dry-run preimage has extrinsic arguments then dry-run extrinsic | ||
// this is usefull to test something after preimage is applied | ||
if (argv['extrinsic']) { | ||
await context.chain.newBlock() | ||
const input = argv['address'] ? { call: argv['extrinsic'], address: argv['address'] } : argv['extrinsic'] | ||
const { outcome, storageDiff } = await context.chain.dryRunExtrinsic(input) | ||
if (outcome.isErr) { | ||
throw new Error(outcome.asErr.toString()) | ||
} else { | ||
defaultLogger.info(outcome.toHuman(), 'dry_run_outcome') | ||
} | ||
|
||
const filePath = await generateHtmlDiffPreviewFile( | ||
context.chain.head, | ||
storageDiff, | ||
blake2AsHex(argv['extrinsic'], 256) | ||
) | ||
console.log(`Generated preview ${filePath}`) | ||
if (argv['open']) { | ||
openHtml(filePath) | ||
} | ||
} | ||
|
||
process.exit(0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters