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

closest-ancestor-merkle-value => closest-descendant-merkle-value #824

Merged
merged 6 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 7 additions & 6 deletions lib/src/json_rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,11 @@ pub struct ChainHeadStorageResponseItem {
pub value: Option<HexString>,
#[serde(skip_serializing_if = "Option::is_none")]
pub hash: Option<HexString>,
#[serde(rename = "merkle-value-key", skip_serializing_if = "Option::is_none")]
pub merkle_value_key: Option<String>, // TODO: `String` because the number of hex digits can be uneven
#[serde(rename = "merkle-value", skip_serializing_if = "Option::is_none")]
pub merkle_value: Option<HexString>,
#[serde(
rename = "closest-descendant-merkle-value",
skip_serializing_if = "Option::is_none"
)]
pub closest_descendant_merkle_value: Option<HexString>,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
Expand All @@ -766,8 +767,8 @@ pub enum ChainHeadStorageType {
Value,
#[serde(rename = "hash")]
Hash,
#[serde(rename = "closest-ancestor-merkle-value")]
ClosestAncestorMerkleValue,
#[serde(rename = "closest-descendant-merkle-value")]
ClosestDescendantMerkleValue,
#[serde(rename = "descendants-values")]
DescendantsValues,
#[serde(rename = "descendants-hashes")]
Expand Down
31 changes: 1 addition & 30 deletions lib/src/trie/proof_decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ impl<T: AsRef<[u8]>> DecodedTrieProof<T> {
if parent_key.len() == key.len() - 1 =>
{
// Exact match, meaning that `parent_key` is precisely one less nibble than `key`.
// This means that there's no need between `parent_key` and `key`. Consequently,
// This means that there's no node between `parent_key` and `key`. Consequently,
// the closest-descendant-or-equal of `key` is also the strict-closest-descendant
// of `parent_key`, and its Merkle value can be found in `parent_key`'s node
// value.
Expand Down Expand Up @@ -1009,35 +1009,6 @@ impl<T: AsRef<[u8]>> DecodedTrieProof<T> {
}
}
}

/// Returns the key and Merkle value of the closest ancestor to the given key.
///
/// Returns `None` if the key has no ancestor within the trie.
pub fn closest_ancestor_merkle_value<'a>(
&'a self,
trie_root_merkle_value: &[u8; 32],
key: &[nibble::Nibble],
) -> Result<Option<(&'a [nibble::Nibble], trie_node::MerkleValueOutput)>, IncompleteProofError>
{
let (full_key, (_, node_value_range, _)) =
match self.closest_ancestor(trie_root_merkle_value, key) {
Ok(Some(v)) => v,
Ok(None) => return Ok(None),
Err(err) => return Err(err),
};

let node_value = &self.proof.as_ref()[node_value_range.clone()];
if node_value.len() < 32 {
Ok(Some((
full_key,
trie_node::MerkleValueOutput::from_bytes(node_value),
)))
} else {
let hash = blake2_rfc::blake2b::blake2b(32, &[], node_value);
let merkle_value = trie_node::MerkleValueOutput::from_bytes(hash.as_bytes());
Ok(Some((full_key, merkle_value)))
}
}
}

/// Proof doesn't contain enough information to answer the request.
Expand Down
22 changes: 8 additions & 14 deletions light-base/src/json_rpc_service/background/chain_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,8 @@ impl<TPlat: PlatformRef> ChainHeadFollowTask<TPlat> {
methods::ChainHeadStorageType::Hash => {
sync_service::StorageRequestItemTy::Hash
}
methods::ChainHeadStorageType::ClosestAncestorMerkleValue => {
sync_service::StorageRequestItemTy::ClosestAncestorMerkleValue
methods::ChainHeadStorageType::ClosestDescendantMerkleValue => {
sync_service::StorageRequestItemTy::ClosestDescendantMerkleValue
}
methods::ChainHeadStorageType::DescendantsValues => {
sync_service::StorageRequestItemTy::DescendantsValues
Expand Down Expand Up @@ -1011,45 +1011,39 @@ impl<TPlat: PlatformRef> ChainHeadFollowTask<TPlat> {
key: methods::HexString(key),
value: Some(methods::HexString(value?)),
hash: None,
merkle_value: None,
merkle_value_key: None,
closest_descendant_merkle_value: None,
})
}
sync_service::StorageResultItem::Hash { key, hash } => {
Some(methods::ChainHeadStorageResponseItem {
key: methods::HexString(key),
value: None,
hash: Some(methods::HexString(hash?.to_vec())),
merkle_value: None,
merkle_value_key: None,
closest_descendant_merkle_value: None,
})
}
sync_service::StorageResultItem::DescendantValue { key, value, .. } => {
Some(methods::ChainHeadStorageResponseItem {
key: methods::HexString(key),
value: Some(methods::HexString(value)),
hash: None,
merkle_value: None,
merkle_value_key: None,
closest_descendant_merkle_value: None,
})
}
sync_service::StorageResultItem::DescendantHash { key, hash, .. } => {
Some(methods::ChainHeadStorageResponseItem {
key: methods::HexString(key),
value: None,
hash: Some(methods::HexString(hash.to_vec())),
merkle_value: None,
merkle_value_key: None,
closest_descendant_merkle_value: None,
})
}
sync_service::StorageResultItem::ClosestAncestorMerkleValue { requested_key, merkle_value } => {
let (merkle_value_of, merkle_value) = merkle_value?;
sync_service::StorageResultItem::ClosestDescendantMerkleValue { requested_key, closest_descendant_merkle_value: merkle_value } => {
Some(methods::ChainHeadStorageResponseItem {
key: methods::HexString(requested_key),
value: None,
hash: None,
merkle_value: Some(methods::HexString(merkle_value)),
merkle_value_key: Some(format!("0x{}", merkle_value_of.iter().map(|n| format!("{:x}", n)).collect::<String>())),
closest_descendant_merkle_value: Some(methods::HexString(merkle_value?)),
})
}
})
Expand Down
59 changes: 33 additions & 26 deletions light-base/src/sync_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

