Skip to content

Commit b178b77

Browse files
committed
feat: UI login and password management
1 parent d94b4e3 commit b178b77

36 files changed

+1018
-408
lines changed

CONVENTIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Conventions
44

5-
> When writing code, follow this conventions
5+
> When writing code, follow these conventions
66
77

88
- Do NOT add comments explaining what is each line/expression doing.

Cargo.lock

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ time = "0.3.41"
9797
tokio = { version = "1.0", features = ["full"] }
9898
tokio-test = "0.4.4"
9999
tower-http = "0.5"
100+
tower-sessions = "0.14.0"
100101
tracing = "0.1.41"
101102
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
102103

crates/bfte/src/lib.rs

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ mod opts;
22

33
use std::io;
44
use std::str::FromStr as _;
5+
use std::sync::Arc;
56

67
use bfte_derive_secret::DeriveableSecret;
8+
use bfte_node::Node;
79
use bfte_node::derive_secret_ext::DeriveSecretExt as _;
810
use bfte_util_error::{Whatever, WhateverResult};
911
use clap::Parser as _;
@@ -48,7 +50,7 @@ impl Bfte {
4850
None
4951
};
5052

51-
match opts.command {
53+
let db = match opts.command {
5254
Commands::GenSecret => {
5355
let root_seckey = DeriveableSecret::generate();
5456
let peer_seckey = root_seckey.get_peer_seckey().expect("Just generated");
@@ -62,39 +64,67 @@ impl Bfte {
6264
return Ok(());
6365
}
6466

65-
Commands::Join { invite } => bfte_node::Node::join(
66-
db_path
67-
.whatever_context("Database path must be set to persist created federation")?,
68-
&invite,
69-
)
70-
.await
71-
.whatever_context("Failed to join consensus")?,
67+
Commands::Join { invite, run } => {
68+
let db = Arc::new(
69+
Node::open_db(db_path)
70+
.await
71+
.whatever_context("Failed to open database")?,
72+
);
7273

73-
Commands::Create { extra_peers } => bfte_node::Node::create(
74-
db_path
75-
.whatever_context("Database path must be set to persist created federation")?,
76-
secret.whatever_context("Secret must be provided to create a new federation")?,
77-
extra_peers,
78-
)
79-
.await
80-
.whatever_context("Failed to create consensus")?,
81-
82-
Commands::Run { bind_ui } => {
83-
bfte_node::Node::builder()
84-
.maybe_root_secret(secret)
85-
.maybe_db_path(db_path)
86-
.ui(Box::new(move |api| {
87-
Box::pin(async move { bfte_node_ui_axum::run(api, bind_ui).await })
88-
}))
89-
.build()
74+
bfte_node::Node::join(db.clone(), &invite)
9075
.await
91-
.whatever_context("Failed to build node")?
92-
.run()
93-
.await
94-
.whatever_context("Failed to run node")?;
76+
.whatever_context("Failed to join consensus")?;
77+
78+
if !run {
79+
return Ok(());
80+
}
81+
82+
db
9583
}
84+
85+
Commands::Create { extra_peers, run } => {
86+
let db = Arc::new(
87+
Node::open_db(db_path)
88+
.await
89+
.whatever_context("Failed to open database")?,
90+
);
91+
92+
bfte_node::Node::create(
93+
db.clone(),
94+
secret
95+
.whatever_context("Secret must be provided to create a new federation")?,
96+
extra_peers,
97+
)
98+
.await
99+
.whatever_context("Failed to create consensus")?;
100+
101+
if !run {
102+
return Ok(());
103+
}
104+
105+
db
106+
}
107+
108+
Commands::Run => Arc::new(
109+
Node::open_db(db_path)
110+
.await
111+
.whatever_context("Failed to open database")?,
112+
),
96113
};
97114

115+
bfte_node::Node::builder()
116+
.maybe_root_secret(secret)
117+
.db(db)
118+
.ui(Box::new(move |api| {
119+
Box::pin(async move { bfte_node_ui_axum::run(api, opts.bind_ui).await })
120+
}))
121+
.build()
122+
.await
123+
.whatever_context("Failed to build node")?
124+
.run()
125+
.await
126+
.whatever_context("Failed to run node")?;
127+
98128
Ok(())
99129
}
100130
}

crates/bfte/src/opts.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ pub(crate) struct Opts {
1010
#[arg(long, env = "BFTE_DATA_DIR", global = true)]
1111
pub data_dir: Option<PathBuf>,
1212

13+
#[arg(
14+
long,
15+
env = "BFTE_DATA_DIR",
16+
default_value = "[::1]:6910",
17+
global = true
18+
)]
19+
pub bind_ui: SocketAddr,
20+
1321
#[arg(long, env = "BFTE_SECRET_PATH", global = true)]
1422
pub secret_path: Option<PathBuf>,
1523

