Skip to content

Commit

Permalink
refactor: implement Deref and DerefMut for wrapper types (#471)
Browse files Browse the repository at this point in the history
* chore: add `solana-program` as a dev-dependency

* refactor: replace methods for `Deref` and `DerefMut`

* fix: gateway test

* chore: revert addding `solana-program` as a dev-dependency

This reverts commit 5b2ab387bfa8eac2cbb5471a490c52674ba1c495.
  • Loading branch information
tilacog authored Oct 10, 2024
1 parent c1cd18a commit 09c8619
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 108 deletions.
3 changes: 1 addition & 2 deletions solana/crates/axelar-rkyv-encoding/src/test_fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ pub fn random_valid_execute_data_and_verifier_set_for_payload(
.iter()
.map(|(pubkey, signing_key)| {
let signature = signing_key.sign(&original_payload_hash);
let weight = verifier_set.signers.inner_map().get(pubkey).unwrap();
let weight = verifier_set.signers.get(pubkey).unwrap();
(*pubkey, WeightedSigner::new(Some(signature), *weight))
})
.collect();
Expand Down Expand Up @@ -251,7 +251,6 @@ pub fn random_execute_data_and_verifier_set_for_payload_with_invalid_signatures(
let signature_bytes: &mut [u8] = match &mut execute_data
.proof
.signers_with_signatures
.mut_inner_map()
.values_mut()
.next()
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion solana/crates/axelar-rkyv-encoding/src/types/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl ArchivedProof {
pub fn signers_with_signatures(
&self,
) -> &ArchivedBTreeMap<ArchivedPublicKey, ArchivedWeightedSigner> {
self.signers_with_signatures.inner_map()
&self.signers_with_signatures
}

pub fn nonce_le_bytes(&self) -> &[u8; 8] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl VerifierSet {
}

pub fn signers(&self) -> &Signers {
self.signers.inner_map()
&self.signers
}

pub fn quorum(&self) -> &U128 {
Expand Down
135 changes: 32 additions & 103 deletions solana/crates/axelar-rkyv-encoding/src/types/wrapper_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! See original issue for more information: https://github.com/eigerco/solana-axelar-internal/issues/361
use std::collections::BTreeMap;
use std::ops::{Deref, DerefMut};

use rkyv::bytecheck::{self, CheckBytes};
use rkyv::collections::ArchivedBTreeMap;
Expand Down Expand Up @@ -37,21 +38,15 @@ impl HasheableMessageVec {
}
}

pub fn as_slice(&self) -> &[Message] {
self.inner_vec.as_slice()
}

pub fn len_le_bytes(&self) -> &[u8; 4] {
&self.len_le_bytes
}
}
impl Deref for HasheableMessageVec {
type Target = [Message];

#[allow(dead_code)]
pub(crate) fn inner_vec(self) -> Vec<Message> {
self.inner_vec
}

pub fn iter(&self) -> std::slice::Iter<Message> {
self.inner_vec.iter()
fn deref(&self) -> &Self::Target {
&self.inner_vec
}
}

Expand Down Expand Up @@ -81,9 +76,13 @@ impl ArchivedHasheableMessageVec {
pub fn len_le_bytes(&self) -> &[u8; 4] {
&self.len_le_bytes
}
}

pub fn iter(&self) -> std::slice::Iter<'_, ArchivedMessage> {
self.inner_vec.iter()
impl Deref for ArchivedHasheableMessageVec {
type Target = ArchivedVec<ArchivedMessage>;

fn deref(&self) -> &Self::Target {
&self.inner_vec
}
}

Expand All @@ -104,33 +103,17 @@ impl HasheableSignersBTreeMap {
}
}

pub fn len(&self) -> usize {
self.inner_map.len()
}

pub fn is_empty(&self) -> bool {
self.inner_map.is_empty()
}

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

pub fn keys(&self) -> std::collections::btree_map::Keys<PublicKey, U128> {
self.inner_map.keys()
}

pub fn values(&self) -> std::collections::btree_map::Values<PublicKey, U128> {
self.inner_map.values()
}
impl Deref for HasheableSignersBTreeMap {
type Target = BTreeMap<PublicKey, U128>;

pub(crate) fn inner_map(&self) -> &BTreeMap<PublicKey, U128> {
fn deref(&self) -> &Self::Target {
&self.inner_map
}

pub fn iter(&self) -> std::collections::btree_map::Iter<PublicKey, U128> {
self.inner_map.iter()
}
}

impl ArchivedHasheableSignersBTreeMap {
Expand All @@ -141,34 +124,17 @@ impl ArchivedHasheableSignersBTreeMap {
}
}

pub fn len(&self) -> usize {
self.inner_map.len()
}

pub fn is_empty(&self) -> bool {
self.inner_map.is_empty()
}

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

pub fn keys(&self) -> rkyv::collections::btree_map::Keys<ArchivedPublicKey, ArchivedU128> {
self.inner_map.keys()
}

pub fn values(&self) -> rkyv::collections::btree_map::Values<ArchivedPublicKey, ArchivedU128> {
self.inner_map.values()
}
impl Deref for ArchivedHasheableSignersBTreeMap {
type Target = ArchivedBTreeMap<ArchivedPublicKey, ArchivedU128>;

#[allow(dead_code)]
pub(crate) fn inner_map(&self) -> &ArchivedBTreeMap<ArchivedPublicKey, ArchivedU128> {
fn deref(&self) -> &Self::Target {
&self.inner_map
}

pub fn iter(&self) -> rkyv::collections::btree_map::Iter<'_, ArchivedPublicKey, ArchivedU128> {
self.inner_map.iter()
}
}

#[derive(Archive, Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
Expand All @@ -188,39 +154,24 @@ impl HasheableSignersWithSignaturesBTreeMap {
}
}

pub fn len(&self) -> usize {
self.inner_map.len()
}

pub fn is_empty(&self) -> bool {
self.inner_map.is_empty()
}

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

pub fn keys(&self) -> std::collections::btree_map::Keys<PublicKey, WeightedSigner> {
self.inner_map.keys()
}

pub fn values(&self) -> std::collections::btree_map::Values<PublicKey, WeightedSigner> {
self.inner_map.values()
}
impl Deref for HasheableSignersWithSignaturesBTreeMap {
type Target = BTreeMap<PublicKey, WeightedSigner>;

#[allow(dead_code)]
pub(crate) fn inner_map(&self) -> &BTreeMap<PublicKey, WeightedSigner> {
fn deref(&self) -> &Self::Target {
&self.inner_map
}
}

#[cfg(any(test, feature = "test-fixtures"))]
pub fn mut_inner_map(&mut self) -> &mut BTreeMap<PublicKey, WeightedSigner> {
#[cfg(any(test, feature = "test-fixtures"))]
impl DerefMut for HasheableSignersWithSignaturesBTreeMap {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner_map
}

pub fn iter(&self) -> std::collections::btree_map::Iter<PublicKey, WeightedSigner> {
self.inner_map.iter()
}
}

impl ArchivedHasheableSignersWithSignaturesBTreeMap {
Expand All @@ -231,37 +182,15 @@ impl ArchivedHasheableSignersWithSignaturesBTreeMap {
}
}

pub fn len(&self) -> usize {
self.inner_map.len()
}

pub fn is_empty(&self) -> bool {
self.inner_map.is_empty()
}

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

pub fn keys(
&self,
) -> rkyv::collections::btree_map::Keys<ArchivedPublicKey, ArchivedWeightedSigner> {
self.inner_map.keys()
}

pub fn values(
&self,
) -> rkyv::collections::btree_map::Values<ArchivedPublicKey, ArchivedWeightedSigner> {
self.inner_map.values()
}
impl Deref for ArchivedHasheableSignersWithSignaturesBTreeMap {
type Target = ArchivedBTreeMap<ArchivedPublicKey, ArchivedWeightedSigner>;

pub(crate) fn inner_map(&self) -> &ArchivedBTreeMap<ArchivedPublicKey, ArchivedWeightedSigner> {
fn deref(&self) -> &Self::Target {
&self.inner_map
}

pub fn iter(
&self,
) -> rkyv::collections::btree_map::Iter<'_, ArchivedPublicKey, ArchivedWeightedSigner> {
self.inner_map.iter()
}
}
1 change: 0 additions & 1 deletion solana/programs/gateway/tests/module/approve_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,6 @@ async fn fail_if_invalid_signatures() {
if let Some(x) = ex
.proof
.signers_with_signatures
.mut_inner_map()
.iter_mut()
.next()
.unwrap()
Expand Down

0 comments on commit 09c8619

Please sign in to comment.