mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-07-23 08:10:20 +00:00
Merge 441e43ef91bd2e8f468343d83a7bed1487634ca0 into 390f4b48d982511883eec8628a9742976f8de8d3
This commit is contained in:
commit
61717b9fe3
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -11019,13 +11019,11 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"bip39",
|
||||
"cbindgen",
|
||||
"common",
|
||||
"key_protocol",
|
||||
"lee",
|
||||
"lee_core",
|
||||
"programs",
|
||||
"risc0-zkvm",
|
||||
"sequencer_service_rpc",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
"vault_core",
|
||||
|
||||
Binary file not shown.
@ -27,7 +27,7 @@ use wallet::WalletCore;
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Initialize wallet
|
||||
let wallet_core = WalletCore::from_env().unwrap();
|
||||
let wallet_core = WalletCore::from_env().await.unwrap();
|
||||
|
||||
// Parse arguments
|
||||
// First argument is the path to the program binary
|
||||
@ -59,7 +59,7 @@ async fn main() {
|
||||
|
||||
// Submit the transaction
|
||||
let _response = wallet_core
|
||||
.sequencer_client
|
||||
.leader_owned()
|
||||
.send_transaction(LeeTransaction::Public(tx))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@ -23,7 +23,7 @@ use wallet::{AccountIdentity, WalletCore};
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Initialize wallet
|
||||
let wallet_core = WalletCore::from_env().unwrap();
|
||||
let wallet_core = WalletCore::from_env().await.unwrap();
|
||||
|
||||
// Parse arguments
|
||||
// First argument is the path to the program binary
|
||||
|
||||
@ -27,7 +27,7 @@ use wallet::WalletCore;
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Initialize wallet
|
||||
let wallet_core = WalletCore::from_env().unwrap();
|
||||
let wallet_core = WalletCore::from_env().await.unwrap();
|
||||
|
||||
// Parse arguments
|
||||
// First argument is the path to the program binary
|
||||
@ -55,7 +55,7 @@ async fn main() {
|
||||
|
||||
// Submit the transaction
|
||||
let _response = wallet_core
|
||||
.sequencer_client
|
||||
.leader_owned()
|
||||
.send_transaction(LeeTransaction::Public(tx))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@ -26,7 +26,7 @@ use wallet::{AccountIdentity, WalletCore};
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Initialize wallet
|
||||
let wallet_core = WalletCore::from_env().unwrap();
|
||||
let wallet_core = WalletCore::from_env().await.unwrap();
|
||||
|
||||
// Parse arguments
|
||||
// First argument is the path to the simple_tail_call program binary
|
||||
|
||||
@ -29,7 +29,7 @@ use wallet::WalletCore;
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Initialize wallet
|
||||
let wallet_core = WalletCore::from_env().unwrap();
|
||||
let wallet_core = WalletCore::from_env().await.unwrap();
|
||||
|
||||
// Parse arguments
|
||||
// First argument is the path to the program binary
|
||||
@ -52,7 +52,8 @@ async fn main() {
|
||||
.storage()
|
||||
.key_chain()
|
||||
.pub_account_signing_key(account_id)
|
||||
.expect("Input account should be a self owned public account");
|
||||
.expect("Input account should be a self owned public account")
|
||||
.clone();
|
||||
|
||||
// Define the desired greeting in ASCII
|
||||
let greeting: Vec<u8> = vec![72, 111, 108, 97, 32, 109, 117, 110, 100, 111, 33];
|
||||
@ -60,10 +61,10 @@ async fn main() {
|
||||
// Construct the public transaction
|
||||
// Query the current nonce from the node
|
||||
let nonces = wallet_core
|
||||
.get_accounts_nonces(vec![account_id])
|
||||
.get_accounts_nonces(&[account_id])
|
||||
.await
|
||||
.expect("Node should be reachable to query account data");
|
||||
let signing_keys = [signing_key];
|
||||
let signing_keys = [&signing_key];
|
||||
let message = Message::try_new(program.id(), vec![account_id], nonces, greeting).unwrap();
|
||||
// Pass the signing key to sign the message. This will be used by the node
|
||||
// to flag the pre_state as `is_authorized` when executing the program
|
||||
@ -72,7 +73,7 @@ async fn main() {
|
||||
|
||||
// Submit the transaction
|
||||
let _response = wallet_core
|
||||
.sequencer_client
|
||||
.leader_owned()
|
||||
.send_transaction(LeeTransaction::Public(tx))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@ -35,7 +35,7 @@ const PDA_SEED: PdaSeed = PdaSeed::new([37; 32]);
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Initialize wallet
|
||||
let wallet_core = WalletCore::from_env().unwrap();
|
||||
let wallet_core = WalletCore::from_env().await.unwrap();
|
||||
|
||||
// Parse arguments
|
||||
// First argument is the path to the program binary
|
||||
@ -57,7 +57,7 @@ async fn main() {
|
||||
|
||||
// Submit the transaction
|
||||
let _response = wallet_core
|
||||
.sequencer_client
|
||||
.leader_owned()
|
||||
.send_transaction(LeeTransaction::Public(tx))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@ -66,7 +66,7 @@ async fn main() {
|
||||
let program = Program::new(bytecode.into()).unwrap();
|
||||
|
||||
// Initialize wallet
|
||||
let wallet_core = WalletCore::from_env().unwrap();
|
||||
let wallet_core = WalletCore::from_env().await.unwrap();
|
||||
|
||||
match cli.command {
|
||||
Command::WritePublic {
|
||||
@ -88,7 +88,7 @@ async fn main() {
|
||||
|
||||
// Submit the transaction
|
||||
let _response = wallet_core
|
||||
.sequencer_client
|
||||
.leader_owned()
|
||||
.send_transaction(LeeTransaction::Public(tx))
|
||||
.await
|
||||
.unwrap();
|
||||
@ -127,7 +127,7 @@ async fn main() {
|
||||
|
||||
// Submit the transaction
|
||||
let _response = wallet_core
|
||||
.sequencer_client
|
||||
.leader_owned()
|
||||
.send_transaction(LeeTransaction::Public(tx))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@ -248,11 +248,7 @@ async fn shielded_transfer_to_foreign_account() -> Result<()> {
|
||||
let acc_1_balance = account_balance(&ctx, from).await?;
|
||||
|
||||
assert!(
|
||||
verify_commitment_is_in_state(
|
||||
tx.message.new_commitments[0].clone(),
|
||||
ctx.sequencer_client()
|
||||
)
|
||||
.await
|
||||
verify_commitment_is_in_state(tx.message.new_commitments[0], ctx.sequencer_client()).await
|
||||
);
|
||||
|
||||
assert_eq!(acc_1_balance, 9900);
|
||||
|
||||
@ -440,8 +440,8 @@ async fn bedrock_deposit_claim_and_withdraw_round_trip_succeeds() -> anyhow::Res
|
||||
|
||||
// Now claim funds from vault back to recipient
|
||||
let nonces = ctx
|
||||
.wallet()
|
||||
.get_accounts_nonces(vec![recipient_id])
|
||||
.wallet_mut()
|
||||
.get_accounts_nonces(&[recipient_id])
|
||||
.await
|
||||
.context("Failed to get nonce for vault claim")?;
|
||||
|
||||
|
||||
@ -91,7 +91,7 @@ async fn fund_private_pda(
|
||||
let tx = PrivacyPreservingTransaction::new(message, witness_set);
|
||||
|
||||
wallet
|
||||
.sequencer_client
|
||||
.leader_owned()
|
||||
.send_transaction(LeeTransaction::PrivacyPreserving(tx))
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("send transaction failed: {e}"))?;
|
||||
@ -180,7 +180,7 @@ async fn private_pda_family_members_receive_and_spend() -> Result<()> {
|
||||
|
||||
info!("Sending to alice_pda_0 (identifier=0)");
|
||||
fund_private_pda(
|
||||
ctx.wallet(),
|
||||
ctx.wallet_mut(),
|
||||
sender_0,
|
||||
alice_npk,
|
||||
alice_vpk.clone(),
|
||||
@ -194,7 +194,7 @@ async fn private_pda_family_members_receive_and_spend() -> Result<()> {
|
||||
|
||||
info!("Sending to alice_pda_1 (identifier=1)");
|
||||
fund_private_pda(
|
||||
ctx.wallet(),
|
||||
ctx.wallet_mut(),
|
||||
sender_1,
|
||||
alice_npk,
|
||||
alice_vpk.clone(),
|
||||
@ -231,7 +231,7 @@ async fn private_pda_family_members_receive_and_spend() -> Result<()> {
|
||||
.get_private_account_commitment(alice_pda_0_id)
|
||||
.context("commitment for alice_pda_0 missing")?;
|
||||
assert!(
|
||||
verify_commitment_is_in_state(commitment_0.clone(), ctx.sequencer_client()).await,
|
||||
verify_commitment_is_in_state(commitment_0, ctx.sequencer_client()).await,
|
||||
"alice_pda_0 commitment not in state after receive"
|
||||
);
|
||||
|
||||
@ -240,7 +240,7 @@ async fn private_pda_family_members_receive_and_spend() -> Result<()> {
|
||||
.get_private_account_commitment(alice_pda_1_id)
|
||||
.context("commitment for alice_pda_1 missing")?;
|
||||
assert!(
|
||||
verify_commitment_is_in_state(commitment_1.clone(), ctx.sequencer_client()).await,
|
||||
verify_commitment_is_in_state(commitment_1, ctx.sequencer_client()).await,
|
||||
"alice_pda_1 commitment not in state after receive"
|
||||
);
|
||||
assert_ne!(
|
||||
@ -262,7 +262,7 @@ async fn private_pda_family_members_receive_and_spend() -> Result<()> {
|
||||
|
||||
info!("Alice spending from alice_pda_0");
|
||||
spend_private_pda(
|
||||
ctx.wallet(),
|
||||
ctx.wallet_mut(),
|
||||
alice_pda_0_id,
|
||||
recipient_npk_0,
|
||||
recipient_vpk_0,
|
||||
@ -275,7 +275,7 @@ async fn private_pda_family_members_receive_and_spend() -> Result<()> {
|
||||
|
||||
info!("Alice spending from alice_pda_1");
|
||||
spend_private_pda(
|
||||
ctx.wallet(),
|
||||
ctx.wallet_mut(),
|
||||
alice_pda_1_id,
|
||||
recipient_npk_1,
|
||||
recipient_vpk_1,
|
||||
|
||||
@ -31,7 +31,7 @@ async fn deploy_and_execute_program() -> Result<()> {
|
||||
|
||||
let account_id = new_account(&mut ctx, false, None).await?;
|
||||
|
||||
let nonces = ctx.wallet().get_accounts_nonces(vec![account_id]).await?;
|
||||
let nonces = ctx.wallet_mut().get_accounts_nonces(&[account_id]).await?;
|
||||
let private_key = ctx
|
||||
.wallet()
|
||||
.get_account_public_signing_key(account_id)
|
||||
|
||||
@ -43,12 +43,14 @@ unsafe extern "C" {
|
||||
fn wallet_ffi_create_new(
|
||||
config_path: *const c_char,
|
||||
storage_path: *const c_char,
|
||||
metrics_path: *const c_char,
|
||||
password: *const c_char,
|
||||
) -> FfiCreateWalletOutput;
|
||||
|
||||
fn wallet_ffi_open(
|
||||
config_path: *const c_char,
|
||||
storage_path: *const c_char,
|
||||
metrics_path: *const c_char,
|
||||
) -> *mut WalletHandle;
|
||||
|
||||
fn wallet_ffi_destroy(handle: *mut WalletHandle);
|
||||
@ -291,6 +293,7 @@ fn new_wallet_ffi_with_test_context_config(
|
||||
) -> Result<FfiCreateWalletOutput> {
|
||||
let config_path = home.join("wallet_config.json");
|
||||
let storage_path = home.join("storage.json");
|
||||
let metrics_path = home.join("metrics.json");
|
||||
let mut config = ctx.ctx().wallet().config().to_owned();
|
||||
if let Some(config_overrides) = ctx.ctx().wallet().config_overrides().clone() {
|
||||
config.apply_overrides(config_overrides);
|
||||
@ -307,12 +310,14 @@ fn new_wallet_ffi_with_test_context_config(
|
||||
|
||||
let config_path = CString::new(config_path.to_str().unwrap())?;
|
||||
let storage_path = CString::new(storage_path.to_str().unwrap())?;
|
||||
let metrics_path = CString::new(metrics_path.to_str().unwrap())?;
|
||||
let password = CString::new(ctx.ctx().wallet_password())?;
|
||||
|
||||
let create_wallet_result = unsafe {
|
||||
wallet_ffi_create_new(
|
||||
config_path.as_ptr(),
|
||||
storage_path.as_ptr(),
|
||||
metrics_path.as_ptr(),
|
||||
password.as_ptr(),
|
||||
)
|
||||
};
|
||||
@ -370,14 +375,17 @@ fn new_wallet_ffi_with_default_config(password: &str) -> Result<FfiCreateWalletO
|
||||
let tempdir = tempdir()?;
|
||||
let config_path = tempdir.path().join("wallet_config.json");
|
||||
let storage_path = tempdir.path().join("storage.json");
|
||||
let metrics_path = tempdir.path().join("metrics.json");
|
||||
let config_path_c = CString::new(config_path.to_str().unwrap())?;
|
||||
let storage_path_c = CString::new(storage_path.to_str().unwrap())?;
|
||||
let metrics_path_c = CString::new(metrics_path.to_str().unwrap())?;
|
||||
let password = CString::new(password)?;
|
||||
|
||||
let create_wallet_result = unsafe {
|
||||
wallet_ffi_create_new(
|
||||
config_path_c.as_ptr(),
|
||||
storage_path_c.as_ptr(),
|
||||
metrics_path_c.as_ptr(),
|
||||
password.as_ptr(),
|
||||
)
|
||||
};
|
||||
@ -388,10 +396,18 @@ fn new_wallet_ffi_with_default_config(password: &str) -> Result<FfiCreateWalletO
|
||||
fn load_existing_ffi_wallet(home: &Path) -> Result<*mut WalletHandle> {
|
||||
let config_path = home.join("wallet_config.json");
|
||||
let storage_path = home.join("storage.json");
|
||||
let metrics_path = home.join("metrics.json");
|
||||
let config_path = CString::new(config_path.to_str().unwrap())?;
|
||||
let storage_path = CString::new(storage_path.to_str().unwrap())?;
|
||||
let metrics_path = CString::new(metrics_path.to_str().unwrap())?;
|
||||
|
||||
Ok(unsafe { wallet_ffi_open(config_path.as_ptr(), storage_path.as_ptr()) })
|
||||
Ok(unsafe {
|
||||
wallet_ffi_open(
|
||||
config_path.as_ptr(),
|
||||
storage_path.as_ptr(),
|
||||
metrics_path.as_ptr(),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@ -32,7 +32,7 @@ pub const DUMMY_COMMITMENT_HASH: [u8; 32] = [
|
||||
#[derive(Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
|
||||
#[cfg_attr(
|
||||
any(feature = "host", test),
|
||||
derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)
|
||||
derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)
|
||||
)]
|
||||
pub struct Commitment(pub(super) [u8; 32]);
|
||||
|
||||
|
||||
@ -44,7 +44,7 @@ impl CommitmentSet {
|
||||
|
||||
/// Inserts a list of commitments to the `CommitmentSet`.
|
||||
pub(crate) fn extend(&mut self, commitments: &[Commitment]) {
|
||||
for commitment in commitments.iter().cloned() {
|
||||
for commitment in commitments.iter().copied() {
|
||||
let index = self.merkle_tree.insert(commitment.to_byte_array());
|
||||
self.commitments.insert(commitment, index);
|
||||
}
|
||||
|
||||
@ -308,7 +308,7 @@ fn authorized_public_account_claiming_succeeds_when_executed_privately() {
|
||||
let sender_commitment = Commitment::new(&sender_account_id, &sender_private_account);
|
||||
let sender_init_nullifier = Nullifier::for_account_initialization(&sender_account_id);
|
||||
let mut state =
|
||||
V03State::new().with_private_accounts([(sender_commitment.clone(), sender_init_nullifier)]);
|
||||
V03State::new().with_private_accounts([(sender_commitment, sender_init_nullifier)]);
|
||||
let sender_pre = AccountWithMetadata::new(
|
||||
sender_private_account,
|
||||
true,
|
||||
@ -401,8 +401,8 @@ fn private_chained_call(number_of_calls: u32) {
|
||||
let to_init_nullifier = Nullifier::for_account_initialization(&to_account_id);
|
||||
let mut state = V03State::new()
|
||||
.with_private_accounts([
|
||||
(from_commitment.clone(), from_init_nullifier),
|
||||
(to_commitment.clone(), to_init_nullifier),
|
||||
(from_commitment, from_init_nullifier),
|
||||
(to_commitment, to_init_nullifier),
|
||||
])
|
||||
.with_test_programs();
|
||||
let amount: u128 = 37;
|
||||
|
||||
@ -505,7 +505,7 @@ fn malicious_authorization_changer_should_fail_in_privacy_preserving_circuit() {
|
||||
sender_account.account_id,
|
||||
sender_account.account.balance,
|
||||
)]))
|
||||
.with_private_accounts([(recipient_commitment.clone(), recipient_init_nullifier)])
|
||||
.with_private_accounts([(recipient_commitment, recipient_init_nullifier)])
|
||||
.with_test_programs();
|
||||
|
||||
let balance_to_transfer = 10_u128;
|
||||
|
||||
@ -14,8 +14,6 @@ crate-type = ["rlib", "cdylib", "staticlib"]
|
||||
wallet.workspace = true
|
||||
lee.workspace = true
|
||||
lee_core.workspace = true
|
||||
sequencer_service_rpc = { workspace = true, features = ["client"] }
|
||||
common.workspace = true
|
||||
programs.workspace = true
|
||||
|
||||
tokio.workspace = true
|
||||
|
||||
@ -1,9 +1,5 @@
|
||||
use std::{ffi::CString, ptr, slice};
|
||||
|
||||
use common::transaction::LeeTransaction;
|
||||
use lee::ProgramDeploymentTransaction;
|
||||
use sequencer_service_rpc::RpcClient as _;
|
||||
|
||||
use crate::{
|
||||
block_on,
|
||||
error::{print_error, WalletFfiError},
|
||||
@ -60,14 +56,7 @@ pub unsafe extern "C" fn wallet_ffi_program_deployment(
|
||||
|
||||
let elf = unsafe { slice::from_raw_parts(elf_data, elf_size) }.to_vec();
|
||||
|
||||
let message = lee::program_deployment_transaction::Message::new(elf);
|
||||
let transaction = ProgramDeploymentTransaction::new(message);
|
||||
|
||||
match block_on(
|
||||
wallet
|
||||
.sequencer_client
|
||||
.send_transaction(LeeTransaction::ProgramDeployment(transaction)),
|
||||
) {
|
||||
match block_on(wallet.send_program_deployment_transaction(elf)) {
|
||||
Ok(tx_hash) => {
|
||||
let tx_hash = CString::new(tx_hash.to_string())
|
||||
.map_or(ptr::null_mut(), std::ffi::CString::into_raw);
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
//! Block synchronization functions.
|
||||
|
||||
use sequencer_service_rpc::RpcClient as _;
|
||||
|
||||
use crate::{
|
||||
block_on,
|
||||
error::{print_error, WalletFfiError},
|
||||
@ -136,7 +134,7 @@ pub unsafe extern "C" fn wallet_ffi_get_current_block_height(
|
||||
}
|
||||
};
|
||||
|
||||
match block_on(wallet.sequencer_client.get_last_block_id()) {
|
||||
match block_on(wallet.get_last_block_id()) {
|
||||
Ok(last_block_id) => {
|
||||
unsafe {
|
||||
*out_block_height = last_block_id;
|
||||
|
||||
@ -86,6 +86,7 @@ fn c_str_to_path(ptr: *const c_char, name: &str) -> Result<PathBuf, WalletFfiErr
|
||||
/// # Parameters
|
||||
/// - `config_path`: Path to the wallet configuration file (JSON)
|
||||
/// - `storage_path`: Path where wallet data will be stored
|
||||
/// - `metrics_path`: Path to the wallet metrics file (JSON)
|
||||
/// - `password`: Password for encrypting the wallet seed
|
||||
///
|
||||
/// # Returns
|
||||
@ -98,6 +99,7 @@ fn c_str_to_path(ptr: *const c_char, name: &str) -> Result<PathBuf, WalletFfiErr
|
||||
pub unsafe extern "C" fn wallet_ffi_create_new(
|
||||
config_path: *const c_char,
|
||||
storage_path: *const c_char,
|
||||
metrics_path: *const c_char,
|
||||
password: *const c_char,
|
||||
) -> FfiCreateWalletOutput {
|
||||
let Ok(config_path) = c_str_to_path(config_path, "config_path") else {
|
||||
@ -112,7 +114,17 @@ pub unsafe extern "C" fn wallet_ffi_create_new(
|
||||
return FfiCreateWalletOutput::default();
|
||||
};
|
||||
|
||||
match WalletCore::new_init_storage(config_path, storage_path, None, &password) {
|
||||
let Ok(metrics_path) = c_str_to_path(metrics_path, "metrics_path") else {
|
||||
return FfiCreateWalletOutput::default();
|
||||
};
|
||||
|
||||
match block_on(WalletCore::new_init_storage(
|
||||
config_path,
|
||||
storage_path,
|
||||
metrics_path,
|
||||
None,
|
||||
&password,
|
||||
)) {
|
||||
Ok((core, mnemonic)) => {
|
||||
let wrapper = Box::new(WalletWrapper {
|
||||
core: Mutex::new(core),
|
||||
@ -142,8 +154,9 @@ pub unsafe extern "C" fn wallet_ffi_create_new(
|
||||
/// This loads a wallet that was previously created with `wallet_ffi_create_new()`.
|
||||
///
|
||||
/// # Parameters
|
||||
/// - `handle` - Valid wallet handle
|
||||
/// - `config_path`: Path to the wallet configuration file (JSON)
|
||||
/// - `storage_path`: Path where wallet data is stored
|
||||
/// - `metrics_path`: Path to the wallet metrics file (JSON)
|
||||
///
|
||||
/// # Returns
|
||||
/// - Opaque wallet handle on success
|
||||
@ -151,10 +164,12 @@ pub unsafe extern "C" fn wallet_ffi_create_new(
|
||||
///
|
||||
/// # Safety
|
||||
/// All string parameters must be valid null-terminated UTF-8 strings.
|
||||
/// `handle` must be a valid wallet handle from `wallet_ffi_create_new` or `wallet_ffi_open`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wallet_ffi_open(
|
||||
config_path: *const c_char,
|
||||
storage_path: *const c_char,
|
||||
metrics_path: *const c_char,
|
||||
) -> *mut WalletHandle {
|
||||
let Ok(config_path) = c_str_to_path(config_path, "config_path") else {
|
||||
return ptr::null_mut();
|
||||
@ -164,7 +179,16 @@ pub unsafe extern "C" fn wallet_ffi_open(
|
||||
return ptr::null_mut();
|
||||
};
|
||||
|
||||
match WalletCore::new_update_chain(config_path, storage_path, None) {
|
||||
let Ok(metrics_path) = c_str_to_path(metrics_path, "metrics_path") else {
|
||||
return ptr::null_mut();
|
||||
};
|
||||
|
||||
match block_on(WalletCore::new_update_chain(
|
||||
config_path,
|
||||
storage_path,
|
||||
metrics_path,
|
||||
None,
|
||||
)) {
|
||||
Ok(core) => {
|
||||
let wrapper = Box::new(WalletWrapper {
|
||||
core: Mutex::new(core),
|
||||
@ -216,7 +240,7 @@ pub unsafe extern "C" fn wallet_ffi_save(handle: *mut WalletHandle) -> WalletFfi
|
||||
Err(e) => return e,
|
||||
};
|
||||
|
||||
let wallet = match wrapper.core.lock() {
|
||||
let mut wallet = match wrapper.core.lock() {
|
||||
Ok(w) => w,
|
||||
Err(e) => {
|
||||
print_error(format!("Failed to lock wallet: {e}"));
|
||||
@ -224,7 +248,10 @@ pub unsafe extern "C" fn wallet_ffi_save(handle: *mut WalletHandle) -> WalletFfi
|
||||
}
|
||||
};
|
||||
|
||||
match wallet.store_persistent_data() {
|
||||
match wallet
|
||||
.store_persistent_data()
|
||||
.and_then(|()| block_on(wallet.store_metrics()))
|
||||
{
|
||||
Ok(()) => WalletFfiError::Success,
|
||||
Err(e) => {
|
||||
print_error(format!("Failed to save wallet: {e}"));
|
||||
@ -334,7 +361,7 @@ pub unsafe extern "C" fn wallet_ffi_get_sequencer_addr(handle: *mut WalletHandle
|
||||
}
|
||||
};
|
||||
|
||||
let addr = wallet.config().sequencer_addr.clone().to_string();
|
||||
let addr = wallet.leader_url().to_string();
|
||||
|
||||
match std::ffi::CString::new(addr) {
|
||||
Ok(s) => s.into_raw(),
|
||||
|
||||
@ -1612,6 +1612,7 @@ enum WalletFfiError wallet_ffi_vault_claim_private(struct WalletHandle *handle,
|
||||
* # Parameters
|
||||
* - `config_path`: Path to the wallet configuration file (JSON)
|
||||
* - `storage_path`: Path where wallet data will be stored
|
||||
* - `metrics_path`: Path to the wallet metrics file (JSON)
|
||||
* - `password`: Password for encrypting the wallet seed
|
||||
*
|
||||
* # Returns
|
||||
@ -1623,6 +1624,7 @@ enum WalletFfiError wallet_ffi_vault_claim_private(struct WalletHandle *handle,
|
||||
*/
|
||||
struct FfiCreateWalletOutput wallet_ffi_create_new(const char *config_path,
|
||||
const char *storage_path,
|
||||
const char *metrics_path,
|
||||
const char *password);
|
||||
|
||||
/**
|
||||
@ -1631,8 +1633,9 @@ struct FfiCreateWalletOutput wallet_ffi_create_new(const char *config_path,
|
||||
* This loads a wallet that was previously created with `wallet_ffi_create_new()`.
|
||||
*
|
||||
* # Parameters
|
||||
* - `handle` - Valid wallet handle
|
||||
* - `config_path`: Path to the wallet configuration file (JSON)
|
||||
* - `storage_path`: Path where wallet data is stored
|
||||
* - `metrics_path`: Path to the wallet metrics file (JSON)
|
||||
*
|
||||
* # Returns
|
||||
* - Opaque wallet handle on success
|
||||
@ -1640,8 +1643,11 @@ struct FfiCreateWalletOutput wallet_ffi_create_new(const char *config_path,
|
||||
*
|
||||
* # Safety
|
||||
* All string parameters must be valid null-terminated UTF-8 strings.
|
||||
* `handle` must be a valid wallet handle from `wallet_ffi_create_new` or `wallet_ffi_open`.
|
||||
*/
|
||||
struct WalletHandle *wallet_ffi_open(const char *config_path, const char *storage_path);
|
||||
struct WalletHandle *wallet_ffi_open(const char *config_path,
|
||||
const char *storage_path,
|
||||
const char *metrics_path);
|
||||
|
||||
/**
|
||||
* Destroy a wallet handle and free its resources.
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
{
|
||||
"sequencer_addr": "http://127.0.0.1:3040",
|
||||
"sequencers_conn_data": [{
|
||||
"sequencer_addr": "http://127.0.0.1:3040"
|
||||
}],
|
||||
"seq_poll_timeout": "30s",
|
||||
"seq_tx_poll_max_blocks": 15,
|
||||
"seq_poll_max_retries": 10,
|
||||
|
||||
@ -384,38 +384,61 @@ impl AccountSubcommand {
|
||||
Ok(SubcommandReturnValue::Empty)
|
||||
}
|
||||
|
||||
async fn handle_list(long: bool, wallet_core: &WalletCore) -> Result<SubcommandReturnValue> {
|
||||
let key_chain = &wallet_core.storage.key_chain();
|
||||
let storage = wallet_core.storage();
|
||||
fn format_with_label(
|
||||
wallet_core: &WalletCore,
|
||||
id: AccountIdWithPrivacy,
|
||||
chain_index: Option<&ChainIndex>,
|
||||
) -> String {
|
||||
let id_str = chain_index.map_or_else(|| id.to_string(), |cci| format!("{cci} {id}"));
|
||||
|
||||
let format_with_label = |id: AccountIdWithPrivacy, chain_index: Option<&ChainIndex>| {
|
||||
let id_str = chain_index.map_or_else(|| id.to_string(), |cci| format!("{cci} {id}"));
|
||||
|
||||
let labels = storage.labels_for_account(id).format(", ").to_string();
|
||||
if labels.is_empty() {
|
||||
id_str
|
||||
} else {
|
||||
format!("{id_str} [{labels}]")
|
||||
}
|
||||
};
|
||||
|
||||
if !long {
|
||||
let accounts = key_chain
|
||||
.account_ids()
|
||||
.map(|(id, idx)| format_with_label(id, idx))
|
||||
.format("\n");
|
||||
println!("{accounts}");
|
||||
|
||||
return Ok(SubcommandReturnValue::Empty);
|
||||
let labels = wallet_core
|
||||
.storage()
|
||||
.labels_for_account(id)
|
||||
.format(", ")
|
||||
.to_string();
|
||||
if labels.is_empty() {
|
||||
id_str
|
||||
} else {
|
||||
format!("{id_str} [{labels}]")
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_list(long: bool, wallet_core: &WalletCore) -> Result<SubcommandReturnValue> {
|
||||
let (public_account_ids, private_account_ids) = {
|
||||
let key_chain = &wallet_core.storage.key_chain();
|
||||
|
||||
if !long {
|
||||
let accounts = key_chain
|
||||
.account_ids()
|
||||
.map(|(id, idx)| Self::format_with_label(wallet_core, id, idx))
|
||||
.format("\n");
|
||||
println!("{accounts}");
|
||||
|
||||
return Ok(SubcommandReturnValue::Empty);
|
||||
}
|
||||
|
||||
let public_account_ids: Vec<_> = key_chain
|
||||
.public_account_ids()
|
||||
.map(|(id, chain_index)| (id, chain_index.cloned()))
|
||||
.collect();
|
||||
let private_account_ids: Vec<_> = key_chain
|
||||
.private_account_ids()
|
||||
.map(|(id, chain_index)| (id, chain_index.cloned()))
|
||||
.collect();
|
||||
|
||||
(public_account_ids, private_account_ids)
|
||||
};
|
||||
|
||||
// Detailed listing with --long flag
|
||||
|
||||
// Public key tree accounts
|
||||
for (id, chain_index) in key_chain.public_account_ids() {
|
||||
for (id, chain_index) in public_account_ids {
|
||||
println!(
|
||||
"{}",
|
||||
format_with_label(AccountIdWithPrivacy::Public(id), chain_index)
|
||||
Self::format_with_label(
|
||||
wallet_core,
|
||||
AccountIdWithPrivacy::Public(id),
|
||||
chain_index.as_ref()
|
||||
)
|
||||
);
|
||||
match wallet_core.get_account_public(id).await {
|
||||
Ok(account) if account != Account::default() => {
|
||||
@ -429,10 +452,14 @@ impl AccountSubcommand {
|
||||
}
|
||||
|
||||
// Private key tree accounts
|
||||
for (id, chain_index) in key_chain.private_account_ids() {
|
||||
for (id, chain_index) in private_account_ids {
|
||||
println!(
|
||||
"{}",
|
||||
format_with_label(AccountIdWithPrivacy::Private(id), chain_index)
|
||||
Self::format_with_label(
|
||||
wallet_core,
|
||||
AccountIdWithPrivacy::Private(id),
|
||||
chain_index.as_ref()
|
||||
)
|
||||
);
|
||||
match wallet_core.get_account_private(id) {
|
||||
Some(account) if account != Account::default() => {
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use common::HashType;
|
||||
use sequencer_service_rpc::RpcClient as _;
|
||||
|
||||
use crate::{
|
||||
WalletCore,
|
||||
@ -33,17 +32,17 @@ impl WalletSubcommand for ChainSubcommand {
|
||||
) -> Result<SubcommandReturnValue> {
|
||||
match self {
|
||||
Self::CurrentBlockId => {
|
||||
let latest_block_id = wallet_core.sequencer_client.get_last_block_id().await?;
|
||||
let latest_block_id = wallet_core.get_last_block_id().await?;
|
||||
|
||||
println!("Last block id is {latest_block_id}");
|
||||
}
|
||||
Self::Block { id } => {
|
||||
let block = wallet_core.sequencer_client.get_block(id).await?;
|
||||
let block = wallet_core.get_block(id).await?;
|
||||
|
||||
println!("Last block id is {block:#?}");
|
||||
}
|
||||
Self::Transaction { hash } => {
|
||||
let tx = wallet_core.sequencer_client.get_transaction(hash).await?;
|
||||
let tx = wallet_core.get_transaction(hash).await?;
|
||||
|
||||
println!("Transaction is {tx:#?}");
|
||||
}
|
||||
|
||||
@ -37,7 +37,7 @@ impl ConfigSubcommand {
|
||||
} else if let Some(key) = key {
|
||||
match key.as_str() {
|
||||
"sequencer_addr" => {
|
||||
println!("{}", config.sequencer_addr);
|
||||
println!("{:?}", config.sequencers_conn_data);
|
||||
}
|
||||
"seq_poll_timeout" => {
|
||||
println!("{:?}", config.seq_poll_timeout);
|
||||
@ -51,13 +51,6 @@ impl ConfigSubcommand {
|
||||
"seq_block_poll_max_amount" => {
|
||||
println!("{}", config.seq_block_poll_max_amount);
|
||||
}
|
||||
"basic_auth" => {
|
||||
if let Some(basic_auth) = &config.basic_auth {
|
||||
println!("{basic_auth}");
|
||||
} else {
|
||||
println!("Not set");
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("Unknown field");
|
||||
}
|
||||
@ -76,9 +69,6 @@ impl ConfigSubcommand {
|
||||
) -> Result<SubcommandReturnValue> {
|
||||
let mut config = wallet_core.config().clone();
|
||||
match key.as_str() {
|
||||
"sequencer_addr" => {
|
||||
config.sequencer_addr = value.parse()?;
|
||||
}
|
||||
"seq_poll_timeout" => {
|
||||
config.seq_poll_timeout = humantime::parse_duration(&value)
|
||||
.map_err(|e| anyhow::anyhow!("Invalid duration: {e}"))?;
|
||||
@ -92,9 +82,6 @@ impl ConfigSubcommand {
|
||||
"seq_block_poll_max_amount" => {
|
||||
config.seq_block_poll_max_amount = value.parse()?;
|
||||
}
|
||||
"basic_auth" => {
|
||||
config.basic_auth = Some(value.parse()?);
|
||||
}
|
||||
"initial_accounts" => {
|
||||
anyhow::bail!("Setting this field from wallet is not supported");
|
||||
}
|
||||
|
||||
@ -3,10 +3,9 @@ use std::{io::Write as _, path::PathBuf, str::FromStr};
|
||||
use anyhow::{Context as _, Result};
|
||||
use bip39::Mnemonic;
|
||||
use clap::{Parser, Subcommand};
|
||||
use common::{HashType, transaction::LeeTransaction};
|
||||
use common::HashType;
|
||||
use derive_more::Display;
|
||||
use futures::TryFutureExt as _;
|
||||
use lee::ProgramDeploymentTransaction;
|
||||
use lee_core::BlockId;
|
||||
use sequencer_service_rpc::RpcClient as _;
|
||||
|
||||
@ -220,7 +219,6 @@ pub async fn execute_subcommand(
|
||||
}
|
||||
Command::CheckHealth => {
|
||||
let remote_program_ids = wallet_core
|
||||
.sequencer_client
|
||||
.get_program_ids()
|
||||
.await
|
||||
.expect("Error fetching program ids");
|
||||
@ -285,21 +283,25 @@ pub async fn execute_subcommand(
|
||||
"Failed to read program binary at {}",
|
||||
binary_filepath.display()
|
||||
))?;
|
||||
let message = lee::program_deployment_transaction::Message::new(bytecode);
|
||||
let transaction = ProgramDeploymentTransaction::new(message);
|
||||
let tx_hash = wallet_core
|
||||
.sequencer_client
|
||||
.send_transaction(LeeTransaction::ProgramDeployment(transaction))
|
||||
let response = wallet_core
|
||||
.send_program_deployment_transaction(bytecode)
|
||||
.await
|
||||
.context("Transaction submission error")?;
|
||||
|
||||
wallet_core
|
||||
.poll_and_finalize_public_transaction(tx_hash)
|
||||
.poll_and_finalize_public_transaction(response)
|
||||
.await
|
||||
.context("Transaction finalization error")?
|
||||
}
|
||||
};
|
||||
|
||||
// Kind of a sledgehammer solution, but it is not clear if there is the case to not store
|
||||
// metrics
|
||||
wallet_core
|
||||
.store_metrics()
|
||||
.await
|
||||
.context("Failed to store metrics")?;
|
||||
|
||||
Ok(subcommand_ret)
|
||||
}
|
||||
|
||||
@ -388,14 +390,13 @@ pub async fn execute_keys_restoration(wallet_core: &mut WalletCore, depth: u32)
|
||||
|
||||
wallet_core.sync_to_latest_block().await?;
|
||||
|
||||
let leader_client = wallet_core.leader_owned();
|
||||
|
||||
wallet_core
|
||||
.storage
|
||||
.key_chain_mut()
|
||||
.cleanup_trees_remove_uninit_layered(depth, |account_id| {
|
||||
wallet_core
|
||||
.sequencer_client
|
||||
.get_account(account_id)
|
||||
.map_err(Into::into)
|
||||
leader_client.get_account(account_id).map_err(Into::into)
|
||||
})
|
||||
.await?;
|
||||
|
||||
|
||||
@ -7,6 +7,15 @@ use log::warn;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SequencerConnectionData {
|
||||
/// Connection data of all known sequencers.
|
||||
pub sequencer_addr: Url,
|
||||
/// Basic authentication credentials.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub basic_auth: Option<BasicAuth>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct GasConfig {
|
||||
/// Gas spent per deploying one byte of data.
|
||||
@ -28,8 +37,8 @@ pub struct GasConfig {
|
||||
#[optfield::optfield(pub WalletConfigOverrides, rewrap, attrs = (derive(Debug, Default, Clone)))]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct WalletConfig {
|
||||
/// Sequencer URL.
|
||||
pub sequencer_addr: Url,
|
||||
/// Connection data of all known sequencers.
|
||||
pub sequencers_conn_data: Vec<SequencerConnectionData>,
|
||||
/// Sequencer polling duration for new blocks.
|
||||
#[serde(with = "humantime_serde")]
|
||||
pub seq_poll_timeout: Duration,
|
||||
@ -39,20 +48,22 @@ pub struct WalletConfig {
|
||||
pub seq_poll_max_retries: u64,
|
||||
/// Max amount of blocks to poll in one request.
|
||||
pub seq_block_poll_max_amount: u64,
|
||||
/// Basic authentication credentials
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub basic_auth: Option<BasicAuth>,
|
||||
/// Limit number of sequencer polls during callibration, should not be zero
|
||||
pub callibration_limit: usize,
|
||||
}
|
||||
|
||||
impl Default for WalletConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sequencer_addr: "http://127.0.0.1:3040".parse().unwrap(),
|
||||
sequencers_conn_data: vec![SequencerConnectionData {
|
||||
sequencer_addr: "http://127.0.0.1:3040".parse().unwrap(),
|
||||
basic_auth: None,
|
||||
}],
|
||||
seq_poll_timeout: Duration::from_secs(12),
|
||||
seq_tx_poll_max_blocks: 5,
|
||||
seq_poll_max_retries: 5,
|
||||
seq_block_poll_max_amount: 100,
|
||||
basic_auth: None,
|
||||
callibration_limit: 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -97,26 +108,26 @@ impl WalletConfig {
|
||||
|
||||
pub fn apply_overrides(&mut self, overrides: WalletConfigOverrides) {
|
||||
let Self {
|
||||
sequencer_addr,
|
||||
sequencers_conn_data,
|
||||
seq_poll_timeout,
|
||||
seq_tx_poll_max_blocks,
|
||||
seq_poll_max_retries,
|
||||
seq_block_poll_max_amount,
|
||||
basic_auth,
|
||||
callibration_limit,
|
||||
} = self;
|
||||
|
||||
let WalletConfigOverrides {
|
||||
sequencer_addr: o_sequencer_addr,
|
||||
sequencers_conn_data: o_sequencers_conn_data,
|
||||
seq_poll_timeout: o_seq_poll_timeout,
|
||||
seq_tx_poll_max_blocks: o_seq_tx_poll_max_blocks,
|
||||
seq_poll_max_retries: o_seq_poll_max_retries,
|
||||
seq_block_poll_max_amount: o_seq_block_poll_max_amount,
|
||||
basic_auth: o_basic_auth,
|
||||
callibration_limit: o_callibration_limit,
|
||||
} = overrides;
|
||||
|
||||
if let Some(v) = o_sequencer_addr {
|
||||
warn!("Overriding wallet config 'sequencer_addr' to {v}");
|
||||
*sequencer_addr = v;
|
||||
if let Some(v) = o_sequencers_conn_data {
|
||||
warn!("Overriding wallet config 'sequencers_conn_data' to {v:?}");
|
||||
*sequencers_conn_data = v;
|
||||
}
|
||||
if let Some(v) = o_seq_poll_timeout {
|
||||
warn!("Overriding wallet config 'seq_poll_timeout' to {v:?}");
|
||||
@ -134,9 +145,9 @@ impl WalletConfig {
|
||||
warn!("Overriding wallet config 'seq_block_poll_max_amount' to {v}");
|
||||
*seq_block_poll_max_amount = v;
|
||||
}
|
||||
if let Some(v) = o_basic_auth {
|
||||
warn!("Overriding wallet config 'basic_auth' to {v:#?}");
|
||||
*basic_auth = v;
|
||||
if let Some(v) = o_callibration_limit {
|
||||
warn!("Overriding wallet config 'callibration_limit' to {v}");
|
||||
*callibration_limit = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,6 +66,15 @@ pub fn fetch_persistent_storage_path() -> Result<PathBuf> {
|
||||
Ok(accs_path)
|
||||
}
|
||||
|
||||
/// Fetch path to metrics storage from default home.
|
||||
///
|
||||
/// File must be created through setup beforehand.
|
||||
pub fn fetch_metrics_path() -> Result<PathBuf> {
|
||||
let home = get_home()?;
|
||||
let metrics_path = home.join("metrics.json");
|
||||
Ok(metrics_path)
|
||||
}
|
||||
|
||||
#[expect(dead_code, reason = "Maybe used later")]
|
||||
pub(crate) fn produce_random_nonces(size: usize) -> Vec<Nonce> {
|
||||
let mut result = vec![[0; 16]; size];
|
||||
|
||||
@ -7,33 +7,39 @@
|
||||
reason = "Most of the shadows come from args parsing which is ok"
|
||||
)]
|
||||
|
||||
use std::{collections::HashSet, path::PathBuf};
|
||||
use std::{
|
||||
collections::{BTreeMap, HashMap, HashSet},
|
||||
path::PathBuf,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
pub use account_manager::AccountIdentity;
|
||||
use anyhow::{Context as _, Result};
|
||||
use bip39::Mnemonic;
|
||||
use common::{HashType, transaction::LeeTransaction};
|
||||
use common::{HashType, block::Block, transaction::LeeTransaction};
|
||||
use config::WalletConfig;
|
||||
use key_protocol::key_management::key_tree::chain_index::ChainIndex;
|
||||
use lee::{
|
||||
Account, AccountId, PrivacyPreservingTransaction, ProgramId,
|
||||
Account, AccountId, PrivacyPreservingTransaction, ProgramDeploymentTransaction, ProgramId,
|
||||
privacy_preserving_transaction::{
|
||||
circuit::ProgramWithDependencies,
|
||||
message::{EncryptedAccountData, Message},
|
||||
},
|
||||
};
|
||||
use lee_core::{
|
||||
Commitment, CommitmentSetDigest, MembershipProof, SharedSecretKey, account::Nonce,
|
||||
BlockId, Commitment, CommitmentSetDigest, MembershipProof, SharedSecretKey, account::Nonce,
|
||||
program::InstructionData,
|
||||
};
|
||||
use log::info;
|
||||
use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder};
|
||||
use sequencer_service_rpc::{RpcClient as _, SequencerClient};
|
||||
use storage::Storage;
|
||||
use tokio::io::AsyncWriteExt as _;
|
||||
use tokio::{io::AsyncWriteExt as _, sync::RwLock};
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
account::{AccountIdWithPrivacy, Label},
|
||||
config::WalletConfigOverrides,
|
||||
multi_client::{Metrics, MetricsUpdate, MultiSequencerClient, extract_metrics_from_path},
|
||||
poller::TxPoller,
|
||||
storage::key_chain::{NullifierIndex, SharedAccountEntry},
|
||||
};
|
||||
@ -43,6 +49,7 @@ mod account_manager;
|
||||
pub mod cli;
|
||||
pub mod config;
|
||||
pub mod helperfunctions;
|
||||
pub mod multi_client;
|
||||
pub mod poller;
|
||||
pub mod program_facades;
|
||||
pub mod signing;
|
||||
@ -84,7 +91,6 @@ pub enum ExecutionFailureKind {
|
||||
KeycardError(#[from] pyo3::PyErr),
|
||||
}
|
||||
|
||||
#[expect(clippy::partial_pub_fields, reason = "TODO: make all fields private")]
|
||||
pub struct WalletCore {
|
||||
config_path: PathBuf,
|
||||
config_overrides: Option<WalletConfigOverrides>,
|
||||
@ -93,45 +99,69 @@ pub struct WalletCore {
|
||||
storage: Storage,
|
||||
storage_path: PathBuf,
|
||||
|
||||
poller: TxPoller,
|
||||
pub sequencer_client: SequencerClient,
|
||||
metrics_path: PathBuf,
|
||||
metrics: HashMap<Url, Metrics>,
|
||||
/// Wrapping metric updates in Arc<RwLock> to not break interfaces too much.
|
||||
///
|
||||
/// It is assumed, that wallet methods can be accesed via immutable reference.
|
||||
metric_updates: Arc<RwLock<Vec<MetricsUpdate>>>,
|
||||
|
||||
multi_sequencer_client: MultiSequencerClient,
|
||||
}
|
||||
|
||||
impl WalletCore {
|
||||
/// Construct wallet using [`HOME_DIR_ENV_VAR`] env var for paths or user home dir if not set.
|
||||
pub fn from_env() -> Result<Self> {
|
||||
pub async fn from_env() -> Result<Self> {
|
||||
let config_path = helperfunctions::fetch_config_path()?;
|
||||
let storage_path = helperfunctions::fetch_persistent_storage_path()?;
|
||||
let metrics_path = helperfunctions::fetch_metrics_path()?;
|
||||
|
||||
Self::new_update_chain(config_path, storage_path, None)
|
||||
Self::new_update_chain(config_path, storage_path, metrics_path, None).await
|
||||
}
|
||||
|
||||
pub fn new_update_chain(
|
||||
pub async fn new_update_chain(
|
||||
config_path: PathBuf,
|
||||
storage_path: PathBuf,
|
||||
metrics_path: PathBuf,
|
||||
config_overrides: Option<WalletConfigOverrides>,
|
||||
) -> Result<Self> {
|
||||
let storage = Storage::from_path(&storage_path)
|
||||
.with_context(|| format!("Failed to load storage from {}", storage_path.display()))?;
|
||||
|
||||
Self::new(config_path, storage_path, config_overrides, storage)
|
||||
Self::new(
|
||||
config_path,
|
||||
storage_path,
|
||||
metrics_path,
|
||||
config_overrides,
|
||||
storage,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn new_init_storage(
|
||||
pub async fn new_init_storage(
|
||||
config_path: PathBuf,
|
||||
storage_path: PathBuf,
|
||||
metrics_path: PathBuf,
|
||||
config_overrides: Option<WalletConfigOverrides>,
|
||||
password: &str,
|
||||
) -> Result<(Self, Mnemonic)> {
|
||||
let (storage, mnemonic) = Storage::new(password).context("Failed to create storage")?;
|
||||
let wallet = Self::new(config_path, storage_path, config_overrides, storage)?;
|
||||
let wallet = Self::new(
|
||||
config_path,
|
||||
storage_path,
|
||||
metrics_path,
|
||||
config_overrides,
|
||||
storage,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok((wallet, mnemonic))
|
||||
}
|
||||
|
||||
fn new(
|
||||
async fn new(
|
||||
config_path: PathBuf,
|
||||
storage_path: PathBuf,
|
||||
metrics_path: PathBuf,
|
||||
config_overrides: Option<WalletConfigOverrides>,
|
||||
storage: Storage,
|
||||
) -> Result<Self> {
|
||||
@ -146,25 +176,14 @@ impl WalletCore {
|
||||
config.apply_overrides(config_overrides);
|
||||
}
|
||||
|
||||
let sequencer_client = {
|
||||
let mut builder = SequencerClientBuilder::default();
|
||||
if let Some(basic_auth) = &config.basic_auth {
|
||||
builder = builder.set_headers(
|
||||
std::iter::once((
|
||||
"Authorization".parse().expect("Header name is valid"),
|
||||
format!("Basic {basic_auth}")
|
||||
.parse()
|
||||
.context("Invalid basic auth format")?,
|
||||
))
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
builder
|
||||
.build(config.sequencer_addr.clone())
|
||||
.context("Failed to create sequencer client")?
|
||||
};
|
||||
let mut metrics = extract_metrics_from_path(&metrics_path)?;
|
||||
|
||||
let tx_poller = TxPoller::new(&config, sequencer_client.clone());
|
||||
let multi_sequencer_client = MultiSequencerClient::new(
|
||||
&config.sequencers_conn_data,
|
||||
&mut metrics,
|
||||
config.callibration_limit,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(Self {
|
||||
config_path,
|
||||
@ -172,8 +191,10 @@ impl WalletCore {
|
||||
config,
|
||||
storage_path,
|
||||
storage,
|
||||
poller: tx_poller,
|
||||
sequencer_client,
|
||||
metrics_path,
|
||||
metrics,
|
||||
metric_updates: Arc::new(RwLock::new(vec![])),
|
||||
multi_sequencer_client,
|
||||
})
|
||||
}
|
||||
|
||||
@ -187,6 +208,21 @@ impl WalletCore {
|
||||
self.config = config;
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn optimal_poller(&self) -> TxPoller {
|
||||
TxPoller::new(self.config(), self.leader_owned())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn leader_owned(&self) -> SequencerClient {
|
||||
self.multi_sequencer_client.leader.clone()
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn leader_url(&self) -> Url {
|
||||
self.multi_sequencer_client.leader_url.clone()
|
||||
}
|
||||
|
||||
/// Get storage.
|
||||
#[must_use]
|
||||
pub const fn storage(&self) -> &Storage {
|
||||
@ -223,6 +259,43 @@ impl WalletCore {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn update_metrics(&mut self) -> Result<()> {
|
||||
let leader_metric = self
|
||||
.metrics
|
||||
.get_mut(&self.multi_sequencer_client.leader_url)
|
||||
.ok_or_else(|| anyhow::anyhow!("Leader URL is not present in metrics"))?;
|
||||
|
||||
{
|
||||
let metric_updates = self.metric_updates.read().await;
|
||||
leader_metric.apply_updates(metric_updates.as_ref());
|
||||
}
|
||||
|
||||
// Clear updates
|
||||
{
|
||||
let mut metric_updates = self.metric_updates.write().await;
|
||||
metric_updates.clear();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn store_metrics(&mut self) -> Result<()> {
|
||||
self.update_metrics().await?;
|
||||
|
||||
let metrics_serialized = serde_json::to_vec_pretty(&self.metrics)?;
|
||||
let mut file = tokio::fs::File::create(&self.metrics_path)
|
||||
.await
|
||||
.context("Failed to create file")?;
|
||||
file.write_all(&metrics_serialized)
|
||||
.await
|
||||
.context("Failed to write to file")?;
|
||||
file.sync_all().await.context("Failed to sync file")?;
|
||||
|
||||
println!("Stored metrics at {}", self.metrics_path.display());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Store persistent data at home.
|
||||
pub async fn store_config_changes(&self) -> Result<()> {
|
||||
let config = serde_json::to_vec_pretty(&self.config)?;
|
||||
@ -414,14 +487,38 @@ impl WalletCore {
|
||||
})
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn get_metrics(&self, sequencer_url: &Url) -> Option<&Metrics> {
|
||||
self.metrics.get(sequencer_url)
|
||||
}
|
||||
|
||||
/// Get account balance.
|
||||
pub async fn get_account_balance(&self, acc: AccountId) -> Result<u128> {
|
||||
Ok(self.sequencer_client.get_account_balance(acc).await?)
|
||||
let call_f = async |client: &SequencerClient| client.get_account_balance(acc).await;
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
/// Get accounts nonces.
|
||||
pub async fn get_accounts_nonces(&self, accs: Vec<AccountId>) -> Result<Vec<Nonce>> {
|
||||
Ok(self.sequencer_client.get_accounts_nonces(accs).await?)
|
||||
pub async fn get_accounts_nonces(&self, accs: &[AccountId]) -> Result<Vec<Nonce>> {
|
||||
let call_f =
|
||||
async |client: &SequencerClient| client.get_accounts_nonces(accs.to_vec()).await;
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
pub async fn get_account(&self, account_id: AccountIdWithPrivacy) -> Result<Account> {
|
||||
@ -437,9 +534,60 @@ impl WalletCore {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_last_block_id(&self) -> Result<u64> {
|
||||
let call_f = async |client: &SequencerClient| client.get_last_block_id().await;
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
pub async fn get_block(&self, block_id: u64) -> Result<Option<Block>> {
|
||||
let call_f = async |client: &SequencerClient| client.get_block(block_id).await;
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
pub async fn get_transaction(
|
||||
&self,
|
||||
hash: HashType,
|
||||
) -> Result<Option<(LeeTransaction, BlockId)>> {
|
||||
let call_f = async |client: &SequencerClient| client.get_transaction(hash).await;
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
/// Get public account.
|
||||
pub async fn get_account_public(&self, account_id: AccountId) -> Result<Account> {
|
||||
Ok(self.sequencer_client.get_account(account_id).await?)
|
||||
let call_f = async |client: &SequencerClient| client.get_account(account_id).await;
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
@ -474,14 +622,43 @@ impl WalletCore {
|
||||
Some(Commitment::new(&account_id, account))
|
||||
}
|
||||
|
||||
pub async fn get_program_ids(&self) -> Result<BTreeMap<String, ProgramId>> {
|
||||
let call_f = async |client: &SequencerClient| client.get_program_ids().await;
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
/// Poll transactions.
|
||||
pub async fn poll_native_token_transfer(
|
||||
&self,
|
||||
hash: HashType,
|
||||
) -> Result<(LeeTransaction, BlockId)> {
|
||||
self.optimal_poller().poll_tx(hash).await
|
||||
}
|
||||
|
||||
pub async fn get_proofs_and_root(
|
||||
&self,
|
||||
commitments: Vec<Commitment>,
|
||||
) -> Result<(Vec<Option<MembershipProof>>, CommitmentSetDigest)> {
|
||||
self.sequencer_client
|
||||
.get_proofs_and_root(commitments)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
let call_f =
|
||||
async |client: &SequencerClient| client.get_proofs_and_root(commitments.clone()).await;
|
||||
|
||||
let (proofs_and_root, metrics_update) =
|
||||
self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(proofs_and_root?)
|
||||
}
|
||||
|
||||
pub fn decode_insert_privacy_preserving_transaction_results(
|
||||
@ -499,7 +676,7 @@ impl WalletCore {
|
||||
match acc_decode_data {
|
||||
AccDecodeData::Decode(secret, acc_account_id) => {
|
||||
let acc_ead = tx.message.encrypted_private_post_states[output_index].clone();
|
||||
let acc_comm = tx.message.new_commitments[output_index].clone();
|
||||
let acc_comm = tx.message.new_commitments[output_index];
|
||||
|
||||
let (kind, res_acc) = lee_core::EncryptionScheme::decrypt(
|
||||
&acc_ead.ciphertext,
|
||||
@ -530,7 +707,7 @@ impl WalletCore {
|
||||
tx_hash: HashType,
|
||||
) -> Result<cli::SubcommandReturnValue> {
|
||||
println!("Transaction hash is {tx_hash}");
|
||||
let (tx, block_id) = self.poller.poll_tx(tx_hash).await?;
|
||||
let (tx, block_id) = self.optimal_poller().poll_tx(tx_hash).await?;
|
||||
println!("Transaction is included in block {block_id}");
|
||||
println!("Transaction data is {tx:?}");
|
||||
self.store_persistent_data()?;
|
||||
@ -544,7 +721,7 @@ impl WalletCore {
|
||||
acc_decode_data: &[AccDecodeData],
|
||||
) -> Result<cli::SubcommandReturnValue> {
|
||||
println!("Transaction hash is {tx_hash}");
|
||||
let (tx, block_id) = self.poller.poll_tx(tx_hash).await?;
|
||||
let (tx, block_id) = self.optimal_poller().poll_tx(tx_hash).await?;
|
||||
println!("Transaction is included in block {block_id}");
|
||||
println!("Transaction data is {tx:?}");
|
||||
if let common::transaction::LeeTransaction::PrivacyPreserving(private_tx) = tx {
|
||||
@ -620,12 +797,20 @@ impl WalletCore {
|
||||
.map(|keys| keys.ssk)
|
||||
.collect();
|
||||
|
||||
Ok((
|
||||
self.sequencer_client
|
||||
.send_transaction(LeeTransaction::PrivacyPreserving(tx))
|
||||
.await?,
|
||||
shared_secrets,
|
||||
))
|
||||
let call_f = async |client: &SequencerClient| {
|
||||
client
|
||||
.send_transaction(LeeTransaction::PrivacyPreserving(tx.clone()))
|
||||
.await
|
||||
};
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok((call_res?, shared_secrets))
|
||||
}
|
||||
|
||||
pub async fn send_pub_tx(
|
||||
@ -684,14 +869,44 @@ impl WalletCore {
|
||||
|
||||
let tx = lee::public_transaction::PublicTransaction::new(message, witness_set);
|
||||
|
||||
Ok(self
|
||||
.sequencer_client
|
||||
.send_transaction(LeeTransaction::Public(tx))
|
||||
.await?)
|
||||
let call_f = async |client: &SequencerClient| {
|
||||
client
|
||||
.send_transaction(LeeTransaction::Public(tx.clone()))
|
||||
.await
|
||||
};
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
pub async fn send_program_deployment_transaction(&self, bytecode: Vec<u8>) -> Result<HashType> {
|
||||
let message = lee::program_deployment_transaction::Message::new(bytecode);
|
||||
let transaction = ProgramDeploymentTransaction::new(message);
|
||||
|
||||
let call_f = async |client: &SequencerClient| {
|
||||
client
|
||||
.send_transaction(LeeTransaction::ProgramDeployment(transaction.clone()))
|
||||
.await
|
||||
};
|
||||
|
||||
let (call_res, metrics_update) = self.multi_sequencer_client.metered_call(call_f).await;
|
||||
|
||||
{
|
||||
let mut metric_updates_guard = self.metric_updates.write().await;
|
||||
metric_updates_guard.push(metrics_update);
|
||||
}
|
||||
|
||||
Ok(call_res?)
|
||||
}
|
||||
|
||||
pub async fn sync_to_latest_block(&mut self) -> Result<u64> {
|
||||
let latest_block_id = self.sequencer_client.get_last_block_id().await?;
|
||||
let latest_block_id = self.get_last_block_id().await?;
|
||||
println!("Latest block is {latest_block_id}");
|
||||
self.sync_to_block(latest_block_id).await?;
|
||||
Ok(latest_block_id)
|
||||
@ -713,7 +928,7 @@ impl WalletCore {
|
||||
|
||||
println!("Syncing to block {block_id}. Blocks to sync: {num_of_blocks}");
|
||||
|
||||
let poller = self.poller.clone();
|
||||
let poller = self.optimal_poller().clone();
|
||||
let mut blocks =
|
||||
std::pin::pin!(poller.poll_block_range(last_synced_block.saturating_add(1)..=block_id));
|
||||
|
||||
|
||||
@ -8,8 +8,7 @@ use clap::{CommandFactory as _, Parser as _};
|
||||
use wallet::{
|
||||
WalletCore,
|
||||
cli::{Args, execute_continuous_run, execute_subcommand, read_password_from_stdin},
|
||||
config::WalletConfigOverrides,
|
||||
helperfunctions::{fetch_config_path, fetch_persistent_storage_path},
|
||||
helperfunctions::{fetch_config_path, fetch_metrics_path, fetch_persistent_storage_path},
|
||||
};
|
||||
|
||||
// TODO #169: We have sample configs for sequencer, but not for wallet
|
||||
@ -21,7 +20,7 @@ use wallet::{
|
||||
async fn main() -> Result<()> {
|
||||
let Args {
|
||||
continuous_run,
|
||||
auth,
|
||||
auth: _auth,
|
||||
command,
|
||||
} = Args::parse();
|
||||
|
||||
@ -30,16 +29,11 @@ async fn main() -> Result<()> {
|
||||
let config_path = fetch_config_path().context("Could not fetch config path")?;
|
||||
let storage_path =
|
||||
fetch_persistent_storage_path().context("Could not fetch persistent storage path")?;
|
||||
|
||||
// Override basic auth if provided via CLI
|
||||
let config_overrides = WalletConfigOverrides {
|
||||
basic_auth: auth.map(|auth| auth.parse()).transpose()?.map(Some),
|
||||
..Default::default()
|
||||
};
|
||||
let metrics_path = fetch_metrics_path().context("Could not fetch metrics path")?;
|
||||
|
||||
if let Some(command) = command {
|
||||
let mut wallet = if storage_path.exists() {
|
||||
WalletCore::new_update_chain(config_path, storage_path, Some(config_overrides))?
|
||||
WalletCore::new_update_chain(config_path, storage_path, metrics_path, None).await?
|
||||
} else {
|
||||
// TODO: Maybe move to `WalletCore::from_env()` or similar?
|
||||
|
||||
@ -49,9 +43,11 @@ async fn main() -> Result<()> {
|
||||
let (wallet, mnemonic) = WalletCore::new_init_storage(
|
||||
config_path,
|
||||
storage_path,
|
||||
Some(config_overrides),
|
||||
metrics_path,
|
||||
None,
|
||||
&password,
|
||||
)?;
|
||||
)
|
||||
.await?;
|
||||
|
||||
println!();
|
||||
println!("IMPORTANT: Write down your recovery phrase and store it securely.");
|
||||
@ -68,7 +64,7 @@ async fn main() -> Result<()> {
|
||||
Ok(())
|
||||
} else if continuous_run {
|
||||
let mut wallet =
|
||||
WalletCore::new_update_chain(config_path, storage_path, Some(config_overrides))?;
|
||||
WalletCore::new_update_chain(config_path, storage_path, metrics_path, None).await?;
|
||||
execute_continuous_run(&mut wallet).await
|
||||
} else {
|
||||
let help = Args::command().render_long_help();
|
||||
|
||||
951
lez/wallet/src/multi_client.rs
Normal file
951
lez/wallet/src/multi_client.rs
Normal file
@ -0,0 +1,951 @@
|
||||
#![expect(
|
||||
clippy::float_arithmetic,
|
||||
reason = "One should expect floating point arithmetic in statistic calculations"
|
||||
)]
|
||||
#![expect(
|
||||
clippy::cast_precision_loss,
|
||||
reason = "Operated numbers is not big enough to have precision loss"
|
||||
)]
|
||||
|
||||
use std::{collections::HashMap, path::Path};
|
||||
|
||||
use anyhow::{Context as _, Result};
|
||||
use sequencer_service_rpc::{RpcClient as _, SequencerClient, SequencerClientBuilder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
use crate::config::SequencerConnectionData;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Metrics {
|
||||
pub latency_avg: f32,
|
||||
pub latency_var: f32,
|
||||
pub sample_size: usize,
|
||||
pub latest_block_id: u64,
|
||||
pub errors: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MetricsUpdate {
|
||||
pub latency: f32,
|
||||
pub new_latest_block_id: Option<u64>,
|
||||
pub is_failed: bool,
|
||||
}
|
||||
|
||||
impl Metrics {
|
||||
pub fn apply_updates(&mut self, updates: &[MetricsUpdate]) {
|
||||
let CumulativeUpdates {
|
||||
failure_count,
|
||||
latest_block_id,
|
||||
cumulative_latency,
|
||||
cumulative_latency_squares,
|
||||
additional_sample_size,
|
||||
} = CumulativeUpdates::from_metric_updates(updates);
|
||||
|
||||
self.errors = self.errors.saturating_add(failure_count);
|
||||
if let Some(latest_block_id) = latest_block_id {
|
||||
self.latest_block_id = latest_block_id;
|
||||
}
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let orig_size_f = self.sample_size as f32;
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let mod_size_f = additional_sample_size as f32;
|
||||
|
||||
let latency_avg_old = self.latency_avg;
|
||||
let latency_avg_new =
|
||||
cumulative_avg(latency_avg_old, cumulative_latency, orig_size_f, mod_size_f);
|
||||
|
||||
let latency_var_new = cumulative_var(
|
||||
latency_avg_old,
|
||||
latency_avg_new,
|
||||
self.latency_var,
|
||||
cumulative_latency,
|
||||
cumulative_latency_squares,
|
||||
orig_size_f,
|
||||
mod_size_f,
|
||||
);
|
||||
|
||||
self.latency_avg = latency_avg_new;
|
||||
self.latency_var = latency_var_new;
|
||||
self.sample_size = self.sample_size.saturating_add(additional_sample_size);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MultiSequencerClient {
|
||||
// For now we store only leader, it is possible, that
|
||||
// in future for important sends(for example for transactions)
|
||||
// we would want to distribute call between known sequencers
|
||||
pub leader: SequencerClient,
|
||||
pub leader_url: Url,
|
||||
}
|
||||
|
||||
impl MultiSequencerClient {
|
||||
pub async fn new(
|
||||
conn_data: &[SequencerConnectionData],
|
||||
metrics: &mut HashMap<Url, Metrics>,
|
||||
callibration_limit: usize,
|
||||
) -> Result<Self> {
|
||||
let mut client_list = HashMap::new();
|
||||
|
||||
for SequencerConnectionData {
|
||||
sequencer_addr,
|
||||
basic_auth,
|
||||
} in conn_data
|
||||
{
|
||||
let sequencer_client = {
|
||||
let mut builder = SequencerClientBuilder::default();
|
||||
if let Some(basic_auth) = &basic_auth {
|
||||
builder = builder.set_headers(
|
||||
std::iter::once((
|
||||
"Authorization".parse().expect("Header name is valid"),
|
||||
format!("Basic {basic_auth}")
|
||||
.parse()
|
||||
.context("Invalid basic auth format")?,
|
||||
))
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
builder
|
||||
.build(sequencer_addr)
|
||||
.context("Failed to create sequencer client")?
|
||||
};
|
||||
|
||||
// If there is no metrics for client, callibrate it
|
||||
if metrics.contains_key(sequencer_addr) {
|
||||
let metric_updates = actualize_client(&sequencer_client).await;
|
||||
|
||||
log::debug!(
|
||||
"Metered call for {sequencer_addr:?}, metric updates is {metric_updates:?}"
|
||||
);
|
||||
|
||||
let metric_mut = metrics.get_mut(sequencer_addr).unwrap();
|
||||
metric_mut.apply_updates(&[metric_updates]);
|
||||
// Otherwise actualize client data
|
||||
} else {
|
||||
metrics.insert(
|
||||
sequencer_addr.clone(),
|
||||
callibrate_client(&sequencer_client, callibration_limit).await,
|
||||
);
|
||||
}
|
||||
|
||||
client_list.insert(sequencer_addr.clone(), sequencer_client);
|
||||
}
|
||||
|
||||
let (leader_url, leader) = choose_leader(&client_list, metrics)
|
||||
.ok_or_else(|| anyhow::anyhow!("Failed to find leader"))?;
|
||||
|
||||
log::info!("Chosen leader is {leader_url:?}");
|
||||
|
||||
// Dropping client list, for reasons why, see comment in structure definition.
|
||||
Ok(Self { leader, leader_url })
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub const fn leader_ref(&self) -> &SequencerClient {
|
||||
&self.leader
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn leader_clone(&self) -> SequencerClient {
|
||||
self.leader.clone()
|
||||
}
|
||||
|
||||
// Keeping this call abstract, in case if we need to do more than one request
|
||||
pub async fn metered_call<R, E, I: AsyncFn(&SequencerClient) -> Result<R, E>>(
|
||||
&self,
|
||||
call: I,
|
||||
) -> (Result<R, E>, MetricsUpdate) {
|
||||
let resp = tokio::join!(call(self.leader_ref()), actualize_client(self.leader_ref()));
|
||||
|
||||
log::debug!(
|
||||
"Metered call for {:?}, metric updates is {:?}",
|
||||
self.leader_url,
|
||||
resp.1
|
||||
);
|
||||
|
||||
resp
|
||||
}
|
||||
}
|
||||
|
||||
struct CumulativeUpdates {
|
||||
pub failure_count: u64,
|
||||
pub latest_block_id: Option<u64>,
|
||||
/// Necessary for cumulative average calculation.
|
||||
pub cumulative_latency: f32,
|
||||
/// Necessary for cumulative variance calculation.
|
||||
pub cumulative_latency_squares: f32,
|
||||
pub additional_sample_size: usize,
|
||||
}
|
||||
|
||||
impl CumulativeUpdates {
|
||||
fn from_metric_updates(metric_updates: &[MetricsUpdate]) -> Self {
|
||||
let (failure_count, latest_block_id, cumulative_latency, cumulative_latency_squares) =
|
||||
metric_updates
|
||||
.iter()
|
||||
.fold((0_u64, None, 0_f32, 0_f32), |acc, x| {
|
||||
let MetricsUpdate {
|
||||
latency,
|
||||
new_latest_block_id,
|
||||
is_failed,
|
||||
} = x;
|
||||
(
|
||||
if *is_failed {
|
||||
acc.0.saturating_add(1)
|
||||
} else {
|
||||
acc.0
|
||||
},
|
||||
match (acc.1, new_latest_block_id) {
|
||||
(None, None) => None,
|
||||
(None, Some(val)) | (Some(val), None) => Some(val),
|
||||
(Some(val_old), Some(val_new)) => Some(std::cmp::max(val_old, val_new)),
|
||||
},
|
||||
if *is_failed { acc.2 } else { acc.2 + latency },
|
||||
if *is_failed {
|
||||
acc.3
|
||||
} else {
|
||||
latency.mul_add(*latency, acc.3)
|
||||
},
|
||||
)
|
||||
});
|
||||
|
||||
Self {
|
||||
failure_count,
|
||||
latest_block_id: latest_block_id.copied(),
|
||||
cumulative_latency,
|
||||
cumulative_latency_squares,
|
||||
additional_sample_size: metric_updates.len().saturating_sub(
|
||||
usize::try_from(failure_count).expect("Sample size should fit usize"),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn extract_metrics_from_path(path: &Path) -> Result<HashMap<Url, Metrics>, anyhow::Error> {
|
||||
match std::fs::File::open(path) {
|
||||
Ok(file) => {
|
||||
let reader = std::io::BufReader::new(file);
|
||||
Ok(serde_json::from_reader(reader)?)
|
||||
}
|
||||
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
|
||||
println!("Metrics not found, choosing empty");
|
||||
Ok(HashMap::new())
|
||||
}
|
||||
Err(err) => Err(err).context("IO error"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn callibrate_client(client: &SequencerClient, callibration_limit: usize) -> Metrics {
|
||||
let mut latencies = vec![];
|
||||
let mut latest_block_id = 0;
|
||||
let mut errors: u64 = 0;
|
||||
|
||||
// ToDo: Add some DDoS adaptation
|
||||
for _ in 0..callibration_limit {
|
||||
let now = tokio::time::Instant::now();
|
||||
|
||||
let block_id = client.get_last_block_id().await;
|
||||
|
||||
let latency = tokio::time::Instant::now().duration_since(now).as_millis();
|
||||
|
||||
let Ok(block_id) = block_id else {
|
||||
errors = errors.saturating_add(1);
|
||||
continue;
|
||||
};
|
||||
|
||||
latest_block_id = block_id;
|
||||
latencies.push(latency);
|
||||
}
|
||||
|
||||
// Precision loss if fine there
|
||||
let sample_size = latencies.len();
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let latency_avg = (latencies.iter().sum::<u128>() as f32) / (sample_size as f32);
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let latency_var = latencies.iter().fold(0_f32, |acc, x| {
|
||||
((*x as f32) - latency_avg).mul_add((*x as f32) - latency_avg, acc)
|
||||
}) / (sample_size as f32);
|
||||
|
||||
Metrics {
|
||||
latency_avg,
|
||||
latency_var,
|
||||
sample_size,
|
||||
latest_block_id,
|
||||
errors,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn actualize_client(client: &SequencerClient) -> MetricsUpdate {
|
||||
let now = tokio::time::Instant::now();
|
||||
|
||||
let block_id = client.get_last_block_id().await.ok();
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let latency = tokio::time::Instant::now().duration_since(now).as_millis() as f32;
|
||||
|
||||
MetricsUpdate {
|
||||
latency,
|
||||
new_latest_block_id: block_id,
|
||||
is_failed: block_id.is_none(),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn choose_leader(
|
||||
client_list: &HashMap<Url, SequencerClient>,
|
||||
metrics: &HashMap<Url, Metrics>,
|
||||
) -> Option<(Url, SequencerClient)> {
|
||||
let mut client_vec = vec![];
|
||||
|
||||
// Sort out all unmetered clients
|
||||
client_vec = client_list
|
||||
.keys()
|
||||
.filter(|item| metrics.contains_key(*item))
|
||||
.collect();
|
||||
|
||||
if client_vec.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// Considering the nature of our requests, the latest_block_id is the dominant characteristic
|
||||
let max_block_id_addr = client_vec.iter().fold(client_vec[0], |acc, x| {
|
||||
let old_latest_block_id = metrics.get(acc).unwrap().latest_block_id;
|
||||
let new_latest_block_id = metrics.get(*x).unwrap().latest_block_id;
|
||||
if new_latest_block_id > old_latest_block_id {
|
||||
*x
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
});
|
||||
|
||||
let max_block_id = metrics.get(max_block_id_addr).unwrap().latest_block_id;
|
||||
|
||||
// Sort out all clients running late
|
||||
client_vec = client_vec
|
||||
.iter()
|
||||
.filter_map(|x| {
|
||||
let latest_block_id = metrics.get(*x).unwrap().latest_block_id;
|
||||
|
||||
(latest_block_id == max_block_id).then_some(*x)
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Get the clients with lesser or equal to average error count
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let avg_err_count = (client_vec.iter().fold(0_u64, |acc, x| {
|
||||
acc.saturating_add(metrics.get(*x).unwrap().errors)
|
||||
}) as f32)
|
||||
/ (client_vec.len() as f32);
|
||||
|
||||
client_vec.sort_by(|a, b| {
|
||||
metrics
|
||||
.get(*a)
|
||||
.unwrap()
|
||||
.errors
|
||||
.cmp(&metrics.get(*b).unwrap().errors)
|
||||
});
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let client_vec = client_vec[..(client_vec
|
||||
.iter()
|
||||
.position(|item| (metrics.get(*item).unwrap().errors as f32) > avg_err_count)
|
||||
.unwrap_or(client_vec.len()))]
|
||||
.to_vec();
|
||||
|
||||
// Choose clients with least latency and variance
|
||||
let min_lat_var_addr = client_vec.iter().fold(client_vec[0], |acc, x| {
|
||||
let old = metrics.get(acc).unwrap();
|
||||
let (old_lat, old_var) = (old.latency_avg, old.latency_var);
|
||||
let new = metrics.get(*x).unwrap();
|
||||
let (new_lat, new_var) = (new.latency_avg, new.latency_var);
|
||||
|
||||
let new_std = new_var.sqrt();
|
||||
let old_std = old_var.sqrt();
|
||||
|
||||
// Client is better if its averabe is better and variance does not make it worse
|
||||
// So basically we want this:
|
||||
// [-old_std............new_lat.......old_lat...............+new_std.........+old_std]
|
||||
//
|
||||
// However one can argue that this:
|
||||
//
|
||||
// [-old_std...................old_lat........new_lat.........+new_std.......+old_std]
|
||||
//
|
||||
// is still better, but it is up to discussion
|
||||
if (new_lat <= old_lat) && ((new_lat + new_std) < (old_lat + old_std)) {
|
||||
*x
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
});
|
||||
|
||||
Some((
|
||||
min_lat_var_addr.clone(),
|
||||
client_list.get(min_lat_var_addr).unwrap().clone(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Helperfunction to calculate cumulative average.
|
||||
///
|
||||
/// Cumulative average calculation is the following problem:
|
||||
///
|
||||
/// We want to calculate avarage of a sample of size `N + N_1`
|
||||
/// where average for `N` is known.
|
||||
///
|
||||
/// To do so we need:
|
||||
/// - old average value
|
||||
/// - sum_{`i=1}^{N_1}{n_i`}
|
||||
/// - `N`
|
||||
/// - `N_1`
|
||||
fn cumulative_avg(
|
||||
latency_avg_old: f32,
|
||||
cumulative_latency: f32,
|
||||
orig_size_f: f32,
|
||||
mod_size_f: f32,
|
||||
) -> f32 {
|
||||
latency_avg_old.mul_add(orig_size_f, cumulative_latency) / (orig_size_f + mod_size_f)
|
||||
}
|
||||
|
||||
/// Helperfunction to calculate cumulative variance.
|
||||
///
|
||||
/// Cumulative variance calculation is the following problem:
|
||||
///
|
||||
/// We want to calculate variance of a sample of size `N + N_1`
|
||||
/// where average for `N` is known.
|
||||
///
|
||||
/// To do so we need:
|
||||
/// - old average value
|
||||
/// - new average value
|
||||
/// - old variance
|
||||
/// - sum_{`i=1}^{N_1}{n_i`}
|
||||
/// - sum_{`i=1}^{N_1}{n_i^2`}
|
||||
/// - `N`
|
||||
/// - `N_1`
|
||||
fn cumulative_var(
|
||||
latency_avg_old: f32,
|
||||
latency_avg_new: f32,
|
||||
latency_var: f32,
|
||||
cumulative_latency: f32,
|
||||
cumulative_latency_squares: f32,
|
||||
orig_size_f: f32,
|
||||
mod_size_f: f32,
|
||||
) -> f32 {
|
||||
// The formula was atrocious before.
|
||||
// `mul_add` function have less precision loss with drawback of being absolutely unreadable
|
||||
((2_f32 * cumulative_latency).mul_add(
|
||||
-latency_avg_new,
|
||||
mod_size_f.mul_add(
|
||||
latency_avg_new * latency_avg_new,
|
||||
latency_var.mul_add(
|
||||
orig_size_f,
|
||||
(latency_avg_new - latency_avg_old)
|
||||
* (latency_avg_new - latency_avg_old)
|
||||
* orig_size_f,
|
||||
),
|
||||
),
|
||||
) + cumulative_latency_squares)
|
||||
/ (orig_size_f + mod_size_f)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
|
||||
use sequencer_service_rpc::{SequencerClient, SequencerClientBuilder};
|
||||
use url::Url;
|
||||
|
||||
use crate::multi_client::{
|
||||
CumulativeUpdates, Metrics, MetricsUpdate, choose_leader, cumulative_avg, cumulative_var,
|
||||
};
|
||||
|
||||
fn update_metrics(
|
||||
metrics: &mut HashMap<Url, Metrics>,
|
||||
leader_url: &Url,
|
||||
metric_updates: &[MetricsUpdate],
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let leader_metric = metrics
|
||||
.get_mut(leader_url)
|
||||
.ok_or_else(|| anyhow::anyhow!("Leader URL is not present in metrics"))?;
|
||||
|
||||
leader_metric.apply_updates(metric_updates);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn client_from_url_unchecked(url: &Url) -> SequencerClient {
|
||||
let builder = SequencerClientBuilder::default();
|
||||
builder.build(url).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cumulative_updates_test() {
|
||||
let metrics_updates_vec = vec![
|
||||
MetricsUpdate {
|
||||
latency: 100_f32,
|
||||
new_latest_block_id: Some(15),
|
||||
is_failed: false,
|
||||
},
|
||||
MetricsUpdate {
|
||||
latency: 115_f32,
|
||||
new_latest_block_id: Some(16),
|
||||
is_failed: false,
|
||||
},
|
||||
MetricsUpdate {
|
||||
latency: 50_f32,
|
||||
new_latest_block_id: None,
|
||||
is_failed: true,
|
||||
},
|
||||
];
|
||||
|
||||
let CumulativeUpdates {
|
||||
failure_count,
|
||||
latest_block_id,
|
||||
cumulative_latency,
|
||||
cumulative_latency_squares,
|
||||
additional_sample_size,
|
||||
} = CumulativeUpdates::from_metric_updates(&metrics_updates_vec);
|
||||
|
||||
let epsilon = 0.01_f32;
|
||||
|
||||
let sum_squared_manual = 100_f32.mul_add(100_f32, 115_f32 * 115_f32);
|
||||
|
||||
assert_eq!(additional_sample_size, 2);
|
||||
assert_eq!(failure_count, 1);
|
||||
assert_eq!(latest_block_id, Some(16));
|
||||
assert!((cumulative_latency - 215_f32).abs() < epsilon);
|
||||
assert!((cumulative_latency_squares - sum_squared_manual).abs() < epsilon);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cumulative_avg_test() {
|
||||
let mut sample = vec![100_f32; 40];
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let old_sample_size_f = sample.len() as f32;
|
||||
|
||||
let old_avg = sample.iter().sum::<f32>() / old_sample_size_f;
|
||||
|
||||
let new_samples = vec![
|
||||
101_f32, 110_f32, 112_f32, 97_f32, 78_f32, 25_f32, 75_f32, 189_f32, 120_f32, 50_f32,
|
||||
];
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let mod_sample_size_f = new_samples.len() as f32;
|
||||
|
||||
let cumulative = new_samples.iter().sum();
|
||||
|
||||
sample.extend_from_slice(&new_samples);
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let new_sample_size_f = sample.len() as f32;
|
||||
|
||||
let new_avg_1 = sample.iter().sum::<f32>() / new_sample_size_f;
|
||||
|
||||
let new_avg_2 = cumulative_avg(old_avg, cumulative, old_sample_size_f, mod_sample_size_f);
|
||||
|
||||
let epsilon = 0.01_f32;
|
||||
|
||||
assert!((new_avg_1 - new_avg_2).abs() < epsilon);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cumulative_var_test() {
|
||||
let mut sample = vec![100_f32; 40];
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let old_sample_size_f = sample.len() as f32;
|
||||
let old_avg = sample.iter().sum::<f32>() / old_sample_size_f;
|
||||
|
||||
let old_var = sample
|
||||
.iter()
|
||||
.fold(0_f32, |acc, x| (x - old_avg).mul_add(x - old_avg, acc))
|
||||
/ old_sample_size_f;
|
||||
|
||||
let new_samples = vec![
|
||||
101_f32, 110_f32, 112_f32, 97_f32, 78_f32, 25_f32, 75_f32, 189_f32, 120_f32, 50_f32,
|
||||
];
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let mod_sample_size_f = new_samples.len() as f32;
|
||||
|
||||
let cumulative = new_samples.iter().sum();
|
||||
let cumulative_squares = new_samples
|
||||
.iter()
|
||||
.fold(0_f32, |acc, x| (*x).mul_add(*x, acc));
|
||||
|
||||
let new_avg = cumulative_avg(old_avg, cumulative, old_sample_size_f, mod_sample_size_f);
|
||||
|
||||
sample.extend_from_slice(&new_samples);
|
||||
|
||||
#[expect(clippy::as_conversions, reason = "int to float conversion is safe")]
|
||||
let new_var_1 = sample
|
||||
.iter()
|
||||
.fold(0_f32, |acc, x| (x - new_avg).mul_add(x - new_avg, acc))
|
||||
/ (sample.len() as f32);
|
||||
|
||||
let new_var_2 = cumulative_var(
|
||||
old_avg,
|
||||
new_avg,
|
||||
old_var,
|
||||
cumulative,
|
||||
cumulative_squares,
|
||||
old_sample_size_f,
|
||||
mod_sample_size_f,
|
||||
);
|
||||
|
||||
let epsilon = 0.01_f32;
|
||||
|
||||
assert!((new_var_1 - new_var_2).abs() < epsilon);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn metric_updates_correctness() {
|
||||
let metrics_updates_vec = vec![
|
||||
MetricsUpdate {
|
||||
latency: 100_f32,
|
||||
new_latest_block_id: Some(105),
|
||||
is_failed: false,
|
||||
},
|
||||
MetricsUpdate {
|
||||
latency: 115_f32,
|
||||
new_latest_block_id: Some(106),
|
||||
is_failed: false,
|
||||
},
|
||||
MetricsUpdate {
|
||||
latency: 50_f32,
|
||||
new_latest_block_id: None,
|
||||
is_failed: true,
|
||||
},
|
||||
];
|
||||
|
||||
let addr_leader = Url::parse("https://127.0.0.1:3040").unwrap();
|
||||
|
||||
let leader_metrics = Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 25_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
};
|
||||
|
||||
let cumulative_latency = 100_f32 + 115_f32;
|
||||
let cumulative_latency_squares = 100_f32.mul_add(100_f32, 115_f32 * 115_f32);
|
||||
|
||||
let avg_manual = cumulative_avg(
|
||||
leader_metrics.latency_avg,
|
||||
cumulative_latency,
|
||||
10_f32,
|
||||
2_f32,
|
||||
);
|
||||
let var_manual = cumulative_var(
|
||||
leader_metrics.latency_avg,
|
||||
avg_manual,
|
||||
leader_metrics.latency_var,
|
||||
cumulative_latency,
|
||||
cumulative_latency_squares,
|
||||
10_f32,
|
||||
2_f32,
|
||||
);
|
||||
|
||||
let mut metric_map = HashMap::new();
|
||||
metric_map.insert(addr_leader.clone(), leader_metrics);
|
||||
|
||||
update_metrics(&mut metric_map, &addr_leader, &metrics_updates_vec).unwrap();
|
||||
|
||||
let Metrics {
|
||||
latency_avg,
|
||||
latency_var,
|
||||
sample_size,
|
||||
latest_block_id,
|
||||
errors,
|
||||
} = metric_map[&addr_leader];
|
||||
|
||||
let epsilon = 0.01_f32;
|
||||
|
||||
assert_eq!(errors, 6);
|
||||
assert_eq!(latest_block_id, 106);
|
||||
assert_eq!(sample_size, 12);
|
||||
assert!((latency_avg - avg_manual).abs() < epsilon);
|
||||
assert!((latency_var - var_manual).abs() < epsilon);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn choose_leader_latest_block() {
|
||||
let addr_leader = Url::parse("http://127.0.0.1:3040").unwrap();
|
||||
let addr_1 = Url::parse("http://127.0.0.1:3041").unwrap();
|
||||
let addr_2 = Url::parse("http://127.0.0.1:3042").unwrap();
|
||||
let addr_3 = Url::parse("http://127.0.0.1:3043").unwrap();
|
||||
|
||||
let leader = client_from_url_unchecked(&addr_leader);
|
||||
let client_1 = client_from_url_unchecked(&addr_1);
|
||||
let client_2 = client_from_url_unchecked(&addr_2);
|
||||
let client_3 = client_from_url_unchecked(&addr_3);
|
||||
|
||||
let mut client_list = HashMap::new();
|
||||
|
||||
client_list.insert(addr_leader.clone(), leader);
|
||||
client_list.insert(addr_1.clone(), client_1);
|
||||
client_list.insert(addr_2.clone(), client_2);
|
||||
client_list.insert(addr_3.clone(), client_3);
|
||||
|
||||
let mut metrics = HashMap::new();
|
||||
|
||||
metrics.insert(
|
||||
addr_3,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 97,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_2,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 98,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_1,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 99,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_leader.clone(),
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
let (leader_url, _) = choose_leader(&client_list, &metrics).unwrap();
|
||||
|
||||
assert_eq!(leader_url, addr_leader);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn choose_leader_least_errors() {
|
||||
let addr_leader = Url::parse("http://127.0.0.1:3040").unwrap();
|
||||
let addr_1 = Url::parse("http://127.0.0.1:3041").unwrap();
|
||||
let addr_2 = Url::parse("http://127.0.0.1:3042").unwrap();
|
||||
let addr_3 = Url::parse("http://127.0.0.1:3043").unwrap();
|
||||
|
||||
let leader = client_from_url_unchecked(&addr_leader);
|
||||
let client_1 = client_from_url_unchecked(&addr_1);
|
||||
let client_2 = client_from_url_unchecked(&addr_2);
|
||||
let client_3 = client_from_url_unchecked(&addr_3);
|
||||
|
||||
let mut client_list = HashMap::new();
|
||||
|
||||
client_list.insert(addr_leader.clone(), leader);
|
||||
client_list.insert(addr_1.clone(), client_1);
|
||||
client_list.insert(addr_2.clone(), client_2);
|
||||
client_list.insert(addr_3.clone(), client_3);
|
||||
|
||||
let mut metrics = HashMap::new();
|
||||
|
||||
metrics.insert(
|
||||
addr_3,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_2,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 4,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_1,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 3,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_leader.clone(),
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 2,
|
||||
},
|
||||
);
|
||||
|
||||
let (leader_url, _) = choose_leader(&client_list, &metrics).unwrap();
|
||||
|
||||
assert_eq!(leader_url, addr_leader);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn choose_leader_simple_latency_check() {
|
||||
let addr_leader = Url::parse("http://127.0.0.1:3040").unwrap();
|
||||
let addr_1 = Url::parse("http://127.0.0.1:3041").unwrap();
|
||||
let addr_2 = Url::parse("http://127.0.0.1:3042").unwrap();
|
||||
let addr_3 = Url::parse("http://127.0.0.1:3043").unwrap();
|
||||
|
||||
let leader = client_from_url_unchecked(&addr_leader);
|
||||
let client_1 = client_from_url_unchecked(&addr_1);
|
||||
let client_2 = client_from_url_unchecked(&addr_2);
|
||||
let client_3 = client_from_url_unchecked(&addr_3);
|
||||
|
||||
let mut client_list = HashMap::new();
|
||||
|
||||
client_list.insert(addr_leader.clone(), leader);
|
||||
client_list.insert(addr_1.clone(), client_1);
|
||||
client_list.insert(addr_2.clone(), client_2);
|
||||
client_list.insert(addr_3.clone(), client_3);
|
||||
|
||||
let mut metrics = HashMap::new();
|
||||
|
||||
metrics.insert(
|
||||
addr_3,
|
||||
Metrics {
|
||||
latency_avg: 103_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_2,
|
||||
Metrics {
|
||||
latency_avg: 102_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_1,
|
||||
Metrics {
|
||||
latency_avg: 101_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_leader.clone(),
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
let (leader_url, _) = choose_leader(&client_list, &metrics).unwrap();
|
||||
|
||||
assert_eq!(leader_url, addr_leader);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn choose_leader_latency_var_check() {
|
||||
let addr_leader = Url::parse("http://127.0.0.1:3040").unwrap();
|
||||
let addr_1 = Url::parse("http://127.0.0.1:3041").unwrap();
|
||||
let addr_2 = Url::parse("http://127.0.0.1:3042").unwrap();
|
||||
let addr_3 = Url::parse("http://127.0.0.1:3043").unwrap();
|
||||
|
||||
let leader = client_from_url_unchecked(&addr_leader);
|
||||
let client_1 = client_from_url_unchecked(&addr_1);
|
||||
let client_2 = client_from_url_unchecked(&addr_2);
|
||||
let client_3 = client_from_url_unchecked(&addr_3);
|
||||
|
||||
let mut client_list = HashMap::new();
|
||||
|
||||
client_list.insert(addr_leader.clone(), leader);
|
||||
client_list.insert(addr_1.clone(), client_1);
|
||||
client_list.insert(addr_2.clone(), client_2);
|
||||
client_list.insert(addr_3.clone(), client_3);
|
||||
|
||||
let mut metrics = HashMap::new();
|
||||
|
||||
metrics.insert(
|
||||
addr_3,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 13_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_2,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 12_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_1,
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 11_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
metrics.insert(
|
||||
addr_leader.clone(),
|
||||
Metrics {
|
||||
latency_avg: 100_f32,
|
||||
latency_var: 10_f32,
|
||||
sample_size: 10,
|
||||
latest_block_id: 100,
|
||||
errors: 5,
|
||||
},
|
||||
);
|
||||
|
||||
let (leader_url, _) = choose_leader(&client_list, &metrics).unwrap();
|
||||
|
||||
assert_eq!(leader_url, addr_leader);
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@ -65,6 +65,7 @@ async fn generate_prebuilt_fixture(dest: &Path) -> Result<()> {
|
||||
&initial_private_accounts,
|
||||
WalletConfigOverrides::default(),
|
||||
)
|
||||
.await
|
||||
.context("Failed to setup wallet for fixture generation")?;
|
||||
|
||||
setup_public_accounts_with_initial_supply(&mut wallet, &initial_public_accounts)
|
||||
|
||||
@ -8,7 +8,7 @@ use lee::{AccountId, PrivateKey, PublicKey};
|
||||
use lee_core::Identifier;
|
||||
use sequencer_core::config::{BedrockConfig, CrossZoneConfig, GenesisAction, SequencerConfig};
|
||||
use url::Url;
|
||||
use wallet::config::WalletConfig;
|
||||
use wallet::config::{SequencerConnectionData, WalletConfig};
|
||||
|
||||
pub const INITIAL_PUBLIC_BALANCES_FOR_WALLET: [u128; 2] = [10_000, 20_000];
|
||||
pub const INITIAL_PRIVATE_BALANCES_FOR_WALLET: [u128; 2] = [10_000, 20_000];
|
||||
@ -194,13 +194,16 @@ pub fn genesis_from_accounts(
|
||||
|
||||
pub fn wallet_config(sequencer_addr: SocketAddr) -> Result<WalletConfig> {
|
||||
Ok(WalletConfig {
|
||||
sequencer_addr: addr_to_url(UrlProtocol::Http, sequencer_addr)
|
||||
.context("Failed to convert sequencer addr to URL")?,
|
||||
sequencers_conn_data: vec![SequencerConnectionData {
|
||||
sequencer_addr: addr_to_url(UrlProtocol::Http, sequencer_addr)
|
||||
.context("Failed to convert sequencer addr to URL")?,
|
||||
basic_auth: None,
|
||||
}],
|
||||
seq_poll_timeout: Duration::from_secs(30),
|
||||
seq_tx_poll_max_blocks: 15,
|
||||
seq_poll_max_retries: 10,
|
||||
seq_block_poll_max_amount: 100,
|
||||
basic_auth: None,
|
||||
callibration_limit: 1,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -393,6 +393,7 @@ impl TestContextBuilder {
|
||||
&initial_private_accounts,
|
||||
wallet_config_overrides,
|
||||
)
|
||||
.await
|
||||
.context("Failed to setup wallet")?;
|
||||
|
||||
if use_prebuilt {
|
||||
@ -456,6 +457,10 @@ impl BlockingTestContext {
|
||||
self.ctx.as_ref().expect("TestContext is set")
|
||||
}
|
||||
|
||||
pub const fn ctx_mut(&mut self) -> &mut TestContext {
|
||||
self.ctx.as_mut().expect("TestContext is set")
|
||||
}
|
||||
|
||||
pub const fn runtime(&self) -> &tokio::runtime::Runtime {
|
||||
&self.runtime
|
||||
}
|
||||
|
||||
@ -214,7 +214,7 @@ async fn setup_sequencer_inner(
|
||||
Ok((sequencer_handle, temp_sequencer_dir))
|
||||
}
|
||||
|
||||
pub fn setup_wallet(
|
||||
pub async fn setup_wallet(
|
||||
sequencer_addr: SocketAddr,
|
||||
initial_public_accounts: &[(PrivateKey, u128)],
|
||||
initial_private_accounts: &[InitialPrivateAccountForWallet],
|
||||
@ -232,14 +232,17 @@ pub fn setup_wallet(
|
||||
.context("Failed to write wallet config in temp dir")?;
|
||||
|
||||
let storage_path = temp_wallet_dir.path().join("storage.json");
|
||||
let metrics_path = temp_wallet_dir.path().join("metrics.json");
|
||||
|
||||
let wallet_password = "test_pass".to_owned();
|
||||
let (mut wallet, _mnemonic) = WalletCore::new_init_storage(
|
||||
config_path,
|
||||
storage_path,
|
||||
metrics_path,
|
||||
Some(config_overrides),
|
||||
&wallet_password,
|
||||
)
|
||||
.await
|
||||
.context("Failed to init wallet")?;
|
||||
|
||||
for (private_key, _balance) in initial_public_accounts {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user