Skip to content

Commit b7d8fe4

Browse files
committed
feat(validations): add validation logic to check the change tally output in wit/2
1 parent 8c65a50 commit b7d8fe4

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

validations/src/validations.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,30 @@ pub fn validate_tally_transaction<'a>(
13301330
}
13311331
.into());
13321332
}
1333+
} else {
1334+
// Validate that the value returned to the data request launcher is correct
1335+
let expected_tally_value = if honests_count == 0 {
1336+
// If there are no honests because the data request fails, all rewards are returned
1337+
// The returned commit-and-reveal fees depends on the amount of commits and reveals observed
1338+
dr_state.data_request.witnesses as u64 * dr_state.data_request.witness_reward
1339+
+ (dr_state.data_request.witnesses as usize - commits_count) as u64
1340+
* dr_state.data_request.commit_and_reveal_fee
1341+
+ (dr_state.data_request.witnesses as usize - reveals_count) as u64
1342+
* dr_state.data_request.commit_and_reveal_fee
1343+
} else {
1344+
// For non-revealers, the commit-and-reveal fee is refunded
1345+
// For liars (which can be both revealers and non-revealers) and errors, the reward is refunded
1346+
(commits_count - reveals_count) as u64 * dr_state.data_request.commit_and_reveal_fee
1347+
+ (errors_count + liars_count) as u64 * dr_state.data_request.witness_reward
1348+
};
1349+
1350+
if total_tally_value != expected_tally_value {
1351+
return Err(TransactionError::InvalidTallyValue {
1352+
value: total_tally_value,
1353+
expected_value: expected_tally_value,
1354+
}
1355+
.into());
1356+
}
13331357
}
13341358

13351359
Ok((ta_tx.outputs.iter().collect(), tally_extra_fee))

0 commit comments

Comments
 (0)