Skip to content

Commit

Permalink
validator: renamed testing_mode to pos_testing_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aggstam committed Nov 21, 2023
1 parent 6e830b3 commit 4d3984a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 42 deletions.
12 changes: 6 additions & 6 deletions bin/darkfid2/darkfid_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ recipient = "5ZHfYpt4mpJcwBNxfEyxLzeFJUEeoePs5NQ5jVEgHrMf"
# Skip syncing process and start node right away
skip_sync = true

# Enable testing mode for local testing
testing_mode = true
# Enable PoS testing mode for local testing
pos_testing_mode = true

## Localnet sync P2P network settings
[localnet.sync_net]
Expand Down Expand Up @@ -188,8 +188,8 @@ consensus = false
# Skip syncing process and start node right away
skip_sync = false

# Enable testing mode for local testing
testing_mode = false
# Enable PoS testing mode for local testing
pos_testing_mode = false

## Testnet sync P2P network settings
[testnet.sync_net]
Expand Down Expand Up @@ -347,8 +347,8 @@ consensus = false
# Skip syncing process and start node right away
skip_sync = false

# Enable testing mode for local testing
testing_mode = false
# Enable PoS testing mode for local testing
pos_testing_mode = false

## Mainnet sync P2P network settings
[mainnet.sync_net]
Expand Down
10 changes: 5 additions & 5 deletions bin/darkfid2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ pub struct BlockchainNetwork {
pub skip_sync: bool,

#[structopt(long)]
/// Enable testing mode for local testing
pub testing_mode: bool,
/// Enable PoS testing mode for local testing
pub pos_testing_mode: bool,

/// Syncing network settings
#[structopt(flatten)]
Expand Down Expand Up @@ -220,8 +220,8 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
}
};

