Skip to content

Commit

Permalink
feat(types)!: Add versioned consts (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique authored Oct 25, 2024
1 parent 78d9fdb commit 5018d88
Show file tree
Hide file tree
Showing 25 changed files with 470 additions and 239 deletions.
7 changes: 4 additions & 3 deletions node/src/daser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ mod tests {
use crate::test_utils::{async_test, MockP2pHandle};
use bytes::BytesMut;
use celestia_proto::bitswap::Block;
use celestia_types::consts::appconsts::AppVersion;
use celestia_types::sample::{Sample, SampleId};
use celestia_types::test_utils::{generate_dummy_eds, ExtendedHeaderGenerator};
use celestia_types::{AxisType, DataAvailabilityHeader, ExtendedDataSquare};
Expand Down Expand Up @@ -570,7 +571,7 @@ mod tests {
let mut headers = Vec::new();

for _ in 0..20 {
let eds = generate_dummy_eds(2);
let eds = generate_dummy_eds(2, AppVersion::V2);
let dah = DataAvailabilityHeader::from_eds(&eds);
let header = gen.next_with_dah(dah);

Expand Down Expand Up @@ -644,7 +645,7 @@ mod tests {
handle.expect_no_cmd().await;

// Push block 21 in the store
let eds = generate_dummy_eds(2);
let eds = generate_dummy_eds(2, AppVersion::V2);
let dah = DataAvailabilityHeader::from_eds(&eds);
let header = gen.next_with_dah(dah);
store.insert(header).await.unwrap();
Expand All @@ -663,7 +664,7 @@ mod tests {
square_width: usize,
simulate_invalid_sampling: bool,
) {
let eds = generate_dummy_eds(square_width);
let eds = generate_dummy_eds(square_width, AppVersion::V2);
let dah = DataAvailabilityHeader::from_eds(&eds);
let header = gen.next_with_dah(dah);
let height = header.height().value();
Expand Down
3 changes: 2 additions & 1 deletion node/src/p2p/shwap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,15 @@ mod tests {
use crate::store::InMemoryStore;
use crate::test_utils::async_test;
use bytes::BytesMut;
use celestia_types::consts::appconsts::AppVersion;
use celestia_types::test_utils::{generate_dummy_eds, ExtendedHeaderGenerator};
use celestia_types::{AxisType, DataAvailabilityHeader};

#[async_test]
async fn hash() {
let store = Arc::new(InMemoryStore::new());

let eds = generate_dummy_eds(4);
let eds = generate_dummy_eds(4, AppVersion::V2);
let dah = DataAvailabilityHeader::from_eds(&eds);

let mut gen = ExtendedHeaderGenerator::new();
Expand Down
3 changes: 2 additions & 1 deletion node/tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::time::Duration;

use celestia_tendermint_proto::Protobuf;
use celestia_types::consts::appconsts::AppVersion;
use celestia_types::consts::HASH_SIZE;
use celestia_types::fraud_proof::BadEncodingFraudProof;
use celestia_types::hash::Hash;
Expand Down Expand Up @@ -182,7 +183,7 @@ async fn stops_services_when_network_is_compromised() {
store.insert(gen.next_many_verified(64)).await.unwrap();

// create a corrupted block and insert it
let mut eds = generate_dummy_eds(8);
let mut eds = generate_dummy_eds(8, AppVersion::V2);
let (header, befp) = corrupt_eds(&mut gen, &mut eds);

store.insert(header).await.unwrap();
Expand Down
5 changes: 2 additions & 3 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ This crate builds on top of the [`jsonrpsee`](https://docs.rs/jsonrpsee) clients

```rust,no_run
use celestia_rpc::{BlobClient, Client};
use celestia_types::{Blob, nmt::Namespace};
use celestia_types::TxConfig;
use celestia_types::{AppVersion, Blob, TxConfig, nmt::Namespace};
async fn submit_blob() {
// create a client to the celestia node
Expand All @@ -18,7 +17,7 @@ async fn submit_blob() {
// create a blob that you want to submit
let my_namespace = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
let blob = Blob::new(my_namespace, b"some data to store on blockchain".to_vec())
let blob = Blob::new(my_namespace, b"some data to store on blockchain".to_vec(), AppVersion::V2)
.expect("Failed to create a blob");
// submit it
Expand Down
10 changes: 6 additions & 4 deletions rpc/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ mod native {
use std::result::Result;

use async_trait::async_trait;
use celestia_types::consts::appconsts::SHARE_SIZE;
use celestia_types::consts::data_availability_header::MAX_EXTENDED_SQUARE_WIDTH;
use celestia_types::consts::appconsts::{self, SHARE_SIZE};
use http::{header, HeaderValue};
use jsonrpsee::core::client::{BatchResponse, ClientT, Subscription, SubscriptionClientT};
use jsonrpsee::core::params::BatchRequestBuilder;
Expand All @@ -29,8 +28,11 @@ mod native {

use crate::Error;

const MAX_EDS_SIZE_BYTES: usize =
MAX_EXTENDED_SQUARE_WIDTH * MAX_EXTENDED_SQUARE_WIDTH * SHARE_SIZE;
// NOTE: Always the largest `appconsts::*::SQUARE_SIZE_UPPER_BOUND` needs to be used.
const MAX_EDS_SIZE_BYTES: usize = appconsts::v3::SQUARE_SIZE_UPPER_BOUND
* appconsts::v3::SQUARE_SIZE_UPPER_BOUND
* 4
* SHARE_SIZE;

// The biggest response we might get is for requesting an EDS.
// Also, we allow 1 MB extra for any metadata they come with it.
Expand Down
20 changes: 18 additions & 2 deletions rpc/src/share.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::future::Future;
use std::marker::{Send, Sync};

use celestia_types::consts::appconsts::AppVersion;
use celestia_types::nmt::Namespace;
use celestia_types::row_namespace_data::NamespacedShares;
use celestia_types::{ExtendedDataSquare, ExtendedHeader, RawShare, Share, ShareProof};
Expand All @@ -23,11 +24,15 @@ pub struct GetRangeResponse {

mod rpc {
use super::*;
use celestia_types::eds::RawExtendedDataSquare;

#[rpc(client)]
pub trait Share {
#[method(name = "share.GetEDS")]
async fn share_get_eds(&self, root: &ExtendedHeader) -> Result<ExtendedDataSquare, Error>;
async fn share_get_eds(
&self,
root: &ExtendedHeader,
) -> Result<RawExtendedDataSquare, Error>;

#[method(name = "share.GetRange")]
async fn share_get_range(
Expand Down Expand Up @@ -69,7 +74,18 @@ pub trait ShareClient: ClientT {
'b: 'fut,
Self: Sized + Sync + 'fut,
{
rpc::ShareClient::share_get_eds(self, root)
async move {
let app_version = root.header.version.app;
let app_version = AppVersion::from_u64(app_version).ok_or_else(|| {
let e = format!("Invalid or unsupported AppVersion: {app_version}");
Error::Custom(e)
})?;

let raw_eds = rpc::ShareClient::share_get_eds(self, root).await?;

ExtendedDataSquare::from_raw(raw_eds, app_version)
.map_err(|e| Error::Custom(e.to_string()))
}
}

/// GetRange gets a list of shares and their corresponding proof.
Expand Down
31 changes: 16 additions & 15 deletions rpc/tests/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::time::Duration;

use celestia_rpc::blob::BlobsAtHeight;
use celestia_rpc::prelude::*;
use celestia_types::consts::appconsts::AppVersion;
use celestia_types::{Blob, Commitment};
use jsonrpsee::core::client::Subscription;

Expand All @@ -18,7 +19,7 @@ async fn blob_submit_and_get() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(5);
let blob = Blob::new(namespace, data).unwrap();
let blob = Blob::new(namespace, data, AppVersion::V2).unwrap();

let submitted_height = blob_submit(&client, &[blob.clone()]).await.unwrap();

Expand All @@ -34,7 +35,7 @@ async fn blob_submit_and_get() {
.await
.unwrap();

received_blob.validate().unwrap();
received_blob.validate(AppVersion::V2).unwrap();
assert_blob_equal_to_sent(&received_blob, &blob);

let proofs = client
Expand All @@ -57,8 +58,8 @@ async fn blob_submit_and_get_all() {
let namespaces = &[random_ns(), random_ns()];

let blobs = &[
Blob::new(namespaces[0], random_bytes(5)).unwrap(),
Blob::new(namespaces[1], random_bytes(15)).unwrap(),
Blob::new(namespaces[0], random_bytes(5), AppVersion::V2).unwrap(),
Blob::new(namespaces[1], random_bytes(15), AppVersion::V2).unwrap(),
];

let submitted_height = blob_submit(&client, &blobs[..]).await.unwrap();
Expand All @@ -74,7 +75,7 @@ async fn blob_submit_and_get_all() {
for (idx, (blob, received_blob)) in blobs.iter().zip(received_blobs.iter()).enumerate() {
let namespace = namespaces[idx];

received_blob.validate().unwrap();
received_blob.validate(AppVersion::V2).unwrap();
assert_blob_equal_to_sent(received_blob, blob);

let proofs = client
Expand All @@ -91,7 +92,7 @@ async fn blob_submit_and_get_large() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(1024 * 1024);
let blob = Blob::new(namespace, data).unwrap();
let blob = Blob::new(namespace, data, AppVersion::V2).unwrap();

let submitted_height = blob_submit(&client, &[blob.clone()]).await.unwrap();

Expand All @@ -104,7 +105,7 @@ async fn blob_submit_and_get_large() {
.await
.unwrap();

blob.validate().unwrap();
blob.validate(AppVersion::V2).unwrap();
assert_blob_equal_to_sent(&received_blob, &blob);

let proofs = client
Expand All @@ -129,24 +130,24 @@ async fn blob_subscribe() {
assert!(received_blobs.blobs.is_none());

// submit and receive blob
let blob = Blob::new(namespace, random_bytes(10)).unwrap();
let blob = Blob::new(namespace, random_bytes(10), AppVersion::V2).unwrap();
let current_height = blob_submit(&client, &[blob.clone()]).await.unwrap();

let received = blobs_at_height(current_height, &mut incoming_blobs).await;
assert_eq!(received.len(), 1);
assert_blob_equal_to_sent(&received[0], &blob);

// submit blob to another ns
let blob_another_ns = Blob::new(random_ns(), random_bytes(10)).unwrap();
let blob_another_ns = Blob::new(random_ns(), random_bytes(10), AppVersion::V2).unwrap();
let current_height = blob_submit(&client, &[blob_another_ns]).await.unwrap();

let received = blobs_at_height(current_height, &mut incoming_blobs).await;
assert!(received.is_empty());

// submit and receive few blobs
let blob1 = Blob::new(namespace, random_bytes(10)).unwrap();
let blob2 = Blob::new(random_ns(), random_bytes(10)).unwrap(); // different ns
let blob3 = Blob::new(namespace, random_bytes(10)).unwrap();
let blob1 = Blob::new(namespace, random_bytes(10), AppVersion::V2).unwrap();
let blob2 = Blob::new(random_ns(), random_bytes(10), AppVersion::V2).unwrap(); // different ns
let blob3 = Blob::new(namespace, random_bytes(10), AppVersion::V2).unwrap();
let current_height = blob_submit(&client, &[blob1.clone(), blob2, blob3.clone()])
.await
.unwrap();
Expand All @@ -162,7 +163,7 @@ async fn blob_submit_too_large() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(5 * 1024 * 1024);
let blob = Blob::new(namespace, data).unwrap();
let blob = Blob::new(namespace, data, AppVersion::V2).unwrap();

blob_submit(&client, &[blob]).await.unwrap_err();
}
Expand All @@ -172,7 +173,7 @@ async fn blob_get_get_proof_wrong_ns() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(5);
let blob = Blob::new(namespace, data).unwrap();
let blob = Blob::new(namespace, data, AppVersion::V2).unwrap();

let submitted_height = blob_submit(&client, &[blob.clone()]).await.unwrap();

Expand All @@ -192,7 +193,7 @@ async fn blob_get_get_proof_wrong_commitment() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(5);
let blob = Blob::new(namespace, data).unwrap();
let blob = Blob::new(namespace, data, AppVersion::V2).unwrap();
let commitment = Commitment(random_bytes_array());

let submitted_height = blob_submit(&client, &[blob.clone()]).await.unwrap();
Expand Down
20 changes: 12 additions & 8 deletions rpc/tests/share.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg(not(target_arch = "wasm32"))]

use celestia_rpc::prelude::*;
use celestia_types::consts::appconsts::AppVersion;
use celestia_types::nmt::{Namespace, NamespacedSha2Hasher};
use celestia_types::{Blob, Share};

Expand Down Expand Up @@ -32,7 +33,7 @@ async fn get_shares_by_namespace() {
let blobs: Vec<_> = (0..4)
.map(|_| {
let data = random_bytes(1024);
Blob::new(namespace, data.clone()).unwrap()
Blob::new(namespace, data.clone(), AppVersion::V2).unwrap()
})
.collect();

Expand All @@ -45,8 +46,11 @@ async fn get_shares_by_namespace() {
.await
.unwrap();

let reconstructed =
Blob::reconstruct_all(ns_shares.rows.iter().flat_map(|row| row.shares.iter())).unwrap();
let reconstructed = Blob::reconstruct_all(
ns_shares.rows.iter().flat_map(|row| row.shares.iter()),
AppVersion::V2,
)
.unwrap();

assert_eq!(reconstructed, blobs);
}
Expand All @@ -70,7 +74,7 @@ async fn get_shares_range() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(1024);
let blob = Blob::new(namespace, data.clone()).unwrap();
let blob = Blob::new(namespace, data.clone(), AppVersion::V2).unwrap();
let commitment = blob.commitment;

let submitted_height = blob_submit(&client, &[blob]).await.unwrap();
Expand Down Expand Up @@ -125,7 +129,7 @@ async fn get_shares_by_namespace_wrong_ns() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(1024);
let blob = Blob::new(namespace, data.clone()).unwrap();
let blob = Blob::new(namespace, data.clone(), AppVersion::V2).unwrap();

let submitted_height = blob_submit(&client, &[blob]).await.unwrap();

Expand Down Expand Up @@ -166,7 +170,7 @@ async fn get_shares_by_namespace_wrong_ns_out_of_range() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(1024);
let blob = Blob::new(namespace, data.clone()).unwrap();
let blob = Blob::new(namespace, data.clone(), AppVersion::V2).unwrap();

let submitted_height = blob_submit(&client, &[blob]).await.unwrap();

Expand All @@ -192,7 +196,7 @@ async fn get_shares_by_namespace_wrong_roots() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(1024);
let blob = Blob::new(namespace, data.clone()).unwrap();
let blob = Blob::new(namespace, data.clone(), AppVersion::V2).unwrap();

blob_submit(&client, &[blob]).await.unwrap();

Expand All @@ -211,7 +215,7 @@ async fn get_eds() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = vec![1, 2, 3, 4];
let blob = Blob::new(namespace, data.clone()).unwrap();
let blob = Blob::new(namespace, data.clone(), AppVersion::V2).unwrap();

let submitted_height = blob_submit(&client, &[blob]).await.unwrap();

Expand Down
6 changes: 3 additions & 3 deletions rpc/tests/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::utils::{random_bytes, random_ns};
use celestia_rpc::prelude::*;
use celestia_types::{Blob, TxConfig};
use celestia_types::{AppVersion, Blob, TxConfig};

pub mod utils;

Expand Down Expand Up @@ -37,7 +37,7 @@ async fn submit_pay_for_blob() {
let client = new_test_client(AuthLevel::Write).await.unwrap();
let namespace = random_ns();
let data = random_bytes(5);
let blob = Blob::new(namespace, data).unwrap();
let blob = Blob::new(namespace, data, AppVersion::V2).unwrap();

let tx_response = client
.state_submit_pay_for_blob(&[blob.clone().into()], TxConfig::default())
Expand All @@ -49,6 +49,6 @@ async fn submit_pay_for_blob() {
.await
.unwrap();

received_blob.validate().unwrap();
received_blob.validate(AppVersion::V2).unwrap();
assert_eq!(received_blob.data, blob.data);
}
4 changes: 2 additions & 2 deletions types/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ and [`celestia-proto`](https://github.com/eigerco/lumina/proto) and support the
protobuf and serde to the format understood by nodes in celestia network.

```rust
use celestia_types::{Blob, nmt::Namespace};
use celestia_types::{AppVersion, Blob, nmt::Namespace};

let my_namespace = Namespace::new_v0(&[1, 2, 3, 4, 5]).expect("Invalid namespace");
let blob = Blob::new(my_namespace, b"some data to store on blockchain".to_vec())
let blob = Blob::new(my_namespace, b"some data to store on blockchain".to_vec(), AppVersion::V2)
.expect("Failed to create a blob");

assert_eq!(
Expand Down
Loading

0 comments on commit 5018d88

Please sign in to comment.