Skip to content

Commit

Permalink
node: Instantiate Acceptor in ChainSrv::Initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
goshawk-3 authored and goshawk-3 committed Feb 27, 2024
1 parent ec2ac2c commit 8c518df
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
50 changes: 32 additions & 18 deletions node/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,29 @@ const TOPICS: &[u8] = &[
const ACCEPT_BLOCK_TIMEOUT_SEC: Duration = Duration::from_secs(20);
const HEARTBEAT_SEC: Duration = Duration::from_secs(1);

pub struct ChainSrv {
pub struct ChainSrv<N: Network, DB: database::DB, VM: vm::VMExecution> {
/// Inbound wire messages queue
inbound: AsyncQueue<Message>,
keys_path: String,
acceptor: Option<Arc<RwLock<Acceptor<N, DB, VM>>>>,
}

#[async_trait]
impl<N: Network, DB: database::DB, VM: vm::VMExecution>
LongLivedService<N, DB, VM> for ChainSrv
LongLivedService<N, DB, VM> for ChainSrv<N, DB, VM>
{
async fn execute(
async fn initialize(
&mut self,
network: Arc<RwLock<N>>,
db: Arc<RwLock<DB>>,
vm: Arc<RwLock<VM>>,
) -> anyhow::Result<usize> {
// Register routes
LongLivedService::<N, DB, VM>::add_routes(
self,
TOPICS,
self.inbound.clone(),
&network,
)
.await?;

// Restore/Load most recent block
) -> anyhow::Result<()> {
let mrb = Self::load_most_recent_block(db.clone(), vm.clone()).await?;

let state_hash = mrb.inner().header().state_hash;
let provisioners_list = vm.read().await.get_provisioners(state_hash)?;

// Initialize Acceptor and trigger consensus task
// Initialize Acceptor
let acc = Acceptor::init_consensus(
&self.keys_path,
mrb,
Expand All @@ -85,7 +76,29 @@ impl<N: Network, DB: database::DB, VM: vm::VMExecution>
vm.clone(),
)
.await?;
let acc = Arc::new(RwLock::new(acc));

self.acceptor = Some(Arc::new(RwLock::new(acc)));

Ok(())
}

async fn execute(
&mut self,
network: Arc<RwLock<N>>,
_db: Arc<RwLock<DB>>,
_vm: Arc<RwLock<VM>>,
) -> anyhow::Result<usize> {
// Register routes
LongLivedService::<N, DB, VM>::add_routes(
self,
TOPICS,
self.inbound.clone(),
&network,
)
.await?;

let acc = self.acceptor.as_mut().unwrap();
acc.write().await.spawn_task().await;

// Start-up FSM instance
let mut fsm = SimpleFSM::new(acc.clone(), network.clone());
Expand Down Expand Up @@ -200,11 +213,12 @@ impl<N: Network, DB: database::DB, VM: vm::VMExecution>
}
}

impl ChainSrv {
impl<N: Network, DB: database::DB, VM: vm::VMExecution> ChainSrv<N, DB, VM> {
pub fn new(keys_path: String) -> Self {
Self {
inbound: Default::default(),
keys_path,
acceptor: None,
}
}

Expand All @@ -213,7 +227,7 @@ impl ChainSrv {
/// Panics
///
/// If register entry is read but block is not found.
async fn load_most_recent_block<DB: database::DB, VM: vm::VMExecution>(
async fn load_most_recent_block(
db: Arc<RwLock<DB>>,
vm: Arc<RwLock<VM>>,
) -> Result<BlockWithLabel> {
Expand Down
3 changes: 1 addition & 2 deletions node/src/chain/acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,10 @@ impl<DB: database::DB, VM: vm::VMExecution, N: Network> Acceptor<N, DB, VM> {
acc.try_revert(RevertTarget::LastFinalizedState).await?;
}

acc.spawn_task().await;
Ok(acc)
}

async fn spawn_task(&self) {
pub async fn spawn_task(&self) {
let provisioners_list = self.provisioners_list.read().await.clone();
let base_timeouts = self.adjust_round_base_timeouts().await;

Expand Down

0 comments on commit 8c518df

Please sign in to comment.