Skip to content

Commit

Permalink
sort out EDS
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Jan 7, 2024
1 parent 2282960 commit 92b6b1f
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 150 deletions.
1 change: 1 addition & 0 deletions proto/vendor/share/p2p/shwap/pb/shwap.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
syntax = "proto3";

// TODO: current celestia-node PR doesn't specify the package, we might want to align on it
package share.p2p.shwap;
import "pb/proof.proto"; // celestiaorg/nmt/pb/proof.proto

Expand Down
10 changes: 9 additions & 1 deletion tools/update-proto-vendor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ extract_urls ../target/proto-vendor-src \
https://github.com/cosmos/cosmos-proto/archive/refs/tags/v1.0.0-alpha4.tar.gz \
https://github.com/cosmos/gogoproto/archive/refs/tags/v1.4.11.tar.gz \
https://github.com/celestiaorg/go-header/archive/refs/heads/main.tar.gz \
https://github.com/googleapis/googleapis/archive/refs/heads/master.tar.gz
https://github.com/googleapis/googleapis/archive/refs/heads/master.tar.gz \
https://codeload.github.com/celestiaorg/celestia-node/zip/refs/heads/hlib/v2-prototype

mkdir -p vendor

Expand Down Expand Up @@ -70,6 +71,13 @@ for pb_dir in ../target/proto-vendor-src/celestia-node-main/share/*/*/pb; do
cp -r "$pb_dir" "$out_dir"
done

# TODO: replace with version from main, once it is merged
# https://github.com/celestiaorg/celestia-node/pull/2675
rm -rf vendor/share/p2p/shwap
mkdir -p vendor/share/p2p/shwap/pb
cp ../target/proto-vendor-src/celestia-node-hlib-v2-prototype/share/shwap/pb/shwap_pb.proto \
vendor/share/p2p/shwap/pb/shwap.proto

rm -rf vendor/tendermint
cp -r ../target/proto-vendor-src/celestia-core-0.34.x-celestia/proto/tendermint vendor

Expand Down
2 changes: 1 addition & 1 deletion types/src/byzantine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use tendermint_proto::Protobuf;

use crate::bail_validation;
use crate::consts::appconsts;
use crate::extended_data_square::AxisType;
use crate::fraud_proof::FraudProof;
use crate::nmt::{
Namespace, NamespaceProof, NamespacedHash, NamespacedHashExt, NMT_CODEC, NMT_ID_SIZE,
NMT_MULTIHASH_CODE, NS_SIZE,
};
use crate::rsmt2d::AxisType;
use crate::{Error, ExtendedHeader, Result, Share};

type Cid = CidGeneric<NMT_ID_SIZE>;
Expand Down
2 changes: 1 addition & 1 deletion types/src/data_availability_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use tendermint_proto::Protobuf;
use crate::consts::data_availability_header::{
MAX_EXTENDED_SQUARE_WIDTH, MIN_EXTENDED_SQUARE_WIDTH,
};
use crate::extended_data_square::AxisType;
use crate::hash::Hash;
use crate::nmt::{NamespacedHash, NamespacedHashExt};
use crate::rsmt2d::AxisType;
use crate::{bail_validation, Error, Result, ValidateBasic, ValidationError};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down
3 changes: 3 additions & 0 deletions types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ pub enum Error {
#[error("Data square index out of range: {0}")]
EdsIndexOutOfRange(usize),

#[error("Invalid dimensions of EDS")]
EdsInvalidDimentions,

#[error("Invalid zero block height")]
ZeroBlockHeight,
}
Expand Down
150 changes: 150 additions & 0 deletions types/src/extended_data_square.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
use std::result::Result as StdResult;

use nmt_rs::NamespaceMerkleHasher;

use crate::namespaced_data::{NamespacedData, NamespacedDataId};
use crate::nmt::{Namespace, NamespacedSha2Hasher, Nmt};
use crate::{DataAvailabilityHeader, Share};
use crate::{Error, Result};

/// Represents either Column or Row of the Data Square.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum AxisType {
Row = 0,
Col,
}

impl TryFrom<u8> for AxisType {
type Error = Error;

fn try_from(value: u8) -> StdResult<Self, Self::Error> {
match value {
0 => Ok(AxisType::Row),
1 => Ok(AxisType::Col),
n => Err(Error::InvalidAxis(n.into())),
}
}
}

/// Represents ExtendedDataSquare with deserialized Shares
pub struct ExtendedDataSquare {
shares: Vec<Share>,
square_len: usize,
}

