Skip to content

Commit

Permalink
pass trie version to calculate root (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci authored May 1, 2023
1 parent b3be5e0 commit db29e2d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 56 deletions.
8 changes: 5 additions & 3 deletions executor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate console_error_panic_hook;

use smoldot::json_rpc::methods::{HashHexString, HexString};
use smoldot::{json_rpc::methods::{HashHexString, HexString}, trie::TrieEntryVersion};
use std::collections::BTreeMap;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -41,11 +41,13 @@ pub async fn get_runtime_version(code: JsValue) -> Result<JsValue, JsValue> {
}

#[wasm_bindgen]
pub async fn calculate_state_root(entries: JsValue) -> Result<JsValue, JsValue> {
pub async fn calculate_state_root(entries: JsValue, trie_version: JsValue) -> Result<JsValue, JsValue> {
setup_console();

let entries = serde_wasm_bindgen::from_value::<Vec<(HexString, HexString)>>(entries)?;
let hash = task::calculate_state_root(entries);
let trie_version = serde_wasm_bindgen::from_value::<u8>(trie_version)?;
let trie_version = TrieEntryVersion::try_from(trie_version).map_err(|_| "invalid trie version")?;
let hash = task::calculate_state_root(entries, trie_version);
let result = serde_wasm_bindgen::to_value(&hash)?;

Ok(result)
Expand Down
9 changes: 3 additions & 6 deletions executor/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ pub async fn run_task(task: TaskCall, js: crate::JsCallback) -> Result<TaskRespo
} else {
None
};
// TODO: not sure if we need to inject correct trie_version because it's ignored
// /~https://github.com/smol-dot/smoldot/blob/82252ea371943bdb1c2caeea5cd1d48494b99660/lib/src/executor/runtime_host.rs#L269
// ExternalStorageGet will drop trie_version so we're passing V1 as default
req.inject_value(value.map(|x| (iter::once(x), TrieEntryVersion::V1)))
}
RuntimeHostVm::PrefixKeys(req) => {
Expand Down Expand Up @@ -231,7 +230,7 @@ pub async fn runtime_version(wasm: HexString) -> Result<RuntimeVersion, String>
Ok(RuntimeVersion::new(core_version))
}

pub fn calculate_state_root(entries: Vec<(HexString, HexString)>) -> HexString {
pub fn calculate_state_root(entries: Vec<(HexString, HexString)>, trie_version: TrieEntryVersion) -> HexString {
let mut calc = root_merkle_value(None);
let map = entries
.into_iter()
Expand All @@ -247,9 +246,7 @@ pub fn calculate_state_root(entries: Vec<(HexString, HexString)>) -> HexString {
}
RootMerkleValueCalculation::StorageValue(req) => {
let key = req.key().collect::<Vec<u8>>();
// TODO: not sure if we need to inject correct trie_version because it's ignored
// /~https://github.com/smol-dot/smoldot/blob/82252ea371943bdb1c2caeea5cd1d48494b99660/lib/src/executor/runtime_host.rs#L269
calc = req.inject(map.get(&key).map(|x| (x, TrieEntryVersion::V1)));
calc = req.inject(map.get(&key).map(|x| (x, trie_version)));
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions packages/chopsticks/src/executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ describe('wasm', () => {
expect(await getRuntimeVersion(getCode())).toMatchObject(expectedRuntimeVersion)
})

it('calculate state root', async () => {
const a = await calculateStateRoot([['0x5301bf5ff0298f5c7b93a446709f8e885f772afdd0d8ba3d4d559a06f0742f12', '0x01']])
const b = await calculateStateRoot([['0x5301bf5ff0298f5c7b93a446709f8e885f772afdd0d8ba3d4d559a06f0742f12', '0x02']])
it.each([0, 1])('calculate state root', async (trie_version) => {
const a = await calculateStateRoot(
[['0x5301bf5ff0298f5c7b93a446709f8e885f772afdd0d8ba3d4d559a06f0742f12', '0x01']],
trie_version
)
const b = await calculateStateRoot(
[['0x5301bf5ff0298f5c7b93a446709f8e885f772afdd0d8ba3d4d559a06f0742f12', '0x02']],
trie_version
)
expect(a).to.not.eq(b)
})

Expand Down
8 changes: 6 additions & 2 deletions packages/chopsticks/src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export const getRuntimeVersion = async (code: HexString): Promise<RuntimeVersion
})
}

export const calculateStateRoot = async (entries: [HexString, HexString][]): Promise<HexString> => {
return calculate_state_root(entries)
// trie_version: 0 for old trie, 1 for new trie
export const calculateStateRoot = async (
entries: [HexString, HexString][],
trie_version: number
): Promise<HexString> => {
return calculate_state_root(entries, trie_version)
}

export const decodeProof = async (trieRootHash: HexString, keys: HexString[], nodes: HexString[]) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/chopsticks/src/genesis-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export class GenesisProvider implements ProviderInterface {
Object.entries(this.#genesis.genesis.raw.top).reduce((accu, item) => {
accu.push(item as any)
return accu
}, [] as [HexString, HexString][])
}, [] as [HexString, HexString][]),
1
)

this.#eventemitter = new EventEmitter()
Expand Down
45 changes: 4 additions & 41 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,7 @@ __metadata:
languageName: node
linkType: hard

"@jridgewell/sourcemap-codec@npm:^1.4.10":
version: 1.4.14
resolution: "@jridgewell/sourcemap-codec@npm:1.4.14"
checksum: 61100637b6d173d3ba786a5dff019e1a74b1f394f323c1fee337ff390239f053b87266c7a948777f4b1ee68c01a8ad0ab61e5ff4abb5a012a0b091bec391ab97
languageName: node
linkType: hard

"@jridgewell/sourcemap-codec@npm:^1.4.13":
"@jridgewell/sourcemap-codec@npm:^1.4.10, @jridgewell/sourcemap-codec@npm:^1.4.13":
version: 1.4.15
resolution: "@jridgewell/sourcemap-codec@npm:1.4.15"
checksum: b881c7e503db3fc7f3c1f35a1dd2655a188cc51a3612d76efc8a6eb74728bef5606e6758ee77423e564092b4a518aba569bbb21c9bac5ab7a35b0c6ae7e344c8
Expand Down Expand Up @@ -3914,19 +3907,7 @@ __metadata:
languageName: node
linkType: hard

"mlly@npm:^1.1.1":
version: 1.1.1
resolution: "mlly@npm:1.1.1"
dependencies:
acorn: ^8.8.2
pathe: ^1.1.0
pkg-types: ^1.0.1
ufo: ^1.1.0
checksum: 6bc4ffe0f4d061c7f6bd6bfe80c675eece0814ec3ac8efecbde2ecf337f31ddd78a8b35836ffcf66f37f17404a7cc094ab694122121b50f337bf435697f4ab9c
languageName: node
linkType: hard

"mlly@npm:^1.2.0":
"mlly@npm:^1.1.1, mlly@npm:^1.2.0":
version: 1.2.0
resolution: "mlly@npm:1.2.0"
dependencies:
Expand Down Expand Up @@ -4431,7 +4412,7 @@ __metadata:
languageName: node
linkType: hard

"pkg-types@npm:^1.0.1, pkg-types@npm:^1.0.2":
"pkg-types@npm:^1.0.2":
version: 1.0.2
resolution: "pkg-types@npm:1.0.2"
dependencies:
Expand Down Expand Up @@ -4776,7 +4757,7 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.3.2":
"semver@npm:^7.3.2, semver@npm:^7.3.5, semver@npm:^7.3.7":
version: 7.5.0
resolution: "semver@npm:7.5.0"
dependencies:
Expand All @@ -4787,17 +4768,6 @@ __metadata:
languageName: node
linkType: hard

"semver@npm:^7.3.5, semver@npm:^7.3.7":
version: 7.3.8
resolution: "semver@npm:7.3.8"
dependencies:
lru-cache: ^6.0.0
bin:
semver: bin/semver.js
checksum: ba9c7cbbf2b7884696523450a61fee1a09930d888b7a8d7579025ad93d459b2d1949ee5bbfeb188b2be5f4ac163544c5e98491ad6152df34154feebc2cc337c1
languageName: node
linkType: hard

"set-blocking@npm:^2.0.0":
version: 2.0.0
resolution: "set-blocking@npm:2.0.0"
Expand Down Expand Up @@ -5515,13 +5485,6 @@ __metadata:
languageName: node
linkType: hard

"ufo@npm:^1.1.0":
version: 1.1.0
resolution: "ufo@npm:1.1.0"
checksum: f4098df457526be8bb1a466f05a2f72075b91580519215e0c219a4ff7dca4327b59964a053012f8547da77501b61cdebffbf8e16ef4d0509a7561efffe8e74ea
languageName: node
linkType: hard

"ufo@npm:^1.1.1":
version: 1.1.1
resolution: "ufo@npm:1.1.1"
Expand Down

0 comments on commit db29e2d

Please sign in to comment.