-
Notifications
You must be signed in to change notification settings - Fork 228
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(light-client): do not skip the genesis block for last state proofs #3697
Merged
bors
merged 2 commits into
nervosnetwork:develop
from
yangby-cryptape:pr/fix-should-not-skip-genesis-in-proof
Nov 10, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
util/light-client-protocol-server/src/tests/components/get_last_state_proof.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use ckb_merkle_mountain_range::{leaf_index_to_mmr_size, leaf_index_to_pos}; | ||
use ckb_network::{CKBProtocolHandler, PeerIndex, SupportProtocols}; | ||
use ckb_types::{ | ||
packed, | ||
prelude::*, | ||
utilities::merkle_mountain_range::{MMRProof, VerifiableHeader}, | ||
}; | ||
|
||
use crate::tests::{ | ||
prelude::*, | ||
utils::{MockChain, MockNetworkContext}, | ||
}; | ||
|
||
#[tokio::test(flavor = "multi_thread")] | ||
async fn get_last_state_proof_with_the_genesis_block() { | ||
let chain = MockChain::new(); | ||
let nc = MockNetworkContext::new(SupportProtocols::LightClient); | ||
|
||
chain.mine_to(1); | ||
|
||
let snapshot = chain.shared().snapshot(); | ||
let verifiable_tip_header: VerifiableHeader = | ||
snapshot.get_verifiable_header_by_number(1).unwrap().into(); | ||
let tip_header = verifiable_tip_header.header(); | ||
let genesis_header = snapshot.get_header_by_number(0).unwrap(); | ||
|
||
let mut protocol = chain.create_light_client_protocol(); | ||
|
||
let data = { | ||
let content = packed::GetLastStateProof::new_builder() | ||
.last_hash(tip_header.hash()) | ||
.start_hash(genesis_header.hash()) | ||
.start_number(0u64.pack()) | ||
.last_n_blocks(10u64.pack()) | ||
.difficulty_boundary(genesis_header.difficulty().pack()) | ||
.build(); | ||
packed::LightClientMessage::new_builder() | ||
.set(content) | ||
.build() | ||
} | ||
.as_bytes(); | ||
|
||
assert!(nc.sent_messages().borrow().is_empty()); | ||
|
||
let peer_index = PeerIndex::new(1); | ||
protocol.received(nc.context(), peer_index, data).await; | ||
|
||
assert!(nc.not_banned(peer_index)); | ||
|
||
assert_eq!(nc.sent_messages().borrow().len(), 1); | ||
|
||
let data = &nc.sent_messages().borrow()[0].2; | ||
let message = packed::LightClientMessageReader::new_unchecked(data); | ||
let content = if let packed::LightClientMessageUnionReader::SendLastStateProof(content) = | ||
message.to_enum() | ||
{ | ||
content | ||
} else { | ||
panic!("unexpected message"); | ||
} | ||
.to_entity(); | ||
|
||
// Verify MMR Proof | ||
{ | ||
let parent_chain_root = verifiable_tip_header.parent_chain_root(); | ||
let proof: MMRProof = { | ||
let mmr_size = leaf_index_to_mmr_size(parent_chain_root.end_number().unpack()); | ||
let proof = content.proof().into_iter().collect(); | ||
MMRProof::new(mmr_size, proof) | ||
}; | ||
let digests_with_positions = { | ||
let result = content | ||
.headers() | ||
.into_iter() | ||
.map(|verifiable_header| { | ||
let header = verifiable_header.header().into_view(); | ||
let index = header.number(); | ||
let position = leaf_index_to_pos(index); | ||
let digest = header.digest(); | ||
digest.verify()?; | ||
Ok((position, digest)) | ||
}) | ||
.collect::<Result<Vec<_>, String>>(); | ||
assert!(result.is_ok(), "failed since {}", result.unwrap_err()); | ||
result.unwrap() | ||
}; | ||
let result = proof.verify(parent_chain_root, digests_with_positions); | ||
assert!(result.is_ok(), "failed since {}", result.unwrap_err()); | ||
} | ||
|
||
assert_eq!(content.headers().len(), 1); | ||
|
||
let verifiable_header: VerifiableHeader = content.headers().get(0).unwrap().into(); | ||
assert!(verifiable_header.header().is_genesis()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod get_blocks_proof; | ||
mod get_last_state_proof; | ||
mod get_transactions_proof; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without the fix, this line will throw the error "Corrupted proof".