From 6ee551bdbd2e57bb93bdbac1fa91cb7806e90207 Mon Sep 17 00:00:00 2001 From: parazyd Date: Tue, 26 Apr 2022 16:29:20 +0200 Subject: [PATCH] consensus/state: Make State and Client be part of ValidatorState. --- src/blockchain/nfstore.rs | 1 + src/blockchain/rootstore.rs | 1 + src/consensus/state.rs | 27 ++++++++++++++++++++++++--- src/consensus/task/block_sync.rs | 4 ++-- src/node/client.rs | 2 +- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/blockchain/nfstore.rs b/src/blockchain/nfstore.rs index 4c206efee..1c0f33975 100644 --- a/src/blockchain/nfstore.rs +++ b/src/blockchain/nfstore.rs @@ -8,6 +8,7 @@ use crate::{ const SLED_NULLIFIER_TREE: &[u8] = b"_nullifiers"; +#[derive(Clone)] pub struct NullifierStore(sled::Tree); impl NullifierStore { diff --git a/src/blockchain/rootstore.rs b/src/blockchain/rootstore.rs index 617cf3027..bcb5aa02a 100644 --- a/src/blockchain/rootstore.rs +++ b/src/blockchain/rootstore.rs @@ -8,6 +8,7 @@ use crate::{ const SLED_ROOTS_TREE: &[u8] = b"_merkleroots"; +#[derive(Clone)] pub struct RootStore(sled::Tree); impl RootStore { diff --git a/src/consensus/state.rs b/src/consensus/state.rs index 3d283b77d..c8b6a990f 100644 --- a/src/consensus/state.rs +++ b/src/consensus/state.rs @@ -5,8 +5,9 @@ use std::{ time::Duration, }; -use async_std::sync::{Arc, RwLock}; +use async_std::sync::{Arc, Mutex, RwLock}; use chrono::{NaiveDateTime, Utc}; +use lazy_init::Lazy; use log::{debug, error, info, warn}; use rand::rngs::OsRng; @@ -22,6 +23,7 @@ use crate::{ schnorr::{SchnorrPublic, SchnorrSecret}, }, net, + node::{Client, State}, util::serial::{serialize, Encodable, SerialDecodable, SerialEncodable}, Result, }; @@ -107,6 +109,10 @@ pub struct ValidatorState { pub consensus: ConsensusState, /// Canonical (finalized) blockchain pub blockchain: Blockchain, + /// Canonical state machine + pub state_machine: Arc>, + /// Client providing wallet access + pub client: Client, /// Pending transactions pub unconfirmed_txs: Vec, /// Participation flag @@ -115,11 +121,13 @@ pub struct ValidatorState { impl ValidatorState { // TODO: Clock sync - pub fn new( + pub async fn new( db: &sled::Db, // <-- TODO: Avoid this with some wrapping, sled should only be in blockchain - address: Address, genesis_ts: Timestamp, genesis_data: blake3::Hash, + client: Client, + cashier_pubkeys: Vec, + faucet_pubkeys: Vec, ) -> Result { let secret = SecretKey::random(&mut OsRng); let public = PublicKey::from_secret(secret); @@ -128,12 +136,25 @@ impl ValidatorState { let unconfirmed_txs = vec![]; let participating = false; + let address = client.wallet.get_default_address().await?; + let state_machine = Arc::new(Mutex::new(State { + tree: client.get_tree().await?, + merkle_roots: blockchain.merkle_roots.clone(), + nullifiers: blockchain.nullifiers.clone(), + cashier_pubkeys, + faucet_pubkeys, + mint_vk: Lazy::new(), + burn_vk: Lazy::new(), + })); + let state = Arc::new(RwLock::new(ValidatorState { address, secret, public, consensus, blockchain, + state_machine, + client, unconfirmed_txs, participating, })); diff --git a/src/consensus/task/block_sync.rs b/src/consensus/task/block_sync.rs index 3ee0c875c..b78cdf221 100644 --- a/src/consensus/task/block_sync.rs +++ b/src/consensus/task/block_sync.rs @@ -36,8 +36,8 @@ pub async fn block_sync_task(p2p: net::P2pPtr, state: ValidatorStatePtr) -> Resu channel.send(order).await?; // Node stores response data. Extra validations can be added here. - let response = response_sub.receive().await?; - state.write().await.blockchain.add(&response.blocks)?; + let resp = response_sub.receive().await?; + state.write().await.blockchain.add(&resp.blocks)?; let last_received = state.read().await.blockchain.last()?.unwrap(); info!("Last received block: {:?} - {:?}", last_received.0, last_received.1); diff --git a/src/node/client.rs b/src/node/client.rs index d5927fde2..7fbdadd59 100644 --- a/src/node/client.rs +++ b/src/node/client.rs @@ -31,7 +31,7 @@ use crate::{ /// This includes, receiving, broadcasting, and building. pub struct Client { pub main_keypair: Mutex, - wallet: WalletPtr, + pub wallet: WalletPtr, mint_pk: Lazy, burn_pk: Lazy, }