impl ExtendedDataSquare {
/// Create a new EDS out of the provided shares. Returns error if share number doesn't
/// create a square
pub fn new(shares: Vec<Share>) -> Result<Self> {
let square_len = f64::sqrt(shares.len() as f64) as usize;
if square_len * square_len != shares.len() {
return Err(Error::EdsInvalidDimentions);
}

Ok(Self { shares, square_len })
}

/// Return row with index
pub fn row(&self, index: usize) -> Result<Vec<Share>> {
self.shares
.get(index * self.square_len..(index + 1) * self.square_len)
.ok_or(Error::EdsIndexOutOfRange(index))
.map(|v| v.to_vec())
}

/// Return colum with index
pub fn column(&self, mut index: usize) -> Result<Vec<Share>> {
let mut r = Vec::with_capacity(self.square_len);
while index < self.shares.len() {
r.push(
self.shares
.get(index)
.ok_or(Error::EdsIndexOutOfRange(index))?
.clone(),
);
index += self.square_len;
}
Ok(r)
}

/// Return column or row with the provided index
pub fn axis(&self, axis: AxisType, index: usize) -> Result<Vec<Share>> {
match axis {
AxisType::Col => self.column(index),
AxisType::Row => self.row(index),
}
}

/// Get EDS square length
pub fn square_len(&self) -> usize {
self.square_len
}

/// Return all the shares that belong to the provided namespace in the EDS.
/// Results are returned as a list of rows of shares with the inclusion proof
pub fn get_namespaced_data(
&self,
namespace: Namespace,
dah: &DataAvailabilityHeader,
height: u64,
) -> Result<Vec<NamespacedData>> {
let mut data = Vec::new();

for i in 0..self.square_len {
let row_root = dah.row_root(i).unwrap();
if !row_root.contains::<NamespacedSha2Hasher>(*namespace) {
continue;
}

let mut shares = Vec::with_capacity(self.square_len);

let mut tree = Nmt::with_hasher(NamespacedSha2Hasher::with_ignore_max_ns(true));
for s in &self.shares {
tree.push_leaf(s.data(), *s.namespace())
.map_err(Error::Nmt)?;
if s.namespace() == namespace {
shares.push(s.clone());
}
}

let proof = tree.get_namespace_proof(*namespace);
let namespaced_data_id = NamespacedDataId {
row_index: i.try_into().unwrap(),
namespace,
hash: dah.hash().as_ref().try_into().unwrap(),
block_height: height,
};

data.push(NamespacedData {
namespaced_data_id,
proof: proof.into(),
shares,
})
}

Ok(data)
}
}

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

#[test]
fn axis_type_serialization() {
assert_eq!(AxisType::Row as u8, 0);
assert_eq!(AxisType::Col as u8, 1);
}

#[test]
fn axis_type_deserialization() {
assert_eq!(AxisType::try_from(0).unwrap(), AxisType::Row);
assert_eq!(AxisType::try_from(1).unwrap(), AxisType::Col);

let axis_type_err = AxisType::try_from(2).unwrap_err();
assert!(matches!(axis_type_err, Error::InvalidAxis(2)));
let axis_type_err = AxisType::try_from(99).unwrap_err();
assert!(matches!(axis_type_err, Error::InvalidAxis(99)));
}
}
1 change: 1 addition & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod byzantine;
pub mod consts;
mod data_availability_header;
mod error;
mod extended_data_square;
mod extended_header;
pub mod fraud_proof;
pub mod hash;
Expand Down
2 changes: 1 addition & 1 deletion types/src/namespaced_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use tendermint_proto::Protobuf;

use crate::extended_data_square::AxisType;
use crate::nmt::{Namespace, NamespaceProof, NamespacedHashExt, HASH_SIZE, NS_SIZE};
use crate::rsmt2d::AxisType;
use crate::{DataAvailabilityHeader, Error, Result, Share};

const NAMESPACED_DATA_ID_SIZE: usize = NamespacedDataId::size();
Expand Down
12 changes: 4 additions & 8 deletions types/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use serde::{Deserialize, Serialize};
use tendermint::Hash;
use tendermint_proto::Protobuf;

use crate::extended_data_square::ExtendedDataSquare;
use crate::nmt::{NamespacedSha2Hasher, Nmt};
use crate::ExtendedDataSquare;
use crate::{Error, Result, Share};

const ROW_ID_SIZE: usize = RowId::size();
Expand Down Expand Up @@ -39,12 +39,8 @@ impl Row {
pub fn new(index: usize, eds: &ExtendedDataSquare, block_height: u64) -> Result<Self> {
let square_len = eds.square_len();

if index >= square_len {
return Err(Error::EdsIndexOutOfRange(index));
}

let row_id = RowId::new(index, block_height)?;
let mut shares = eds.row(index, square_len);
let mut shares = eds.row(index)?;
shares.truncate(square_len / 2);

Ok(Row { row_id, shares })
Expand Down Expand Up @@ -215,8 +211,8 @@ mod tests {
#[test]
fn index_calculation() {
let height = 100;
let shares = vec![vec![0; SHARE_SIZE]; 8 * 8];
let eds = ExtendedDataSquare::new(shares, "".to_string());
let shares = vec![Share::from_raw(&[0; SHARE_SIZE]).unwrap(); 8 * 8];
let eds = ExtendedDataSquare::new(shares).unwrap();

Row::new(1, &eds, height).unwrap();
Row::new(7, &eds, height).unwrap();
Expand Down
Loading

0 comments on commit 92b6b1f

Please sign in to comment.