Skip to content

Commit

Permalink
Merge pull request #40 from confio/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
ethanfrey committed Apr 19, 2021
2 parents 267cfba + 46f2126 commit 19f273d
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 156 deletions.
22 changes: 7 additions & 15 deletions rust/src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,14 @@ pub fn decompress_batch(proof: &ics23::CompressedBatchProof) -> Result<ics23::Co
.map(|cent| -> Result<ics23::BatchEntry> {
match &cent.proof {
Some(ics23::compressed_batch_entry::Proof::Exist(ex)) => {
let exist = decompress_exist(&ex, &lookup)?;
let exist = decompress_exist(&ex, &lookup);
Ok(ics23::BatchEntry {
proof: Some(ics23::batch_entry::Proof::Exist(exist)),
})
}
Some(ics23::compressed_batch_entry::Proof::Nonexist(non)) => {
let left = non
.left
.clone()
.map(|l| decompress_exist(&l, &lookup))
.transpose()?;
let right = non
.right
.clone()
.map(|r| decompress_exist(&r, &lookup))
.transpose()?;
let left = non.left.clone().map(|l| decompress_exist(&l, &lookup));
let right = non.right.clone().map(|r| decompress_exist(&r, &lookup));
let nonexist = ics23::NonExistenceProof {
key: non.key.clone(),
left,
Expand All @@ -154,17 +146,17 @@ pub fn decompress_batch(proof: &ics23::CompressedBatchProof) -> Result<ics23::Co
fn decompress_exist(
exist: &ics23::CompressedExistenceProof,
lookup: &[ics23::InnerOp],
) -> Result<ics23::ExistenceProof> {
) -> ics23::ExistenceProof {
let path = exist
.path
.iter()
.map(|&x| lookup.get(x as usize).cloned())
.collect::<Option<Vec<_>>>()
.unwrap();
Ok(ics23::ExistenceProof {
.unwrap_or_default();
ics23::ExistenceProof {
key: exist.key.clone(),
value: exist.value.clone(),
leaf: exist.leaf.clone(),
path,
})
}
}
94 changes: 47 additions & 47 deletions rust/src/ics23.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
///length-prefix the data before hashing it.
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ExistenceProof {
#[prost(bytes, tag="1")]
#[prost(bytes, tag = "1")]
pub key: std::vec::Vec<u8>,
#[prost(bytes, tag="2")]
#[prost(bytes, tag = "2")]
pub value: std::vec::Vec<u8>,
#[prost(message, optional, tag="3")]
#[prost(message, optional, tag = "3")]
pub leaf: ::std::option::Option<LeafOp>,
#[prost(message, repeated, tag="4")]
#[prost(message, repeated, tag = "4")]
pub path: ::std::vec::Vec<InnerOp>,
}
///
Expand All @@ -36,30 +36,30 @@ pub struct ExistenceProof {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct NonExistenceProof {
/// TODO: remove this as unnecessary??? we prove a range
#[prost(bytes, tag="1")]
#[prost(bytes, tag = "1")]
pub key: std::vec::Vec<u8>,
#[prost(message, optional, tag="2")]
#[prost(message, optional, tag = "2")]
pub left: ::std::option::Option<ExistenceProof>,
#[prost(message, optional, tag="3")]
#[prost(message, optional, tag = "3")]
pub right: ::std::option::Option<ExistenceProof>,
}
///
///CommitmentProof is either an ExistenceProof or a NonExistenceProof, or a Batch of such messages
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CommitmentProof {
#[prost(oneof="commitment_proof::Proof", tags="1, 2, 3, 4")]
#[prost(oneof = "commitment_proof::Proof", tags = "1, 2, 3, 4")]
pub proof: ::std::option::Option<commitment_proof::Proof>,
}
pub mod commitment_proof {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Proof {
#[prost(message, tag="1")]
#[prost(message, tag = "1")]
Exist(super::ExistenceProof),
#[prost(message, tag="2")]
#[prost(message, tag = "2")]
Nonexist(super::NonExistenceProof),
#[prost(message, tag="3")]
#[prost(message, tag = "3")]
Batch(super::BatchProof),
#[prost(message, tag="4")]
#[prost(message, tag = "4")]
Compressed(super::CompressedBatchProof),
}
}
Expand All @@ -80,17 +80,17 @@ pub mod commitment_proof {
///output = hash(prefix || length(hkey) || hkey || length(hvalue) || hvalue)
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct LeafOp {
#[prost(enumeration="HashOp", tag="1")]
#[prost(enumeration = "HashOp", tag = "1")]
pub hash: i32,
#[prost(enumeration="HashOp", tag="2")]
#[prost(enumeration = "HashOp", tag = "2")]
pub prehash_key: i32,
#[prost(enumeration="HashOp", tag="3")]
#[prost(enumeration = "HashOp", tag = "3")]
pub prehash_value: i32,
#[prost(enumeration="LengthOp", tag="4")]
#[prost(enumeration = "LengthOp", tag = "4")]
pub length: i32,
/// prefix is a fixed bytes that may optionally be included at the beginning to differentiate
/// a leaf node from an inner node.
#[prost(bytes, tag="5")]
#[prost(bytes, tag = "5")]
pub prefix: std::vec::Vec<u8>,
}
///*
Expand All @@ -111,11 +111,11 @@ pub struct LeafOp {
///If either of prefix or suffix is empty, we just treat it as an empty string
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct InnerOp {
#[prost(enumeration="HashOp", tag="1")]
#[prost(enumeration = "HashOp", tag = "1")]
pub hash: i32,
#[prost(bytes, tag="2")]
#[prost(bytes, tag = "2")]
pub prefix: std::vec::Vec<u8>,
#[prost(bytes, tag="3")]
#[prost(bytes, tag = "3")]
pub suffix: std::vec::Vec<u8>,
}
///*
Expand All @@ -132,16 +132,16 @@ pub struct InnerOp {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ProofSpec {
/// any field in the ExistenceProof must be the same as in this spec.
/// except Prefix, which is just the first bytes of prefix (spec can be longer)
#[prost(message, optional, tag="1")]
/// except Prefix, which is just the first bytes of prefix (spec can be longer)
#[prost(message, optional, tag = "1")]
pub leaf_spec: ::std::option::Option<LeafOp>,
#[prost(message, optional, tag="2")]
#[prost(message, optional, tag = "2")]
pub inner_spec: ::std::option::Option<InnerSpec>,
/// max_depth (if > 0) is the maximum number of InnerOps allowed (mainly for fixed-depth tries)
#[prost(int32, tag="3")]
#[prost(int32, tag = "3")]
pub max_depth: i32,
/// min_depth (if > 0) is the minimum number of InnerOps allowed (mainly for fixed-depth tries)
#[prost(int32, tag="4")]
#[prost(int32, tag = "4")]
pub min_depth: i32,
}
///
Expand All @@ -158,87 +158,87 @@ pub struct InnerSpec {
/// Child order is the ordering of the children node, must count from 0
/// iavl tree is [0, 1] (left then right)
/// merk is [0, 2, 1] (left, right, here)
#[prost(int32, repeated, tag="1")]
#[prost(int32, repeated, tag = "1")]
pub child_order: ::std::vec::Vec<i32>,
#[prost(int32, tag="2")]
#[prost(int32, tag = "2")]
pub child_size: i32,
#[prost(int32, tag="3")]
#[prost(int32, tag = "3")]
pub min_prefix_length: i32,
#[prost(int32, tag="4")]
#[prost(int32, tag = "4")]
pub max_prefix_length: i32,
/// empty child is the prehash image that is used when one child is nil (eg. 20 bytes of 0)
#[prost(bytes, tag="5")]
#[prost(bytes, tag = "5")]
pub empty_child: std::vec::Vec<u8>,
/// hash is the algorithm that must be used for each InnerOp
#[prost(enumeration="HashOp", tag="6")]
#[prost(enumeration = "HashOp", tag = "6")]
pub hash: i32,
}
///
///BatchProof is a group of multiple proof types than can be compressed
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BatchProof {
#[prost(message, repeated, tag="1")]
#[prost(message, repeated, tag = "1")]
pub entries: ::std::vec::Vec<BatchEntry>,
}
/// Use BatchEntry not CommitmentProof, to avoid recursion
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BatchEntry {
#[prost(oneof="batch_entry::Proof", tags="1, 2")]
#[prost(oneof = "batch_entry::Proof", tags = "1, 2")]
pub proof: ::std::option::Option<batch_entry::Proof>,
}
pub mod batch_entry {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Proof {
#[prost(message, tag="1")]
#[prost(message, tag = "1")]
Exist(super::ExistenceProof),
#[prost(message, tag="2")]
#[prost(message, tag = "2")]
Nonexist(super::NonExistenceProof),
}
}
//***** all items here are compressed forms ******

#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompressedBatchProof {
#[prost(message, repeated, tag="1")]
#[prost(message, repeated, tag = "1")]
pub entries: ::std::vec::Vec<CompressedBatchEntry>,
#[prost(message, repeated, tag="2")]
#[prost(message, repeated, tag = "2")]
pub lookup_inners: ::std::vec::Vec<InnerOp>,
}
/// Use BatchEntry not CommitmentProof, to avoid recursion
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompressedBatchEntry {
#[prost(oneof="compressed_batch_entry::Proof", tags="1, 2")]
#[prost(oneof = "compressed_batch_entry::Proof", tags = "1, 2")]
pub proof: ::std::option::Option<compressed_batch_entry::Proof>,
}
pub mod compressed_batch_entry {
#[derive(Clone, PartialEq, ::prost::Oneof)]
pub enum Proof {
#[prost(message, tag="1")]
#[prost(message, tag = "1")]
Exist(super::CompressedExistenceProof),
#[prost(message, tag="2")]
#[prost(message, tag = "2")]
Nonexist(super::CompressedNonExistenceProof),
}
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompressedExistenceProof {
#[prost(bytes, tag="1")]
#[prost(bytes, tag = "1")]
pub key: std::vec::Vec<u8>,
#[prost(bytes, tag="2")]
#[prost(bytes, tag = "2")]
pub value: std::vec::Vec<u8>,
#[prost(message, optional, tag="3")]
#[prost(message, optional, tag = "3")]
pub leaf: ::std::option::Option<LeafOp>,
/// these are indexes into the lookup_inners table in CompressedBatchProof
#[prost(int32, repeated, tag="4")]
#[prost(int32, repeated, tag = "4")]
pub path: ::std::vec::Vec<i32>,
}
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompressedNonExistenceProof {
/// TODO: remove this as unnecessary??? we prove a range
#[prost(bytes, tag="1")]
#[prost(bytes, tag = "1")]
pub key: std::vec::Vec<u8>,
#[prost(message, optional, tag="2")]
#[prost(message, optional, tag = "2")]
pub left: ::std::option::Option<CompressedExistenceProof>,
#[prost(message, optional, tag="3")]
#[prost(message, optional, tag = "3")]
pub right: ::std::option::Option<CompressedExistenceProof>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
Expand Down

0 comments on commit 19f273d

Please sign in to comment.