Files
nym/ephemera/src/block/builder.rs
T
Bogdan-Ștefan Neacşu ee5b55fab6 Feature/ephemera (#3731)
* Feature/ephemera compile (#3437)

* Include ephemera node code in repo

* Upgrade deps

* Bump minor version of cosmwasm-std

* Include ephemera in nym-api dep and downgrade rusqlite

* Fix clippy and ephemera docs code

* More clippy on ephemera

---------

Co-authored-by: Andrus Salumets <andrus@nymtech.net>

* Start ephemera components in nym-api (#3475)

* Start ephemera components in nym-api

* Pass nyxd client and use common metric structures

* Swap url endpoint with contract for sending rewarding messages

* Fix build after rebase

* Perform ephemera rewards computation before normal nym-api ones

* Remove contract mock from ephemera

* Take raw rewards from network monitor

* Remove ephemera old reward version

* Use nym shutdown procedure in ephemera

* Temporary fix for some warnings

* Umock contract membership of ephemera (#3574)

* Pass nyxd client to members provider

* Basic ephemera contract

* Add register peer tx

* Add query all peers

* Nyxd ephemera client

* Add registration of ephemera peer

* Replace epoch http api with actual contract

* Merge ephemera config into nym-api config

* Load cluster from contract

* Guard nym-outfox out of cosmwasm builds (#3650)

* Feature/fixes while testing (#3668)

* Commit local peer before querying contract

* Default to anyonline

* Remove string from template

* Fix avg computing

* Use updated qa env

* Fix clippy

* Add unit tests for ephemera contract

* Upload ephemera contract in CI

* Add group check for peer signup

* Peer registration unit test

* Start ephemera only on monitoring

* Remove old MixnodeToReward struct

* Move all ephemera config to its file

* Skip with serde ephemera config

* Fix default value in args

* Feature/add ephemera flag (#3727)

* Replace unwrap with error handling

* Add ephemera enable flag

* Fix template

* Add json schema to ephemera contract (#3735)

* Update lock files

* Update changelog

---------

Co-authored-by: Andrus Salumets <andrus@nymtech.net>
2023-08-18 14:14:13 +03:00

75 lines
2.4 KiB
Rust

use std::collections::HashSet;
use std::{sync::Arc, time::Duration};
use log::{debug, info};
use crate::block::manager::State;
use crate::peer::ToPeerId;
use crate::{
block::{
manager::{BlockChainState, BlockManager},
message_pool::MessagePool,
producer::BlockProducer,
types::block::Block,
},
broadcast::signing::BlockSigner,
config::BlockManagerConfiguration,
crypto::Keypair,
storage::EphemeraDatabase,
};
pub(crate) struct BlockManagerBuilder {
config: BlockManagerConfiguration,
block_producer: BlockProducer,
keypair: Arc<Keypair>,
}
impl BlockManagerBuilder {
pub(crate) fn new(config: BlockManagerConfiguration, keypair: Arc<Keypair>) -> Self {
let block_producer = BlockProducer::new(keypair.peer_id());
Self {
config,
block_producer,
keypair,
}
}
pub(crate) fn build<D: EphemeraDatabase + ?Sized>(
self,
storage: &mut D,
) -> anyhow::Result<BlockManager> {
let mut most_recent_block = storage.get_last_block()?;
if most_recent_block.is_none() {
//Although Ephemera is not a blockchain(chain of historically dependent blocks),
//it's helpful to have some sort of notion of progress in time. So we use the concept of height.
//The genesis block helps to define the start of it.
info!("No last block found in database. Creating genesis block.");
let genesis_block = Block::new_genesis_block(self.block_producer.peer_id);
storage.store_block(&genesis_block, HashSet::new(), HashSet::new())?;
most_recent_block = Some(genesis_block);
}
let last_created_block = most_recent_block.expect("Block should be present");
debug!("Most recent block: {:?}", last_created_block);
let block_signer = BlockSigner::new(self.keypair.clone());
let message_pool = MessagePool::new();
let block_chain_state = BlockChainState::new(last_created_block);
let block_creation_interval =
tokio::time::interval(Duration::from_secs(self.config.creation_interval_sec));
Ok(BlockManager {
config: self.config,
block_producer: self.block_producer,
block_signer,
message_pool,
block_chain_state,
state: State::Paused,
backoff: None,
block_creation_interval,
})
}
}