Skip to content

Commit

Permalink
fix: a transaction may visit miner account but don't update it
Browse files Browse the repository at this point in the history
  • Loading branch information
AshinGau committed Nov 26, 2024
1 parent f40762c commit 960e82f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ where
pub(crate) fn execute(&mut self) {
let start = Instant::now();
let coinbase = self.env.block.coinbase;
let mut committed_accumulated_rewards = 0;
let mut evm = EvmBuilder::default()
.with_db(&mut self.partition_db)
.with_spec_id(self.spec_id)
Expand Down Expand Up @@ -173,6 +174,16 @@ where
read_set.iter().all(|l| tx_states[txid].read_set.contains_key(l.0));
skip_validation &=
write_set.iter().all(|l| tx_states[txid].write_set.contains(l));
if let Some(accumulator) = self.rewards_accumulators.get(&txid) {
let contains_miner =
write_set.contains(&LocationAndType::Basic(coinbase));
accumulator.rewards_committed.store(contains_miner, Ordering::Release);
if contains_miner {
committed_accumulated_rewards = evm.db().accumulated_rewards;
} else {
evm.db_mut().accumulated_rewards = committed_accumulated_rewards;
}
}

// temporary commit to cache_db, to make use the remaining txs can read the
// updated data
Expand Down
19 changes: 13 additions & 6 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ use revm::{
use std::{
collections::{BTreeMap, BTreeSet},
ops::DerefMut,
sync::{atomic::AtomicUsize, Arc, RwLock},
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
Arc, RwLock,
},
time::{Duration, Instant},
};
use tokio::sync::Notify;
Expand Down Expand Up @@ -137,6 +140,7 @@ pub(crate) struct RewardsAccumulator {
pub accumulate_counter: AtomicUsize,
pub accumulate_rewards: Atomic<u128>,
pub notifier: Arc<Notify>,
pub rewards_committed: AtomicBool,
}

impl RewardsAccumulator {
Expand All @@ -146,6 +150,7 @@ impl RewardsAccumulator {
accumulate_counter: AtomicUsize::new(0),
accumulate_rewards: Atomic::<u128>::new(0),
notifier: Arc::new(Notify::new()),
rewards_committed: AtomicBool::new(false),
}
}
}
Expand Down Expand Up @@ -556,11 +561,13 @@ where

let span = Span::enter_with_local_parent("database commit transitions");
let mut rewards = 0;
let rewards_start_txid =
match self.rewards_accumulators.range(..self.num_finality_txs).next_back() {
Some((txid, _)) => *txid,
None => start_txid,
};
let mut rewards_start_txid = start_txid;
for (txid, accumulator) in self.rewards_accumulators.range(..self.num_finality_txs).rev() {
if accumulator.rewards_committed.load(Ordering::Acquire) {
rewards_start_txid = *txid;
break;
}
}
for txid in start_txid..self.num_finality_txs {
if txid >= rewards_start_txid {
rewards += tx_states[txid].execute_result.rewards;
Expand Down
2 changes: 1 addition & 1 deletion src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ pub(crate) struct PartitionDB<DB> {
pub current_txid: TxId,
pub raw_transfer: bool,
rewards_accumulators: Arc<RewardsAccumulators>,
accumulated_rewards: u128,
pub accumulated_rewards: u128,
}

impl<DB> PartitionDB<DB> {
Expand Down

0 comments on commit 960e82f

Please sign in to comment.