Skip to main content

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, ChainWorkerConfig};
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 config = ChainWorkerConfig {
92            nickname: "Single validator node".to_string(),
93            key_pair: Some(Arc::new(validator_keypair.secret_key.copy())),
94            ..ChainWorkerConfig::default()
95        };
96        let worker = WorkerState::new(storage.clone(), config, None);
97
98        // Create an admin chain.
99        let key_pair = AccountSecretKey::generate();
100
101        let new_chain_config = InitialChainConfig {
102            ownership: ChainOwnership::single(key_pair.public().into()),
103            epoch,
104            balance: Amount::from_tokens(1_000_000),
105            application_permissions: ApplicationPermissions::default(),
106        };
107
108        let origin = ChainOrigin::Root(0);
109        let description = ChainDescription::new(origin, new_chain_config, Timestamp::from(0));
110        let admin_chain_id = description.id();
111
112        let committee_blob = Blob::new_committee(
113            bcs::to_bytes(&committee).expect("serializing a committee should succeed"),
114        );
115
116        let network_description = NetworkDescription {
117            name: "Test network".to_string(),
118            genesis_config_hash: CryptoHash::test_hash("genesis config"),
119            genesis_timestamp: description.timestamp(),
120            genesis_committee_blob_hash: committee_blob.id().hash,
121            admin_chain_id,
122        };
123        storage
124            .write_network_description(&network_description)
125            .await
126            .unwrap();
127        storage
128            .write_blob(&committee_blob)
129            .await
130            .expect("writing a blob should succeed");
131        worker
132            .storage_client()
133            .create_chain(description.clone())
134            .await
135            .expect("Failed to create root admin chain");
136
137        let validator = TestValidator {
138            validator_secret: validator_keypair.secret_key,
139            account_secret,
140            committee: Arc::new(Mutex::new((epoch, committee))),
141            storage,
142            worker,
143            clock,
144            admin_chain_id,
145            chains: Arc::default(),
146        };
147
148        let chain = ActiveChain::new(key_pair, description.clone(), validator.clone());
149
150        validator.chains.pin().insert(description.id(), chain);
151
152        validator
153    }
154
155    /// Creates a new [`TestValidator`] with a single microchain with the bytecode of the crate
156    /// calling this method published on it.
157    ///
158    /// Returns the new [`TestValidator`] and the [`ModuleId`] of the published module.
159    pub async fn with_current_module<Abi, Parameters, InstantiationArgument>() -> (
160        TestValidator,
161        ModuleId<Abi, Parameters, InstantiationArgument>,
162    ) {
163        let validator = TestValidator::new().await;
164        let publisher = Box::pin(validator.new_chain()).await;
165
166        let module_id = Box::pin(publisher.publish_current_module()).await;
167
168        (validator, module_id)
169    }
170
171    /// Creates a new [`TestValidator`] with the application of the crate calling this method
172    /// created on a chain.
173    ///
174    /// The bytecode is first published on one microchain, then the application is created on
175    /// another microchain.
176    ///
177    /// Returns the new [`TestValidator`], the [`ApplicationId`] of the created application, and
178    /// the chain on which it was created.
179    pub async fn with_current_application<Abi, Parameters, InstantiationArgument>(
180        parameters: Parameters,
181        instantiation_argument: InstantiationArgument,
182    ) -> (TestValidator, ApplicationId<Abi>, ActiveChain)
183    where
184        Abi: ContractAbi,
185        Parameters: Serialize,
186        InstantiationArgument: Serialize,
187    {
188        let (validator, module_id) = Box::pin(TestValidator::with_current_module::<
189            Abi,
190            Parameters,
191            InstantiationArgument,
192        >())
193        .await;
194
195        let mut creator = Box::pin(validator.new_chain()).await;
196
197        let application_id = creator
198            .create_application(module_id, parameters, instantiation_argument, vec![])
199            .await;
200
201        (validator, application_id, creator)
202    }
203
204    /// Returns this validator's storage.
205    pub(crate) fn storage(&self) -> &DbStorage<MemoryDatabase, TestClock> {
206        &self.storage
207    }
208
209    /// Returns the locked [`WorkerState`] of this validator.
210    pub(crate) fn worker(&self) -> WorkerState<DbStorage<MemoryDatabase, TestClock>> {
211        self.worker.clone()
212    }
213
214    /// Returns the [`TestClock`] of this validator.
215    pub fn clock(&self) -> &TestClock {
216        &self.clock
217    }
218
219    /// Returns the keys this test validator uses for signing certificates.
220    pub fn key_pair(&self) -> &ValidatorSecretKey {
221        &self.validator_secret
222    }
223
224    /// Returns the ID of the admin chain.
225    pub fn admin_chain_id(&self) -> ChainId {
226        self.admin_chain_id
227    }
228
229    /// Returns the latest committee that this test validator is part of.
230    ///
231    /// The committee contains only this validator.
232    pub async fn committee(&self) -> MappedMutexGuard<'_, (Epoch, Committee), Committee> {
233        MutexGuard::map(self.committee.lock().await, |(_epoch, committee)| committee)
234    }
235
236    /// Updates the admin chain, creating a new epoch with an updated
237    /// [`ResourceControlPolicy`].
238    pub async fn change_resource_control_policy(
239        &mut self,
240        adjustment: impl FnOnce(&mut ResourceControlPolicy),
241    ) {
242        let (epoch, committee) = {
243            let (ref mut epoch, ref mut committee) = &mut *self.committee.lock().await;
244
245            epoch
246                .try_add_assign_one()
247                .expect("Reached the limit of epochs");
248
249            adjustment(committee.policy_mut());
250
251            (*epoch, committee.clone())
252        };
253
254        let admin_chain = self.get_chain(&self.admin_chain_id);
255
256        let committee_blob = Blob::new(BlobContent::new_committee(
257            bcs::to_bytes(&committee).unwrap(),
258        ));
259        let blob_hash = committee_blob.id().hash;
260        self.storage
261            .write_blob(&committee_blob)
262            .await
263            .expect("Should write committee blob");
264
265        Box::pin(admin_chain.add_block(|block| {
266            block.with_system_operation(SystemOperation::Admin(AdminOperation::CreateCommittee {
267                epoch,
268                blob_hash,
269            }));
270        }))
271        .await;
272
273        let pinned = self.chains.pin();
274        for chain in pinned.values() {
275            if chain.id() != self.admin_chain_id {
276                Box::pin(chain.add_block(|block| {
277                    block.with_system_operation(SystemOperation::ProcessNewEpoch(epoch));
278                }))
279                .await;
280            }
281        }
282    }
283
284    /// Creates a new microchain and returns the [`ActiveChain`] that can be used to add blocks to
285    /// it with the given key pair.
286    pub async fn new_chain_with_keypair(&self, key_pair: AccountSecretKey) -> ActiveChain {
287        let description =
288            Box::pin(self.request_new_chain_from_admin_chain(key_pair.public().into())).await;
289        let chain = ActiveChain::new(key_pair, description.clone(), self.clone());
290
291        Box::pin(chain.handle_received_messages()).await;
292
293        self.chains.pin().insert(description.id(), chain.clone());
294
295        chain
296    }
297
298    /// Creates a new microchain and returns the [`ActiveChain`] that can be used to add blocks to
299    /// it.
300    pub async fn new_chain(&self) -> ActiveChain {
301        let key_pair = AccountSecretKey::generate();
302        Box::pin(self.new_chain_with_keypair(key_pair)).await
303    }
304
305    /// Adds an existing [`ActiveChain`].
306    pub fn add_chain(&self, chain: ActiveChain) {
307        self.chains.pin().insert(chain.id(), chain);
308    }
309
310    /// Adds a block to the admin chain to create a new chain.
311    ///
312    /// Returns the [`ChainDescription`] of the new chain.
313    async fn request_new_chain_from_admin_chain(&self, owner: AccountOwner) -> ChainDescription {
314        let admin_chain_id = self.admin_chain_id;
315        let pinned = self.chains.pin();
316        let admin_chain = pinned
317            .get(&admin_chain_id)
318            .expect("Admin chain should be created when the `TestValidator` is constructed");
319
320        let open_chain_config = OpenChainConfig {
321            ownership: ChainOwnership::single(owner),
322            balance: Amount::from_tokens(10),
323            application_permissions: ApplicationPermissions::default(),
324        };
325
326        let chain_state = Box::pin(self.worker.chain_state_view(admin_chain_id))
327            .await
328            .expect("Failed to read admin chain state");
329        let epoch = *chain_state.execution_state.system.epoch.get();
330        drop(chain_state);
331
332        let new_chain_config = open_chain_config.init_chain_config(epoch);
333
334        let (certificate, _) = Box::pin(admin_chain.add_block(|block| {
335            block.with_system_operation(SystemOperation::OpenChain(open_chain_config));
336        }))
337        .await;
338        let block = certificate.inner().block();
339
340        let origin = ChainOrigin::Child {
341            parent: block.header.chain_id,
342            block_height: block.header.height,
343            chain_index: 0,
344        };
345
346        ChainDescription::new(origin, new_chain_config, Timestamp::from(0))
347    }
348
349    /// Returns the [`ActiveChain`] reference to the microchain identified by `chain_id`.
350    pub fn get_chain(&self, chain_id: &ChainId) -> ActiveChain {
351        self.chains
352            .pin()
353            .get(chain_id)
354            .expect("Chain not found")
355            .clone()
356    }
357}