fix submission rewards accumulation

This commit is contained in:
De Facto 2021-02-04 17:43:00 +08:00
parent 88303c8545
commit 67f15852be
2 changed files with 14 additions and 1 deletions

View File

@ -36,6 +36,9 @@ pub enum Error {
#[error("Each oracle may only submit once per round")] #[error("Each oracle may only submit once per round")]
OracleAlreadySubmitted, OracleAlreadySubmitted,
#[error("Rewards overflow")]
RewardsOverflow,
#[error("Unknown error")] #[error("Unknown error")]
UnknownError, UnknownError,
} }

View File

@ -150,7 +150,10 @@ impl<'a> SubmitContext<'a> {
self.submit(&mut aggregator)?; self.submit(&mut aggregator)?;
// credit oracle for submission // credit oracle for submission
oracle.withdrawable = aggregator.config.reward_amount; oracle.withdrawable = oracle
.withdrawable
.checked_add(aggregator.config.reward_amount)
.ok_or(Error::RewardsOverflow)?;
aggregator.save(self.aggregator)?; aggregator.save(self.aggregator)?;
oracle.save(self.oracle)?; oracle.save(self.oracle)?;
@ -466,6 +469,7 @@ mod tests {
min_submissions: 2, min_submissions: 2,
max_submissions: 2, max_submissions: 2,
restart_delay: 1, restart_delay: 1,
reward_amount: 10,
..AggregatorConfig::default() ..AggregatorConfig::default()
}, },
@ -591,8 +595,10 @@ mod tests {
let time = 100; let time = 100;
let agr = fixture.submit(&mut oracle, &mut oracle_owner, time, 0, 1)?; let agr = fixture.submit(&mut oracle, &mut oracle_owner, time, 0, 1)?;
let oracle_state = Oracle::load_initialized(&oracle.info())?;
let sub = &agr.current_round.submissions[0]; let sub = &agr.current_round.submissions[0];
let round = &agr.current_round; let round = &agr.current_round;
assert_eq!(oracle_state.withdrawable, 10);
assert_eq!(round.started_at, time); assert_eq!(round.started_at, time);
assert_eq!(round.updated_at, time); assert_eq!(round.updated_at, time);
assert_eq!(sub.oracle, oracle.pubkey.to_bytes()); assert_eq!(sub.oracle, oracle.pubkey.to_bytes());
@ -612,8 +618,10 @@ mod tests {
let old_time = time; let old_time = time;
let time = 200; let time = 200;
let agr = fixture.submit(&mut oracle2, &mut oracle_owner2, time, 0, 2)?; let agr = fixture.submit(&mut oracle2, &mut oracle_owner2, time, 0, 2)?;
let oracle_state = Oracle::load_initialized(&oracle.info())?;
let sub = &agr.current_round.submissions[1]; let sub = &agr.current_round.submissions[1];
let round = &agr.current_round; let round = &agr.current_round;
assert_eq!(oracle_state.withdrawable, 10);
assert_eq!(round.started_at, old_time); assert_eq!(round.started_at, old_time);
assert_eq!(round.updated_at, time); assert_eq!(round.updated_at, time);
assert_eq!(sub.oracle, oracle2.pubkey.to_bytes()); assert_eq!(sub.oracle, oracle2.pubkey.to_bytes());
@ -639,8 +647,10 @@ mod tests {
let time = 300; let time = 300;
let agr = fixture.submit(&mut oracle, &mut oracle_owner, time, 1, 10)?; let agr = fixture.submit(&mut oracle, &mut oracle_owner, time, 1, 10)?;
let oracle_state = Oracle::load_initialized(&oracle.info())?;
let sub = &agr.current_round.submissions[0]; let sub = &agr.current_round.submissions[0];
let round = &agr.current_round; let round = &agr.current_round;
assert_eq!(oracle_state.withdrawable, 20);
assert_eq!(round.id, 1); assert_eq!(round.id, 1);
assert_eq!(round.started_at, time); assert_eq!(round.started_at, time);
assert_eq!(round.updated_at, time); assert_eq!(round.updated_at, time);