Skip to content

Commit

Permalink
WIP continuing testing
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Mar 13, 2023
1 parent cc8e5eb commit d046e7f
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 202 deletions.
152 changes: 103 additions & 49 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ where
height, current_epoch, new_epoch
);

println!("BYZANTINE VALIDATORS:");
dbg!(&self.byzantine_validators);

if new_epoch {
namada::ledger::storage::update_allowed_conversions(
&mut self.wl_storage,
Expand Down Expand Up @@ -503,12 +500,8 @@ where
.update_epoch(height, header_time)
.expect("Must be able to update epoch");

println!("\nRECORDING SLASHES FROM EVIDENCE");
self.record_slashes_from_evidence();
println!("\nPROCESSING SLASHES ENQUEUED FOR THIS EPOCH");

self.process_slashes();
println!("\nSLASHES PROCESSED");

(height, new_epoch)
}
Expand Down Expand Up @@ -819,7 +812,7 @@ mod test_finalize_block {
read_consensus_validator_set_addresses_with_stake,
read_num_consensus_validators, rewards_accumulator_handle,
validator_consensus_key_handle, validator_rewards_products_handle,
validator_slashes_handle, validator_state_handle,
validator_slashes_handle, validator_state_handle, write_pos_params,
};
use namada::types::governance::ProposalVote;
use namada::types::storage::Epoch;
Expand Down Expand Up @@ -1545,7 +1538,17 @@ mod test_finalize_block {
votes: Vec<VoteInfo>,
byzantine_validators: Option<Vec<Misbehavior>>,
) {
// Let the header time be always ahead of the next epoch min start time
let header = Header {
time: shell
.wl_storage
.storage
.next_epoch_min_start_time
.next_second(),
..Default::default()
};
let mut req = FinalizeBlock {
header,
proposer_address,
votes,
..Default::default()
Expand All @@ -1558,10 +1561,12 @@ mod test_finalize_block {
}

#[test]
fn test_ledger_slashing() {
fn test_ledger_slashing() -> storage_api::Result<()> {
let num_validators = 7_u64;
let (mut shell, _) = setup(num_validators);
let params = read_pos_params(&shell.wl_storage).unwrap();
let mut params = read_pos_params(&shell.wl_storage).unwrap();
params.unbonding_len = 4;
write_pos_params(&mut shell.wl_storage, params.clone());

let validator_set: Vec<WeightedValidator> =
read_consensus_validator_set_addresses_with_stake(
Expand Down Expand Up @@ -1590,22 +1595,27 @@ mod test_finalize_block {
HEXUPPER.decode(hash_string.as_bytes()).unwrap()
};

let mut pkhs: Vec<Vec<u8>> = Vec::new();
for validator in &validator_set {
let mut all_pkhs: Vec<Vec<u8>> = Vec::new();
let mut behaving_pkhs: Vec<Vec<u8>> = Vec::new();
for (idx, validator) in validator_set.iter().enumerate() {
assert_eq!(
validator_state_handle(&validator.address)
.get(&shell.wl_storage, Epoch::default(), &params)
.unwrap(),
Some(ValidatorState::Consensus)
);
pkhs.push(get_pkh(validator.address.clone(), Epoch::default()));
all_pkhs.push(get_pkh(validator.address.clone(), Epoch::default()));
if idx > 1 as usize {
behaving_pkhs
.push(get_pkh(validator.address.clone(), Epoch::default()));
}
}

let pkh1 = pkhs[0].clone();
let _pkh2 = pkhs[1].clone();
let pkh1 = all_pkhs[0].clone();
let pkh2 = all_pkhs[1].clone();

// Finalize block 1
next_block_for_inflation(&mut shell, pkh1, vec![], None);
next_block_for_inflation(&mut shell, pkh1.clone(), vec![], None);
// for validator in &validator_set {
// assert_eq!(
// validator_state_handle(&validator.address)
Expand All @@ -1616,10 +1626,7 @@ mod test_finalize_block {
// pkhs.push(get_pkh(validator.address.clone(), Epoch::default()));
// }

let pkh1 = pkhs[0].clone();
let pkh2 = pkhs[1].clone();

let votes = get_default_votes(&pkhs, &validator_set);
let votes = get_default_true_votes(&all_pkhs, &validator_set);
assert!(!votes.is_empty());

// For block 2, include the evidences found for block 1. Only the type,
Expand Down Expand Up @@ -1648,7 +1655,7 @@ mod test_finalize_block {
];
next_block_for_inflation(
&mut shell,
pkh1,
pkh1.clone(),
votes,
Some(byzantine_validators),
);
Expand All @@ -1674,48 +1681,95 @@ mod test_finalize_block {
Some(ValidatorState::Jailed)
);
assert_eq!(
5_u64,
read_num_consensus_validators(&shell.wl_storage).unwrap()
read_num_consensus_validators(&shell.wl_storage).unwrap(),
5_u64
);

let processing_epoch =
shell.wl_storage.storage.block.epoch + params.unbonding_len;

dbg!(
&enqueued_slashes_handle()
.at(&Epoch::default())
.is_empty(&shell.wl_storage)
);
dbg!(
&enqueued_slashes_handle()
for epoch in Epoch::default().iter_range(params.unbonding_len) {
assert!(
enqueued_slashes_handle()
.at(&epoch)
.is_empty(&shell.wl_storage)?
);
}
assert!(
!enqueued_slashes_handle()
.at(&processing_epoch)
.is_empty(&shell.wl_storage)
.is_empty(&shell.wl_storage)?
);
assert!(
validator_slashes_handle(&val1.address)
.is_empty(&shell.wl_storage)?
);
assert!(
validator_slashes_handle(&val2.address)
.is_empty(&shell.wl_storage)?
);

let all_slashes: Vec<_> = enqueued_slashes_handle()
.at(&processing_epoch)
.iter(&shell.wl_storage)
.unwrap()
.collect();
// let enqueued_slashes: Vec<_> = enqueued_slashes_handle()
// .at(&processing_epoch)
// .iter(&shell.wl_storage)
// .unwrap()
// .collect();

dbg!(&all_slashes);
let val1_slashes: Vec<Slash> = validator_slashes_handle(&val1.address)
.iter(&shell.wl_storage)
.unwrap()
.map(|a| a.unwrap())
.collect();
let val2_slashes: Vec<Slash> = validator_slashes_handle(&val2.address)
.iter(&shell.wl_storage)
let validator_set: Vec<WeightedValidator> =
read_consensus_validator_set_addresses_with_stake(
&shell.wl_storage,
Epoch::default(),
)
.unwrap()
.map(|a| a.unwrap())
.into_iter()
.collect();

dbg!(&val1_slashes, &val2_slashes);
assert!(val1_slashes.len() == 1 && val1_slashes[0].block_height == 1);
assert!(val2_slashes.len() == 1 && val2_slashes[0].block_height == 1);
let votes = get_default_true_votes(&behaving_pkhs, &validator_set);
loop {
next_block_for_inflation(
&mut shell,
pkh1.clone(),
votes.clone(),
None,
);
if shell.wl_storage.storage.block.epoch == processing_epoch {
break;
} else {
println!(
"Block {} epoch {}",
shell.wl_storage.storage.block.height,
shell.wl_storage.storage.block.epoch
);
assert!(
enqueued_slashes_handle()
.at(&shell.wl_storage.storage.block.epoch)
.is_empty(&shell.wl_storage)?
);
}
}
assert!(
!validator_slashes_handle(&val1.address)
.is_empty(&shell.wl_storage)?
);
assert!(
!validator_slashes_handle(&val2.address)
.is_empty(&shell.wl_storage)?
);

// let val1_slashes: Vec<Slash> =
// validator_slashes_handle(&val1.address) .iter(&shell.
// wl_storage) .unwrap()
// .map(|a| a.unwrap())
// .collect();
// let val2_slashes: Vec<Slash> =
// validator_slashes_handle(&val2.address) .iter(&shell.
// wl_storage) .unwrap()
// .map(|a| a.unwrap())
// .collect();
Ok(())
}

fn get_default_votes(
fn get_default_true_votes(
addresses: &Vec<Vec<u8>>,
powers: &Vec<WeightedValidator>,
) -> Vec<VoteInfo> {
Expand Down
2 changes: 1 addition & 1 deletion apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ where
let pos_params = read_pos_params(&self.wl_storage).unwrap();
let current_epoch = self.wl_storage.storage.block.epoch;
for evidence in byzantine_validators {
dbg!(&evidence);
// dbg!(&evidence);
tracing::info!("Processing evidence {evidence:?}.");
let evidence_height = match u64::try_from(evidence.height) {
Ok(height) => height,
Expand Down
Loading

0 comments on commit d046e7f

Please sign in to comment.