Skip to content

Commit

Permalink
dev(better_theoros): better opti
Browse files Browse the repository at this point in the history
  • Loading branch information
akhercha committed Nov 2, 2024
1 parent efbed1e commit 28a5ad8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
18 changes: 5 additions & 13 deletions rust/theoros/src/rpc/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,15 @@ impl HyperlaneValidatorsMapping {
Ok(Self(contracts))
}

pub fn get_validators(&self, chain_name: EvmChainName) -> Option<&Vec<Felt>> {
self.0.get(&chain_name)
/// Get the available validators for a chain & their indexes
pub fn get_validators(&self, chain_name: &EvmChainName) -> Option<Vec<(Felt, u8)>> {
self.0
.get(chain_name)
.map(|validators| validators.iter().enumerate().map(|(idx, validator)| (*validator, idx as u8)).collect())
}

/// Get all configured chains names
pub fn chain_names(&self) -> Vec<EvmChainName> {
self.0.keys().cloned().collect()
}

/// Get the index of a validator for a chain
pub fn validator_index(&self, chain_name: &EvmChainName, searched_validator: &Felt) -> Option<u8> {
match self.0.get(chain_name) {
Some(validators) => validators
.iter()
.position(|validator| validator == searched_validator)
.and_then(|pos| pos.try_into().ok()),
None => None,
}
}
}
19 changes: 9 additions & 10 deletions rust/theoros/src/types/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::Context;
use pragma_utils::conversions::alloy::hex_str_to_u256;
use serde::{Deserialize, Serialize};
use starknet::core::types::Felt;
use std::str::FromStr;
use std::{collections::HashMap, str::FromStr};

use crate::{
configs::evm_config::EvmChainName,
Expand Down Expand Up @@ -40,24 +40,23 @@ impl Calldata {
.await?
.context("No update found")?;

// Get validators and their signatures
let validators =
state.hyperlane_validators_mapping.get_validators(chain_name).context("No validators found")?;
let validators_with_indices =
state.hyperlane_validators_mapping.get_validators(&chain_name).context("No validators found")?;

let checkpoints = state.storage.signed_checkpoints().get(validators, update_info.nonce).await;
let validators: Vec<Felt> = validators_with_indices.iter().map(|(validator, _)| *validator).collect();
let checkpoints = state.storage.signed_checkpoints().get(&validators, update_info.nonce).await;
anyhow::ensure!(!checkpoints.is_empty(), "No signatures found");

let validator_index_map: HashMap<Felt, u8> = validators_with_indices.into_iter().collect();
let signatures: Vec<ValidatorSignature> = checkpoints
.iter()
.filter_map(|(validator, signed_checkpoint)| {
state
.hyperlane_validators_mapping
.validator_index(&chain_name, validator)
.map(|idx| ValidatorSignature { validator_index: idx, signature: signed_checkpoint.signature })
validator_index_map
.get(validator)
.map(|&idx| ValidatorSignature { validator_index: idx, signature: signed_checkpoint.signature })
})
.collect();

// Build payload using first checkpoint (all validators sign the same checkpoint since it's the same nonce)
let update = match update_info.update {
DispatchUpdate::SpotMedian { update, .. } => update,
};
Expand Down
2 changes: 1 addition & 1 deletion solidity/test/PragmaDecoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract PragmaHarnessTest is Test {
setupRaw();
// encoded update
bytes memory encodedUpdate =
hex"0100000170030100aeffc47b4d795e4978f92378ba5276697dd58fea699470add9e855cbb782b19b05da6229faf0cd99fef5a908b976102af271f6ec0e8d736b22df6dfd794fc3a81b0003c3de000000006726476300611a3d0060240f2bccef7e64f920eec05c5bfffbc48c6ceaa4efca8748772b60cbafc30536953cdd0dd5b8e24428e4fb6eab5c143daba15f62b24606e50d822508faefba5410a4a5555b80baf66026cec6481aa28a3291a2f25546b1187bf9ac18306d0003c3dec2e4e2ec168d8437f4c052570f20994259cd442d7068ae2ee2f33401e5243bb301006b00000000000000000000004254432f5553440000000000000000000000000000000000000000672647630008080000000000000000000000000000000000000000000000000000064fd736747b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004254432f5553440000000067264763";
hex"010000017003010084c952cde2a8b15194c9f60982a25399df800bf046f8984acef7e389cb2072ec23ac617442547a115cc69f038225bd36ceb1f748a8d8698f35de2110729d5f9a1b0003c4d1000000000000000000611a3d0060240f2bccef7e64f920eec05c5bfffbc48c6ceaa4efca8748772b60cbafc30536953cdd0dd5b8e24428e4fb6eab5c143daba15f62b24606e50d822508faef2bdfc6c6de418348c73cf78541d8f09c53e6f5eb56ef35a4db5b52e4919f302f0003c4d18660df2e348a6b5322d034079fc498a749263d71ba47a7acb222e3edece8eca901006b000000000000000000004c5553442f555344000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004c5553442f5553440000000000000000";
uint8 numUpdates = pragmaHarness.exposed_updateDataInfoFromUpdate(encodedUpdate);
assertEq(numUpdates, 1, "Number of updates should be 1");
}
Expand Down

0 comments on commit 28a5ad8

Please sign in to comment.