Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update rkyv encoding dep #34

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions ampd/src/solana/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl RpcSender for RpcRecorder {
"getAccountInfo" => serde_json::to_value(Response {
context: RpcResponseContext { slot: 1, api_version: None },
value: Value::Null,
})?,
})?,
"getTransaction" => serde_json::to_value(EncodedConfirmedTransactionWithStatusMeta {
slot: 2,
transaction: EncodedTransactionWithStatusMeta {
Expand Down Expand Up @@ -108,7 +108,7 @@ impl RpcSender for RpcRecorder {
}),
},
block_time: Some(1628633791),
})?,
})?,
"getVersion" => {
let version = Version::default();
json!(RpcVersionInfo {
Expand Down
24 changes: 6 additions & 18 deletions contracts/multisig-prover/src/encoding/rkyv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ pub fn encode(
.map(|x| to_weighted_signature(x, &payload_hash))
.transpose()?
.unwrap_or_else(|| {
let weight =
axelar_rkyv_encoding::types::U256::from_le(to_u256_le(signer.weight.u128()));
let weight = axelar_rkyv_encoding::types::U128::from(signer.weight.u128());
(
pubkey,
axelar_rkyv_encoding::types::WeightedSigner::new(None, weight),
Expand All @@ -40,9 +39,7 @@ pub fn encode(
signers_with_signatures.push(signature_found);
}
let created_at = verifier_set.created_at;
let threshold = axelar_rkyv_encoding::types::U256::from_le(crate::encoding::rkyv::to_u256_le(
verifier_set.threshold.u128(),
));
let threshold = axelar_rkyv_encoding::types::U128::from(verifier_set.threshold.u128());
let bytes = axelar_rkyv_encoding::encode::<1024>(
// Todo reason about this "1024" magic number.
created_at,
Expand All @@ -57,15 +54,14 @@ pub fn encode(
pub fn to_verifier_set(vs: &VerifierSet) -> Result<axelar_rkyv_encoding::types::VerifierSet> {
let mut signers: BTreeMap<
axelar_rkyv_encoding::types::PublicKey,
axelar_rkyv_encoding::types::U256,
axelar_rkyv_encoding::types::U128,
> = BTreeMap::new();

vs.signers
.iter()
.try_for_each(|(_, signer)| -> Result<()> {
let enc_pubkey = to_pub_key(&signer.pub_key)?;
let enc_weight =
axelar_rkyv_encoding::types::U256::from_le(to_u256_le(signer.weight.u128()));
let enc_weight = axelar_rkyv_encoding::types::U128::from(signer.weight.u128());

signers.insert(enc_pubkey, enc_weight);
Ok(())
Expand All @@ -74,7 +70,7 @@ pub fn to_verifier_set(vs: &VerifierSet) -> Result<axelar_rkyv_encoding::types::
Ok(axelar_rkyv_encoding::types::VerifierSet::new(
vs.created_at,
signers,
axelar_rkyv_encoding::types::U256::from_le(to_u256_le(vs.threshold.u128())),
axelar_rkyv_encoding::types::U128::from(vs.threshold.u128()),
))
}

Expand All @@ -89,13 +85,6 @@ fn to_pub_key(pk: &PublicKey) -> Result<axelar_rkyv_encoding::types::PublicKey>
})
}

// Fits a u128 into a u256 in little endian representation.
fn to_u256_le(u: u128) -> [u8; 32] {
let mut uin256 = [0u8; 32];
uin256[0..16].copy_from_slice(&u.to_le_bytes());
uin256
}

impl TryFrom<&Payload> for axelar_rkyv_encoding::types::Payload {
type Error = ContractError;
fn try_from(value: &Payload) -> std::result::Result<Self, Self::Error> {
Expand Down Expand Up @@ -134,8 +123,7 @@ pub fn to_weighted_signature(
)> {
let enc_pub_key = to_pub_key(&sig.signer.pub_key)?;
let enc_signature = to_signature(&sig.signature, &sig.signer.pub_key, payload_hash)?;
let enc_weight =
axelar_rkyv_encoding::types::U256::from_le(to_u256_le(sig.signer.weight.u128()));
let enc_weight = axelar_rkyv_encoding::types::U128::from(sig.signer.weight.u128());

Ok((
enc_pub_key,
Expand Down
Loading