Skip to content

Commit

Permalink
revert: bring back Ledger::advance_to_next_block
Browse files Browse the repository at this point in the history
Signed-off-by: ljedrz <[email protected]>
  • Loading branch information
ljedrz committed Aug 26, 2022
1 parent 165d1cf commit 4d732e6
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion snarkos/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
mod server;
pub use server::*;

use crate::{handle_dispatch_error, BlockDB, ProgramDB};
use crate::{handle_dispatch_error, BlockDB, Data, ProgramDB};
use snarkvm::prelude::*;

use colored::Colorize;
Expand Down Expand Up @@ -137,6 +137,35 @@ impl<N: Network> Ledger<N> {
self.ledger.write().add_to_memory_pool(transaction)
}

/// Advances the ledger to the next block.
pub async fn advance_to_next_block(self: &Arc<Self>) -> Result<Block<N>> {
let self_clone = self.clone();
let next_block = task::spawn_blocking(move || {
// Initialize an RNG.
let rng = &mut ::rand::thread_rng();
// Propose the next block.
self_clone.ledger.read().propose_next_block(&self_clone.private_key, rng)
})
.await??;

// Add the next block to the ledger.
self.add_next_block(next_block.clone()).await?;

// Serialize the block ahead of time to not do it for each peer.
let serialized_block = Data::Object(next_block.clone()).serialize().await?;

// Broadcast the block to all peers.
let peers = self.peers().read().clone();
for (_, sender) in peers.iter() {
let _ = sender
.send(crate::Message::<N>::BlockBroadcast(Data::Buffer(serialized_block.clone())))
.await;
}

// Return the next block.
Ok(next_block)
}

/// Attempts to add the given block to the ledger.
pub(crate) async fn add_next_block(self: &Arc<Self>, next_block: Block<N>) -> Result<()> {
// Add the next block to the ledger.
Expand Down

0 comments on commit 4d732e6

Please sign in to comment.