Skip to content

Commit

Permalink
Moved Message & ProofResponse types to core
Browse files Browse the repository at this point in the history
  • Loading branch information
ToufeeqP committed Jan 11, 2024
1 parent e80c3ef commit 6aad812
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
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 core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "Apache-2.0"
[dependencies]
# Others
derive_more = { version = "0.99.17", default-features = false, features = ["constructor", "from", "add", "deref", "mul", "into"] }
ethabi = { version = "18.0.0", default-features = false }
hash256-std-hasher = { version = "0.15.2", default-features = false }
hex = { version = "0.4", optional = true, default-features = false, features = ["alloc", "serde"] }
log = { version = "0.4.8", default-features = false }
Expand Down
83 changes: 74 additions & 9 deletions core/src/data_proof_v2.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,80 @@
#[cfg(feature = "runtime")]
use binary_merkle_tree::MerkleProof;
use codec::{Decode, Encode};
use ethabi::{encode, Token};
use frame_support::BoundedVec;
#[cfg(feature = "runtime")]
use nomad_core::keccak256_concat;
use scale_info::TypeInfo;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use sp_core::H256;
use sp_core::{ConstU32, H256};
use sp_std::vec;
use sp_std::vec::Vec;
use thiserror_no_std::Error;

/// Max data supported on bidge (Ethereum calldata limits)
pub const BOUNDED_DATA_MAX_LENGTH: u32 = 102_400;
/// Maximum size of data allowed in the bridge
pub type BoundedData = BoundedVec<u8, ConstU32<BOUNDED_DATA_MAX_LENGTH>>;

/// Possible types of Messages allowed by Avail to bridge to other chains.
#[derive(
TypeInfo, Debug, Default, Eq, Clone, Encode, Decode, PartialEq, Serialize, Deserialize,
)]
#[serde(rename_all = "camelCase")]
pub enum MessageType {
ArbitraryMessage,
#[default]
FungibleToken,
}

impl From<MessageType> for Vec<u8> {
fn from(msg_type: MessageType) -> Self {
match msg_type {
MessageType::ArbitraryMessage => vec![0x01],
MessageType::FungibleToken => vec![0x02],
}
}
}

/// Message type used to bridge between Avail & other chains
#[derive(
Debug, Default, Clone, Eq, Encode, Decode, PartialEq, TypeInfo, Serialize, Deserialize,
)]
#[serde(rename_all = "camelCase")]
pub struct Message {
pub message_type: MessageType,
pub from: H256,
pub to: H256,
pub origin_domain: u32,
pub destination_domain: u32,
pub data: BoundedData,
pub id: u64, // a global nonce that is incremented with each leaf
}

impl Message {
pub fn abi_encode(self) -> Vec<u8> {
encode(&[Token::Tuple(vec![
Token::FixedBytes(self.message_type.into()),
Token::FixedBytes(self.from.to_fixed_bytes().to_vec()),
Token::FixedBytes(self.to.to_fixed_bytes().to_vec()),
Token::Uint(ethabi::Uint::from(self.origin_domain)),
Token::Uint(ethabi::Uint::from(self.destination_domain)),
Token::Bytes(self.data.into()),
Token::Uint(ethabi::Uint::from(self.id)),
])])
}
}

#[derive(Clone, Debug, PartialEq, Eq, Encode, Decode, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProofResponse {
pub data_proof: DataProofV2,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<Message>,
}

#[derive(PartialEq, Debug)]
pub enum SubTrie {
Left,
Expand Down Expand Up @@ -92,14 +158,13 @@ where
.map_err(|_| InvalidRoot)?
.into();

let leaf: H256;
if sub_trie == SubTrie::Right {
leaf = keccak_256(merkle_proof.leaf.as_ref()).into();
let leaf: H256 = if sub_trie == SubTrie::Right {
keccak_256(merkle_proof.leaf.as_ref()).into()
} else {
leaf = <[u8; 32]>::try_from(merkle_proof.leaf.as_ref())
<[u8; 32]>::try_from(merkle_proof.leaf.as_ref())
.map_err(|_| InvalidLeaf)?
.into();
}
.into()
};

let proof = merkle_proof
.proof
Expand All @@ -119,8 +184,8 @@ where
ensure!(leaf_index < number_of_leaves, InvalidLeafIndex);

let data_root: H256;
let mut blob_root: H256;
let mut bridge_root: H256;
let blob_root: H256;
let bridge_root: H256;
match sub_trie {
SubTrie::Right => {
data_root = keccak256_concat!(root, sub_trie_root.as_bytes());
Expand Down
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ pub use keccak256::Keccak256;

pub mod data_proof;
pub use data_proof::DataProof;
#[cfg(feature = "runtime")]
pub mod data_proof_v2;
#[cfg(feature = "runtime")]
pub use data_proof_v2::DataProofV2;

pub mod data_lookup;
Expand Down
2 changes: 1 addition & 1 deletion kate/src/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ pub fn build_proof<M: Metrics>(
let result = get_cell_row(cell);
let Ok((row, r_index, c_index)) = result else {
if let Ok(mut errors) = locked_errors.lock() {
errors.push(result.err().expect("We checked before that this is OK. "))
errors.push(result.expect_err("We checked before that this is OK."))
}
return;
};
Expand Down

0 comments on commit 6aad812

Please sign in to comment.