Skip to content

Commit

Permalink
refactor FinalizationHandler to expose intrinsics like creator and ro…
Browse files Browse the repository at this point in the history
…und of a finalized unit
  • Loading branch information
joschisan committed Mar 12, 2024
1 parent a48614e commit 0f8d418
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ More details are available [in the book][reference-link-implementation-details].
- Import AlephBFT in your crate
```toml
[dependencies]
aleph-bft = "^0.35"
aleph-bft = "^0.36"
```
- The main entry point is the `run_session` function, which returns a Future that runs the
consensus algorithm.
Expand Down
4 changes: 2 additions & 2 deletions consensus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aleph-bft"
version = "0.35.0"
version = "0.36.0"
edition = "2021"
authors = ["Cardinal Cryptography"]
categories = ["algorithms", "data-structures", "cryptography", "database"]
Expand All @@ -14,7 +14,7 @@ description = "AlephBFT is an asynchronous and Byzantine fault tolerant consensu

[dependencies]
aleph-bft-rmc = { path = "../rmc", version = "0.12" }
aleph-bft-types = { path = "../types", version = "0.12" }
aleph-bft-types = { path = "../types", version = "0.13" }
anyhow = "1.0"
async-trait = "0.1"
codec = { package = "parity-scale-codec", version = "3.0", default-features = false, features = ["derive"] }
Expand Down
29 changes: 12 additions & 17 deletions consensus/src/runway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,23 +649,18 @@ where
}

fn on_ordered_batch(&mut self, batch: Vec<H::Hash>) {
let data_iter: Vec<_> = batch
.iter()
.map(|h| {
let unit = self
.store
.unit_by_hash(h)
.expect("Ordered units must be in store")
.as_signable();

(unit.data().clone(), unit.creator())
})
.collect();

for (d, creator) in data_iter {
if let Some(d) = d {
self.finalization_handler.data_finalized(d, creator);
}
for hash in batch {
let unit = self
.store
.unit_by_hash(&hash)
.expect("Ordered units must be in store")
.as_signable();

self.finalization_handler.unit_finalized(
unit.creator(),
unit.round(),
unit.data().clone(),
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/ordering/src/dataio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub struct FinalizationHandler {
}

impl FinalizationHandlerT<Data> for FinalizationHandler {
fn data_finalized(&mut self, d: Data, _creator: NodeIndex) {
fn data_finalized(&mut self, d: Data) {
if let Err(e) = self.tx.unbounded_send(d) {
error!(target: "finalization-handler", "Error when sending data from FinalizationHandler {:?}.", e);
}
Expand Down
2 changes: 1 addition & 1 deletion mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "./README.md"
description = "Mock implementations of traits required by the aleph-bft package. Do NOT use outside of testing!"

[dependencies]
aleph-bft-types = { path = "../types", version = "0.12" }
aleph-bft-types = { path = "../types", version = "0.13" }
async-trait = "0.1"
codec = { package = "parity-scale-codec", version = "3.0", default-features = false, features = ["derive"] }
futures = "0.3"
Expand Down
6 changes: 2 additions & 4 deletions mock/src/dataio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use aleph_bft_types::{
DataProvider as DataProviderT, FinalizationHandler as FinalizationHandlerT, NodeIndex,
};
use aleph_bft_types::{DataProvider as DataProviderT, FinalizationHandler as FinalizationHandlerT};
use async_trait::async_trait;
use codec::{Decode, Encode};
use futures::{channel::mpsc::unbounded, future::pending, AsyncWrite};
Expand Down Expand Up @@ -75,7 +73,7 @@ pub struct FinalizationHandler {
}

impl FinalizationHandlerT<Data> for FinalizationHandler {
fn data_finalized(&mut self, d: Data, _creator: NodeIndex) {
fn data_finalized(&mut self, d: Data) {
if let Err(e) = self.tx.unbounded_send(d) {
error!(target: "finalization-handler", "Error when sending data from FinalizationHandler {:?}.", e);
}
Expand Down
2 changes: 1 addition & 1 deletion rmc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ description = "Reliable MultiCast - a primitive for Reliable Broadcast protocol.

[dependencies]
aleph-bft-crypto = { path = "../crypto", version = "0.9" }
aleph-bft-types = { path = "../types", version = "0.12" }
aleph-bft-types = { path = "../types", version = "0.13" }
async-trait = "0.1"
codec = { package = "parity-scale-codec", version = "3.0", default-features = false, features = ["derive"] }
futures = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aleph-bft-types"
version = "0.12.0"
version = "0.13.0"
edition = "2021"
authors = ["Cardinal Cryptography"]
documentation = "https://docs.rs/?"
Expand Down
12 changes: 11 additions & 1 deletion types/src/dataio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;

use crate::NodeIndex;
use crate::Round;

/// The source of data items that consensus should order.
///
Expand All @@ -21,5 +22,14 @@ pub trait DataProvider<Data>: Sync + Send + 'static {
pub trait FinalizationHandler<Data>: Sync + Send + 'static {
/// Data, provided by [DataProvider::get_data], has been finalized.
/// The calls to this function follow the order of finalization.
fn data_finalized(&mut self, data: Data, creator: NodeIndex);
fn data_finalized(&mut self, data: Data);
/// A unit has been finalized. You can overwrite this method for advanced finalisation handling.
/// Please note that this interface is less stable as it exposes intrinsics which migh be subject to change.
/// Do not implement this method and only implement [`FinalizationHandler::data_finalized`] unless you
/// absolutely know what you are doing.
fn unit_finalized(&mut self, _creator: NodeIndex, _round: Round, data: Option<Data>) {
if let Some(d) = data {
self.data_finalized(d);
}
}
}

0 comments on commit 0f8d418

Please sign in to comment.