|
| 1 | +// Copyright (c) Zefchain Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Tests for [`ClientContext::update_wallet_from_client`]. |
| 5 | +
|
| 6 | +use std::{ |
| 7 | + collections::{BTreeMap, BTreeSet, HashSet}, |
| 8 | + sync::Arc, |
| 9 | + time::Duration, |
| 10 | +}; |
| 11 | + |
| 12 | +use linera_base::{ |
| 13 | + crypto::InMemorySigner, |
| 14 | + data_types::{Amount, Round, TimeDelta}, |
| 15 | + identifiers::{AccountOwner, ChainId}, |
| 16 | + ownership::{ChainOwnership, TimeoutConfig}, |
| 17 | +}; |
| 18 | +use linera_core::{ |
| 19 | + client::{chain_client, Client, ListeningMode}, |
| 20 | + environment, |
| 21 | + join_set_ext::JoinSet, |
| 22 | + test_utils::{FaultType, MemoryStorageBuilder, TestBuilder}, |
| 23 | + worker::{DEFAULT_BLOCK_CACHE_SIZE, DEFAULT_EXECUTION_STATE_CACHE_SIZE}, |
| 24 | +}; |
| 25 | +use linera_rpc::node_provider::DEFAULT_MAX_BACKOFF; |
| 26 | + |
| 27 | +use crate::{client_context::ClientContext, config::GenesisConfig}; |
| 28 | + |
| 29 | +/// Builds a production [`ClientContext`] with a fresh in-memory wallet, sharing validators |
| 30 | +/// with the given [`TestBuilder`]. |
| 31 | +async fn make_context( |
| 32 | + builder: &mut TestBuilder<MemoryStorageBuilder>, |
| 33 | + signer: InMemorySigner, |
| 34 | + chain_id: ChainId, |
| 35 | +) -> anyhow::Result<ClientContext<environment::Test>> { |
| 36 | + let genesis_config = GenesisConfig::new_for_testing(builder); |
| 37 | + let admin_chain_id = genesis_config.admin_chain_id(); |
| 38 | + let storage = builder.make_storage().await?; |
| 39 | + let client = Client::new( |
| 40 | + environment::Impl { |
| 41 | + storage, |
| 42 | + network: builder.make_node_provider(), |
| 43 | + signer, |
| 44 | + wallet: environment::TestWallet::default(), |
| 45 | + }, |
| 46 | + admin_chain_id, |
| 47 | + false, |
| 48 | + [(chain_id, ListeningMode::FullChain)], |
| 49 | + format!("Client node for {:.8}", chain_id), |
| 50 | + Some(Duration::from_secs(30)), |
| 51 | + Some(Duration::from_secs(1)), |
| 52 | + HashSet::new(), |
| 53 | + chain_client::Options::test_default(), |
| 54 | + DEFAULT_BLOCK_CACHE_SIZE, |
| 55 | + DEFAULT_EXECUTION_STATE_CACHE_SIZE, |
| 56 | + &linera_core::client::RequestsSchedulerConfig::default(), |
| 57 | + ); |
| 58 | + Ok(ClientContext { |
| 59 | + client: Arc::new(client), |
| 60 | + genesis_config, |
| 61 | + send_timeout: Duration::from_secs(4), |
| 62 | + recv_timeout: Duration::from_secs(4), |
| 63 | + retry_delay: Duration::from_secs(1), |
| 64 | + max_retries: 10, |
| 65 | + max_backoff: DEFAULT_MAX_BACKOFF, |
| 66 | + chain_listeners: JoinSet::default(), |
| 67 | + default_chain: None, |
| 68 | + client_metrics: None, |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +/// A fast-round pending proposal must be persisted in the wallet. |
| 73 | +#[test_log::test(tokio::test)] |
| 74 | +async fn test_wallet_persists_fast_pending_proposal() -> anyhow::Result<()> { |
| 75 | + let signer = InMemorySigner::new(None); |
| 76 | + let mut builder = |
| 77 | + TestBuilder::new(MemoryStorageBuilder::default(), 4, 0, signer.clone()).await?; |
| 78 | + let mut client = builder.add_root_chain(1, Amount::from_tokens(10)).await?; |
| 79 | + client.options_mut().allow_fast_blocks = true; |
| 80 | + let chain_id = client.chain_id(); |
| 81 | + let owner = client.identity().await?; |
| 82 | + |
| 83 | + let timeout_config = TimeoutConfig { |
| 84 | + fast_round_duration: Some(TimeDelta::from_secs(5)), |
| 85 | + ..TimeoutConfig::default() |
| 86 | + }; |
| 87 | + let ownership = ChainOwnership { |
| 88 | + super_owners: BTreeSet::from_iter([owner]), |
| 89 | + owners: BTreeMap::default(), |
| 90 | + first_leader: None, |
| 91 | + multi_leader_rounds: 10, |
| 92 | + open_multi_leader_rounds: false, |
| 93 | + timeout_config, |
| 94 | + }; |
| 95 | + client.change_ownership(ownership).await?; |
| 96 | + |
| 97 | + // Three offline validators make the burn fail; the proposal stays pending. |
| 98 | + builder.set_fault_type([1, 2, 3], FaultType::OfflineWithInfo); |
| 99 | + assert!(client |
| 100 | + .burn(AccountOwner::CHAIN, Amount::from_tokens(3)) |
| 101 | + .await |
| 102 | + .is_err()); |
| 103 | + let pending = client |
| 104 | + .pending_proposal() |
| 105 | + .await |
| 106 | + .expect("expected fast pending proposal after failed burn"); |
| 107 | + assert_eq!(pending.round, Some(Round::Fast)); |
| 108 | + |
| 109 | + let context = make_context(&mut builder, signer, chain_id).await?; |
| 110 | + context.update_wallet_from_client(&client).await?; |
| 111 | + let stored = context |
| 112 | + .wallet() |
| 113 | + .get(chain_id) |
| 114 | + .expect("wallet missing chain entry") |
| 115 | + .pending_fast_proposal |
| 116 | + .expect("wallet missing fast pending proposal"); |
| 117 | + assert_eq!(stored.round, Some(Round::Fast)); |
| 118 | + Ok(()) |
| 119 | +} |
| 120 | + |
| 121 | +/// A non-fast pending proposal must NOT be persisted in the wallet. |
| 122 | +#[test_log::test(tokio::test)] |
| 123 | +async fn test_wallet_drops_non_fast_pending_proposal() -> anyhow::Result<()> { |
| 124 | + let mut signer = InMemorySigner::new(None); |
| 125 | + let mut builder = |
| 126 | + TestBuilder::new(MemoryStorageBuilder::default(), 4, 0, signer.clone()).await?; |
| 127 | + let client = builder.add_root_chain(1, Amount::from_tokens(10)).await?; |
| 128 | + let chain_id = client.chain_id(); |
| 129 | + let owner0 = client.identity().await?; |
| 130 | + let owner1: AccountOwner = signer.generate_new().into(); |
| 131 | + |
| 132 | + let timeout_config = TimeoutConfig { |
| 133 | + fast_round_duration: Some(TimeDelta::from_secs(5)), |
| 134 | + ..TimeoutConfig::default() |
| 135 | + }; |
| 136 | + let ownership = ChainOwnership::multiple([(owner0, 100), (owner1, 100)], 10, timeout_config); |
| 137 | + client.change_ownership(ownership).await?; |
| 138 | + |
| 139 | + // Three offline validators make the burn fail; the proposal stays pending. |
| 140 | + builder.set_fault_type([1, 2, 3], FaultType::OfflineWithInfo); |
| 141 | + assert!(client |
| 142 | + .burn(AccountOwner::CHAIN, Amount::from_tokens(3)) |
| 143 | + .await |
| 144 | + .is_err()); |
| 145 | + let pending = client |
| 146 | + .pending_proposal() |
| 147 | + .await |
| 148 | + .expect("expected non-fast pending proposal after failed burn"); |
| 149 | + assert_eq!(pending.round, Some(Round::MultiLeader(0))); |
| 150 | + |
| 151 | + let context = make_context(&mut builder, signer, chain_id).await?; |
| 152 | + context.update_wallet_from_client(&client).await?; |
| 153 | + let stored = context |
| 154 | + .wallet() |
| 155 | + .get(chain_id) |
| 156 | + .expect("wallet missing chain entry"); |
| 157 | + assert!(stored.pending_fast_proposal.is_none()); |
| 158 | + Ok(()) |
| 159 | +} |
0 commit comments