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: implement Blob commitment calculation #10

Merged
merged 10 commits into from
Jun 29, 2023
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 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 types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license = "Apache-2.0"

[dependencies]
base64 = "0.21.2"
bytes = "1.4.0"
celestia-proto = { workspace = true }
nmt-rs = { git = "https://github.com/eigerco/nmt-rs.git", rev = "a371723" }
serde = { version = "1.0.164", features = ["derive"] }
Expand Down
34 changes: 31 additions & 3 deletions types/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ use tendermint_proto::v0_34::types::Blob as RawBlob;
use tendermint_proto::Protobuf;

use crate::nmt::Namespace;
use crate::Error;
use crate::{Error, Result};

mod commitment;

// NOTE: We don't use the `serde(try_from)` pattern for this type
// becase JSON representation needs to have `commitment` field but
// Protobuf definition doesn't.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Blob {
pub namespace: Namespace,
#[serde(with = "tendermint_proto::serializers::bytes::base64string")]
Expand All @@ -25,10 +27,10 @@ impl TryFrom<RawBlob> for Blob {

fn try_from(value: RawBlob) -> Result<Self, Self::Error> {
Ok(Blob {
commitment: commitment::create_commitment(&value)?,
namespace: Namespace::new(value.namespace_version as u8, &value.namespace_id)?,
data: value.data,
share_version: value.share_version as u8,
commitment: Vec::new(),
})
}
}
Expand All @@ -43,3 +45,29 @@ impl From<Blob> for RawBlob {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

fn sample_blob() -> Blob {
serde_json::from_str(
r#"{
"namespace": "AAAAAAAAAAAAAAAAAAAAAAAAAAAADCBNOWAP3dM=",
"data": "8fIMqAB+kQo7+LLmHaDya8oH73hxem6lQWX1",
"share_version": 0,
"commitment": "D6YGsPWdxR8ju2OcOspnkgPG2abD30pSHxsFdiPqnVk="
}"#,
)
.unwrap()
}

#[test]
fn create_from_raw() {
let expected = sample_blob();
let raw = RawBlob::try_from(expected.clone()).unwrap();
let created = Blob::try_from(raw).unwrap();

assert_eq!(created, expected);
}
}
Loading