Skip to content

Commit

Permalink
Refactor TxSigner
Browse files Browse the repository at this point in the history
  • Loading branch information
lxfind committed Dec 16, 2024
1 parent 963754c commit c04fc3a
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 178 deletions.
4 changes: 3 additions & 1 deletion src/benchmarks/kms_stress.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::tx_signer::{SidecarTxSigner, TxSigner};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use sui_types::base_types::{random_object_ref, SuiAddress};
use sui_types::transaction::{ProgrammableTransaction, TransactionData, TransactionKind};

use crate::tx_signer::sidecar_signer::SidecarTxSigner;
use crate::tx_signer::TxSignerTrait;

pub async fn run_kms_stress_test(kms_url: String, num_tasks: usize) {
let signer = SidecarTxSigner::new(kms_url).await;
let test_tx_data = TransactionData::new(
Expand Down
40 changes: 31 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::tx_signer::{SidecarTxSigner, TestTxSigner, TxSigner};
use crate::tx_signer::in_memory_signer::InMemoryTxSigner;
use crate::tx_signer::sidecar_signer::SidecarTxSigner;
use crate::tx_signer::{TxSigner, TxSignerTrait};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use std::net::Ipv4Addr;
Expand Down Expand Up @@ -82,7 +84,15 @@ impl Default for GasPoolStorageConfig {
pub enum TxSignerConfig {
Local { keypair: SuiKeyPair },
Sidecar { sidecar_url: String },
MultiSidecar { sidecar_urls: Vec<String> },
MultiSigner { signers: Vec<SingleSignerType> },
}

#[serde_as]
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum SingleSignerType {
Local { keypair: SuiKeyPair },
Sidecar { sidecar_url: String },
}

impl Default for TxSignerConfig {
Expand All @@ -95,16 +105,28 @@ impl Default for TxSignerConfig {
}

impl TxSignerConfig {
pub async fn new_signer(self) -> Arc<dyn TxSigner> {
match self {
TxSignerConfig::Local { keypair } => TestTxSigner::new(keypair),
pub async fn new_signer(self) -> Arc<TxSigner> {
let all_signers: Vec<Arc<dyn TxSignerTrait>> = match self {
TxSignerConfig::Local { keypair } => vec![InMemoryTxSigner::new(keypair)],
TxSignerConfig::Sidecar { sidecar_url } => {
SidecarTxSigner::new(vec![sidecar_url]).await
vec![SidecarTxSigner::new(sidecar_url).await]
}
TxSignerConfig::MultiSidecar { sidecar_urls } => {
SidecarTxSigner::new(sidecar_urls).await
TxSignerConfig::MultiSigner { signers } => {
let mut all_signers: Vec<Arc<dyn TxSignerTrait>> = Vec::new();
for signer_config in signers {
match signer_config {
SingleSignerType::Local { keypair } => {
all_signers.push(InMemoryTxSigner::new(keypair))
}
SingleSignerType::Sidecar { sidecar_url } => {
all_signers.push(SidecarTxSigner::new(sidecar_url).await)
}
}
}
all_signers
}
}
};
TxSigner::new(all_signers)
}
}

Expand Down
17 changes: 6 additions & 11 deletions src/gas_pool/gas_pool_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct GasPoolContainer {
}

pub struct GasPool {
signer: Arc<dyn TxSigner>,
signer: Arc<TxSigner>,
gas_pool_store: Arc<dyn Storage>,
sui_client: SuiClient,
metrics: Arc<GasPoolCoreMetrics>,
Expand All @@ -43,7 +43,7 @@ pub struct GasPool {

impl GasPool {
pub async fn new(
signer: Arc<dyn TxSigner>,
signer: Arc<TxSigner>,
gas_pool_store: Arc<dyn Storage>,
sui_client: SuiClient,
metrics: Arc<GasPoolCoreMetrics>,
Expand Down Expand Up @@ -274,19 +274,14 @@ impl GasPool {
/// Performs an end-to-end flow of reserving gas, signing a transaction, and releasing the gas coins.
pub async fn debug_check_health(&self) -> anyhow::Result<()> {
let gas_budget = MIST_PER_SUI / 10;
let (_address, _reservation_id, gas_coins) =
let (sender, _reservation_id, gas_coins) =
self.reserve_gas(gas_budget, Duration::from_secs(3)).await?;
let tx_kind = TransactionKind::ProgrammableTransaction(
ProgrammableTransactionBuilder::new().finish(),
);
// Since we just want to check the health of the signer, we don't need to actually execute the transaction.
let tx_data = TransactionData::new_with_gas_coins(
tx_kind,
SuiAddress::default(),
gas_coins,
gas_budget,
0,
);
let tx_data =
TransactionData::new_with_gas_coins(tx_kind, sender, gas_coins, gas_budget, 0);
self.signer.sign_transaction(&tx_data).await?;
Ok(())
}
Expand Down Expand Up @@ -337,7 +332,7 @@ impl GasPool {

impl GasPoolContainer {
pub async fn new(
signer: Arc<dyn TxSigner>,
signer: Arc<TxSigner>,
gas_pool_store: Arc<dyn Storage>,
sui_client: SuiClient,
gas_usage_daily_cap: u64,
Expand Down
8 changes: 4 additions & 4 deletions src/gas_pool_initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const MAX_INIT_DURATION_SEC: u64 = 60 * 60 * 12;
struct CoinSplitEnv {
target_init_coin_balance: u64,
gas_cost_per_object: u64,
signer: Arc<dyn TxSigner>,
signer: Arc<TxSigner>,
sponsor_address: SuiAddress,
sui_client: SuiClient,
task_queue: Arc<Mutex<VecDeque<JoinHandle<Vec<GasCoin>>>>>,
Expand Down Expand Up @@ -177,7 +177,7 @@ impl GasPoolInitializer {
sui_client: SuiClient,
storage: Arc<dyn Storage>,
coin_init_config: CoinInitConfig,
signer: Arc<dyn TxSigner>,
signer: Arc<TxSigner>,
) -> Self {
for address in signer.get_all_addresses() {
if !storage.is_initialized(address).await.unwrap() {
Expand Down Expand Up @@ -211,7 +211,7 @@ impl GasPoolInitializer {
sui_client: SuiClient,
storage: Arc<dyn Storage>,
coin_init_config: CoinInitConfig,
signer: Arc<dyn TxSigner>,
signer: Arc<TxSigner>,
mut cancel_receiver: tokio::sync::oneshot::Receiver<()>,
) {
loop {
Expand Down Expand Up @@ -243,7 +243,7 @@ impl GasPoolInitializer {
storage: &Arc<dyn Storage>,
mode: RunMode,
target_init_coin_balance: u64,
signer: &Arc<dyn TxSigner>,
signer: &Arc<TxSigner>,
) {
if storage
.acquire_init_lock(sponsor_address, MAX_INIT_DURATION_SEC)
Expand Down
10 changes: 7 additions & 3 deletions src/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use crate::metrics::{GasPoolCoreMetrics, GasPoolRpcMetrics};
use crate::rpc::GasPoolServer;
use crate::storage::connect_storage_for_testing;
use crate::sui_client::SuiClient;
use crate::tx_signer::{TestTxSigner, TxSigner};
use crate::tx_signer::in_memory_signer::InMemoryTxSigner;
use crate::tx_signer::TxSigner;
use crate::AUTH_ENV_NAME;
use std::sync::Arc;
use sui_config::local_ip_utils::{get_available_port, localhost_for_testing};
Expand All @@ -21,7 +22,7 @@ use sui_types::transaction::{TransactionData, TransactionDataAPI};
use test_cluster::{TestCluster, TestClusterBuilder};
use tracing::debug;

pub async fn start_sui_cluster(init_gas_amounts: Vec<u64>) -> (TestCluster, Arc<dyn TxSigner>) {
pub async fn start_sui_cluster(init_gas_amounts: Vec<u64>) -> (TestCluster, Arc<TxSigner>) {
let (sponsor, keypair) = get_account_key_pair();
let cluster = TestClusterBuilder::new()
.with_accounts(vec![
Expand All @@ -37,7 +38,10 @@ pub async fn start_sui_cluster(init_gas_amounts: Vec<u64>) -> (TestCluster, Arc<
])
.build()
.await;
(cluster, TestTxSigner::new(keypair.into()))
(
cluster,
TxSigner::new(vec![InMemoryTxSigner::new(keypair.into())]),
)
}

pub async fn start_gas_station(
Expand Down
150 changes: 0 additions & 150 deletions src/tx_signer.rs

This file was deleted.

38 changes: 38 additions & 0 deletions src/tx_signer/in_memory_signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::sync::Arc;

use shared_crypto::intent::{Intent, IntentMessage};
use sui_types::base_types::SuiAddress;
use sui_types::crypto::{Signature, SuiKeyPair};
use sui_types::signature::GenericSignature;
use sui_types::transaction::TransactionData;

use super::TxSignerTrait;

pub struct InMemoryTxSigner {
keypair: SuiKeyPair,
}

impl InMemoryTxSigner {
pub fn new(keypair: SuiKeyPair) -> Arc<Self> {
Arc::new(Self { keypair })
}
}

#[async_trait::async_trait]
impl TxSignerTrait for InMemoryTxSigner {
async fn sign_transaction(
&self,
tx_data: &TransactionData,
) -> anyhow::Result<GenericSignature> {
let intent_msg = IntentMessage::new(Intent::sui_transaction(), tx_data);
let sponsor_sig = Signature::new_secure(&intent_msg, &self.keypair).into();
Ok(sponsor_sig)
}

fn sui_address(&self) -> SuiAddress {
(&self.keypair.public()).into()
}
}
Loading

0 comments on commit c04fc3a

Please sign in to comment.