Skip to content

Commit 886d3b2

Browse files
committed
chore: installing modules
1 parent 66e0b7b commit 886d3b2

File tree

24 files changed

+498
-99
lines changed

24 files changed

+498
-99
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,5 @@
1-
use std::fmt;
2-
use std::io::Write as _;
3-
use std::sync::Arc;
4-
51
use bfte_util_array_type::array_type_define;
6-
use bincode::{Decode, Encode};
7-
8-
use crate::signed::Hashable;
9-
10-
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
11-
pub struct ModuleParamsRaw(Arc<[u8]>);
12-
13-
impl From<Vec<u8>> for ModuleParamsRaw {
14-
fn from(value: Vec<u8>) -> Self {
15-
Self(value.into())
16-
}
17-
}
18-
impl From<Arc<[u8]>> for ModuleParamsRaw {
19-
fn from(value: Arc<[u8]>) -> Self {
20-
Self(value)
21-
}
22-
}
23-
impl fmt::Debug for ModuleParamsRaw {
24-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25-
f.debug_struct("ModuleConfigRaw")
26-
.field("len", &self.0.len())
27-
.finish_non_exhaustive()
28-
}
29-
}
30-
31-
impl Hashable for ModuleParamsRaw {
32-
fn hash(&self) -> blake3::Hash {
33-
let mut hasher = blake3::Hasher::new();
34-
35-
hasher
36-
.write_all(&self.0)
37-
.expect("Can't fail to write to hasher");
38-
39-
hasher.finalize()
40-
}
41-
}
422

433
array_type_define! {
444
pub struct ModuleConfigHash[32];
455
}
46-
// framed_payload_define! {
47-
// /// Raw, undecoded module config
48-
// pub struct ModuleConfigRaw;
49-
50-
// ModuleConfigHash;
51-
// ModuleConfigLen;
52-
53-
// pub struct ModuleConfigSlice;
54-
55-
// TAG = *b"moco";
56-
// }

