Skip to content

Commit c19179c

Browse files
committed
feat(validations): validate eligibility of included commits
1 parent c9e9fa4 commit c19179c

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

data_structures/src/error.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ pub enum TransactionError {
4949
DataRequestNotFound { hash: Hash },
5050
#[fail(display = "Commit transaction has a invalid Proof of Eligibility")]
5151
InvalidDataRequestPoe,
52+
#[fail(
53+
display = "Validator {} is not eligible to commit to a data request",
54+
validator
55+
)]
56+
ValidatorNotEligible { validator: PublicKeyHash },
5257
#[fail(
5358
display = "The data request eligibility claim VRF proof hash is greater than the target hash: {} > {}",
5459
vrf_hash, target_hash
@@ -475,7 +480,7 @@ pub enum BlockError {
475480
count, block_hash
476481
)]
477482
MissingExpectedTallies { count: usize, block_hash: Hash },
478-
/// Missing expected tallies
483+
/// Validator is not eligible to propose a block
479484
#[fail(display = "Validator {} is not eligible to propose a block", validator)]
480485
ValidatorNotEligible { validator: PublicKeyHash },
481486
}

validations/src/validations.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ pub fn validate_commit_transaction(
668668
active_wips: &ActiveWips,
669669
superblock_period: u16,
670670
protocol_version: ProtocolVersion,
671+
stakes: &Stakes<PublicKeyHash, Wit, u32, u64>,
671672
) -> Result<(Hash, u16, u64), failure::Error> {
672673
// Get DataRequest information
673674
let dr_pointer = co_tx.body.dr_pointer;
@@ -680,9 +681,28 @@ pub fn validate_commit_transaction(
680681

681682
let dr_output = &dr_state.data_request;
682683

684+
let proof_pkh = co_tx.body.proof.proof.pkh();
685+
686+
// Check if the commit transaction is from an eligible validator
687+
if protocol_version >= ProtocolVersion::V2_0 {
688+
let eligibility = stakes.witnessing_eligibility(
689+
proof_pkh,
690+
epoch,
691+
dr_state.data_request.witnesses,
692+
dr_state.info.current_commit_round,
693+
);
694+
if eligibility == Ok(Eligible::No(InsufficientPower))
695+
|| eligibility == Ok(Eligible::No(NotStaking))
696+
{
697+
return Err(TransactionError::ValidatorNotEligible {
698+
validator: proof_pkh,
699+
}
700+
.into());
701+
}
702+
}
703+
683704
// Commitment's output is only for change propose, so it only has to be one output and the
684705
// address has to be the same than the address which creates the commitment
685-
let proof_pkh = co_tx.body.proof.proof.pkh();
686706
if co_tx.body.outputs.len() > 1 {
687707
return Err(TransactionError::SeveralCommitOutputs.into());
688708
}
@@ -1787,6 +1807,7 @@ pub fn validate_block_transactions(
17871807
active_wips,
17881808
consensus_constants.superblock_period,
17891809
protocol_version,
1810+
stakes,
17901811
)?;
17911812

17921813
// Validation for only one commit for pkh/data request in a block
@@ -2307,7 +2328,8 @@ pub fn validate_new_transaction(
23072328
minimum_reppoe_difficulty,
23082329
active_wips,
23092330
superblock_period,
2310-
protocol_version
2331+
protocol_version,
2332+
stakes,
23112333
)
23122334
.map(|(_, _, fee)| fee),
23132335
Transaction::Reveal(tx) => {

0 commit comments

Comments
 (0)