darkfid: consensus fixes

This commit is contained in:
skoupidi
2024-02-22 17:52:56 +02:00
parent 5e9892363a
commit cff856971d
4 changed files with 65 additions and 28 deletions

View File

@@ -109,12 +109,15 @@ pub async fn miner_task(node: &Darkfid, recipient: &PublicKey, skip_sync: bool)
// Start miner loop
loop {
// Grab best current fork index
let fork_index = best_forks_indexes(&node.validator.consensus.forks.read().await)?[0];
// Grab best current fork proposals sequence
let forks = node.validator.consensus.forks.read().await;
let fork_index = best_forks_indexes(&forks)?[0];
let fork_proposals = forks[fork_index].proposals.clone();
drop(forks);
// Start listenning for network proposals and mining next block for best fork.
smol::future::or(
listen_to_network(node, fork_index, &subscription),
listen_to_network(node, fork_proposals, &subscription),
mine_next_block(node, fork_index, &mut secret, recipient, &zkbin, &pk),
)
.await?;
@@ -134,19 +137,29 @@ pub async fn miner_task(node: &Darkfid, recipient: &PublicKey, skip_sync: bool)
/// Auxiliary function to listen for incoming proposals and check if the best fork has changed
async fn listen_to_network(
node: &Darkfid,
fork_index: usize,
fork_proposals: Vec<blake3::Hash>,
subscription: &Subscription<JsonNotification>,
) -> Result<()> {
loop {
'main_loop: loop {
// Wait until a new proposal has been received
subscription.receive().await;
// Grab best current fork indexes
let fork_indexes = best_forks_indexes(&node.validator.consensus.forks.read().await)?;
// Grab a lock over node forks
let forks = node.validator.consensus.forks.read().await;
if !fork_indexes.contains(&fork_index) {
return Ok(())
// Grab best current fork indexes
let fork_indexes = best_forks_indexes(&forks)?;
// Iterate to verify if proposals sequence has changed
for index in fork_indexes {
if forks[index].proposals == fork_proposals {
drop(forks);
continue 'main_loop
}
}
drop(forks);
return Ok(())
}
}

View File

@@ -20,6 +20,9 @@ database = "darkfid2"
# Finalization threshold, denominated by number of blocks
threshold = 3
# minerd JSON-RPC endpoint
minerd_endpoint = "tcp://127.0.0.1:28467"
# PoW block production target, in seconds
pow_target = 20
@@ -42,3 +45,17 @@ peers = ["tcp+tls://0.0.0.0:48242", "tcp+tls://0.0.0.0:48342"]
# Allow localnet hosts
localnet = true
## Localnet miners P2P network settings
[network_config."localnet".miners_net]
# P2P accept addresses the instance listens on for inbound connections
inbound = ["tcp+tls://0.0.0.0:48441"]
# Whitelisted network transports for outbound connections
allowed_transports = ["tcp+tls"]
# Peer nodes to manually connect to
peers = ["tcp+tls://0.0.0.0:48241", "tcp+tls://0.0.0.0:48341"]
# Allow localnet hosts
localnet = true

View File

@@ -1,6 +1,8 @@
#!/bin/sh
set -e
session=darkfid-small
# Start a tmux session with two mining and a non-mining darkfid nodes.
# Additionally, start two minerd daemons.
@@ -11,18 +13,18 @@ else
verbose=""
fi
tmux new-session -d
tmux send-keys "../../../minerd ${verbose} -c minerd0.toml" Enter
tmux new-session -d -s $session
tmux send-keys -t $session "../../../minerd ${verbose} -c minerd0.toml" Enter
sleep 1
tmux split-window -v
tmux send-keys "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid0.toml" Enter
tmux split-window -t $session -v
tmux send-keys -t $session "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid0.toml" Enter
sleep 2
tmux new-window
tmux send-keys "../../../minerd ${verbose} -c minerd1.toml" Enter
tmux new-window -t $session
tmux send-keys -t $session "../../../minerd ${verbose} -c minerd1.toml" Enter
sleep 1
tmux split-window -v
tmux send-keys "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid1.toml" Enter
tmux split-window -t $session -v
tmux send-keys -t $session "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid1.toml" Enter
sleep 2
tmux new-window
tmux send-keys "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid2.toml" Enter
tmux attach
tmux new-window -t $session
tmux send-keys -t $session "LOG_TARGETS='!sled' ../../../darkfid ${verbose} -c darkfid2.toml" Enter
tmux attach -t $session

View File

@@ -141,18 +141,23 @@ impl Consensus {
// If a fork index was found, replace forks with the mutated one,
// otherwise push the new fork.
let mut lock = self.forks.write().await;
// Check if fork already exists
for f in lock.iter() {
if f.proposals == fork.proposals {
drop(lock);
return Err(Error::ProposalAlreadyExists)
}
}
match index {
Some(i) => {
lock[i] = fork;
if i < lock.len() && lock[i].proposals == fork.proposals[..fork.proposals.len() - 1]
{
lock[i] = fork;
} else {
lock.push(fork);
}
}
None => {
// Check if fork already exists
for f in lock.iter() {
if f.proposals == fork.proposals {
drop(lock);
return Err(Error::ProposalAlreadyExists)
}
}
lock.push(fork);
}
}