crates/consensus/src/consensus/getters.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ impl Consensus {
5252
let cur_round = self.current_round_with_timeout_rx.borrow().0;
5353
let num_peers = self.get_consensus_params(cur_round).await.num_peers();
5454

55-
let finality_lag = cur_round
55+
let finality_lag = dbg!(cur_round)
5656
.to_number()
57-
.checked_sub(
58-
finality.to_number() + u64::try_from(num_peers.total()).expect("Can't overfolow"),
59-
)
57+
.checked_sub(dbg!(
58+
finality.to_number() + u64::try_from(num_peers.total()).expect("Can't overfolow")
59+
))
6060
.unwrap_or_default();
6161

6262
let multiplier = 1 << finality_lag.min(32);

crates/consensus/src/consensus/handle_finality_vote.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ impl Consensus {
103103
votes.push(ctx.get_finality_vote(*peer_pubkey)?.unwrap_or_default());
104104
}
105105

106+
dbg!(&votes);
106107
votes.sort();
107108

109+
dbg!(&votes);
110+
108111
let num_peers = cur_round_consensus_params.peers.to_num_peers();
109112

110113
let finality_cons = votes[num_peers.max_faulty()];

crates/module/src/module.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::sync::Arc;
88
use async_trait::async_trait;
99
use bfte_consensus_core::block::BlockRound;
1010
use bfte_consensus_core::citem::{CItemRaw, InputRaw, OutputRaw};
11-
use bfte_consensus_core::module::config::ModuleParamsRaw;
1211
use bfte_consensus_core::module::{ModuleId, ModuleKind};
1312
use bfte_consensus_core::peer::PeerPubkey;
1413
use bfte_consensus_core::peer_set::PeerSet;
@@ -35,8 +34,10 @@ pub struct DynModuleWithConfig {
3534
pub struct ModuleInitArgs {
3635
pub db: ModuleDatabase,
3736
pub module_consensus_version: ConsensusVersion,
38-
pub config: ModuleParamsRaw,
3937
pub peer_pubkey: Option<PeerPubkey>,
38+
/// Only AppConsensus module should use this
39+
#[doc(hidden)]
40+
pub modules_inits: BTreeMap<ModuleKind, DynModuleInit>,
4041
}
4142

4243
pub type DynModuleInit = Arc<dyn IModuleInit + Send + Sync>;
@@ -46,14 +47,14 @@ impl ModuleInitArgs {
4647
module_id: ModuleId,
4748
db: Arc<Database>,
4849
module_consensus_version: ConsensusVersion,
49-
config: ModuleParamsRaw,
50+
modules_inits: BTreeMap<ModuleKind, DynModuleInit>,
5051
peer_pubkey: Option<PeerPubkey>,
5152
) -> Self {
5253
Self {
5354
db: ModuleDatabase::new(module_id, db),
5455
module_consensus_version,
55-
config,
5656
peer_pubkey,
57+
modules_inits,
5758
}
5859
}
5960
}
@@ -78,6 +79,9 @@ pub type ModuleSupportedConsensusVersions = BTreeMap<ConsensusVersionMajor, Cons
7879
pub trait IModuleInit: Any {
7980
fn kind(&self) -> ModuleKind;
8081

82+
/// If true, this module kind can be installed only once in the federation
83+
fn singleton(&self) -> bool;
84+
8185
fn display_name(&self) -> &'static str;
8286

8387
/// All major consensus version supported by the module, with latest

crates/module/src/module/config.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use bfte_consensus_core::module::ModuleKind;
2-
use bfte_consensus_core::module::config::ModuleParamsRaw;
32
use bfte_consensus_core::ver::ConsensusVersion;
43
use bincode::{Decode, Encode};
54

65
#[derive(Debug, PartialEq, Eq, Encode, Decode, Clone)]
76
pub struct ModuleConfig {
87
pub kind: ModuleKind,
98
pub version: ConsensusVersion,
10-
pub params: ModuleParamsRaw,
119
}

crates/modules/app-consensus/src/citem.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use bfte_consensus_core::bincode::CONSENSUS_BINCODE_CONFIG;
22
use bfte_consensus_core::citem::CItemRaw;
3-
use bfte_consensus_core::module::ModuleId;
3+
use bfte_consensus_core::module::{ModuleId, ModuleKind};
44
use bfte_consensus_core::peer::PeerPubkey;
5-
use bfte_consensus_core::ver::ConsensusVersionMinor;
5+
use bfte_consensus_core::ver::{ConsensusVersion, ConsensusVersionMinor};
66
use bfte_util_bincode::decode_whole;
77
use bfte_util_error::WhateverResult;
88
use bincode::{Decode, Encode};
@@ -13,6 +13,10 @@ use snafu::ResultExt as _;
1313
pub enum AppConsensusCitem {
1414
VoteAddPeer(PeerPubkey),
1515
VoteRemovePeer(PeerPubkey),
16+
VoteAddModule {
17+
module_kind: ModuleKind,
18+
consensus_version: ConsensusVersion,
19+
},
1620
VoteModuleVersion {
1721
module_id: ModuleId,
1822
minor_consensus_version: ConsensusVersionMinor,

crates/modules/app-consensus/src/effects.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,15 @@ impl EffectKind for ModuleVersionUpgrade {
4747
const MODULE_KIND: ModuleKind = crate::KIND;
4848
const EFFECT_ID: EffectId = EffectId::new(3);
4949
}
50+
51+
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
52+
pub struct AddModuleEffect {
53+
pub module_kind: ModuleKind,
54+
pub module_id: ModuleId,
55+
pub consensus_version: ConsensusVersion,
56+
}
57+
58+
impl EffectKind for AddModuleEffect {
59+
const MODULE_KIND: ModuleKind = crate::KIND;
60+
const EFFECT_ID: EffectId = EffectId::new(4);
61+
}

crates/modules/app-consensus/src/init.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::collections::BTreeMap;
22
use std::sync::Arc;
33

44
use async_trait::async_trait;
5-
use bfte_consensus_core::bincode::CONSENSUS_BINCODE_CONFIG;
65
use bfte_consensus_core::module::{ModuleId, ModuleKind};
76
use bfte_consensus_core::peer_set::PeerSet;
87
use bfte_consensus_core::ver::{ConsensusVersion, ConsensusVersionMajor, ConsensusVersionMinor};
@@ -35,12 +34,6 @@ impl AppConsensusModuleInit {
3534
let config = ModuleConfig {
3635
kind: KIND,
3736
version,
38-
params: bincode::encode_to_vec(
39-
&super::params::CoreConsensusModuleParams {},
40-
CONSENSUS_BINCODE_CONFIG,
41-
)
42-
.expect("Can't fail")
43-
.into(),
4437
};
4538

4639
{
@@ -82,6 +75,10 @@ impl IModuleInit for AppConsensusModuleInit {
8275
crate::KIND
8376
}
8477

78+
fn singleton(&self) -> bool {
79+
true
80+
}
81+
8582
fn display_name(&self) -> &'static str {
8683
"App Consensus"
8784
}
@@ -124,6 +121,7 @@ impl IModuleInit for AppConsensusModuleInit {
124121
peer_pubkey: args.peer_pubkey,
125122
propose_citems_rx,
126123
propose_citems_tx,
124+
modules_inits: args.modules_inits,
127125
};
128126

129127
module.refresh_consensus_proposals().await;

0 commit comments

Comments
 (0)