linera_sdk/test/
validator.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A minimal validator implementation suited for tests.
5//!
6//! The [`TestValidator`] is a minimal validator with a single shard. Micro-chains can be added to
7//! it, and blocks can be added to each microchain individually.
8
9use std::sync::Arc;
10
11use futures::{
12    lock::{MappedMutexGuard, Mutex, MutexGuard},
13    FutureExt as _,
14};
15use linera_base::{
16    crypto::{AccountSecretKey, CryptoHash, ValidatorKeypair, ValidatorSecretKey},
17    data_types::{
18        Amount, ApplicationPermissions, Blob, BlobContent, ChainDescription, ChainOrigin, Epoch,
19        InitialChainConfig, NetworkDescription, Timestamp,
20    },
21    identifiers::{AccountOwner, ApplicationId, ChainId, ModuleId},
22    ownership::ChainOwnership,
23};
24use linera_core::worker::WorkerState;
25use linera_execution::{
26    committee::Committee,
27    system::{AdminOperation, OpenChainConfig, SystemOperation},
28    ResourceControlPolicy, WasmRuntime,
29};
30use linera_storage::{DbStorage, Storage, TestClock};
31use linera_views::memory::MemoryDatabase;
32use serde::Serialize;
33
34use super::ActiveChain;
35use crate::ContractAbi;
36
37/// A minimal validator implementation suited for tests.
38///
39/// ```rust
40/// # use linera_sdk::test::*;
41/// # use linera_base::{data_types::Amount, identifiers::ChainId};
42/// # tokio_test::block_on(async {
43/// let validator = TestValidator::new().await;
44/// assert_eq!(
45///     validator.new_chain().await.chain_balance().await,
46///     Amount::from_tokens(10)
47/// );
48/// # });
49/// ```
50pub struct TestValidator {
51    validator_secret: ValidatorSecretKey,
52    account_secret: AccountSecretKey,
53    committee: Arc<Mutex<(Epoch, Committee)>>,
54    storage: DbStorage<MemoryDatabase, TestClock>,
55    worker: WorkerState<DbStorage<MemoryDatabase, TestClock>>,
56    clock: TestClock,
57    admin_chain_id: ChainId,
58    chains: Arc<papaya::HashMap<ChainId, ActiveChain>>,
59}
60
61impl Clone for TestValidator {
62    fn clone(&self) -> Self {
63        TestValidator {
64            admin_chain_id: self.admin_chain_id,
65            validator_secret: self.validator_secret.copy(),
66            account_secret: self.account_secret.copy(),
67            committee: self.committee.clone(),
68            storage: self.storage.clone(),
69            worker: self.worker.clone(),
70            clock: self.clock.clone(),
71            chains: self.chains.clone(),
72        }
73    }
74}
75
76impl TestValidator {
77    /// Creates a new [`TestValidator`].
78    pub async fn new() -> Self {
79        let validator_keypair = ValidatorKeypair::generate();
80        let account_secret = AccountSecretKey::generate();
81        let epoch = Epoch::ZERO;
82        let committee = Committee::make_simple(vec![(
83            validator_keypair.public_key,
84            account_secret.public(),
85        )]);
86        let wasm_runtime = Some(WasmRuntime::default());
87        let storage = DbStorage::<MemoryDatabase, _>::make_test_storage(wasm_runtime)
88            .now_or_never()
89            .expect("execution of DbStorage::new should not await anything");
90        let clock = storage.clock().clone();
91        let worker = WorkerState::new(
92            "Single validator node".to_string(),
93            Some(validator_keypair.secret_key.copy()),
94            storage.clone(),
95        );
96
97        // Create an admin chain.
98        let key_pair = AccountSecretKey::generate();
99
100        let new_chain_config = InitialChainConfig {
101            ownership: ChainOwnership::single(key_pair.public().into()),
102            min_active_epoch: epoch,
103            max_active_epoch: epoch,
104            epoch,
105            balance: Amount::from_tokens(1_000_000),
106            application_permissions: ApplicationPermissions::default(),
107        };
108
109        let origin = ChainOrigin::Root(0);
110        let description = ChainDescription::new(origin, new_chain_config, Timestamp::from(0));
111        let admin_chain_id = description.id();
112
113        let committee_blob = Blob::new_committee(
114            bcs::to_bytes(&committee).expect("serializing a committee should succeed"),
115        );
116
117        let network_description = NetworkDescription {
118            name: "Test network".to_string(),
119            genesis_config_hash: CryptoHash::test_hash("genesis config"),
120            genesis_timestamp: description.timestamp(),
121            genesis_committee_blob_hash: committee_blob.id().hash,
122            admin_chain_id,
123        };
124        storage
125            .write_network_description(&network_description)
126            .await
127            .unwrap();
128        storage
129            .write_blob(&committee_blob)
130            .await
131            .expect("writing a blob should succeed");
132        worker
133            .storage_client()
134            .create_chain(description.clone())
135            .await
136            .expect("Failed to create root admin chain");
137
138        let validator = TestValidator {
139            validator_secret: validator_keypair.secret_key,
140            account_secret,
141            committee: Arc::new(Mutex::new((epoch, committee))),
142            storage,
143            worker,
144            clock,
145            admin_chain_id,
146            chains: Arc::default(),
147        };
148
149        let chain = ActiveChain::new(key_pair, description.clone(), validator.clone());
150
151        validator.chains.pin().insert(description.id(), chain);
152
153        validator
154    }
155
156    /// Creates a new [`TestValidator`] with a single microchain with the bytecode of the crate
157    /// calling this method published on it.
158    ///
159    /// Returns the new [`TestValidator`] and the [`ModuleId`] of the published module.
160    pub async fn with_current_module<Abi, Parameters, InstantiationArgument>() -> (
161        TestValidator,
162        ModuleId<Abi, Parameters, InstantiationArgument>,
163    ) {
164        let validator = TestValidator::new().await;
165        let publisher = Box::pin(validator.new_chain()).await;
166
167        let module_id = publisher.publish_current_module().await;
168
169        (validator, module_id)
170    }
171
172    /// Creates a new [`TestValidator`] with the application of the crate calling this method
173    /// created on a chain.
174    ///
175    /// The bytecode is first published on one microchain, then the application is created on
176    /// another microchain.
177    ///
178    /// Returns the new [`TestValidator`], the [`ApplicationId`] of the created application, and
179    /// the chain on which it was created.
180    pub async fn with_current_application<Abi, Parameters, InstantiationArgument>(
181        parameters: Parameters,
182        instantiation_argument: InstantiationArgument,
183    ) -> (TestValidator, ApplicationId<Abi>, ActiveChain)
184    where
185        Abi: ContractAbi,
186        Parameters: Serialize,
187        InstantiationArgument: Serialize,
188    {
189        let (validator, module_id) =
190            TestValidator::with_current_module::<Abi, Parameters, InstantiationArgument>().await;
191
192        let mut creator = validator.new_chain().await;
193
194        let application_id = creator
195            .create_application(module_id, parameters, instantiation_argument, vec![])
196            .await;
197
198        (validator, application_id, creator)
199    }
200
201    /// Returns this validator's storage.
202    pub(crate) fn storage(&self) -> &DbStorage<MemoryDatabase, TestClock> {
203        &self.storage
204    }
205
206    /// Returns the locked [`WorkerState`] of this validator.
207    pub(crate) fn worker(&self) -> WorkerState<DbStorage<MemoryDatabase, TestClock>> {
208        self.worker.clone()
209    }
210
211    /// Returns the [`TestClock`] of this validator.
212    pub fn clock(&self) -> &TestClock {
213        &self.clock
214    }
215
216    /// Returns the keys this test validator uses for signing certificates.
217    pub fn key_pair(&self) -> &ValidatorSecretKey {
218        &self.validator_secret
219    }
220
221    /// Returns the ID of the admin chain.
222    pub fn admin_chain_id(&self) -> ChainId {
223        self.admin_chain_id
224    }
225
226    /// Returns the latest committee that this test validator is part of.
227    ///
228    /// The committee contains only this validator.
229    pub async fn committee(&self) -> MappedMutexGuard<'_, (Epoch, Committee), Committee> {
230        MutexGuard::map(self.committee.lock().await, |(_epoch, committee)| committee)
231    }
232
233    /// Updates the admin chain, creating a new epoch with an updated
234    /// [`ResourceControlPolicy`].
235    pub async fn change_resource_control_policy(
236        &mut self,
237        adjustment: impl FnOnce(&mut ResourceControlPolicy),
238    ) {
239        let (epoch, committee) = {
240            let (ref mut epoch, ref mut committee) = &mut *self.committee.lock().await;
241
242            epoch
243                .try_add_assign_one()
244                .expect("Reached the limit of epochs");
245
246            adjustment(committee.policy_mut());
247
248            (*epoch, committee.clone())
249        };
250
251        let admin_chain = self.get_chain(&self.admin_chain_id);
252
253        let committee_blob = Blob::new(BlobContent::new_committee(
254            bcs::to_bytes(&committee).unwrap(),
255        ));
256        let blob_hash = committee_blob.id().hash;
257        self.storage
258            .write_blob(&committee_blob)
259            .await
260            .expect("Should write committee blob");
261
262        admin_chain
263            .add_block(|block| {
264                block.with_system_operation(SystemOperation::Admin(
265                    AdminOperation::CreateCommittee { epoch, blob_hash },
266                ));
267            })
268            .await;
269
270        let pinned = self.chains.pin();
271        for chain in pinned.values() {
272            if chain.id() != self.admin_chain_id {
273                chain
274                    .add_block(|block| {
275                        block.with_system_operation(SystemOperation::ProcessNewEpoch(epoch));
276                    })
277                    .await;
278            }
279        }
280    }
281
282    /// Creates a new microchain and returns the [`ActiveChain`] that can be used to add blocks to
283    /// it with the given key pair.
284    pub async fn new_chain_with_keypair(&self, key_pair: AccountSecretKey) -> ActiveChain {
285        let description = self
286            .request_new_chain_from_admin_chain(key_pair.public().into())
287            .await;
288        let chain = ActiveChain::new(key_pair, description.clone(), self.clone());
289
290        chain.handle_received_messages().await;
291
292        self.chains.pin().insert(description.id(), chain.clone());
293
294        chain
295    }
296
297    /// Creates a new microchain and returns the [`ActiveChain`] that can be used to add blocks to
298    /// it.
299    pub async fn new_chain(&self) -> ActiveChain {
300        let key_pair = AccountSecretKey::generate();
301        self.new_chain_with_keypair(key_pair).await
302    }
303
304    /// Adds an existing [`ActiveChain`].
305    pub fn add_chain(&self, chain: ActiveChain) {
306        self.chains.pin().insert(chain.id(), chain);
307    }
308
309    /// Adds a block to the admin chain to create a new chain.
310    ///
311    /// Returns the [`ChainDescription`] of the new chain.
312    async fn request_new_chain_from_admin_chain(&self, owner: AccountOwner) -> ChainDescription {
313        let admin_id = self.admin_chain_id;
314        let pinned = self.chains.pin();
315        let admin_chain = pinned
316            .get(&admin_id)
317            .expect("Admin chain should be created when the `TestValidator` is constructed");
318
319        let (epoch, _) = self.committee.lock().await.clone();
320
321        let open_chain_config = OpenChainConfig {
322            ownership: ChainOwnership::single(owner),
323            balance: Amount::from_tokens(10),
324            application_permissions: ApplicationPermissions::default(),
325        };
326        let new_chain_config = open_chain_config.init_chain_config(epoch, epoch, epoch);
327
328        let certificate = admin_chain
329            .add_block(|block| {
330                block.with_system_operation(SystemOperation::OpenChain(open_chain_config));
331            })
332            .await;
333        let block = certificate.inner().block();
334
335        let origin = ChainOrigin::Child {
336            parent: block.header.chain_id,
337            block_height: block.header.height,
338            chain_index: 0,
339        };
340
341        ChainDescription::new(origin, new_chain_config, Timestamp::from(0))
342    }
343
344    /// Returns the [`ActiveChain`] reference to the microchain identified by `chain_id`.
345    pub fn get_chain(&self, chain_id: &ChainId) -> ActiveChain {
346        self.chains
347            .pin()
348            .get(chain_id)
349            .expect("Chain not found")
350            .clone()
351    }
352}