Skip to content

Commit

Permalink
fix prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Jun 26, 2023
1 parent 161471c commit 8450788
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
6 changes: 3 additions & 3 deletions executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ fn setup_console() {
#[wasm_bindgen(typescript_custom_section)]
const _: &'static str = r#"
import { HexString } from '@polkadot/util/types';
interface JsCallback {
export interface JsCallback {
getStorage: (key: HexString) => Promise<string | undefined>
getStateRoot: () => Promise<string>
getNextKey: (key: HexString) => Promise<string | undefined>
getNextKey: (prefix: HexString, key: HexString) => Promise<string | undefined>
}
"#;

Expand All @@ -40,7 +40,7 @@ extern "C" {
pub async fn get_state_root(this: &JsCallback) -> JsValue;

#[wasm_bindgen(structural, method, js_name = "getNextKey")]
pub async fn get_next_key(this: &JsCallback, key: JsValue) -> JsValue;
pub async fn get_next_key(this: &JsCallback, prefix: JsValue, key: JsValue) -> JsValue;
}

#[wasm_bindgen]
Expand Down
7 changes: 6 additions & 1 deletion executor/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,16 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
// root_calculation, skip
req.inject_key(None::<Vec<_>>.map(|x| x.into_iter()))
} else {
let prefix = HexString(
nibbles_to_bytes_suffix_extend(req.prefix()).collect::<Vec<_>>(),
);
let key = HexString(
nibbles_to_bytes_suffix_extend(req.key()).collect::<Vec<_>>(),
);
let prefix =
serde_wasm_bindgen::to_value(&prefix).map_err(|e| e.to_string())?;
let key = serde_wasm_bindgen::to_value(&key).map_err(|e| e.to_string())?;
let value = js.get_next_key(key).await;
let value = js.get_next_key(prefix, key).await;
let value = if value.is_string() {
serde_wasm_bindgen::from_value::<HexString>(value)
.map(|x| Some(x.0))
Expand Down
7 changes: 3 additions & 4 deletions packages/chopsticks/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
get_runtime_version,
run_task,
} from '@acala-network/chopsticks-executor'
import { PREFIX_LENGTH } from './utils/key-cache'
import { Registry } from '@polkadot/types-codec/types'
import { defaultLogger, truncate } from './logger'
import _ from 'lodash'
Expand Down Expand Up @@ -87,8 +86,8 @@ export const taskHandler = (block: Block): JsCallback => {
const header = await block.header
return header.stateRoot.toHex()
},
getNextKey: async function (key: HexString) {
const [nextKey] = await block.getKeysPaged({ prefix: key.slice(0, PREFIX_LENGTH), pageSize: 1, startKey: key })
getNextKey: async function (prefix: HexString, key: HexString) {
const [nextKey] = await block.getKeysPaged({ prefix, pageSize: 1, startKey: key })
return nextKey
},
}
Expand All @@ -101,7 +100,7 @@ export const emptyTaskHandler = {
getStateRoot: async function () {
throw new Error('Method not implemented')
},
getNextKey: async function (_key: HexString) {
getNextKey: async function (_prefix: HexString, _key: HexString) {
throw new Error('Method not implemented')
},
}
Expand Down
7 changes: 4 additions & 3 deletions packages/chopsticks/src/genesis-provider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from 'node:events'
import { HexString } from '@polkadot/util/types'
import { JsCallback } from '@acala-network/chopsticks-executor'
import {
ProviderInterface,
ProviderInterfaceCallback,
Expand Down Expand Up @@ -121,7 +122,7 @@ export class GenesisProvider implements ProviderInterface {
}
}

get _jsCallback() {
get _jsCallback(): JsCallback {
const storage = this.#genesis.genesis.raw.top
return {
getStorage: async function (key: HexString) {
Expand All @@ -130,8 +131,8 @@ export class GenesisProvider implements ProviderInterface {
getStateRoot: async function () {
return '0x49416764844ff0d8bad851e8abe686dff9dd2de78621180ef8e9f99bb7a480f1'
},
getNextKey: async function (_key: HexString) {
return '0x'
getNextKey: async function (_prefix: HexString, _key: HexString) {
return null
},
}
}
Expand Down

0 comments on commit 8450788

Please sign in to comment.