Skip to content

Commit 00ca64b

Browse files
authored
Merge pull request #3144 from stacks-network/fix/microblock-fees-3140
Fix/microblock fees
2 parents 114f64f + b33ad10 commit 00ca64b

File tree

3 files changed

+1082
-60
lines changed

3 files changed

+1082
-60
lines changed

src/chainstate/stacks/db/accounts.rs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ use clarity::vm::types::*;
3333

3434
use crate::types::chainstate::{StacksAddress, StacksBlockId};
3535

36+
use crate::core::StacksEpochId;
37+
3638
#[derive(Debug, Clone, PartialEq)]
3739
pub struct MinerReward {
3840
pub address: StacksAddress,
@@ -873,6 +875,7 @@ impl StacksChainState {
873875
/// not the miner, for reporting the microblock stream fork.
874876
fn calculate_miner_reward(
875877
mainnet: bool,
878+
evaluated_epoch: StacksEpochId,
876879
participant: &MinerPaymentSchedule,
877880
miner: &MinerPaymentSchedule,
878881
users: &Vec<MinerPaymentSchedule>,
@@ -946,7 +949,7 @@ impl StacksChainState {
946949
)
947950
} else {
948951
// users that helped a miner that reported a poison-microblock get nothing
949-
(StacksAddress::burn_address(mainnet), coinbase_reward, false)
952+
(StacksAddress::burn_address(mainnet), 0, false)
950953
}
951954
} else {
952955
// no poison microblock reported
@@ -963,12 +966,16 @@ impl StacksChainState {
963966
} else {
964967
0
965968
},
966-
// TODO: this is wrong, per #3140. It should be
967-
// `participant.streamed_tx_fees_produced()`, since
968-
// `participant.tx_fees_streamed` contains the sum of the microblock
969-
// transaction fees that `participant` confirmed (and thus `participant`'s
970-
// parent produced).
971-
parent.streamed_tx_fees_produced(),
969+
if evaluated_epoch < StacksEpochId::Epoch21 {
970+
// this is wrong, per #3140. It should be
971+
// `participant.streamed_tx_fees_produced()`, since
972+
// `participant.tx_fees_streamed` contains the sum of the microblock
973+
// transaction fees that `participant` confirmed (and thus `participant`'s
974+
// parent produced). But we're stuck with it for earlier epochs.
975+
parent.streamed_tx_fees_produced()
976+
} else {
977+
participant.streamed_tx_fees_produced()
978+
},
972979
if !punished {
973980
participant.streamed_tx_fees_confirmed()
974981
} else {
@@ -1016,6 +1023,7 @@ impl StacksChainState {
10161023
/// well as an info struct about where the rewards took place on the chain.
10171024
pub fn find_mature_miner_rewards(
10181025
clarity_tx: &mut ClarityTx,
1026+
sortdb_conn: &Connection,
10191027
tip: &StacksHeaderInfo,
10201028
mut latest_matured_miners: Vec<MinerPaymentSchedule>,
10211029
parent_miner: MinerPaymentSchedule,
@@ -1044,6 +1052,15 @@ impl StacksChainState {
10441052
from_parent_block_consensus_hash: parent_miner.consensus_hash.clone(),
10451053
};
10461054

1055+
// what epoch was the parent miner's block evaluated in?
1056+
let evaluated_snapshot =
1057+
SortitionDB::get_block_snapshot_consensus(sortdb_conn, &parent_miner.consensus_hash)?
1058+
.expect("FATAL: no snapshot for evaluated block");
1059+
1060+
let evaluated_epoch =
1061+
SortitionDB::get_stacks_epoch(sortdb_conn, evaluated_snapshot.block_height)?
1062+
.expect("FATAL: no epoch for evaluated block");
1063+
10471064
// was this block penalized for mining a forked microblock stream?
10481065
// If so, find the principal that detected the poison, and reward them instead.
10491066
let poison_recipient_opt =
@@ -1063,6 +1080,7 @@ impl StacksChainState {
10631080
// calculate miner reward
10641081
let (parent_miner_reward, miner_reward) = StacksChainState::calculate_miner_reward(
10651082
mainnet,
1083+
evaluated_epoch.epoch_id,
10661084
&miner,
10671085
&miner,
10681086
&users,
@@ -1075,6 +1093,7 @@ impl StacksChainState {
10751093
for user_reward in users.iter() {
10761094
let (parent_reward, reward) = StacksChainState::calculate_miner_reward(
10771095
mainnet,
1096+
evaluated_epoch.epoch_id,
10781097
user_reward,
10791098
&miner,
10801099
&users,
@@ -1102,6 +1121,7 @@ mod test {
11021121
use crate::chainstate::stacks::index::*;
11031122
use crate::chainstate::stacks::Error;
11041123
use crate::chainstate::stacks::*;
1124+
use crate::core::StacksEpochId;
11051125
use clarity::vm::costs::ExecutionCost;
11061126
use stacks_common::util::hash::*;
11071127

@@ -1378,6 +1398,7 @@ mod test {
13781398

13791399
let (parent_reward, miner_reward) = StacksChainState::calculate_miner_reward(
13801400
false,
1401+
StacksEpochId::Epoch2_05,
13811402
&participant,
13821403
&participant,
13831404
&vec![],
@@ -1412,6 +1433,7 @@ mod test {
14121433

14131434
let (parent_miner_1, reward_miner_1) = StacksChainState::calculate_miner_reward(
14141435
false,
1436+
StacksEpochId::Epoch2_05,
14151437
&miner,
14161438
&miner,
14171439
&vec![user.clone()],
@@ -1420,6 +1442,7 @@ mod test {
14201442
);
14211443
let (parent_user_1, reward_user_1) = StacksChainState::calculate_miner_reward(
14221444
false,
1445+
StacksEpochId::Epoch2_05,
14231446
&user,
14241447
&miner,
14251448
&vec![user.clone()],
@@ -1460,6 +1483,7 @@ mod test {
14601483

14611484
let (parent_reward, miner_reward) = StacksChainState::calculate_miner_reward(
14621485
false,
1486+
StacksEpochId::Epoch2_05,
14631487
&participant,
14641488
&participant,
14651489
&vec![],

src/chainstate/stacks/db/blocks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5002,7 +5002,7 @@ impl StacksChainState {
50025002
chainstate_tx: &'b mut ChainstateTx,
50035003
clarity_instance: &'a mut ClarityInstance,
50045004
burn_dbconn: &'b dyn BurnStateDB,
5005-
conn: &Connection,
5005+
conn: &Connection, // connection to the sortition DB
50065006
chain_tip: &StacksHeaderInfo,
50075007
burn_tip: BurnchainHeaderHash,
50085008
burn_tip_height: u32,
@@ -5077,6 +5077,7 @@ impl StacksChainState {
50775077

50785078
let matured_miner_rewards_opt = match StacksChainState::find_mature_miner_rewards(
50795079
&mut clarity_tx,
5080+
conn,
50805081
&chain_tip,
50815082
latest_matured_miners,
50825083
matured_miner_parent,

0 commit comments

Comments
 (0)