From 56c3a9acc43e43b7a028e1c38dc39b0feb325707 Mon Sep 17 00:00:00 2001 From: Ke Wang Date: Sat, 26 Jan 2019 22:16:30 +0800 Subject: [PATCH] feat: use new merkle proof structure --- protocol/src/builder.rs | 30 ++++---- protocol/src/protocol.fbs | 9 ++- protocol/src/protocol_generated.rs | 112 +++++++++++++++++++++++++---- util/merkle-tree/src/lib.rs | 13 +++- 4 files changed, 132 insertions(+), 32 deletions(-) diff --git a/protocol/src/builder.rs b/protocol/src/builder.rs index ae2366000a..9b2cc00232 100644 --- a/protocol/src/builder.rs +++ b/protocol/src/builder.rs @@ -5,7 +5,7 @@ use crate::protocol_generated::ckb::protocol::{ FilteredBlock, FilteredBlockBuilder, GetBlockProposalBuilder, GetBlockTransactionsBuilder, GetBlocks as FbsGetBlocks, GetBlocksBuilder, GetHeaders as FbsGetHeaders, GetHeadersBuilder, Header as FbsHeader, HeaderBuilder, Headers as FbsHeaders, HeadersBuilder, - IndexTransactionBuilder, OutPoint as FbsOutPoint, OutPointBuilder, + IndexTransactionBuilder, MerkleProofBuilder, OutPoint as FbsOutPoint, OutPointBuilder, ProposalShortId as FbsProposalShortId, RelayMessage, RelayMessageBuilder, RelayPayload, Script as FbsScript, ScriptBuilder, SyncMessage, SyncMessageBuilder, SyncPayload, Time as FbsTime, TimeBuilder, TimeMessage, TimeMessageBuilder, Transaction as FbsTransaction, @@ -374,14 +374,7 @@ impl<'a> FilteredBlock<'a> { } else { let transactions = transactions_index .iter() - .map(|ti| { - let fbs_transaction = - FbsTransaction::build(fbb, &block.commit_transactions()[*ti]); - let mut builder = IndexTransactionBuilder::new(fbb); - builder.add_index(*ti as u32); - builder.add_transaction(fbs_transaction); - builder.finish() - }) + .map(|ti| FbsTransaction::build(fbb, &block.commit_transactions()[*ti])) .collect::>(); let proof = build_merkle_proof( @@ -392,19 +385,26 @@ impl<'a> FilteredBlock<'a> { .collect::>(), transactions_index, ); - let lemmas = proof - .map(|proof| proof.lemmas().to_vec()) - .unwrap_or_else(Vec::new); + + let proof = proof.map(|p| { + let lemmas = + fbb.create_vector(&p.lemmas().iter().map(Into::into).collect::>()); + let indices = fbb.create_vector(p.indices()); + let mut builder = MerkleProofBuilder::new(fbb); + builder.add_lemmas(lemmas); + builder.add_indices(indices); + builder.finish() + }); let header = FbsHeader::build(fbb, &block.header()); let fbs_transactions = fbb.create_vector(&transactions); - let fbs_hashes = - fbb.create_vector(&lemmas.iter().map(Into::into).collect::>()); let mut builder = FilteredBlockBuilder::new(fbb); builder.add_header(header); builder.add_transactions(fbs_transactions); - builder.add_hashes(fbs_hashes); + if let Some(p) = proof { + builder.add_proof(p); + } builder.finish() } } diff --git a/protocol/src/protocol.fbs b/protocol/src/protocol.fbs index b28001260f..a271154146 100644 --- a/protocol/src/protocol.fbs +++ b/protocol/src/protocol.fbs @@ -205,8 +205,13 @@ table ClearFilter { table FilteredBlock { header: Header; - transactions: [IndexTransaction]; - hashes: [H256]; + transactions: [Transaction]; + proof: MerkleProof; +} + +table MerkleProof { + indices: [uint32]; + lemmas: [H256]; } table TimeMessage { diff --git a/protocol/src/protocol_generated.rs b/protocol/src/protocol_generated.rs index d5ddbdbea0..55f4a8891a 100644 --- a/protocol/src/protocol_generated.rs +++ b/protocol/src/protocol_generated.rs @@ -2926,7 +2926,7 @@ impl<'a> FilteredBlock<'a> { _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, args: &'args FilteredBlockArgs<'args>) -> flatbuffers::WIPOffset> { let mut builder = FilteredBlockBuilder::new(_fbb); - if let Some(x) = args.hashes { builder.add_hashes(x); } + if let Some(x) = args.proof { builder.add_proof(x); } if let Some(x) = args.transactions { builder.add_transactions(x); } if let Some(x) = args.header { builder.add_header(x); } builder.finish() @@ -2934,26 +2934,26 @@ impl<'a> FilteredBlock<'a> { pub const VT_HEADER: flatbuffers::VOffsetT = 4; pub const VT_TRANSACTIONS: flatbuffers::VOffsetT = 6; - pub const VT_HASHES: flatbuffers::VOffsetT = 8; + pub const VT_PROOF: flatbuffers::VOffsetT = 8; #[inline] pub fn header(&self) -> Option> { self._tab.get::>>(FilteredBlock::VT_HEADER, None) } #[inline] - pub fn transactions(&self) -> Option>>> { - self._tab.get::>>>>(FilteredBlock::VT_TRANSACTIONS, None) + pub fn transactions(&self) -> Option>>> { + self._tab.get::>>>>(FilteredBlock::VT_TRANSACTIONS, None) } #[inline] - pub fn hashes(&self) -> Option<&'a [H256]> { - self._tab.get::>>(FilteredBlock::VT_HASHES, None).map(|v| v.safe_slice() ) + pub fn proof(&self) -> Option> { + self._tab.get::>>(FilteredBlock::VT_PROOF, None) } } pub struct FilteredBlockArgs<'a> { pub header: Option>>, - pub transactions: Option>>>>, - pub hashes: Option>>, + pub transactions: Option>>>>, + pub proof: Option>>, } impl<'a> Default for FilteredBlockArgs<'a> { #[inline] @@ -2961,7 +2961,7 @@ impl<'a> Default for FilteredBlockArgs<'a> { FilteredBlockArgs { header: None, transactions: None, - hashes: None, + proof: None, } } } @@ -2975,12 +2975,12 @@ impl<'a: 'b, 'b> FilteredBlockBuilder<'a, 'b> { self.fbb_.push_slot_always::>(FilteredBlock::VT_HEADER, header); } #[inline] - pub fn add_transactions(&mut self, transactions: flatbuffers::WIPOffset>>>) { + pub fn add_transactions(&mut self, transactions: flatbuffers::WIPOffset>>>) { self.fbb_.push_slot_always::>(FilteredBlock::VT_TRANSACTIONS, transactions); } #[inline] - pub fn add_hashes(&mut self, hashes: flatbuffers::WIPOffset>) { - self.fbb_.push_slot_always::>(FilteredBlock::VT_HASHES, hashes); + pub fn add_proof(&mut self, proof: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(FilteredBlock::VT_PROOF, proof); } #[inline] pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> FilteredBlockBuilder<'a, 'b> { @@ -2997,6 +2997,94 @@ impl<'a: 'b, 'b> FilteredBlockBuilder<'a, 'b> { } } +pub enum MerkleProofOffset {} +#[derive(Copy, Clone, Debug, PartialEq)] + +pub struct MerkleProof<'a> { + pub _tab: flatbuffers::Table<'a>, +} + +impl<'a> flatbuffers::Follow<'a> for MerkleProof<'a> { + type Inner = MerkleProof<'a>; + #[inline] + fn follow(buf: &'a [u8], loc: usize) -> Self::Inner { + Self { + _tab: flatbuffers::Table { buf: buf, loc: loc }, + } + } +} + +impl<'a> MerkleProof<'a> { + #[inline] + pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self { + MerkleProof { + _tab: table, + } + } + #[allow(unused_mut)] + pub fn create<'bldr: 'args, 'args: 'mut_bldr, 'mut_bldr>( + _fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>, + args: &'args MerkleProofArgs<'args>) -> flatbuffers::WIPOffset> { + let mut builder = MerkleProofBuilder::new(_fbb); + if let Some(x) = args.lemmas { builder.add_lemmas(x); } + if let Some(x) = args.indices { builder.add_indices(x); } + builder.finish() + } + + pub const VT_INDICES: flatbuffers::VOffsetT = 4; + pub const VT_LEMMAS: flatbuffers::VOffsetT = 6; + + #[inline] + pub fn indices(&self) -> Option> { + self._tab.get::>>(MerkleProof::VT_INDICES, None) + } + #[inline] + pub fn lemmas(&self) -> Option<&'a [H256]> { + self._tab.get::>>(MerkleProof::VT_LEMMAS, None).map(|v| v.safe_slice() ) + } +} + +pub struct MerkleProofArgs<'a> { + pub indices: Option>>, + pub lemmas: Option>>, +} +impl<'a> Default for MerkleProofArgs<'a> { + #[inline] + fn default() -> Self { + MerkleProofArgs { + indices: None, + lemmas: None, + } + } +} +pub struct MerkleProofBuilder<'a: 'b, 'b> { + fbb_: &'b mut flatbuffers::FlatBufferBuilder<'a>, + start_: flatbuffers::WIPOffset, +} +impl<'a: 'b, 'b> MerkleProofBuilder<'a, 'b> { + #[inline] + pub fn add_indices(&mut self, indices: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(MerkleProof::VT_INDICES, indices); + } + #[inline] + pub fn add_lemmas(&mut self, lemmas: flatbuffers::WIPOffset>) { + self.fbb_.push_slot_always::>(MerkleProof::VT_LEMMAS, lemmas); + } + #[inline] + pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> MerkleProofBuilder<'a, 'b> { + let start = _fbb.start_table(); + MerkleProofBuilder { + fbb_: _fbb, + start_: start, + } + } + #[inline] + pub fn finish(self) -> flatbuffers::WIPOffset> { + let o = self.fbb_.end_table(self.start_); + flatbuffers::WIPOffset::new(o.value()) + } +} + pub enum TimeMessageOffset {} #[derive(Copy, Clone, Debug, PartialEq)] diff --git a/util/merkle-tree/src/lib.rs b/util/merkle-tree/src/lib.rs index 6354be7f77..bcedf7431d 100644 --- a/util/merkle-tree/src/lib.rs +++ b/util/merkle-tree/src/lib.rs @@ -1,13 +1,20 @@ -use merkle_cbt::{MerkleProof, MerkleTree, CBMT, H256}; +use merkle_cbt::MerkleProof as ExMerkleProof; +use merkle_cbt::MerkleTree as ExMerkleTree; +use merkle_cbt::CBMT as ExCBMT; +use merkle_cbt::H256; + +pub type MerkleProof = ExMerkleProof; +pub type MerkleTree = ExMerkleTree; +pub type CBMT = ExCBMT; pub fn merkle_root(leaves: &[H256]) -> H256 { CBMT::build_merkle_root(leaves) } -pub fn build_merkle_tree(leaves: Vec) -> MerkleTree { +pub fn build_merkle_tree(leaves: Vec) -> MerkleTree { CBMT::build_merkle_tree(leaves) } -pub fn build_merkle_proof(leaves: &[H256], indices: &[usize]) -> Option> { +pub fn build_merkle_proof(leaves: &[H256], indices: &[usize]) -> Option { CBMT::build_merkle_proof(leaves, indices) }