From 44d0d0e6998bc183d901f9ae3d86114ba56f4423 Mon Sep 17 00:00:00 2001 From: Pavel Strakhov Date: Fri, 8 Nov 2024 13:59:26 +0000 Subject: [PATCH] chore: apply rustfmt --- apps/fortuna/src/api.rs | 89 ++--- apps/fortuna/src/api/chain_ids.rs | 10 +- apps/fortuna/src/api/index.rs | 5 +- apps/fortuna/src/api/live.rs | 5 +- apps/fortuna/src/api/metrics.rs | 5 +- apps/fortuna/src/api/ready.rs | 5 +- apps/fortuna/src/api/revelation.rs | 17 +- apps/fortuna/src/chain/eth_gas_oracle.rs | 12 +- apps/fortuna/src/chain/ethereum.rs | 72 +--- apps/fortuna/src/chain/nonce_manager.rs | 25 +- apps/fortuna/src/chain/reader.rs | 39 +- apps/fortuna/src/chain/traced_client.rs | 32 +- apps/fortuna/src/command.rs | 11 +- apps/fortuna/src/command/generate.rs | 10 +- apps/fortuna/src/command/get_request.rs | 5 +- apps/fortuna/src/command/inspect.rs | 16 +- apps/fortuna/src/command/register_provider.rs | 26 +- .../fortuna/src/command/request_randomness.rs | 5 +- apps/fortuna/src/command/run.rs | 64 +--- apps/fortuna/src/command/setup_provider.rs | 47 +-- apps/fortuna/src/command/withdraw_fees.rs | 16 +- apps/fortuna/src/config.rs | 44 +-- apps/fortuna/src/config/generate.rs | 5 +- apps/fortuna/src/config/get_request.rs | 5 +- apps/fortuna/src/config/inspect.rs | 6 +- apps/fortuna/src/config/register_provider.rs | 5 +- apps/fortuna/src/config/request_randomness.rs | 5 +- apps/fortuna/src/config/run.rs | 6 +- apps/fortuna/src/config/setup_provider.rs | 5 +- apps/fortuna/src/config/withdraw_fees.rs | 5 +- apps/fortuna/src/keeper.rs | 115 ++---- apps/fortuna/src/main.rs | 6 +- apps/fortuna/src/state.rs | 24 +- apps/hermes/server/src/api.rs | 22 +- .../server/src/api/metrics_middleware.rs | 17 +- apps/hermes/server/src/api/rest.rs | 24 +- .../server/src/api/rest/get_price_feed.rs | 16 +- apps/hermes/server/src/api/rest/get_vaa.rs | 28 +- .../server/src/api/rest/get_vaa_ccip.rs | 31 +- apps/hermes/server/src/api/rest/index.rs | 5 +- .../server/src/api/rest/latest_price_feeds.rs | 15 +- .../hermes/server/src/api/rest/latest_vaas.rs | 23 +- apps/hermes/server/src/api/rest/live.rs | 5 +- .../server/src/api/rest/price_feed_ids.rs | 11 +- apps/hermes/server/src/api/rest/ready.rs | 10 +- .../src/api/rest/v2/latest_price_updates.rs | 32 +- .../rest/v2/latest_publisher_stake_caps.rs | 17 +- .../src/api/rest/v2/price_feeds_metadata.rs | 11 +- apps/hermes/server/src/api/rest/v2/sse.rs | 28 +- .../api/rest/v2/timestamp_price_updates.rs | 28 +- apps/hermes/server/src/api/types.rs | 145 +++---- apps/hermes/server/src/api/ws.rs | 124 ++---- apps/hermes/server/src/config.rs | 9 +- apps/hermes/server/src/config/aggregate.rs | 5 +- apps/hermes/server/src/config/benchmarks.rs | 5 +- apps/hermes/server/src/config/metrics.rs | 5 +- apps/hermes/server/src/config/pythnet.rs | 5 +- apps/hermes/server/src/config/rpc.rs | 6 +- apps/hermes/server/src/config/wormhole.rs | 5 +- apps/hermes/server/src/main.rs | 10 +- apps/hermes/server/src/metrics_server.rs | 14 +- apps/hermes/server/src/network/pythnet.rs | 67 +--- apps/hermes/server/src/network/wormhole.rs | 36 +- apps/hermes/server/src/serde.rs | 7 +- apps/hermes/server/src/state.rs | 39 +- apps/hermes/server/src/state/aggregate.rs | 180 ++++----- .../server/src/state/aggregate/metrics.rs | 28 +- .../src/state/aggregate/wormhole_merkle.rs | 71 ++-- apps/hermes/server/src/state/benchmarks.rs | 12 +- apps/hermes/server/src/state/cache.rs | 72 ++-- apps/hermes/server/src/state/metrics.rs | 5 +- .../server/src/state/price_feeds_metadata.rs | 6 +- apps/hermes/server/src/state/wormhole.rs | 45 +-- governance/remote_executor/cli/src/cli.rs | 26 +- governance/remote_executor/cli/src/main.rs | 94 ++--- .../programs/remote-executor/src/lib.rs | 27 +- .../remote-executor/src/state/claim_record.rs | 5 +- .../src/state/governance_payload.rs | 62 ++- .../remote-executor/src/state/posted_vaa.rs | 8 +- .../src/tests/executor_simulator.rs | 88 ++--- .../src/tests/test_adversarial.rs | 19 +- .../src/tests/test_basic_instructions.rs | 19 +- .../src/instructions/create_buffer.rs | 17 +- .../src/instructions/delete_buffer.rs | 6 +- .../message_buffer/src/instructions/mod.rs | 13 +- .../src/instructions/put_all.rs | 8 +- .../src/instructions/resize_buffer.rs | 8 +- .../programs/message_buffer/src/lib.rs | 17 +- .../src/state/message_buffer.rs | 39 +- .../programs/message_buffer/src/state/mod.rs | 5 +- .../message_buffer/src/state/whitelist.rs | 10 +- .../src/instructions/add_price.rs | 42 +-- .../src/instructions/cpi_max_test.rs | 11 +- .../mock-cpi-caller/src/instructions/mod.rs | 6 +- .../src/instructions/update_price.rs | 31 +- .../programs/mock-cpi-caller/src/lib.rs | 13 +- .../programs/mock-cpi-caller/src/message.rs | 5 +- .../mock-cpi-caller/src/message/price.rs | 51 ++- .../programs/mock-cpi-caller/src/state/mod.rs | 8 +- .../mock-cpi-caller/src/state/price.rs | 20 +- .../mock-cpi-caller/tests/cases/mod.rs | 5 +- .../tests/cases/test_create_buffer.rs | 3 - .../tests/cases/test_delete_buffer.rs | 1 - .../tests/cases/test_put_all.rs | 1 - .../tests/cases/test_resize_buffer.rs | 20 - .../tests/cases/test_set_allowed_programs.rs | 1 - .../mock-cpi-caller/tests/program_test/mod.rs | 63 +--- .../examples/generate_pyth_data.rs | 5 +- .../pythnet_sdk/src/accumulators/merkle.rs | 48 +-- pythnet/pythnet_sdk/src/accumulators/mul.rs | 14 +- pythnet/pythnet_sdk/src/hashers.rs | 10 +- pythnet/pythnet_sdk/src/hashers/keccak256.rs | 10 +- .../pythnet_sdk/src/hashers/keccak256_160.rs | 11 +- pythnet/pythnet_sdk/src/hashers/prime.rs | 7 +- pythnet/pythnet_sdk/src/lib.rs | 5 +- pythnet/pythnet_sdk/src/messages.rs | 75 ++-- pythnet/pythnet_sdk/src/test_utils/mod.rs | 80 ++-- pythnet/pythnet_sdk/src/wire.rs | 126 +++---- pythnet/pythnet_sdk/src/wire/array.rs | 6 +- pythnet/pythnet_sdk/src/wire/de.rs | 22 +- pythnet/pythnet_sdk/src/wire/prefixed_vec.rs | 23 +- pythnet/pythnet_sdk/src/wire/ser.rs | 21 +- pythnet/pythnet_sdk/src/wormhole.rs | 58 ++- pythnet/stake_caps_parameters/cli/src/cli.rs | 13 +- pythnet/stake_caps_parameters/cli/src/main.rs | 24 +- .../programs/stake_caps_parameters/src/lib.rs | 9 +- .../tests/test_stake_caps_parameters.rs | 47 +-- .../cosmwasm/contracts/pyth/src/bin/schema.rs | 10 +- .../cosmwasm/contracts/pyth/src/contract.rs | 354 +++++++----------- .../cosmwasm/contracts/pyth/src/governance.rs | 34 +- .../cosmwasm/contracts/pyth/src/injective.rs | 49 +-- .../cosmwasm/contracts/pyth/src/msg.rs | 14 +- .../cosmwasm/contracts/pyth/src/state.rs | 40 +- .../examples/cw-contract/src/bin/schema.rs | 7 +- .../examples/cw-contract/src/contract.rs | 77 +--- .../cosmwasm/examples/cw-contract/src/msg.rs | 14 +- .../examples/cw-contract/src/state.rs | 7 +- target_chains/cosmwasm/sdk/rust/src/error.rs | 5 +- target_chains/cosmwasm/sdk/rust/src/lib.rs | 29 +- .../cosmwasm/sdk/rust/src/testing.rs | 28 +- target_chains/fuel/contracts/src/constants.rs | 5 +- .../fuel/contracts/tests/functions/mod.rs | 2 +- .../tests/functions/pyth_core/ema_price.rs | 3 +- .../pyth_core/ema_price_no_older_than.rs | 3 +- .../functions/pyth_core/ema_price_unsafe.rs | 3 +- .../pyth_core/price_no_older_than.rs | 3 +- .../tests/functions/pyth_init/constructor.rs | 6 +- .../contracts/tests/utils/interface/mod.rs | 2 +- target_chains/near/example/src/lib.rs | 20 +- target_chains/near/receiver/src/error.rs | 5 +- target_chains/near/receiver/src/ext.rs | 11 +- target_chains/near/receiver/src/governance.rs | 57 +-- target_chains/near/receiver/src/lib.rs | 50 +-- target_chains/near/receiver/src/state.rs | 95 +++-- target_chains/near/receiver/src/tests.rs | 11 +- .../near/receiver/tests/workspaces.rs | 210 +++++------ target_chains/near/wormhole-stub/src/lib.rs | 9 +- target_chains/solana/cli/src/cli.rs | 19 +- target_chains/solana/cli/src/main.rs | 88 ++--- .../solana/common_test_utils/src/lib.rs | 70 ++-- .../solana/program_simulator/src/lib.rs | 26 +- .../programs/pyth-price-store/src/accounts.rs | 5 +- .../pyth-price-store/src/accounts/buffer.rs | 24 +- .../pyth-price-store/src/accounts/config.rs | 9 +- .../pyth-price-store/src/accounts/errors.rs | 5 +- .../src/accounts/publisher_config.rs | 11 +- .../pyth-price-store/src/instruction.rs | 11 +- .../pyth-price-store/src/processor.rs | 9 +- .../src/processor/initialize.rs | 35 +- .../src/processor/initialize_publisher.rs | 51 +-- .../src/processor/submit_prices.rs | 20 +- .../programs/pyth-price-store/src/validate.rs | 15 +- .../programs/pyth-push-oracle/src/lib.rs | 36 +- .../programs/pyth-push-oracle/src/sdk.rs | 27 +- .../tests/test_update_price_feed.rs | 28 +- .../programs/pyth-solana-receiver/src/lib.rs | 82 ++-- .../programs/pyth-solana-receiver/src/sdk.rs | 79 ++-- .../tests/test_governance.rs | 43 +-- .../tests/test_post_price_update_from_vaa.rs | 52 +-- .../tests/test_post_updates.rs | 35 +- .../tests/test_post_updates_atomic.rs | 30 +- .../pyth_solana_receiver_sdk/src/config.rs | 38 +- .../src/cpi/accounts.rs | 24 +- .../pyth_solana_receiver_sdk/src/cpi/mod.rs | 10 +- .../pyth_solana_receiver_sdk/src/lib.rs | 21 +- .../pyth_solana_receiver_sdk/src/pda.rs | 5 +- .../src/price_update.rs | 71 ++-- wormhole_attester/sdk/rust/src/lib.rs | 92 ++--- 188 files changed, 1672 insertions(+), 3899 deletions(-) diff --git a/apps/fortuna/src/api.rs b/apps/fortuna/src/api.rs index b74258e777..2ee00af49a 100644 --- a/apps/fortuna/src/api.rs +++ b/apps/fortuna/src/api.rs @@ -1,47 +1,27 @@ use { crate::{ - chain::reader::{ - BlockNumber, - BlockStatus, - EntropyReader, - }, + chain::reader::{BlockNumber, BlockStatus, EntropyReader}, state::HashChainState, }, anyhow::Result, axum::{ body::Body, http::StatusCode, - response::{ - IntoResponse, - Response, - }, + response::{IntoResponse, Response}, routing::get, Router, }, ethers::core::types::Address, prometheus_client::{ encoding::EncodeLabelSet, - metrics::{ - counter::Counter, - family::Family, - }, + metrics::{counter::Counter, family::Family}, registry::Registry, }, - std::{ - collections::HashMap, - sync::Arc, - }, + std::{collections::HashMap, sync::Arc}, tokio::sync::RwLock, url::Url, }; -pub use { - chain_ids::*, - index::*, - live::*, - metrics::*, - ready::*, - revelation::*, -}; +pub use {chain_ids::*, index::*, live::*, metrics::*, ready::*, revelation::*}; mod chain_ids; mod index; @@ -99,16 +79,16 @@ impl ApiState { #[derive(Clone)] pub struct BlockchainState { /// The chain id for this blockchain, useful for logging - pub id: ChainId, + pub id: ChainId, /// The hash chain(s) required to serve random numbers for this blockchain - pub state: Arc, + pub state: Arc, /// The contract that the server is fulfilling requests for. - pub contract: Arc, + pub contract: Arc, /// The address of the provider that this server is operating for. - pub provider_address: Address, + pub provider_address: Address, /// The server will wait for this many block confirmations of a request before revealing /// the random number. - pub reveal_delay_blocks: BlockNumber, + pub reveal_delay_blocks: BlockNumber, /// The BlockStatus of the block that is considered to be confirmed on the blockchain. /// For eg., Finalized, Safe pub confirmed_block_status: BlockStatus, @@ -194,35 +174,16 @@ pub fn get_register_uri(base_uri: &str, chain_id: &str) -> Result { mod test { use { crate::{ - api::{ - self, - ApiState, - BinaryEncoding, - Blob, - BlockchainState, - GetRandomValueResponse, - }, - chain::reader::{ - mock::MockEntropyReader, - BlockStatus, - }, - state::{ - HashChainState, - PebbleHashChain, - }, + api::{self, ApiState, BinaryEncoding, Blob, BlockchainState, GetRandomValueResponse}, + chain::reader::{mock::MockEntropyReader, BlockStatus}, + state::{HashChainState, PebbleHashChain}, }, axum::http::StatusCode, - axum_test::{ - TestResponse, - TestServer, - }, + axum_test::{TestResponse, TestServer}, ethers::prelude::Address, lazy_static::lazy_static, prometheus_client::registry::Registry, - std::{ - collections::HashMap, - sync::Arc, - }, + std::{collections::HashMap, sync::Arc}, tokio::sync::RwLock, }; @@ -245,11 +206,11 @@ mod test { let eth_read = Arc::new(MockEntropyReader::with_requests(10, &[])); let eth_state = BlockchainState { - id: "ethereum".into(), - state: ETH_CHAIN.clone(), - contract: eth_read.clone(), - provider_address: PROVIDER, - reveal_delay_blocks: 1, + id: "ethereum".into(), + state: ETH_CHAIN.clone(), + contract: eth_read.clone(), + provider_address: PROVIDER, + reveal_delay_blocks: 1, confirmed_block_status: BlockStatus::Latest, }; @@ -258,11 +219,11 @@ mod test { let avax_read = Arc::new(MockEntropyReader::with_requests(10, &[])); let avax_state = BlockchainState { - id: "avalanche".into(), - state: AVAX_CHAIN.clone(), - contract: avax_read.clone(), - provider_address: PROVIDER, - reveal_delay_blocks: 2, + id: "avalanche".into(), + state: AVAX_CHAIN.clone(), + contract: avax_read.clone(), + provider_address: PROVIDER, + reveal_delay_blocks: 2, confirmed_block_status: BlockStatus::Latest, }; diff --git a/apps/fortuna/src/api/chain_ids.rs b/apps/fortuna/src/api/chain_ids.rs index f29e1eda37..0df175443f 100644 --- a/apps/fortuna/src/api/chain_ids.rs +++ b/apps/fortuna/src/api/chain_ids.rs @@ -1,13 +1,7 @@ use { - crate::api::{ - ChainId, - RestError, - }, + crate::api::{ChainId, RestError}, anyhow::Result, - axum::{ - extract::State, - Json, - }, + axum::{extract::State, Json}, }; /// Get the list of supported chain ids diff --git a/apps/fortuna/src/api/index.rs b/apps/fortuna/src/api/index.rs index fe83daabe0..7ef4081d01 100644 --- a/apps/fortuna/src/api/index.rs +++ b/apps/fortuna/src/api/index.rs @@ -1,7 +1,4 @@ -use axum::{ - response::IntoResponse, - Json, -}; +use axum::{response::IntoResponse, Json}; /// This is the index page for the REST service. It lists all the available endpoints. /// diff --git a/apps/fortuna/src/api/live.rs b/apps/fortuna/src/api/live.rs index 66343cc6f1..2a6f8bff4d 100644 --- a/apps/fortuna/src/api/live.rs +++ b/apps/fortuna/src/api/live.rs @@ -1,9 +1,6 @@ use axum::{ http::StatusCode, - response::{ - IntoResponse, - Response, - }, + response::{IntoResponse, Response}, }; pub async fn live() -> Response { diff --git a/apps/fortuna/src/api/metrics.rs b/apps/fortuna/src/api/metrics.rs index 8e162f0523..7977c58d96 100644 --- a/apps/fortuna/src/api/metrics.rs +++ b/apps/fortuna/src/api/metrics.rs @@ -1,10 +1,7 @@ //! Exposing prometheus metrics via HTTP in openmetrics format. use { - axum::{ - extract::State, - response::IntoResponse, - }, + axum::{extract::State, response::IntoResponse}, prometheus_client::encoding::text::encode, }; diff --git a/apps/fortuna/src/api/ready.rs b/apps/fortuna/src/api/ready.rs index e3834b94cd..976d16680b 100644 --- a/apps/fortuna/src/api/ready.rs +++ b/apps/fortuna/src/api/ready.rs @@ -1,9 +1,6 @@ use axum::{ http::StatusCode, - response::{ - IntoResponse, - Response, - }, + response::{IntoResponse, Response}, }; pub async fn ready() -> Response { diff --git a/apps/fortuna/src/api/revelation.rs b/apps/fortuna/src/api/revelation.rs index d55bda75cd..aca52985ee 100644 --- a/apps/fortuna/src/api/revelation.rs +++ b/apps/fortuna/src/api/revelation.rs @@ -1,25 +1,14 @@ use { - crate::api::{ - ChainId, - RequestLabel, - RestError, - }, + crate::api::{ChainId, RequestLabel, RestError}, anyhow::Result, axum::{ - extract::{ - Path, - Query, - State, - }, + extract::{Path, Query, State}, Json, }, pythnet_sdk::wire::array, serde_with::serde_as, tokio::try_join, - utoipa::{ - IntoParams, - ToSchema, - }, + utoipa::{IntoParams, ToSchema}, }; /// Reveal the random value for a given sequence number and blockchain. diff --git a/apps/fortuna/src/chain/eth_gas_oracle.rs b/apps/fortuna/src/chain/eth_gas_oracle.rs index 5961e25e94..869c00eab7 100644 --- a/apps/fortuna/src/chain/eth_gas_oracle.rs +++ b/apps/fortuna/src/chain/eth_gas_oracle.rs @@ -2,17 +2,11 @@ use { axum::async_trait, ethers::{ prelude::{ - gas_oracle::{ - GasOracleError, - Result, - }, + gas_oracle::{GasOracleError, Result}, GasOracle, }, providers::Middleware, - types::{ - I256, - U256, - }, + types::{I256, U256}, }, }; @@ -35,12 +29,10 @@ pub const SURGE_THRESHOLD_1: u64 = 40_000; pub const SURGE_THRESHOLD_2: u64 = 100_000; pub const SURGE_THRESHOLD_3: u64 = 200_000; - /// The threshold max change/difference (in %) at which we will ignore the fee history values /// under it. pub const EIP1559_FEE_ESTIMATION_THRESHOLD_MAX_CHANGE: i64 = 200; - /// Gas oracle from a [`Middleware`] implementation such as an /// Ethereum RPC provider. #[derive(Clone, Debug)] diff --git a/apps/fortuna/src/chain/ethereum.rs b/apps/fortuna/src/chain/ethereum.rs index 106dd8b502..481c05517b 100644 --- a/apps/fortuna/src/chain/ethereum.rs +++ b/apps/fortuna/src/chain/ethereum.rs @@ -4,64 +4,24 @@ use { chain::{ eth_gas_oracle::EthProviderOracle, nonce_manager::NonceManagerMiddleware, - reader::{ - self, - BlockNumber, - BlockStatus, - EntropyReader, - RequestedWithCallbackEvent, - }, - traced_client::{ - RpcMetrics, - TracedClient, - }, + reader::{self, BlockNumber, BlockStatus, EntropyReader, RequestedWithCallbackEvent}, + traced_client::{RpcMetrics, TracedClient}, }, config::EthereumConfig, }, - anyhow::{ - anyhow, - Error, - Result, - }, + anyhow::{anyhow, Error, Result}, axum::async_trait, ethers::{ abi::RawLog, - contract::{ - abigen, - ContractCall, - EthLogDecode, - }, + contract::{abigen, ContractCall, EthLogDecode}, core::types::Address, - middleware::{ - gas_oracle::GasOracleMiddleware, - MiddlewareError, - SignerMiddleware, - }, - prelude::{ - BlockId, - JsonRpcClient, - PendingTransaction, - TransactionRequest, - }, - providers::{ - Http, - Middleware, - Provider, - }, - signers::{ - LocalWallet, - Signer, - }, - types::{ - transaction::eip2718::TypedTransaction, - BlockNumber as EthersBlockNumber, - U256, - }, - }, - sha3::{ - Digest, - Keccak256, + middleware::{gas_oracle::GasOracleMiddleware, MiddlewareError, SignerMiddleware}, + prelude::{BlockId, JsonRpcClient, PendingTransaction, TransactionRequest}, + providers::{Http, Middleware, Provider}, + signers::{LocalWallet, Signer}, + types::{transaction::eip2718::TypedTransaction, BlockNumber as EthersBlockNumber, U256}, }, + sha3::{Digest, Keccak256}, std::sync::Arc, thiserror::Error, }; @@ -94,7 +54,7 @@ pub type InstrumentedPythContract = PythRandom>; #[derive(Clone, Debug)] pub struct LegacyTxMiddleware { use_legacy_tx: bool, - inner: M, + inner: M, } impl LegacyTxMiddleware { @@ -333,10 +293,10 @@ impl EntropyReader for PythRandom> { // sequence_number == 0 means the request does not exist. if r.sequence_number != 0 { Ok(Some(reader::Request { - provider: r.provider, + provider: r.provider, sequence_number: r.sequence_number, - block_number: r.block_number.try_into()?, - use_blockhash: r.use_blockhash, + block_number: r.block_number.try_into()?, + use_blockhash: r.use_blockhash, })) } else { Ok(None) @@ -370,9 +330,9 @@ impl EntropyReader for PythRandom> { Ok(res .iter() .map(|r| RequestedWithCallbackEvent { - sequence_number: r.sequence_number, + sequence_number: r.sequence_number, user_random_number: r.user_random_number, - provider_address: r.request.provider, + provider_address: r.request.provider, }) .collect()) } diff --git a/apps/fortuna/src/chain/nonce_manager.rs b/apps/fortuna/src/chain/nonce_manager.rs index f09e03df7c..a414f1da77 100644 --- a/apps/fortuna/src/chain/nonce_manager.rs +++ b/apps/fortuna/src/chain/nonce_manager.rs @@ -4,21 +4,10 @@ use { axum::async_trait, ethers::{ - providers::{ - Middleware, - MiddlewareError, - PendingTransaction, - }, - types::{ - transaction::eip2718::TypedTransaction, - *, - }, - }, - std::sync::atomic::{ - AtomicBool, - AtomicU64, - Ordering, + providers::{Middleware, MiddlewareError, PendingTransaction}, + types::{transaction::eip2718::TypedTransaction, *}, }, + std::sync::atomic::{AtomicBool, AtomicU64, Ordering}, thiserror::Error, }; @@ -26,11 +15,11 @@ use { /// Middleware used for calculating nonces locally, useful for signing multiple /// consecutive transactions without waiting for them to hit the mempool pub struct NonceManagerMiddleware { - inner: M, - init_guard: futures_locks::Mutex<()>, + inner: M, + init_guard: futures_locks::Mutex<()>, initialized: AtomicBool, - nonce: AtomicU64, - address: Address, + nonce: AtomicU64, + address: Address, } impl NonceManagerMiddleware diff --git a/apps/fortuna/src/chain/reader.rs b/apps/fortuna/src/chain/reader.rs index 30aa8350bd..89956ee3ae 100644 --- a/apps/fortuna/src/chain/reader.rs +++ b/apps/fortuna/src/chain/reader.rs @@ -1,11 +1,7 @@ use { anyhow::Result, axum::async_trait, - ethers::types::{ - Address, - BlockNumber as EthersBlockNumber, - U256, - }, + ethers::types::{Address, BlockNumber as EthersBlockNumber, U256}, }; pub type BlockNumber = u64; @@ -35,9 +31,9 @@ impl From for EthersBlockNumber { #[derive(Clone)] pub struct RequestedWithCallbackEvent { - pub sequence_number: u64, + pub sequence_number: u64, pub user_random_number: [u8; 32], - pub provider_address: Address, + pub provider_address: Address, } /// EntropyReader is the read-only interface of the Entropy contract. @@ -73,29 +69,20 @@ pub trait EntropyReader: Send + Sync { /// aren't used in fortuna anywhere. Feel free to add any missing fields as necessary.) #[derive(Clone, Debug)] pub struct Request { - pub provider: Address, + pub provider: Address, pub sequence_number: u64, // The block number where this request was created - pub block_number: BlockNumber, - pub use_blockhash: bool, + pub block_number: BlockNumber, + pub use_blockhash: bool, } - #[cfg(test)] pub mod mock { use { - crate::chain::reader::{ - BlockNumber, - BlockStatus, - EntropyReader, - Request, - }, + crate::chain::reader::{BlockNumber, BlockStatus, EntropyReader, Request}, anyhow::Result, axum::async_trait, - ethers::types::{ - Address, - U256, - }, + ethers::types::{Address, U256}, std::sync::RwLock, }; @@ -105,7 +92,7 @@ pub mod mock { pub struct MockEntropyReader { block_number: RwLock, /// The set of requests that are currently in-flight. - requests: RwLock>, + requests: RwLock>, } impl MockEntropyReader { @@ -115,14 +102,14 @@ pub mod mock { ) -> MockEntropyReader { MockEntropyReader { block_number: RwLock::new(block_number), - requests: RwLock::new( + requests: RwLock::new( requests .iter() .map(|&(a, s, b, u)| Request { - provider: a, + provider: a, sequence_number: s, - block_number: b, - use_blockhash: u, + block_number: b, + use_blockhash: u, }) .collect(), ), diff --git a/apps/fortuna/src/chain/traced_client.rs b/apps/fortuna/src/chain/traced_client.rs index 83f50810b0..3685f4d6c2 100644 --- a/apps/fortuna/src/chain/traced_client.rs +++ b/apps/fortuna/src/chain/traced_client.rs @@ -4,41 +4,27 @@ use { axum::async_trait, ethers::{ prelude::Http, - providers::{ - HttpClientError, - JsonRpcClient, - Provider, - }, + providers::{HttpClientError, JsonRpcClient, Provider}, }, prometheus_client::{ encoding::EncodeLabelSet, - metrics::{ - counter::Counter, - family::Family, - histogram::Histogram, - }, + metrics::{counter::Counter, family::Family, histogram::Histogram}, registry::Registry, }, - std::{ - str::FromStr, - sync::Arc, - }, - tokio::{ - sync::RwLock, - time::Instant, - }, + std::{str::FromStr, sync::Arc}, + tokio::{sync::RwLock, time::Instant}, }; #[derive(Debug, Clone, PartialEq, Eq, Hash, EncodeLabelSet)] pub struct RpcLabel { chain_id: ChainId, - method: String, + method: String, } #[derive(Debug)] pub struct RpcMetrics { - count: Family, - latency: Family, + count: Family, + latency: Family, errors_count: Family, } @@ -87,7 +73,7 @@ pub struct TracedClient { inner: Http, chain_id: ChainId, - metrics: Arc, + metrics: Arc, } #[async_trait] @@ -105,7 +91,7 @@ impl JsonRpcClient for TracedClient { let start = Instant::now(); let label = &RpcLabel { chain_id: self.chain_id.clone(), - method: method.to_string(), + method: method.to_string(), }; self.metrics.count.get_or_create(label).inc(); let res = match self.inner.request(method, params).await { diff --git a/apps/fortuna/src/command.rs b/apps/fortuna/src/command.rs index bf3a7b0856..3d895af458 100644 --- a/apps/fortuna/src/command.rs +++ b/apps/fortuna/src/command.rs @@ -8,12 +8,7 @@ mod setup_provider; mod withdraw_fees; pub use { - generate::generate, - get_request::get_request, - inspect::inspect, - register_provider::register_provider, - request_randomness::request_randomness, - run::run, - setup_provider::setup_provider, - withdraw_fees::withdraw_fees, + generate::generate, get_request::get_request, inspect::inspect, + register_provider::register_provider, request_randomness::request_randomness, run::run, + setup_provider::setup_provider, withdraw_fees::withdraw_fees, }; diff --git a/apps/fortuna/src/command/generate.rs b/apps/fortuna/src/command/generate.rs index dd5c4c106f..a1216d0b14 100644 --- a/apps/fortuna/src/command/generate.rs +++ b/apps/fortuna/src/command/generate.rs @@ -2,16 +2,10 @@ use { crate::{ api::GetRandomValueResponse, chain::ethereum::SignablePythContract, - config::{ - Config, - GenerateOptions, - }, + config::{Config, GenerateOptions}, }, anyhow::Result, - base64::{ - engine::general_purpose::STANDARD as base64_standard_engine, - Engine as _, - }, + base64::{engine::general_purpose::STANDARD as base64_standard_engine, Engine as _}, std::sync::Arc, }; diff --git a/apps/fortuna/src/command/get_request.rs b/apps/fortuna/src/command/get_request.rs index 1bd25f5df8..02741b42ff 100644 --- a/apps/fortuna/src/command/get_request.rs +++ b/apps/fortuna/src/command/get_request.rs @@ -1,10 +1,7 @@ use { crate::{ chain::ethereum::PythContract, - config::{ - Config, - GetRequestOptions, - }, + config::{Config, GetRequestOptions}, }, anyhow::Result, std::sync::Arc, diff --git a/apps/fortuna/src/command/inspect.rs b/apps/fortuna/src/command/inspect.rs index fc4caa04e0..454c143186 100644 --- a/apps/fortuna/src/command/inspect.rs +++ b/apps/fortuna/src/command/inspect.rs @@ -1,23 +1,13 @@ use { crate::{ - chain::ethereum::{ - PythContract, - Request, - }, - config::{ - Config, - EthereumConfig, - InspectOptions, - }, + chain::ethereum::{PythContract, Request}, + config::{Config, EthereumConfig, InspectOptions}, }, anyhow::Result, ethers::{ contract::Multicall, middleware::Middleware, - prelude::{ - Http, - Provider, - }, + prelude::{Http, Provider}, }, }; diff --git a/apps/fortuna/src/command/register_provider.rs b/apps/fortuna/src/command/register_provider.rs index b094c4dbcf..1833003434 100644 --- a/apps/fortuna/src/command/register_provider.rs +++ b/apps/fortuna/src/command/register_provider.rs @@ -1,28 +1,14 @@ use { crate::{ - api::{ - get_register_uri, - ChainId, - }, + api::{get_register_uri, ChainId}, chain::ethereum::SignablePythContract, - config::{ - Config, - EthereumConfig, - ProviderConfig, - RegisterProviderOptions, - }, + config::{Config, EthereumConfig, ProviderConfig, RegisterProviderOptions}, state::PebbleHashChain, }, - anyhow::{ - anyhow, - Result, - }, + anyhow::{anyhow, Result}, ethers::{ abi::Bytes, - signers::{ - LocalWallet, - Signer, - }, + signers::{LocalWallet, Signer}, types::U256, }, std::sync::Arc, @@ -30,7 +16,7 @@ use { #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct CommitmentMetadata { - pub seed: [u8; 32], + pub seed: [u8; 32], pub chain_length: u64, } @@ -83,7 +69,7 @@ pub async fn register_provider_from_config( // Store the random seed and chain length in the metadata field so that we can regenerate the hash // chain at-will. (This is secure because you can't generate the chain unless you also have the secret) let commitment_metadata = CommitmentMetadata { - seed: random, + seed: random, chain_length: commitment_length, }; let uri = get_register_uri(&provider_config.uri, chain_id)?; diff --git a/apps/fortuna/src/command/request_randomness.rs b/apps/fortuna/src/command/request_randomness.rs index 6491284b60..e69bf4bdfc 100644 --- a/apps/fortuna/src/command/request_randomness.rs +++ b/apps/fortuna/src/command/request_randomness.rs @@ -1,10 +1,7 @@ use { crate::{ chain::ethereum::SignablePythContract, - config::{ - Config, - RequestRandomnessOptions, - }, + config::{Config, RequestRandomnessOptions}, }, anyhow::Result, std::sync::Arc, diff --git a/apps/fortuna/src/command/run.rs b/apps/fortuna/src/command/run.rs index 0bcedf9990..adaa2e6930 100644 --- a/apps/fortuna/src/command/run.rs +++ b/apps/fortuna/src/command/run.rs @@ -1,71 +1,36 @@ use { crate::{ - api::{ - self, - BlockchainState, - ChainId, - }, + api::{self, BlockchainState, ChainId}, chain::{ ethereum::InstrumentedPythContract, - traced_client::{ - RpcMetrics, - TracedClient, - }, + traced_client::{RpcMetrics, TracedClient}, }, command::register_provider::CommitmentMetadata, - config::{ - Commitment, - Config, - EthereumConfig, - RunOptions, - }, - keeper::{ - self, - KeeperMetrics, - }, - state::{ - HashChainState, - PebbleHashChain, - }, - }, - anyhow::{ - anyhow, - Error, - Result, + config::{Commitment, Config, EthereumConfig, RunOptions}, + keeper::{self, KeeperMetrics}, + state::{HashChainState, PebbleHashChain}, }, + anyhow::{anyhow, Error, Result}, axum::Router, ethers::{ middleware::Middleware, - types::{ - Address, - BlockNumber, - }, + types::{Address, BlockNumber}, }, futures::future::join_all, prometheus_client::{ encoding::EncodeLabelSet, - metrics::{ - family::Family, - gauge::Gauge, - }, + metrics::{family::Family, gauge::Gauge}, registry::Registry, }, std::{ collections::HashMap, net::SocketAddr, sync::Arc, - time::{ - Duration, - SystemTime, - UNIX_EPOCH, - }, + time::{Duration, SystemTime, UNIX_EPOCH}, }, tokio::{ spawn, - sync::{ - watch, - RwLock, - }, + sync::{watch, RwLock}, time, }, tower_http::cors::CorsLayer, @@ -130,7 +95,6 @@ pub async fn run_api( Ok(()) } - pub async fn run_keeper( chains: HashMap, config: Config, @@ -205,7 +169,6 @@ pub async fn run(opts: &RunOptions) -> Result<()> { return Err(anyhow!("No chains were successfully setup")); } - // Listen for Ctrl+C so we can set the exit flag and wait for a graceful shutdown. spawn(async move { tracing::info!("Registered shutdown signal handler..."); @@ -284,8 +247,8 @@ async fn setup_chain_state( } provider_commitments.push(Commitment { - seed: latest_metadata.seed, - chain_length: latest_metadata.chain_length, + seed: latest_metadata.seed, + chain_length: latest_metadata.chain_length, original_commitment_sequence_number: provider_info.original_commitment_sequence_number, }); @@ -337,13 +300,11 @@ async fn setup_chain_state( Ok(state) } - #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)] pub struct ChainLabel { pub chain_id: String, } - #[tracing::instrument(name = "block_timestamp_lag", skip_all, fields(chain_id = chain_id))] pub async fn check_block_timestamp_lag( chain_id: String, @@ -360,7 +321,6 @@ pub async fn check_block_timestamp_lag( } }; - const INF_LAG: i64 = 1000000; // value that definitely triggers an alert let lag = match provider.get_block(BlockNumber::Latest).await { Ok(block) => match block { diff --git a/apps/fortuna/src/command/setup_provider.rs b/apps/fortuna/src/command/setup_provider.rs index 87fe7db976..3c587b0d14 100644 --- a/apps/fortuna/src/command/setup_provider.rs +++ b/apps/fortuna/src/command/setup_provider.rs @@ -1,41 +1,16 @@ use { crate::{ - api::{ - get_register_uri, - ChainId, - }, - chain::ethereum::{ - ProviderInfo, - SignablePythContract, - }, - command::register_provider::{ - register_provider_from_config, - CommitmentMetadata, - }, - config::{ - Config, - EthereumConfig, - SetupProviderOptions, - }, - state::{ - HashChainState, - PebbleHashChain, - }, - }, - anyhow::{ - anyhow, - Result, + api::{get_register_uri, ChainId}, + chain::ethereum::{ProviderInfo, SignablePythContract}, + command::register_provider::{register_provider_from_config, CommitmentMetadata}, + config::{Config, EthereumConfig, SetupProviderOptions}, + state::{HashChainState, PebbleHashChain}, }, + anyhow::{anyhow, Result}, ethers::{ abi::Bytes as AbiBytes, - signers::{ - LocalWallet, - Signer, - }, - types::{ - Address, - Bytes, - }, + signers::{LocalWallet, Signer}, + types::{Address, Bytes}, }, futures::future::join_all, std::sync::Arc, @@ -79,7 +54,6 @@ pub async fn setup_provider(opts: &SetupProviderOptions) -> Result<()> { } } - /// Setup provider for a single chain. /// 1. Register if there was no previous registration. /// 2. Re-register if there are no more random numbers to request on the contract. @@ -148,13 +122,12 @@ async fn setup_chain_provider( provider_config.chain_sample_interval, )?; let chain_state = HashChainState { - offsets: vec![provider_info + offsets: vec![provider_info .original_commitment_sequence_number .try_into()?], hash_chains: vec![hash_chain], }; - if chain_state.reveal(provider_info.original_commitment_sequence_number)? != provider_info.original_commitment { @@ -173,7 +146,6 @@ async fn setup_chain_provider( tracing::info!("Registered"); } - let provider_info = contract.get_provider_info(provider_address).call().await?; sync_fee(&contract, &provider_info, chain_config.fee) @@ -257,7 +229,6 @@ async fn sync_fee_manager( Ok(()) } - async fn sync_max_num_hashes( contract: &Arc, provider_info: &ProviderInfo, diff --git a/apps/fortuna/src/command/withdraw_fees.rs b/apps/fortuna/src/command/withdraw_fees.rs index ed83e92811..8f701823a9 100644 --- a/apps/fortuna/src/command/withdraw_fees.rs +++ b/apps/fortuna/src/command/withdraw_fees.rs @@ -1,22 +1,12 @@ use { crate::{ chain::ethereum::SignablePythContract, - config::{ - Config, - WithdrawFeesOptions, - }, - }, - anyhow::{ - anyhow, - Result, - }, - ethers::{ - signers::Signer, - types::Address, + config::{Config, WithdrawFeesOptions}, }, + anyhow::{anyhow, Result}, + ethers::{signers::Signer, types::Address}, }; - pub async fn withdraw_fees(opts: &WithdrawFeesOptions) -> Result<()> { let config = Config::load(&opts.config.config)?; diff --git a/apps/fortuna/src/config.rs b/apps/fortuna/src/config.rs index b176bbd202..87707aee20 100644 --- a/apps/fortuna/src/config.rs +++ b/apps/fortuna/src/config.rs @@ -1,38 +1,17 @@ use { crate::{ api::ChainId, - chain::reader::{ - BlockNumber, - BlockStatus, - }, - }, - anyhow::{ - anyhow, - Result, - }, - clap::{ - crate_authors, - crate_description, - crate_name, - crate_version, - Args, - Parser, + chain::reader::{BlockNumber, BlockStatus}, }, + anyhow::{anyhow, Result}, + clap::{crate_authors, crate_description, crate_name, crate_version, Args, Parser}, ethers::types::Address, - std::{ - collections::HashMap, - fs, - }, + std::{collections::HashMap, fs}, }; pub use { - generate::GenerateOptions, - get_request::GetRequestOptions, - inspect::InspectOptions, - register_provider::RegisterProviderOptions, - request_randomness::RequestRandomnessOptions, - run::RunOptions, - setup_provider::SetupProviderOptions, - withdraw_fees::WithdrawFeesOptions, + generate::GenerateOptions, get_request::GetRequestOptions, inspect::InspectOptions, + register_provider::RegisterProviderOptions, request_randomness::RequestRandomnessOptions, + run::RunOptions, setup_provider::SetupProviderOptions, withdraw_fees::WithdrawFeesOptions, }; mod generate; @@ -93,9 +72,9 @@ pub struct ConfigOptions { #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct Config { - pub chains: HashMap, + pub chains: HashMap, pub provider: ProviderConfig, - pub keeper: KeeperConfig, + pub keeper: KeeperConfig, } impl Config { @@ -189,15 +168,14 @@ pub struct EthereumConfig { pub max_num_hashes: Option, } - /// A commitment that the provider used to generate random numbers at some point in the past. /// These historical commitments need to be stored in the configuration to support transition points where /// the commitment changes. In theory, this information is stored on the blockchain, but unfortunately it /// is hard to retrieve from there. #[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct Commitment { - pub seed: [u8; 32], - pub chain_length: u64, + pub seed: [u8; 32], + pub chain_length: u64, pub original_commitment_sequence_number: u64, } diff --git a/apps/fortuna/src/config/generate.rs b/apps/fortuna/src/config/generate.rs index a2272586b4..198b15eee4 100644 --- a/apps/fortuna/src/config/generate.rs +++ b/apps/fortuna/src/config/generate.rs @@ -1,8 +1,5 @@ use { - crate::{ - api::ChainId, - config::ConfigOptions, - }, + crate::{api::ChainId, config::ConfigOptions}, clap::Args, ethers::types::Address, reqwest::Url, diff --git a/apps/fortuna/src/config/get_request.rs b/apps/fortuna/src/config/get_request.rs index d4fb242f72..9377ba369f 100644 --- a/apps/fortuna/src/config/get_request.rs +++ b/apps/fortuna/src/config/get_request.rs @@ -1,8 +1,5 @@ use { - crate::{ - api::ChainId, - config::ConfigOptions, - }, + crate::{api::ChainId, config::ConfigOptions}, clap::Args, ethers::types::Address, }; diff --git a/apps/fortuna/src/config/inspect.rs b/apps/fortuna/src/config/inspect.rs index 4b06895be1..c38469da24 100644 --- a/apps/fortuna/src/config/inspect.rs +++ b/apps/fortuna/src/config/inspect.rs @@ -1,12 +1,8 @@ use { - crate::{ - api::ChainId, - config::ConfigOptions, - }, + crate::{api::ChainId, config::ConfigOptions}, clap::Args, }; - #[derive(Args, Clone, Debug)] #[command(next_help_heading = "Inspect Options")] #[group(id = "Inspect")] diff --git a/apps/fortuna/src/config/register_provider.rs b/apps/fortuna/src/config/register_provider.rs index 992298c242..2d26ff5985 100644 --- a/apps/fortuna/src/config/register_provider.rs +++ b/apps/fortuna/src/config/register_provider.rs @@ -1,8 +1,5 @@ use { - crate::{ - api::ChainId, - config::ConfigOptions, - }, + crate::{api::ChainId, config::ConfigOptions}, clap::Args, }; diff --git a/apps/fortuna/src/config/request_randomness.rs b/apps/fortuna/src/config/request_randomness.rs index dd5ae6aff2..2b5a54c5a2 100644 --- a/apps/fortuna/src/config/request_randomness.rs +++ b/apps/fortuna/src/config/request_randomness.rs @@ -1,8 +1,5 @@ use { - crate::{ - api::ChainId, - config::ConfigOptions, - }, + crate::{api::ChainId, config::ConfigOptions}, clap::Args, ethers::types::Address, }; diff --git a/apps/fortuna/src/config/run.rs b/apps/fortuna/src/config/run.rs index a26092c123..fc7de83ef1 100644 --- a/apps/fortuna/src/config/run.rs +++ b/apps/fortuna/src/config/run.rs @@ -1,8 +1,4 @@ -use { - crate::config::ConfigOptions, - clap::Args, - std::net::SocketAddr, -}; +use {crate::config::ConfigOptions, clap::Args, std::net::SocketAddr}; /// Run the webservice #[derive(Args, Clone, Debug)] diff --git a/apps/fortuna/src/config/setup_provider.rs b/apps/fortuna/src/config/setup_provider.rs index 2d1955cfd7..70683da21b 100644 --- a/apps/fortuna/src/config/setup_provider.rs +++ b/apps/fortuna/src/config/setup_provider.rs @@ -1,7 +1,4 @@ -use { - crate::config::ConfigOptions, - clap::Args, -}; +use {crate::config::ConfigOptions, clap::Args}; #[derive(Args, Clone, Debug)] #[command(next_help_heading = "Setup Provider Options")] diff --git a/apps/fortuna/src/config/withdraw_fees.rs b/apps/fortuna/src/config/withdraw_fees.rs index 129adba9c9..1d708c9e82 100644 --- a/apps/fortuna/src/config/withdraw_fees.rs +++ b/apps/fortuna/src/config/withdraw_fees.rs @@ -1,8 +1,5 @@ use { - crate::{ - api::ChainId, - config::ConfigOptions, - }, + crate::{api::ChainId, config::ConfigOptions}, clap::Args, }; diff --git a/apps/fortuna/src/keeper.rs b/apps/fortuna/src/keeper.rs index 4d50cab127..cd1583bebd 100644 --- a/apps/fortuna/src/keeper.rs +++ b/apps/fortuna/src/keeper.rs @@ -1,78 +1,39 @@ use { crate::{ - api::{ - self, - BlockchainState, - ChainId, - }, + api::{self, BlockchainState, ChainId}, chain::{ eth_gas_oracle::eip1559_default_estimator, ethereum::{ - InstrumentedPythContract, - InstrumentedSignablePythContract, - PythContractCall, - }, - reader::{ - BlockNumber, - RequestedWithCallbackEvent, - }, - traced_client::{ - RpcMetrics, - TracedClient, + InstrumentedPythContract, InstrumentedSignablePythContract, PythContractCall, }, + reader::{BlockNumber, RequestedWithCallbackEvent}, + traced_client::{RpcMetrics, TracedClient}, }, config::EthereumConfig, }, - anyhow::{ - anyhow, - Result, - }, + anyhow::{anyhow, Result}, backoff::ExponentialBackoff, ethers::{ - providers::{ - Middleware, - Provider, - Ws, - }, + providers::{Middleware, Provider, Ws}, signers::Signer, - types::{ - Address, - U256, - }, + types::{Address, U256}, }, futures::StreamExt, prometheus_client::{ encoding::EncodeLabelSet, - metrics::{ - counter::Counter, - family::Family, - gauge::Gauge, - }, + metrics::{counter::Counter, family::Family, gauge::Gauge}, registry::Registry, }, std::{ collections::HashSet, - sync::{ - atomic::AtomicU64, - Arc, - }, + sync::{atomic::AtomicU64, Arc}, }, tokio::{ spawn, - sync::{ - mpsc, - RwLock, - }, - time::{ - self, - timeout, - Duration, - }, - }, - tracing::{ - self, - Instrument, + sync::{mpsc, RwLock}, + time::{self, timeout, Duration}, }, + tracing::{self, Instrument}, }; /// How much to wait before retrying in case of an RPC error @@ -99,21 +60,21 @@ const RETRY_PREVIOUS_BLOCKS: u64 = 100; #[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)] pub struct AccountLabel { pub chain_id: String, - pub address: String, + pub address: String, } #[derive(Default)] pub struct KeeperMetrics { pub current_sequence_number: Family, - pub end_sequence_number: Family, - pub balance: Family>, - pub collected_fee: Family>, - pub current_fee: Family>, - pub total_gas_spent: Family>, - pub requests: Family, - pub requests_processed: Family, - pub requests_reprocessed: Family, - pub reveals: Family, + pub end_sequence_number: Family, + pub balance: Family>, + pub collected_fee: Family>, + pub current_fee: Family>, + pub total_gas_spent: Family>, + pub requests: Family, + pub requests_processed: Family, + pub requests_reprocessed: Family, + pub reveals: Family, } impl KeeperMetrics { @@ -188,7 +149,7 @@ impl KeeperMetrics { #[derive(Debug)] pub struct BlockRange { pub from: BlockNumber, - pub to: BlockNumber, + pub to: BlockNumber, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -258,7 +219,7 @@ pub async fn run_keeper_threads( process_backlog( BlockRange { from: latest_safe_block.saturating_sub(BACKLOG_RANGE), - to: latest_safe_block, + to: latest_safe_block, }, contract.clone(), gas_limit, @@ -322,7 +283,6 @@ pub async fn run_keeper_threads( spawn(update_commitments_loop(contract.clone(), chain_state.clone()).in_current_span()); - // Spawn a thread to track the provider info and the balance of the keeper spawn( async move { @@ -372,7 +332,6 @@ pub async fn run_keeper_threads( ); } - /// Process an event with backoff. It will retry the reveal on failure for 5 minutes. #[tracing::instrument(name = "process_event_with_backoff", skip_all, fields( sequence_number = event.sequence_number @@ -388,7 +347,7 @@ pub async fn process_event_with_backoff( .requests .get_or_create(&AccountLabel { chain_id: chain_state.id.clone(), - address: chain_state.provider_address.to_string(), + address: chain_state.provider_address.to_string(), }) .inc(); tracing::info!("Started processing event"); @@ -416,12 +375,11 @@ pub async fn process_event_with_backoff( .requests_processed .get_or_create(&AccountLabel { chain_id: chain_state.id.clone(), - address: chain_state.provider_address.to_string(), + address: chain_state.provider_address.to_string(), }) .inc(); } - const TX_CONFIRMATION_TIMEOUT_SECS: u64 = 30; /// Process a callback on a chain. It estimates the gas for the reveal with callback and @@ -463,7 +421,6 @@ pub async fn process_event( backoff::Error::transient(anyhow!("Error estimating gas for reveal: {:?}", e)) })?; - if gas_estimate > gas_limit { return Err(backoff::Error::permanent(anyhow!( "Gas estimate for reveal with callback is higher than the gas limit {} > {}", @@ -557,7 +514,7 @@ pub async fn process_event( .total_gas_spent .get_or_create(&AccountLabel { chain_id: chain_config.id.clone(), - address: client + address: client .inner() .inner() .inner() @@ -572,14 +529,13 @@ pub async fn process_event( .reveals .get_or_create(&AccountLabel { chain_id: chain_config.id.clone(), - address: chain_config.provider_address.to_string(), + address: chain_config.provider_address.to_string(), }) .inc(); Ok(()) } - /// Process a range of blocks in batches. It calls the `process_single_block_batch` method for each batch. #[tracing::instrument(skip_all, fields( range_from_block = block_range.from, range_to_block = block_range.to @@ -607,7 +563,7 @@ pub async fn process_block_range( process_single_block_batch( BlockRange { from: current_block, - to: to_block, + to: to_block, }, contract.clone(), gas_limit, @@ -849,7 +805,6 @@ pub async fn process_backlog( tracing::info!("Backlog processed"); } - /// tracks the balance of the given address on the given chain /// if there was an error, the function will just return #[tracing::instrument(skip_all)] @@ -876,7 +831,7 @@ pub async fn track_balance( .balance .get_or_create(&AccountLabel { chain_id: chain_id.clone(), - address: address.to_string(), + address: address.to_string(), }) .set(balance); } @@ -910,7 +865,7 @@ pub async fn track_provider( .collected_fee .get_or_create(&AccountLabel { chain_id: chain_id.clone(), - address: provider_address.to_string(), + address: provider_address.to_string(), }) .set(collected_fee); @@ -918,7 +873,7 @@ pub async fn track_provider( .current_fee .get_or_create(&AccountLabel { chain_id: chain_id.clone(), - address: provider_address.to_string(), + address: provider_address.to_string(), }) .set(current_fee); @@ -926,7 +881,7 @@ pub async fn track_provider( .current_sequence_number .get_or_create(&AccountLabel { chain_id: chain_id.clone(), - address: provider_address.to_string(), + address: provider_address.to_string(), }) // sequence_number type on chain is u64 but practically it will take // a long time for it to cross the limits of i64. @@ -936,7 +891,7 @@ pub async fn track_provider( .end_sequence_number .get_or_create(&AccountLabel { chain_id: chain_id.clone(), - address: provider_address.to_string(), + address: provider_address.to_string(), }) .set(end_sequence_number as i64); } @@ -1082,7 +1037,6 @@ pub async fn update_commitments_loop( } } - pub async fn update_commitments_if_necessary( contract: Arc, chain_state: &BlockchainState, @@ -1231,7 +1185,6 @@ pub async fn adjust_fee_if_necessary( } }; - Ok(()) } diff --git a/apps/fortuna/src/main.rs b/apps/fortuna/src/main.rs index 7af8ea6aed..c09ef12e9c 100644 --- a/apps/fortuna/src/main.rs +++ b/apps/fortuna/src/main.rs @@ -1,10 +1,6 @@ #![allow(clippy::just_underscores_and_digits)] -use { - anyhow::Result, - clap::Parser, - std::io::IsTerminal, -}; +use {anyhow::Result, clap::Parser, std::io::IsTerminal}; pub mod api; pub mod chain; diff --git a/apps/fortuna/src/state.rs b/apps/fortuna/src/state.rs index d4b5041fd2..011a2199be 100644 --- a/apps/fortuna/src/state.rs +++ b/apps/fortuna/src/state.rs @@ -1,14 +1,8 @@ use { crate::api::ChainId, - anyhow::{ - ensure, - Result, - }, + anyhow::{ensure, Result}, ethers::types::Address, - sha3::{ - Digest, - Keccak256, - }, + sha3::{Digest, Keccak256}, }; /// A hash chain of a specific length. The hash chain has the property that @@ -18,9 +12,9 @@ use { /// to keep the chain around. #[derive(Clone)] pub struct PebbleHashChain { - hash: Vec<[u8; 32]>, + hash: Vec<[u8; 32]>, sample_interval: usize, - length: usize, + length: usize, } impl PebbleHashChain { @@ -48,7 +42,6 @@ impl PebbleHashChain { } } - pub fn from_config( secret: &str, chain_id: &ChainId, @@ -100,14 +93,14 @@ impl PebbleHashChain { /// which requires tracking multiple hash chains here. pub struct HashChainState { // The sequence number where the hash chain starts. Must be stored in sorted order. - pub offsets: Vec, + pub offsets: Vec, pub hash_chains: Vec, } impl HashChainState { pub fn from_chain_at_offset(offset: usize, chain: PebbleHashChain) -> HashChainState { HashChainState { - offsets: vec![offset], + offsets: vec![offset], hash_chains: vec![chain], } } @@ -129,10 +122,7 @@ impl HashChainState { mod test { use { crate::state::PebbleHashChain, - sha3::{ - Digest, - Keccak256, - }, + sha3::{Digest, Keccak256}, }; fn run_hash_chain_test(secret: [u8; 32], length: usize, sample_interval: usize) { diff --git a/apps/hermes/server/src/api.rs b/apps/hermes/server/src/api.rs index 36bd5a8877..0484576d49 100644 --- a/apps/hermes/server/src/api.rs +++ b/apps/hermes/server/src/api.rs @@ -1,20 +1,10 @@ use { crate::{ config::RunOptions, - state::{ - Aggregates, - Benchmarks, - Cache, - Metrics, - }, + state::{Aggregates, Benchmarks, Cache, Metrics}, }, anyhow::Result, - axum::{ - extract::Extension, - middleware::from_fn_with_state, - routing::get, - Router, - }, + axum::{extract::Extension, middleware::from_fn_with_state, routing::get, Router}, ipnet::IpNet, serde_qs::axum::QsQueryConfig, std::sync::Arc, @@ -30,8 +20,8 @@ pub mod types; mod ws; pub struct ApiState { - pub state: Arc, - pub ws: Arc, + pub state: Arc, + pub ws: Arc, pub metrics: Arc, } @@ -40,8 +30,8 @@ pub struct ApiState { impl Clone for ApiState { fn clone(&self) -> Self { Self { - state: self.state.clone(), - ws: self.ws.clone(), + state: self.state.clone(), + ws: self.ws.clone(), metrics: self.metrics.clone(), } } diff --git a/apps/hermes/server/src/api/metrics_middleware.rs b/apps/hermes/server/src/api/metrics_middleware.rs index be97f25a52..aef971e48c 100644 --- a/apps/hermes/server/src/api/metrics_middleware.rs +++ b/apps/hermes/server/src/api/metrics_middleware.rs @@ -2,28 +2,21 @@ use { super::ApiState, crate::state::metrics::Metrics, axum::{ - extract::{ - MatchedPath, - State, - }, + extract::{MatchedPath, State}, http::Request, middleware::Next, response::IntoResponse, }, prometheus_client::{ encoding::EncodeLabelSet, - metrics::{ - counter::Counter, - family::Family, - histogram::Histogram, - }, + metrics::{counter::Counter, family::Family, histogram::Histogram}, }, std::sync::Arc, tokio::time::Instant, }; pub struct ApiMetrics { - pub requests: Family, + pub requests: Family, pub latencies: Family, } @@ -34,7 +27,7 @@ impl ApiMetrics { S: Send + Sync + 'static, { let new = Self { - requests: Family::default(), + requests: Family::default(), latencies: Family::new_with_constructor(|| { Histogram::new( [ @@ -75,7 +68,7 @@ impl ApiMetrics { #[derive(Clone, Debug, PartialEq, Eq, Hash, EncodeLabelSet)] pub struct Labels { pub method: String, - pub path: String, + pub path: String, pub status: u16, } diff --git a/apps/hermes/server/src/api/rest.rs b/apps/hermes/server/src/api/rest.rs index e0cf7c8946..d4a381a25c 100644 --- a/apps/hermes/server/src/api/rest.rs +++ b/apps/hermes/server/src/api/rest.rs @@ -3,10 +3,7 @@ use { crate::state::aggregate::Aggregates, axum::{ http::StatusCode, - response::{ - IntoResponse, - Response, - }, + response::{IntoResponse, Response}, }, pyth_sdk::PriceIdentifier, }; @@ -22,7 +19,6 @@ mod price_feed_ids; mod ready; mod v2; - pub use { get_price_feed::*, get_vaa::*, @@ -34,10 +30,7 @@ pub use { price_feed_ids::*, ready::*, v2::{ - latest_price_updates::*, - latest_publisher_stake_caps::*, - price_feeds_metadata::*, - sse::*, + latest_price_updates::*, latest_publisher_stake_caps::*, price_feeds_metadata::*, sse::*, timestamp_price_updates::*, }, }; @@ -131,12 +124,8 @@ mod tests { super::*, crate::state::{ aggregate::{ - AggregationEvent, - PriceFeedsWithUpdateData, - PublisherStakeCapsWithUpdateData, - ReadinessMetadata, - RequestTime, - Update, + AggregationEvent, PriceFeedsWithUpdateData, PublisherStakeCapsWithUpdateData, + ReadinessMetadata, RequestTime, Update, }, benchmarks::BenchmarksState, cache::CacheState, @@ -144,10 +133,7 @@ mod tests { price_feeds_metadata::PriceFeedMetaState, }, anyhow::Result, - std::{ - collections::HashSet, - sync::Arc, - }, + std::{collections::HashSet, sync::Arc}, tokio::sync::broadcast::Receiver, }; diff --git a/apps/hermes/server/src/api/rest/get_price_feed.rs b/apps/hermes/server/src/api/rest/get_price_feed.rs index 1ec96eb353..765b56d755 100644 --- a/apps/hermes/server/src/api/rest/get_price_feed.rs +++ b/apps/hermes/server/src/api/rest/get_price_feed.rs @@ -4,23 +4,13 @@ use { api::{ doc_examples, rest::RestError, - types::{ - PriceIdInput, - RpcPriceFeed, - }, + types::{PriceIdInput, RpcPriceFeed}, ApiState, }, - state::aggregate::{ - Aggregates, - RequestTime, - UnixTimestamp, - }, + state::aggregate::{Aggregates, RequestTime, UnixTimestamp}, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, + axum::{extract::State, Json}, pyth_sdk::PriceIdentifier, serde_qs::axum::QsQuery, utoipa::IntoParams, diff --git a/apps/hermes/server/src/api/rest/get_vaa.rs b/apps/hermes/server/src/api/rest/get_vaa.rs index 13060f7d68..d9e3ddfcb9 100644 --- a/apps/hermes/server/src/api/rest/get_vaa.rs +++ b/apps/hermes/server/src/api/rest/get_vaa.rs @@ -1,33 +1,15 @@ use { super::validate_price_ids, crate::{ - api::{ - doc_examples, - rest::RestError, - types::PriceIdInput, - ApiState, - }, - state::aggregate::{ - Aggregates, - RequestTime, - UnixTimestamp, - }, + api::{doc_examples, rest::RestError, types::PriceIdInput, ApiState}, + state::aggregate::{Aggregates, RequestTime, UnixTimestamp}, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, - base64::{ - engine::general_purpose::STANDARD as base64_standard_engine, - Engine as _, - }, + axum::{extract::State, Json}, + base64::{engine::general_purpose::STANDARD as base64_standard_engine, Engine as _}, pyth_sdk::PriceIdentifier, serde_qs::axum::QsQuery, - utoipa::{ - IntoParams, - ToSchema, - }, + utoipa::{IntoParams, ToSchema}, }; #[derive(Debug, serde::Deserialize, IntoParams)] diff --git a/apps/hermes/server/src/api/rest/get_vaa_ccip.rs b/apps/hermes/server/src/api/rest/get_vaa_ccip.rs index 80854e6fb3..d4af86d67d 100644 --- a/apps/hermes/server/src/api/rest/get_vaa_ccip.rs +++ b/apps/hermes/server/src/api/rest/get_vaa_ccip.rs @@ -1,35 +1,16 @@ use { super::validate_price_ids, crate::{ - api::{ - rest::RestError, - ApiState, - }, - state::aggregate::{ - Aggregates, - RequestTime, - UnixTimestamp, - }, + api::{rest::RestError, ApiState}, + state::aggregate::{Aggregates, RequestTime, UnixTimestamp}, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, - derive_more::{ - Deref, - DerefMut, - }, + axum::{extract::State, Json}, + derive_more::{Deref, DerefMut}, pyth_sdk::PriceIdentifier, - serde::{ - Deserialize, - Serialize, - }, + serde::{Deserialize, Serialize}, serde_qs::axum::QsQuery, - utoipa::{ - IntoParams, - ToSchema, - }, + utoipa::{IntoParams, ToSchema}, }; #[derive(Clone, Debug, Deref, DerefMut, Deserialize, Serialize, ToSchema)] diff --git a/apps/hermes/server/src/api/rest/index.rs b/apps/hermes/server/src/api/rest/index.rs index 99611f821e..fee938984b 100644 --- a/apps/hermes/server/src/api/rest/index.rs +++ b/apps/hermes/server/src/api/rest/index.rs @@ -1,7 +1,4 @@ -use axum::{ - response::IntoResponse, - Json, -}; +use axum::{response::IntoResponse, Json}; /// This is the index page for the REST service. It lists all the available endpoints. /// diff --git a/apps/hermes/server/src/api/rest/latest_price_feeds.rs b/apps/hermes/server/src/api/rest/latest_price_feeds.rs index 86d7e25369..22d606dfcd 100644 --- a/apps/hermes/server/src/api/rest/latest_price_feeds.rs +++ b/apps/hermes/server/src/api/rest/latest_price_feeds.rs @@ -3,22 +3,13 @@ use { crate::{ api::{ rest::RestError, - types::{ - PriceIdInput, - RpcPriceFeed, - }, + types::{PriceIdInput, RpcPriceFeed}, ApiState, }, - state::aggregate::{ - Aggregates, - RequestTime, - }, + state::aggregate::{Aggregates, RequestTime}, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, + axum::{extract::State, Json}, pyth_sdk::PriceIdentifier, serde_qs::axum::QsQuery, utoipa::IntoParams, diff --git a/apps/hermes/server/src/api/rest/latest_vaas.rs b/apps/hermes/server/src/api/rest/latest_vaas.rs index 5b9ee0dcc1..5b387bb322 100644 --- a/apps/hermes/server/src/api/rest/latest_vaas.rs +++ b/apps/hermes/server/src/api/rest/latest_vaas.rs @@ -1,26 +1,12 @@ use { super::validate_price_ids, crate::{ - api::{ - doc_examples, - rest::RestError, - types::PriceIdInput, - ApiState, - }, - state::aggregate::{ - Aggregates, - RequestTime, - }, + api::{doc_examples, rest::RestError, types::PriceIdInput, ApiState}, + state::aggregate::{Aggregates, RequestTime}, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, - base64::{ - engine::general_purpose::STANDARD as base64_standard_engine, - Engine as _, - }, + axum::{extract::State, Json}, + base64::{engine::general_purpose::STANDARD as base64_standard_engine, Engine as _}, pyth_sdk::PriceIdentifier, serde_qs::axum::QsQuery, utoipa::IntoParams, @@ -42,7 +28,6 @@ pub struct LatestVaasQueryParams { ids: Vec, } - /// **Deprecated: use /v2/updates/price/latest instead** /// /// Get VAAs for a set of price feed ids. diff --git a/apps/hermes/server/src/api/rest/live.rs b/apps/hermes/server/src/api/rest/live.rs index 66343cc6f1..2a6f8bff4d 100644 --- a/apps/hermes/server/src/api/rest/live.rs +++ b/apps/hermes/server/src/api/rest/live.rs @@ -1,9 +1,6 @@ use axum::{ http::StatusCode, - response::{ - IntoResponse, - Response, - }, + response::{IntoResponse, Response}, }; pub async fn live() -> Response { diff --git a/apps/hermes/server/src/api/rest/price_feed_ids.rs b/apps/hermes/server/src/api/rest/price_feed_ids.rs index 5f767f7837..92f9e53b5b 100644 --- a/apps/hermes/server/src/api/rest/price_feed_ids.rs +++ b/apps/hermes/server/src/api/rest/price_feed_ids.rs @@ -1,17 +1,10 @@ use { crate::{ - api::{ - rest::RestError, - types::RpcPriceIdentifier, - ApiState, - }, + api::{rest::RestError, types::RpcPriceIdentifier, ApiState}, state::aggregate::Aggregates, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, + axum::{extract::State, Json}, }; /// **Deprecated: use /v2/price_feeds instead** diff --git a/apps/hermes/server/src/api/rest/ready.rs b/apps/hermes/server/src/api/rest/ready.rs index 87a57d8dc8..92b538440d 100644 --- a/apps/hermes/server/src/api/rest/ready.rs +++ b/apps/hermes/server/src/api/rest/ready.rs @@ -1,15 +1,9 @@ use { - crate::{ - api::ApiState, - state::aggregate::Aggregates, - }, + crate::{api::ApiState, state::aggregate::Aggregates}, axum::{ extract::State, http::StatusCode, - response::{ - IntoResponse, - Response, - }, + response::{IntoResponse, Response}, Json, }, }; diff --git a/apps/hermes/server/src/api/rest/v2/latest_price_updates.rs b/apps/hermes/server/src/api/rest/v2/latest_price_updates.rs index 2d22731965..a249063966 100644 --- a/apps/hermes/server/src/api/rest/v2/latest_price_updates.rs +++ b/apps/hermes/server/src/api/rest/v2/latest_price_updates.rs @@ -1,40 +1,21 @@ use { crate::{ api::{ - rest::{ - validate_price_ids, - RestError, - }, - types::{ - BinaryUpdate, - EncodingType, - ParsedPriceUpdate, - PriceIdInput, - PriceUpdate, - }, + rest::{validate_price_ids, RestError}, + types::{BinaryUpdate, EncodingType, ParsedPriceUpdate, PriceIdInput, PriceUpdate}, ApiState, }, - state::aggregate::{ - Aggregates, - RequestTime, - }, + state::aggregate::{Aggregates, RequestTime}, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, - base64::{ - engine::general_purpose::STANDARD as base64_standard_engine, - Engine as _, - }, + axum::{extract::State, Json}, + base64::{engine::general_purpose::STANDARD as base64_standard_engine, Engine as _}, pyth_sdk::PriceIdentifier, serde::Deserialize, serde_qs::axum::QsQuery, utoipa::IntoParams, }; - #[derive(Debug, Deserialize, IntoParams)] #[into_params(parameter_in=Query)] pub struct LatestPriceUpdatesQueryParams { @@ -116,7 +97,7 @@ where .collect(); let binary_price_update = BinaryUpdate { encoding: params.encoding, - data: encoded_data, + data: encoded_data, }; let parsed_price_updates: Option> = if params.parsed { Some( @@ -135,6 +116,5 @@ where parsed: parsed_price_updates, }; - Ok(Json(compressed_price_update)) } diff --git a/apps/hermes/server/src/api/rest/v2/latest_publisher_stake_caps.rs b/apps/hermes/server/src/api/rest/v2/latest_publisher_stake_caps.rs index a429efd042..7db8729572 100644 --- a/apps/hermes/server/src/api/rest/v2/latest_publisher_stake_caps.rs +++ b/apps/hermes/server/src/api/rest/v2/latest_publisher_stake_caps.rs @@ -3,9 +3,7 @@ use { api::{ rest::RestError, types::{ - BinaryUpdate, - EncodingType, - LatestPublisherStakeCapsUpdateDataResponse, + BinaryUpdate, EncodingType, LatestPublisherStakeCapsUpdateDataResponse, ParsedPublisherStakeCapsUpdate, }, ApiState, @@ -13,20 +11,13 @@ use { state::Aggregates, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, - base64::{ - engine::general_purpose::STANDARD as base64_standard_engine, - Engine as _, - }, + axum::{extract::State, Json}, + base64::{engine::general_purpose::STANDARD as base64_standard_engine, Engine as _}, serde::Deserialize, serde_qs::axum::QsQuery, utoipa::IntoParams, }; - #[derive(Debug, Deserialize, IntoParams)] #[into_params(parameter_in=Query)] pub struct LatestPublisherStakeCapsUpdateData { @@ -86,7 +77,7 @@ where let binary = BinaryUpdate { encoding: params.encoding, - data: encoded_data, + data: encoded_data, }; let parsed: Option> = if params.parsed { diff --git a/apps/hermes/server/src/api/rest/v2/price_feeds_metadata.rs b/apps/hermes/server/src/api/rest/v2/price_feeds_metadata.rs index 82036298e9..db20d77367 100644 --- a/apps/hermes/server/src/api/rest/v2/price_feeds_metadata.rs +++ b/apps/hermes/server/src/api/rest/v2/price_feeds_metadata.rs @@ -2,25 +2,18 @@ use { crate::{ api::{ rest::RestError, - types::{ - AssetType, - PriceFeedMetadata, - }, + types::{AssetType, PriceFeedMetadata}, ApiState, }, state::price_feeds_metadata::PriceFeedMeta, }, anyhow::Result, - axum::{ - extract::State, - Json, - }, + axum::{extract::State, Json}, serde::Deserialize, serde_qs::axum::QsQuery, utoipa::IntoParams, }; - #[derive(Debug, Deserialize, IntoParams)] #[into_params(parameter_in=Query)] pub struct PriceFeedsMetadataQueryParams { diff --git a/apps/hermes/server/src/api/rest/v2/sse.rs b/apps/hermes/server/src/api/rest/v2/sse.rs index 8e741855fd..70c5766870 100644 --- a/apps/hermes/server/src/api/rest/v2/sse.rs +++ b/apps/hermes/server/src/api/rest/v2/sse.rs @@ -1,34 +1,19 @@ use { crate::{ api::{ - rest::{ - validate_price_ids, - RestError, - }, + rest::{validate_price_ids, RestError}, types::{ - BinaryUpdate, - EncodingType, - ParsedPriceUpdate, - PriceIdInput, - PriceUpdate, + BinaryUpdate, EncodingType, ParsedPriceUpdate, PriceIdInput, PriceUpdate, RpcPriceIdentifier, }, ApiState, }, - state::aggregate::{ - Aggregates, - AggregationEvent, - RequestTime, - }, + state::aggregate::{Aggregates, AggregationEvent, RequestTime}, }, anyhow::Result, axum::{ extract::State, - response::sse::{ - Event, - KeepAlive, - Sse, - }, + response::sse::{Event, KeepAlive, Sse}, }, futures::Stream, pyth_sdk::PriceIdentifier, @@ -36,10 +21,7 @@ use { serde_qs::axum::QsQuery, std::convert::Infallible, tokio::sync::broadcast, - tokio_stream::{ - wrappers::BroadcastStream, - StreamExt as _, - }, + tokio_stream::{wrappers::BroadcastStream, StreamExt as _}, utoipa::IntoParams, }; diff --git a/apps/hermes/server/src/api/rest/v2/timestamp_price_updates.rs b/apps/hermes/server/src/api/rest/v2/timestamp_price_updates.rs index 4e75c0999b..924b80f7ca 100644 --- a/apps/hermes/server/src/api/rest/v2/timestamp_price_updates.rs +++ b/apps/hermes/server/src/api/rest/v2/timestamp_price_updates.rs @@ -2,31 +2,15 @@ use { crate::{ api::{ doc_examples, - rest::{ - validate_price_ids, - RestError, - }, - types::{ - BinaryUpdate, - EncodingType, - ParsedPriceUpdate, - PriceIdInput, - PriceUpdate, - }, + rest::{validate_price_ids, RestError}, + types::{BinaryUpdate, EncodingType, ParsedPriceUpdate, PriceIdInput, PriceUpdate}, ApiState, }, - state::aggregate::{ - Aggregates, - RequestTime, - UnixTimestamp, - }, + state::aggregate::{Aggregates, RequestTime, UnixTimestamp}, }, anyhow::Result, axum::{ - extract::{ - Path, - State, - }, + extract::{Path, State}, Json, }, pyth_sdk::PriceIdentifier, @@ -73,7 +57,6 @@ pub struct TimestampPriceUpdatesQueryParams { ignore_invalid_price_ids: bool, } - fn default_true() -> bool { true } @@ -133,7 +116,7 @@ where .collect(); let binary_price_update = BinaryUpdate { encoding: query_params.encoding, - data: encoded_data, + data: encoded_data, }; let parsed_price_updates: Option> = if query_params.parsed { Some( @@ -152,6 +135,5 @@ where parsed: parsed_price_updates, }; - Ok(Json(compressed_price_update)) } diff --git a/apps/hermes/server/src/api/types.rs b/apps/hermes/server/src/api/types.rs index 865a12744d..c9af0e631a 100644 --- a/apps/hermes/server/src/api/types.rs +++ b/apps/hermes/server/src/api/types.rs @@ -1,46 +1,20 @@ use { super::doc_examples, - crate::state::aggregate::{ - PriceFeedUpdate, - PriceFeedsWithUpdateData, - Slot, - UnixTimestamp, - }, + crate::state::aggregate::{PriceFeedUpdate, PriceFeedsWithUpdateData, Slot, UnixTimestamp}, anyhow::Result, - base64::{ - engine::general_purpose::STANDARD as base64_standard_engine, - Engine as _, - }, - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - derive_more::{ - Deref, - DerefMut, - }, - pyth_sdk::{ - Price, - PriceFeed, - PriceIdentifier, - }, - serde::{ - Deserialize, - Serialize, - }, + base64::{engine::general_purpose::STANDARD as base64_standard_engine, Engine as _}, + borsh::{BorshDeserialize, BorshSerialize}, + derive_more::{Deref, DerefMut}, + pyth_sdk::{Price, PriceFeed, PriceIdentifier}, + serde::{Deserialize, Serialize}, std::{ collections::BTreeMap, - fmt::{ - Display, - Formatter, - Result as FmtResult, - }, + fmt::{Display, Formatter, Result as FmtResult}, }, utoipa::ToSchema, wormhole_sdk::Chain, }; - /// A price id is a 32-byte hex string, optionally prefixed with "0x". /// Price ids are case insensitive. /// @@ -64,36 +38,36 @@ type Base64String = String; #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct RpcPriceFeedMetadata { #[schema(value_type = Option, example=85480034)] - pub slot: Option, + pub slot: Option, #[schema(example = 26)] - pub emitter_chain: u16, + pub emitter_chain: u16, #[schema(value_type = Option, example=doc_examples::timestamp_example)] pub price_service_receive_time: Option, #[schema(value_type = Option, example=doc_examples::timestamp_example)] - pub prev_publish_time: Option, + pub prev_publish_time: Option, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct RpcPriceFeedMetadataV2 { #[schema(value_type = Option, example=85480034)] - pub slot: Option, + pub slot: Option, #[schema(value_type = Option, example=doc_examples::timestamp_example)] pub proof_available_time: Option, #[schema(value_type = Option, example=doc_examples::timestamp_example)] - pub prev_publish_time: Option, + pub prev_publish_time: Option, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct RpcPriceFeed { - pub id: RpcPriceIdentifier, - pub price: RpcPrice, + pub id: RpcPriceIdentifier, + pub price: RpcPrice, pub ema_price: RpcPrice, #[serde(skip_serializing_if = "Option::is_none")] - pub metadata: Option, + pub metadata: Option, /// The VAA binary represented as a base64 string. #[serde(skip_serializing_if = "Option::is_none")] #[schema(value_type = Option, example=doc_examples::vaa_example)] - pub vaa: Option, + pub vaa: Option, } impl RpcPriceFeed { @@ -107,26 +81,26 @@ impl RpcPriceFeed { let price_feed = price_feed_update.price_feed; Self { - id: RpcPriceIdentifier::new(price_feed.id.to_bytes()), - price: RpcPrice { - price: price_feed.get_price_unchecked().price, - conf: price_feed.get_price_unchecked().conf, - expo: price_feed.get_price_unchecked().expo, + id: RpcPriceIdentifier::new(price_feed.id.to_bytes()), + price: RpcPrice { + price: price_feed.get_price_unchecked().price, + conf: price_feed.get_price_unchecked().conf, + expo: price_feed.get_price_unchecked().expo, publish_time: price_feed.get_price_unchecked().publish_time, }, ema_price: RpcPrice { - price: price_feed.get_ema_price_unchecked().price, - conf: price_feed.get_ema_price_unchecked().conf, - expo: price_feed.get_ema_price_unchecked().expo, + price: price_feed.get_ema_price_unchecked().price, + conf: price_feed.get_ema_price_unchecked().conf, + expo: price_feed.get_ema_price_unchecked().expo, publish_time: price_feed.get_ema_price_unchecked().publish_time, }, - metadata: verbose.then_some(RpcPriceFeedMetadata { - emitter_chain: Chain::Pythnet.into(), + metadata: verbose.then_some(RpcPriceFeedMetadata { + emitter_chain: Chain::Pythnet.into(), price_service_receive_time: price_feed_update.received_at, - slot: price_feed_update.slot, - prev_publish_time: price_feed_update.prev_publish_time, + slot: price_feed_update.slot, + prev_publish_time: price_feed_update.prev_publish_time, }), - vaa: match binary { + vaa: match binary { false => None, true => price_feed_update .update_data @@ -159,22 +133,21 @@ pub struct RpcPrice { /// The price itself, stored as a string to avoid precision loss #[serde(with = "pyth_sdk::utils::as_string")] #[schema(value_type = String, example="2920679499999")] - pub price: i64, + pub price: i64, /// The confidence interval associated with the price, stored as a string to avoid precision loss #[serde(with = "pyth_sdk::utils::as_string")] #[schema(value_type = String, example="509500001")] - pub conf: u64, + pub conf: u64, /// The exponent associated with both the price and confidence interval. Multiply those values /// by `10^expo` to get the real value. #[schema(example=-8)] - pub expo: i32, + pub expo: i32, /// When the price was published. The `publish_time` is a unix timestamp, i.e., the number of /// seconds since the Unix epoch (00:00:00 UTC on 1 Jan 1970). #[schema(value_type = i64, example=doc_examples::timestamp_example)] pub publish_time: UnixTimestamp, } - #[derive( Copy, Clone, @@ -234,15 +207,15 @@ impl EncodingType { #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct BinaryUpdate { pub encoding: EncodingType, - pub data: Vec, + pub data: Vec, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct ParsedPriceUpdate { - pub id: RpcPriceIdentifier, - pub price: RpcPrice, + pub id: RpcPriceIdentifier, + pub price: RpcPrice, pub ema_price: RpcPrice, - pub metadata: RpcPriceFeedMetadataV2, + pub metadata: RpcPriceFeedMetadataV2, } impl From for ParsedPriceUpdate { @@ -250,23 +223,23 @@ impl From for ParsedPriceUpdate { let price_feed = price_feed_update.price_feed; Self { - id: RpcPriceIdentifier::from(price_feed.id), - price: RpcPrice { - price: price_feed.get_price_unchecked().price, - conf: price_feed.get_price_unchecked().conf, - expo: price_feed.get_price_unchecked().expo, + id: RpcPriceIdentifier::from(price_feed.id), + price: RpcPrice { + price: price_feed.get_price_unchecked().price, + conf: price_feed.get_price_unchecked().conf, + expo: price_feed.get_price_unchecked().expo, publish_time: price_feed.get_price_unchecked().publish_time, }, ema_price: RpcPrice { - price: price_feed.get_ema_price_unchecked().price, - conf: price_feed.get_ema_price_unchecked().conf, - expo: price_feed.get_ema_price_unchecked().expo, + price: price_feed.get_ema_price_unchecked().price, + conf: price_feed.get_ema_price_unchecked().conf, + expo: price_feed.get_ema_price_unchecked().expo, publish_time: price_feed.get_ema_price_unchecked().publish_time, }, - metadata: RpcPriceFeedMetadataV2 { + metadata: RpcPriceFeedMetadataV2 { proof_available_time: price_feed_update.received_at, - slot: price_feed_update.slot, - prev_publish_time: price_feed_update.prev_publish_time, + slot: price_feed_update.slot, + prev_publish_time: price_feed_update.prev_publish_time, }, } } @@ -280,7 +253,7 @@ pub struct ParsedPublisherStakeCapsUpdate { #[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize, Clone, ToSchema)] pub struct ParsedPublisherStakeCap { pub publisher: String, - pub cap: u64, + pub cap: u64, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] @@ -305,24 +278,24 @@ impl TryFrom for PriceFeedsWithUpdateData { .into_iter() .map(|parsed_price_update| { Ok(PriceFeedUpdate { - price_feed: PriceFeed::new( + price_feed: PriceFeed::new( parsed_price_update.id.into(), Price { - price: parsed_price_update.price.price, - conf: parsed_price_update.price.conf, - expo: parsed_price_update.price.expo, + price: parsed_price_update.price.price, + conf: parsed_price_update.price.conf, + expo: parsed_price_update.price.expo, publish_time: parsed_price_update.price.publish_time, }, Price { - price: parsed_price_update.ema_price.price, - conf: parsed_price_update.ema_price.conf, - expo: parsed_price_update.ema_price.expo, + price: parsed_price_update.ema_price.price, + conf: parsed_price_update.ema_price.conf, + expo: parsed_price_update.ema_price.expo, publish_time: parsed_price_update.ema_price.publish_time, }, ), - slot: parsed_price_update.metadata.slot, - received_at: parsed_price_update.metadata.proof_available_time, - update_data: None, // This field is not available in ParsedPriceUpdate + slot: parsed_price_update.metadata.slot, + received_at: parsed_price_update.metadata.proof_available_time, + update_data: None, // This field is not available in ParsedPriceUpdate prev_publish_time: parsed_price_update.metadata.prev_publish_time, }) }) @@ -346,7 +319,7 @@ impl TryFrom for PriceFeedsWithUpdateData { #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] pub struct PriceFeedMetadata { - pub id: RpcPriceIdentifier, + pub id: RpcPriceIdentifier, // BTreeMap is used to automatically sort the keys to ensure consistent ordering of attributes in the JSON response. // This enhances user experience by providing a predictable structure, avoiding confusion from varying orders in different responses. pub attributes: BTreeMap, diff --git a/apps/hermes/server/src/api/ws.rs b/apps/hermes/server/src/api/ws.rs index 125be8ac1f..92b6ea5998 100644 --- a/apps/hermes/server/src/api/ws.rs +++ b/apps/hermes/server/src/api/ws.rs @@ -1,85 +1,46 @@ use { super::{ - types::{ - PriceIdInput, - RpcPriceFeed, - }, + types::{PriceIdInput, RpcPriceFeed}, ApiState, }, crate::state::{ - aggregate::{ - Aggregates, - AggregationEvent, - RequestTime, - }, + aggregate::{Aggregates, AggregationEvent, RequestTime}, metrics::Metrics, - Benchmarks, - Cache, - PriceFeedMeta, - }, - anyhow::{ - anyhow, - Result, + Benchmarks, Cache, PriceFeedMeta, }, + anyhow::{anyhow, Result}, axum::{ extract::{ - ws::{ - Message, - WebSocket, - WebSocketUpgrade, - }, + ws::{Message, WebSocket, WebSocketUpgrade}, State as AxumState, }, http::HeaderMap, response::IntoResponse, }, futures::{ - stream::{ - SplitSink, - SplitStream, - }, - SinkExt, - StreamExt, - }, - governor::{ - DefaultKeyedRateLimiter, - Quota, - RateLimiter, + stream::{SplitSink, SplitStream}, + SinkExt, StreamExt, }, + governor::{DefaultKeyedRateLimiter, Quota, RateLimiter}, ipnet::IpNet, nonzero_ext::nonzero, prometheus_client::{ - encoding::{ - EncodeLabelSet, - EncodeLabelValue, - }, - metrics::{ - counter::Counter, - family::Family, - }, + encoding::{EncodeLabelSet, EncodeLabelValue}, + metrics::{counter::Counter, family::Family}, }, pyth_sdk::PriceIdentifier, - serde::{ - Deserialize, - Serialize, - }, + serde::{Deserialize, Serialize}, std::{ collections::HashMap, net::IpAddr, num::NonZeroU32, sync::{ - atomic::{ - AtomicUsize, - Ordering, - }, + atomic::{AtomicUsize, Ordering}, Arc, }, time::Duration, }, - tokio::sync::{ - broadcast::Receiver, - watch, - }, + tokio::sync::{broadcast::Receiver, watch}, }; const PING_INTERVAL_DURATION: Duration = Duration::from_secs(30); @@ -91,8 +52,8 @@ const BYTES_LIMIT_PER_IP_PER_SECOND: u32 = 256 * 1024; // 256 KiB #[derive(Clone)] pub struct PriceFeedClientConfig { - verbose: bool, - binary: bool, + verbose: bool, + binary: bool, allow_out_of_order: bool, } @@ -115,7 +76,7 @@ pub enum Status { #[derive(Clone, Debug, PartialEq, Eq, Hash, EncodeLabelSet)] pub struct Labels { pub interaction: Interaction, - pub status: Status, + pub status: Status, } pub struct WsMetrics { @@ -153,11 +114,11 @@ impl WsMetrics { } pub struct WsState { - pub subscriber_counter: AtomicUsize, - pub bytes_limit_whitelist: Vec, - pub rate_limiter: DefaultKeyedRateLimiter, + pub subscriber_counter: AtomicUsize, + pub bytes_limit_whitelist: Vec, + pub rate_limiter: DefaultKeyedRateLimiter, pub requester_ip_header_name: String, - pub metrics: WsMetrics, + pub metrics: WsMetrics, } impl WsState { @@ -178,17 +139,16 @@ impl WsState { } } - #[derive(Deserialize, Debug, Clone)] #[serde(tag = "type")] enum ClientMessage { #[serde(rename = "subscribe")] Subscribe { - ids: Vec, + ids: Vec, #[serde(default)] - verbose: bool, + verbose: bool, #[serde(default)] - binary: bool, + binary: bool, #[serde(default)] allow_out_of_order: bool, }, @@ -196,7 +156,6 @@ enum ClientMessage { Unsubscribe { ids: Vec }, } - #[derive(Serialize, Debug, Clone)] #[serde(tag = "type")] enum ServerMessage { @@ -257,7 +216,7 @@ where .interactions .get_or_create(&Labels { interaction: Interaction::NewConnection, - status: Status::Success, + status: Status::Success, }) .inc(); @@ -281,18 +240,18 @@ pub type SubscriberId = usize; /// Subscriber is an actor that handles a single websocket connection. /// It listens to the store for updates and sends them to the client. pub struct Subscriber { - id: SubscriberId, - ip_addr: Option, - closed: bool, - state: Arc, - ws_state: Arc, - notify_receiver: Receiver, - receiver: SplitStream, - sender: SplitSink, + id: SubscriberId, + ip_addr: Option, + closed: bool, + state: Arc, + ws_state: Arc, + notify_receiver: Receiver, + receiver: SplitStream, + sender: SplitSink, price_feeds_with_config: HashMap, - ping_interval: tokio::time::Interval, - exit: watch::Receiver, - responded_to_ping: bool, + ping_interval: tokio::time::Interval, + exit: watch::Receiver, + responded_to_ping: bool, } impl Subscriber @@ -460,11 +419,10 @@ where .interactions .get_or_create(&Labels { interaction: Interaction::RateLimit, - status: Status::Error, + status: Status::Error, }) .inc(); - self.sender .send( serde_json::to_string(&ServerResponseMessage::Err { @@ -488,7 +446,7 @@ where .interactions .get_or_create(&Labels { interaction: Interaction::PriceUpdate, - status: Status::Success, + status: Status::Success, }) .inc(); } @@ -511,7 +469,7 @@ where .interactions .get_or_create(&Labels { interaction: Interaction::CloseConnection, - status: Status::Success, + status: Status::Success, }) .inc(); @@ -535,7 +493,7 @@ where .interactions .get_or_create(&Labels { interaction: Interaction::ClientHeartbeat, - status: Status::Success, + status: Status::Success, }) .inc(); @@ -551,7 +509,7 @@ where .interactions .get_or_create(&Labels { interaction: Interaction::ClientMessage, - status: Status::Error, + status: Status::Error, }) .inc(); self.sender @@ -624,7 +582,7 @@ where .interactions .get_or_create(&Labels { interaction: Interaction::ClientMessage, - status: Status::Success, + status: Status::Success, }) .inc(); diff --git a/apps/hermes/server/src/config.rs b/apps/hermes/server/src/config.rs index cde7f78d62..275cb26b60 100644 --- a/apps/hermes/server/src/config.rs +++ b/apps/hermes/server/src/config.rs @@ -1,11 +1,4 @@ -use clap::{ - crate_authors, - crate_description, - crate_name, - crate_version, - Args, - Parser, -}; +use clap::{crate_authors, crate_description, crate_name, crate_version, Args, Parser}; mod aggregate; mod benchmarks; diff --git a/apps/hermes/server/src/config/aggregate.rs b/apps/hermes/server/src/config/aggregate.rs index 6ba2a64f72..c015f48afa 100644 --- a/apps/hermes/server/src/config/aggregate.rs +++ b/apps/hermes/server/src/config/aggregate.rs @@ -1,7 +1,4 @@ -use { - clap::Args, - humantime::Duration, -}; +use {clap::Args, humantime::Duration}; #[derive(Args, Clone, Debug)] #[command(next_help_heading = "Aggregate Options")] diff --git a/apps/hermes/server/src/config/benchmarks.rs b/apps/hermes/server/src/config/benchmarks.rs index 75f7115c85..566765b5dd 100644 --- a/apps/hermes/server/src/config/benchmarks.rs +++ b/apps/hermes/server/src/config/benchmarks.rs @@ -1,7 +1,4 @@ -use { - clap::Args, - reqwest::Url, -}; +use {clap::Args, reqwest::Url}; #[derive(Args, Clone, Debug)] #[command(next_help_heading = "Benchmark Options")] diff --git a/apps/hermes/server/src/config/metrics.rs b/apps/hermes/server/src/config/metrics.rs index c963a065e3..4aff7dc7ed 100644 --- a/apps/hermes/server/src/config/metrics.rs +++ b/apps/hermes/server/src/config/metrics.rs @@ -1,7 +1,4 @@ -use { - clap::Args, - std::net::SocketAddr, -}; +use {clap::Args, std::net::SocketAddr}; const DEFAULT_METRICS_SERVER_LISTEN_ADDR: &str = "127.0.0.1:33888"; diff --git a/apps/hermes/server/src/config/pythnet.rs b/apps/hermes/server/src/config/pythnet.rs index e110352964..8362ca618f 100644 --- a/apps/hermes/server/src/config/pythnet.rs +++ b/apps/hermes/server/src/config/pythnet.rs @@ -1,7 +1,4 @@ -use { - clap::Args, - solana_sdk::pubkey::Pubkey, -}; +use {clap::Args, solana_sdk::pubkey::Pubkey}; const DEFAULT_PYTHNET_MAPPING_ADDR: &str = "AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J"; diff --git a/apps/hermes/server/src/config/rpc.rs b/apps/hermes/server/src/config/rpc.rs index 79e53993e5..88ae6fdbeb 100644 --- a/apps/hermes/server/src/config/rpc.rs +++ b/apps/hermes/server/src/config/rpc.rs @@ -1,8 +1,4 @@ -use { - clap::Args, - ipnet::IpNet, - std::net::SocketAddr, -}; +use {clap::Args, ipnet::IpNet, std::net::SocketAddr}; const DEFAULT_RPC_LISTEN_ADDR: &str = "127.0.0.1:33999"; const DEFAULT_RPC_REQUESTER_IP_HEADER_NAME: &str = "X-Forwarded-For"; diff --git a/apps/hermes/server/src/config/wormhole.rs b/apps/hermes/server/src/config/wormhole.rs index 0b16fa6033..a6410fd741 100644 --- a/apps/hermes/server/src/config/wormhole.rs +++ b/apps/hermes/server/src/config/wormhole.rs @@ -1,7 +1,4 @@ -use { - clap::Args, - solana_sdk::pubkey::Pubkey, -}; +use {clap::Args, solana_sdk::pubkey::Pubkey}; const DEFAULT_CONTRACT_ADDR: &str = "H3fxXJ86ADW2PNuDDmZJg6mzTtPxkYCpNuQUTgmJ7AjU"; diff --git a/apps/hermes/server/src/main.rs b/apps/hermes/server/src/main.rs index 7e419556a4..8dd7913977 100644 --- a/apps/hermes/server/src/main.rs +++ b/apps/hermes/server/src/main.rs @@ -1,16 +1,10 @@ use { anyhow::Result, - clap::{ - CommandFactory, - Parser, - }, + clap::{CommandFactory, Parser}, futures::future::join_all, lazy_static::lazy_static, std::io::IsTerminal, - tokio::{ - spawn, - sync::watch, - }, + tokio::{spawn, sync::watch}, }; mod api; diff --git a/apps/hermes/server/src/metrics_server.rs b/apps/hermes/server/src/metrics_server.rs index 93b7d3ff93..390a3d061d 100644 --- a/apps/hermes/server/src/metrics_server.rs +++ b/apps/hermes/server/src/metrics_server.rs @@ -3,22 +3,12 @@ //! This server serves metrics over /metrics in OpenMetrics format. use { - crate::{ - config::RunOptions, - state::metrics::Metrics, - }, + crate::{config::RunOptions, state::metrics::Metrics}, anyhow::Result, - axum::{ - extract::State, - http::header, - response::IntoResponse, - routing::get, - Router, - }, + axum::{extract::State, http::header, response::IntoResponse, routing::get, Router}, std::sync::Arc, }; - #[tracing::instrument(skip(opts, state))] pub async fn run(opts: RunOptions, state: Arc) -> Result<()> where diff --git a/apps/hermes/server/src/network/pythnet.rs b/apps/hermes/server/src/network/pythnet.rs index 07d8cfae22..03f6746a65 100644 --- a/apps/hermes/server/src/network/pythnet.rs +++ b/apps/hermes/server/src/network/pythnet.rs @@ -4,68 +4,30 @@ use { crate::{ - api::types::{ - PriceFeedMetadata, - RpcPriceIdentifier, - }, + api::types::{PriceFeedMetadata, RpcPriceIdentifier}, config::RunOptions, - network::wormhole::{ - BridgeData, - GuardianSet, - GuardianSetData, - }, + network::wormhole::{BridgeData, GuardianSet, GuardianSetData}, state::{ - aggregate::{ - AccumulatorMessages, - Aggregates, - Update, - }, - price_feeds_metadata::{ - PriceFeedMeta, - DEFAULT_PRICE_FEEDS_CACHE_UPDATE_INTERVAL, - }, + aggregate::{AccumulatorMessages, Aggregates, Update}, + price_feeds_metadata::{PriceFeedMeta, DEFAULT_PRICE_FEEDS_CACHE_UPDATE_INTERVAL}, wormhole::Wormhole, }, }, - anyhow::{ - anyhow, - Result, - }, + anyhow::{anyhow, Result}, borsh::BorshDeserialize, futures::stream::StreamExt, pyth_sdk::PriceIdentifier, - pyth_sdk_solana::state::{ - load_mapping_account, - load_product_account, - }, + pyth_sdk_solana::state::{load_mapping_account, load_product_account}, solana_account_decoder::UiAccountEncoding, solana_client::{ - nonblocking::{ - pubsub_client::PubsubClient, - rpc_client::RpcClient, - }, - rpc_config::{ - RpcAccountInfoConfig, - RpcProgramAccountsConfig, - }, - rpc_filter::{ - Memcmp, - MemcmpEncodedBytes, - RpcFilterType, - }, + nonblocking::{pubsub_client::PubsubClient, rpc_client::RpcClient}, + rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig}, + rpc_filter::{Memcmp, MemcmpEncodedBytes, RpcFilterType}, }, solana_sdk::{ - account::Account, - bs58, - commitment_config::CommitmentConfig, - pubkey::Pubkey, - system_program, - }, - std::{ - collections::BTreeMap, - sync::Arc, - time::Duration, + account::Account, bs58, commitment_config::CommitmentConfig, pubkey::Pubkey, system_program, }, + std::{collections::BTreeMap, sync::Arc, time::Duration}, tokio::time::Instant, }; @@ -155,11 +117,11 @@ where encoding: Some(UiAccountEncoding::Base64Zstd), ..Default::default() }, - filters: Some(vec![RpcFilterType::Memcmp(Memcmp::new( + filters: Some(vec![RpcFilterType::Memcmp(Memcmp::new( 0, // offset MemcmpEncodedBytes::Bytes(b"PAS1".to_vec()), // bytes ))]), - with_context: Some(true), + with_context: Some(true), }; // Listen for all PythNet accounts, we will filter down to the Accumulator related accounts. @@ -333,7 +295,6 @@ where }) }; - let task_price_feeds_metadata_updater = { let price_feeds_state = state.clone(); let mut exit = crate::EXIT.subscribe(); @@ -378,7 +339,6 @@ where Ok(()) } - pub async fn fetch_and_store_price_feeds_metadata( state: &S, mapping_address: &Pubkey, @@ -396,7 +356,6 @@ where .filter(|metadata| all_ids.contains(&PriceIdentifier::from(metadata.id))) .collect(); - state.store_price_feeds_metadata(&filtered_metadata).await?; Ok(filtered_metadata) } diff --git a/apps/hermes/server/src/network/wormhole.rs b/apps/hermes/server/src/network/wormhole.rs index 8903d75294..ef53f5eccc 100644 --- a/apps/hermes/server/src/network/wormhole.rs +++ b/apps/hermes/server/src/network/wormhole.rs @@ -5,27 +5,15 @@ //! application. use { - crate::{ - config::RunOptions, - state::wormhole::Wormhole, - }, - anyhow::{ - anyhow, - Result, - }, + crate::{config::RunOptions, state::wormhole::Wormhole}, + anyhow::{anyhow, Result}, futures::StreamExt, proto::spy::v1::{ - filter_entry::Filter, - spy_rpc_service_client::SpyRpcServiceClient, - EmitterFilter, - FilterEntry, - SubscribeSignedVaaRequest, + filter_entry::Filter, spy_rpc_service_client::SpyRpcServiceClient, EmitterFilter, + FilterEntry, SubscribeSignedVaaRequest, }, pythnet_sdk::ACCUMULATOR_EMITTER_ADDRESS, - std::{ - sync::Arc, - time::Duration, - }, + std::{sync::Arc, time::Duration}, tokio::time::Instant, tonic::Request, wormhole_sdk::Chain, @@ -58,8 +46,8 @@ impl std::fmt::Display for GuardianSet { #[allow(dead_code)] pub struct BridgeData { pub guardian_set_index: u32, - pub last_lamports: u64, - pub config: BridgeConfig, + pub last_lamports: u64, + pub config: BridgeConfig, } /// BridgeConfig extracted from wormhole bridge account, due to no API. @@ -67,15 +55,15 @@ pub struct BridgeData { #[allow(dead_code)] pub struct BridgeConfig { pub guardian_set_expiration_time: u32, - pub fee: u64, + pub fee: u64, } /// GuardianSetData extracted from wormhole bridge account, due to no API. #[derive(borsh::BorshDeserialize)] pub struct GuardianSetData { - pub _index: u32, - pub keys: Vec<[u8; 20]>, - pub _creation_time: u32, + pub _index: u32, + pub keys: Vec<[u8; 20]>, + pub _creation_time: u32, pub _expiration_time: u32, } @@ -150,7 +138,7 @@ where .subscribe_signed_vaa(Request::new(SubscribeSignedVaaRequest { filters: vec![FilterEntry { filter: Some(Filter::EmitterFilter(EmitterFilter { - chain_id: Into::::into(Chain::Pythnet).into(), + chain_id: Into::::into(Chain::Pythnet).into(), emitter_address: hex::encode(ACCUMULATOR_EMITTER_ADDRESS), })), }], diff --git a/apps/hermes/server/src/serde.rs b/apps/hermes/server/src/serde.rs index 2efc327f10..d119d1d251 100644 --- a/apps/hermes/server/src/serde.rs +++ b/apps/hermes/server/src/serde.rs @@ -1,12 +1,7 @@ pub mod hex { use { hex::FromHex, - serde::{ - de::IntoDeserializer, - Deserialize, - Deserializer, - Serializer, - }, + serde::{de::IntoDeserializer, Deserialize, Deserializer, Serializer}, }; pub fn serialize(b: &[u8; N], s: S) -> Result diff --git a/apps/hermes/server/src/state.rs b/apps/hermes/server/src/state.rs index d481e9a3d0..d03fec6ca4 100644 --- a/apps/hermes/server/src/state.rs +++ b/apps/hermes/server/src/state.rs @@ -2,10 +2,7 @@ use { self::{ - aggregate::{ - AggregateState, - AggregationEvent, - }, + aggregate::{AggregateState, AggregationEvent}, benchmarks::BenchmarksState, cache::CacheState, metrics::MetricsState, @@ -15,10 +12,7 @@ use { aggregate::Slot, prometheus_client::registry::Registry, reqwest::Url, - std::{ - sync::Arc, - time::Duration, - }, + std::{sync::Arc, time::Duration}, tokio::sync::broadcast::Sender, }; @@ -31,12 +25,8 @@ pub mod wormhole; // Expose State interfaces and types for other modules. pub use { - aggregate::Aggregates, - benchmarks::Benchmarks, - cache::Cache, - metrics::Metrics, - price_feeds_metadata::PriceFeedMeta, - wormhole::Wormhole, + aggregate::Aggregates, benchmarks::Benchmarks, cache::Cache, metrics::Metrics, + price_feeds_metadata::PriceFeedMeta, wormhole::Wormhole, }; /// State contains all relevant shared application state. @@ -73,33 +63,26 @@ pub fn new( ) -> Arc { let mut metrics_registry = Registry::default(); Arc::new(State { - cache: CacheState::new(cache_size), - benchmarks: BenchmarksState::new(benchmarks_endpoint), + cache: CacheState::new(cache_size), + benchmarks: BenchmarksState::new(benchmarks_endpoint), price_feed_meta: PriceFeedMetaState::new(), - aggregates: AggregateState::new( + aggregates: AggregateState::new( update_tx, readiness_staleness_threshold, readiness_max_allowed_slot_lag, &mut metrics_registry, ), - wormhole: WormholeState::new(), - metrics: MetricsState::new(metrics_registry), + wormhole: WormholeState::new(), + metrics: MetricsState::new(metrics_registry), }) } #[cfg(test)] pub mod test { use { - super::{ - aggregate::AggregationEvent, - Aggregates, - Wormhole, - }, + super::{aggregate::AggregationEvent, Aggregates, Wormhole}, crate::network::wormhole::GuardianSet, - std::{ - sync::Arc, - time::Duration, - }, + std::{sync::Arc, time::Duration}, tokio::sync::broadcast::Receiver, }; diff --git a/apps/hermes/server/src/state/aggregate.rs b/apps/hermes/server/src/state/aggregate.rs index cf4971e08e..0232628a73 100644 --- a/apps/hermes/server/src/state/aggregate.rs +++ b/apps/hermes/server/src/state/aggregate.rs @@ -1,75 +1,39 @@ #[cfg(test)] -use mock_instant::{ - SystemTime, - UNIX_EPOCH, -}; +use mock_instant::{SystemTime, UNIX_EPOCH}; #[cfg(not(test))] -use std::time::{ - SystemTime, - UNIX_EPOCH, -}; +use std::time::{SystemTime, UNIX_EPOCH}; use { self::wormhole_merkle::{ - construct_message_states_proofs, - construct_update_data, - store_wormhole_merkle_verified_message, - WormholeMerkleMessageProof, - WormholeMerkleState, + construct_message_states_proofs, construct_update_data, + store_wormhole_merkle_verified_message, WormholeMerkleMessageProof, WormholeMerkleState, }, crate::{ - api::types::{ - ParsedPublisherStakeCap, - ParsedPublisherStakeCapsUpdate, - }, + api::types::{ParsedPublisherStakeCap, ParsedPublisherStakeCapsUpdate}, network::wormhole::VaaBytes, state::{ benchmarks::Benchmarks, - cache::{ - Cache, - MessageState, - MessageStateFilter, - }, + cache::{Cache, MessageState, MessageStateFilter}, price_feeds_metadata::PriceFeedMeta, State, }, }, - anyhow::{ - anyhow, - Result, - }, + anyhow::{anyhow, Result}, borsh::BorshDeserialize, byteorder::BigEndian, prometheus_client::registry::Registry, - pyth_sdk::{ - Price, - PriceFeed, - PriceIdentifier, - }, + pyth_sdk::{Price, PriceFeed, PriceIdentifier}, pythnet_sdk::{ - messages::{ - Message, - MessageType, - PUBLISHER_STAKE_CAPS_MESSAGE_FEED_ID, - }, + messages::{Message, MessageType, PUBLISHER_STAKE_CAPS_MESSAGE_FEED_ID}, wire::{ from_slice, - v1::{ - WormholeMessage, - WormholePayload, - }, + v1::{WormholeMessage, WormholePayload}, }, }, serde::Serialize, solana_sdk::pubkey::Pubkey, - std::{ - collections::HashSet, - time::Duration, - }, + std::{collections::HashSet, time::Duration}, tokio::sync::{ - broadcast::{ - Receiver, - Sender, - }, + broadcast::{Receiver, Sender}, RwLock, }, wormhole_sdk::Vaa, @@ -157,7 +121,7 @@ impl AggregateStateData { } pub struct AggregateState { - pub data: RwLock, + pub data: RwLock, pub api_update_tx: Sender, } @@ -169,7 +133,7 @@ impl AggregateState { metrics_registry: &mut Registry, ) -> Self { Self { - data: RwLock::new(AggregateStateData::new( + data: RwLock::new(AggregateStateData::new( readiness_staleness_threshold, readiness_max_allowed_slot_lag, metrics_registry, @@ -187,9 +151,9 @@ impl AggregateState { /// uses little-endian byte order. #[derive(Clone, PartialEq, Debug, BorshDeserialize)] pub struct AccumulatorMessages { - pub magic: [u8; 4], - pub slot: u64, - pub ring_size: u32, + pub magic: [u8; 4], + pub slot: u64, + pub ring_size: u32, pub raw_messages: Vec, } @@ -207,10 +171,10 @@ pub enum Update { #[derive(Debug, PartialEq)] pub struct PriceFeedUpdate { - pub price_feed: PriceFeed, - pub slot: Option, - pub received_at: Option, - pub update_data: Option>, + pub price_feed: PriceFeed, + pub slot: Option, + pub received_at: Option, + pub update_data: Option>, pub prev_publish_time: Option, } @@ -223,18 +187,18 @@ pub struct PriceFeedsWithUpdateData { #[derive(Debug, PartialEq)] pub struct PublisherStakeCapsWithUpdateData { pub publisher_stake_caps: Vec, - pub update_data: Vec>, + pub update_data: Vec>, } #[derive(Debug, Serialize)] pub struct ReadinessMetadata { - pub has_completed_recently: bool, - pub is_not_behind: bool, - pub is_metadata_loaded: bool, - pub latest_completed_slot: Option, - pub latest_observed_slot: Option, + pub has_completed_recently: bool, + pub is_not_behind: bool, + pub is_metadata_loaded: bool, + pub latest_completed_slot: Option, + pub latest_observed_slot: Option, pub latest_completed_unix_timestamp: Option, - pub price_feeds_metadata_len: usize, + pub price_feeds_metadata_len: usize, } #[async_trait::async_trait] @@ -437,7 +401,7 @@ where .iter() .map(|cap| ParsedPublisherStakeCap { publisher: Pubkey::from(cap.publisher).to_string(), - cap: cap.cap, + cap: cap.cap, }) .collect(), }), @@ -567,24 +531,24 @@ where .iter() .map(|message_state| match message_state.message { Message::PriceFeedMessage(price_feed) => Ok(PriceFeedUpdate { - price_feed: PriceFeed::new( + price_feed: PriceFeed::new( PriceIdentifier::new(price_feed.feed_id), Price { - price: price_feed.price, - conf: price_feed.conf, - expo: price_feed.exponent, + price: price_feed.price, + conf: price_feed.conf, + expo: price_feed.exponent, publish_time: price_feed.publish_time, }, Price { - price: price_feed.ema_price, - conf: price_feed.ema_conf, - expo: price_feed.exponent, + price: price_feed.ema_price, + conf: price_feed.ema_conf, + expo: price_feed.exponent, publish_time: price_feed.publish_time, }, ), - received_at: Some(message_state.received_at), - slot: Some(message_state.slot), - update_data: Some( + received_at: Some(message_state.received_at), + slot: Some(message_state.slot), + update_data: Some( construct_update_data(vec![message_state.clone().into()])? .into_iter() .next() @@ -609,37 +573,24 @@ mod test { use { super::*, crate::{ - api::types::{ - PriceFeedMetadata, - RpcPriceIdentifier, - }, + api::types::{PriceFeedMetadata, RpcPriceIdentifier}, state::test::setup_state, }, futures::future::join_all, mock_instant::MockClock, pythnet_sdk::{ accumulators::{ - merkle::{ - MerkleRoot, - MerkleTree, - }, + merkle::{MerkleRoot, MerkleTree}, Accumulator, }, hashers::keccak256_160::Keccak160, messages::PriceFeedMessage, - wire::v1::{ - AccumulatorUpdateData, - Proof, - WormholeMerkleRoot, - }, + wire::v1::{AccumulatorUpdateData, Proof, WormholeMerkleRoot}, }, rand::seq::SliceRandom, serde_wormhole::RawMessage, std::sync::Arc, - wormhole_sdk::{ - Address, - Chain, - }, + wormhole_sdk::{Address, Chain}, }; /// Generate list of updates for the given list of messages at a given slot with given sequence @@ -761,24 +712,24 @@ mod test { assert_eq!( price_feeds_with_update_data.price_feeds, vec![PriceFeedUpdate { - price_feed: PriceFeed::new( + price_feed: PriceFeed::new( PriceIdentifier::new(price_feed_message.feed_id), Price { - price: price_feed_message.price, - conf: price_feed_message.conf, - expo: price_feed_message.exponent, + price: price_feed_message.price, + conf: price_feed_message.conf, + expo: price_feed_message.exponent, publish_time: price_feed_message.publish_time, }, Price { - price: price_feed_message.ema_price, - conf: price_feed_message.ema_conf, - expo: price_feed_message.exponent, + price: price_feed_message.ema_price, + conf: price_feed_message.ema_conf, + expo: price_feed_message.exponent, publish_time: price_feed_message.publish_time, } ), - slot: Some(10), - received_at: price_feeds_with_update_data.price_feeds[0].received_at, // Ignore checking this field. - update_data: price_feeds_with_update_data.price_feeds[0] + slot: Some(10), + received_at: price_feeds_with_update_data.price_feeds[0].received_at, // Ignore checking this field. + update_data: price_feeds_with_update_data.price_feeds[0] .update_data .clone(), // Ignore checking this field. prev_publish_time: Some(9), @@ -797,16 +748,16 @@ mod test { assert_eq!( vaa, Vaa { - nonce: 0, - version: 0, - sequence: 20, - timestamp: 0, - signatures: vec![], + nonce: 0, + version: 0, + sequence: 20, + timestamp: 0, + signatures: vec![], guardian_set_index: 0, - emitter_chain: Chain::Pythnet, - emitter_address: Address(pythnet_sdk::ACCUMULATOR_EMITTER_ADDRESS), - consistency_level: 0, - payload: vaa.payload, // Ignore checking this field. + emitter_chain: Chain::Pythnet, + emitter_address: Address(pythnet_sdk::ACCUMULATOR_EMITTER_ADDRESS), + consistency_level: 0, + payload: vaa.payload, // Ignore checking this field. } ); let merkle_root = WormholeMessage::try_from_bytes(vaa.payload.as_ref()).unwrap(); @@ -814,9 +765,9 @@ mod test { assert_eq!( merkle_root, WormholeMerkleRoot { - slot: 10, + slot: 10, ring_size: 100, - root: merkle_root.root, // Ignore checking this field. + root: merkle_root.root, // Ignore checking this field. } ); @@ -967,11 +918,10 @@ mod test { Some(unix_timestamp as i64) ); - // Add a dummy price feeds metadata state .store_price_feeds_metadata(&[PriceFeedMetadata { - id: RpcPriceIdentifier::new([100; 32]), + id: RpcPriceIdentifier::new([100; 32]), attributes: Default::default(), }]) .await diff --git a/apps/hermes/server/src/state/aggregate/metrics.rs b/apps/hermes/server/src/state/aggregate/metrics.rs index 9842bf9658..aba2464168 100644 --- a/apps/hermes/server/src/state/aggregate/metrics.rs +++ b/apps/hermes/server/src/state/aggregate/metrics.rs @@ -1,21 +1,11 @@ use { super::Slot, prometheus_client::{ - encoding::{ - EncodeLabelSet, - EncodeLabelValue, - }, - metrics::{ - counter::Counter, - family::Family, - histogram::Histogram, - }, + encoding::{EncodeLabelSet, EncodeLabelValue}, + metrics::{counter::Counter, family::Family, histogram::Histogram}, registry::Registry, }, - std::collections::{ - BTreeMap, - HashMap, - }, + std::collections::{BTreeMap, HashMap}, tokio::time::Instant, }; @@ -42,17 +32,17 @@ struct ObservedSlotLabels { #[derive(Clone, Debug)] pub struct Metrics { - observed_slot: Family, - observed_slot_latency: Family, + observed_slot: Family, + observed_slot_latency: Family, first_observed_time_of_slot: BTreeMap, - newest_observed_slot: HashMap, + newest_observed_slot: HashMap, } impl Metrics { pub fn new(metrics_registry: &mut Registry) -> Self { let new = Self { - observed_slot: Family::default(), - observed_slot_latency: Family::new_with_constructor(|| { + observed_slot: Family::default(), + observed_slot_latency: Family::new_with_constructor(|| { Histogram::new( [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.7, 1.0, 1.3, 1.7, 2.0, 3.0, 5.0, 10.0, 20.0, @@ -61,7 +51,7 @@ impl Metrics { ) }), first_observed_time_of_slot: BTreeMap::new(), - newest_observed_slot: HashMap::new(), + newest_observed_slot: HashMap::new(), }; { diff --git a/apps/hermes/server/src/state/aggregate/wormhole_merkle.rs b/apps/hermes/server/src/state/aggregate/wormhole_merkle.rs index 1cd69f9bc3..0c41df204b 100644 --- a/apps/hermes/server/src/state/aggregate/wormhole_merkle.rs +++ b/apps/hermes/server/src/state/aggregate/wormhole_merkle.rs @@ -1,37 +1,19 @@ use { - super::{ - AccumulatorMessages, - RawMessage, - Slot, - }, + super::{AccumulatorMessages, RawMessage, Slot}, crate::{ network::wormhole::VaaBytes, - state::cache::{ - Cache, - MessageState, - }, - }, - anyhow::{ - anyhow, - Result, + state::cache::{Cache, MessageState}, }, + anyhow::{anyhow, Result}, pythnet_sdk::{ accumulators::{ - merkle::{ - MerklePath, - MerkleTree, - }, + merkle::{MerklePath, MerkleTree}, Accumulator, }, hashers::keccak256_160::Keccak160, wire::{ to_vec, - v1::{ - AccumulatorUpdateData, - MerklePriceUpdate, - Proof, - WormholeMerkleRoot, - }, + v1::{AccumulatorUpdateData, MerklePriceUpdate, Proof, WormholeMerkleRoot}, }, }, }; @@ -43,28 +25,28 @@ pub const MAX_MESSAGE_IN_SINGLE_UPDATE_DATA: usize = 255; #[derive(Clone, PartialEq, Debug)] pub struct WormholeMerkleState { pub root: WormholeMerkleRoot, - pub vaa: VaaBytes, + pub vaa: VaaBytes, } #[derive(Clone, PartialEq, Debug)] pub struct WormholeMerkleMessageProof { pub proof: MerklePath, - pub vaa: VaaBytes, + pub vaa: VaaBytes, } #[derive(Clone, PartialEq, Debug)] pub struct RawMessageWithMerkleProof { - pub slot: Slot, + pub slot: Slot, pub raw_message: RawMessage, - pub proof: WormholeMerkleMessageProof, + pub proof: WormholeMerkleMessageProof, } impl From for RawMessageWithMerkleProof { fn from(message_state: MessageState) -> Self { Self { - slot: message_state.slot, + slot: message_state.slot, raw_message: message_state.raw_message, - proof: message_state.proof_set.wormhole_merkle_proof, + proof: message_state.proof_set.wormhole_merkle_proof, } } } @@ -104,7 +86,7 @@ pub fn construct_message_states_proofs( .iter() .map(|m| { Ok(WormholeMerkleMessageProof { - vaa: wormhole_merkle_state.vaa.clone(), + vaa: wormhole_merkle_state.vaa.clone(), proof: merkle_acc .prove(m.as_ref()) .ok_or(anyhow!("Failed to prove message"))?, @@ -126,14 +108,14 @@ pub fn construct_update_data(mut messages: Vec) -> Re let vaa = message.proof.vaa; let mut updates = vec![MerklePriceUpdate { message: message.raw_message.into(), - proof: message.proof.proof, + proof: message.proof.proof, }]; while updates.len() < MAX_MESSAGE_IN_SINGLE_UPDATE_DATA { if let Some(message) = iter.next_if(|m| m.slot == slot) { updates.push(MerklePriceUpdate { message: message.raw_message.into(), - proof: message.proof.proof, + proof: message.proof.proof, }); } else { break; @@ -162,10 +144,7 @@ mod test { use { super::*, pythnet_sdk::{ - messages::{ - Message, - PriceFeedMessage, - }, + messages::{Message, PriceFeedMessage}, wire::from_slice, }, }; @@ -174,19 +153,19 @@ mod test { slot_and_pubtime: u64, ) -> RawMessageWithMerkleProof { let price_feed_message = Message::PriceFeedMessage(PriceFeedMessage { - conf: 0, - price: 0, - feed_id: [0; 32], - exponent: 0, - ema_conf: 0, - ema_price: 0, - publish_time: slot_and_pubtime as i64, + conf: 0, + price: 0, + feed_id: [0; 32], + exponent: 0, + ema_conf: 0, + ema_price: 0, + publish_time: slot_and_pubtime as i64, prev_publish_time: 0, }); RawMessageWithMerkleProof { - slot: slot_and_pubtime, - proof: WormholeMerkleMessageProof { - vaa: vec![], + slot: slot_and_pubtime, + proof: WormholeMerkleMessageProof { + vaa: vec![], proof: MerklePath::default(), }, raw_message: to_vec::<_, byteorder::BE>(&price_feed_message).unwrap(), diff --git a/apps/hermes/server/src/state/benchmarks.rs b/apps/hermes/server/src/state/benchmarks.rs index 5be95c2d21..015668914d 100644 --- a/apps/hermes/server/src/state/benchmarks.rs +++ b/apps/hermes/server/src/state/benchmarks.rs @@ -2,18 +2,12 @@ use { super::{ - aggregate::{ - PriceFeedsWithUpdateData, - UnixTimestamp, - }, + aggregate::{PriceFeedsWithUpdateData, UnixTimestamp}, State, }, crate::api::types::PriceUpdate, anyhow::Result, - base64::{ - engine::general_purpose::STANDARD as base64_standard_engine, - Engine as _, - }, + base64::{engine::general_purpose::STANDARD as base64_standard_engine, Engine as _}, pyth_sdk::PriceIdentifier, reqwest::Url, serde::Deserialize, @@ -32,7 +26,7 @@ enum BlobEncoding { #[derive(Deserialize, Debug, Clone)] struct BinaryBlob { pub encoding: BlobEncoding, - pub data: Vec, + pub data: Vec, } impl TryFrom for Vec> { diff --git a/apps/hermes/server/src/state/cache.rs b/apps/hermes/server/src/state/cache.rs index 5befcbd5f9..2562b1932b 100644 --- a/apps/hermes/server/src/state/cache.rs +++ b/apps/hermes/server/src/state/cache.rs @@ -1,30 +1,14 @@ use { super::State, crate::state::aggregate::{ - wormhole_merkle::WormholeMerkleState, - AccumulatorMessages, - ProofSet, - RawMessage, - RequestTime, - Slot, - UnixTimestamp, - }, - anyhow::{ - anyhow, - Result, + wormhole_merkle::WormholeMerkleState, AccumulatorMessages, ProofSet, RawMessage, + RequestTime, Slot, UnixTimestamp, }, + anyhow::{anyhow, Result}, futures::future::join_all, - pythnet_sdk::messages::{ - FeedId, - Message, - MessageType, - }, + pythnet_sdk::messages::{FeedId, Message, MessageType}, std::{ - collections::{ - BTreeMap, - HashMap, - HashSet, - }, + collections::{BTreeMap, HashMap, HashSet}, sync::Arc, }, strum::IntoEnumIterator, @@ -34,26 +18,26 @@ use { #[derive(Clone, PartialEq, Eq, Debug, Hash)] pub struct MessageStateKey { pub feed_id: FeedId, - pub type_: MessageType, + pub type_: MessageType, } #[derive(Clone, PartialEq, Eq, Debug, PartialOrd, Ord)] pub struct MessageStateTime { pub publish_time: UnixTimestamp, - pub slot: Slot, + pub slot: Slot, } #[derive(Clone, PartialEq, Debug)] pub struct MessageState { - pub slot: Slot, - pub message: Message, + pub slot: Slot, + pub message: Message, /// The raw updated message. /// /// We need to store the raw message binary because the Message /// struct might lose some data due to its support for forward /// compatibility. pub raw_message: RawMessage, - pub proof_set: ProofSet, + pub proof_set: ProofSet, pub received_at: UnixTimestamp, } @@ -61,14 +45,14 @@ impl MessageState { pub fn time(&self) -> MessageStateTime { MessageStateTime { publish_time: self.message.publish_time(), - slot: self.slot, + slot: self.slot, } } pub fn key(&self) -> MessageStateKey { MessageStateKey { feed_id: self.message.feed_id(), - type_: self.message.clone().into(), + type_: self.message.clone().into(), } } @@ -107,19 +91,19 @@ type MessageCache = Arc Self { Self { - accumulator_messages_cache: Arc::new(RwLock::new(BTreeMap::new())), + accumulator_messages_cache: Arc::new(RwLock::new(BTreeMap::new())), wormhole_merkle_state_cache: Arc::new(RwLock::new(BTreeMap::new())), - message_cache: Arc::new(RwLock::new(HashMap::new())), - cache_size: size, + message_cache: Arc::new(RwLock::new(HashMap::new())), + cache_size: size, } } } @@ -228,7 +212,7 @@ where message_types.into_iter().map(move |message_type| { let key = MessageStateKey { feed_id: id, - type_: message_type, + type_: message_type, }; retrieve_message_state(self.into(), key, request_time.clone()) }) @@ -294,7 +278,7 @@ async fn retrieve_message_state( let lookup_time = MessageStateTime { publish_time: time, - slot: 0, + slot: 0, }; // Get the first element that is greater than or equal to the lookup time. @@ -323,16 +307,11 @@ async fn retrieve_message_state( mod test { use { super::*, - crate::state::{ - aggregate::wormhole_merkle::WormholeMerkleMessageProof, - test::setup_state, - }, + crate::state::{aggregate::wormhole_merkle::WormholeMerkleMessageProof, test::setup_state}, pyth_sdk::UnixTimestamp, pythnet_sdk::{ - accumulators::merkle::MerklePath, - hashers::keccak256_160::Keccak160, - messages::PriceFeedMessage, - wire::v1::WormholeMerkleRoot, + accumulators::merkle::MerklePath, hashers::keccak256_160::Keccak160, + messages::PriceFeedMessage, wire::v1::WormholeMerkleRoot, }, }; @@ -357,7 +336,7 @@ mod test { received_at: publish_time, proof_set: ProofSet { wormhole_merkle_proof: WormholeMerkleMessageProof { - vaa: vec![], + vaa: vec![], proof: MerklePath::::new(vec![]), }, }, @@ -593,7 +572,6 @@ mod test { ); } - #[tokio::test] pub async fn test_store_and_retrieve_first_after_message_state_fails_for_past_time() { // Initialize state with a cache size of 2 per key. @@ -771,7 +749,7 @@ mod test { pub fn create_empty_wormhole_merkle_state_at_slot(slot: Slot) -> WormholeMerkleState { WormholeMerkleState { - vaa: vec![], + vaa: vec![], root: WormholeMerkleRoot { slot, root: [0; 20], diff --git a/apps/hermes/server/src/state/metrics.rs b/apps/hermes/server/src/state/metrics.rs index 3a3869eb81..fd4dedfa13 100644 --- a/apps/hermes/server/src/state/metrics.rs +++ b/apps/hermes/server/src/state/metrics.rs @@ -2,10 +2,7 @@ use { super::State, prometheus_client::{ encoding::text::encode, - registry::{ - Metric, - Registry, - }, + registry::{Metric, Registry}, }, tokio::sync::RwLock, }; diff --git a/apps/hermes/server/src/state/price_feeds_metadata.rs b/apps/hermes/server/src/state/price_feeds_metadata.rs index 312f31049f..d6cbe09a7e 100644 --- a/apps/hermes/server/src/state/price_feeds_metadata.rs +++ b/apps/hermes/server/src/state/price_feeds_metadata.rs @@ -1,9 +1,6 @@ use { crate::{ - api::types::{ - AssetType, - PriceFeedMetadata, - }, + api::types::{AssetType, PriceFeedMetadata}, state::State, }, anyhow::Result, @@ -66,7 +63,6 @@ where Ok(()) } - async fn get_price_feeds_metadata( &self, query: Option, diff --git a/apps/hermes/server/src/state/wormhole.rs b/apps/hermes/server/src/state/wormhole.rs index f5edbe94ae..f57d2b5431 100644 --- a/apps/hermes/server/src/state/wormhole.rs +++ b/apps/hermes/server/src/state/wormhole.rs @@ -1,51 +1,26 @@ use { super::{ - aggregate::{ - Aggregates, - Update, - }, + aggregate::{Aggregates, Update}, State, }, crate::network::wormhole::GuardianSet, - anyhow::{ - anyhow, - ensure, - Result, - }, + anyhow::{anyhow, ensure, Result}, chrono::DateTime, pythnet_sdk::{ - wire::v1::{ - WormholeMessage, - WormholePayload, - }, + wire::v1::{WormholeMessage, WormholePayload}, ACCUMULATOR_EMITTER_ADDRESS, }, secp256k1::{ - ecdsa::{ - RecoverableSignature, - RecoveryId, - }, - Message, - Secp256k1, + ecdsa::{RecoverableSignature, RecoveryId}, + Message, Secp256k1, }, serde_wormhole::RawMessage, - sha3::{ - Digest, - Keccak256, - }, - std::collections::{ - BTreeMap, - BTreeSet, - }, + sha3::{Digest, Keccak256}, + std::collections::{BTreeMap, BTreeSet}, tokio::sync::RwLock, wormhole_sdk::{ - vaa::{ - Body, - Header, - }, - Address, - Chain, - Vaa, + vaa::{Body, Header}, + Address, Chain, Vaa, }, }; @@ -71,7 +46,7 @@ impl WormholeState { pub fn new() -> Self { Self { observed_vaa_seqs: RwLock::new(BTreeSet::new()), - guardian_set: RwLock::new(BTreeMap::new()), + guardian_set: RwLock::new(BTreeMap::new()), } } } diff --git a/governance/remote_executor/cli/src/cli.rs b/governance/remote_executor/cli/src/cli.rs index eced73160a..6d118baaa0 100644 --- a/governance/remote_executor/cli/src/cli.rs +++ b/governance/remote_executor/cli/src/cli.rs @@ -1,14 +1,8 @@ //! CLI options use { - clap::{ - Parser, - Subcommand, - }, + clap::{Parser, Subcommand}, remote_executor::state::governance_payload::CHAIN_ID_ARRAY, - solana_sdk::{ - commitment_config::CommitmentConfig, - pubkey::Pubkey, - }, + solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey}, }; #[derive(Parser, Debug)] @@ -20,11 +14,11 @@ pub struct Cli { #[clap(long, default_value = "confirmed")] pub commitment: CommitmentConfig, #[clap(long, default_value = "pythnet", parse(try_from_str = parse_chain))] - pub chain: u16, + pub chain: u16, #[clap(long, default_value = "https://pythnet.rpcpool.com/")] - pub rpc_url: String, + pub rpc_url: String, #[clap(subcommand)] - pub action: Action, + pub action: Action, } fn parse_chain(chain: &str) -> Result { @@ -40,7 +34,7 @@ pub enum Action { #[clap(about = "Post a VAA and execute it through the remote executor")] PostAndExecute { #[clap(short = 'v', long = "vaa")] - vaa: String, + vaa: String, #[clap( long, default_value = "~/.config/solana/id.json", @@ -62,22 +56,22 @@ pub enum Action { #[clap(about = "Get set upgrade authority payload for squads-cli")] GetSetUpgradeAuthorityPayload { #[clap(short, long, help = "Current authority")] - current: Pubkey, + current: Pubkey, #[clap(short, long, help = "New authority")] - new: Pubkey, + new: Pubkey, #[clap(short, long, help = "Program id")] program_id: Pubkey, }, #[clap(about = "Get upgrade program payload for squads-cli")] GetUpgradeProgramPayload { #[clap(short, long, help = "Current authority")] - authority: Pubkey, + authority: Pubkey, #[clap(short, long, help = "Program id")] program_id: Pubkey, #[clap(short, long, help = "New buffer")] new_buffer: Pubkey, #[clap(short, long, help = "Spill address")] - spill: Pubkey, + spill: Pubkey, }, #[clap(about = "Map solana key to pythnet key")] MapKey { diff --git a/governance/remote_executor/cli/src/main.rs b/governance/remote_executor/cli/src/main.rs index 1ce098f50f..d9356268cc 100644 --- a/governance/remote_executor/cli/src/main.rs +++ b/governance/remote_executor/cli/src/main.rs @@ -2,75 +2,41 @@ use { serde_wormhole::RawMessage, - wormhole_sdk::vaa::{ - Body, - Header, - Vaa, - }, + wormhole_sdk::vaa::{Body, Header, Vaa}, }; pub mod cli; use { anchor_client::{ anchor_lang::{ - AccountDeserialize, - AnchorDeserialize, - AnchorSerialize, - InstructionData as AnchorInstructionData, - Owner, - ToAccountMetas, + AccountDeserialize, AnchorDeserialize, AnchorSerialize, + InstructionData as AnchorInstructionData, Owner, ToAccountMetas, }, solana_sdk::bpf_loader_upgradeable, }, anyhow::Result, clap::Parser, - cli::{ - Action, - Cli, - }, + cli::{Action, Cli}, remote_executor::{ accounts::ExecutePostedVaa, state::{ - governance_payload::{ - ExecutorPayload, - GovernanceHeader, - InstructionData, - }, + governance_payload::{ExecutorPayload, GovernanceHeader, InstructionData}, posted_vaa::AnchorVaa, }, - EXECUTOR_KEY_SEED, - ID, - }, - solana_client::{ - rpc_client::RpcClient, - rpc_config::RpcSendTransactionConfig, + EXECUTOR_KEY_SEED, ID, }, + solana_client::{rpc_client::RpcClient, rpc_config::RpcSendTransactionConfig}, solana_sdk::{ - instruction::{ - AccountMeta, - Instruction, - }, + instruction::{AccountMeta, Instruction}, pubkey::Pubkey, - signature::{ - read_keypair_file, - Keypair, - }, + signature::{read_keypair_file, Keypair}, signer::Signer, - system_instruction::{self,}, + system_instruction::{self}, transaction::Transaction, }, std::str::FromStr, wormhole_solana::{ - instructions::{ - post_message, - post_vaa, - verify_signatures_txs, - PostVAAData, - }, - Account, - Config, - FeeCollector, - GuardianSet, - VAA as PostedVAA, + instructions::{post_message, post_vaa, verify_signatures_txs, PostVAAData}, + Account, Config, FeeCollector, GuardianSet, VAA as PostedVAA, }, }; @@ -119,15 +85,15 @@ fn main() -> Result<()> { // Post VAA let post_vaa_data = PostVAAData { - version: header.version, + version: header.version, guardian_set_index: header.guardian_set_index, - timestamp: body.timestamp, - nonce: body.nonce, - emitter_chain: body.emitter_chain.into(), - emitter_address: body.emitter_address.0, - sequence: body.sequence, - consistency_level: body.consistency_level, - payload: body.payload.to_vec(), + timestamp: body.timestamp, + nonce: body.nonce, + emitter_chain: body.emitter_chain.into(), + emitter_address: body.emitter_address.0, + sequence: body.sequence, + consistency_level: body.consistency_level, + payload: body.payload.to_vec(), }; process_transaction( @@ -178,7 +144,7 @@ fn main() -> Result<()> { ) .0; let payload = ExecutorPayload { - header: GovernanceHeader::executor_governance_header(cli.chain), + header: GovernanceHeader::executor_governance_header(cli.chain), instructions: vec![InstructionData::from(&system_instruction::transfer( &executor_key, &payer.pubkey(), @@ -210,7 +176,7 @@ fn main() -> Result<()> { } Action::GetTestPayload {} => { let payload = ExecutorPayload { - header: GovernanceHeader::executor_governance_header(cli.chain), + header: GovernanceHeader::executor_governance_header(cli.chain), instructions: vec![], } .try_to_vec()?; @@ -237,7 +203,7 @@ fn main() -> Result<()> { instruction.accounts[2].is_signer = true; // Require signature of new authority for safety println!("New authority : {:}", instruction.accounts[2].pubkey); let payload = ExecutorPayload { - header: GovernanceHeader::executor_governance_header(cli.chain), + header: GovernanceHeader::executor_governance_header(cli.chain), instructions: vec![InstructionData::from(&instruction)], } .try_to_vec()?; @@ -259,7 +225,7 @@ fn main() -> Result<()> { instruction.accounts[3].pubkey ); let payload = ExecutorPayload { - header: GovernanceHeader::executor_governance_header(cli.chain), + header: GovernanceHeader::executor_governance_header(cli.chain), instructions: vec![InstructionData::from(&instruction)], } .try_to_vec()?; @@ -338,8 +304,8 @@ pub fn get_execute_instruction( .0; account_metas.push(AccountMeta { - pubkey: executor_key, - is_signer: false, + pubkey: executor_key, + is_signer: false, is_writable: true, }); @@ -347,8 +313,8 @@ pub fn get_execute_instruction( for instruction in executor_payload.instructions { // Push program_id account_metas.push(AccountMeta { - pubkey: instruction.program_id, - is_signer: false, + pubkey: instruction.program_id, + is_signer: false, is_writable: false, }); // Push other accounts @@ -361,7 +327,7 @@ pub fn get_execute_instruction( Ok(Instruction { program_id: ID, - accounts: account_metas, - data: remote_executor::instruction::ExecutePostedVaa.data(), + accounts: account_metas, + data: remote_executor::instruction::ExecutePostedVaa.data(), }) } diff --git a/governance/remote_executor/programs/remote-executor/src/lib.rs b/governance/remote_executor/programs/remote-executor/src/lib.rs index ccda797351..a0e7f94377 100644 --- a/governance/remote_executor/programs/remote-executor/src/lib.rs +++ b/governance/remote_executor/programs/remote-executor/src/lib.rs @@ -3,20 +3,10 @@ #![allow(clippy::result_large_err)] use { - anchor_lang::{ - prelude::*, - solana_program::borsh::get_packed_len, - system_program, - }, + anchor_lang::{prelude::*, solana_program::borsh::get_packed_len, system_program}, error::ExecutorError, - state::{ - claim_record::ClaimRecord, - posted_vaa::AnchorVaa, - }, - wormhole_sdk::Chain::{ - self, - Solana, - }, + state::{claim_record::ClaimRecord, posted_vaa::AnchorVaa}, + wormhole_sdk::Chain::{self, Solana}, }; mod error; @@ -33,10 +23,7 @@ pub mod remote_executor { use { super::*, crate::state::governance_payload::ExecutorPayload, - anchor_lang::solana_program::{ - instruction::Instruction, - program::invoke_signed, - }, + anchor_lang::solana_program::{instruction::Instruction, program::invoke_signed}, }; pub fn execute_posted_vaa(ctx: Context) -> Result<()> { @@ -74,12 +61,12 @@ pub const CLAIM_RECORD_SEED: &str = "CLAIM_RECORD"; #[derive(Accounts)] pub struct ExecutePostedVaa<'info> { #[account(mut)] - pub payer: Signer<'info>, + pub payer: Signer<'info>, #[account(constraint = Chain::from(posted_vaa.emitter_chain) == Solana @ ExecutorError::EmitterChainNotSolana, constraint = posted_vaa.sequence > claim_record.sequence @ExecutorError::NonIncreasingSequence, constraint = (&posted_vaa.magic == b"vaa" || &posted_vaa.magic == b"msg" || &posted_vaa.magic == b"msu") @ExecutorError::PostedVaaHeaderWrongMagicNumber )] - pub posted_vaa: Account<'info, AnchorVaa>, + pub posted_vaa: Account<'info, AnchorVaa>, /// The reason claim_record has different seeds than executor_key is that executor key might need to pay in the CPI, so we want it to be a native wallet #[account(init_if_needed, space = 8 + get_packed_len::(), payer=payer, seeds = [CLAIM_RECORD_SEED.as_bytes(), &posted_vaa.emitter_address], bump)] - pub claim_record: Account<'info, ClaimRecord>, + pub claim_record: Account<'info, ClaimRecord>, pub system_program: Program<'info, System>, // Additional accounts passed to the instruction will be passed down to the CPIs. Very importantly executor_key needs to be passed as it will be the signer of the CPIs. // Below is the "anchor specification" of that account diff --git a/governance/remote_executor/programs/remote-executor/src/state/claim_record.rs b/governance/remote_executor/programs/remote-executor/src/state/claim_record.rs index 68a5c81d52..269957e939 100644 --- a/governance/remote_executor/programs/remote-executor/src/state/claim_record.rs +++ b/governance/remote_executor/programs/remote-executor/src/state/claim_record.rs @@ -1,9 +1,6 @@ use anchor_lang::{ account, - prelude::{ - borsh::BorshSchema, - *, - }, + prelude::{borsh::BorshSchema, *}, }; #[account] diff --git a/governance/remote_executor/programs/remote-executor/src/state/governance_payload.rs b/governance/remote_executor/programs/remote-executor/src/state/governance_payload.rs index b784ae2503..ddf670f607 100644 --- a/governance/remote_executor/programs/remote-executor/src/state/governance_payload.rs +++ b/governance/remote_executor/programs/remote-executor/src/state/governance_payload.rs @@ -1,15 +1,8 @@ use { crate::error::ExecutorError, - anchor_lang::{ - prelude::*, - solana_program::instruction::Instruction, - }, + anchor_lang::{prelude::*, solana_program::instruction::Instruction}, boolinator::Boolinator, - std::{ - io::ErrorKind, - mem::size_of, - ops::Deref, - }, + std::{io::ErrorKind, mem::size_of, ops::Deref}, }; pub const MAGIC_NUMBER: u32 = 0x4d475450; // Reverse order of the solidity contract because borsh uses little endian numbers (the solidity contract uses 0x5054474d) @@ -76,9 +69,9 @@ pub enum Action { #[derive(AnchorDeserialize, AnchorSerialize, Eq, PartialEq, Debug)] pub struct GovernanceHeader { pub magic_number: u32, - pub module: Module, - pub action: Action, - pub chain: BigEndianU16, + pub module: Module, + pub action: Action, + pub chain: BigEndianU16, } impl GovernanceHeader { @@ -86,9 +79,9 @@ impl GovernanceHeader { pub fn executor_governance_header(chain: u16) -> Self { Self { magic_number: MAGIC_NUMBER, - module: Module::Executor, - action: Action::ExecutePostedVaa, - chain: BigEndianU16 { value: chain }, + module: Module::Executor, + action: Action::ExecutePostedVaa, + chain: BigEndianU16 { value: chain }, } } } @@ -133,18 +126,18 @@ pub struct InstructionData { /// Pubkey of the instruction processor that executes this instruction pub program_id: Pubkey, /// Metadata for what accounts should be passed to the instruction processor - pub accounts: Vec, + pub accounts: Vec, /// Opaque data passed to the instruction processor - pub data: Vec, + pub data: Vec, } /// Account metadata used to define Instructions #[derive(Clone, Debug, PartialEq, Eq, AnchorDeserialize, AnchorSerialize)] pub struct AccountMetaData { /// An account's public key - pub pubkey: Pubkey, + pub pubkey: Pubkey, /// True if an Instruction requires a Transaction signature matching `pubkey`. - pub is_signer: bool, + pub is_signer: bool, /// True if the `pubkey` can be loaded as a read-write account. pub is_writable: bool, } @@ -153,16 +146,16 @@ impl From<&InstructionData> for Instruction { fn from(instruction: &InstructionData) -> Self { Instruction { program_id: instruction.program_id, - accounts: instruction + accounts: instruction .accounts .iter() .map(|a| AccountMeta { - pubkey: a.pubkey, - is_signer: a.is_signer, + pubkey: a.pubkey, + is_signer: a.is_signer, is_writable: a.is_writable, }) .collect(), - data: instruction.data.clone(), + data: instruction.data.clone(), } } } @@ -171,16 +164,16 @@ impl From<&Instruction> for InstructionData { fn from(instruction: &Instruction) -> Self { InstructionData { program_id: instruction.program_id, - accounts: instruction + accounts: instruction .accounts .iter() .map(|a| AccountMetaData { - pubkey: a.pubkey, - is_signer: a.is_signer, + pubkey: a.pubkey, + is_signer: a.is_signer, is_writable: a.is_writable, }) .collect(), - data: instruction.data.clone(), + data: instruction.data.clone(), } } } @@ -207,18 +200,11 @@ pub mod tests { super::ExecutorPayload, crate::{ error::ExecutorError, - state::governance_payload::{ - InstructionData, - CHAIN_ID, - }, + state::governance_payload::{InstructionData, CHAIN_ID}, }, anchor_lang::{ - prelude::{ - Pubkey, - *, - }, - AnchorDeserialize, - AnchorSerialize, + prelude::{Pubkey, *}, + AnchorDeserialize, AnchorSerialize, }, }; @@ -226,7 +212,7 @@ pub mod tests { fn test_check_deserialization_serialization() { // No instructions let payload = ExecutorPayload { - header: super::GovernanceHeader::executor_governance_header(CHAIN_ID), + header: super::GovernanceHeader::executor_governance_header(CHAIN_ID), instructions: vec![], }; diff --git a/governance/remote_executor/programs/remote-executor/src/state/posted_vaa.rs b/governance/remote_executor/programs/remote-executor/src/state/posted_vaa.rs index e7d3fa7909..f17fec6833 100644 --- a/governance/remote_executor/programs/remote-executor/src/state/posted_vaa.rs +++ b/governance/remote_executor/programs/remote-executor/src/state/posted_vaa.rs @@ -1,10 +1,6 @@ use { anchor_lang::prelude::*, - std::{ - io::Write, - ops::Deref, - str::FromStr, - }, + std::{io::Write, ops::Deref, str::FromStr}, wormhole_solana::VAA, }; @@ -65,5 +61,5 @@ impl Deref for AnchorVaa { #[derive(Clone, AnchorDeserialize, AnchorSerialize)] pub struct AnchorVaa { pub magic: [u8; 3], - pub vaa: VAA, + pub vaa: VAA, } diff --git a/governance/remote_executor/programs/remote-executor/src/tests/executor_simulator.rs b/governance/remote_executor/programs/remote-executor/src/tests/executor_simulator.rs index 26aff7f94d..9457c47d7f 100644 --- a/governance/remote_executor/programs/remote-executor/src/tests/executor_simulator.rs +++ b/governance/remote_executor/programs/remote-executor/src/tests/executor_simulator.rs @@ -3,61 +3,31 @@ use { error::ExecutorError, state::{ claim_record::ClaimRecord, - governance_payload::{ - ExecutorPayload, - GovernanceHeader, - InstructionData, - CHAIN_ID, - }, + governance_payload::{ExecutorPayload, GovernanceHeader, InstructionData, CHAIN_ID}, posted_vaa::AnchorVaa, }, - CLAIM_RECORD_SEED, - EXECUTOR_KEY_SEED, + CLAIM_RECORD_SEED, EXECUTOR_KEY_SEED, }, anchor_lang::{ - prelude::{ - AccountMeta, - ProgramError, - Pubkey, - Rent, - UpgradeableLoaderState, - }, + prelude::{AccountMeta, ProgramError, Pubkey, Rent, UpgradeableLoaderState}, solana_program::hash::Hash, - AccountDeserialize, - AnchorDeserialize, - AnchorSerialize, - InstructionData as AnchorInstructionData, - Key, - Owner, - ToAccountMetas, + AccountDeserialize, AnchorDeserialize, AnchorSerialize, + InstructionData as AnchorInstructionData, Key, Owner, ToAccountMetas, }, solana_program_test::{ - read_file, - BanksClient, - BanksClientError, - ProgramTest, - ProgramTestBanksClientExt, + read_file, BanksClient, BanksClientError, ProgramTest, ProgramTestBanksClientExt, }, solana_sdk::{ account::Account, bpf_loader_upgradeable, - instruction::{ - Instruction, - InstructionError, - }, + instruction::{Instruction, InstructionError}, signature::Keypair, signer::Signer, stake_history::Epoch, system_instruction, - transaction::{ - Transaction, - TransactionError, - }, - }, - std::{ - collections::HashMap, - path::Path, + transaction::{Transaction, TransactionError}, }, + std::{collections::HashMap, path::Path}, wormhole_sdk::Chain, wormhole_solana::VAA, }; @@ -65,8 +35,8 @@ use { /// Bench for the tests, the goal of this struct is to be able to setup solana accounts before starting the local validator pub struct ExecutorBench { program_test: ProgramTest, - program_id: Pubkey, - seqno: HashMap, + program_id: Pubkey, + seqno: HashMap, } /// When passed to `add_vaa_account` modify the posted vaa in a way that makes the vaa invalid @@ -100,7 +70,7 @@ impl ExecutorBench { programdata_address: programdata_key, }; let programdata_deserialized = UpgradeableLoaderState::ProgramData { - slot: 1, + slot: 1, upgrade_authority_address: Some(upgrade_authority_keypair.pubkey()), }; @@ -111,16 +81,16 @@ impl ExecutorBench { programdata_vec.append(&mut bpf_data); let program_account = Account { - lamports: Rent::default().minimum_balance(program_vec.len()), - data: program_vec, - owner: bpf_loader_upgradeable::ID, + lamports: Rent::default().minimum_balance(program_vec.len()), + data: program_vec, + owner: bpf_loader_upgradeable::ID, executable: true, rent_epoch: Epoch::default(), }; let programdata_account = Account { - lamports: Rent::default().minimum_balance(programdata_vec.len()), - data: programdata_vec, - owner: bpf_loader_upgradeable::ID, + lamports: Rent::default().minimum_balance(programdata_vec.len()), + data: programdata_vec, + owner: bpf_loader_upgradeable::ID, executable: false, rent_epoch: Epoch::default(), }; @@ -172,7 +142,7 @@ impl ExecutorBench { }; let payload = ExecutorPayload { - header: GovernanceHeader::executor_governance_header(CHAIN_ID), + header: GovernanceHeader::executor_governance_header(CHAIN_ID), instructions: instructions.iter().map(InstructionData::from).collect(), }; @@ -180,7 +150,7 @@ impl ExecutorBench { let vaa = AnchorVaa { magic: *vaa_magic, - vaa: VAA { + vaa: VAA { vaa_version: 0, consistency_level: 0, vaa_time: 0, @@ -235,10 +205,10 @@ impl ExecutorBench { } } pub struct ExecutorSimulator { - banks_client: BanksClient, - payer: Keypair, + banks_client: BanksClient, + payer: Keypair, last_blockhash: Hash, - program_id: Pubkey, + program_id: Pubkey, } /// When passed to execute_posted_vaa, try to impersonate some of the accounts @@ -336,8 +306,8 @@ impl ExecutorSimulator { // We need to add `executor_key` to the list of accounts account_metas.push(AccountMeta { - pubkey: executor_key, - is_signer: false, + pubkey: executor_key, + is_signer: false, is_writable: true, }); @@ -345,8 +315,8 @@ impl ExecutorSimulator { for instruction in executor_payload.instructions { // Push program_id account_metas.push(AccountMeta { - pubkey: instruction.program_id, - is_signer: false, + pubkey: instruction.program_id, + is_signer: false, is_writable: false, }); // Push other accounts @@ -359,8 +329,8 @@ impl ExecutorSimulator { let instruction = Instruction { program_id: self.program_id, - accounts: account_metas, - data: crate::instruction::ExecutePostedVaa.data(), + accounts: account_metas, + data: crate::instruction::ExecutePostedVaa.data(), }; self.process_ix(instruction, signers).await diff --git a/governance/remote_executor/programs/remote-executor/src/tests/test_adversarial.rs b/governance/remote_executor/programs/remote-executor/src/tests/test_adversarial.rs index 83dc4d433c..7e92cc9142 100644 --- a/governance/remote_executor/programs/remote-executor/src/tests/test_adversarial.rs +++ b/governance/remote_executor/programs/remote-executor/src/tests/test_adversarial.rs @@ -1,21 +1,10 @@ use { - super::executor_simulator::{ - ExecutorAttack, - ExecutorBench, - VaaAttack, - }, + super::executor_simulator::{ExecutorAttack, ExecutorBench, VaaAttack}, crate::error::ExecutorError, - anchor_lang::prelude::{ - ErrorCode, - ProgramError, - Pubkey, - Rent, - }, + anchor_lang::prelude::{ErrorCode, ProgramError, Pubkey, Rent}, solana_sdk::{ - instruction::InstructionError, - native_token::LAMPORTS_PER_SOL, - system_instruction::transfer, - transaction::TransactionError, + instruction::InstructionError, native_token::LAMPORTS_PER_SOL, + system_instruction::transfer, transaction::TransactionError, }, }; diff --git a/governance/remote_executor/programs/remote-executor/src/tests/test_basic_instructions.rs b/governance/remote_executor/programs/remote-executor/src/tests/test_basic_instructions.rs index a44d9b1b6f..bbb76b46b6 100644 --- a/governance/remote_executor/programs/remote-executor/src/tests/test_basic_instructions.rs +++ b/governance/remote_executor/programs/remote-executor/src/tests/test_basic_instructions.rs @@ -2,25 +2,14 @@ use { super::executor_simulator::ExecutorBench, crate::{ error::ExecutorError, - tests::executor_simulator::{ - ExecutorAttack, - VaaAttack, - }, + tests::executor_simulator::{ExecutorAttack, VaaAttack}, }, anchor_lang::{ - prelude::{ - Pubkey, - Rent, - }, - solana_program::{ - system_instruction::create_account, - system_program, - }, + prelude::{Pubkey, Rent}, + solana_program::{system_instruction::create_account, system_program}, }, solana_sdk::{ - native_token::LAMPORTS_PER_SOL, - signature::Keypair, - signer::Signer, + native_token::LAMPORTS_PER_SOL, signature::Keypair, signer::Signer, system_instruction::transfer, }, }; diff --git a/pythnet/message_buffer/programs/message_buffer/src/instructions/create_buffer.rs b/pythnet/message_buffer/programs/message_buffer/src/instructions/create_buffer.rs index 7fbe3417e0..0c2bead240 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/instructions/create_buffer.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/instructions/create_buffer.rs @@ -1,20 +1,11 @@ use { crate::{ - instructions::is_uninitialized_account, - state::*, - MessageBufferError, - MESSAGE, - WHITELIST, + instructions::is_uninitialized_account, state::*, MessageBufferError, MESSAGE, WHITELIST, }, anchor_lang::{ prelude::*, solana_program::entrypoint::MAX_PERMITTED_DATA_INCREASE, - system_program::{ - self, - Allocate, - Assign, - Transfer, - }, + system_program::{self, Allocate, Assign, Transfer}, }, }; @@ -85,7 +76,6 @@ pub fn create_buffer<'info>( Ok(()) } - #[derive(Accounts)] pub struct CreateBuffer<'info> { #[account( @@ -105,7 +95,6 @@ pub struct CreateBuffer<'info> { // remaining_accounts: - [AccumulatorInput PDA] } - impl<'info> CreateBuffer<'info> { /// Manually invoke transfer, allocate & assign ixs to create an account /// to handle situation where an account already has lamports @@ -124,7 +113,7 @@ impl<'info> CreateBuffer<'info> { system_program.to_account_info(), Transfer { from: payer.to_account_info(), - to: new_account_info.to_account_info(), + to: new_account_info.to_account_info(), }, seeds, ), diff --git a/pythnet/message_buffer/programs/message_buffer/src/instructions/delete_buffer.rs b/pythnet/message_buffer/programs/message_buffer/src/instructions/delete_buffer.rs index a9d61c08af..21769b09c6 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/instructions/delete_buffer.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/instructions/delete_buffer.rs @@ -1,9 +1,5 @@ use { - crate::{ - state::*, - MESSAGE, - WHITELIST, - }, + crate::{state::*, MESSAGE, WHITELIST}, anchor_lang::prelude::*, }; diff --git a/pythnet/message_buffer/programs/message_buffer/src/instructions/mod.rs b/pythnet/message_buffer/programs/message_buffer/src/instructions/mod.rs index 67a377ab2a..b57a972aeb 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/instructions/mod.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/instructions/mod.rs @@ -1,14 +1,5 @@ -use anchor_lang::{ - prelude::*, - system_program, -}; -pub use { - create_buffer::*, - delete_buffer::*, - put_all::*, - resize_buffer::*, -}; - +use anchor_lang::{prelude::*, system_program}; +pub use {create_buffer::*, delete_buffer::*, put_all::*, resize_buffer::*}; mod create_buffer; mod delete_buffer; diff --git a/pythnet/message_buffer/programs/message_buffer/src/instructions/put_all.rs b/pythnet/message_buffer/programs/message_buffer/src/instructions/put_all.rs index 8ba34d0229..92e2198014 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/instructions/put_all.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/instructions/put_all.rs @@ -1,12 +1,8 @@ use { - crate::{ - state::*, - MESSAGE, - }, + crate::{state::*, MESSAGE}, anchor_lang::prelude::*, }; - pub fn put_all<'info>( ctx: Context<'_, '_, '_, 'info, PutAll<'info>>, _base_account_key: Pubkey, @@ -39,5 +35,5 @@ pub struct PutAll<'info> { seeds = [whitelist_verifier.cpi_caller_auth.key().as_ref(), MESSAGE.as_bytes(), base_account_key.as_ref()], bump = message_buffer.load()?.bump, )] - pub message_buffer: AccountLoader<'info, MessageBuffer>, + pub message_buffer: AccountLoader<'info, MessageBuffer>, } diff --git a/pythnet/message_buffer/programs/message_buffer/src/instructions/resize_buffer.rs b/pythnet/message_buffer/programs/message_buffer/src/instructions/resize_buffer.rs index 1c01915ea5..1110f3e873 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/instructions/resize_buffer.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/instructions/resize_buffer.rs @@ -1,14 +1,8 @@ use { - crate::{ - state::*, - MessageBufferError, - MESSAGE, - WHITELIST, - }, + crate::{state::*, MessageBufferError, MESSAGE, WHITELIST}, anchor_lang::prelude::*, }; - pub fn resize_buffer<'info>( ctx: Context<'_, '_, '_, 'info, ResizeBuffer<'info>>, allowed_program_auth: Pubkey, diff --git a/pythnet/message_buffer/programs/message_buffer/src/lib.rs b/pythnet/message_buffer/programs/message_buffer/src/lib.rs index 9b7b915374..6f40ada507 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/lib.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/lib.rs @@ -1,12 +1,8 @@ pub mod instructions; mod state; - use { - crate::{ - MESSAGE, - WHITELIST, - }, + crate::{MESSAGE, WHITELIST}, anchor_lang::prelude::*, instructions::*, state::*, @@ -18,7 +14,6 @@ declare_id!("7Vbmv1jt4vyuqBZcpYPpnVhrqVe5e6ZPb6JxDcffRHUM"); pub mod message_buffer { use super::*; - /// Initializes the whitelist and sets it's admin. Once initialized, /// the admin must sign all further changes to the whitelist. pub fn initialize(ctx: Context) -> Result<()> { @@ -51,7 +46,6 @@ pub mod message_buffer { Ok(()) } - /// Put messages into the Accumulator. All messages put for the same /// `base_account_key` go into the same buffer PDA. The PDA's address is /// `[allowed_program_auth, MESSAGE, base_account_key]`, where `allowed_program_auth` @@ -83,7 +77,6 @@ pub mod message_buffer { instructions::put_all(ctx, base_account_key, messages) } - /// Initializes the buffer account with the `target_size` /// /// *`allowed_program_auth` - The whitelisted pubkey representing an @@ -148,7 +141,7 @@ pub struct Initialize<'info> { pub admin: Signer<'info>, #[account(mut)] - pub payer: Signer<'info>, + pub payer: Signer<'info>, #[account( init, payer = payer, @@ -156,14 +149,13 @@ pub struct Initialize<'info> { bump, space = 8 + Whitelist::INIT_SPACE, )] - pub whitelist: Account<'info, Whitelist>, + pub whitelist: Account<'info, Whitelist>, pub system_program: Program<'info, System>, } - #[derive(Accounts)] pub struct UpdateWhitelist<'info> { - pub admin: Signer<'info>, + pub admin: Signer<'info>, #[account( mut, seeds = [MESSAGE.as_bytes(), WHITELIST.as_bytes()], @@ -173,7 +165,6 @@ pub struct UpdateWhitelist<'info> { pub whitelist: Account<'info, Whitelist>, } - #[error_code] pub enum MessageBufferError { #[msg("CPI Caller not allowed")] diff --git a/pythnet/message_buffer/programs/message_buffer/src/state/message_buffer.rs b/pythnet/message_buffer/programs/message_buffer/src/state/message_buffer.rs index a6cd3235c4..501df561b2 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/state/message_buffer.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/state/message_buffer.rs @@ -27,24 +27,23 @@ use anchor_lang::prelude::*; #[derive(InitSpace, Debug)] pub struct MessageBuffer { /* header */ - pub bump: u8, // 1 - pub version: u8, // 1 + pub bump: u8, // 1 + pub version: u8, // 1 // byte offset of accounts where data starts // e.g. account_info.data[offset + header_len] - pub header_len: u16, // 2 + pub header_len: u16, // 2 /// endpoints of every message. /// ex: [10, 14] /// => msg1 = account_info.data[(header_len + 0)..(header_len + 10)] /// => msg2 = account_info.data[(header_len + 10)..(header_len + 14)] pub end_offsets: [u16; 255], // 510 - /* messages */ - // not defined in struct since needs to support variable length - // and work with zero_copy - // pub messages: [u8; accountInfo.data.len - header_len] + /* messages */ + // not defined in struct since needs to support variable length + // and work with zero_copy + // pub messages: [u8; accountInfo.data.len - header_len] } - impl MessageBuffer { // HEADER_LEN allows for append-only forward-compatibility for the header. // this is the number of bytes from the beginning of the account_info.data @@ -72,7 +71,6 @@ impl MessageBuffer { self.end_offsets = [0u16; u8::MAX as usize]; } - /// `put_all` writes all the messages to the `AccumulatorInput` account /// and updates the `end_offsets` array. /// @@ -124,10 +122,7 @@ mod test { bytemuck::bytes_of_mut, std::{ io::Write, - mem::{ - align_of, - size_of, - }, + mem::{align_of, size_of}, }, }; @@ -157,7 +152,6 @@ mod test { let discriminator = &mut sighash("accounts", "MessageBuffer"); let destination = &mut vec![0u8; destination_size]; - account_info_data.write_all(discriminator).unwrap(); account_info_data .write_all(bytes_of_mut(message_buffer)) @@ -166,7 +160,6 @@ mod test { account_info_data.to_vec() } - #[test] fn test_sizes_and_alignments() { let (message_buffer_size, message_buffer_align) = @@ -193,7 +186,6 @@ mod test { assert_eq!(num_msgs, 2); assert_eq!(num_bytes, 5); - assert_eq!(message_buffer.end_offsets[0], 2); assert_eq!(message_buffer.end_offsets[1], 5); @@ -218,7 +210,6 @@ mod test { } } - #[test] fn test_put_all_exceed_max() { let data = vec![vec![0u8; 9_718 - 2], vec![0u8], vec![0u8; 2]]; @@ -244,11 +235,9 @@ mod test { assert_eq!(message_buffer.end_offsets[0], 9_718 - 2); assert_eq!(message_buffer.end_offsets[1], 9_718 - 1); - let message_buffer: &MessageBuffer = bytemuck::from_bytes(&account_info_data.as_slice()[8..header_len]); - let iter = message_buffer.end_offsets.iter().take_while(|x| **x != 0); let mut start = header_len; let mut data_iter = data_bytes.iter(); @@ -284,14 +273,12 @@ mod test { let (num_msgs, num_bytes) = message_buffer.put_all_in_buffer(body_bytes, &data_bytes); - assert_eq!(num_msgs, 3); assert_eq!( num_bytes, data_bytes[0..3].iter().map(|x| x.len()).sum::() as u16 ); - let message_buffer: &MessageBuffer = bytemuck::from_bytes(&account_info_data.as_slice()[8..header_len]); @@ -315,10 +302,7 @@ mod test { #[test] pub fn test_cursor_read() { - use byteorder::{ - LittleEndian, - ReadBytesExt, - }; + use byteorder::{LittleEndian, ReadBytesExt}; let data = vec![vec![12, 34], vec![56, 78, 90]]; let data_bytes: Vec> = data.into_iter().map(data_bytes).collect(); @@ -336,7 +320,6 @@ mod test { assert_eq!(message_buffer.end_offsets[0], 2); assert_eq!(message_buffer.end_offsets[1], 5); - let mut cursor = std::io::Cursor::new(&account_info_data[10..]); let header_len = cursor.read_u16::().unwrap(); println!("header_len: {}", header_len); @@ -383,7 +366,6 @@ mod test { let (num_msgs, num_bytes) = message_buffer.put_all_in_buffer(body_bytes, &data_bytes); - assert_eq!(num_msgs, u8::MAX as usize); assert_eq!( num_bytes, @@ -393,7 +375,6 @@ mod test { .sum::() as u16 ); - let message_buffer: &MessageBuffer = bytemuck::from_bytes(&account_info_data.as_slice()[8..header_len]); @@ -439,14 +420,12 @@ mod test { let (num_msgs, num_bytes) = message_buffer.put_all_in_buffer(body_bytes, &data_bytes); - assert_eq!(num_msgs, 3); assert_eq!( num_bytes, data_bytes[0..3].iter().map(|x| x.len()).sum::() as u16 ); - let message_buffer: &MessageBuffer = bytemuck::from_bytes(&account_info_data.as_slice()[8..header_len]); diff --git a/pythnet/message_buffer/programs/message_buffer/src/state/mod.rs b/pythnet/message_buffer/programs/message_buffer/src/state/mod.rs index ec1f3654f4..75a15c8271 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/state/mod.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/state/mod.rs @@ -1,7 +1,4 @@ -pub use { - self::message_buffer::*, - whitelist::*, -}; +pub use {self::message_buffer::*, whitelist::*}; mod message_buffer; mod whitelist; diff --git a/pythnet/message_buffer/programs/message_buffer/src/state/whitelist.rs b/pythnet/message_buffer/programs/message_buffer/src/state/whitelist.rs index ed457513ad..b5ed7112bb 100644 --- a/pythnet/message_buffer/programs/message_buffer/src/state/whitelist.rs +++ b/pythnet/message_buffer/programs/message_buffer/src/state/whitelist.rs @@ -1,9 +1,5 @@ use { - crate::{ - MessageBufferError, - MESSAGE, - WHITELIST, - }, + crate::{MessageBufferError, MESSAGE, WHITELIST}, anchor_lang::prelude::*, }; @@ -13,8 +9,8 @@ use { #[account] #[derive(InitSpace)] pub struct Whitelist { - pub bump: u8, - pub admin: Pubkey, + pub bump: u8, + pub admin: Pubkey, // This is used by the `#[derive(InitSpace)]` // to determine initial account size #[max_len(32)] diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/add_price.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/add_price.rs index 57ea5bb81f..43011d4775 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/add_price.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/add_price.rs @@ -1,27 +1,14 @@ use { crate::{ - instructions::{ - sighash, - ACCUMULATOR_UPDATER_IX_NAME, - UPD_PRICE_WRITE, - }, + instructions::{sighash, ACCUMULATOR_UPDATER_IX_NAME, UPD_PRICE_WRITE}, message::{ get_schemas, - price::{ - CompactPriceMessage, - FullPriceMessage, - }, + price::{CompactPriceMessage, FullPriceMessage}, AccumulatorSerializer, }, - state::{ - PriceAccount, - PythAccountType, - }, - }, - anchor_lang::{ - prelude::*, - system_program, + state::{PriceAccount, PythAccountType}, }, + anchor_lang::{prelude::*, system_program}, message_buffer::program::MessageBuffer as MessageBufferProgram, }; @@ -41,20 +28,17 @@ pub fn add_price<'info>( inputs.push(price_full_data); - let price_compact_data = CompactPriceMessage::from(&**pyth_price_acct).accumulator_serialize()?; inputs.push(price_compact_data); } - // Note: normally pyth oracle add_price wouldn't call emit_accumulator_inputs // since add_price doesn't actually add/update any price data we would // want included in the accumulator anyways. This is just for testing AddPrice::emit_messages(ctx, inputs) } - impl<'info> AddPrice<'info> { /// Invoke message_buffer::put_all ix cpi call using solana pub fn emit_messages( @@ -109,11 +93,11 @@ impl<'info> AddPrice<'info> { #[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, PartialEq, Eq)] pub struct AddPriceParams { - pub id: u64, - pub price: u64, + pub id: u64, + pub price: u64, pub price_expo: u64, - pub ema: u64, - pub ema_expo: u64, + pub ema: u64, + pub ema_expo: u64, } #[derive(Accounts)] @@ -126,13 +110,13 @@ pub struct AddPrice<'info> { bump, space = 8 + PriceAccount::INIT_SPACE )] - pub pyth_price_account: AccountLoader<'info, PriceAccount>, + pub pyth_price_account: AccountLoader<'info, PriceAccount>, #[account(mut)] - pub payer: Signer<'info>, + pub payer: Signer<'info>, /// also needed for accumulator_updater - pub system_program: Program<'info, System>, + pub system_program: Program<'info, System>, /// CHECK: whitelist - pub accumulator_whitelist: UncheckedAccount<'info>, + pub accumulator_whitelist: UncheckedAccount<'info>, /// PDA representing this program's authority /// to call the accumulator program #[account( @@ -140,7 +124,7 @@ pub struct AddPrice<'info> { owner = system_program::System::id(), bump, )] - pub auth: SystemAccount<'info>, + pub auth: SystemAccount<'info>, pub message_buffer_program: Program<'info, MessageBufferProgram>, // Remaining Accounts // MessageBuffer PDA diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/cpi_max_test.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/cpi_max_test.rs index ccea8f0b16..f1e89b7674 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/cpi_max_test.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/cpi_max_test.rs @@ -1,13 +1,7 @@ use { crate::{ - instructions::{ - UpdatePrice, - UpdatePriceParams, - }, - message::{ - price::DummyPriceMessage, - AccumulatorSerializer, - }, + instructions::{UpdatePrice, UpdatePriceParams}, + message::{price::DummyPriceMessage, AccumulatorSerializer}, }, anchor_lang::prelude::*, }; @@ -32,6 +26,5 @@ pub fn cpi_max_test<'info>( let input_len = inputs.iter().map(|x| x.len()).sum::(); msg!("input_len: {}", input_len); - UpdatePrice::emit_messages(ctx, inputs) } diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/mod.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/mod.rs index fdc26fff63..edd6fbf313 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/mod.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/mod.rs @@ -1,9 +1,5 @@ use anchor_lang::solana_program::hash::hashv; -pub use { - add_price::*, - cpi_max_test::*, - update_price::*, -}; +pub use {add_price::*, cpi_max_test::*, update_price::*}; mod add_price; mod cpi_max_test; diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/update_price.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/update_price.rs index 814b76bed4..f3ef7e93e5 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/update_price.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/instructions/update_price.rs @@ -1,35 +1,24 @@ use { crate::{ - instructions::{ - sighash, - ACCUMULATOR_UPDATER_IX_NAME, - UPD_PRICE_WRITE, - }, + instructions::{sighash, ACCUMULATOR_UPDATER_IX_NAME, UPD_PRICE_WRITE}, message::{ - price::{ - CompactPriceMessage, - FullPriceMessage, - }, + price::{CompactPriceMessage, FullPriceMessage}, AccumulatorSerializer, }, state::PriceAccount, }, - anchor_lang::{ - prelude::*, - system_program, - }, + anchor_lang::{prelude::*, system_program}, message_buffer::program::MessageBuffer as MessageBufferProgram, }; #[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, PartialEq, Eq)] pub struct UpdatePriceParams { - pub price: u64, + pub price: u64, pub price_expo: u64, - pub ema: u64, - pub ema_expo: u64, + pub ema: u64, + pub ema_expo: u64, } - #[derive(Accounts)] pub struct UpdatePrice<'info> { #[account( @@ -41,15 +30,15 @@ pub struct UpdatePrice<'info> { ], bump, )] - pub pyth_price_account: AccountLoader<'info, PriceAccount>, + pub pyth_price_account: AccountLoader<'info, PriceAccount>, /// CHECK: whitelist - pub accumulator_whitelist: UncheckedAccount<'info>, + pub accumulator_whitelist: UncheckedAccount<'info>, #[account( seeds = [b"upd_price_write".as_ref(), message_buffer_program.key().as_ref()], owner = system_program::System::id(), bump, )] - pub auth: SystemAccount<'info>, + pub auth: SystemAccount<'info>, pub message_buffer_program: Program<'info, MessageBufferProgram>, } @@ -69,13 +58,11 @@ pub fn update_price<'info>( inputs.push(price_full_data); - let price_compact_data = CompactPriceMessage::from(&**pyth_price_acct).accumulator_serialize()?; inputs.push(price_compact_data); } - UpdatePrice::emit_messages(ctx, inputs) } diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/lib.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/lib.rs index b685908190..c0fa10ec75 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/lib.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/lib.rs @@ -1,7 +1,4 @@ -use { - anchor_lang::prelude::*, - instructions::*, -}; +use {anchor_lang::prelude::*, instructions::*}; pub mod instructions; pub mod message; @@ -39,19 +36,15 @@ pub mod mock_cpi_caller { } } - #[cfg(test)] mod test { - use { - super::*, - anchor_lang::InstructionData, - }; + use {super::*, anchor_lang::InstructionData}; #[test] fn ix_discriminator() { let a = &(message_buffer::instruction::PutAll { base_account_key: anchor_lang::prelude::Pubkey::default(), - messages: vec![], + messages: vec![], } .data()[..8]); diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/message.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/message.rs index ca70cc5732..d9c75ad6ce 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/message.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/message.rs @@ -5,10 +5,10 @@ pub mod price; #[derive(Copy, Clone)] #[repr(u8)] pub enum MessageSchema { - Full = 0, + Full = 0, Compact = 1, Minimal = 2, - Dummy = 3, + Dummy = 3, } impl MessageSchema { @@ -17,7 +17,6 @@ impl MessageSchema { } } - pub fn get_schemas(account_type: PythAccountType) -> Vec { match account_type { PythAccountType::Price => vec![MessageSchema::Full, MessageSchema::Compact], diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/message/price.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/message/price.rs index 3c42f0370a..f79087f609 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/message/price.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/message/price.rs @@ -1,22 +1,18 @@ use { crate::{ - message::{ - AccumulatorSerializer, - MessageSchema, - }, + message::{AccumulatorSerializer, MessageSchema}, state::PriceAccount, }, anchor_lang::prelude::*, std::io::Write, }; - #[repr(C)] #[derive(Clone, Default, Debug, Eq, PartialEq)] pub struct MessageHeader { - pub schema: u8, + pub schema: u8, pub version: u16, - pub size: u32, + pub size: u32, } impl MessageHeader { @@ -34,13 +30,12 @@ impl MessageHeader { #[repr(C)] #[derive(Clone, Default, Debug, Eq, PartialEq)] pub struct CompactPriceMessage { - pub header: MessageHeader, - pub id: u64, - pub price: u64, + pub header: MessageHeader, + pub id: u64, + pub price: u64, pub price_expo: u64, } - impl CompactPriceMessage { // size without header pub const SIZE: usize = 24; @@ -62,24 +57,23 @@ impl AccumulatorSerializer for CompactPriceMessage { impl From<&PriceAccount> for CompactPriceMessage { fn from(other: &PriceAccount) -> Self { Self { - header: MessageHeader::new(MessageSchema::Compact, Self::SIZE as u32), - id: other.id, - price: other.price, + header: MessageHeader::new(MessageSchema::Compact, Self::SIZE as u32), + id: other.id, + price: other.price, price_expo: other.price_expo, } } } - #[repr(C)] #[derive(Clone, Default, Debug, Eq, PartialEq)] pub struct FullPriceMessage { - pub header: MessageHeader, - pub id: u64, - pub price: u64, + pub header: MessageHeader, + pub id: u64, + pub price: u64, pub price_expo: u64, - pub ema: u64, - pub ema_expo: u64, + pub ema: u64, + pub ema_expo: u64, } impl FullPriceMessage { @@ -89,12 +83,12 @@ impl FullPriceMessage { impl From<&PriceAccount> for FullPriceMessage { fn from(other: &PriceAccount) -> Self { Self { - header: MessageHeader::new(MessageSchema::Full, Self::SIZE as u32), - id: other.id, - price: other.price, + header: MessageHeader::new(MessageSchema::Full, Self::SIZE as u32), + id: other.id, + price: other.price, price_expo: other.price_expo, - ema: other.ema, - ema_expo: other.ema_expo, + ema: other.ema, + ema_expo: other.ema_expo, } } } @@ -114,27 +108,24 @@ impl AccumulatorSerializer for FullPriceMessage { } } - #[repr(C)] #[derive(Clone, Debug, Eq, PartialEq)] pub struct DummyPriceMessage { pub header: MessageHeader, - pub data: Vec, + pub data: Vec, } - impl DummyPriceMessage { pub const SIZE: usize = 1017; pub fn new(msg_size: u16) -> Self { Self { header: MessageHeader::new(MessageSchema::Dummy, msg_size as u32), - data: vec![0u8; msg_size as usize], + data: vec![0u8; msg_size as usize], } } } - impl AccumulatorSerializer for DummyPriceMessage { fn accumulator_serialize(&self) -> Result> { let mut bytes = vec![]; diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/state/mod.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/state/mod.rs index 83485111de..334624ce74 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/state/mod.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/state/mod.rs @@ -12,10 +12,10 @@ trait PythAccount { #[derive(Copy, Clone)] #[repr(u32)] pub enum PythAccountType { - Mapping = 1, - Product = 2, - Price = 3, - Test = 4, + Mapping = 1, + Product = 2, + Price = 3, + Test = 4, Permissions = 5, } impl PythAccountType { diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/src/state/price.rs b/pythnet/message_buffer/programs/mock-cpi-caller/src/state/price.rs index fd6662908b..bc7baf1984 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/src/state/price.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/src/state/price.rs @@ -1,13 +1,7 @@ use { crate::{ - instructions::{ - AddPriceParams, - UpdatePriceParams, - }, - state::{ - PythAccount, - PythAccountType, - }, + instructions::{AddPriceParams, UpdatePriceParams}, + state::{PythAccount, PythAccountType}, }, anchor_lang::prelude::*, }; @@ -15,12 +9,12 @@ use { #[account(zero_copy)] #[derive(InitSpace)] pub struct PriceAccount { - pub id: u64, - pub price: u64, + pub id: u64, + pub price: u64, pub price_expo: u64, - pub ema: u64, - pub ema_expo: u64, - pub comp_: [Pubkey; 32], + pub ema: u64, + pub ema_expo: u64, + pub comp_: [Pubkey; 32], } impl PriceAccount { diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/mod.rs b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/mod.rs index 93361e14a4..b6df349384 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/mod.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/mod.rs @@ -5,10 +5,7 @@ pub use { solana_program_test::*, solana_sdk::{ pubkey::Pubkey, - signature::{ - Keypair, - Signer, - }, + signature::{Keypair, Signer}, }, }; diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_create_buffer.rs b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_create_buffer.rs index 374ec37dea..89540d1582 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_create_buffer.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_create_buffer.rs @@ -14,13 +14,11 @@ async fn test_create_buffer() { .await .unwrap(); - let msg_buffer_account_data = context .fetch_msg_buffer_account_data(&msg_buffer_pda) .await .unwrap(); - assert_eq!(msg_buffer_account_data.len(), space as usize); let (bump, _version, _header_len, end_offsets) = @@ -75,7 +73,6 @@ async fn create_buffer_with_invalid_size_should_fail() { .await .unwrap(); - let pyth_price_acct = MessageBufferTestContext::get_mock_pyth_price_account( MessageBufferTestContext::DEFAULT_TEST_PRICE_ID, ); diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_delete_buffer.rs b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_delete_buffer.rs index 367ed52c11..6e56879025 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_delete_buffer.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_delete_buffer.rs @@ -54,7 +54,6 @@ async fn delete_buffer_after_update_allowed_programs() { .await .unwrap(); - let payer = context.payer.pubkey(); let (msg_buffer_pda, _) = MessageBufferTestContext::default_msg_buffer(); diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_put_all.rs b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_put_all.rs index face68b1ff..390357fffa 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_put_all.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_put_all.rs @@ -28,7 +28,6 @@ async fn test_put_all() { .await .unwrap(); - let (bump, _version, header_len, end_offsets) = deserialize_msg_buffer_header(&msg_buffer_account_data); diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_resize_buffer.rs b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_resize_buffer.rs index 96ac479e9b..ef3b9875de 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_resize_buffer.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_resize_buffer.rs @@ -11,7 +11,6 @@ async fn test_resize_buffer() { let (msg_buffer_pda, _) = MessageBufferTestContext::default_msg_buffer(); - // increase buffer size let mut target_size = MessageBufferTestContext::DEFAULT_TARGET_SIZE + 10240; let target_sizes = vec![target_size]; @@ -23,13 +22,11 @@ async fn test_resize_buffer() { .await .unwrap(); - let msg_buffer_account_data = context .fetch_msg_buffer_account_data(&msg_buffer_pda) .await .unwrap(); - assert_eq!(msg_buffer_account_data.len(), target_size as usize); // decrease buffer size to less than original @@ -43,13 +40,11 @@ async fn test_resize_buffer() { .await .unwrap(); - let msg_buffer_account_data = context .fetch_msg_buffer_account_data(&msg_buffer_pda) .await .unwrap(); - assert_eq!(msg_buffer_account_data.len(), target_size as usize); } @@ -64,7 +59,6 @@ async fn test_multiple_resize_buffer_ixs_in_same_txn() { let (msg_buffer_pda, _) = MessageBufferTestContext::default_msg_buffer(); - // increase buffer size let mut target_size = MessageBufferTestContext::DEFAULT_TARGET_SIZE + 10240; let mut target_sizes = vec![]; @@ -79,13 +73,11 @@ async fn test_multiple_resize_buffer_ixs_in_same_txn() { .await .unwrap(); - let msg_buffer_account_data = context .fetch_msg_buffer_account_data(&msg_buffer_pda) .await .unwrap(); - assert_eq!(msg_buffer_account_data.len(), target_size as usize); } @@ -105,7 +97,6 @@ async fn fail_resize_buffer_invalid_increase() { let pyth_price_acct = MessageBufferTestContext::default_pyth_price_account(); let (msg_buffer_pda, msg_buffer_bump) = MessageBufferTestContext::default_msg_buffer(); - // increase buffer size beyond maximum allowed let target_size = MessageBufferTestContext::DEFAULT_TARGET_SIZE + 10240 + 100; @@ -129,13 +120,11 @@ async fn fail_resize_buffer_invalid_increase() { ProgramError::Custom(anchor_lang::error::ErrorCode::AccountReallocExceedsLimit.into()) ); - let msg_buffer_account_data = context .fetch_msg_buffer_account_data(&msg_buffer_pda) .await .unwrap(); - assert_eq!( msg_buffer_account_data.len(), MessageBufferTestContext::DEFAULT_TARGET_SIZE as usize @@ -197,7 +186,6 @@ async fn test_resize_initialized_buffer() { .await .unwrap(); - let (bump, _version, header_len, end_offsets) = deserialize_msg_buffer_header(&msg_buffer_account_data); @@ -215,7 +203,6 @@ async fn test_resize_initialized_buffer() { let msgs = extract_msg_buffer_messages(header_len, end_offsets, &msg_buffer_account_data); validate_price_msgs(id, price, price_expo, ema, ema_expo, &msgs).unwrap(); - // increase buffer size should not edit the original data let target_size = MessageBufferTestContext::DEFAULT_TARGET_SIZE + 10240; let target_sizes = vec![target_size]; @@ -227,7 +214,6 @@ async fn test_resize_initialized_buffer() { .await .unwrap(); - let msg_buffer_account_data = context .fetch_msg_buffer_account_data(&msg_buffer_pda) .await @@ -273,24 +259,20 @@ async fn fail_resize_initialized_buffer() { .await .unwrap(); - let msg_buffer_account_data = context .fetch_msg_buffer_account_data(&msg_buffer_pda) .await .unwrap(); - let (_, _version, header_len, end_offsets) = deserialize_msg_buffer_header(&msg_buffer_account_data); let max_end_offset = end_offsets.iter().max().unwrap(); let min_size = header_len + max_end_offset; - // decrease buffer size to less than something that can fit the current messages let target_size = (min_size as u32) - 1; - let resize_ix = resize_msg_buffer_ix( cpi_caller_auth, pyth_price_acct, @@ -313,7 +295,6 @@ async fn fail_resize_initialized_buffer() { let target_size = (min_size as u32) + 1; - let resize_ix = resize_msg_buffer_ix( cpi_caller_auth, pyth_price_acct, @@ -350,7 +331,6 @@ async fn fail_resize_buffer_exceed_max_size() { let pyth_price_acct = MessageBufferTestContext::default_pyth_price_account(); let (msg_buffer_pda, _msg_buffer_bump) = MessageBufferTestContext::default_msg_buffer(); - // increase buffer size beyond maximum allowed let mut target_size = MessageBufferTestContext::DEFAULT_TARGET_SIZE + 10240; while target_size < u32::from(u16::MAX) { diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_set_allowed_programs.rs b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_set_allowed_programs.rs index 7786134b0e..3ed7f95e70 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_set_allowed_programs.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/tests/cases/test_set_allowed_programs.rs @@ -6,7 +6,6 @@ async fn test_set_allowed_programs() { let admin = Keypair::new(); context.initialize(&admin).await.unwrap(); - let mock_cpi_caller_auth = MessageBufferTestContext::get_mock_cpi_auth(); let allowed_programs = vec![mock_cpi_caller_auth]; context diff --git a/pythnet/message_buffer/programs/mock-cpi-caller/tests/program_test/mod.rs b/pythnet/message_buffer/programs/mock-cpi-caller/tests/program_test/mod.rs index 0d614a99a8..e4d8283c68 100644 --- a/pythnet/message_buffer/programs/mock-cpi-caller/tests/program_test/mod.rs +++ b/pythnet/message_buffer/programs/mock-cpi-caller/tests/program_test/mod.rs @@ -1,56 +1,30 @@ use { anchor_lang::{ - prelude::{ - ProgramError::Custom, - *, - }, + prelude::{ProgramError::Custom, *}, solana_program::{ hash::hashv, - instruction::{ - Instruction, - InstructionError, - }, + instruction::{Instruction, InstructionError}, }, Id, }, - byteorder::{ - BigEndian, - LittleEndian, - ReadBytesExt, - }, - message_buffer::instructions::{ - MESSAGE, - WHITELIST, - }, - solana_program_test::{ - BanksClientError, - ProgramTest, - ProgramTestContext, - }, + byteorder::{BigEndian, LittleEndian, ReadBytesExt}, + message_buffer::instructions::{MESSAGE, WHITELIST}, + solana_program_test::{BanksClientError, ProgramTest, ProgramTestContext}, solana_sdk::{ account::ReadableAccount, - signature::{ - Keypair, - Signer, - }, - transaction::{ - Transaction, - TransactionError, - }, + signature::{Keypair, Signer}, + transaction::{Transaction, TransactionError}, }, std::{ - io::{ - Cursor, - Read, - }, + io::{Cursor, Read}, str::FromStr, }, }; pub struct MessageBufferTestContext { - context: ProgramTestContext, + context: ProgramTestContext, pub payer: Keypair, - admin: Option, + admin: Option, whitelist: Option, } @@ -72,7 +46,6 @@ impl MessageBufferTestContext { solana_program_test=debug"; solana_logger::setup_with(log_filter); - let mut pt = ProgramTest::new("message_buffer", ::message_buffer::id(), None); pt.add_program("mock_cpi_caller", ::mock_cpi_caller::id(), None); if disable_loosen_cpi_limit { @@ -107,7 +80,6 @@ impl MessageBufferTestContext { Ok(context) } - pub async fn initialize_with_default_test_buffer( disable_loosen_cpi_limit: bool, target_size: u32, @@ -123,7 +95,6 @@ impl MessageBufferTestContext { Ok(context) } - pub async fn get_balance(&mut self, pubkey: Pubkey) -> u64 { self.context.banks_client.get_balance(pubkey).await.unwrap() } @@ -152,7 +123,6 @@ impl MessageBufferTestContext { vec![MessageBufferTestContext::get_mock_cpi_auth()] } - pub fn default_pyth_price_account() -> Pubkey { Self::get_mock_pyth_price_account(Self::DEFAULT_TEST_PRICE_ID) } @@ -172,7 +142,6 @@ impl MessageBufferTestContext { ) } - pub async fn process_ixs( &mut self, instructions: &[Instruction], @@ -241,7 +210,6 @@ impl MessageBufferTestContext { deserialize_whitelist(account_data) } - pub async fn set_allowed_programs(&mut self, allowed_programs: &Vec) -> Result<()> { let set_allowed_programs_ix = set_allowed_programs_ix(self.admin_pubkey(), self.whitelist(), allowed_programs); @@ -255,7 +223,6 @@ impl MessageBufferTestContext { Ok(()) } - pub async fn create_buffer(&mut self, id: u64, target_size: u32) -> Result<(Pubkey, u8)> { let pyth_price_account = Self::get_mock_pyth_price_account(id); let (msg_buffer_pda, msg_buffer_bump) = @@ -274,7 +241,6 @@ impl MessageBufferTestContext { self.process_ixs(&[create_msg_buffer_ix], vec![&admin]) .await?; - Ok((msg_buffer_pda, msg_buffer_bump)) } @@ -322,7 +288,6 @@ impl MessageBufferTestContext { let admin = self.admin.as_ref().unwrap().insecure_clone(); - for target_size in target_sizes { let resize_ix = resize_msg_buffer_ix( Self::get_mock_cpi_auth(), @@ -432,7 +397,6 @@ pub fn create_msg_buffer_ix( ) } - pub fn resize_msg_buffer_ix( cpi_caller_auth: Pubkey, pyth_price_acct: Pubkey, @@ -462,7 +426,6 @@ pub fn resize_msg_buffer_ix( ) } - pub fn delete_msg_buffer_ix( cpi_caller_auth: Pubkey, pyth_price_acct: Pubkey, @@ -518,7 +481,6 @@ type Version = u8; type HeaderLen = u16; type EndOffsets = [u16; 255]; - pub fn deserialize_msg_buffer_header( account_data: &[u8], ) -> (Bump, Version, HeaderLen, EndOffsets) { @@ -527,7 +489,6 @@ pub fn deserialize_msg_buffer_header( cursor.read_exact(discriminator).unwrap(); assert_eq!(discriminator, &sighash("account", "MessageBuffer")); - let msg_buffer_acct_bump = cursor.read_u8().unwrap(); let version = cursor.read_u8().unwrap(); let header_len = cursor.read_u16::().unwrap(); @@ -574,7 +535,6 @@ type PriceMsgSchema = u8; type PriceMsgVersion = u16; type PriceMsgSize = u32; - pub fn validate_price_msgs( id: u64, price: u64, @@ -639,7 +599,6 @@ pub fn deserialize_price_msg_header(msg: &[u8]) -> (PriceMsgSchema, PriceMsgVers (schema, version, size) } - pub fn find_msg_buffer_pda(cpi_caller_auth: Pubkey, pyth_price_acct: Pubkey) -> (Pubkey, u8) { Pubkey::find_program_address( &[ @@ -651,14 +610,12 @@ pub fn find_msg_buffer_pda(cpi_caller_auth: Pubkey, pyth_price_acct: Pubkey) -> ) } - pub fn deserialize_whitelist(account_data: &[u8]) -> Result<(u8, Pubkey, u32, Vec)> { let mut cursor = Cursor::new(account_data); let discriminator = &mut vec![0u8; 8]; cursor.read_exact(discriminator).unwrap(); assert_eq!(discriminator, &sighash("account", "Whitelist")); - let whitelist_acct_bump = cursor.read_u8().unwrap(); let admin_bytes = &mut vec![0u8; 32]; diff --git a/pythnet/pythnet_sdk/examples/generate_pyth_data.rs b/pythnet/pythnet_sdk/examples/generate_pyth_data.rs index 798528b6d3..60790319f9 100644 --- a/pythnet/pythnet_sdk/examples/generate_pyth_data.rs +++ b/pythnet/pythnet_sdk/examples/generate_pyth_data.rs @@ -6,10 +6,7 @@ use { serde_json::json, solana_client::rpc_client::RpcClient, solana_sdk::pubkey::Pubkey, - std::{ - io::Write, - str::FromStr, - }, + std::{io::Write, str::FromStr}, }; fn main() { diff --git a/pythnet/pythnet_sdk/src/accumulators/merkle.rs b/pythnet/pythnet_sdk/src/accumulators/merkle.rs index 054a0ff137..ee3dc0468f 100644 --- a/pythnet/pythnet_sdk/src/accumulators/merkle.rs +++ b/pythnet/pythnet_sdk/src/accumulators/merkle.rs @@ -3,19 +3,10 @@ use { crate::{ accumulators::Accumulator, - hashers::{ - keccak256::Keccak256, - Hasher, - }, - }, - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - serde::{ - Deserialize, - Serialize, + hashers::{keccak256::Keccak256, Hasher}, }, + borsh::{BorshDeserialize, BorshSerialize}, + serde::{Deserialize, Serialize}, }; // We need to discern between leaf and intermediate nodes to prevent trivial second pre-image @@ -176,7 +167,7 @@ impl MerkleTree { } Some(Self { - root: MerkleRoot::new(tree[1]), + root: MerkleRoot::new(tree[1]), nodes: tree, }) } @@ -244,25 +235,22 @@ mod test { use { super::*, proptest::prelude::*, - std::{ - collections::BTreeSet, - mem::size_of, - }, + std::{collections::BTreeSet, mem::size_of}, }; #[derive(Default, Clone, Debug, borsh::BorshSerialize)] struct PriceAccount { - pub id: u64, - pub price: u64, + pub id: u64, + pub price: u64, pub price_expo: u64, - pub ema: u64, - pub ema_expo: u64, + pub ema: u64, + pub ema_expo: u64, } #[derive(Default, Debug, borsh::BorshSerialize)] struct PriceOnly { pub price_expo: u64, - pub price: u64, + pub price: u64, pub id: u64, } @@ -270,8 +258,8 @@ mod test { impl From for PriceOnly { fn from(other: PriceAccount) -> Self { Self { - id: other.id, - price: other.price, + id: other.id, + price: other.price, price_expo: other.price_expo, } } @@ -280,7 +268,7 @@ mod test { #[derive(Debug)] struct MerkleTreeDataWrapper { pub accumulator: MerkleTree, - pub data: BTreeSet>, + pub data: BTreeSet>, } impl Arbitrary for MerkleTreeDataWrapper { @@ -330,11 +318,11 @@ mod test { // Create some random elements (converted to bytes). All accumulators store arbitrary bytes so // that we can target any account (or subset of accounts). let price_account_a = PriceAccount { - id: 1, - price: 100, + id: 1, + price: 100, price_expo: 2, - ema: 50, - ema_expo: 1, + ema: 50, + ema_expo: 1, }; let item_a = borsh::BorshSerialize::try_to_vec(&price_account_a).unwrap(); @@ -457,7 +445,7 @@ mod test { // falsely prove `A` was in the original tree by tricking the implementation into performing // H(a || b) at the leaf. let faulty_accumulator = MerkleTree:: { - root: accumulator.root, + root: accumulator.root, nodes: vec![ accumulator.nodes[0], accumulator.nodes[1], // Root Stays the Same diff --git a/pythnet/pythnet_sdk/src/accumulators/mul.rs b/pythnet/pythnet_sdk/src/accumulators/mul.rs index cd650e5c6b..6c6b7a5a2f 100644 --- a/pythnet/pythnet_sdk/src/accumulators/mul.rs +++ b/pythnet/pythnet_sdk/src/accumulators/mul.rs @@ -2,10 +2,7 @@ use crate::{ accumulators::Accumulator, - hashers::{ - prime::PrimeHasher, - Hasher, - }, + hashers::{prime::PrimeHasher, Hasher}, }; /// A multiplication based Accumulator @@ -17,7 +14,7 @@ use crate::{ /// proved to be a member. pub struct MulAccumulator { pub accumulator: H::Hash, - pub items: Vec, + pub items: Vec, } impl<'a> Accumulator<'a> for MulAccumulator { @@ -38,7 +35,7 @@ impl<'a> Accumulator<'a> for MulAccumulator { fn from_set(items: impl Iterator) -> Option { let primes: Vec<[u8; 16]> = items.map(|i| PrimeHasher::hashv(&[i])).collect(); Some(Self { - items: primes.clone(), + items: primes.clone(), accumulator: primes.into_iter().reduce(|acc, v| { u128::to_be_bytes(u128::from_be_bytes(acc) * u128::from_be_bytes(v)) })?, @@ -48,10 +45,7 @@ impl<'a> Accumulator<'a> for MulAccumulator { #[cfg(test)] mod test { - use { - super::*, - std::collections::HashSet, - }; + use {super::*, std::collections::HashSet}; #[test] fn test_membership() { diff --git a/pythnet/pythnet_sdk/src/hashers.rs b/pythnet/pythnet_sdk/src/hashers.rs index ffc22998d6..8264aeccfc 100644 --- a/pythnet/pythnet_sdk/src/hashers.rs +++ b/pythnet/pythnet_sdk/src/hashers.rs @@ -1,12 +1,6 @@ use { - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - serde::{ - Deserialize, - Serialize, - }, + borsh::{BorshDeserialize, BorshSerialize}, + serde::{Deserialize, Serialize}, std::fmt::Debug, }; diff --git a/pythnet/pythnet_sdk/src/hashers/keccak256.rs b/pythnet/pythnet_sdk/src/hashers/keccak256.rs index a7a2b0a866..48a4449259 100644 --- a/pythnet/pythnet_sdk/src/hashers/keccak256.rs +++ b/pythnet/pythnet_sdk/src/hashers/keccak256.rs @@ -1,10 +1,7 @@ use { crate::hashers::Hasher, serde::Serialize, - sha3::{ - Digest, - Keccak256 as Keccak256Digest, - }, + sha3::{Digest, Keccak256 as Keccak256Digest}, }; #[derive(Clone, Default, Debug, Eq, PartialEq, Serialize)] @@ -22,10 +19,7 @@ impl Hasher for Keccak256 { #[cfg(test)] mod tests { - use { - super::*, - crate::hashers::Hasher, - }; + use {super::*, crate::hashers::Hasher}; #[test] fn test_keccak256() { diff --git a/pythnet/pythnet_sdk/src/hashers/keccak256_160.rs b/pythnet/pythnet_sdk/src/hashers/keccak256_160.rs index 2ef08ea0a7..20efde9944 100644 --- a/pythnet/pythnet_sdk/src/hashers/keccak256_160.rs +++ b/pythnet/pythnet_sdk/src/hashers/keccak256_160.rs @@ -1,15 +1,8 @@ #[cfg(not(feature = "solana-program"))] -use sha3::{ - Digest, - Keccak256, -}; +use sha3::{Digest, Keccak256}; #[cfg(feature = "solana-program")] use solana_program::keccak::hashv; -use { - crate::hashers::Hasher, - serde::Serialize, -}; - +use {crate::hashers::Hasher, serde::Serialize}; #[derive(Clone, Default, Debug, Eq, Hash, PartialEq, Serialize)] pub struct Keccak160 {} diff --git a/pythnet/pythnet_sdk/src/hashers/prime.rs b/pythnet/pythnet_sdk/src/hashers/prime.rs index e0a016ce9b..ca643c7a97 100644 --- a/pythnet/pythnet_sdk/src/hashers/prime.rs +++ b/pythnet/pythnet_sdk/src/hashers/prime.rs @@ -1,9 +1,4 @@ -use { - crate::hashers::Hasher, - serde::Serialize, - sha3::Digest, - slow_primes::is_prime_miller_rabin, -}; +use {crate::hashers::Hasher, serde::Serialize, sha3::Digest, slow_primes::is_prime_miller_rabin}; #[derive(Clone, Default, Debug, Eq, PartialEq, Serialize)] pub struct PrimeHasher {} diff --git a/pythnet/pythnet_sdk/src/lib.rs b/pythnet/pythnet_sdk/src/lib.rs index 2c3632c6b9..08d22b37dc 100644 --- a/pythnet/pythnet_sdk/src/lib.rs +++ b/pythnet/pythnet_sdk/src/lib.rs @@ -55,10 +55,7 @@ pub(crate) mod tests { #[test] fn test_pubkeys() { - use solana_sdk::{ - pubkey, - pubkey::Pubkey, - }; + use solana_sdk::{pubkey, pubkey::Pubkey}; let accumulator_emitter_address = pubkey!("G9LV2mp9ua1znRAfYwZz5cPiJMAbo1T6mbjdQsDZuMJg"); assert_eq!( diff --git a/pythnet/pythnet_sdk/src/messages.rs b/pythnet/pythnet_sdk/src/messages.rs index 4444bb2717..12b8a18021 100644 --- a/pythnet/pythnet_sdk/src/messages.rs +++ b/pythnet/pythnet_sdk/src/messages.rs @@ -1,22 +1,13 @@ #[cfg(feature = "solana-program")] -use anchor_lang::{ - AnchorDeserialize, - AnchorSerialize, -}; +use anchor_lang::{AnchorDeserialize, AnchorSerialize}; #[cfg(not(feature = "solana-program"))] -use borsh::{ - BorshDeserialize, - BorshSerialize, -}; +use borsh::{BorshDeserialize, BorshSerialize}; #[cfg(feature = "quickcheck")] use quickcheck::Arbitrary; use { crate::wire::PrefixedVec, borsh::BorshSchema, - serde::{ - Deserialize, - Serialize, - }, + serde::{Deserialize, Serialize}, }; /// Message format for sending data to other chains via the accumulator program @@ -100,12 +91,12 @@ pub type Pubkey = [u8; 32]; )] pub struct PriceFeedMessage { - pub feed_id: FeedId, - pub price: i64, - pub conf: u64, - pub exponent: i32, + pub feed_id: FeedId, + pub price: i64, + pub conf: u64, + pub exponent: i32, /// The timestamp of this price update in seconds - pub publish_time: i64, + pub publish_time: i64, /// The timestamp of the previous price update. This field is intended to allow users to /// identify the single unique price update for any moment in time: /// for any time t, the unique update is the one such that prev_publish_time < t <= publish_time. @@ -119,8 +110,8 @@ pub struct PriceFeedMessage { /// where the aggregation was unsuccesful. This problem will go away once all publishers have /// migrated over to a recent version of pyth-agent. pub prev_publish_time: i64, - pub ema_price: i64, - pub ema_conf: u64, + pub ema_price: i64, + pub ema_conf: u64, } #[cfg(feature = "quickcheck")] @@ -150,14 +141,14 @@ impl Arbitrary for PriceFeedMessage { #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub struct TwapMessage { - pub feed_id: FeedId, - pub cumulative_price: i128, - pub cumulative_conf: u128, - pub num_down_slots: u64, - pub exponent: i32, - pub publish_time: i64, + pub feed_id: FeedId, + pub cumulative_price: i128, + pub cumulative_conf: u128, + pub num_down_slots: u64, + pub exponent: i32, + pub publish_time: i64, pub prev_publish_time: i64, - pub publish_slot: u64, + pub publish_slot: u64, } #[cfg(feature = "quickcheck")] @@ -187,14 +178,14 @@ impl Arbitrary for TwapMessage { #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct PublisherStakeCapsMessage { pub publish_time: i64, - pub caps: PrefixedVec, // PrefixedVec because we might have more than 256 publishers + pub caps: PrefixedVec, // PrefixedVec because we might have more than 256 publishers } #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] pub struct PublisherStakeCap { pub publisher: Pubkey, - pub cap: u64, + pub cap: u64, } #[cfg(feature = "quickcheck")] @@ -203,7 +194,7 @@ impl Arbitrary for PublisherStakeCapsMessage { let caps = Vec::arbitrary(g); PublisherStakeCapsMessage { publish_time: i64::arbitrary(g), - caps: caps.into(), + caps: caps.into(), } } } @@ -219,7 +210,7 @@ impl Arbitrary for PublisherStakeCap { } publisher }, - cap: u64::arbitrary(g), + cap: u64::arbitrary(g), } } } @@ -228,29 +219,23 @@ impl Arbitrary for PublisherStakeCap { mod tests { use crate::{ - messages::{ - Message, - PriceFeedMessage, - }, + messages::{Message, PriceFeedMessage}, wire::Serializer, }; // Test if additional payload to the end of a message is forward compatible #[test] fn test_forward_compatibility() { - use { - serde::Serialize, - std::iter, - }; + use {serde::Serialize, std::iter}; let msg = Message::PriceFeedMessage(PriceFeedMessage { - feed_id: [1u8; 32], - price: 1, - conf: 1, - exponent: 1, - publish_time: 1, + feed_id: [1u8; 32], + price: 1, + conf: 1, + exponent: 1, + publish_time: 1, prev_publish_time: 1, - ema_price: 1, - ema_conf: 1, + ema_price: 1, + ema_conf: 1, }); let mut buffer = Vec::new(); let mut cursor = std::io::Cursor::new(&mut buffer); diff --git a/pythnet/pythnet_sdk/src/test_utils/mod.rs b/pythnet/pythnet_sdk/src/test_utils/mod.rs index c0af8497da..ec5328f6f3 100644 --- a/pythnet/pythnet_sdk/src/test_utils/mod.rs +++ b/pythnet/pythnet_sdk/src/test_utils/mod.rs @@ -1,85 +1,55 @@ use { crate::{ - accumulators::{ - merkle::MerkleTree, - Accumulator, - }, - hashers::{ - keccak256::Keccak256, - keccak256_160::Keccak160, - Hasher, - }, - messages::{ - FeedId, - Message, - PriceFeedMessage, - TwapMessage, - }, + accumulators::{merkle::MerkleTree, Accumulator}, + hashers::{keccak256::Keccak256, keccak256_160::Keccak160, Hasher}, + messages::{FeedId, Message, PriceFeedMessage, TwapMessage}, wire::{ to_vec, v1::{ - AccumulatorUpdateData, - MerklePriceUpdate, - Proof, - WormholeMerkleRoot, - WormholeMessage, - WormholePayload, + AccumulatorUpdateData, MerklePriceUpdate, Proof, WormholeMerkleRoot, + WormholeMessage, WormholePayload, }, PrefixedVec, }, }, byteorder::BigEndian, - libsecp256k1::{ - Message as libsecp256k1Message, - PublicKey, - RecoveryId, - SecretKey, - Signature, - }, - rand::{ - seq::SliceRandom, - thread_rng, - }, + libsecp256k1::{Message as libsecp256k1Message, PublicKey, RecoveryId, SecretKey, Signature}, + rand::{seq::SliceRandom, thread_rng}, serde_wormhole::RawMessage, wormhole_sdk::{ - vaa::{ - Body, - Header, - }, - Address, - Chain, - Vaa, + vaa::{Body, Header}, + Address, Chain, Vaa, }, }; pub struct DataSource { pub address: Address, - pub chain: Chain, + pub chain: Chain, } pub const DEFAULT_DATA_SOURCE: DataSource = DataSource { address: Address([1u8; 32]), - chain: Chain::Solana, + chain: Chain::Solana, }; pub const DEFAULT_GOVERNANCE_SOURCE: DataSource = DataSource { address: Address([2u8; 32]), - chain: Chain::Ethereum, + chain: Chain::Ethereum, }; pub const WRONG_SOURCE: DataSource = DataSource { address: Address([3u8; 32]), - chain: Chain::Bsc, + chain: Chain::Bsc, }; pub const SECONDARY_DATA_SOURCE: DataSource = DataSource { address: Address([4u8; 32]), - chain: Chain::Polygon, + chain: Chain::Polygon, }; pub const SECONDARY_GOVERNANCE_SOURCE: DataSource = DataSource { address: Address([5u8; 32]), - chain: Chain::Avalanche, + chain: Chain::Avalanche, }; pub const DEFAULT_CHAIN_ID: Chain = Chain::Oasis; @@ -140,14 +110,14 @@ pub fn create_dummy_price_feed_message(value: i64) -> Message { pub fn create_dummy_twap_message() -> Message { let msg = TwapMessage { - feed_id: [0; 32], - cumulative_price: 0, - cumulative_conf: 0, - num_down_slots: 0, - exponent: 0, - publish_time: 0, + feed_id: [0; 32], + cumulative_price: 0, + cumulative_conf: 0, + num_down_slots: 0, + exponent: 0, + publish_time: 0, prev_publish_time: 0, - publish_slot: 0, + publish_slot: 0, }; Message::TwapMessage(msg) } @@ -217,9 +187,9 @@ pub fn create_accumulator_message_from_updates( let mut root_hash = [0u8; 20]; root_hash.copy_from_slice(&to_vec::<_, BigEndian>(&tree.root).unwrap()[..20]); let wormhole_message = WormholeMessage::new(WormholePayload::Merkle(WormholeMerkleRoot { - slot: 0, + slot: 0, ring_size: 0, - root: root_hash, + root: root_hash, })); let mut vaa_payload = to_vec::<_, BigEndian>(&wormhole_message).unwrap(); @@ -235,7 +205,7 @@ pub fn create_accumulator_message_from_updates( ); let accumulator_update_data = AccumulatorUpdateData::new(Proof::WormholeMerkle { - vaa: PrefixedVec::from(serde_wormhole::to_vec(&vaa).unwrap()), + vaa: PrefixedVec::from(serde_wormhole::to_vec(&vaa).unwrap()), updates: price_updates, }); diff --git a/pythnet/pythnet_sdk/src/wire.rs b/pythnet/pythnet_sdk/src/wire.rs index 64a4eee6f5..b4236f4ad0 100644 --- a/pythnet/pythnet_sdk/src/wire.rs +++ b/pythnet/pythnet_sdk/src/wire.rs @@ -12,18 +12,9 @@ mod prefixed_vec; mod ser; pub use { - de::{ - from_slice, - Deserializer, - DeserializerError, - }, + de::{from_slice, Deserializer, DeserializerError}, prefixed_vec::PrefixedVec, - ser::{ - to_vec, - to_writer, - Serializer, - SerializerError, - }, + ser::{to_vec, to_writer, Serializer, SerializerError}, }; // Proof Format (V1) @@ -35,19 +26,11 @@ pub mod v1 { use { super::*, crate::{ - accumulators::merkle::MerklePath, - error::Error, - hashers::keccak256_160::Keccak160, + accumulators::merkle::MerklePath, error::Error, hashers::keccak256_160::Keccak160, require, }, - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - serde::{ - Deserialize, - Serialize, - }, + borsh::{BorshDeserialize, BorshSerialize}, + serde::{Deserialize, Serialize}, }; pub const PYTHNET_ACCUMULATOR_UPDATE_MAGIC: &[u8; 4] = b"PNAU"; pub const CURRENT_MINOR_VERSION: u8 = 0; @@ -58,11 +41,11 @@ pub mod v1 { // to target chains). #[derive(Clone, Debug, Hash, PartialEq, Serialize, Deserialize)] pub struct AccumulatorUpdateData { - magic: [u8; 4], + magic: [u8; 4], major_version: u8, minor_version: u8, - trailing: Vec, - pub proof: Proof, + trailing: Vec, + pub proof: Proof, } impl AccumulatorUpdateData { @@ -101,7 +84,7 @@ pub mod v1 { #[derive(Clone, Debug, Hash, PartialEq, Serialize, Deserialize)] pub enum Proof { WormholeMerkle { - vaa: PrefixedVec, + vaa: PrefixedVec, updates: Vec, }, } @@ -111,12 +94,12 @@ pub mod v1 { )] pub struct MerklePriceUpdate { pub message: PrefixedVec, - pub proof: MerklePath, + pub proof: MerklePath, } #[derive(Clone, Debug, Hash, PartialEq, Serialize, Deserialize)] pub struct WormholeMessage { - pub magic: [u8; 4], + pub magic: [u8; 4], pub payload: WormholePayload, } @@ -148,9 +131,9 @@ pub mod v1 { #[derive(Clone, Debug, Hash, PartialEq, Serialize, Deserialize)] pub struct WormholeMerkleRoot { - pub slot: u64, + pub slot: u64, pub ring_size: u32, - pub root: Hash, + pub root: Hash, } } @@ -158,13 +141,8 @@ pub mod v1 { mod tests { use crate::wire::{ array, - v1::{ - AccumulatorUpdateData, - Proof, - }, - Deserializer, - PrefixedVec, - Serializer, + v1::{AccumulatorUpdateData, Proof}, + Deserializer, PrefixedVec, Serializer, }; // Test the arbitrary fixed sized array serialization implementation. @@ -233,41 +211,41 @@ mod tests { t_bool: bool, // Test integer serializations. - t_u8: u8, + t_u8: u8, t_u16: u16, t_u32: u32, t_u64: u64, // Test `str` is serialized to a variable length array. t_string: String, - t_str: &'a str, + t_str: &'a str, // Test `Vec` is serialized to a variable length array. - t_vec: Vec, - t_vec_empty: Vec, - t_vec_nested: Vec>, + t_vec: Vec, + t_vec_empty: Vec, + t_vec_nested: Vec>, t_vec_nested_empty: Vec>, - t_slice: &'a [u8], - t_slice_empty: &'a [u8], + t_slice: &'a [u8], + t_slice_empty: &'a [u8], // Test tuples serialize as expected. - t_tuple: (u8, u16, u32, u64, String, Vec, &'a [u8]), + t_tuple: (u8, u16, u32, u64, String, Vec, &'a [u8]), t_tuple_nested: ((u8, u16), (u32, u64)), // Test enum serializations. - t_enum_unit: GoldenEnum, + t_enum_unit: GoldenEnum, t_enum_newtype: GoldenEnum, - t_enum_tuple: GoldenEnum, - t_enum_struct: GoldenEnum, + t_enum_tuple: GoldenEnum, + t_enum_struct: GoldenEnum, // Test nested structs, which includes our PrefixedVec implementations work as we expect. - t_struct: GoldenNested, + t_struct: GoldenNested, t_prefixed: PrefixedVec, } #[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)] struct GoldenNested { - nested_u8: T, + nested_u8: T, nested_tuple: (u8, u8), } @@ -281,21 +259,21 @@ mod tests { // Serialize the golden test value. let golden_struct = GoldenStruct { - unit: (), - t_bool: true, - t_u8: 1, - t_u16: 2, - t_u32: 3, - t_u64: 4, - t_string: "9".to_string(), - t_str: "10", - t_vec: vec![11, 12, 13], - t_vec_empty: vec![], - t_vec_nested: vec![vec![14, 15, 16], vec![17, 18, 19]], + unit: (), + t_bool: true, + t_u8: 1, + t_u16: 2, + t_u32: 3, + t_u64: 4, + t_string: "9".to_string(), + t_str: "10", + t_vec: vec![11, 12, 13], + t_vec_empty: vec![], + t_vec_nested: vec![vec![14, 15, 16], vec![17, 18, 19]], t_vec_nested_empty: vec![vec![], vec![]], - t_slice: &[20, 21, 22], - t_slice_empty: &[], - t_tuple: ( + t_slice: &[20, 21, 22], + t_slice_empty: &[], + t_tuple: ( 29, 30, 31, @@ -304,16 +282,16 @@ mod tests { vec![35, 36, 37], &[38, 39, 40], ), - t_tuple_nested: ((41, 42), (43, 44)), - t_enum_unit: GoldenEnum::Unit, - t_enum_newtype: GoldenEnum::Newtype(45), - t_enum_tuple: GoldenEnum::Tuple(46, 47), - t_enum_struct: GoldenEnum::Struct { a: 48, b: 49 }, - t_struct: GoldenNested { - nested_u8: 50, + t_tuple_nested: ((41, 42), (43, 44)), + t_enum_unit: GoldenEnum::Unit, + t_enum_newtype: GoldenEnum::Newtype(45), + t_enum_tuple: GoldenEnum::Tuple(46, 47), + t_enum_struct: GoldenEnum::Struct { a: 48, b: 49 }, + t_struct: GoldenNested { + nested_u8: 50, nested_tuple: (51, 52), }, - t_prefixed: vec![0u8; 512].into(), + t_prefixed: vec![0u8; 512].into(), }; golden_struct.serialize(&mut serializer).unwrap(); @@ -504,7 +482,7 @@ mod tests { use serde::Serialize; // Serialize an empty update into a buffer. let empty_update = AccumulatorUpdateData::new(Proof::WormholeMerkle { - vaa: PrefixedVec::from(vec![]), + vaa: PrefixedVec::from(vec![]), updates: vec![], }); let mut buffer = Vec::new(); @@ -526,7 +504,7 @@ mod tests { // Serialize an empty update into a buffer. let empty_update = AccumulatorUpdateData::new(Proof::WormholeMerkle { - vaa: PrefixedVec::from(vec![]), + vaa: PrefixedVec::from(vec![]), updates: vec![], }); let mut buffer = Vec::new(); diff --git a/pythnet/pythnet_sdk/src/wire/array.rs b/pythnet/pythnet_sdk/src/wire/array.rs index 476e8d4275..2485c6a409 100644 --- a/pythnet/pythnet_sdk/src/wire/array.rs +++ b/pythnet/pythnet_sdk/src/wire/array.rs @@ -12,11 +12,7 @@ //! } //! ``` use { - serde::{ - Deserialize, - Serialize, - Serializer, - }, + serde::{Deserialize, Serialize, Serializer}, std::mem::MaybeUninit, }; diff --git a/pythnet/pythnet_sdk/src/wire/de.rs b/pythnet/pythnet_sdk/src/wire/de.rs index 8417ec8621..af952efa29 100644 --- a/pythnet/pythnet_sdk/src/wire/de.rs +++ b/pythnet/pythnet_sdk/src/wire/de.rs @@ -24,25 +24,13 @@ use { crate::require, - byteorder::{ - ByteOrder, - ReadBytesExt, - }, + byteorder::{ByteOrder, ReadBytesExt}, serde::{ - de::{ - EnumAccess, - MapAccess, - SeqAccess, - VariantAccess, - }, + de::{EnumAccess, MapAccess, SeqAccess, VariantAccess}, Deserialize, }, std::{ - io::{ - Cursor, - Seek, - SeekFrom, - }, + io::{Cursor, Seek, SeekFrom}, mem::size_of, }, thiserror::Error, @@ -481,7 +469,7 @@ impl<'de, 'a, B: ByteOrder> VariantAccess<'de> for &'a mut Deserializer<'de, B> } struct SequenceIterator<'de, 'a, B: ByteOrder> { - de: &'a mut Deserializer<'de, B>, + de: &'a mut Deserializer<'de, B>, len: usize, } @@ -539,7 +527,7 @@ impl<'de, 'a, B: ByteOrder> MapAccess<'de> for SequenceIterator<'de, 'a, B> { } struct Enum<'de, 'a, B: ByteOrder> { - de: &'a mut Deserializer<'de, B>, + de: &'a mut Deserializer<'de, B>, variant: u8, } diff --git a/pythnet/pythnet_sdk/src/wire/prefixed_vec.rs b/pythnet/pythnet_sdk/src/wire/prefixed_vec.rs index 9459a13735..761594e9f6 100644 --- a/pythnet/pythnet_sdk/src/wire/prefixed_vec.rs +++ b/pythnet/pythnet_sdk/src/wire/prefixed_vec.rs @@ -1,16 +1,9 @@ use { - borsh::{ - BorshDeserialize, - BorshSerialize, - }, + borsh::{BorshDeserialize, BorshSerialize}, serde::{ de::DeserializeSeed, - ser::{ - SerializeSeq, - SerializeStruct, - }, - Deserialize, - Serialize, + ser::{SerializeSeq, SerializeStruct}, + Deserialize, Serialize, }, }; @@ -36,7 +29,7 @@ where struct PrefixlessSeed { __phantom: std::marker::PhantomData, - len: usize, + len: usize, } /// We implement DeserializeSeed for PrefixlessSeed which is aware of the len that should be read @@ -54,7 +47,7 @@ where deserializer: D, ) -> Result { struct PrefixlessVecVisitor { - len: usize, + len: usize, __phantom: std::marker::PhantomData, } @@ -88,7 +81,7 @@ where deserializer.deserialize_tuple( self.len, PrefixlessVecVisitor { - len: self.len, + len: self.len, __phantom: std::marker::PhantomData, }, ) @@ -108,14 +101,14 @@ where #[derive(Clone, Debug, Hash, PartialEq, PartialOrd, BorshDeserialize, BorshSerialize)] pub struct PrefixedVec { __phantom: std::marker::PhantomData, - data: PrefixlessVec, + data: PrefixlessVec, } impl From> for PrefixedVec { fn from(data: Vec) -> Self { Self { __phantom: std::marker::PhantomData, - data: PrefixlessVec { inner: data }, + data: PrefixlessVec { inner: data }, } } } diff --git a/pythnet/pythnet_sdk/src/wire/ser.rs b/pythnet/pythnet_sdk/src/wire/ser.rs index 3c52857290..e78f68c5ac 100644 --- a/pythnet/pythnet_sdk/src/wire/ser.rs +++ b/pythnet/pythnet_sdk/src/wire/ser.rs @@ -142,26 +142,15 @@ //! ``` use { - byteorder::{ - ByteOrder, - WriteBytesExt, - }, + byteorder::{ByteOrder, WriteBytesExt}, serde::{ ser::{ - SerializeMap, - SerializeSeq, - SerializeStruct, - SerializeStructVariant, - SerializeTuple, - SerializeTupleStruct, - SerializeTupleVariant, + SerializeMap, SerializeSeq, SerializeStruct, SerializeStructVariant, SerializeTuple, + SerializeTupleStruct, SerializeTupleVariant, }, Serialize, }, - std::{ - fmt::Display, - io::Write, - }, + std::{fmt::Display, io::Write}, thiserror::Error, }; @@ -210,7 +199,7 @@ pub enum SerializerError { /// we serialize in both big and little endian depending on different use-cases. #[derive(Clone)] pub struct Serializer { - writer: W, + writer: W, _endian: std::marker::PhantomData, } diff --git a/pythnet/pythnet_sdk/src/wormhole.rs b/pythnet/pythnet_sdk/src/wormhole.rs index e7418cfefc..e52401d888 100644 --- a/pythnet/pythnet_sdk/src/wormhole.rs +++ b/pythnet/pythnet_sdk/src/wormhole.rs @@ -5,24 +5,11 @@ //! allows us to emit and parse messages through Wormhole. use { crate::Pubkey, - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - serde::{ - Deserialize, - Serialize, - }, + borsh::{BorshDeserialize, BorshSerialize}, + serde::{Deserialize, Serialize}, std::{ - io::{ - Error, - ErrorKind::InvalidData, - Write, - }, - ops::{ - Deref, - DerefMut, - }, + io::{Error, ErrorKind::InvalidData, Write}, + ops::{Deref, DerefMut}, }, }; @@ -36,16 +23,16 @@ pub struct PostedMessageUnreliableData { Debug, Default, BorshSerialize, BorshDeserialize, Clone, Serialize, Deserialize, PartialEq, )] pub struct MessageData { - pub vaa_version: u8, - pub consistency_level: u8, - pub vaa_time: u32, + pub vaa_version: u8, + pub consistency_level: u8, + pub vaa_time: u32, pub vaa_signature_account: Pubkey, - pub submission_time: u32, - pub nonce: u32, - pub sequence: u64, - pub emitter_chain: u16, - pub emitter_address: [u8; 32], - pub payload: Vec, + pub submission_time: u32, + pub nonce: u32, + pub sequence: u64, + pub emitter_chain: u16, + pub emitter_address: [u8; 32], + pub payload: Vec, } impl BorshSerialize for PostedMessageUnreliableData { @@ -103,20 +90,19 @@ pub struct AccumulatorSequenceTracker { fn test_borsh_roundtrip() { let post_message_unreliable_data = PostedMessageUnreliableData { message: MessageData { - vaa_version: 1, - consistency_level: 2, - vaa_time: 3, + vaa_version: 1, + consistency_level: 2, + vaa_time: 3, vaa_signature_account: [4u8; 32], - submission_time: 5, - nonce: 6, - sequence: 7, - emitter_chain: 8, - emitter_address: [9u8; 32], - payload: vec![10u8; 32], + submission_time: 5, + nonce: 6, + sequence: 7, + emitter_chain: 8, + emitter_address: [9u8; 32], + payload: vec![10u8; 32], }, }; - let encoded = borsh::to_vec(&post_message_unreliable_data).unwrap(); let decoded = PostedMessageUnreliableData::try_from_slice(encoded.as_slice()).unwrap(); diff --git a/pythnet/stake_caps_parameters/cli/src/cli.rs b/pythnet/stake_caps_parameters/cli/src/cli.rs index 69e1441de8..a1b724faf3 100644 --- a/pythnet/stake_caps_parameters/cli/src/cli.rs +++ b/pythnet/stake_caps_parameters/cli/src/cli.rs @@ -3,10 +3,7 @@ use { clap::Parser, solana_sdk::{ pubkey::Pubkey, - signature::{ - read_keypair_file, - Keypair, - }, + signature::{read_keypair_file, Keypair}, }, }; @@ -17,18 +14,18 @@ use { )] pub struct Cli { #[clap(long, default_value = "https://pythnet.rpcpool.com/")] - pub rpc_url: String, + pub rpc_url: String, #[clap( long, default_value = "~/.config/solana/id.json", help = "Keypair file the funder of the transaction", parse(try_from_str = get_keypair_from_file) )] - pub keypair: Keypair, + pub keypair: Keypair, #[clap(long, help = "M parameter")] - pub m: u64, + pub m: u64, #[clap(long, help = "Z parameter")] - pub z: u64, + pub z: u64, #[clap(long, help = "Update authority for the parameters")] pub authority: Pubkey, } diff --git a/pythnet/stake_caps_parameters/cli/src/main.rs b/pythnet/stake_caps_parameters/cli/src/main.rs index ba7b25f062..394a898a87 100644 --- a/pythnet/stake_caps_parameters/cli/src/main.rs +++ b/pythnet/stake_caps_parameters/cli/src/main.rs @@ -1,25 +1,16 @@ pub mod cli; use { - anchor_lang::{ - InstructionData, - ToAccountMetas, - }, + anchor_lang::{InstructionData, ToAccountMetas}, clap::Parser, cli::Cli, solana_client::rpc_client::RpcClient, solana_sdk::{ - commitment_config::CommitmentConfig, - instruction::Instruction, - signer::Signer, + commitment_config::CommitmentConfig, instruction::Instruction, signer::Signer, transaction::Transaction, }, - stake_caps_parameters::{ - Parameters, - PARAMETERS_ADDRESS, - }, + stake_caps_parameters::{Parameters, PARAMETERS_ADDRESS}, }; - fn main() { let Cli { keypair, @@ -29,16 +20,16 @@ fn main() { authority, } = Cli::parse(); let accs = stake_caps_parameters::accounts::SetParameters { - signer: keypair.pubkey(), - parameters: PARAMETERS_ADDRESS, + signer: keypair.pubkey(), + parameters: PARAMETERS_ADDRESS, system_program: solana_sdk::system_program::id(), }; let rpc_client = RpcClient::new_with_commitment(rpc_url, CommitmentConfig::confirmed()); let instruction = Instruction { program_id: stake_caps_parameters::id(), - accounts: accs.to_account_metas(None), - data: stake_caps_parameters::instruction::SetParameters { + accounts: accs.to_account_metas(None), + data: stake_caps_parameters::instruction::SetParameters { parameters: Parameters { m, z, @@ -48,7 +39,6 @@ fn main() { .data(), }; - let mut transaction = Transaction::new_with_payer(&[instruction], Some(&keypair.pubkey())); transaction.sign(&[&keypair], rpc_client.get_latest_blockhash().unwrap()); let transaction_signature = rpc_client diff --git a/pythnet/stake_caps_parameters/programs/stake_caps_parameters/src/lib.rs b/pythnet/stake_caps_parameters/programs/stake_caps_parameters/src/lib.rs index 5b83df021c..b6d93238b2 100644 --- a/pythnet/stake_caps_parameters/programs/stake_caps_parameters/src/lib.rs +++ b/pythnet/stake_caps_parameters/programs/stake_caps_parameters/src/lib.rs @@ -23,7 +23,7 @@ pub mod stake_caps_parameters { #[derive(Accounts)] pub struct SetParameters<'info> { #[account(mut)] - pub signer: Signer<'info>, + pub signer: Signer<'info>, #[account( init_if_needed, seeds = ["parameters".as_bytes()], @@ -31,17 +31,16 @@ pub struct SetParameters<'info> { payer = signer, space = Parameters::LEN )] - pub parameters: Account<'info, Parameters>, + pub parameters: Account<'info, Parameters>, pub system_program: Program<'info, System>, } - #[account] #[derive(PartialEq, Eq, Debug, Copy)] pub struct Parameters { pub current_authority: Pubkey, - pub m: u64, - pub z: u64, + pub m: u64, + pub z: u64, } impl Parameters { diff --git a/pythnet/stake_caps_parameters/programs/stake_caps_parameters/tests/test_stake_caps_parameters.rs b/pythnet/stake_caps_parameters/programs/stake_caps_parameters/tests/test_stake_caps_parameters.rs index ed3d795328..086812f15d 100644 --- a/pythnet/stake_caps_parameters/programs/stake_caps_parameters/tests/test_stake_caps_parameters.rs +++ b/pythnet/stake_caps_parameters/programs/stake_caps_parameters/tests/test_stake_caps_parameters.rs @@ -1,36 +1,19 @@ use { anchor_lang::{ - error::Error, - solana_program::native_token::LAMPORTS_PER_SOL, - AccountDeserialize, - InstructionData, - ToAccountMetas, - }, - litesvm::{ - types::TransactionResult, - LiteSVM, + error::Error, solana_program::native_token::LAMPORTS_PER_SOL, AccountDeserialize, + InstructionData, ToAccountMetas, }, + litesvm::{types::TransactionResult, LiteSVM}, solana_sdk::{ - instruction::{ - Instruction, - InstructionError, - }, + instruction::{Instruction, InstructionError}, program_error::ProgramError, signature::Keypair, signer::Signer, - transaction::{ - Transaction, - TransactionError, - }, - }, - stake_caps_parameters::{ - ErrorCode, - Parameters, - PARAMETERS_ADDRESS, + transaction::{Transaction, TransactionError}, }, + stake_caps_parameters::{ErrorCode, Parameters, PARAMETERS_ADDRESS}, }; - #[test] fn test_stake_caps_parameters() { let mut svm = litesvm::LiteSVM::new(); @@ -43,15 +26,13 @@ fn test_stake_caps_parameters() { ) .unwrap(); - svm.airdrop(&payer_1.pubkey(), LAMPORTS_PER_SOL).unwrap(); svm.airdrop(&payer_2.pubkey(), LAMPORTS_PER_SOL).unwrap(); - let parameter_1 = Parameters { current_authority: payer_1.pubkey(), - m: 1, - z: 2, + m: 1, + z: 2, }; assert!(set_parameters(&mut svm, &payer_1, parameter_1).is_ok()); @@ -59,8 +40,8 @@ fn test_stake_caps_parameters() { let parameter_2 = Parameters { current_authority: payer_2.pubkey(), - m: 3, - z: 4, + m: 3, + z: 4, }; require_wrong_authority_error(set_parameters(&mut svm, &payer_2, parameter_2)); @@ -71,8 +52,8 @@ fn test_stake_caps_parameters() { let parameter_3 = Parameters { current_authority: payer_1.pubkey(), - m: 5, - z: 6, + m: 5, + z: 6, }; require_wrong_authority_error(set_parameters(&mut svm, &payer_1, parameter_3)); @@ -85,8 +66,8 @@ fn set_parameters(svm: &mut LiteSVM, payer: &Keypair, parameters: Parameters) -> stake_caps_parameters::ID, &stake_caps_parameters::instruction::SetParameters { parameters }.data(), stake_caps_parameters::accounts::SetParameters { - signer: payer.pubkey(), - parameters: PARAMETERS_ADDRESS, + signer: payer.pubkey(), + parameters: PARAMETERS_ADDRESS, system_program: solana_sdk::system_program::ID, } .to_account_metas(None), diff --git a/target_chains/cosmwasm/contracts/pyth/src/bin/schema.rs b/target_chains/cosmwasm/contracts/pyth/src/bin/schema.rs index 0827959805..49881b222e 100644 --- a/target_chains/cosmwasm/contracts/pyth/src/bin/schema.rs +++ b/target_chains/cosmwasm/contracts/pyth/src/bin/schema.rs @@ -1,13 +1,7 @@ use { cosmwasm_schema::write_api, - pyth_cosmwasm::msg::{ - InstantiateMsg, - MigrateMsg, - }, - pyth_sdk_cw::{ - ExecuteMsg, - QueryMsg, - }, + pyth_cosmwasm::msg::{InstantiateMsg, MigrateMsg}, + pyth_sdk_cw::{ExecuteMsg, QueryMsg}, }; fn main() { diff --git a/target_chains/cosmwasm/contracts/pyth/src/contract.rs b/target_chains/cosmwasm/contracts/pyth/src/contract.rs index 2b3c0cb67e..2b8d49ba04 100644 --- a/target_chains/cosmwasm/contracts/pyth/src/contract.rs +++ b/target_chains/cosmwasm/contracts/pyth/src/contract.rs @@ -1,8 +1,5 @@ #[cfg(feature = "injective")] -use crate::injective::{ - create_relay_pyth_prices_msg, - InjectiveMsgWrapper as MsgWrapper, -}; +use crate::injective::{create_relay_pyth_prices_msg, InjectiveMsgWrapper as MsgWrapper}; #[cfg(not(feature = "injective"))] use cosmwasm_std::Empty as MsgWrapper; #[cfg(feature = "osmosis")] @@ -11,73 +8,30 @@ use { crate::{ governance::{ GovernanceAction::{ - AuthorizeGovernanceDataSourceTransfer, - RequestGovernanceDataSourceTransfer, - SetDataSources, - SetFee, - SetValidPeriod, - UpgradeContract, + AuthorizeGovernanceDataSourceTransfer, RequestGovernanceDataSourceTransfer, + SetDataSources, SetFee, SetValidPeriod, UpgradeContract, }, - GovernanceInstruction, - GovernanceModule, - }, - msg::{ - InstantiateMsg, - MigrateMsg, + GovernanceInstruction, GovernanceModule, }, + msg::{InstantiateMsg, MigrateMsg}, state::{ - config, - config_read, - price_feed_bucket, - price_feed_read_bucket, - set_contract_version, - ConfigInfo, - PythDataSource, + config, config_read, price_feed_bucket, price_feed_read_bucket, set_contract_version, + ConfigInfo, PythDataSource, }, }, byteorder::BigEndian, cosmwasm_std::{ - coin, - entry_point, - to_binary, - Addr, - Binary, - Coin, - CosmosMsg, - Deps, - DepsMut, - Env, - MessageInfo, - OverflowError, - OverflowOperation, - QueryRequest, - Response, - StdResult, - WasmMsg, + coin, entry_point, to_binary, Addr, Binary, Coin, CosmosMsg, Deps, DepsMut, Env, + MessageInfo, OverflowError, OverflowOperation, QueryRequest, Response, StdResult, WasmMsg, WasmQuery, }, - cw_wormhole::{ - msg::QueryMsg as WormholeQueryMsg, - state::ParsedVAA, - }, - pyth_sdk::{ - Identifier, - UnixTimestamp, - }, + cw_wormhole::{msg::QueryMsg as WormholeQueryMsg, state::ParsedVAA}, + pyth_sdk::{Identifier, UnixTimestamp}, pyth_sdk_cw::{ - error::PythContractError, - ExecuteMsg, - Price, - PriceFeed, - PriceFeedResponse, - PriceIdentifier, + error::PythContractError, ExecuteMsg, Price, PriceFeed, PriceFeedResponse, PriceIdentifier, QueryMsg, }, - pyth_wormhole_attester_sdk::{ - BatchPriceAttestation, - PriceAttestation, - PriceStatus, - }, + pyth_wormhole_attester_sdk::{BatchPriceAttestation, PriceAttestation, PriceStatus}, pythnet_sdk::{ accumulators::merkle::MerkleRoot, hashers::keccak256_160::Keccak160, @@ -85,20 +39,12 @@ use { wire::{ from_slice, v1::{ - AccumulatorUpdateData, - Proof, - WormholeMessage, - WormholePayload, + AccumulatorUpdateData, Proof, WormholeMessage, WormholePayload, PYTHNET_ACCUMULATOR_UPDATE_MAGIC, }, }, }, - std::{ - collections::HashSet, - convert::TryFrom, - iter::FromIterator, - time::Duration, - }, + std::{collections::HashSet, convert::TryFrom, iter::FromIterator, time::Duration}, }; const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -129,14 +75,14 @@ pub fn instantiate( ) -> StdResult { // Save general wormhole and pyth info let state = ConfigInfo { - wormhole_contract: deps.api.addr_validate(msg.wormhole_contract.as_ref())?, - data_sources: msg.data_sources.iter().cloned().collect(), - chain_id: msg.chain_id, - governance_source: msg.governance_source.clone(), - governance_source_index: msg.governance_source_index, + wormhole_contract: deps.api.addr_validate(msg.wormhole_contract.as_ref())?, + data_sources: msg.data_sources.iter().cloned().collect(), + chain_id: msg.chain_id, + governance_source: msg.governance_source.clone(), + governance_source_index: msg.governance_source_index, governance_sequence_number: msg.governance_sequence_number, - valid_time_period: Duration::from_secs(msg.valid_time_period_secs as u64), - fee: msg.fee, + valid_time_period: Duration::from_secs(msg.valid_time_period_secs as u64), + fee: msg.fee, }; config(deps.storage).save(&state)?; @@ -154,7 +100,7 @@ pub fn parse_and_verify_vaa(deps: Deps, block_time: u64, data: &Binary) -> StdRe let cfg = config_read(deps.storage).load()?; let vaa: ParsedVAA = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: cfg.wormhole_contract.to_string(), - msg: to_binary(&WormholeQueryMsg::VerifyVAA { + msg: to_binary(&WormholeQueryMsg::VerifyVAA { vaa: data.clone(), block_time, })?, @@ -417,7 +363,7 @@ fn transfer_governance( next_config.governance_source_index = governance_data_source_index; let new_governance_source = PythDataSource { - emitter: Binary::from(parsed_claim_vaa.emitter_address.clone()), + emitter: Binary::from(parsed_claim_vaa.emitter_address.clone()), chain_id: parsed_claim_vaa.emitter_chain, }; next_config.governance_source = new_governance_source; @@ -459,7 +405,7 @@ fn upgrade_contract(address: &Addr, new_code_id: u64) -> StdResult StdResult<()> { let vaa_data_source = PythDataSource { - emitter: vaa.emitter_address.clone().into(), + emitter: vaa.emitter_address.clone().into(), chain_id: vaa.emitter_chain, }; if !state.data_sources.contains(&vaa_data_source) { @@ -471,7 +417,7 @@ fn verify_vaa_from_data_source(state: &ConfigInfo, vaa: &ParsedVAA) -> StdResult /// Check that `vaa` is from a valid governance source (and hence is a legitimate governance instruction). fn verify_vaa_from_governance_source(state: &ConfigInfo, vaa: &ParsedVAA) -> StdResult<()> { let vaa_data_source = PythDataSource { - emitter: vaa.emitter_address.clone().into(), + emitter: vaa.emitter_address.clone().into(), chain_id: vaa.emitter_chain, }; if state.governance_source != vaa_data_source { @@ -544,15 +490,15 @@ fn parse_accumulator(deps: &Deps, env: &Env, data: &[u8]) -> StdResult PriceFeed::new( PriceIdentifier::new(price_attestation.price_id.to_bytes()), Price { - price: price_attestation.price, - conf: price_attestation.conf, - expo: price_attestation.expo, + price: price_attestation.price, + conf: price_attestation.conf, + expo: price_attestation.expo, publish_time: price_attestation.publish_time, }, Price { - price: price_attestation.ema_price, - conf: price_attestation.ema_conf, - expo: price_attestation.expo, + price: price_attestation.ema_price, + conf: price_attestation.ema_conf, + expo: price_attestation.expo, publish_time: price_attestation.publish_time, }, ), _ => PriceFeed::new( PriceIdentifier::new(price_attestation.price_id.to_bytes()), Price { - price: price_attestation.prev_price, - conf: price_attestation.prev_conf, - expo: price_attestation.expo, + price: price_attestation.prev_price, + conf: price_attestation.prev_conf, + expo: price_attestation.expo, publish_time: price_attestation.prev_publish_time, }, Price { - price: price_attestation.ema_price, - conf: price_attestation.ema_conf, - expo: price_attestation.expo, + price: price_attestation.ema_price, + conf: price_attestation.ema_conf, + expo: price_attestation.expo, publish_time: price_attestation.prev_publish_time, }, ), @@ -668,7 +614,6 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { } } - /// This function is not used in the contract yet but mimicks the behavior implemented /// in the EVM contract. We are yet to finalize how the parsed prices should be consumed /// in injective as well as other chains. @@ -797,70 +742,32 @@ mod test { use { super::*, crate::{ - governance::GovernanceModule::{ - Executor, - Target, - }, + governance::GovernanceModule::{Executor, Target}, state::get_contract_version, }, cosmwasm_std::{ - coins, - from_binary, - testing::{ - mock_dependencies, - mock_env, - mock_info, - MockApi, - MockQuerier, - MockStorage, - }, - Addr, - ContractResult, - OwnedDeps, - QuerierResult, - StdError, - SystemError, - SystemResult, + coins, from_binary, + testing::{mock_dependencies, mock_env, mock_info, MockApi, MockQuerier, MockStorage}, + Addr, ContractResult, OwnedDeps, QuerierResult, StdError, SystemError, SystemResult, Uint128, }, pyth_sdk::UnixTimestamp, pyth_sdk_cw::PriceIdentifier, pyth_wormhole_attester_sdk::PriceAttestation, pythnet_sdk::{ - accumulators::{ - merkle::MerkleTree, - Accumulator, - }, - messages::{ - PriceFeedMessage, - TwapMessage, - }, + accumulators::{merkle::MerkleTree, Accumulator}, + messages::{PriceFeedMessage, TwapMessage}, test_utils::{ - create_accumulator_message, - create_accumulator_message_from_updates, - create_dummy_price_feed_message, - create_vaa_from_payload, - DEFAULT_CHAIN_ID, - DEFAULT_DATA_SOURCE, - DEFAULT_GOVERNANCE_SOURCE, - DEFAULT_VALID_TIME_PERIOD, - SECONDARY_GOVERNANCE_SOURCE, - WRONG_CHAIN_ID, - WRONG_SOURCE, - }, - wire::{ - to_vec, - v1::MerklePriceUpdate, - PrefixedVec, + create_accumulator_message, create_accumulator_message_from_updates, + create_dummy_price_feed_message, create_vaa_from_payload, DEFAULT_CHAIN_ID, + DEFAULT_DATA_SOURCE, DEFAULT_GOVERNANCE_SOURCE, DEFAULT_VALID_TIME_PERIOD, + SECONDARY_GOVERNANCE_SOURCE, WRONG_CHAIN_ID, WRONG_SOURCE, }, + wire::{to_vec, v1::MerklePriceUpdate, PrefixedVec}, }, serde_wormhole::RawMessage, std::time::Duration, - wormhole_sdk::{ - Address, - Chain, - Vaa, - }, + wormhole_sdk::{Address, Chain, Vaa}, }; /// Default valid time period for testing purposes. @@ -902,7 +809,7 @@ mod test { )) } Err(_e) => SystemResult::Err(SystemError::InvalidRequest { - error: "Invalid message".into(), + error: "Invalid message".into(), request: msg.clone(), }), _ => SystemResult::Err(SystemError::NoSuchContract { @@ -957,20 +864,19 @@ mod test { ) } - fn create_zero_config_info() -> ConfigInfo { ConfigInfo { - wormhole_contract: Addr::unchecked(String::default()), - data_sources: HashSet::default(), - governance_source: PythDataSource { - emitter: Binary(vec![]), + wormhole_contract: Addr::unchecked(String::default()), + data_sources: HashSet::default(), + governance_source: PythDataSource { + emitter: Binary(vec![]), chain_id: 0, }, - governance_source_index: 0, + governance_source_index: 0, governance_sequence_number: 0, - chain_id: 0, - valid_time_period: Duration::new(0, 0), - fee: Coin::new(0, ""), + chain_id: 0, + valid_time_period: Duration::new(0, 0), + fee: Coin::new(0, ""), } } @@ -994,7 +900,7 @@ mod test { pyth_emitter_chain: u16, ) -> HashSet { HashSet::from([PythDataSource { - emitter: pyth_emitter.into(), + emitter: pyth_emitter.into(), chain_id: pyth_emitter_chain, }]) } @@ -1023,17 +929,17 @@ mod test { let instantiate_msg = InstantiateMsg { // this is an example wormhole contract address in order to create a valid instantiate message - wormhole_contract: String::from("inj1xx3aupmgv3ce537c0yce8zzd3sz567syuyedpg"), - data_sources: Vec::new(), - governance_source: PythDataSource { - emitter: Binary(vec![]), + wormhole_contract: String::from("inj1xx3aupmgv3ce537c0yce8zzd3sz567syuyedpg"), + data_sources: Vec::new(), + governance_source: PythDataSource { + emitter: Binary(vec![]), chain_id: 0, }, - governance_source_index: 0, + governance_source_index: 0, governance_sequence_number: 0, - chain_id: 0, - valid_time_period_secs: 0, - fee: Coin::new(0, ""), + chain_id: 0, + valid_time_period_secs: 0, + fee: Coin::new(0, ""), }; let res = instantiate( @@ -1041,7 +947,7 @@ mod test { mock_env(), MessageInfo { sender: Addr::unchecked(""), - funds: Vec::new(), + funds: Vec::new(), }, instantiate_msg, ); @@ -1061,17 +967,17 @@ mod test { let mut deps = mock_dependencies(); let instantiate_msg = InstantiateMsg { - wormhole_contract: String::from(""), - data_sources: Vec::new(), - governance_source: PythDataSource { - emitter: Binary(vec![]), + wormhole_contract: String::from(""), + data_sources: Vec::new(), + governance_source: PythDataSource { + emitter: Binary(vec![]), chain_id: 0, }, - governance_source_index: 0, + governance_source_index: 0, governance_sequence_number: 0, - chain_id: 0, - valid_time_period_secs: 0, - fee: Coin::new(0, ""), + chain_id: 0, + valid_time_period_secs: 0, + fee: Coin::new(0, ""), }; let res = instantiate( @@ -1079,14 +985,13 @@ mod test { mock_env(), MessageInfo { sender: Addr::unchecked(""), - funds: Vec::new(), + funds: Vec::new(), }, instantiate_msg, ); assert!(res.is_err()); } - #[cfg(feature = "osmosis")] fn check_sufficient_fee(deps: &Deps, data: &[Binary]) { let mut info = mock_info("123", coins(100, "foo").as_slice()); @@ -1209,7 +1114,7 @@ mod test { let proof1 = tree.prove(&feed1_bytes).unwrap(); price_updates.push(MerklePriceUpdate { message: PrefixedVec::from(feed1_bytes), - proof: proof1, + proof: proof1, }); let msg = create_accumulator_message_from_updates( price_updates, @@ -1242,7 +1147,6 @@ mod test { let (mut deps, _env) = setup_test(); config(&mut deps.storage).save(&config_info).unwrap(); - let feed1 = create_dummy_price_feed_message(100); let feed2 = create_dummy_price_feed_message(200); let feed3 = create_dummy_price_feed_message(300); @@ -1293,7 +1197,6 @@ mod test { ); } - #[test] fn test_accumulator_message_single_update() { let (mut deps, env) = setup_test(); @@ -1483,14 +1386,14 @@ mod test { // Although Twap Message is a valid message but it won't get stored on-chain via // `update_price_feeds` and (will be) used in other methods let feed1 = Message::TwapMessage(TwapMessage { - feed_id: [0; 32], - cumulative_price: 0, - cumulative_conf: 0, - num_down_slots: 0, - exponent: 0, - publish_time: 0, + feed_id: [0; 32], + cumulative_price: 0, + cumulative_conf: 0, + num_down_slots: 0, + exponent: 0, + publish_time: 0, prev_publish_time: 0, - publish_slot: 0, + publish_slot: 0, }); let msg = create_accumulator_message(&[&feed1], &[&feed1], false, false, None); let info = mock_info("123", &[]); @@ -1520,7 +1423,7 @@ mod test { price_updates.push(MerklePriceUpdate { // proof1 is valid for feed1, but not feed2 message: PrefixedVec::from(feed2_bytes), - proof: proof1, + proof: proof1, }); let msg = create_accumulator_message_from_updates( price_updates, @@ -1554,7 +1457,7 @@ mod test { let proof1 = tree.prove(&feed1_bytes).unwrap(); price_updates.push(MerklePriceUpdate { message: PrefixedVec::from(feed1_bytes), - proof: proof1, + proof: proof1, }); let msg = create_accumulator_message_from_updates( price_updates, @@ -1852,9 +1755,9 @@ mod test { let dummy_price_feed = PriceFeed::new( PriceIdentifier::new([0u8; 32]), Price { - price: 300, - conf: 301, - expo: 302, + price: 300, + conf: 301, + expo: 302, publish_time: 303, }, Default::default(), @@ -1967,7 +1870,6 @@ mod test { Ok(Coin::new(20, "uion")) ); - // test for invalid denom assert_eq!( get_update_fee_for_denom(&deps.as_ref(), &updates[0..0], "invalid_denom".to_string()), @@ -2077,7 +1979,7 @@ mod test { ConfigInfo { wormhole_contract: Addr::unchecked(WORMHOLE_ADDR), governance_source: PythDataSource { - emitter: Binary(DEFAULT_GOVERNANCE_SOURCE.address.0.to_vec()), + emitter: Binary(DEFAULT_GOVERNANCE_SOURCE.address.0.to_vec()), chain_id: DEFAULT_GOVERNANCE_SOURCE.chain.into(), }, governance_sequence_number: 4, @@ -2100,9 +2002,9 @@ mod test { let test_config = governance_test_config(); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: DEFAULT_CHAIN_ID.into(), - action: SetFee { val: 6, expo: 0 }, + action: SetFee { val: 6, expo: 0 }, }; let test_vaa = governance_vaa(&test_instruction); @@ -2162,7 +2064,7 @@ mod test { #[test] fn test_authorize_governance_transfer_success() { let source_2 = PythDataSource { - emitter: Binary::from(SECONDARY_GOVERNANCE_SOURCE.address.0), + emitter: Binary::from(SECONDARY_GOVERNANCE_SOURCE.address.0), chain_id: SECONDARY_GOVERNANCE_SOURCE.chain.into(), }; @@ -2170,9 +2072,9 @@ mod test { let claim_vaa = create_vaa_from_payload( &GovernanceInstruction { - module: Target, + module: Target, target_chain_id: test_config.chain_id, - action: RequestGovernanceDataSourceTransfer { + action: RequestGovernanceDataSourceTransfer { governance_data_source_index: 1, }, } @@ -2184,9 +2086,9 @@ mod test { ); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: test_config.chain_id, - action: AuthorizeGovernanceDataSourceTransfer { + action: AuthorizeGovernanceDataSourceTransfer { claim_vaa: serde_wormhole::to_vec(&claim_vaa).unwrap().into(), }, }; @@ -2205,9 +2107,9 @@ mod test { let claim_vaa = create_vaa_from_payload( &GovernanceInstruction { - module: Target, + module: Target, target_chain_id: test_config.chain_id, - action: RequestGovernanceDataSourceTransfer { + action: RequestGovernanceDataSourceTransfer { governance_data_source_index: 10, }, } @@ -2219,9 +2121,9 @@ mod test { ); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: test_config.chain_id, - action: AuthorizeGovernanceDataSourceTransfer { + action: AuthorizeGovernanceDataSourceTransfer { claim_vaa: serde_wormhole::to_vec(&claim_vaa).unwrap().into(), }, }; @@ -2239,9 +2141,9 @@ mod test { let claim_vaa = create_vaa_from_payload( &GovernanceInstruction { - module: Target, + module: Target, target_chain_id: WRONG_CHAIN_ID.into(), - action: RequestGovernanceDataSourceTransfer { + action: RequestGovernanceDataSourceTransfer { governance_data_source_index: 11, }, } @@ -2253,9 +2155,9 @@ mod test { ); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: test_config.chain_id, - action: AuthorizeGovernanceDataSourceTransfer { + action: AuthorizeGovernanceDataSourceTransfer { claim_vaa: serde_wormhole::to_vec(&claim_vaa).unwrap().into(), }, }; @@ -2270,15 +2172,15 @@ mod test { #[test] fn test_set_data_sources() { let source_1 = PythDataSource { - emitter: Binary::from([1u8; 32]), + emitter: Binary::from([1u8; 32]), chain_id: 2, }; let source_2 = PythDataSource { - emitter: Binary::from([2u8; 32]), + emitter: Binary::from([2u8; 32]), chain_id: 4, }; let source_3 = PythDataSource { - emitter: Binary::from([3u8; 32]), + emitter: Binary::from([3u8; 32]), chain_id: 6, }; @@ -2286,9 +2188,9 @@ mod test { test_config.data_sources = HashSet::from([source_1]); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: test_config.chain_id, - action: SetDataSources { + action: SetDataSources { data_sources: vec![source_2.clone(), source_3.clone()], }, }; @@ -2299,9 +2201,9 @@ mod test { ); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: test_config.chain_id, - action: SetDataSources { + action: SetDataSources { data_sources: vec![], }, }; @@ -2318,9 +2220,9 @@ mod test { test_config.fee = Coin::new(1, "foo"); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: DEFAULT_CHAIN_ID.into(), - action: SetFee { val: 6, expo: 1 }, + action: SetFee { val: 6, expo: 1 }, }; let test_vaa = governance_vaa(&test_instruction); @@ -2330,9 +2232,9 @@ mod test { ); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: DEFAULT_CHAIN_ID.into(), - action: SetFee { val: 6, expo: 0 }, + action: SetFee { val: 6, expo: 0 }, }; let test_vaa = governance_vaa(&test_instruction); @@ -2348,9 +2250,9 @@ mod test { test_config.valid_time_period = Duration::from_secs(10); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: DEFAULT_CHAIN_ID.into(), - action: SetValidPeriod { valid_seconds: 20 }, + action: SetValidPeriod { valid_seconds: 20 }, }; let test_vaa = governance_vaa(&test_instruction); @@ -2365,9 +2267,9 @@ mod test { let test_config = governance_test_config(); let test_instruction = GovernanceInstruction { - module: Target, + module: Target, target_chain_id: test_config.chain_id, - action: RequestGovernanceDataSourceTransfer { + action: RequestGovernanceDataSourceTransfer { governance_data_source_index: 7, }, }; diff --git a/target_chains/cosmwasm/contracts/pyth/src/governance.rs b/target_chains/cosmwasm/contracts/pyth/src/governance.rs index 5f4382ba31..490a911395 100644 --- a/target_chains/cosmwasm/contracts/pyth/src/governance.rs +++ b/target_chains/cosmwasm/contracts/pyth/src/governance.rs @@ -1,21 +1,11 @@ use { crate::state::PythDataSource, - byteorder::{ - BigEndian, - ReadBytesExt, - WriteBytesExt, - }, + byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}, cosmwasm_std::Binary, pyth_wormhole_attester_sdk::ErrBox, schemars::JsonSchema, - serde::{ - Deserialize, - Serialize, - }, - std::{ - convert::TryFrom, - io::Write, - }, + serde::{Deserialize, Serialize}, + std::{convert::TryFrom, io::Write}, }; const PYTH_GOVERNANCE_MAGIC: &[u8] = b"PTGM"; @@ -27,7 +17,7 @@ pub enum GovernanceModule { /// The PythNet executor contract. Messages sent to the Executor = 0, /// A target chain contract (like this one!) - Target = 1, + Target = 1, } impl GovernanceModule { @@ -80,8 +70,8 @@ pub enum GovernanceAction { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct GovernanceInstruction { - pub module: GovernanceModule, - pub action: GovernanceAction, + pub module: GovernanceModule, + pub action: GovernanceAction, pub target_chain_id: u16, } @@ -230,18 +220,14 @@ impl GovernanceInstruction { #[cfg(test)] mod test { - use crate::governance::{ - GovernanceAction, - GovernanceInstruction, - GovernanceModule, - }; + use crate::governance::{GovernanceAction, GovernanceInstruction, GovernanceModule}; #[test] fn test_payload_wrong_size() { let instruction = GovernanceInstruction { - module: GovernanceModule::Target, - action: GovernanceAction::SetFee { - val: 100, + module: GovernanceModule::Target, + action: GovernanceAction::SetFee { + val: 100, expo: 200, }, target_chain_id: 7, diff --git a/target_chains/cosmwasm/contracts/pyth/src/injective.rs b/target_chains/cosmwasm/contracts/pyth/src/injective.rs index 65d6c335c4..5be25596b1 100644 --- a/target_chains/cosmwasm/contracts/pyth/src/injective.rs +++ b/target_chains/cosmwasm/contracts/pyth/src/injective.rs @@ -1,26 +1,19 @@ use { - cosmwasm_std::{ - Addr, - CosmosMsg, - CustomMsg, - }, + cosmwasm_std::{Addr, CosmosMsg, CustomMsg}, pyth_sdk_cw::PriceFeed, schemars::JsonSchema, - serde::{ - Deserialize, - Serialize, - }, + serde::{Deserialize, Serialize}, }; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct InjectivePriceAttestation { - pub price_id: String, - pub price: i64, - pub conf: u64, - pub expo: i32, - pub ema_price: i64, - pub ema_conf: u64, - pub ema_expo: i32, + pub price_id: String, + pub price: i64, + pub conf: u64, + pub expo: i32, + pub ema_price: i64, + pub ema_conf: u64, + pub ema_expo: i32, pub publish_time: i64, } @@ -29,13 +22,13 @@ impl From<&PriceFeed> for InjectivePriceAttestation { let price = pa.get_price_unchecked(); let ema_price = pa.get_ema_price_unchecked(); InjectivePriceAttestation { - price_id: pa.id.to_hex(), - price: price.price, - conf: price.conf, - expo: price.expo, - ema_price: ema_price.price, - ema_conf: ema_price.conf, - ema_expo: ema_price.expo, + price_id: pa.id.to_hex(), + price: price.price, + conf: price.conf, + expo: price.expo, + ema_price: ema_price.price, + ema_conf: ema_price.conf, + ema_expo: ema_price.expo, publish_time: price.publish_time, } } @@ -45,16 +38,15 @@ impl From<&PriceFeed> for InjectivePriceAttestation { #[serde(rename_all = "snake_case")] pub enum InjectiveMsg { RelayPythPrices { - sender: Addr, + sender: Addr, price_attestations: Vec, }, } - #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, JsonSchema)] #[serde(rename_all = "snake_case")] pub struct InjectiveMsgWrapper { - pub route: String, + pub route: String, pub msg_data: InjectiveMsg, } @@ -63,7 +55,7 @@ pub fn create_relay_pyth_prices_msg( price_feeds: Vec, ) -> CosmosMsg { InjectiveMsgWrapper { - route: "oracle".to_string(), + route: "oracle".to_string(), msg_data: InjectiveMsg::RelayPythPrices { sender, price_attestations: price_feeds @@ -81,5 +73,4 @@ impl From for CosmosMsg { } } -impl CustomMsg for InjectiveMsgWrapper { -} +impl CustomMsg for InjectiveMsgWrapper {} diff --git a/target_chains/cosmwasm/contracts/pyth/src/msg.rs b/target_chains/cosmwasm/contracts/pyth/src/msg.rs index f77151360b..058950f22d 100644 --- a/target_chains/cosmwasm/contracts/pyth/src/msg.rs +++ b/target_chains/cosmwasm/contracts/pyth/src/msg.rs @@ -1,8 +1,4 @@ -use { - crate::state::PythDataSource, - cosmwasm_schema::cw_serde, - cosmwasm_std::Coin, -}; +use {crate::state::PythDataSource, cosmwasm_schema::cw_serde, cosmwasm_std::Coin}; // cw_serde attribute is equivalent to // #[derive(Serialize, Deserialize, PartialEq, Debug, Clone, JsonSchema)] @@ -13,13 +9,13 @@ type HumanAddr = String; #[cw_serde] pub struct InstantiateMsg { pub wormhole_contract: HumanAddr, - pub data_sources: Vec, + pub data_sources: Vec, - pub governance_source: PythDataSource, - pub governance_source_index: u32, + pub governance_source: PythDataSource, + pub governance_source_index: u32, pub governance_sequence_number: u64, - pub chain_id: u16, + pub chain_id: u16, pub valid_time_period_secs: u16, pub fee: Coin, diff --git a/target_chains/cosmwasm/contracts/pyth/src/state.rs b/target_chains/cosmwasm/contracts/pyth/src/state.rs index 31ff9134ff..bd1563fde8 100644 --- a/target_chains/cosmwasm/contracts/pyth/src/state.rs +++ b/target_chains/cosmwasm/contracts/pyth/src/state.rs @@ -1,31 +1,13 @@ use { - cosmwasm_std::{ - Addr, - Binary, - Coin, - StdResult, - Storage, - }, + cosmwasm_std::{Addr, Binary, Coin, StdResult, Storage}, cosmwasm_storage::{ - bucket, - bucket_read, - singleton, - singleton_read, - Bucket, - ReadonlyBucket, - ReadonlySingleton, + bucket, bucket_read, singleton, singleton_read, Bucket, ReadonlyBucket, ReadonlySingleton, Singleton, }, pyth_sdk_cw::PriceFeed, schemars::JsonSchema, - serde::{ - Deserialize, - Serialize, - }, - std::{ - collections::HashSet, - time::Duration, - }, + serde::{Deserialize, Serialize}, + std::{collections::HashSet, time::Duration}, }; pub static CONFIG_KEY: &[u8] = b"config_v1"; @@ -36,19 +18,19 @@ pub static CONTRACT_VERSION_KEY: &[u8] = b"contract_version"; /// a specific blockchain (given by `chain_id`). #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Hash, JsonSchema)] pub struct PythDataSource { - pub emitter: Binary, + pub emitter: Binary, pub chain_id: u16, } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct ConfigInfo { - pub wormhole_contract: Addr, - pub data_sources: HashSet, - pub governance_source: PythDataSource, + pub wormhole_contract: Addr, + pub data_sources: HashSet, + pub governance_source: PythDataSource, // Index for preventing replay attacks on governance data source transfers. // This index increases every time the governance data source is changed, which prevents old // transfer request VAAs from being replayed. - pub governance_source_index: u32, + pub governance_source_index: u32, // The wormhole sequence number for governance messages. This value is increased every time the // a governance instruction is executed. // @@ -58,8 +40,8 @@ pub struct ConfigInfo { pub governance_sequence_number: u64, // Warning: This id needs to agree with the wormhole chain id. // We should read this directly from wormhole, but their contract doesn't expose it. - pub chain_id: u16, - pub valid_time_period: Duration, + pub chain_id: u16, + pub valid_time_period: Duration, // The fee to pay, denominated in fee_denom (typically, the chain's native token) pub fee: Coin, diff --git a/target_chains/cosmwasm/examples/cw-contract/src/bin/schema.rs b/target_chains/cosmwasm/examples/cw-contract/src/bin/schema.rs index 74d67fad2a..19cd4440d0 100644 --- a/target_chains/cosmwasm/examples/cw-contract/src/bin/schema.rs +++ b/target_chains/cosmwasm/examples/cw-contract/src/bin/schema.rs @@ -1,11 +1,6 @@ use { cosmwasm_schema::write_api, - example_cw_contract::msg::{ - ExecuteMsg, - InstantiateMsg, - MigrateMsg, - QueryMsg, - }, + example_cw_contract::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}, }; fn main() { diff --git a/target_chains/cosmwasm/examples/cw-contract/src/contract.rs b/target_chains/cosmwasm/examples/cw-contract/src/contract.rs index bddee2eb96..fcc83e37ef 100644 --- a/target_chains/cosmwasm/examples/cw-contract/src/contract.rs +++ b/target_chains/cosmwasm/examples/cw-contract/src/contract.rs @@ -2,36 +2,13 @@ use cosmwasm_std::entry_point; use { crate::{ - msg::{ - ExecuteMsg, - FetchPriceResponse, - InstantiateMsg, - MigrateMsg, - QueryMsg, - }, - state::{ - State, - STATE, - }, + msg::{ExecuteMsg, FetchPriceResponse, InstantiateMsg, MigrateMsg, QueryMsg}, + state::{State, STATE}, }, cosmwasm_std::{ - to_binary, - Binary, - Coin, - Deps, - DepsMut, - Env, - MessageInfo, - Response, - StdError, - StdResult, - }, - pyth_sdk_cw::{ - get_update_fee, - get_valid_time_period, - query_price_feed, - PriceFeedResponse, + to_binary, Binary, Coin, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult, }, + pyth_sdk_cw::{get_update_fee, get_valid_time_period, query_price_feed, PriceFeedResponse}, std::time::Duration, }; @@ -54,7 +31,7 @@ pub fn instantiate( // that a wrong address won't be used. let state = State { pyth_contract_addr: deps.api.addr_validate(msg.pyth_contract_addr.as_ref())?, - price_feed_id: msg.price_feed_id, + price_feed_id: msg.price_feed_id, }; STATE.save(deps.storage, &state)?; @@ -137,33 +114,11 @@ mod test { super::*, cosmwasm_std::{ from_binary, - testing::{ - mock_dependencies, - mock_env, - MockApi, - MockQuerier, - MockStorage, - }, - Addr, - Coin, - OwnedDeps, - QuerierResult, - SystemError, - SystemResult, - Timestamp, - WasmQuery, - }, - pyth_sdk_cw::{ - testing::MockPyth, - Price, - PriceFeed, - PriceIdentifier, - UnixTimestamp, - }, - std::{ - convert::TryFrom, - time::Duration, + testing::{mock_dependencies, mock_env, MockApi, MockQuerier, MockStorage}, + Addr, Coin, OwnedDeps, QuerierResult, SystemError, SystemResult, Timestamp, WasmQuery, }, + pyth_sdk_cw::{testing::MockPyth, Price, PriceFeed, PriceIdentifier, UnixTimestamp}, + std::{convert::TryFrom, time::Duration}, }; // Dummy contract address for testing. @@ -175,7 +130,7 @@ mod test { fn default_state() -> State { State { pyth_contract_addr: Addr::unchecked(PYTH_CONTRACT_ADDR), - price_feed_id: PriceIdentifier::from_hex(PRICE_ID).unwrap(), + price_feed_id: PriceIdentifier::from_hex(PRICE_ID).unwrap(), } } @@ -234,15 +189,15 @@ mod test { let price_feed = PriceFeed::new( PriceIdentifier::from_hex(PRICE_ID).unwrap(), Price { - price: 100, - conf: 10, - expo: -1, + price: 100, + conf: 10, + expo: -1, publish_time: current_unix_time, }, Price { - price: 200, - conf: 20, - expo: -1, + price: 200, + conf: 20, + expo: -1, publish_time: current_unix_time, }, ); diff --git a/target_chains/cosmwasm/examples/cw-contract/src/msg.rs b/target_chains/cosmwasm/examples/cw-contract/src/msg.rs index c4954aa283..a176036d44 100644 --- a/target_chains/cosmwasm/examples/cw-contract/src/msg.rs +++ b/target_chains/cosmwasm/examples/cw-contract/src/msg.rs @@ -1,22 +1,16 @@ use std::time::Duration; use cosmwasm_std::{Binary, Coin}; -use pyth_sdk_cw::{ - Price, - PriceIdentifier, -}; +use pyth_sdk_cw::{Price, PriceIdentifier}; -use cosmwasm_schema::{ - cw_serde, - QueryResponses, -}; +use cosmwasm_schema::{cw_serde, QueryResponses}; #[cw_serde] pub struct MigrateMsg {} #[cw_serde] pub struct InstantiateMsg { - pub price_feed_id: PriceIdentifier, + pub price_feed_id: PriceIdentifier, pub pyth_contract_addr: String, } @@ -37,5 +31,5 @@ pub enum QueryMsg { #[cw_serde] pub struct FetchPriceResponse { pub current_price: Price, - pub ema_price: Price, + pub ema_price: Price, } diff --git a/target_chains/cosmwasm/examples/cw-contract/src/state.rs b/target_chains/cosmwasm/examples/cw-contract/src/state.rs index 7ca28b2df8..661d9193ed 100644 --- a/target_chains/cosmwasm/examples/cw-contract/src/state.rs +++ b/target_chains/cosmwasm/examples/cw-contract/src/state.rs @@ -1,9 +1,6 @@ use cosmwasm_std::Addr; use schemars::JsonSchema; -use serde::{ - Deserialize, - Serialize, -}; +use serde::{Deserialize, Serialize}; use cw_storage_plus::Item; use pyth_sdk_cw::PriceIdentifier; @@ -11,7 +8,7 @@ use pyth_sdk_cw::PriceIdentifier; #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct State { // Available price feeds and their ids are listed in pyth-sdk-cw Readme. - pub price_feed_id: PriceIdentifier, + pub price_feed_id: PriceIdentifier, // Contract address of Pyth in different networks are listed in pyth-sdk-cw Readme. pub pyth_contract_addr: Addr, } diff --git a/target_chains/cosmwasm/sdk/rust/src/error.rs b/target_chains/cosmwasm/sdk/rust/src/error.rs index 784a00fc17..8e98833baf 100644 --- a/target_chains/cosmwasm/sdk/rust/src/error.rs +++ b/target_chains/cosmwasm/sdk/rust/src/error.rs @@ -1,7 +1,4 @@ -use { - cosmwasm_std::StdError, - thiserror::Error, -}; +use {cosmwasm_std::StdError, thiserror::Error}; #[derive(Error, Debug)] pub enum PythContractError { diff --git a/target_chains/cosmwasm/sdk/rust/src/lib.rs b/target_chains/cosmwasm/sdk/rust/src/lib.rs index a66e23cba5..f7cea2d728 100644 --- a/target_chains/cosmwasm/sdk/rust/src/lib.rs +++ b/target_chains/cosmwasm/sdk/rust/src/lib.rs @@ -1,26 +1,11 @@ pub mod error; pub mod testing; -pub use pyth_sdk::{ - Price, - PriceFeed, - PriceIdentifier, - UnixTimestamp, -}; +pub use pyth_sdk::{Price, PriceFeed, PriceIdentifier, UnixTimestamp}; use { - cosmwasm_schema::{ - cw_serde, - QueryResponses, - }, + cosmwasm_schema::{cw_serde, QueryResponses}, cosmwasm_std::{ - to_binary, - Addr, - Binary, - Coin, - QuerierWrapper, - QueryRequest, - StdResult, - WasmQuery, + to_binary, Addr, Binary, Coin, QuerierWrapper, QueryRequest, StdResult, WasmQuery, }, std::time::Duration, }; @@ -59,7 +44,7 @@ pub fn query_price_feed( ) -> StdResult { let price_feed_response = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: contract_addr.into_string(), - msg: to_binary(&QueryMsg::PriceFeed { id })?, + msg: to_binary(&QueryMsg::PriceFeed { id })?, }))?; Ok(price_feed_response) } @@ -73,7 +58,7 @@ pub fn get_update_fee( ) -> StdResult { querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: contract_addr.into_string(), - msg: to_binary(&QueryMsg::GetUpdateFee { + msg: to_binary(&QueryMsg::GetUpdateFee { vaas: price_update_vaas.to_vec(), })?, })) @@ -90,7 +75,7 @@ pub fn get_update_fee_for_denom( ) -> StdResult { querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: contract_addr.into_string(), - msg: to_binary(&QueryMsg::GetUpdateFeeForDenom { + msg: to_binary(&QueryMsg::GetUpdateFeeForDenom { vaas: price_update_vaas.to_vec(), denom, })?, @@ -101,6 +86,6 @@ pub fn get_update_fee_for_denom( pub fn get_valid_time_period(querier: &QuerierWrapper, contract_addr: Addr) -> StdResult { querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: contract_addr.into_string(), - msg: to_binary(&QueryMsg::GetValidTimePeriod)?, + msg: to_binary(&QueryMsg::GetValidTimePeriod)?, })) } diff --git a/target_chains/cosmwasm/sdk/rust/src/testing.rs b/target_chains/cosmwasm/sdk/rust/src/testing.rs index b202bdb015..47ecdb0a70 100644 --- a/target_chains/cosmwasm/sdk/rust/src/testing.rs +++ b/target_chains/cosmwasm/sdk/rust/src/testing.rs @@ -1,26 +1,10 @@ use { - crate::{ - error::PythContractError, - PriceFeed, - PriceFeedResponse, - PriceIdentifier, - QueryMsg, - }, + crate::{error::PythContractError, PriceFeed, PriceFeedResponse, PriceIdentifier, QueryMsg}, cosmwasm_std::{ - from_binary, - to_binary, - Binary, - Coin, - ContractResult, - QuerierResult, - SystemError, + from_binary, to_binary, Binary, Coin, ContractResult, QuerierResult, SystemError, SystemResult, }, - std::{ - collections::HashMap, - time::Duration, - u128, - }, + std::{collections::HashMap, time::Duration, u128}, }; /// Mock version of Pyth for testing cosmwasm contracts. @@ -28,8 +12,8 @@ use { #[derive(Clone)] pub struct MockPyth { pub valid_time_period: Duration, - pub fee_per_vaa: Coin, - pub feeds: HashMap, + pub fee_per_vaa: Coin, + pub feeds: HashMap, } impl MockPyth { @@ -92,7 +76,7 @@ impl MockPyth { SystemResult::Ok(to_binary(&Coin::new(new_amount, denom)).into()) } Err(_e) => SystemResult::Err(SystemError::InvalidRequest { - error: "Invalid message".into(), + error: "Invalid message".into(), request: msg.clone(), }), } diff --git a/target_chains/fuel/contracts/src/constants.rs b/target_chains/fuel/contracts/src/constants.rs index f805378082..62975e70ce 100644 --- a/target_chains/fuel/contracts/src/constants.rs +++ b/target_chains/fuel/contracts/src/constants.rs @@ -6,8 +6,9 @@ pub const MAGIC: u32 = 0x5054474d; pub const GOVERNANCE_DATA_SOURCE: DataSource = DataSource { chain_id: 1, emitter_address: Bits256([ - 0x56, 0x35, 0x97, 0x9a, 0x22, 0x1c, 0x34, 0x93, 0x1e, 0x32, 0x62, 0x0b, 0x92, 0x93, 0xa4, 0x63, - 0x06, 0x55, 0x55, 0xea, 0x71, 0xfe, 0x97, 0xcd, 0x62, 0x37, 0xad, 0xe8, 0x75, 0xb1, 0x2e, 0x9e + 0x56, 0x35, 0x97, 0x9a, 0x22, 0x1c, 0x34, 0x93, 0x1e, 0x32, 0x62, 0x0b, 0x92, 0x93, 0xa4, + 0x63, 0x06, 0x55, 0x55, 0xea, 0x71, 0xfe, 0x97, 0xcd, 0x62, 0x37, 0xad, 0xe8, 0x75, 0xb1, + 0x2e, 0x9e, ]), }; diff --git a/target_chains/fuel/contracts/tests/functions/mod.rs b/target_chains/fuel/contracts/tests/functions/mod.rs index 277143fc15..8d01f7c2a7 100644 --- a/target_chains/fuel/contracts/tests/functions/mod.rs +++ b/target_chains/fuel/contracts/tests/functions/mod.rs @@ -1,5 +1,5 @@ pub(crate) mod pyth_core; +pub(crate) mod pyth_governance; pub(crate) mod pyth_info; pub(crate) mod pyth_init; -pub(crate) mod pyth_governance; pub(crate) mod wormhole_guardians; diff --git a/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price.rs b/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price.rs index bf0a7dccf9..70f5d9ff4e 100644 --- a/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price.rs +++ b/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price.rs @@ -41,8 +41,7 @@ mod success { .await .value; - update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()) - .await; + update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await; let eth_usd_ema_price = ema_price(&deployer.instance, default_price_feed_ids()[0]) .await diff --git a/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_no_older_than.rs b/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_no_older_than.rs index 68ee825856..8e0887f64a 100644 --- a/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_no_older_than.rs +++ b/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_no_older_than.rs @@ -41,8 +41,7 @@ mod success { .await .value; - update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()) - .await; + update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await; let eth_usd_ema_price = ema_price_no_older_than( &deployer.instance, diff --git a/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_unsafe.rs b/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_unsafe.rs index dacfb7db44..02a7b1f085 100644 --- a/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_unsafe.rs +++ b/target_chains/fuel/contracts/tests/functions/pyth_core/ema_price_unsafe.rs @@ -41,8 +41,7 @@ mod success { .await .value; - update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()) - .await; + update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await; let eth_usd_ema_price = ema_price_unsafe(&deployer.instance, default_price_feed_ids()[0]) .await diff --git a/target_chains/fuel/contracts/tests/functions/pyth_core/price_no_older_than.rs b/target_chains/fuel/contracts/tests/functions/pyth_core/price_no_older_than.rs index b67032cdf1..10911c93c0 100644 --- a/target_chains/fuel/contracts/tests/functions/pyth_core/price_no_older_than.rs +++ b/target_chains/fuel/contracts/tests/functions/pyth_core/price_no_older_than.rs @@ -42,8 +42,7 @@ mod success { .await .value; - update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()) - .await; + update_price_feeds(&deployer.instance, fee, test_batch_update_data_bytes()).await; let eth_usd_price = price_no_older_than( &deployer.instance, diff --git a/target_chains/fuel/contracts/tests/functions/pyth_init/constructor.rs b/target_chains/fuel/contracts/tests/functions/pyth_init/constructor.rs index bd2c77a67c..4cf62bcfda 100644 --- a/target_chains/fuel/contracts/tests/functions/pyth_init/constructor.rs +++ b/target_chains/fuel/contracts/tests/functions/pyth_init/constructor.rs @@ -8,7 +8,9 @@ use crate::utils::interface::{ }; use pyth_sdk::{ constants::{ - DEFAULT_SINGLE_UPDATE_FEE, DEFAULT_VALID_TIME_PERIOD, UPGRADE_3_VAA_GOVERNANCE_ACTION_HASH, GOVERNANCE_DATA_SOURCE, WORMHOLE_GOVERNANCE_DATA_SOURCE, DUMMY_CHAIN_ID + DEFAULT_SINGLE_UPDATE_FEE, DEFAULT_VALID_TIME_PERIOD, DUMMY_CHAIN_ID, + GOVERNANCE_DATA_SOURCE, UPGRADE_3_VAA_GOVERNANCE_ACTION_HASH, + WORMHOLE_GOVERNANCE_DATA_SOURCE, }, pyth_utils::{ default_data_sources, guardian_set_upgrade_3_addresses, ConstructedEvent, DataSource, State, @@ -66,7 +68,7 @@ mod success { DEFAULT_VALID_TIME_PERIOD, guardian_set_upgrade_3_addresses(), 3, - DUMMY_CHAIN_ID + DUMMY_CHAIN_ID, ) .await; diff --git a/target_chains/fuel/contracts/tests/utils/interface/mod.rs b/target_chains/fuel/contracts/tests/utils/interface/mod.rs index 277143fc15..8d01f7c2a7 100644 --- a/target_chains/fuel/contracts/tests/utils/interface/mod.rs +++ b/target_chains/fuel/contracts/tests/utils/interface/mod.rs @@ -1,5 +1,5 @@ pub(crate) mod pyth_core; +pub(crate) mod pyth_governance; pub(crate) mod pyth_info; pub(crate) mod pyth_init; -pub(crate) mod pyth_governance; pub(crate) mod wormhole_guardians; diff --git a/target_chains/near/example/src/lib.rs b/target_chains/near/example/src/lib.rs index 91016bc544..e7b1965d0f 100644 --- a/target_chains/near/example/src/lib.rs +++ b/target_chains/near/example/src/lib.rs @@ -1,23 +1,9 @@ use { near_sdk::{ - borsh::{ - self, - BorshDeserialize, - BorshSerialize, - }, - env, - is_promise_success, - log, - near_bindgen, - AccountId, - Gas, - PanicOnDefault, - Promise, - }, - pyth::state::{ - Price, - PriceIdentifier, + borsh::{self, BorshDeserialize, BorshSerialize}, + env, is_promise_success, log, near_bindgen, AccountId, Gas, PanicOnDefault, Promise, }, + pyth::state::{Price, PriceIdentifier}, }; /// Our contract simply processes prices, so for now the only state we diff --git a/target_chains/near/receiver/src/error.rs b/target_chains/near/receiver/src/error.rs index b3b45272c4..c85c0a61b2 100644 --- a/target_chains/near/receiver/src/error.rs +++ b/target_chains/near/receiver/src/error.rs @@ -1,8 +1,5 @@ use { - near_sdk::{ - serde::Serialize, - FunctionError, - }, + near_sdk::{serde::Serialize, FunctionError}, thiserror::Error, }; diff --git a/target_chains/near/receiver/src/ext.rs b/target_chains/near/receiver/src/ext.rs index 3ae91d9c5e..95a686d184 100644 --- a/target_chains/near/receiver/src/ext.rs +++ b/target_chains/near/receiver/src/ext.rs @@ -4,16 +4,9 @@ use { crate::{ error::Error, - state::{ - Price, - PriceIdentifier, - Source, - }, - }, - near_sdk::{ - ext_contract, - json_types::U128, + state::{Price, PriceIdentifier, Source}, }, + near_sdk::{ext_contract, json_types::U128}, std::collections::HashMap, }; diff --git a/target_chains/near/receiver/src/governance.rs b/target_chains/near/receiver/src/governance.rs index 91c74c9b93..f254023561 100644 --- a/target_chains/near/receiver/src/governance.rs +++ b/target_chains/near/receiver/src/governance.rs @@ -3,36 +3,16 @@ use { crate::{ ensure, - error::Error::{ - self, - *, - }, + error::Error::{self, *}, ext::ext_wormhole, - state::{ - Chain, - Source, - Vaa, - }, - Pyth, - PythExt, + state::{Chain, Source, Vaa}, + Pyth, PythExt, }, near_sdk::{ - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - env, - is_promise_success, - near_bindgen, - serde::{ - Deserialize, - Serialize, - }, - AccountId, - Gas, - NearToken, - Promise, - PromiseOrValue, + borsh::{BorshDeserialize, BorshSerialize}, + env, is_promise_success, near_bindgen, + serde::{Deserialize, Serialize}, + AccountId, Gas, NearToken, Promise, PromiseOrValue, }, num_traits::FromPrimitive, serde_wormhole::RawMessage, @@ -64,7 +44,7 @@ pub enum GovernanceModule { /// The PythNet executor contract Executor = 0, /// A target chain contract (like this one!) - Target = 1, + Target = 1, } /// A `GovernanceAction` represents the different actions that can be voted on and executed by the @@ -119,12 +99,7 @@ impl GovernanceInstruction { bytes::complete::take, combinator::all_consuming, multi::length_count, - number::complete::{ - be_u16, - be_u32, - be_u64, - be_u8, - }, + number::complete::{be_u16, be_u32, be_u64, be_u8}, }; let input = input.as_ref(); @@ -278,7 +253,7 @@ impl Pyth { self.gov_source == (Source { emitter: vaa.emitter_address, - chain: vaa.emitter_chain, + chain: vaa.emitter_chain, }), UnknownSource(vaa.emitter_address) ); @@ -465,7 +440,7 @@ impl Pyth { // Update Governance Source self.gov_source = Source { emitter: vaa.emitter_address, - chain: vaa.emitter_chain, + chain: vaa.emitter_chain, }; } @@ -568,16 +543,10 @@ mod tests { super::*, crate::governance::GovernanceActionId, near_sdk::{ - test_utils::{ - accounts, - VMContextBuilder, - }, + test_utils::{accounts, VMContextBuilder}, testing_env, }, - std::io::{ - Cursor, - Write, - }, + std::io::{Cursor, Write}, }; fn get_context() -> VMContextBuilder { diff --git a/target_chains/near/receiver/src/lib.rs b/target_chains/near/receiver/src/lib.rs index 5161214cf3..18fc9a0e78 100644 --- a/target_chains/near/receiver/src/lib.rs +++ b/target_chains/near/receiver/src/lib.rs @@ -4,32 +4,14 @@ use { error::Error, ext::ext_wormhole, near_sdk::{ - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - collections::{ - UnorderedMap, - UnorderedSet, - }, - env, - is_promise_success, + borsh::{BorshDeserialize, BorshSerialize}, + collections::{UnorderedMap, UnorderedSet}, + env, is_promise_success, json_types::U128, - log, - near_bindgen, - AccountId, - BorshStorageKey, - Duration, - Gas, - NearToken, - PanicOnDefault, - Promise, - StorageUsage, - }, - pyth_wormhole_attester_sdk::{ - BatchPriceAttestation, - P2W_MAGIC, + log, near_bindgen, AccountId, BorshStorageKey, Duration, Gas, NearToken, PanicOnDefault, + Promise, StorageUsage, }, + pyth_wormhole_attester_sdk::{BatchPriceAttestation, P2W_MAGIC}, pythnet_sdk::{ accumulators::merkle::MerkleRoot, hashers::keccak256_160::Keccak160, @@ -37,28 +19,16 @@ use { wire::{ from_slice, v1::{ - AccumulatorUpdateData, - Proof, - WormholeMessage, - WormholePayload, + AccumulatorUpdateData, Proof, WormholeMessage, WormholePayload, PYTHNET_ACCUMULATOR_UPDATE_MAGIC, }, }, }, serde_wormhole::RawMessage, - state::{ - Price, - PriceFeed, - PriceIdentifier, - Source, - Vaa, - }, + state::{Price, PriceFeed, PriceIdentifier, Source, Vaa}, std::{ collections::HashMap, - io::{ - Cursor, - Read, - }, + io::{Cursor, Read}, }, }; @@ -662,7 +632,7 @@ impl Pyth { if !self.sources.contains(&Source { emitter: vaa.emitter_address, - chain: vaa.emitter_chain, + chain: vaa.emitter_chain, }) { return Err(Error::UnknownSource(vaa.emitter_address)); } diff --git a/target_chains/near/receiver/src/state.rs b/target_chains/near/receiver/src/state.rs index a27004da6e..f6c0c6cc6a 100644 --- a/target_chains/near/receiver/src/state.rs +++ b/target_chains/near/receiver/src/state.rs @@ -1,17 +1,8 @@ use { near_sdk::{ - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - json_types::{ - I64, - U64, - }, - serde::{ - Deserialize, - Serialize, - }, + borsh::{BorshDeserialize, BorshSerialize}, + json_types::{I64, U64}, + serde::{Deserialize, Serialize}, }, pyth_wormhole_attester_sdk::PriceAttestation, pythnet_sdk::messages::PriceFeedMessage, @@ -89,11 +80,11 @@ impl near_sdk::serde::Serialize for PriceIdentifier { #[borsh(crate = "near_sdk::borsh")] #[serde(crate = "near_sdk::serde")] pub struct Price { - pub price: I64, + pub price: I64, /// Confidence interval around the price - pub conf: U64, + pub conf: U64, /// The exponent - pub expo: i32, + pub expo: i32, /// Unix timestamp of when this price was computed pub publish_time: i64, } @@ -107,9 +98,9 @@ pub struct Price { #[serde(crate = "near_sdk::serde")] pub struct PriceFeed { /// Unique identifier for this price. - pub id: PriceIdentifier, + pub id: PriceIdentifier, /// The current aggregation price. - pub price: Price, + pub price: Price, /// Exponentially moving average price. pub ema_price: Price, } @@ -117,17 +108,17 @@ pub struct PriceFeed { impl From<&PriceAttestation> for PriceFeed { fn from(price_attestation: &PriceAttestation) -> Self { Self { - id: PriceIdentifier(price_attestation.price_id.to_bytes()), - price: Price { - price: price_attestation.price.into(), - conf: price_attestation.conf.into(), - expo: price_attestation.expo, + id: PriceIdentifier(price_attestation.price_id.to_bytes()), + price: Price { + price: price_attestation.price.into(), + conf: price_attestation.conf.into(), + expo: price_attestation.expo, publish_time: price_attestation.publish_time, }, ema_price: Price { - price: price_attestation.ema_price.into(), - conf: price_attestation.ema_conf.into(), - expo: price_attestation.expo, + price: price_attestation.ema_price.into(), + conf: price_attestation.ema_conf.into(), + expo: price_attestation.expo, publish_time: price_attestation.publish_time, }, } @@ -137,17 +128,17 @@ impl From<&PriceAttestation> for PriceFeed { impl From<&PriceFeedMessage> for PriceFeed { fn from(price_feed_message: &PriceFeedMessage) -> Self { Self { - id: PriceIdentifier(price_feed_message.feed_id), - price: Price { - price: price_feed_message.price.into(), - conf: price_feed_message.conf.into(), - expo: price_feed_message.exponent, + id: PriceIdentifier(price_feed_message.feed_id), + price: Price { + price: price_feed_message.price.into(), + conf: price_feed_message.conf.into(), + expo: price_feed_message.exponent, publish_time: price_feed_message.publish_time, }, ema_price: Price { - price: price_feed_message.ema_price.into(), - conf: price_feed_message.ema_conf.into(), - expo: price_feed_message.exponent, + price: price_feed_message.ema_price.into(), + conf: price_feed_message.ema_conf.into(), + expo: price_feed_message.exponent, publish_time: price_feed_message.publish_time, }, } @@ -211,37 +202,37 @@ impl From for u16 { #[serde(crate = "near_sdk::serde")] pub struct Source { pub emitter: WormholeAddress, - pub chain: Chain, + pub chain: Chain, } /// A local `Vaa` type converted to from the Wormhole definition, this helps catch any upstream /// changes to the Wormhole VAA format. pub struct Vaa

{ - pub version: u8, + pub version: u8, pub guardian_set_index: u32, - pub signatures: Vec, - pub timestamp: u32, // Seconds since UNIX epoch - pub nonce: u32, - pub emitter_chain: Chain, - pub emitter_address: WormholeAddress, - pub sequence: u64, - pub consistency_level: u8, - pub payload: P, + pub signatures: Vec, + pub timestamp: u32, // Seconds since UNIX epoch + pub nonce: u32, + pub emitter_chain: Chain, + pub emitter_address: WormholeAddress, + pub sequence: u64, + pub consistency_level: u8, + pub payload: P, } impl

From> for Vaa

{ fn from(vaa: wormhole_sdk::Vaa

) -> Self { Self { - version: vaa.version, + version: vaa.version, guardian_set_index: vaa.guardian_set_index, - signatures: vaa.signatures.iter().map(|s| s.signature).collect(), - timestamp: vaa.timestamp, - nonce: vaa.nonce, - emitter_chain: vaa.emitter_chain.into(), - emitter_address: vaa.emitter_address.0, - sequence: vaa.sequence, - consistency_level: vaa.consistency_level, - payload: vaa.payload, + signatures: vaa.signatures.iter().map(|s| s.signature).collect(), + timestamp: vaa.timestamp, + nonce: vaa.nonce, + emitter_chain: vaa.emitter_chain.into(), + emitter_address: vaa.emitter_address.0, + sequence: vaa.sequence, + consistency_level: vaa.consistency_level, + payload: vaa.payload, } } } diff --git a/target_chains/near/receiver/src/tests.rs b/target_chains/near/receiver/src/tests.rs index f5285a6593..8cf6be6bb8 100644 --- a/target_chains/near/receiver/src/tests.rs +++ b/target_chains/near/receiver/src/tests.rs @@ -2,15 +2,8 @@ #[allow(clippy::module_inception)] mod tests { use { - crate::{ - state::Source, - Pyth, - }, - near_sdk::{ - test_utils::VMContextBuilder, - testing_env, - VMContext, - }, + crate::{state::Source, Pyth}, + near_sdk::{test_utils::VMContextBuilder, testing_env, VMContext}, }; fn create_contract() -> Pyth { diff --git a/target_chains/near/receiver/tests/workspaces.rs b/target_chains/near/receiver/tests/workspaces.rs index 58d1b4c27e..68abee2856 100644 --- a/target_chains/near/receiver/tests/workspaces.rs +++ b/target_chains/near/receiver/tests/workspaces.rs @@ -1,41 +1,17 @@ use { - near_sdk::json_types::{ - I64, - U128, - U64, - }, - near_workspaces::types::{ - Gas, - NearToken, - }, + near_sdk::json_types::{I64, U128, U64}, + near_workspaces::types::{Gas, NearToken}, pyth::{ - governance::{ - GovernanceAction, - GovernanceInstruction, - GovernanceModule, - }, - state::{ - Chain, - Price, - PriceIdentifier, - Source, - }, + governance::{GovernanceAction, GovernanceInstruction, GovernanceModule}, + state::{Chain, Price, PriceIdentifier, Source}, }, pyth_wormhole_attester_sdk::{ - BatchPriceAttestation, - Identifier, - PriceAttestation, - PriceStatus, + BatchPriceAttestation, Identifier, PriceAttestation, PriceStatus, }, pythnet_sdk::test_utils::{ - create_accumulator_message, - create_dummy_price_feed_message, - create_vaa_from_payload, - DEFAULT_DATA_SOURCE, - DEFAULT_GOVERNANCE_SOURCE, - DEFAULT_VALID_TIME_PERIOD, - SECONDARY_DATA_SOURCE, - SECONDARY_GOVERNANCE_SOURCE, + create_accumulator_message, create_dummy_price_feed_message, create_vaa_from_payload, + DEFAULT_DATA_SOURCE, DEFAULT_GOVERNANCE_SOURCE, DEFAULT_VALID_TIME_PERIOD, + SECONDARY_DATA_SOURCE, SECONDARY_GOVERNANCE_SOURCE, }, serde_json::json, std::collections::HashMap, @@ -117,13 +93,13 @@ async fn test_set_sources() { data_sources: vec![ Source { emitter: DEFAULT_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from( + chain: Chain::from(WormholeChain::from(u16::from( DEFAULT_DATA_SOURCE.chain, ))), }, Source { emitter: SECONDARY_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from( + chain: Chain::from(WormholeChain::from(u16::from( SECONDARY_DATA_SOURCE.chain, ))), }, @@ -160,11 +136,11 @@ async fn test_set_sources() { &[ Source { emitter: DEFAULT_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from(DEFAULT_DATA_SOURCE.chain))), + chain: Chain::from(WormholeChain::from(u16::from(DEFAULT_DATA_SOURCE.chain))), }, Source { emitter: SECONDARY_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from(SECONDARY_DATA_SOURCE.chain))), + chain: Chain::from(WormholeChain::from(u16::from(SECONDARY_DATA_SOURCE.chain))), }, ] ); @@ -231,13 +207,13 @@ async fn test_set_governance_source() { data_sources: vec![ Source { emitter: DEFAULT_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from( + chain: Chain::from(WormholeChain::from(u16::from( DEFAULT_DATA_SOURCE.chain, ))), }, Source { emitter: SECONDARY_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from( + chain: Chain::from(WormholeChain::from(u16::from( SECONDARY_DATA_SOURCE.chain, ))), }, @@ -277,13 +253,13 @@ async fn test_set_governance_source() { Source::default(), Source { emitter: DEFAULT_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from( + chain: Chain::from(WormholeChain::from(u16::from( DEFAULT_DATA_SOURCE.chain, ))), }, Source { emitter: SECONDARY_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from( + chain: Chain::from(WormholeChain::from(u16::from( SECONDARY_DATA_SOURCE.chain, ))), }, @@ -298,7 +274,6 @@ async fn test_set_governance_source() { ); let vaa = hex::encode(serde_wormhole::to_vec(&vaa).unwrap()); - assert!(contract .call("execute_governance_instruction") .gas(Gas::from_gas(300_000_000_000_000)) @@ -331,21 +306,21 @@ async fn test_stale_threshold() { let vaa = create_vaa_from_payload( &BatchPriceAttestation { price_attestations: vec![PriceAttestation { - product_id: Identifier::default(), - price_id: Identifier::default(), - price: 100, - conf: 1, - expo: 8, - ema_price: 100, - ema_conf: 1, - status: PriceStatus::Trading, - num_publishers: 8, - max_num_publishers: 8, - attestation_time: now.try_into().unwrap(), - publish_time: now.try_into().unwrap(), - prev_publish_time: now.try_into().unwrap(), - prev_price: 100, - prev_conf: 1, + product_id: Identifier::default(), + price_id: Identifier::default(), + price: 100, + conf: 1, + expo: 8, + ema_price: 100, + ema_conf: 1, + status: PriceStatus::Trading, + num_publishers: 8, + max_num_publishers: 8, + attestation_time: now.try_into().unwrap(), + publish_time: now.try_into().unwrap(), + prev_publish_time: now.try_into().unwrap(), + prev_price: 100, + prev_conf: 1, last_attested_publish_time: now.try_into().unwrap(), }], } @@ -408,21 +383,21 @@ async fn test_stale_threshold() { let vaa = create_vaa_from_payload( &BatchPriceAttestation { price_attestations: vec![PriceAttestation { - product_id: Identifier::default(), - price_id: Identifier::default(), - price: 1000, - conf: 1, - expo: 8, - ema_price: 1000, - ema_conf: 1, - status: PriceStatus::Trading, - num_publishers: 8, - max_num_publishers: 8, - attestation_time: (now - 1024).try_into().unwrap(), - publish_time: (now - 1024).try_into().unwrap(), - prev_publish_time: (now - 1024).try_into().unwrap(), - prev_price: 90, - prev_conf: 1, + product_id: Identifier::default(), + price_id: Identifier::default(), + price: 1000, + conf: 1, + expo: 8, + ema_price: 1000, + ema_conf: 1, + status: PriceStatus::Trading, + num_publishers: 8, + max_num_publishers: 8, + attestation_time: (now - 1024).try_into().unwrap(), + publish_time: (now - 1024).try_into().unwrap(), + prev_publish_time: (now - 1024).try_into().unwrap(), + prev_price: 90, + prev_conf: 1, last_attested_publish_time: (now - 1024).try_into().unwrap(), }], } @@ -454,9 +429,9 @@ async fn test_stale_threshold() { // timestamp and price should be unchanged. assert_eq!( Price { - price: 100.into(), - conf: 1.into(), - expo: 8, + price: 100.into(), + conf: 1.into(), + expo: 8, publish_time: now as i64, }, serde_json::from_slice::( @@ -504,9 +479,9 @@ async fn test_stale_threshold() { // [ref:failed_price_check] assert_eq!( Some(Price { - price: 100.into(), - conf: 1.into(), - expo: 8, + price: 100.into(), + conf: 1.into(), + expo: 8, publish_time: now as i64, }), serde_json::from_slice::>( @@ -521,9 +496,9 @@ async fn test_stale_threshold() { ); assert_eq!( &Some(Price { - price: 100.into(), - conf: 1.into(), - expo: 8, + price: 100.into(), + conf: 1.into(), + expo: 8, publish_time: now as i64, }), serde_json::from_slice::>>( @@ -615,21 +590,21 @@ async fn test_contract_fees() { let vaa = create_vaa_from_payload( &BatchPriceAttestation { price_attestations: vec![PriceAttestation { - product_id: Identifier::default(), - price_id: Identifier::default(), - price: 1000, - conf: 1, - expo: 8, - ema_price: 1000, - ema_conf: 1, - status: PriceStatus::Trading, - num_publishers: 8, - max_num_publishers: 8, - attestation_time: (now - 1024).try_into().unwrap(), - publish_time: (now - 1024).try_into().unwrap(), - prev_publish_time: (now - 1024).try_into().unwrap(), - prev_price: 90, - prev_conf: 1, + product_id: Identifier::default(), + price_id: Identifier::default(), + price: 1000, + conf: 1, + expo: 8, + ema_price: 1000, + ema_conf: 1, + status: PriceStatus::Trading, + num_publishers: 8, + max_num_publishers: 8, + attestation_time: (now - 1024).try_into().unwrap(), + publish_time: (now - 1024).try_into().unwrap(), + prev_publish_time: (now - 1024).try_into().unwrap(), + prev_price: 90, + prev_conf: 1, last_attested_publish_time: (now - 1024).try_into().unwrap(), }], } @@ -692,7 +667,6 @@ async fn test_same_governance_sequence_fails() { ); let vaa = hex::encode(serde_wormhole::to_vec(&vaa).unwrap()); - // Attempt our first SetFee. assert!(contract .call("execute_governance_instruction") @@ -845,7 +819,6 @@ async fn test_governance_target_fails_if_not_near() { ); let vaa = hex::encode(serde_wormhole::to_vec(&vaa).unwrap()); - // This should fail as the target is Solana, when Near is expected. assert!(!contract .call("execute_governance_instruction") @@ -876,7 +849,7 @@ async fn test_accumulator_updates() { action: GovernanceAction::SetDataSources { data_sources: vec![Source { emitter: DEFAULT_DATA_SOURCE.address.0, - chain: Chain::from(WormholeChain::from(u16::from(DEFAULT_DATA_SOURCE.chain))), + chain: Chain::from(WormholeChain::from(u16::from(DEFAULT_DATA_SOURCE.chain))), }], }, } @@ -931,9 +904,9 @@ async fn test_accumulator_updates() { assert_eq!( Some(Price { - price: 100.into(), - conf: 100.into(), - expo: 100, + price: 100.into(), + conf: 100.into(), + expo: 100, publish_time: 100, }), serde_json::from_slice::>( @@ -951,9 +924,9 @@ async fn test_accumulator_updates() { #[tokio::test] async fn test_sdk_compat() { let price = pyth_sdk::Price { - price: i64::MAX, - conf: u64::MAX, - expo: 100, + price: i64::MAX, + conf: u64::MAX, + expo: 100, publish_time: 100, }; @@ -962,9 +935,9 @@ async fn test_sdk_compat() { assert_eq!( decoded_price, Price { - price: i64::MAX.into(), - conf: u64::MAX.into(), - expo: 100, + price: i64::MAX.into(), + conf: u64::MAX.into(), + expo: 100, publish_time: 100, } ); @@ -972,15 +945,12 @@ async fn test_sdk_compat() { #[tokio::test] async fn test_borsh_field_cmopat() { - use near_sdk::borsh::{ - BorshDeserialize, - BorshSerialize, - }; + use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; let price = Price { - price: I64(i64::MAX), - conf: U64(u64::MAX), - expo: 100, + price: I64(i64::MAX), + conf: U64(u64::MAX), + expo: 100, publish_time: 100, }; @@ -989,9 +959,9 @@ async fn test_borsh_field_cmopat() { #[derive(Eq, PartialEq, Debug, BorshSerialize, BorshDeserialize)] #[borsh(crate = "near_sdk::borsh")] struct PriceTester { - price: i64, - conf: u64, - expo: u32, + price: i64, + conf: u64, + expo: u32, bad_field_name: u64, } @@ -1001,9 +971,9 @@ async fn test_borsh_field_cmopat() { assert_eq!( decoded_price, PriceTester { - price: i64::MAX, - conf: u64::MAX, - expo: 100, + price: i64::MAX, + conf: u64::MAX, + expo: 100, bad_field_name: 100, } ); diff --git a/target_chains/near/wormhole-stub/src/lib.rs b/target_chains/near/wormhole-stub/src/lib.rs index 93d81fa854..8f206bc8c8 100644 --- a/target_chains/near/wormhole-stub/src/lib.rs +++ b/target_chains/near/wormhole-stub/src/lib.rs @@ -1,13 +1,8 @@ //#![deny(warnings)] use near_sdk::{ - borsh::{ - BorshDeserialize, - BorshSerialize, - }, - log, - near_bindgen, - PanicOnDefault, + borsh::{BorshDeserialize, BorshSerialize}, + log, near_bindgen, PanicOnDefault, }; #[near_bindgen] diff --git a/target_chains/solana/cli/src/cli.rs b/target_chains/solana/cli/src/cli.rs index fe05eaae4c..eea41a787a 100644 --- a/target_chains/solana/cli/src/cli.rs +++ b/target_chains/solana/cli/src/cli.rs @@ -1,8 +1,5 @@ use { - clap::{ - Parser, - Subcommand, - }, + clap::{Parser, Subcommand}, solana_sdk::pubkey::Pubkey, std::str::FromStr, }; @@ -19,18 +16,18 @@ pub struct Cli { default_value = "~/.config/solana/id.json", help = "Keypair of the payer of transactions" )] - pub keypair: String, + pub keypair: String, #[clap( short = 'u', long, default_value = "http://localhost:8899", help = "RPC endpoint of the solana" )] - pub url: String, + pub url: String, #[clap(short = 'w', long, parse(try_from_str = Pubkey::from_str), help = "Address of the wormhole contract")] pub wormhole: Pubkey, #[clap(subcommand)] - pub action: Action, + pub action: Action, } #[derive(Subcommand, Debug)] @@ -43,7 +40,7 @@ pub enum Action { #[clap(about = "Post a price update from Hermes to Solana in one transaction")] PostPriceUpdateAtomic { #[clap(short = 'p', long, help = "Payload from Hermes")] - payload: String, + payload: String, #[clap( short = 'n', default_value = "5", @@ -57,11 +54,11 @@ pub enum Action { InitializeWormholeReceiver {}, InitializePythReceiver { #[clap(short = 'f', long, help = "Fee in lmaports")] - fee: u64, + fee: u64, #[clap(short = 'e', long, parse(try_from_str = Pubkey::from_str), help = "Source emitter")] - emitter: Pubkey, + emitter: Pubkey, #[clap(short = 'c', long, help = "Source chain")] - chain: u16, + chain: u16, #[clap(short = 'a', long, parse(try_from_str = Pubkey::from_str), help = "Governance authority")] governance_authority: Pubkey, }, diff --git a/target_chains/solana/cli/src/main.rs b/target_chains/solana/cli/src/main.rs index 53195925d4..48ec19f02c 100644 --- a/target_chains/solana/cli/src/main.rs +++ b/target_chains/solana/cli/src/main.rs @@ -2,68 +2,40 @@ pub mod cli; - use { - anchor_client::anchor_lang::{ - InstructionData, - ToAccountMetas, - }, + anchor_client::anchor_lang::{InstructionData, ToAccountMetas}, anyhow::Result, borsh::BorshDeserialize, clap::Parser, - cli::{ - Action, - Cli, - }, + cli::{Action, Cli}, pyth_solana_receiver::sdk::{ - deserialize_accumulator_update_data, - get_random_treasury_id, - VAA_SPLIT_INDEX, + deserialize_accumulator_update_data, get_random_treasury_id, VAA_SPLIT_INDEX, }, pyth_solana_receiver_sdk::config::DataSource, pythnet_sdk::wire::v1::MerklePriceUpdate, serde_wormhole::RawMessage, - solana_client::{ - rpc_client::RpcClient, - rpc_config::RpcSendTransactionConfig, - }, + solana_client::{rpc_client::RpcClient, rpc_config::RpcSendTransactionConfig}, solana_sdk::{ commitment_config::CommitmentConfig, compute_budget::ComputeBudgetInstruction, instruction::Instruction, pubkey::Pubkey, rent::Rent, - signature::{ - read_keypair_file, - Keypair, - }, + signature::{read_keypair_file, Keypair}, signer::Signer, system_instruction, transaction::Transaction, }, - wormhole_core_bridge_solana::sdk::{ - WriteEncodedVaaArgs, - VAA_START, - }, + wormhole_core_bridge_solana::sdk::{WriteEncodedVaaArgs, VAA_START}, wormhole_sdk::{ - vaa::{ - Body, - Header, - }, + vaa::{Body, Header}, Vaa, }, wormhole_solana::{ instructions::{ - initialize, - post_vaa, - upgrade_guardian_set, - verify_signatures_txs, - PostVAAData, + initialize, post_vaa, upgrade_guardian_set, verify_signatures_txs, PostVAAData, }, - Account, - Config as BridgeConfig, - GuardianSet, - VAA as LegacyPostedVaa, + Account, Config as BridgeConfig, GuardianSet, VAA as LegacyPostedVaa, }, }; @@ -357,15 +329,15 @@ pub fn process_legacy_post_vaa( process_transaction(rpc_client, tx, &vec![payer, &signature_set_keypair])?; } let post_vaa_data = PostVAAData { - version: header.version, + version: header.version, guardian_set_index: header.guardian_set_index, - timestamp: body.timestamp, - nonce: body.nonce, - emitter_chain: body.emitter_chain.into(), - emitter_address: body.emitter_address.0, - sequence: body.sequence, - consistency_level: body.consistency_level, - payload: body.payload.to_vec(), + timestamp: body.timestamp, + nonce: body.nonce, + emitter_chain: body.emitter_chain.into(), + emitter_address: body.emitter_address.0, + sequence: body.sequence, + consistency_level: body.consistency_level, + payload: body.payload.to_vec(), }; process_transaction( @@ -406,35 +378,34 @@ pub fn process_write_encoded_vaa_and_post_price_update( ); let init_encoded_vaa_accounts = wormhole_core_bridge_solana::accounts::InitEncodedVaa { write_authority: payer.pubkey(), - encoded_vaa: encoded_vaa_keypair.pubkey(), + encoded_vaa: encoded_vaa_keypair.pubkey(), } .to_account_metas(None); let init_encoded_vaa_instruction = Instruction { program_id: wormhole, - accounts: init_encoded_vaa_accounts, - data: wormhole_core_bridge_solana::instruction::InitEncodedVaa.data(), + accounts: init_encoded_vaa_accounts, + data: wormhole_core_bridge_solana::instruction::InitEncodedVaa.data(), }; let write_encoded_vaa_accounts = wormhole_core_bridge_solana::accounts::WriteEncodedVaa { write_authority: payer.pubkey(), - draft_vaa: encoded_vaa_keypair.pubkey(), + draft_vaa: encoded_vaa_keypair.pubkey(), } .to_account_metas(None); let write_encoded_vaa_accounts_instruction = Instruction { program_id: wormhole, - accounts: write_encoded_vaa_accounts.clone(), - data: wormhole_core_bridge_solana::instruction::WriteEncodedVaa { + accounts: write_encoded_vaa_accounts.clone(), + data: wormhole_core_bridge_solana::instruction::WriteEncodedVaa { args: WriteEncodedVaaArgs { index: 0, - data: vaa[..VAA_SPLIT_INDEX].to_vec(), + data: vaa[..VAA_SPLIT_INDEX].to_vec(), }, } .data(), }; - // 1st transaction process_transaction( rpc_client, @@ -448,11 +419,11 @@ pub fn process_write_encoded_vaa_and_post_price_update( let write_encoded_vaa_accounts_instruction_2 = Instruction { program_id: wormhole, - accounts: write_encoded_vaa_accounts, - data: wormhole_core_bridge_solana::instruction::WriteEncodedVaa { + accounts: write_encoded_vaa_accounts, + data: wormhole_core_bridge_solana::instruction::WriteEncodedVaa { args: WriteEncodedVaaArgs { index: VAA_SPLIT_INDEX.try_into().unwrap(), - data: vaa[VAA_SPLIT_INDEX..].to_vec(), + data: vaa[VAA_SPLIT_INDEX..].to_vec(), }, } .data(), @@ -473,8 +444,8 @@ pub fn process_write_encoded_vaa_and_post_price_update( let verify_encoded_vaa_instruction = Instruction { program_id: wormhole, - accounts: verify_encoded_vaa_accounts, - data: wormhole_core_bridge_solana::instruction::VerifyEncodedVaaV1 {}.data(), + accounts: verify_encoded_vaa_accounts, + data: wormhole_core_bridge_solana::instruction::VerifyEncodedVaaV1 {}.data(), }; let price_update_keypair = Keypair::new(); @@ -500,7 +471,6 @@ pub fn process_write_encoded_vaa_and_post_price_update( &vec![payer, &price_update_keypair], )?; - Ok(price_update_keypair.pubkey()) } diff --git a/target_chains/solana/common_test_utils/src/lib.rs b/target_chains/solana/common_test_utils/src/lib.rs index 5889270150..c80765a41a 100644 --- a/target_chains/solana/common_test_utils/src/lib.rs +++ b/target_chains/solana/common_test_utils/src/lib.rs @@ -2,45 +2,19 @@ use { anchor_lang::AnchorSerialize, libsecp256k1::PublicKey, program_simulator::ProgramSimulator, - pyth_solana_receiver::{ - instruction::Initialize, - sdk::get_guardian_set_address, - ID, - }, + pyth_solana_receiver::{instruction::Initialize, sdk::get_guardian_set_address, ID}, pyth_solana_receiver_sdk::{ - config::{ - Config, - DataSource, - }, - pda::{ - get_config_address, - get_treasury_address, - }, + config::{Config, DataSource}, + pda::{get_config_address, get_treasury_address}, PYTH_PUSH_ORACLE_ID, }, - pythnet_sdk::test_utils::{ - dummy_guardians, - DEFAULT_DATA_SOURCE, - }, + pythnet_sdk::test_utils::{dummy_guardians, DEFAULT_DATA_SOURCE}, serde_wormhole::RawMessage, - solana_program::{ - keccak, - pubkey::Pubkey, - rent::Rent, - }, + solana_program::{keccak, pubkey::Pubkey, rent::Rent}, solana_program_test::ProgramTest, - solana_sdk::{ - account::Account, - signature::Keypair, - signer::Signer, - }, + solana_sdk::{account::Account, signature::Keypair, signer::Signer}, wormhole_core_bridge_solana::{ - state::{ - EncodedVaa, - GuardianSet, - Header, - ProcessingStatus, - }, + state::{EncodedVaa, GuardianSet, Header, ProcessingStatus}, ID as BRIDGE_ID, }, wormhole_sdk::Vaa, @@ -55,7 +29,7 @@ pub fn default_receiver_config(governance_authority: Pubkey) -> Config { target_governance_authority: None, wormhole: BRIDGE_ID, valid_data_sources: vec![DataSource { - chain: DEFAULT_DATA_SOURCE.chain.into(), + chain: DEFAULT_DATA_SOURCE.chain.into(), emitter: Pubkey::from(DEFAULT_DATA_SOURCE.address.0), }], single_update_fee_in_lamports: 1, @@ -63,11 +37,10 @@ pub fn default_receiver_config(governance_authority: Pubkey) -> Config { } } - pub struct ProgramTestFixtures { - pub program_simulator: ProgramSimulator, + pub program_simulator: ProgramSimulator, pub encoded_vaa_addresses: Vec, - pub governance_authority: Keypair, + pub governance_authority: Keypair, } pub fn build_encoded_vaa_account_from_vaa( @@ -77,7 +50,7 @@ pub fn build_encoded_vaa_account_from_vaa( let encoded_vaa_data = ( ::DISCRIMINATOR, Header { - status: { + status: { if matches!(wrong_setup_option, WrongSetupOption::UnverifiedEncodedVaa) { ProcessingStatus::Writing } else { @@ -85,7 +58,7 @@ pub fn build_encoded_vaa_account_from_vaa( } }, write_authority: Pubkey::new_unique(), - version: 1, + version: 1, }, serde_wormhole::to_vec(&vaa).unwrap(), ) @@ -93,9 +66,9 @@ pub fn build_encoded_vaa_account_from_vaa( .unwrap(); Account { - lamports: Rent::default().minimum_balance(encoded_vaa_data.len()), - data: encoded_vaa_data, - owner: BRIDGE_ID, + lamports: Rent::default().minimum_balance(encoded_vaa_data.len()), + data: encoded_vaa_data, + owner: BRIDGE_ID, executable: false, rent_epoch: 0, } @@ -103,14 +76,14 @@ pub fn build_encoded_vaa_account_from_vaa( pub fn build_guardian_set_account(wrong_setup_option: WrongSetupOption) -> Account { let guardian_set = GuardianSet { - index: { + index: { if matches!(wrong_setup_option, WrongSetupOption::GuardianSetWrongIndex) { WRONG_GUARDIAN_SET_INDEX } else { DEFAULT_GUARDIAN_SET_INDEX } }, - keys: dummy_guardians() + keys: dummy_guardians() .iter() .map(|x| { let mut result: [u8; 20] = [0u8; 20]; @@ -120,7 +93,7 @@ pub fn build_guardian_set_account(wrong_setup_option: WrongSetupOption) -> Accou result }) .collect::>(), - creation_time: 0.into(), + creation_time: 0.into(), expiration_time: { if matches!(wrong_setup_option, WrongSetupOption::GuardianSetExpired) { 1 @@ -139,9 +112,9 @@ pub fn build_guardian_set_account(wrong_setup_option: WrongSetupOption) -> Accou .unwrap(); Account { - lamports: Rent::default().minimum_balance(guardian_set_data.len()), - data: guardian_set_data, - owner: BRIDGE_ID, + lamports: Rent::default().minimum_balance(guardian_set_data.len()), + data: guardian_set_data, + owner: BRIDGE_ID, executable: false, rent_epoch: 0, } @@ -187,7 +160,6 @@ pub async fn setup_pyth_receiver( let setup_keypair: Keypair = program_simulator.get_funded_keypair().await.unwrap(); let initial_config = default_receiver_config(setup_keypair.pubkey()); - program_simulator .process_ix_with_default_compute_limit( Initialize::populate(&setup_keypair.pubkey(), initial_config.clone()), diff --git a/target_chains/solana/program_simulator/src/lib.rs b/target_chains/solana/program_simulator/src/lib.rs index dfb1c70b92..a07ad88d32 100644 --- a/target_chains/solana/program_simulator/src/lib.rs +++ b/target_chains/solana/program_simulator/src/lib.rs @@ -2,41 +2,27 @@ use { borsh::BorshDeserialize, solana_program::{ hash::Hash, - instruction::{ - Instruction, - InstructionError, - }, + instruction::{Instruction, InstructionError}, native_token::LAMPORTS_PER_SOL, program_error::ProgramError, pubkey::Pubkey, system_instruction, }, - solana_program_test::{ - BanksClient, - BanksClientError, - ProgramTest, - ProgramTestBanksClientExt, - }, + solana_program_test::{BanksClient, BanksClientError, ProgramTest, ProgramTestBanksClientExt}, solana_sdk::{ clock::Clock, compute_budget, - signature::{ - Keypair, - Signer, - }, - transaction::{ - Transaction, - TransactionError, - }, + signature::{Keypair, Signer}, + transaction::{Transaction, TransactionError}, }, }; pub struct ProgramSimulator { - banks_client: BanksClient, + banks_client: BanksClient, /// Hash used to submit the last transaction. The hash must be advanced for each new /// transaction; otherwise, replayed transactions in different states can return stale /// results. - last_blockhash: Hash, + last_blockhash: Hash, genesis_keypair: Keypair, } diff --git a/target_chains/solana/programs/pyth-price-store/src/accounts.rs b/target_chains/solana/programs/pyth-price-store/src/accounts.rs index 0d70a47480..932a18bc8a 100644 --- a/target_chains/solana/programs/pyth-price-store/src/accounts.rs +++ b/target_chains/solana/programs/pyth-price-store/src/accounts.rs @@ -1,7 +1,4 @@ -use { - bytemuck::from_bytes, - std::mem::size_of, -}; +use {bytemuck::from_bytes, std::mem::size_of}; pub mod buffer; pub mod config; diff --git a/target_chains/solana/programs/pyth-price-store/src/accounts/buffer.rs b/target_chains/solana/programs/pyth-price-store/src/accounts/buffer.rs index 41ef8cc009..7dd0732932 100644 --- a/target_chains/solana/programs/pyth-price-store/src/accounts/buffer.rs +++ b/target_chains/solana/programs/pyth-price-store/src/accounts/buffer.rs @@ -1,16 +1,6 @@ use { - super::errors::{ - ExtendError, - PublisherPriceError, - ReadAccountError, - }, - bytemuck::{ - cast_slice, - from_bytes, - from_bytes_mut, - Pod, - Zeroable, - }, + super::errors::{ExtendError, PublisherPriceError, ReadAccountError}, + bytemuck::{cast_slice, from_bytes, from_bytes_mut, Pod, Zeroable}, std::mem::size_of, }; @@ -28,12 +18,12 @@ const FORMAT: u32 = 2848712303; #[repr(C, packed)] pub struct BufferHeader { /// Account magic to avoid account confusion. - pub format: u32, + pub format: u32, /// The publisher this buffer is associated with. - pub publisher: [u8; 32], + pub publisher: [u8; 32], /// The slot corresponding to all prices currently stored in the account. /// Determined by the clock value when `SubmitPrices` is called. - pub slot: u64, + pub slot: u64, /// The number of prices currently stored in the account. pub num_prices: u32, } @@ -55,8 +45,8 @@ pub struct BufferedPrice { // 4 high bits: trading status // 28 low bits: feed index pub trading_status_and_feed_index: u32, - pub price: i64, - pub confidence: u64, + pub price: i64, + pub confidence: u64, } impl BufferedPrice { diff --git a/target_chains/solana/programs/pyth-price-store/src/accounts/config.rs b/target_chains/solana/programs/pyth-price-store/src/accounts/config.rs index 2e5aab9f89..a4c97d2c2b 100644 --- a/target_chains/solana/programs/pyth-price-store/src/accounts/config.rs +++ b/target_chains/solana/programs/pyth-price-store/src/accounts/config.rs @@ -1,11 +1,6 @@ use { super::errors::ReadAccountError, - bytemuck::{ - from_bytes, - from_bytes_mut, - Pod, - Zeroable, - }, + bytemuck::{from_bytes, from_bytes_mut, Pod, Zeroable}, std::mem::size_of, }; @@ -21,7 +16,7 @@ pub fn format_matches(data: &[u8]) -> bool { #[repr(C, packed)] pub struct Config { /// Account magic to avoid account confusion. - pub format: u32, + pub format: u32, /// The signature of the authority account will be required to execute /// `InitializePublisher` instruction. pub authority: [u8; 32], diff --git a/target_chains/solana/programs/pyth-price-store/src/accounts/errors.rs b/target_chains/solana/programs/pyth-price-store/src/accounts/errors.rs index e5691bdabb..193093fe70 100644 --- a/target_chains/solana/programs/pyth-price-store/src/accounts/errors.rs +++ b/target_chains/solana/programs/pyth-price-store/src/accounts/errors.rs @@ -26,10 +26,7 @@ pub enum ExtendError { #[cfg(feature = "solana-program")] mod convert { - use { - super::*, - solana_program::program_error::ProgramError, - }; + use {super::*, solana_program::program_error::ProgramError}; impl From for ProgramError { fn from(value: ReadAccountError) -> Self { diff --git a/target_chains/solana/programs/pyth-price-store/src/accounts/publisher_config.rs b/target_chains/solana/programs/pyth-price-store/src/accounts/publisher_config.rs index 9e36247e58..15ddac0034 100644 --- a/target_chains/solana/programs/pyth-price-store/src/accounts/publisher_config.rs +++ b/target_chains/solana/programs/pyth-price-store/src/accounts/publisher_config.rs @@ -1,11 +1,6 @@ use { super::errors::ReadAccountError, - bytemuck::{ - from_bytes, - from_bytes_mut, - Pod, - Zeroable, - }, + bytemuck::{from_bytes, from_bytes_mut, Pod, Zeroable}, std::mem::size_of, }; @@ -21,10 +16,10 @@ pub fn format_matches(data: &[u8]) -> bool { #[repr(C, packed)] pub struct PublisherConfig { /// Account magic to avoid account confusion. - pub format: u32, + pub format: u32, /// The publisher this config is associated with. /// Always matches the pubkey used to derive the PDA pubkey. - pub publisher: [u8; 32], + pub publisher: [u8; 32], /// The publisher's buffer account. pub buffer_account: [u8; 32], } diff --git a/target_chains/solana/programs/pyth-price-store/src/instruction.rs b/target_chains/solana/programs/pyth-price-store/src/instruction.rs index 285e991e63..214c852a60 100644 --- a/target_chains/solana/programs/pyth-price-store/src/instruction.rs +++ b/target_chains/solana/programs/pyth-price-store/src/instruction.rs @@ -1,7 +1,4 @@ -use bytemuck::{ - Pod, - Zeroable, -}; +use bytemuck::{Pod, Zeroable}; /// Seed used to derive the config account. pub const CONFIG_SEED: &str = "CONFIG"; @@ -54,18 +51,18 @@ pub struct InitializeArgs { pub config_bump: u8, /// The signature of the authority account will be required to execute /// `InitializePublisher` instruction. - pub authority: [u8; 32], + pub authority: [u8; 32], } #[derive(Debug, Clone, Copy, Zeroable, Pod)] #[repr(C, packed)] pub struct InitializePublisherArgs { /// PDA bump of the config account. - pub config_bump: u8, + pub config_bump: u8, /// PDA bump of the publisher config account. pub publisher_config_bump: u8, /// The publisher to be initialized. - pub publisher: [u8; 32], + pub publisher: [u8; 32], } #[derive(Debug, Clone, Copy, Zeroable, Pod)] diff --git a/target_chains/solana/programs/pyth-price-store/src/processor.rs b/target_chains/solana/programs/pyth-price-store/src/processor.rs index ad2f706559..39ed8dc4a5 100644 --- a/target_chains/solana/programs/pyth-price-store/src/processor.rs +++ b/target_chains/solana/programs/pyth-price-store/src/processor.rs @@ -6,19 +6,14 @@ use { crate::{ ensure, instruction::{ - InitializeArgs, - InitializePublisherArgs, - Instruction, - SubmitPricesArgsHeader, + InitializeArgs, InitializePublisherArgs, Instruction, SubmitPricesArgsHeader, }, }, bytemuck::try_from_bytes, initialize::initialize, initialize_publisher::initialize_publisher, solana_program::{ - account_info::AccountInfo, - entrypoint::ProgramResult, - program_error::ProgramError, + account_info::AccountInfo, entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey, }, std::mem::size_of, diff --git a/target_chains/solana/programs/pyth-price-store/src/processor/initialize.rs b/target_chains/solana/programs/pyth-price-store/src/processor/initialize.rs index d852fdd255..6e32a84fe8 100644 --- a/target_chains/solana/programs/pyth-price-store/src/processor/initialize.rs +++ b/target_chains/solana/programs/pyth-price-store/src/processor/initialize.rs @@ -1,24 +1,12 @@ use { crate::{ accounts, - instruction::{ - InitializeArgs, - CONFIG_SEED, - }, - validate::{ - validate_config, - validate_payer, - validate_system, - }, + instruction::{InitializeArgs, CONFIG_SEED}, + validate::{validate_config, validate_payer, validate_system}, }, solana_program::{ - account_info::AccountInfo, - entrypoint::ProgramResult, - program::invoke_signed, - pubkey::Pubkey, - rent::Rent, - system_instruction, - sysvar::Sysvar, + account_info::AccountInfo, entrypoint::ProgramResult, program::invoke_signed, + pubkey::Pubkey, rent::Rent, system_instruction, sysvar::Sysvar, }, }; @@ -60,23 +48,14 @@ pub fn initialize( #[cfg(test)] mod tests { use { - crate::{ - accounts, - instruction::CONFIG_SEED, - }, + crate::{accounts, instruction::CONFIG_SEED}, solana_program::{ - instruction::{ - AccountMeta, - Instruction, - }, + instruction::{AccountMeta, Instruction}, pubkey::Pubkey, system_program, }, solana_program_test::*, - solana_sdk::{ - signature::Signer, - transaction::Transaction, - }, + solana_sdk::{signature::Signer, transaction::Transaction}, }; #[tokio::test] diff --git a/target_chains/solana/programs/pyth-price-store/src/processor/initialize_publisher.rs b/target_chains/solana/programs/pyth-price-store/src/processor/initialize_publisher.rs index 0a1aacf1f1..05165883bd 100644 --- a/target_chains/solana/programs/pyth-price-store/src/processor/initialize_publisher.rs +++ b/target_chains/solana/programs/pyth-price-store/src/processor/initialize_publisher.rs @@ -1,30 +1,16 @@ use { crate::{ - accounts::{ - buffer, - publisher_config, - }, + accounts::{buffer, publisher_config}, ensure, - instruction::{ - InitializePublisherArgs, - PUBLISHER_CONFIG_SEED, - }, + instruction::{InitializePublisherArgs, PUBLISHER_CONFIG_SEED}, validate::{ - validate_authority, - validate_buffer, - validate_config, - validate_publisher_config_for_init, - validate_system, + validate_authority, validate_buffer, validate_config, + validate_publisher_config_for_init, validate_system, }, }, solana_program::{ - account_info::AccountInfo, - entrypoint::ProgramResult, - program::invoke_signed, - program_error::ProgramError, - pubkey::Pubkey, - rent::Rent, - system_instruction, + account_info::AccountInfo, entrypoint::ProgramResult, program::invoke_signed, + program_error::ProgramError, pubkey::Pubkey, rent::Rent, system_instruction, sysvar::Sysvar, }, }; @@ -106,35 +92,20 @@ mod tests { crate::{ accounts::{ self, - buffer::{ - BufferHeader, - BufferedPrice, - }, - }, - instruction::{ - CONFIG_SEED, - PUBLISHER_CONFIG_SEED, + buffer::{BufferHeader, BufferedPrice}, }, + instruction::{CONFIG_SEED, PUBLISHER_CONFIG_SEED}, }, - bytemuck::{ - bytes_of, - cast_slice, - }, + bytemuck::{bytes_of, cast_slice}, solana_program::{ - instruction::{ - AccountMeta, - Instruction, - }, + instruction::{AccountMeta, Instruction}, pubkey::Pubkey, system_program, }, solana_program_test::*, solana_sdk::{ rent::Rent, - signature::{ - Keypair, - Signer, - }, + signature::{Keypair, Signer}, transaction::Transaction, }, }; diff --git a/target_chains/solana/programs/pyth-price-store/src/processor/submit_prices.rs b/target_chains/solana/programs/pyth-price-store/src/processor/submit_prices.rs index 5f521a78d5..cc4f01a49f 100644 --- a/target_chains/solana/programs/pyth-price-store/src/processor/submit_prices.rs +++ b/target_chains/solana/programs/pyth-price-store/src/processor/submit_prices.rs @@ -1,25 +1,13 @@ use { crate::{ - accounts::{ - buffer, - publisher_config, - }, + accounts::{buffer, publisher_config}, ensure, instruction::SubmitPricesArgsHeader, - validate::{ - validate_buffer, - validate_publisher, - validate_publisher_config_for_access, - }, + validate::{validate_buffer, validate_publisher, validate_publisher_config_for_access}, }, solana_program::{ - account_info::AccountInfo, - clock::Clock, - entrypoint::ProgramResult, - program_error::ProgramError, - program_memory::sol_memcmp, - pubkey::Pubkey, - sysvar::Sysvar, + account_info::AccountInfo, clock::Clock, entrypoint::ProgramResult, + program_error::ProgramError, program_memory::sol_memcmp, pubkey::Pubkey, sysvar::Sysvar, }, }; diff --git a/target_chains/solana/programs/pyth-price-store/src/validate.rs b/target_chains/solana/programs/pyth-price-store/src/validate.rs index 74e6682a13..ffc92326d4 100644 --- a/target_chains/solana/programs/pyth-price-store/src/validate.rs +++ b/target_chains/solana/programs/pyth-price-store/src/validate.rs @@ -1,18 +1,11 @@ use { crate::{ - accounts, - ensure, - instruction::{ - CONFIG_SEED, - PUBLISHER_CONFIG_SEED, - }, + accounts, ensure, + instruction::{CONFIG_SEED, PUBLISHER_CONFIG_SEED}, }, solana_program::{ - account_info::AccountInfo, - program_error::ProgramError, - program_memory::sol_memcmp, - pubkey::Pubkey, - system_program, + account_info::AccountInfo, program_error::ProgramError, program_memory::sol_memcmp, + pubkey::Pubkey, system_program, }, }; diff --git a/target_chains/solana/programs/pyth-push-oracle/src/lib.rs b/target_chains/solana/programs/pyth-push-oracle/src/lib.rs index 94d02696a9..b3af72bf89 100644 --- a/target_chains/solana/programs/pyth-push-oracle/src/lib.rs +++ b/target_chains/solana/programs/pyth-push-oracle/src/lib.rs @@ -1,17 +1,11 @@ use { anchor_lang::prelude::*, pyth_solana_receiver_sdk::{ - cpi::accounts::PostUpdate, - price_update::PriceUpdateV2, - program::PythSolanaReceiver, - PostUpdateParams, - PYTH_PUSH_ORACLE_ID, + cpi::accounts::PostUpdate, price_update::PriceUpdateV2, program::PythSolanaReceiver, + PostUpdateParams, PYTH_PUSH_ORACLE_ID, }, pythnet_sdk::{ - messages::{ - FeedId, - Message, - }, + messages::{FeedId, Message}, wire::from_slice, }, }; @@ -43,13 +37,13 @@ pub mod pyth_push_oracle { ) -> Result<()> { let cpi_program = ctx.accounts.pyth_solana_receiver.to_account_info().clone(); let cpi_accounts = PostUpdate { - payer: ctx.accounts.payer.to_account_info().clone(), - encoded_vaa: ctx.accounts.encoded_vaa.to_account_info().clone(), - config: ctx.accounts.config.to_account_info().clone(), - treasury: ctx.accounts.treasury.to_account_info().clone(), + payer: ctx.accounts.payer.to_account_info().clone(), + encoded_vaa: ctx.accounts.encoded_vaa.to_account_info().clone(), + config: ctx.accounts.config.to_account_info().clone(), + treasury: ctx.accounts.treasury.to_account_info().clone(), price_update_account: ctx.accounts.price_feed_account.to_account_info().clone(), - system_program: ctx.accounts.system_program.to_account_info().clone(), - write_authority: ctx.accounts.price_feed_account.to_account_info().clone(), + system_program: ctx.accounts.system_program.to_account_info().clone(), + write_authority: ctx.accounts.price_feed_account.to_account_info().clone(), }; let seeds = &[ @@ -111,17 +105,17 @@ pub mod pyth_push_oracle { #[instruction(params : PostUpdateParams, shard_id : u16, feed_id : FeedId)] pub struct UpdatePriceFeed<'info> { #[account(mut)] - pub payer: Signer<'info>, + pub payer: Signer<'info>, pub pyth_solana_receiver: Program<'info, PythSolanaReceiver>, /// CHECK: Checked by CPI into the Pyth Solana Receiver - pub encoded_vaa: AccountInfo<'info>, + pub encoded_vaa: AccountInfo<'info>, /// CHECK: Checked by CPI into the Pyth Solana Receiver - pub config: AccountInfo<'info>, + pub config: AccountInfo<'info>, /// CHECK: Checked by CPI into the Pyth Solana Receiver #[account(mut)] - pub treasury: AccountInfo<'info>, + pub treasury: AccountInfo<'info>, /// CHECK: This account's seeds are checked #[account(mut, seeds = [&shard_id.to_le_bytes(), &feed_id], bump)] - pub price_feed_account: AccountInfo<'info>, - pub system_program: Program<'info, System>, + pub price_feed_account: AccountInfo<'info>, + pub system_program: Program<'info, System>, } diff --git a/target_chains/solana/programs/pyth-push-oracle/src/sdk.rs b/target_chains/solana/programs/pyth-push-oracle/src/sdk.rs index 11841a409d..ca35042987 100644 --- a/target_chains/solana/programs/pyth-push-oracle/src/sdk.rs +++ b/target_chains/solana/programs/pyth-push-oracle/src/sdk.rs @@ -1,23 +1,8 @@ use { - crate::{ - accounts, - instruction, - PostUpdateParams, - ID, - }, - anchor_lang::{ - prelude::*, - system_program, - InstructionData, - }, - pyth_solana_receiver_sdk::pda::{ - get_config_address, - get_treasury_address, - }, - pythnet_sdk::{ - messages::FeedId, - wire::v1::MerklePriceUpdate, - }, + crate::{accounts, instruction, PostUpdateParams, ID}, + anchor_lang::{prelude::*, system_program, InstructionData}, + pyth_solana_receiver_sdk::pda::{get_config_address, get_treasury_address}, + pythnet_sdk::{messages::FeedId, wire::v1::MerklePriceUpdate}, solana_program::instruction::Instruction, }; @@ -59,8 +44,8 @@ impl instruction::UpdatePriceFeed { .to_account_metas(None); Instruction { program_id: ID, - accounts: update_price_feed_accounts, - data: instruction::UpdatePriceFeed { + accounts: update_price_feed_accounts, + data: instruction::UpdatePriceFeed { params: PostUpdateParams { merkle_price_update, treasury_id, diff --git a/target_chains/solana/programs/pyth-push-oracle/tests/test_update_price_feed.rs b/target_chains/solana/programs/pyth-push-oracle/tests/test_update_price_feed.rs index 62654152f1..24330c11c4 100644 --- a/target_chains/solana/programs/pyth-push-oracle/tests/test_update_price_feed.rs +++ b/target_chains/solana/programs/pyth-push-oracle/tests/test_update_price_feed.rs @@ -1,36 +1,21 @@ use { common_test_utils::{ - assert_treasury_balance, - setup_pyth_receiver, - ProgramTestFixtures, - WrongSetupOption, + assert_treasury_balance, setup_pyth_receiver, ProgramTestFixtures, WrongSetupOption, }, program_simulator::into_transaction_error, pyth_push_oracle::{ - instruction::UpdatePriceFeed, - sdk::get_price_feed_address, - PushOracleError, - }, - pyth_solana_receiver::sdk::{ - deserialize_accumulator_update_data, - DEFAULT_TREASURY_ID, - }, - pyth_solana_receiver_sdk::price_update::{ - PriceUpdateV2, - VerificationLevel, + instruction::UpdatePriceFeed, sdk::get_price_feed_address, PushOracleError, }, + pyth_solana_receiver::sdk::{deserialize_accumulator_update_data, DEFAULT_TREASURY_ID}, + pyth_solana_receiver_sdk::price_update::{PriceUpdateV2, VerificationLevel}, pythnet_sdk::{ messages::Message, test_utils::{ - create_accumulator_message, - create_dummy_feed_id, + create_accumulator_message, create_dummy_feed_id, create_dummy_price_feed_message_with_feed_id, }, }, - solana_sdk::{ - rent::Rent, - signer::Signer, - }, + solana_sdk::{rent::Rent, signer::Signer}, }; const DEFAULT_SHARD: u16 = 0; @@ -55,7 +40,6 @@ async fn test_update_price_feed() { ); let (vaa, merkle_price_updates) = deserialize_accumulator_update_data(message).unwrap(); - let ProgramTestFixtures { mut program_simulator, encoded_vaa_addresses, diff --git a/target_chains/solana/programs/pyth-solana-receiver/src/lib.rs b/target_chains/solana/programs/pyth-solana-receiver/src/lib.rs index 5c83458cb3..49eb3ff792 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/src/lib.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/src/lib.rs @@ -3,20 +3,10 @@ use { crate::error::ReceiverError, anchor_lang::prelude::*, pyth_solana_receiver_sdk::{ - config::{ - Config, - DataSource, - }, - pda::{ - CONFIG_SEED, - TREASURY_SEED, - }, - price_update::{ - PriceUpdateV2, - VerificationLevel, - }, - PostUpdateAtomicParams, - PostUpdateParams, + config::{Config, DataSource}, + pda::{CONFIG_SEED, TREASURY_SEED}, + price_update::{PriceUpdateV2, VerificationLevel}, + PostUpdateAtomicParams, PostUpdateParams, }, pythnet_sdk::{ accumulators::merkle::MerkleRoot, @@ -24,30 +14,18 @@ use { messages::Message, wire::{ from_slice, - v1::{ - WormholeMessage, - WormholePayload, - }, + v1::{WormholeMessage, WormholePayload}, }, }, solana_program::{ - keccak, - program_memory::sol_memcpy, - secp256k1_recover::secp256k1_recover, + keccak, program_memory::sol_memcpy, secp256k1_recover::secp256k1_recover, system_instruction, }, wormhole_core_bridge_solana::{ - sdk::{ - legacy::AccountVariant, - VaaAccount, - }, + sdk::{legacy::AccountVariant, VaaAccount}, state::GuardianSet, }, - wormhole_raw_vaas::{ - utils::quorum, - GuardianSetSig, - Vaa, - }, + wormhole_raw_vaas::{utils::quorum, GuardianSetSig, Vaa}, }; pub mod error; @@ -233,8 +211,8 @@ pub mod pyth_solana_receiver { let vaa_components = VaaComponents { verification_level: VerificationLevel::Full, - emitter_address: encoded_vaa.try_emitter_address()?, - emitter_chain: encoded_vaa.try_emitter_chain()?, + emitter_address: encoded_vaa.try_emitter_address()?, + emitter_chain: encoded_vaa.try_emitter_chain()?, }; post_price_update_from_vaa( @@ -260,9 +238,9 @@ pub mod pyth_solana_receiver { #[instruction(initial_config : Config)] pub struct Initialize<'info> { #[account(mut)] - pub payer: Signer<'info>, + pub payer: Signer<'info>, #[account(init, space = Config::LEN, payer=payer, seeds = [CONFIG_SEED.as_ref()], bump)] - pub config: Account<'info, Config>, + pub config: Account<'info, Config>, pub system_program: Program<'info, System>, } @@ -272,7 +250,7 @@ pub struct Governance<'info> { payer.key() == config.governance_authority @ ReceiverError::GovernanceAuthorityMismatch )] - pub payer: Signer<'info>, + pub payer: Signer<'info>, #[account(mut, seeds = [CONFIG_SEED.as_ref()], bump)] pub config: Account<'info, Config>, } @@ -283,7 +261,7 @@ pub struct AcceptGovernanceAuthorityTransfer<'info> { payer.key() == config.target_governance_authority.ok_or(error!(ReceiverError::NonexistentGovernanceAuthorityTransferRequest))? @ ReceiverError::TargetGovernanceAuthorityMismatch )] - pub payer: Signer<'info>, + pub payer: Signer<'info>, #[account(mut, seeds = [CONFIG_SEED.as_ref()], bump)] pub config: Account<'info, Config>, } @@ -292,50 +270,50 @@ pub struct AcceptGovernanceAuthorityTransfer<'info> { #[instruction(params: PostUpdateParams)] pub struct PostUpdate<'info> { #[account(mut)] - pub payer: Signer<'info>, + pub payer: Signer<'info>, #[account(owner = config.wormhole @ ReceiverError::WrongVaaOwner)] /// CHECK: We aren't deserializing the VAA here but later with VaaAccount::load, which is the recommended way - pub encoded_vaa: AccountInfo<'info>, + pub encoded_vaa: AccountInfo<'info>, #[account(seeds = [CONFIG_SEED.as_ref()], bump)] - pub config: Account<'info, Config>, + pub config: Account<'info, Config>, /// CHECK: This is just a PDA controlled by the program. There is currently no way to withdraw funds from it. #[account(mut, seeds = [TREASURY_SEED.as_ref(), &[params.treasury_id]], bump)] - pub treasury: AccountInfo<'info>, + pub treasury: AccountInfo<'info>, /// The constraint is such that either the price_update_account is uninitialized or the write_authority is the write_authority. /// Pubkey::default() is the SystemProgram on Solana and it can't sign so it's impossible that price_update_account.write_authority == Pubkey::default() once the account is initialized #[account(init_if_needed, constraint = price_update_account.write_authority == Pubkey::default() || price_update_account.write_authority == write_authority.key() @ ReceiverError::WrongWriteAuthority , payer =payer, space = PriceUpdateV2::LEN)] pub price_update_account: Account<'info, PriceUpdateV2>, - pub system_program: Program<'info, System>, - pub write_authority: Signer<'info>, + pub system_program: Program<'info, System>, + pub write_authority: Signer<'info>, } #[derive(Accounts)] #[instruction(params: PostUpdateAtomicParams)] pub struct PostUpdateAtomic<'info> { #[account(mut)] - pub payer: Signer<'info>, + pub payer: Signer<'info>, /// CHECK: We can't use AccountVariant:: here because its owner is hardcoded as the "official" Wormhole program and we want to get the wormhole address from the config. /// Instead we do the same steps in deserialize_guardian_set_checked. #[account( owner = config.wormhole @ ReceiverError::WrongGuardianSetOwner)] - pub guardian_set: AccountInfo<'info>, + pub guardian_set: AccountInfo<'info>, #[account(seeds = [CONFIG_SEED.as_ref()], bump)] - pub config: Account<'info, Config>, + pub config: Account<'info, Config>, #[account(mut, seeds = [TREASURY_SEED.as_ref(), &[params.treasury_id]], bump)] /// CHECK: This is just a PDA controlled by the program. There is currently no way to withdraw funds from it. - pub treasury: AccountInfo<'info>, + pub treasury: AccountInfo<'info>, /// The constraint is such that either the price_update_account is uninitialized or the write_authority is the write_authority. /// Pubkey::default() is the SystemProgram on Solana and it can't sign so it's impossible that price_update_account.write_authority == Pubkey::default() once the account is initialized #[account(init_if_needed, constraint = price_update_account.write_authority == Pubkey::default() || price_update_account.write_authority == write_authority.key() @ ReceiverError::WrongWriteAuthority, payer = payer, space = PriceUpdateV2::LEN)] pub price_update_account: Account<'info, PriceUpdateV2>, - pub system_program: Program<'info, System>, - pub write_authority: Signer<'info>, + pub system_program: Program<'info, System>, + pub write_authority: Signer<'info>, } #[derive(Accounts)] pub struct ReclaimRent<'info> { #[account(mut)] - pub payer: Signer<'info>, + pub payer: Signer<'info>, #[account(mut, close = payer, constraint = price_update_account.write_authority == payer.key() @ ReceiverError::WrongWriteAuthority)] pub price_update_account: Account<'info, PriceUpdateV2>, } @@ -372,8 +350,8 @@ fn deserialize_guardian_set_checked( struct VaaComponents { verification_level: VerificationLevel, - emitter_address: [u8; 32], - emitter_chain: u16, + emitter_address: [u8; 32], + emitter_chain: u16, } fn post_price_update_from_vaa<'info>( @@ -409,7 +387,7 @@ fn post_price_update_from_vaa<'info>( let valid_data_source = config.valid_data_sources.iter().any(|x| { *x == DataSource { - chain: vaa_components.emitter_chain, + chain: vaa_components.emitter_chain, emitter: Pubkey::from(vaa_components.emitter_address), } }); diff --git a/target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs b/target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs index 1d78484ccc..d89268b6d1 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/src/sdk.rs @@ -1,31 +1,12 @@ use { - crate::{ - accounts, - instruction, - ID, - }, - anchor_lang::{ - prelude::*, - system_program, - InstructionData, - }, + crate::{accounts, instruction, ID}, + anchor_lang::{prelude::*, system_program, InstructionData}, pyth_solana_receiver_sdk::{ - config::{ - Config, - DataSource, - }, - pda::{ - get_config_address, - get_treasury_address, - }, - PostUpdateAtomicParams, - PostUpdateParams, - }, - pythnet_sdk::wire::v1::{ - AccumulatorUpdateData, - MerklePriceUpdate, - Proof, + config::{Config, DataSource}, + pda::{get_config_address, get_treasury_address}, + PostUpdateAtomicParams, PostUpdateParams, }, + pythnet_sdk::wire::v1::{AccumulatorUpdateData, MerklePriceUpdate, Proof}, rand::Rng, solana_program::instruction::Instruction, wormhole_core_bridge_solana::state::GuardianSet, @@ -127,8 +108,8 @@ impl instruction::Initialize { pub fn populate(payer: &Pubkey, initial_config: Config) -> Instruction { Instruction { program_id: ID, - accounts: accounts::Initialize::populate(payer).to_account_metas(None), - data: instruction::Initialize { initial_config }.data(), + accounts: accounts::Initialize::populate(payer).to_account_metas(None), + data: instruction::Initialize { initial_config }.data(), } } } @@ -152,8 +133,8 @@ impl instruction::PostUpdate { .to_account_metas(None); Instruction { program_id: ID, - accounts: post_update_accounts, - data: instruction::PostUpdate { + accounts: post_update_accounts, + data: instruction::PostUpdate { params: PostUpdateParams { merkle_price_update, treasury_id, @@ -164,7 +145,6 @@ impl instruction::PostUpdate { } } - impl instruction::PostUpdateAtomic { pub fn populate( payer: Pubkey, @@ -187,8 +167,8 @@ impl instruction::PostUpdateAtomic { .to_account_metas(None); Instruction { program_id: ID, - accounts: post_update_accounts, - data: instruction::PostUpdateAtomic { + accounts: post_update_accounts, + data: instruction::PostUpdateAtomic { params: PostUpdateAtomicParams { vaa, merkle_price_update, @@ -200,14 +180,13 @@ impl instruction::PostUpdateAtomic { } } - impl instruction::SetDataSources { pub fn populate(payer: Pubkey, data_sources: Vec) -> Instruction { let governance_accounts = accounts::Governance::populate(payer).to_account_metas(None); Instruction { program_id: ID, - accounts: governance_accounts, - data: instruction::SetDataSources { + accounts: governance_accounts, + data: instruction::SetDataSources { valid_data_sources: data_sources, } .data(), @@ -220,8 +199,8 @@ impl instruction::SetFee { let governance_accounts = accounts::Governance::populate(payer).to_account_metas(None); Instruction { program_id: ID, - accounts: governance_accounts, - data: instruction::SetFee { + accounts: governance_accounts, + data: instruction::SetFee { single_update_fee_in_lamports: fee, } .data(), @@ -229,26 +208,24 @@ impl instruction::SetFee { } } - impl instruction::SetWormholeAddress { pub fn populate(payer: Pubkey, wormhole: Pubkey) -> Instruction { let governance_accounts = accounts::Governance::populate(payer).to_account_metas(None); Instruction { program_id: ID, - accounts: governance_accounts, - data: instruction::SetWormholeAddress { wormhole }.data(), + accounts: governance_accounts, + data: instruction::SetWormholeAddress { wormhole }.data(), } } } - impl instruction::SetMinimumSignatures { pub fn populate(payer: Pubkey, minimum_signatures: u8) -> Instruction { let governance_accounts = accounts::Governance::populate(payer).to_account_metas(None); Instruction { program_id: ID, - accounts: governance_accounts, - data: instruction::SetMinimumSignatures { minimum_signatures }.data(), + accounts: governance_accounts, + data: instruction::SetMinimumSignatures { minimum_signatures }.data(), } } } @@ -258,8 +235,8 @@ impl instruction::RequestGovernanceAuthorityTransfer { let governance_accounts = accounts::Governance::populate(payer).to_account_metas(None); Instruction { program_id: ID, - accounts: governance_accounts, - data: instruction::RequestGovernanceAuthorityTransfer { + accounts: governance_accounts, + data: instruction::RequestGovernanceAuthorityTransfer { target_governance_authority, } .data(), @@ -272,8 +249,8 @@ impl instruction::CancelGovernanceAuthorityTransfer { let governance_accounts = accounts::Governance::populate(payer).to_account_metas(None); Instruction { program_id: ID, - accounts: governance_accounts, - data: instruction::CancelGovernanceAuthorityTransfer.data(), + accounts: governance_accounts, + data: instruction::CancelGovernanceAuthorityTransfer.data(), } } } @@ -284,8 +261,8 @@ impl instruction::AcceptGovernanceAuthorityTransfer { accounts::AcceptGovernanceAuthorityTransfer::populate(payer).to_account_metas(None); Instruction { program_id: ID, - accounts: governance_accounts, - data: instruction::AcceptGovernanceAuthorityTransfer {}.data(), + accounts: governance_accounts, + data: instruction::AcceptGovernanceAuthorityTransfer {}.data(), } } } @@ -296,8 +273,8 @@ impl instruction::ReclaimRent { accounts::ReclaimRent::populate(payer, price_update_account).to_account_metas(None); Instruction { program_id: ID, - accounts: governance_accounts, - data: instruction::ReclaimRent {}.data(), + accounts: governance_accounts, + data: instruction::ReclaimRent {}.data(), } } } diff --git a/target_chains/solana/programs/pyth-solana-receiver/tests/test_governance.rs b/target_chains/solana/programs/pyth-solana-receiver/tests/test_governance.rs index 1147e4342f..1779eb4577 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/tests/test_governance.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/tests/test_governance.rs @@ -1,38 +1,23 @@ use { - common_test_utils::{ - setup_pyth_receiver, - ProgramTestFixtures, - WrongSetupOption, - }, + common_test_utils::{setup_pyth_receiver, ProgramTestFixtures, WrongSetupOption}, program_simulator::into_transaction_error, pyth_solana_receiver::{ error::ReceiverError, instruction::{ - AcceptGovernanceAuthorityTransfer, - CancelGovernanceAuthorityTransfer, - RequestGovernanceAuthorityTransfer, - SetDataSources, - SetFee, - SetMinimumSignatures, + AcceptGovernanceAuthorityTransfer, CancelGovernanceAuthorityTransfer, + RequestGovernanceAuthorityTransfer, SetDataSources, SetFee, SetMinimumSignatures, SetWormholeAddress, }, }, pyth_solana_receiver_sdk::{ - config::{ - Config, - DataSource, - }, + config::{Config, DataSource}, pda::get_config_address, }, pythnet_sdk::test_utils::SECONDARY_DATA_SOURCE, - solana_program::{ - native_token::LAMPORTS_PER_SOL, - pubkey::Pubkey, - }, + solana_program::{native_token::LAMPORTS_PER_SOL, pubkey::Pubkey}, solana_sdk::signer::Signer, }; - #[tokio::test] async fn test_governance() { let ProgramTestFixtures { @@ -49,18 +34,17 @@ async fn test_governance() { .unwrap(); let new_config = Config { - governance_authority: new_governance_authority.pubkey(), - target_governance_authority: None, - wormhole: Pubkey::new_unique(), - valid_data_sources: vec![DataSource { - chain: SECONDARY_DATA_SOURCE.chain.into(), + governance_authority: new_governance_authority.pubkey(), + target_governance_authority: None, + wormhole: Pubkey::new_unique(), + valid_data_sources: vec![DataSource { + chain: SECONDARY_DATA_SOURCE.chain.into(), emitter: Pubkey::from(SECONDARY_DATA_SOURCE.address.0), }], single_update_fee_in_lamports: LAMPORTS_PER_SOL, - minimum_signatures: 20, + minimum_signatures: 20, }; - // this authority is not allowed to do anything assert_eq!( program_simulator @@ -150,7 +134,6 @@ async fn test_governance() { initial_config ); - // Now start changing for real program_simulator .process_ix_with_default_compute_limit( @@ -330,7 +313,6 @@ async fn test_governance() { .await .unwrap(); - current_config = program_simulator .get_anchor_account_data::(get_config_address()) .await @@ -373,7 +355,6 @@ async fn test_governance() { into_transaction_error(ReceiverError::TargetGovernanceAuthorityMismatch) ); - // Undo the request program_simulator .process_ix_with_default_compute_limit( @@ -407,7 +388,6 @@ async fn test_governance() { new_config.minimum_signatures ); - // Redo the request program_simulator .process_ix_with_default_compute_limit( @@ -447,7 +427,6 @@ async fn test_governance() { new_config.minimum_signatures ); - // New authority can accept program_simulator .process_ix_with_default_compute_limit( diff --git a/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_price_update_from_vaa.rs b/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_price_update_from_vaa.rs index 1fb36f9a4b..e04ff105f8 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_price_update_from_vaa.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_price_update_from_vaa.rs @@ -1,51 +1,27 @@ use { common_test_utils::{ - assert_treasury_balance, - setup_pyth_receiver, - ProgramTestFixtures, - WrongSetupOption, + assert_treasury_balance, setup_pyth_receiver, ProgramTestFixtures, WrongSetupOption, DEFAULT_GUARDIAN_SET_INDEX, }, program_simulator::into_transaction_error, pyth_solana_receiver::{ error::ReceiverError, - instruction::{ - PostUpdateAtomic, - SetDataSources, - SetFee, - }, - sdk::{ - deserialize_accumulator_update_data, - DEFAULT_TREASURY_ID, - }, + instruction::{PostUpdateAtomic, SetDataSources, SetFee}, + sdk::{deserialize_accumulator_update_data, DEFAULT_TREASURY_ID}, }, pyth_solana_receiver_sdk::{ config::DataSource, - price_update::{ - PriceUpdateV2, - VerificationLevel, - }, + price_update::{PriceUpdateV2, VerificationLevel}, }, pythnet_sdk::{ messages::Message, test_utils::{ - create_accumulator_message, - create_dummy_price_feed_message, - create_dummy_twap_message, - trim_vaa_signatures, - DEFAULT_DATA_SOURCE, - SECONDARY_DATA_SOURCE, + create_accumulator_message, create_dummy_price_feed_message, create_dummy_twap_message, + trim_vaa_signatures, DEFAULT_DATA_SOURCE, SECONDARY_DATA_SOURCE, }, }, - solana_program::{ - native_token::LAMPORTS_PER_SOL, - pubkey::Pubkey, - }, - solana_sdk::{ - rent::Rent, - signature::Keypair, - signer::Signer, - }, + solana_program::{native_token::LAMPORTS_PER_SOL, pubkey::Pubkey}, + solana_sdk::{rent::Rent, signature::Keypair, signer::Signer}, wormhole_core_bridge_solana::ID as BRIDGE_ID, }; @@ -105,7 +81,6 @@ async fn test_invalid_update_message() { create_accumulator_message(&[&feed_1, &feed_2], &[&feed_1, &feed_2], false, true, None); let (vaa, merkle_price_updates) = deserialize_accumulator_update_data(message).unwrap(); - let ProgramTestFixtures { mut program_simulator, encoded_vaa_addresses: _, @@ -143,7 +118,6 @@ async fn test_invalid_update_message() { ); } - #[tokio::test] async fn test_post_price_update_from_vaa() { let feed_1 = create_dummy_price_feed_message(100); @@ -228,14 +202,13 @@ async fn test_post_price_update_from_vaa() { into_transaction_error(ReceiverError::UnsupportedMessageType) ); - // change the data source program_simulator .process_ix_with_default_compute_limit( SetDataSources::populate( governance_authority.pubkey(), vec![DataSource { - chain: SECONDARY_DATA_SOURCE.chain.into(), + chain: SECONDARY_DATA_SOURCE.chain.into(), emitter: Pubkey::from(DEFAULT_DATA_SOURCE.address.0), }], ), @@ -274,7 +247,7 @@ async fn test_post_price_update_from_vaa() { SetDataSources::populate( governance_authority.pubkey(), vec![DataSource { - chain: DEFAULT_DATA_SOURCE.chain.into(), + chain: DEFAULT_DATA_SOURCE.chain.into(), emitter: Pubkey::from(SECONDARY_DATA_SOURCE.address.0), }], ), @@ -284,7 +257,6 @@ async fn test_post_price_update_from_vaa() { .await .unwrap(); - assert_eq!( program_simulator .process_ix_with_default_compute_limit( @@ -313,7 +285,7 @@ async fn test_post_price_update_from_vaa() { SetDataSources::populate( governance_authority.pubkey(), vec![DataSource { - chain: DEFAULT_DATA_SOURCE.chain.into(), + chain: DEFAULT_DATA_SOURCE.chain.into(), emitter: Pubkey::from(DEFAULT_DATA_SOURCE.address.0), }], ), @@ -413,7 +385,6 @@ async fn test_post_price_update_from_vaa() { program_simulator.get_clock().await.unwrap().slot ); - // Now change the fee! program_simulator .process_ix_with_default_compute_limit( @@ -479,7 +450,6 @@ async fn test_post_price_update_from_vaa() { program_simulator.get_clock().await.unwrap().slot ); - // Airdrop more program_simulator .airdrop(&poster.pubkey(), LAMPORTS_PER_SOL) diff --git a/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates.rs b/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates.rs index e273a73a4c..9161153477 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates.rs @@ -1,43 +1,22 @@ use { common_test_utils::{ - assert_treasury_balance, - setup_pyth_receiver, - ProgramTestFixtures, - WrongSetupOption, + assert_treasury_balance, setup_pyth_receiver, ProgramTestFixtures, WrongSetupOption, }, program_simulator::into_transaction_error, pyth_solana_receiver::{ error::ReceiverError, - instruction::{ - PostUpdate, - ReclaimRent, - }, - sdk::{ - deserialize_accumulator_update_data, - get_random_treasury_id, - DEFAULT_TREASURY_ID, - }, - }, - pyth_solana_receiver_sdk::price_update::{ - PriceUpdateV2, - VerificationLevel, + instruction::{PostUpdate, ReclaimRent}, + sdk::{deserialize_accumulator_update_data, get_random_treasury_id, DEFAULT_TREASURY_ID}, }, + pyth_solana_receiver_sdk::price_update::{PriceUpdateV2, VerificationLevel}, pythnet_sdk::{ messages::Message, - test_utils::{ - create_accumulator_message, - create_dummy_price_feed_message, - }, + test_utils::{create_accumulator_message, create_dummy_price_feed_message}, }, solana_program::pubkey::Pubkey, - solana_sdk::{ - rent::Rent, - signature::Keypair, - signer::Signer, - }, + solana_sdk::{rent::Rent, signature::Keypair, signer::Signer}, }; - #[tokio::test] async fn test_post_update() { let feed_1 = create_dummy_price_feed_message(100); @@ -46,7 +25,6 @@ async fn test_post_update() { create_accumulator_message(&[&feed_1, &feed_2], &[&feed_1, &feed_2], false, false, None); let (vaa, merkle_price_updates) = deserialize_accumulator_update_data(message).unwrap(); - let ProgramTestFixtures { mut program_simulator, encoded_vaa_addresses, @@ -148,7 +126,6 @@ async fn test_post_update() { program_simulator.get_clock().await.unwrap().slot ); - // This poster doesn't have the write authority let poster_2 = program_simulator.get_funded_keypair().await.unwrap(); assert_eq!( diff --git a/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates_atomic.rs b/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates_atomic.rs index 8eb811cbf9..7ca0227aea 100644 --- a/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates_atomic.rs +++ b/target_chains/solana/programs/pyth-solana-receiver/tests/test_post_updates_atomic.rs @@ -1,9 +1,6 @@ use { common_test_utils::{ - assert_treasury_balance, - setup_pyth_receiver, - ProgramTestFixtures, - WrongSetupOption, + assert_treasury_balance, setup_pyth_receiver, ProgramTestFixtures, WrongSetupOption, DEFAULT_GUARDIAN_SET_INDEX, }, program_simulator::into_transaction_error, @@ -11,35 +8,23 @@ use { error::ReceiverError, instruction::PostUpdateAtomic, sdk::{ - deserialize_accumulator_update_data, - get_guardian_set_address, - DEFAULT_TREASURY_ID, + deserialize_accumulator_update_data, get_guardian_set_address, DEFAULT_TREASURY_ID, SECONDARY_TREASURY_ID, }, }, - pyth_solana_receiver_sdk::price_update::{ - PriceUpdateV2, - VerificationLevel, - }, + pyth_solana_receiver_sdk::price_update::{PriceUpdateV2, VerificationLevel}, pythnet_sdk::{ messages::Message, test_utils::{ - create_accumulator_message, - create_dummy_price_feed_message, - trim_vaa_signatures, + create_accumulator_message, create_dummy_price_feed_message, trim_vaa_signatures, }, }, serde_wormhole::RawMessage, - solana_sdk::{ - rent::Rent, - signature::Keypair, - signer::Signer, - }, + solana_sdk::{rent::Rent, signature::Keypair, signer::Signer}, wormhole_core_bridge_solana::ID as BRIDGE_ID, wormhole_sdk::Vaa, }; - #[tokio::test] async fn test_post_update_atomic() { let feed_1 = create_dummy_price_feed_message(100); @@ -275,7 +260,6 @@ async fn test_post_update_atomic_wrong_vaa() { into_transaction_error(ReceiverError::InsufficientGuardianSignatures) ); - let mut vaa_copy: Vaa<&RawMessage> = serde_wormhole::from_slice(&vaa).unwrap(); vaa_copy.version = 0; @@ -351,7 +335,6 @@ async fn test_post_update_atomic_wrong_vaa() { into_transaction_error(ReceiverError::InvalidGuardianOrder) ); - let mut vaa_copy: Vaa<&RawMessage> = serde_wormhole::from_slice(&vaa).unwrap(); vaa_copy.signatures[0].index = 20; @@ -402,7 +385,6 @@ async fn test_post_update_atomic_wrong_vaa() { into_transaction_error(ReceiverError::InvalidSignature) ); - let mut vaa_copy: Vaa<&RawMessage> = serde_wormhole::from_slice(&vaa).unwrap(); vaa_copy.signatures[0].signature = vaa_copy.signatures[1].signature; @@ -455,7 +437,6 @@ async fn test_post_update_atomic_wrong_vaa() { ); } - #[tokio::test] async fn test_post_update_atomic_wrong_setup() { let feed_1 = create_dummy_price_feed_message(100); @@ -493,7 +474,6 @@ async fn test_post_update_atomic_wrong_setup() { into_transaction_error(ReceiverError::InvalidGuardianSetPda) ); - let ProgramTestFixtures { mut program_simulator, encoded_vaa_addresses: _, diff --git a/target_chains/solana/pyth_solana_receiver_sdk/src/config.rs b/target_chains/solana/pyth_solana_receiver_sdk/src/config.rs index 49d3c7d764..ea04370876 100644 --- a/target_chains/solana/pyth_solana_receiver_sdk/src/config.rs +++ b/target_chains/solana/pyth_solana_receiver_sdk/src/config.rs @@ -1,22 +1,19 @@ -use { - anchor_lang::prelude::*, - solana_program::pubkey::Pubkey, -}; +use {anchor_lang::prelude::*, solana_program::pubkey::Pubkey}; #[account] #[derive(Debug, PartialEq)] pub struct Config { - pub governance_authority: Pubkey, // This authority can update the other fields - pub target_governance_authority: Option, // This field is used for a two-step governance authority transfer - pub wormhole: Pubkey, // The address of the wormhole receiver - pub valid_data_sources: Vec, // The list of valid data sources for oracle price updates - pub single_update_fee_in_lamports: u64, // The fee in lamports for a single price update - pub minimum_signatures: u8, // The minimum number of signatures required to accept a VAA + pub governance_authority: Pubkey, // This authority can update the other fields + pub target_governance_authority: Option, // This field is used for a two-step governance authority transfer + pub wormhole: Pubkey, // The address of the wormhole receiver + pub valid_data_sources: Vec, // The list of valid data sources for oracle price updates + pub single_update_fee_in_lamports: u64, // The fee in lamports for a single price update + pub minimum_signatures: u8, // The minimum number of signatures required to accept a VAA } #[derive(AnchorSerialize, AnchorDeserialize, Clone, PartialEq, Debug)] pub struct DataSource { - pub chain: u16, + pub chain: u16, pub emitter: Pubkey, } @@ -29,31 +26,28 @@ pub mod tests { use { super::DataSource, crate::config::Config, - anchor_lang::{ - AnchorSerialize, - Discriminator, - }, + anchor_lang::{AnchorSerialize, Discriminator}, solana_program::pubkey::Pubkey, }; #[test] fn check_size() { let test_config = Config { - governance_authority: Pubkey::new_unique(), - target_governance_authority: Some(Pubkey::new_unique()), - wormhole: Pubkey::new_unique(), - valid_data_sources: vec![ + governance_authority: Pubkey::new_unique(), + target_governance_authority: Some(Pubkey::new_unique()), + wormhole: Pubkey::new_unique(), + valid_data_sources: vec![ DataSource { - chain: 1, + chain: 1, emitter: Pubkey::new_unique(), }, DataSource { - chain: 2, + chain: 2, emitter: Pubkey::new_unique(), }, ], single_update_fee_in_lamports: 0, - minimum_signatures: 0, + minimum_signatures: 0, }; assert_eq!( diff --git a/target_chains/solana/pyth_solana_receiver_sdk/src/cpi/accounts.rs b/target_chains/solana/pyth_solana_receiver_sdk/src/cpi/accounts.rs index 78f93bea48..ffd3078c04 100644 --- a/target_chains/solana/pyth_solana_receiver_sdk/src/cpi/accounts.rs +++ b/target_chains/solana/pyth_solana_receiver_sdk/src/cpi/accounts.rs @@ -1,16 +1,16 @@ // This file was populated with the expanded macros of programs/pyth-solana-receiver/src/lib.rs pub struct PostUpdateAtomic<'info> { - pub payer: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub payer: anchor_lang::solana_program::account_info::AccountInfo<'info>, ///Instead we do the same steps in deserialize_guardian_set_checked. - pub guardian_set: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub config: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub treasury: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub guardian_set: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub config: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub treasury: anchor_lang::solana_program::account_info::AccountInfo<'info>, ///The constraint is such that either the price_update_account is uninitialized or the write_authority is the write_authority. ///Pubkey::default() is the SystemProgram on Solana and it can't sign so it's impossible that price_update_account.write_authority == Pubkey::default() once the account is initialized pub price_update_account: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub system_program: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub write_authority: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub system_program: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub write_authority: anchor_lang::solana_program::account_info::AccountInfo<'info>, } #[automatically_derived] impl<'info> anchor_lang::ToAccountMetas for PostUpdateAtomic<'info> { @@ -86,15 +86,15 @@ impl<'info> anchor_lang::ToAccountInfos<'info> for PostUpdateAtomic<'info> { } pub struct PostUpdate<'info> { - pub payer: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub encoded_vaa: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub config: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub treasury: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub payer: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub encoded_vaa: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub config: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub treasury: anchor_lang::solana_program::account_info::AccountInfo<'info>, ///The constraint is such that either the price_update_account is uninitialized or the write_authority is the write_authority. ///Pubkey::default() is the SystemProgram on Solana and it can't sign so it's impossible that price_update_account.write_authority == Pubkey::default() once the account is initialized pub price_update_account: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub system_program: anchor_lang::solana_program::account_info::AccountInfo<'info>, - pub write_authority: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub system_program: anchor_lang::solana_program::account_info::AccountInfo<'info>, + pub write_authority: anchor_lang::solana_program::account_info::AccountInfo<'info>, } #[automatically_derived] impl<'info> anchor_lang::ToAccountMetas for PostUpdate<'info> { diff --git a/target_chains/solana/pyth_solana_receiver_sdk/src/cpi/mod.rs b/target_chains/solana/pyth_solana_receiver_sdk/src/cpi/mod.rs index ec9ea90a09..44c870b102 100644 --- a/target_chains/solana/pyth_solana_receiver_sdk/src/cpi/mod.rs +++ b/target_chains/solana/pyth_solana_receiver_sdk/src/cpi/mod.rs @@ -1,12 +1,6 @@ use { - self::accounts::{ - PostUpdate, - PostUpdateAtomic, - }, - crate::{ - PostUpdateAtomicParams, - PostUpdateParams, - }, + self::accounts::{PostUpdate, PostUpdateAtomic}, + crate::{PostUpdateAtomicParams, PostUpdateParams}, anchor_lang::prelude::*, }; diff --git a/target_chains/solana/pyth_solana_receiver_sdk/src/lib.rs b/target_chains/solana/pyth_solana_receiver_sdk/src/lib.rs index 2ca231e447..31b4fe6e16 100644 --- a/target_chains/solana/pyth_solana_receiver_sdk/src/lib.rs +++ b/target_chains/solana/pyth_solana_receiver_sdk/src/lib.rs @@ -1,17 +1,8 @@ use { - anchor_lang::{ - declare_id, - prelude::*, - }, - borsh::{ - BorshDeserialize, - BorshSerialize, - }, + anchor_lang::{declare_id, prelude::*}, + borsh::{BorshDeserialize, BorshSerialize}, pythnet_sdk::wire::v1::MerklePriceUpdate, - solana_program::{ - pubkey, - pubkey::Pubkey, - }, + solana_program::{pubkey, pubkey::Pubkey}, }; pub mod config; @@ -28,12 +19,12 @@ pub const PYTH_PUSH_ORACLE_ID: Pubkey = pubkey!("pythWSnswVUd12oZpeFP8e9CVaEqJg2 #[derive(Debug, BorshSerialize, BorshDeserialize, Clone)] pub struct PostUpdateParams { pub merkle_price_update: MerklePriceUpdate, - pub treasury_id: u8, + pub treasury_id: u8, } #[derive(Debug, BorshSerialize, BorshDeserialize, Clone)] pub struct PostUpdateAtomicParams { - pub vaa: Vec, + pub vaa: Vec, pub merkle_price_update: MerklePriceUpdate, - pub treasury_id: u8, + pub treasury_id: u8, } diff --git a/target_chains/solana/pyth_solana_receiver_sdk/src/pda.rs b/target_chains/solana/pyth_solana_receiver_sdk/src/pda.rs index a6ba1852db..266993d38b 100644 --- a/target_chains/solana/pyth_solana_receiver_sdk/src/pda.rs +++ b/target_chains/solana/pyth_solana_receiver_sdk/src/pda.rs @@ -1,7 +1,4 @@ -use { - crate::ID, - anchor_lang::prelude::*, -}; +use {crate::ID, anchor_lang::prelude::*}; pub const CONFIG_SEED: &str = "config"; pub const TREASURY_SEED: &str = "treasury"; diff --git a/target_chains/solana/pyth_solana_receiver_sdk/src/price_update.rs b/target_chains/solana/pyth_solana_receiver_sdk/src/price_update.rs index 405e1f253b..877333eecd 100644 --- a/target_chains/solana/pyth_solana_receiver_sdk/src/price_update.rs +++ b/target_chains/solana/pyth_solana_receiver_sdk/src/price_update.rs @@ -1,20 +1,10 @@ -pub use pythnet_sdk::messages::{ - FeedId, - PriceFeedMessage, -}; +pub use pythnet_sdk::messages::{FeedId, PriceFeedMessage}; use { - crate::{ - check, - error::GetPriceError, - }, - anchor_lang::prelude::{ - borsh::BorshSchema, - *, - }, + crate::{check, error::GetPriceError}, + anchor_lang::prelude::{borsh::BorshSchema, *}, solana_program::pubkey::Pubkey, }; - /// Pyth price updates are bridged to all blockchains via Wormhole. /// Using the price updates on another chain requires verifying the signatures of the Wormhole guardians. /// The usual process is to check the signatures for two thirds of the total number of guardians, but this can be cumbersome on Solana because of the transaction size limits, @@ -60,10 +50,10 @@ impl VerificationLevel { #[account] #[derive(BorshSchema)] pub struct PriceUpdateV2 { - pub write_authority: Pubkey, + pub write_authority: Pubkey, pub verification_level: VerificationLevel, - pub price_message: PriceFeedMessage, - pub posted_slot: u64, + pub price_message: PriceFeedMessage, + pub posted_slot: u64, } impl PriceUpdateV2 { @@ -74,9 +64,9 @@ impl PriceUpdateV2 { /// The actual price is `(price ± conf)* 10^exponent`. `publish_time` may be used to check the recency of the price. #[derive(PartialEq, Debug, Clone, Copy)] pub struct Price { - pub price: i64, - pub conf: u64, - pub exponent: i32, + pub price: i64, + pub conf: u64, + pub exponent: i32, pub publish_time: i64, } @@ -98,9 +88,9 @@ impl PriceUpdateV2 { GetPriceError::MismatchedFeedId ); Ok(Price { - price: self.price_message.price, - conf: self.price_message.conf, - exponent: self.price_message.exponent, + price: self.price_message.price, + conf: self.price_message.conf, + exponent: self.price_message.exponent, publish_time: self.price_message.publish_time, }) } @@ -220,19 +210,11 @@ pub mod tests { use { crate::{ error::GetPriceError, - price_update::{ - Price, - PriceUpdateV2, - VerificationLevel, - }, + price_update::{Price, PriceUpdateV2, VerificationLevel}, }, anchor_lang::Discriminator, pythnet_sdk::messages::PriceFeedMessage, - solana_program::{ - borsh0_10, - clock::Clock, - pubkey::Pubkey, - }, + solana_program::{borsh0_10, clock::Clock, pubkey::Pubkey}, }; #[test] @@ -285,9 +267,9 @@ pub mod tests { #[test] fn get_price() { let expected_price = Price { - price: 1, - conf: 2, - exponent: 3, + price: 1, + conf: 2, + exponent: 3, publish_time: 900, }; @@ -299,9 +281,9 @@ pub mod tests { }; let price_update_unverified = PriceUpdateV2 { - write_authority: Pubkey::new_unique(), + write_authority: Pubkey::new_unique(), verification_level: VerificationLevel::Partial { num_signatures: 0 }, - price_message: PriceFeedMessage { + price_message: PriceFeedMessage { feed_id, ema_conf: 0, ema_price: 0, @@ -311,13 +293,13 @@ pub mod tests { prev_publish_time: 899, publish_time: 900, }, - posted_slot: 0, + posted_slot: 0, }; let price_update_partially_verified = PriceUpdateV2 { - write_authority: Pubkey::new_unique(), + write_authority: Pubkey::new_unique(), verification_level: VerificationLevel::Partial { num_signatures: 5 }, - price_message: PriceFeedMessage { + price_message: PriceFeedMessage { feed_id, ema_conf: 0, ema_price: 0, @@ -327,13 +309,13 @@ pub mod tests { prev_publish_time: 899, publish_time: 900, }, - posted_slot: 0, + posted_slot: 0, }; let price_update_fully_verified = PriceUpdateV2 { - write_authority: Pubkey::new_unique(), + write_authority: Pubkey::new_unique(), verification_level: VerificationLevel::Full, - price_message: PriceFeedMessage { + price_message: PriceFeedMessage { feed_id, ema_conf: 0, ema_price: 0, @@ -343,10 +325,9 @@ pub mod tests { prev_publish_time: 899, publish_time: 900, }, - posted_slot: 0, + posted_slot: 0, }; - assert_eq!( price_update_unverified.get_price_unchecked(&feed_id), Ok(expected_price) diff --git a/wormhole_attester/sdk/rust/src/lib.rs b/wormhole_attester/sdk/rust/src/lib.rs index 62c280b143..4bd5d02d4c 100644 --- a/wormhole_attester/sdk/rust/src/lib.rs +++ b/wormhole_attester/sdk/rust/src/lib.rs @@ -6,31 +6,15 @@ //! similar human-readable names and provide a failsafe for some of //! the probable adversarial scenarios. -pub use pyth_sdk::{ - Identifier, - PriceStatus, - UnixTimestamp, -}; +pub use pyth_sdk::{Identifier, PriceStatus, UnixTimestamp}; #[cfg(feature = "solana")] use { pyth_sdk_solana::state::PriceAccount, - solitaire::{ - Derive, - Info, - }, + solitaire::{Derive, Info}, }; use { - serde::{ - Deserialize, - Serialize, - Serializer, - }, - std::{ - convert::TryInto, - io::Read, - iter::Iterator, - mem, - }, + serde::{Deserialize, Serialize, Serializer}, + std::{convert::TryInto, io::Read, iter::Iterator, mem}, }; pub type ErrBox = Box; @@ -64,7 +48,7 @@ pub type P2WEmitter<'b> = Derive, "p2w-emitter">; /// Decides the format of following bytes #[repr(u8)] pub enum PayloadId { - PriceAttestation = 1, // Not in use, currently batch attestations imply PriceAttestation messages inside + PriceAttestation = 1, // Not in use, currently batch attestations imply PriceAttestation messages inside PriceBatchAttestation = 2, } @@ -80,28 +64,28 @@ pub enum PayloadId { #[serde(rename_all = "camelCase")] pub struct PriceAttestation { #[serde(serialize_with = "pubkey_to_hex")] - pub product_id: Identifier, + pub product_id: Identifier, #[serde(serialize_with = "pubkey_to_hex")] - pub price_id: Identifier, + pub price_id: Identifier, #[serde(serialize_with = "use_to_string")] - pub price: i64, + pub price: i64, #[serde(serialize_with = "use_to_string")] - pub conf: u64, - pub expo: i32, + pub conf: u64, + pub expo: i32, #[serde(serialize_with = "use_to_string")] - pub ema_price: i64, + pub ema_price: i64, #[serde(serialize_with = "use_to_string")] - pub ema_conf: u64, - pub status: PriceStatus, - pub num_publishers: u32, - pub max_num_publishers: u32, - pub attestation_time: UnixTimestamp, - pub publish_time: UnixTimestamp, - pub prev_publish_time: UnixTimestamp, + pub ema_conf: u64, + pub status: PriceStatus, + pub num_publishers: u32, + pub max_num_publishers: u32, + pub attestation_time: UnixTimestamp, + pub publish_time: UnixTimestamp, + pub prev_publish_time: UnixTimestamp, #[serde(serialize_with = "use_to_string")] - pub prev_price: i64, + pub prev_price: i64, #[serde(serialize_with = "use_to_string")] - pub prev_conf: u64, + pub prev_conf: u64, pub last_attested_publish_time: UnixTimestamp, } @@ -276,7 +260,6 @@ impl BatchPriceAttestation { } } - // On-chain data types impl PriceAttestation { @@ -499,30 +482,27 @@ impl PriceAttestation { /// using `cargo test -- --nocapture`. #[cfg(test)] mod tests { - use { - super::*, - pyth_sdk_solana::state::PriceStatus, - }; + use {super::*, pyth_sdk_solana::state::PriceStatus}; fn mock_attestation(prod: Option<[u8; 32]>, price: Option<[u8; 32]>) -> PriceAttestation { let product_id_bytes = prod.unwrap_or([21u8; 32]); let price_id_bytes = price.unwrap_or([222u8; 32]); PriceAttestation { - product_id: Identifier::new(product_id_bytes), - price_id: Identifier::new(price_id_bytes), - price: 0x2bad2feed7, - conf: 101, - ema_price: -42, - ema_conf: 42, - expo: -3, - status: PriceStatus::Trading, - num_publishers: 123212u32, - max_num_publishers: 321232u32, - attestation_time: (0xdeadbeeffadedeedu64) as i64, - publish_time: 0xdadebeefi64, - prev_publish_time: 0xdeadbabei64, - prev_price: 0xdeadfacebeefi64, - prev_conf: 0xbadbadbeefu64, // I could do this all day -SD + product_id: Identifier::new(product_id_bytes), + price_id: Identifier::new(price_id_bytes), + price: 0x2bad2feed7, + conf: 101, + ema_price: -42, + ema_conf: 42, + expo: -3, + status: PriceStatus::Trading, + num_publishers: 123212u32, + max_num_publishers: 321232u32, + attestation_time: (0xdeadbeeffadedeedu64) as i64, + publish_time: 0xdadebeefi64, + prev_publish_time: 0xdeadbabei64, + prev_price: 0xdeadfacebeefi64, + prev_conf: 0xbadbadbeefu64, // I could do this all day -SD last_attested_publish_time: (0xdeadbeeffadedeafu64) as i64, } }