if blockchain_config.testing_mode {
info!(target: "darkfid", "Node is configured to run in testing mode!");
if blockchain_config.pos_testing_mode {
info!(target: "darkfid", "Node is configured to run in PoS testing mode!");
}

// Parse the genesis block
Expand Down Expand Up @@ -255,7 +255,7 @@ async fn realmain(args: Args, ex: Arc<smol::Executor<'static>>) -> Result<()> {
genesis_block,
genesis_txs_total,
vec![],
blockchain_config.testing_mode,
blockchain_config.pos_testing_mode,
);

// Initialize validator
Expand Down
4 changes: 2 additions & 2 deletions bin/darkfid2/src/tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct HarnessConfig {
pub pow_threads: usize,
pub pow_target: usize,
pub pow_fixed_difficulty: Option<BigUint>,
pub testing_node: bool,
pub pos_testing_mode: bool,
pub alice_initial: u64,
pub bob_initial: u64,
}
Expand Down Expand Up @@ -92,7 +92,7 @@ impl Harness {
genesis_block,
genesis_txs_total,
vec![],
config.testing_node,
config.pos_testing_mode,
);

// Generate validators using pregenerated vks
Expand Down
2 changes: 1 addition & 1 deletion bin/darkfid2/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn sync_pos_blocks_real(ex: Arc<Executor<'static>>) -> Result<()> {
pow_threads,
pow_target,
pow_fixed_difficulty: pow_fixed_difficulty.clone(),
testing_node: true,
pos_testing_mode: true,
alice_initial: 1000,
bob_initial: 500,
};
Expand Down
24 changes: 14 additions & 10 deletions src/validator/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ pub struct Consensus {
pub forks: Vec<Fork>,
/// Canonical blockchain PoW module state
pub module: PoWModule,
/// Flag to enable testing mode
pub testing_mode: bool,
/// Flag to enable PoS testing mode
pub pos_testing_mode: bool,
}

impl Consensus {
Expand All @@ -69,7 +69,7 @@ impl Consensus {
pow_threads: usize,
pow_target: usize,
pow_fixed_difficulty: Option<BigUint>,
testing_mode: bool,
pos_testing_mode: bool,
) -> Result<Self> {
let module =
PoWModule::new(blockchain.clone(), pow_threads, pow_target, pow_fixed_difficulty)?;
Expand All @@ -81,7 +81,7 @@ impl Consensus {
checked_finalization: 0,
forks: vec![],
module,
testing_mode,
pos_testing_mode,
})
}

Expand Down Expand Up @@ -214,7 +214,7 @@ impl Consensus {
let (mut fork, index) = verify_proposal(self, proposal).await?;

// Append proposal to the fork
fork.append_proposal(proposal.hash, self.testing_mode)?;
fork.append_proposal(proposal.hash, self.pos_testing_mode)?;

// Update fork slots based on proposal version
match proposal.block.header.version {
Expand Down Expand Up @@ -366,7 +366,7 @@ impl Consensus {
block,
previous,
expected_reward,
self.testing_mode,
self.pos_testing_mode,
)
.await
.is_err()
Expand Down Expand Up @@ -486,9 +486,13 @@ impl Fork {
}

/// Auxiliary function to append a proposal and recalculate current fork rank
pub fn append_proposal(&mut self, proposal: blake3::Hash, testing_mode: bool) -> Result<()> {
pub fn append_proposal(
&mut self,
proposal: blake3::Hash,
pos_testing_mode: bool,
) -> Result<()> {
self.proposals.push(proposal);
self.rank = self.rank(testing_mode)?;
self.rank = self.rank(pos_testing_mode)?;

Ok(())
}
Expand Down Expand Up @@ -637,7 +641,7 @@ impl Fork {
}

/// Auxiliarry function to compute fork's rank, assuming all proposals are valid.
pub fn rank(&self, testing_mode: bool) -> Result<u64> {
pub fn rank(&self, pos_testing_mode: bool) -> Result<u64> {
// If the fork is empty its rank is 0
if self.proposals.is_empty() {
return Ok(0)
Expand All @@ -659,7 +663,7 @@ impl Fork {
} else {
proposal.clone()
};
sum += block_rank(proposal, &previous_previous, testing_mode)?;
sum += block_rank(proposal, &previous_previous, pos_testing_mode)?;
}

// Use fork(proposals) length as a multiplier to compute the actual fork rank
Expand Down
22 changes: 11 additions & 11 deletions src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ pub struct ValidatorConfig {
pub genesis_txs_total: u64,
/// Whitelisted faucet pubkeys (testnet stuff)
pub faucet_pubkeys: Vec<PublicKey>,
/// Flag to enable testing mode
pub testing_mode: bool,
/// Flag to enable PoS testing mode
pub pos_testing_mode: bool,
}

impl ValidatorConfig {
Expand All @@ -100,7 +100,7 @@ impl ValidatorConfig {
genesis_block: BlockInfo,
genesis_txs_total: u64,
faucet_pubkeys: Vec<PublicKey>,
testing_mode: bool,
pos_testing_mode: bool,
) -> Self {
Self {
time_keeper,
Expand All @@ -111,7 +111,7 @@ impl ValidatorConfig {
genesis_block,
genesis_txs_total,
faucet_pubkeys,
testing_mode,
pos_testing_mode,
}
}
}
Expand All @@ -127,14 +127,14 @@ pub struct Validator {
pub consensus: Consensus,
/// Flag signalling node has finished initial sync
pub synced: bool,
/// Flag to enable testing mode
pub testing_mode: bool,
/// Flag to enable PoS testing mode
pub pos_testing_mode: bool,
}

impl Validator {
pub async fn new(db: &sled::Db, config: ValidatorConfig) -> Result<ValidatorPtr> {
info!(target: "validator::new", "Initializing Validator");
let testing_mode = config.testing_mode;
let pos_testing_mode = config.pos_testing_mode;

info!(target: "validator::new", "Initializing Blockchain");
let blockchain = Blockchain::new(db)?;
Expand Down Expand Up @@ -168,12 +168,12 @@ impl Validator {
config.pow_threads,
config.pow_target,
config.pow_fixed_difficulty,
testing_mode,
pos_testing_mode,
)?;

// Create the actual state
let state =
Arc::new(RwLock::new(Self { blockchain, consensus, synced: false, testing_mode }));
Arc::new(RwLock::new(Self { blockchain, consensus, synced: false, pos_testing_mode }));
info!(target: "validator::new", "Finished initializing validator");

Ok(state)
Expand Down Expand Up @@ -388,7 +388,7 @@ impl Validator {
block,
previous,
expected_reward,
self.testing_mode,
self.pos_testing_mode,
)
.await
.is_err()
Expand Down Expand Up @@ -590,7 +590,7 @@ impl Validator {
block,
previous,
expected_reward,
self.testing_mode,
self.pos_testing_mode,
)
.await
.is_err()
Expand Down
4 changes: 2 additions & 2 deletions src/validator/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn deploy_native_contracts(
pub fn block_rank(
block: &BlockInfo,
previous_previous: &BlockInfo,
testing_mode: bool,
pos_testing_mode: bool,
) -> Result<u64> {
// Genesis block has rank 0
if block.header.height == 0 {
Expand All @@ -120,7 +120,7 @@ pub fn block_rank(
let nonce = u64::from_be_bytes(nonce);

// First 2 blocks or testing ones have rank equal to their nonce
if block.header.height < 3 || testing_mode {
if block.header.height < 3 || pos_testing_mode {
return Ok(nonce)
}

Expand Down
11 changes: 6 additions & 5 deletions src/validator/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub async fn verify_block(
block: &BlockInfo,
previous: &BlockInfo,
expected_reward: u64,
testing_mode: bool,
pos_testing_mode: bool,
) -> Result<()> {
let block_hash = block.hash()?.to_string();
debug!(target: "validator::verification::verify_block", "Validating block {}", block_hash);
Expand Down Expand Up @@ -158,8 +158,9 @@ pub async fn verify_block(
// Since an overlay is used, original database is not affected.
overlay.lock().unwrap().slots.insert(&[block.slots.last().unwrap().clone()])?;

// Verify proposal transaction if not in testing mode
if !testing_mode {
// Verify proposal transaction.
// For PoS blocks(version 2) verify if not in PoS testing mode.
if block.header.version != 2 || !pos_testing_mode {
let tx = block.txs.last().unwrap();
let public_key =
verify_producer_transaction(overlay, time_keeper, tx, block.header.version).await?;
Expand Down Expand Up @@ -553,7 +554,7 @@ pub async fn verify_pow_proposal(
&proposal.block,
&previous,
expected_reward,
consensus.testing_mode,
consensus.pos_testing_mode,
)
.await
.is_err()
Expand Down Expand Up @@ -644,7 +645,7 @@ pub async fn verify_pos_proposal(
&proposal.block,
&previous,
expected_reward,
consensus.testing_mode,
consensus.pos_testing_mode,
)
.await
.is_err()
Expand Down

0 comments on commit 4d3984a

Please sign in to comment.