Skip to content

feat(node): txn prioritization based on gas parameters #1185

Merged
raulk merged 10 commits intomainfrom
base-fee-filtering
Dec 17, 2024
Merged

feat(node): txn prioritization based on gas parameters #1185
raulk merged 10 commits intomainfrom
base-fee-filtering

Conversation

@cryptoAtwill
Copy link
Copy Markdown
Contributor

@cryptoAtwill cryptoAtwill commented Oct 25, 2024

This is a follow up to gas market feature and closes #1183.

The implementation follows the method 2 described. A high-level review is as follows:

@cryptoAtwill cryptoAtwill requested a review from a team as a code owner October 25, 2024 08:34
)
.context("error creating check state")?
}
None => self.create_check_state()?,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid loading the 5 blocks state params for check_tx, to do this, we must change the commit to hold some historical check_state instead of purging everything. Not sure if we should do in this PR.

@raulk raulk changed the title feat(node): txn prioritizatioin based on gas parameters feat(node): txn prioritization based on gas parameters Nov 27, 2024
Comment on lines +43 to +51
if msg.gas_fee_cap >= base_fee {
let i = msg.gas_fee_cap.clone() - base_fee + msg.gas_premium.clone();
i.atto()
.min(&BigInt::from(i64::MAX))
.to_i64()
.expect("clipped to i64 max")
} else {
0
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. These kinds of conditionals are easier to follow when written as an early return. If the gas_fee_cap is below the base fee, return 0.

  2. Are we sure 0 is the right value here? Why not return i64::MIN if the message is non-includable? Right now messages with gas_fee_cap == base_fee && gas_premium == 0 would rank exactly the same as non-includable messages. Seems wrong?

  3. Adding the gas premium to the delta between the gas_fee_cap and the base fee is incorrect. The gas_fee_cap includes the payable base fee and the gas premium. When the message is includable, our priority should be proportional to the effective gas premium. See how the effective gas premium is calculated in ref-fvm; copy the logic here.

  4. i64::MAX atto is equal to 9.223372036854775807 base units. We are probably safe in clipping the effective premium to i64::MAX, since nobody sane will set such a high premium, but I'm uncomfortable with the panic here. I suggest this instead, which saturates to i64::MAX:

    effective_premium.atto().to_i64().unwrap_or(i64::MAX);

@cryptoAtwill
Copy link
Copy Markdown
Contributor Author

@raulk Updated, please help take another look.

@cryptoAtwill cryptoAtwill requested a review from raulk December 12, 2024 12:44
Comment on lines +622 to +624
let mut t = to_check_tx(ret);
t.priority = priority;
t
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: feed the priority as a param to to_check_tx?

#[derive(Clone, Debug)]
pub struct TxnPriorityCalculator {
/// Ring buffer of base fee history
base_fee_history: Vec<Option<TokenAmount>>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we need to keep a base fee history? In my head, this component would just know the current base fee and prioritise inclusion based on it -- past base fees are irrelevant. A transaction is either includable or not includable now. And we know for certain that after a block is committed, CometBFT will recheck all pending transactions before proposing the next block, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was quite early discussion on one of the huddles to use the lowest base fee in recent history to let slightly more transaction to go through to avoid empty blocks. An array is also tuneable in how base fee can be selected. The requirement is updated slightly and base fee selection is now includable/non-includable. Then the most recent base fee should be enough. I can update this accordingly.

Copy link
Copy Markdown
Contributor

@raulk raulk Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to fix this. The base fee determines inclusion and the burn rate (network fee). By no means should a transaction be included if its gas fee cap is below the current base fee for the block it's included in. That's a consensus fault.

@cryptoAtwill
Copy link
Copy Markdown
Contributor Author

@raulk updated accordingly. Now the priority calculation is based on the latest base fee from the block gas market reading. Anything below will have priority i64::MIN.

@cryptoAtwill cryptoAtwill requested a review from raulk December 16, 2024 13:45
Copy link
Copy Markdown
Contributor

@raulk raulk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An integration test here would be great.

  1. Submit many transactions with high gas limit to stuff blocks and make the base fee spike to X or above.
  2. Submit a transaction with gas fee cap way below X but above the minimum base fee.
  3. Stop the flood so the base fee decreases, and ensure the transaction does not appear until the base fee is below the gas fee cap.

I will merge this and open an issue for these extra tests.

@raulk raulk merged commit 94ec24f into main Dec 17, 2024
@raulk raulk deleted the base-fee-filtering branch December 17, 2024 18:52
@raulk
Copy link
Copy Markdown
Contributor

raulk commented Dec 17, 2024

@sanderpick feel free to integrate this fix on your side. There are still some edge cases for us to deal with (e.g. rejecting transactions below minimum base fee).

@sanderpick
Copy link
Copy Markdown
Collaborator

@sanderpick feel free to integrate this fix on your side. There are still some edge cases for us to deal with (e.g. rejecting transactions below minimum base fee).

great, ty!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

Enforce new base fee in mempool after gas market base fee update

3 participants