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

fix: FI-1574: last_block_index shall be leb128 encoded #147

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# CHANGELOG

## [Unreleased] - ReleaseDate
* Adapted `icrc3_get_tip_certificate` to be compliant with the ICRC-3 specification by changing the encoding of `last_block_index` to `leb128`.

## [1.0.2] - 2024-10-28
* Added `get_icp_xdr_conversion_rate` to mock CMC
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cycles-ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ic-canister-log = "0.2.0"
ic-canisters-http-types = { git = "https://github.com/dfinity/ic", rev = "b2f18ac0794d2225b53d9c3190b60dbadb4ac9b9" }
ic-metrics-encoder = "1.1"
serde_json = "1.0.107"
leb128 = "0.2.5"

[dev-dependencies]
assert_matches = "1.5.0"
Expand Down
17 changes: 16 additions & 1 deletion cycles-ledger/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,12 +775,17 @@ fn check_invariants(s: &State) {
}
}

/// The maximum number of bytes a 64-bit number can occupy when encoded in LEB128.
const MAX_U64_ENCODING_BYTES: usize = 10;

pub fn populate_last_block_hash_and_hash_tree(
hash_tree: &mut RbTree<&'static str, Vec<u8>>,
last_block_index: u64,
last_block_hash: Hash,
) {
hash_tree.insert("last_block_index", last_block_index.to_be_bytes().to_vec());
let mut last_block_index_buf = Vec::with_capacity(MAX_U64_ENCODING_BYTES);
leb128::write::unsigned(&mut last_block_index_buf, last_block_index).unwrap();
mbjorkqvist marked this conversation as resolved.
Show resolved Hide resolved
hash_tree.insert("last_block_index", last_block_index_buf.to_vec());
hash_tree.insert("last_block_hash", last_block_hash.to_vec());
}

Expand Down Expand Up @@ -2713,6 +2718,16 @@ mod tests {
},
}
}

#[test]
fn test_u64_to_leb128() {
for i in [0, 1, u64::MAX - 1, u64::MAX] {
let mut buf = Vec::with_capacity(crate::storage::MAX_U64_ENCODING_BYTES);
leb128::write::unsigned(&mut buf, i).unwrap();
let decoded = leb128::read::unsigned(&mut buf.as_slice()).unwrap();
assert_eq!(i, decoded);
}
}
}

mod phash {
Expand Down
3 changes: 2 additions & 1 deletion cycles-ledger/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,8 @@ impl TestEnv {
let expected_last_block_index = match hash_tree.lookup_subtree([b"last_block_index"]) {
SubtreeLookupResult::Found(tree) => match tree.as_ref() {
HashTreeNode::Leaf(last_block_index_bytes) => {
u64::from_be_bytes(last_block_index_bytes.clone().try_into().unwrap())
leb128::read::unsigned(&mut last_block_index_bytes.as_slice())
.expect("Unable to read last_block_index from the hash_tree")
}
_ => panic!("last_block_index value in the hash_tree should be a Leaf"),
},
Expand Down
Loading