@@ -21,20 +29,18 @@ pub(crate) struct Opts {
2129
pub(crate) enum Commands {
2230
GenSecret,
2331
Create {
32+
#[arg(long, default_value = "false")]
33+
run: bool,
34+
2435
#[arg(long = "extra-peer")]
2536
extra_peers: Vec<PeerPubkey>,
2637
},
2738
Join {
39+
#[arg(long, default_value = "false")]
40+
run: bool,
41+
2842
#[arg(long)]
2943
invite: Invite,
3044
},
31-
Run {
32-
#[arg(
33-
long,
34-
env = "BFTE_DATA_DIR",
35-
default_value = "[::1]:6910",
36-
global = true
37-
)]
38-
bind_ui: SocketAddr,
39-
},
45+
Run,
4046
}

crates/consensus/src/consensus.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub(crate) trait ConsensusReadDbOps {
6565
.get_consensus_params_opt(round)?
6666
.expect("Initialized consensus database must set consensus params for round 0"))
6767
}
68+
fn get_consensus_params_opt(&self, round: BlockRound) -> DbResult<Option<ConsensusParams>>;
6869
fn get_finality_consensus(&self) -> DbResult<Option<BlockRound>>;
6970

7071
fn get_vote_dummy(&self, round: BlockRound, peer_idx: PeerIdx) -> DbResult<Option<Signature>>;
@@ -76,7 +77,6 @@ pub(crate) trait ConsensusReadDbOps {
7677

7778
fn get_payload(&self, payload_hash: BlockPayloadHash) -> DbResult<Option<BlockPayloadRaw>>;
7879
fn get_finality_vote(&self, peer_pubkey: PeerPubkey) -> DbResult<Option<BlockRound>>;
79-
fn get_consensus_params_opt(&self, round: BlockRound) -> DbResult<Option<ConsensusParams>>;
8080
fn get_peers_with_proposal_votes(&self, round: BlockRound) -> DbResult<VoteSet>;
8181
fn get_peers_with_dummy_votes(&self, round: BlockRound) -> DbResult<VoteSet>;
8282
fn get_proposal(&self, round: BlockRound) -> DbResult<Option<BlockHeader>>;

crates/consensus/src/consensus/init.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ use crate::tables::{
1616

1717
#[derive(Debug, Snafu)]
1818
pub enum InitError {
19-
AlreadyJoined,
19+
AlreadyInitialized,
2020
}
21+
2122
type InitResult<T> = Result<T, InitError>;
2223

2324
#[derive(Debug, Snafu)]
2425
pub enum OpenError {
25-
NotJoined,
26+
NotInitialized,
2627
}
2728
type OpenResult<T> = Result<T, OpenError>;
2829

@@ -46,9 +47,7 @@ impl Consensus {
4647
pub async fn open(db: Arc<Database>, our_peer_pubkey: Option<PeerPubkey>) -> OpenResult<Self> {
4748
let cur_round = db.write_with_expect_falliable(Self::open_tx).await?;
4849

49-
let s = Self::open_internal(cur_round, db, our_peer_pubkey).await;
50-
51-
Ok(s)
50+
Ok(Self::open_internal(cur_round, db, our_peer_pubkey).await)
5251
}
5352

5453
async fn open_internal(
@@ -97,7 +96,7 @@ impl Consensus {
9796
ctx.insert_consensus_params(BlockRound::ZERO, params)?
9897
{
9998
if existing != params.hash() {
100-
return AlreadyJoinedSnafu.fail().context(TxSnafu);
99+
return AlreadyInitializedSnafu.fail().context(TxSnafu);
101100
}
102101
}
103102
if let Some((round, hash)) = pinned {
@@ -114,7 +113,7 @@ impl Consensus {
114113

115114
fn open_tx(ctx: &WriteTransactionCtx) -> Result<BlockRound, DbTxError<OpenError>> {
116115
let Some(_) = ctx.get_consensus_params_opt(BlockRound::ZERO)? else {
117-
return NotJoinedSnafu.fail().context(TxSnafu);
116+
return NotInitializedSnafu.fail().context(TxSnafu);
118117
};
119118

120119
Self::init_tables_tx(ctx)?;

0 commit comments

Comments
 (0)