From 28a5ad8495d71839f54442e99190df3c68e89d21 Mon Sep 17 00:00:00 2001 From: akhercha Date: Sat, 2 Nov 2024 17:06:32 +0100 Subject: [PATCH] dev(better_theoros): better opti --- rust/theoros/src/rpc/evm/mod.rs | 18 +++++------------- rust/theoros/src/types/calldata.rs | 19 +++++++++---------- solidity/test/PragmaDecoder.t.sol | 2 +- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/rust/theoros/src/rpc/evm/mod.rs b/rust/theoros/src/rpc/evm/mod.rs index 2baafed..44907d6 100644 --- a/rust/theoros/src/rpc/evm/mod.rs +++ b/rust/theoros/src/rpc/evm/mod.rs @@ -31,23 +31,15 @@ impl HyperlaneValidatorsMapping { Ok(Self(contracts)) } - pub fn get_validators(&self, chain_name: EvmChainName) -> Option<&Vec> { - self.0.get(&chain_name) + /// Get the available validators for a chain & their indexes + pub fn get_validators(&self, chain_name: &EvmChainName) -> Option> { + 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 { 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 { - match self.0.get(chain_name) { - Some(validators) => validators - .iter() - .position(|validator| validator == searched_validator) - .and_then(|pos| pos.try_into().ok()), - None => None, - } - } } diff --git a/rust/theoros/src/types/calldata.rs b/rust/theoros/src/types/calldata.rs index 553b00a..24d12d3 100644 --- a/rust/theoros/src/types/calldata.rs +++ b/rust/theoros/src/types/calldata.rs @@ -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, @@ -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 = 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 = validators_with_indices.into_iter().collect(); let signatures: Vec = 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, }; diff --git a/solidity/test/PragmaDecoder.t.sol b/solidity/test/PragmaDecoder.t.sol index 52779d4..64d6bd8 100644 --- a/solidity/test/PragmaDecoder.t.sol +++ b/solidity/test/PragmaDecoder.t.sol @@ -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"); }