Skip to content

Commit

Permalink
fix: be consistent in endianness (rkyv crate) (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
eloylp authored Sep 11, 2024
1 parent c03baac commit 80171c6
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
16 changes: 8 additions & 8 deletions solana/crates/axelar-rkyv-encoding/src/types/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct Proof {
pub signers_with_signatures: HasheableSignersWithSignaturesBTreeMap,
pub threshold: U128,
pub nonce: u64,
nonce_be_bytes: [u8; 8],
nonce_le_bytes: [u8; 8],
}

impl Proof {
Expand All @@ -35,12 +35,12 @@ impl Proof {
),
threshold,
nonce,
nonce_be_bytes: nonce.to_be_bytes(),
nonce_le_bytes: nonce.to_le_bytes(),
}
}

pub fn nonce_be_bytes(&self) -> &[u8; 8] {
&self.nonce_be_bytes
pub fn nonce_le_bytes(&self) -> &[u8; 8] {
&self.nonce_le_bytes
}

pub fn verifier_set(&self) -> VerifierSet {
Expand Down Expand Up @@ -68,13 +68,13 @@ impl ArchivedProof {
visitor: &mut impl crate::visitor::ArchivedVisitor<'a>,
) {
// Follow `ArchivedVisitor::visit_verifier_set` exact steps
visitor.prefix_length(self.signers_with_signatures.len_be_bytes());
visitor.prefix_length(self.signers_with_signatures.len_le_bytes());
for (pubkey, weighted_signature) in self.signers_with_signatures.iter() {
visitor.visit_public_key(pubkey);
visitor.visit_u128(&weighted_signature.weight);
}
visitor.visit_u128(&self.threshold);
visitor.visit_u64(self.nonce_be_bytes());
visitor.visit_u64(self.nonce_le_bytes());
}

pub fn validate_for_message(&self, message: &[u8; 32]) -> Result<(), MessageValidationError> {
Expand Down Expand Up @@ -147,8 +147,8 @@ impl ArchivedProof {
self.signers_with_signatures.inner_map()
}

pub fn nonce_be_bytes(&self) -> &[u8; 8] {
&self.nonce_be_bytes
pub fn nonce_le_bytes(&self) -> &[u8; 8] {
&self.nonce_le_bytes
}
}

Expand Down
12 changes: 6 additions & 6 deletions solana/crates/axelar-rkyv-encoding/src/types/verifier_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Signers = BTreeMap<PublicKey, U128>;
#[archive_attr(derive(Debug, PartialEq, Eq, CheckBytes))]
pub struct VerifierSet {
pub(crate) created_at: u64,
created_at_be_bytes: [u8; 8],
created_at_le_bytes: [u8; 8],
pub(crate) signers: HasheableSignersBTreeMap,
pub(crate) quorum: U128,
}
Expand All @@ -26,7 +26,7 @@ impl VerifierSet {
pub fn new(created_at: u64, signers: Signers, quorum: U128) -> Self {
Self {
created_at,
created_at_be_bytes: created_at.to_be_bytes(),
created_at_le_bytes: created_at.to_le_bytes(),
signers: HasheableSignersBTreeMap::new(signers),
quorum,
}
Expand Down Expand Up @@ -60,8 +60,8 @@ impl VerifierSet {
self.created_at
}

pub fn created_at_be_bytes(&self) -> &[u8; 8] {
&self.created_at_be_bytes
pub fn created_at_le_bytes(&self) -> &[u8; 8] {
&self.created_at_le_bytes
}
}

Expand Down Expand Up @@ -103,8 +103,8 @@ impl ArchivedVerifierSet {
rkyv::check_archived_root::<VerifierSet>(bytes)
}

pub fn created_at_be_bytes(&self) -> &[u8; 8] {
&self.created_at_be_bytes
pub fn created_at_le_bytes(&self) -> &[u8; 8] {
&self.created_at_le_bytes
}
}

Expand Down
42 changes: 21 additions & 21 deletions solana/crates/axelar-rkyv-encoding/src/types/wrapper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ use super::{
#[archive(compare(PartialEq))]
#[archive_attr(derive(Debug, PartialEq, Eq, CheckBytes))]
pub struct HasheableMessageVec {
len_be_bytes: [u8; 8],
len_le_bytes: [u8; 8],
inner_vec: Vec<Message>,
}

impl HasheableMessageVec {
pub fn new(inner_vec: Vec<Message>) -> Self {
Self {
len_be_bytes: inner_vec.len().to_be_bytes(),
len_le_bytes: inner_vec.len().to_le_bytes(),
inner_vec,
}
}
Expand All @@ -40,8 +40,8 @@ impl HasheableMessageVec {
self.inner_vec.as_slice()
}

pub fn len_be_bytes(&self) -> &[u8; 8] {
&self.len_be_bytes
pub fn len_le_bytes(&self) -> &[u8; 8] {
&self.len_le_bytes
}

#[allow(dead_code)]
Expand All @@ -64,13 +64,13 @@ impl FromIterator<Message> for HasheableMessageVec {
impl ArchivedHasheableMessageVec {
pub fn new(inner_vec: ArchivedVec<ArchivedMessage>) -> Self {
Self {
len_be_bytes: inner_vec.len().to_be_bytes(),
len_le_bytes: inner_vec.len().to_le_bytes(),
inner_vec,
}
}

pub fn len_be_bytes(&self) -> &[u8; 8] {
&self.len_be_bytes
pub fn len_le_bytes(&self) -> &[u8; 8] {
&self.len_le_bytes
}

pub fn iter(&self) -> std::slice::Iter<'_, ArchivedMessage> {
Expand All @@ -82,14 +82,14 @@ impl ArchivedHasheableMessageVec {
#[archive(compare(PartialEq))]
#[archive_attr(derive(Debug, PartialEq, Eq, CheckBytes))]
pub struct HasheableSignersBTreeMap {
len_be_bytes: [u8; 8],
len_le_bytes: [u8; 8],
inner_map: BTreeMap<PublicKey, U128>,
}

impl HasheableSignersBTreeMap {
pub fn new(inner_map: BTreeMap<PublicKey, U128>) -> Self {
Self {
len_be_bytes: inner_map.len().to_be_bytes(),
len_le_bytes: inner_map.len().to_le_bytes(),
inner_map,
}
}
Expand All @@ -102,8 +102,8 @@ impl HasheableSignersBTreeMap {
self.inner_map.is_empty()
}

pub fn len_be_bytes(&self) -> &[u8; 8] {
&self.len_be_bytes
pub fn len_le_bytes(&self) -> &[u8; 8] {
&self.len_le_bytes
}

pub fn keys(&self) -> std::collections::btree_map::Keys<PublicKey, U128> {
Expand All @@ -126,7 +126,7 @@ impl HasheableSignersBTreeMap {
impl ArchivedHasheableSignersBTreeMap {
pub fn new(inner_map: ArchivedBTreeMap<ArchivedPublicKey, ArchivedU128>) -> Self {
Self {
len_be_bytes: inner_map.len().to_be_bytes(),
len_le_bytes: inner_map.len().to_le_bytes(),
inner_map,
}
}
Expand All @@ -139,8 +139,8 @@ impl ArchivedHasheableSignersBTreeMap {
self.inner_map.is_empty()
}

pub fn len_be_bytes(&self) -> &[u8; 8] {
&self.len_be_bytes
pub fn len_le_bytes(&self) -> &[u8; 8] {
&self.len_le_bytes
}

pub fn keys(&self) -> rkyv::collections::btree_map::Keys<ArchivedPublicKey, ArchivedU128> {
Expand All @@ -165,14 +165,14 @@ impl ArchivedHasheableSignersBTreeMap {
#[archive(compare(PartialEq))]
#[archive_attr(derive(Debug, PartialEq, Eq, CheckBytes))]
pub struct HasheableSignersWithSignaturesBTreeMap {
len_be_bytes: [u8; 8],
len_le_bytes: [u8; 8],
inner_map: BTreeMap<PublicKey, WeightedSigner>,
}

impl HasheableSignersWithSignaturesBTreeMap {
pub fn new(inner_map: BTreeMap<PublicKey, WeightedSigner>) -> Self {
Self {
len_be_bytes: inner_map.len().to_be_bytes(),
len_le_bytes: inner_map.len().to_le_bytes(),
inner_map,
}
}
Expand All @@ -185,8 +185,8 @@ impl HasheableSignersWithSignaturesBTreeMap {
self.inner_map.is_empty()
}

pub fn len_be_bytes(&self) -> &[u8; 8] {
&self.len_be_bytes
pub fn len_le_bytes(&self) -> &[u8; 8] {
&self.len_le_bytes
}

pub fn keys(&self) -> std::collections::btree_map::Keys<PublicKey, WeightedSigner> {
Expand Down Expand Up @@ -215,7 +215,7 @@ impl HasheableSignersWithSignaturesBTreeMap {
impl ArchivedHasheableSignersWithSignaturesBTreeMap {
pub fn new(inner_map: ArchivedBTreeMap<ArchivedPublicKey, ArchivedWeightedSigner>) -> Self {
Self {
len_be_bytes: inner_map.len().to_be_bytes(),
len_le_bytes: inner_map.len().to_le_bytes(),
inner_map,
}
}
Expand All @@ -228,8 +228,8 @@ impl ArchivedHasheableSignersWithSignaturesBTreeMap {
self.inner_map.is_empty()
}

pub fn len_be_bytes(&self) -> &[u8; 8] {
&self.len_be_bytes
pub fn len_le_bytes(&self) -> &[u8; 8] {
&self.len_le_bytes
}

pub fn keys(
Expand Down
20 changes: 10 additions & 10 deletions solana/crates/axelar-rkyv-encoding/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub trait Visitor<'a> {
..
} = proof;

self.prefix_length(signers_with_signatures.len_be_bytes());
self.prefix_length(signers_with_signatures.len_le_bytes());
for signature in signers_with_signatures.iter() {
self.visit_weighted_signature(signature.0, signature.1);
}
self.visit_u128(threshold);
self.visit_u64(proof.nonce_be_bytes());
self.visit_u64(proof.nonce_le_bytes());
}

fn visit_weighted_signature(&mut self, pubkey: &'a PublicKey, signature: &'a WeightedSigner) {
Expand Down Expand Up @@ -68,7 +68,7 @@ pub trait Visitor<'a> {
}

fn visit_messages(&mut self, messages: &'a HasheableMessageVec) {
self.prefix_length(messages.len_be_bytes());
self.prefix_length(messages.len_le_bytes());
for message in messages.iter() {
self.visit_message(message);
}
Expand All @@ -90,13 +90,13 @@ pub trait Visitor<'a> {
}

fn visit_verifier_set(&mut self, verifier_set: &'a VerifierSet) {
self.prefix_length(verifier_set.signers.len_be_bytes());
self.prefix_length(verifier_set.signers.len_le_bytes());
for (public_key, weight) in verifier_set.signers.iter() {
self.visit_public_key(public_key);
self.visit_u128(weight);
}
self.visit_u128(&verifier_set.quorum);
self.visit_u64(verifier_set.created_at_be_bytes())
self.visit_u64(verifier_set.created_at_le_bytes())
}

fn visit_public_key(&mut self, public_key: &'a PublicKey) {
Expand Down Expand Up @@ -147,12 +147,12 @@ pub trait ArchivedVisitor<'a> {
threshold,
..
} = proof;
self.prefix_length(signers_with_signatures.len_be_bytes());
self.prefix_length(signers_with_signatures.len_le_bytes());
for (pubkey, signature) in signers_with_signatures.iter() {
self.visit_weighted_signature(pubkey, signature);
}
self.visit_u128(threshold);
self.visit_u64(proof.nonce_be_bytes());
self.visit_u64(proof.nonce_le_bytes());
}

fn visit_weighted_signature(
Expand Down Expand Up @@ -196,7 +196,7 @@ pub trait ArchivedVisitor<'a> {
}

fn visit_messages(&mut self, messages: &'a ArchivedHasheableMessageVec) {
self.prefix_length(messages.len_be_bytes());
self.prefix_length(messages.len_le_bytes());
for message in messages.iter() {
self.visit_message(message);
}
Expand All @@ -218,13 +218,13 @@ pub trait ArchivedVisitor<'a> {
}

fn visit_verifier_set(&mut self, verifier_set: &'a ArchivedVerifierSet) {
self.prefix_length(verifier_set.signers.len_be_bytes());
self.prefix_length(verifier_set.signers.len_le_bytes());
for (public_key, weight) in verifier_set.signers.iter() {
self.visit_public_key(public_key);
self.visit_u128(weight);
}
self.visit_u128(&verifier_set.quorum);
self.visit_u64(verifier_set.created_at_be_bytes())
self.visit_u64(verifier_set.created_at_le_bytes())
}

fn visit_public_key(&mut self, public_key: &'a ArchivedPublicKey) {
Expand Down

0 comments on commit 80171c6

Please sign in to comment.