Skip to content

Add priority ordering core#1083

Open
LorenzSchueler wants to merge 4 commits into
mainfrom
lorenz/gbs-7
Open

Add priority ordering core#1083
LorenzSchueler wants to merge 4 commits into
mainfrom
lorenz/gbs-7

Conversation

@LorenzSchueler

@LorenzSchueler LorenzSchueler commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

This PR adds the pure ordering core of the transaction-priorities feature.
This is used in a follow-up to order the transactions in the c_block_callbacks.go after the scrambler run.

Prioritize reorders a base-ordered, pre-filtered transaction list so that registry-prioritized transactions form a prefix in (level desc, weight desc, hash asc) order, subject to two coupled constraints:

  • Per-sender nonce ordering — a transaction keeps its priority only while it extends its sender's contiguous run of prioritized nonces from the block-start account nonce; stale nonces are demoted and the first gap demotes the rest of the sender's txs (they would be nonce-too-high in the prefix).
  • Per-entity gas budget — each entity may spend at most Config.MaxGasPerEntityPerBlock gas (by gas limit) on prioritized transactions. Selection walks the per-sender frontiers greedily; a frontier that doesn't fit its entity's remaining budget blocks that sender's run, so budget is only spent on transactions that can actually execute in the prefix.
    Everything not selected (non-prioritized, nonce-blocked, over budget) keeps its relative base order after the prefix. The function is a pure, deterministic total function of its inputs.

Classification is abstracted behind a Classifier interface (the EVM-backed implementation and the block-formation call site land in a follow-up).

Unit tests cover each helper (classification, nonce-run, budgeted prefix selection, packing) plus an end-to-end scenario exercising all constraints together.

@LorenzSchueler
LorenzSchueler requested a review from Copilot July 23, 2026 13:32
@LorenzSchueler LorenzSchueler changed the title Add priority ordering utilities Add priority ordering core Jul 23, 2026

Copilot AI left a comment

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.

Pull request overview

Adds the deterministic, pure “priority ordering core” for the transaction-priorities feature, including helper routines and unit tests, so block formation can produce a prioritized prefix while enforcing sender-nonce contiguity and per-entity gas budgets.

Changes:

  • Updated Config.MaxGasPerEntityPerBlock documentation to reflect sender-chain demotion under budget constraints.
  • Added Prioritize implementation plus helper functions for classification, per-sender nonce-run reduction, budgeted greedy prefix selection, and recombination with the remainder.
  • Added unit tests covering comparators, helpers, and an end-to-end scenario spanning both constraints.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
gossip/blockproc/priorities/types.go Updates config comment to match budget/nonce-chain demotion semantics.
gossip/blockproc/priorities/ordering.go Introduces the pure transaction prioritization/ordering core and helper routines.
gossip/blockproc/priorities/ordering_test.go Adds unit tests for ordering semantics and helper behavior.
Comments suppressed due to low confidence (2)

gossip/blockproc/priorities/ordering_test.go:89

  • This test assumes a specific hash ordering between makeTxG(0, 21000) and makeTxG(0, 22000). Hash ordering between two arbitrary txs isn’t something the test should rely on. Derive lowHash/highHash by comparing the hashes and swapping to make the test resilient to changes in tx encoding/hashing.
	// Same nonce, different hash
	lowHash, highHash := makeTxG(0, 21000), makeTxG(0, 22000)
	require.Negative(t, bytes.Compare(lowHash.Hash().Bytes(), highHash.Hash().Bytes()))
	prio := Prio(1, 10, 100)

gossip/blockproc/priorities/ordering_test.go:366

  • This test assumes makeTxN(0) has a higher hash than makeTxN(3). Since tx hashes aren’t ordered by nonce, this is brittle across encoding/hashing changes. Swap the two based on bytes.Compare so lowHashTx/highHashTx are derived from the actual hashes produced.
	highHashTx, lowHashTx := makeTxN(0), makeTxN(3)
	require.Positive(t, bytes.Compare(highHashTx.Hash().Bytes(), lowHashTx.Hash().Bytes()))


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread gossip/blockproc/priorities/ordering.go
Comment thread gossip/blockproc/priorities/ordering_test.go Outdated
Comment thread gossip/blockproc/priorities/ordering_test.go
@LorenzSchueler
LorenzSchueler force-pushed the lorenz/gbs-7 branch 2 times, most recently from ac08560 to e48ba7c Compare July 23, 2026 13:45
@codecov

codecov Bot commented Jul 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.75281% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
gossip/blockproc/priorities/ordering.go 97.75% 1 Missing and 1 partial ⚠️
Files with missing lines Coverage Δ
gossip/blockproc/priorities/types.go 100.00% <ø> (ø)
gossip/blockproc/priorities/ordering.go 97.75% <97.75%> (ø)

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

LuisPH3
LuisPH3 previously approved these changes Jul 23, 2026

@LuisPH3 LuisPH3 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The pr proposes an algorithm which sorts a list of transactions using an order:
the order prepends transactions based in their priority level/weight.
It respects the nonce order of prioritised transactions. tiebreakers with hash make replicated resolution deterministic.

Comment thread gossip/blockproc/priorities/types.go Outdated
Comment thread gossip/blockproc/priorities/ordering.go Outdated
Comment thread gossip/blockproc/priorities/ordering.go Outdated
Comment thread gossip/blockproc/priorities/ordering.go Outdated
Comment thread gossip/blockproc/priorities/ordering.go Outdated
Comment thread gossip/blockproc/priorities/ordering.go Outdated
Comment thread gossip/blockproc/priorities/ordering.go Outdated
Comment thread gossip/blockproc/priorities/ordering_test.go Outdated
Comment thread gossip/blockproc/priorities/ordering_test.go
LuisPH3
LuisPH3 previously approved these changes Jul 24, 2026
simonlechner
simonlechner previously approved these changes Jul 24, 2026

@simonlechner simonlechner left a comment

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.

Just some minor comments.
The amount of code comments will be hard to keep up to date in future changes.

Comment on lines +54 to +57
if c := cmp.Compare(x.priority.Level, y.priority.Level); c != 0 {
return c
}
if c := cmp.Compare(x.priority.Weight, y.priority.Weight); c != 0 {

@simonlechner simonlechner Jul 24, 2026

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.

Suggested change
if c := cmp.Compare(x.priority.Level, y.priority.Level); c != 0 {
return c
}
if c := cmp.Compare(x.priority.Weight, y.priority.Weight); c != 0 {
if c := x.priority.Cmp(y.priority); c != 0 {

No need to reimplement the priority comparison.

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.

Not quite. Priority.Cmp returns equal if level is 0 no matter the weight. If we use it here we would have a situation where if the level is 0 we ignore the weight and break ties only using the hash. This would be weird since this breaks the order level→weight→hash

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.

Right, then a unit test case is missing. I tried it locally before the review and no test failed.

Comment thread gossip/blockproc/priorities/ordering.go Outdated
HerbertJordan
HerbertJordan previously approved these changes Jul 24, 2026

@HerbertJordan HerbertJordan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very nice and clean code, for the feature as well as for the tests. Great!

I added one suggestion for a named type and two potential issues that should be addressed. Please let me know what you think

Comment thread gossip/blockproc/priorities/ordering.go Outdated
Comment thread gossip/blockproc/priorities/ordering.go
Comment thread gossip/blockproc/priorities/ordering.go
Comment thread gossip/blockproc/priorities/ordering.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants