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

feat: use new merkle proof structure #232

Merged
merged 1 commit into from
Jan 28, 2019
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
30 changes: 15 additions & 15 deletions protocol/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<Vec<_>>();

let proof = build_merkle_proof(
Expand All @@ -392,19 +385,26 @@ impl<'a> FilteredBlock<'a> {
.collect::<Vec<_>>(),
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::<Vec<FbsH256>>());
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::<Vec<FbsH256>>());

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()
}
}
Expand Down
9 changes: 7 additions & 2 deletions protocol/src/protocol.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
112 changes: 100 additions & 12 deletions protocol/src/protocol_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2926,42 +2926,42 @@ impl<'a> FilteredBlock<'a> {
_fbb: &'mut_bldr mut flatbuffers::FlatBufferBuilder<'bldr>,
args: &'args FilteredBlockArgs<'args>) -> flatbuffers::WIPOffset<FilteredBlock<'bldr>> {
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()
}

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<Header<'a>> {
self._tab.get::<flatbuffers::ForwardsUOffset<Header<'a>>>(FilteredBlock::VT_HEADER, None)
}
#[inline]
pub fn transactions(&self) -> Option<flatbuffers::Vector<flatbuffers::ForwardsUOffset<IndexTransaction<'a>>>> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<flatbuffers::ForwardsUOffset<IndexTransaction<'a>>>>>(FilteredBlock::VT_TRANSACTIONS, None)
pub fn transactions(&self) -> Option<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Transaction<'a>>>> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<flatbuffers::ForwardsUOffset<Transaction<'a>>>>>(FilteredBlock::VT_TRANSACTIONS, None)
}
#[inline]
pub fn hashes(&self) -> Option<&'a [H256]> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<H256>>>(FilteredBlock::VT_HASHES, None).map(|v| v.safe_slice() )
pub fn proof(&self) -> Option<MerkleProof<'a>> {
self._tab.get::<flatbuffers::ForwardsUOffset<MerkleProof<'a>>>(FilteredBlock::VT_PROOF, None)
}
}

pub struct FilteredBlockArgs<'a> {
pub header: Option<flatbuffers::WIPOffset<Header<'a >>>,
pub transactions: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , flatbuffers::ForwardsUOffset<IndexTransaction<'a >>>>>,
pub hashes: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , H256>>>,
pub transactions: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , flatbuffers::ForwardsUOffset<Transaction<'a >>>>>,
pub proof: Option<flatbuffers::WIPOffset<MerkleProof<'a >>>,
}
impl<'a> Default for FilteredBlockArgs<'a> {
#[inline]
fn default() -> Self {
FilteredBlockArgs {
header: None,
transactions: None,
hashes: None,
proof: None,
}
}
}
Expand All @@ -2975,12 +2975,12 @@ impl<'a: 'b, 'b> FilteredBlockBuilder<'a, 'b> {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<Header>>(FilteredBlock::VT_HEADER, header);
}
#[inline]
pub fn add_transactions(&mut self, transactions: flatbuffers::WIPOffset<flatbuffers::Vector<'b , flatbuffers::ForwardsUOffset<IndexTransaction<'b >>>>) {
pub fn add_transactions(&mut self, transactions: flatbuffers::WIPOffset<flatbuffers::Vector<'b , flatbuffers::ForwardsUOffset<Transaction<'b >>>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(FilteredBlock::VT_TRANSACTIONS, transactions);
}
#[inline]
pub fn add_hashes(&mut self, hashes: flatbuffers::WIPOffset<flatbuffers::Vector<'b , H256>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(FilteredBlock::VT_HASHES, hashes);
pub fn add_proof(&mut self, proof: flatbuffers::WIPOffset<MerkleProof<'b >>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<MerkleProof>>(FilteredBlock::VT_PROOF, proof);
}
#[inline]
pub fn new(_fbb: &'b mut flatbuffers::FlatBufferBuilder<'a>) -> FilteredBlockBuilder<'a, 'b> {
Expand All @@ -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<MerkleProof<'bldr>> {
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<flatbuffers::Vector<'a, u32>> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<'a, u32>>>(MerkleProof::VT_INDICES, None)
}
#[inline]
pub fn lemmas(&self) -> Option<&'a [H256]> {
self._tab.get::<flatbuffers::ForwardsUOffset<flatbuffers::Vector<H256>>>(MerkleProof::VT_LEMMAS, None).map(|v| v.safe_slice() )
}
}

pub struct MerkleProofArgs<'a> {
pub indices: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , u32>>>,
pub lemmas: Option<flatbuffers::WIPOffset<flatbuffers::Vector<'a , H256>>>,
}
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<flatbuffers::TableUnfinishedWIPOffset>,
}
impl<'a: 'b, 'b> MerkleProofBuilder<'a, 'b> {
#[inline]
pub fn add_indices(&mut self, indices: flatbuffers::WIPOffset<flatbuffers::Vector<'b , u32>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(MerkleProof::VT_INDICES, indices);
}
#[inline]
pub fn add_lemmas(&mut self, lemmas: flatbuffers::WIPOffset<flatbuffers::Vector<'b , H256>>) {
self.fbb_.push_slot_always::<flatbuffers::WIPOffset<_>>(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<MerkleProof<'a>> {
let o = self.fbb_.end_table(self.start_);
flatbuffers::WIPOffset::new(o.value())
}
}

pub enum TimeMessageOffset {}
#[derive(Copy, Clone, Debug, PartialEq)]

Expand Down
13 changes: 10 additions & 3 deletions util/merkle-tree/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<H256>;
pub type MerkleTree = ExMerkleTree<H256>;
pub type CBMT = ExCBMT<H256>;

pub fn merkle_root(leaves: &[H256]) -> H256 {
CBMT::build_merkle_root(leaves)
}

pub fn build_merkle_tree(leaves: Vec<H256>) -> MerkleTree<H256> {
pub fn build_merkle_tree(leaves: Vec<H256>) -> MerkleTree {
CBMT::build_merkle_tree(leaves)
}

pub fn build_merkle_proof(leaves: &[H256], indices: &[usize]) -> Option<MerkleProof<H256>> {
pub fn build_merkle_proof(leaves: &[H256], indices: &[usize]) -> Option<MerkleProof> {
CBMT::build_merkle_proof(leaves, indices)
}