use crate::{network_service, platform::PlatformRef, runtime_service};

use alloc::{boxed::Box, format, string::String, sync::Arc, vec::Vec};
use alloc::{borrow::ToOwned as _, boxed::Box, format, string::String, sync::Arc, vec::Vec};
use async_lock::Mutex;
use core::{fmt, mem, num::NonZeroU32, time::Duration};
use futures_channel::{mpsc, oneshot};
Expand Down Expand Up @@ -413,7 +413,7 @@ impl<TPlat: PlatformRef> SyncService<TPlat> {
key: Vec<u8>,
hash: bool,
},
ClosestAncestorMerkleValue {
ClosestDescendantMerkleValue {
key: Vec<u8>,
},
}
Expand All @@ -440,8 +440,8 @@ impl<TPlat: PlatformRef> SyncService<TPlat> {
key: request.key,
hash: true,
},
StorageRequestItemTy::ClosestAncestorMerkleValue => {
RequestImpl::ClosestAncestorMerkleValue { key: request.key }
StorageRequestItemTy::ClosestDescendantMerkleValue => {
RequestImpl::ClosestDescendantMerkleValue { key: request.key }
}
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -494,8 +494,13 @@ impl<TPlat: PlatformRef> SyncService<TPlat> {
RequestImpl::ValueOrHash { key, .. } => {
keys.insert(key.clone());
}
RequestImpl::ClosestAncestorMerkleValue { key } => {
keys.insert(key.clone());
RequestImpl::ClosestDescendantMerkleValue { key } => {
// We query the parent of `key`.
if key.is_empty() {
keys.insert(Vec::new());
} else {
keys.insert(key[..key.len() - 1].to_owned());
}
}
}
}
Expand Down Expand Up @@ -651,26 +656,25 @@ impl<TPlat: PlatformRef> SyncService<TPlat> {
}
}
}
RequestImpl::ClosestAncestorMerkleValue { key } => {
match decoded_proof.closest_ancestor_merkle_value(
RequestImpl::ClosestDescendantMerkleValue { key } => {
match decoded_proof.closest_descendant_merkle_value(
main_trie_root_hash,
&trie::bytes_to_nibbles(key.iter().copied()).collect::<Vec<_>>(),
) {
Ok(Some((ancestor_key, merkle_value))) => {
final_results.push(StorageResultItem::ClosestAncestorMerkleValue {
Ok(Some(merkle_value)) => final_results.push(
StorageResultItem::ClosestDescendantMerkleValue {
requested_key: key,
merkle_value: Some((
ancestor_key.to_vec(),
closest_descendant_merkle_value: Some(
merkle_value.as_ref().to_vec(),
)),
})
}
Ok(None) => {
final_results.push(StorageResultItem::ClosestAncestorMerkleValue {
),
},
),
Ok(None) => final_results.push(
StorageResultItem::ClosestDescendantMerkleValue {
requested_key: key,
merkle_value: None,
})
}
closest_descendant_merkle_value: None,
},
),
Err(proof_decode::IncompleteProofError { .. }) => {
outcome_errors.push(StorageQueryErrorDetail::MissingProofEntry);
}
Expand Down Expand Up @@ -775,10 +779,10 @@ pub enum StorageRequestItemTy {

/// The Merkle value of the trie node that is the closest ancestor to
/// [`StorageRequestItem::key`] is requested.
/// A [`StorageResultItem::ClosestAncestorMerkleValue`] will be returned where
/// [`StorageResultItem::ClosestAncestorMerkleValue::requested_key`] is equal to
/// A [`StorageResultItem::ClosestDescendantMerkleValue`] will be returned where
/// [`StorageResultItem::ClosestDescendantMerkleValue::requested_key`] is equal to
/// [`StorageRequestItem::key`].
ClosestAncestorMerkleValue,
ClosestDescendantMerkleValue,
}

/// An item returned by [`SyncService::storage_query`].
Expand Down Expand Up @@ -818,11 +822,14 @@ pub enum StorageResultItem {
/// Hash of the storage value associated with [`StorageResultItem::DescendantHash::key`].
hash: [u8; 32],
},
/// Corresponds to a [`StorageRequestItemTy::ClosestAncestorMerkleValue`].
ClosestAncestorMerkleValue {
/// Corresponds to a [`StorageRequestItemTy::ClosestDescendantMerkleValue`].
ClosestDescendantMerkleValue {
/// Key that was requested. Equal to the value of [`StorageRequestItem::key`].
requested_key: Vec<u8>,
merkle_value: Option<(Vec<trie::Nibble>, Vec<u8>)>,
/// Merkle value of the closest descendant of
/// [`StorageResultItem::DescendantValue::requested_key`]. The key that corresponds
/// to this Merkle value is not included. `None` if the key has no descendant.
closest_descendant_merkle_value: Option<Vec<u8>>,
},
}

Expand Down
4 changes: 4 additions & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Changed

- The `chainHead_unstable_storage` JSON-RPC function now supports a `type` equal to `closest-descendant-merkle-value` and no longer supports `closest-ancestor-merkle-value`, in accordance with the latest changes in the JSON-RPC API specification. ([#824](https://github.com/smol-dot/smoldot/pull/824))

## 1.0.11 - 2023-06-25

### Changed
Expand Down