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

View function poc - do not merge #153

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions executor/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub struct TaskCall {
pub struct CallResponse {
result: HexString,
storage_diff: Vec<(HexString, Option<HexString>)>,
accessed_storage_keys: Vec<HexString>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -101,6 +102,8 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
.unwrap();
let mut ret: Result<Vec<u8>, String> = Ok(Vec::new());

let mut keys: Vec<HexString> = Vec::new();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to deupe this


for (call, params) in task.calls.as_ref().unwrap() {
let mut vm = runtime_host::run(runtime_host::Config {
virtual_machine: vm_proto.clone(),
Expand All @@ -121,6 +124,9 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
}
RuntimeHostVm::StorageGet(req) => {
let key = HexString(req.key().as_ref().to_vec());

keys.push(key.clone());
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved

let key = serde_wasm_bindgen::to_value(&key).map_err(|e| e.to_string())?;
let value = js.get_storage(key).await;
let value = if value.is_string() {
Expand Down Expand Up @@ -202,6 +208,7 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
TaskResponse::Call(CallResponse {
result: HexString(ret),
storage_diff: diff,
accessed_storage_keys: keys,
})
}))
}
Expand Down
4 changes: 4 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export class Api {
return this.#provider.disconnect()
}

get provider() {
return this.#provider
}

get isReady() {
if (this.#provider instanceof WsProvider) {
return this.#provider.isReady
Expand Down
26 changes: 26 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { dryRun } from './dry-run'
import { runBlock } from './run-block'
import { setup } from './setup'
import { setupWithServer } from './setup-with-server'
import { nonce } from './nonce'

const processConfig = (path: string) => {
const configFile = readFileSync(path, 'utf8')
Expand Down Expand Up @@ -110,6 +111,31 @@ yargs(hideBin(process.argv))
await dryRun(processArgv(argv))
}
)
.command(
'nonce',
'get nonce',
(yargs) =>
yargs.options({
...defaultOptions,
'account-id': {
desc: 'Account id hex string',
string: true,
},
'output-path': {
desc: 'File path to print output',
string: true,
},
html: {
desc: 'Generate html with storage diff',
},
open: {
desc: 'Open generated html',
},
}),
async (argv) => {
await nonce(processArgv(argv))
}
)
.command(
'dev',
'Dev mode',
Expand Down
48 changes: 48 additions & 0 deletions src/nonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ApiPromise } from '@polkadot/api'
import { TypeRegistry } from '@polkadot/types'
import { hexToU8a } from '@polkadot/util'

import { Config } from './schema'
import { runTask, taskHandler } from './executor'
import { setup } from './setup'

export const nonce = async (argv: Config) => {
const accountId = argv['accountId'] as `0x${string}`

const context = await setup(argv)
const wasm = await context.chain.head.wasm
const block = context.chain.head
const parent = await block.parentBlock

const getNonce = async () => {
const result = await runTask(
{
wasm,
calls: [['AccountNonceApi_account_nonce', accountId]],
storage: [],
mockSignatureHost: false,
allowUnresolvedImports: false,
},
taskHandler(parent)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not correct. this need to be head

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be context.chain.head.call('AccountNonceApi_account_nonce', accountId)

)
if (result.Error) {
throw new Error(result.Error)
}

const registry = new TypeRegistry();
return {
result: registry.createType('u64', hexToU8a(result.Call.result)).toNumber(),
accessedStorageKeys: result.Call.accessedStorageKeys,
}
}

const { accessedStorageKeys } = await getNonce()

const api = await ApiPromise.create({ provider: context.api.provider })
const unsub = await api.rpc.state.subscribeStorage(accessedStorageKeys, async (_changes) => {
const nonce = await getNonce();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to resub the new storages just in case it is accessing something else

console.log('>>> nonce:', nonce.result)
})

// process.exit(0)
}