Skip to content

Commit 9889f1b

Browse files
committed
BRG4-20.3: pass current block number in to NewPoLTx for more clarity
(cherry picked from commit ac2da12)
1 parent 49847a6 commit 9889f1b

File tree

6 files changed

+57
-24
lines changed

6 files changed

+57
-24
lines changed

core/block_validator.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package core
1919
import (
2020
"errors"
2121
"fmt"
22-
"math/big"
2322

2423
"github.com/davecgh/go-spew/spew"
2524
"github.com/ethereum/go-ethereum/consensus"
@@ -100,7 +99,7 @@ func (v *BlockValidator) ValidateBody(block *types.Block) error {
10099
polTx, err := types.NewPoLTx(
101100
v.config.ChainID,
102101
v.config.Berachain.Prague1.PoLDistributorAddress,
103-
new(big.Int).Sub(block.Number(), big.NewInt(1)),
102+
block.Number(),
104103
params.PoLTxGasLimit,
105104
block.BaseFee(),
106105
block.ProposerPubkey(),

core/block_validator_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func TestValidateBody_Prague1_Valid(t *testing.T) {
9393
chain, validator := buildTestChain(t, cfg)
9494

9595
// Build PoL tx + dummy tx.
96-
polTx, err := types.NewPoLTx(cfg.ChainID, distributor, big.NewInt(0), params.PoLTxGasLimit, big.NewInt(1000000000), samplePubkey())
96+
polTx, err := types.NewPoLTx(cfg.ChainID, distributor, big.NewInt(1), params.PoLTxGasLimit, big.NewInt(1000000000), samplePubkey())
9797
if err != nil {
9898
t.Fatalf("failed to create PoL tx: %v", err)
9999
}
@@ -113,7 +113,7 @@ func TestValidateBody_Prague1_InvalidHash(t *testing.T) {
113113

114114
// PoL tx with WRONG pubkey (different from header.ParentProposerPubkey).
115115
wrongPk := &common.Pubkey{}
116-
polTx, _ := types.NewPoLTx(cfg.ChainID, distributor, big.NewInt(0), params.PoLTxGasLimit, big.NewInt(1000000000), wrongPk)
116+
polTx, _ := types.NewPoLTx(cfg.ChainID, distributor, big.NewInt(1), params.PoLTxGasLimit, big.NewInt(1000000000), wrongPk)
117117
block := makeBlock(chain.CurrentHeader(), types.Transactions{polTx}, 1)
118118

119119
if err := validator.ValidateBody(block); err == nil {
@@ -126,7 +126,7 @@ func TestValidateBody_Prague1_MisplacedPoL(t *testing.T) {
126126
cfg := newPrague1Config(distributor)
127127
chain, validator := buildTestChain(t, cfg)
128128

129-
polTx, _ := types.NewPoLTx(cfg.ChainID, distributor, big.NewInt(0), params.PoLTxGasLimit, big.NewInt(1000000000), samplePubkey())
129+
polTx, _ := types.NewPoLTx(cfg.ChainID, distributor, big.NewInt(1), params.PoLTxGasLimit, big.NewInt(1000000000), samplePubkey())
130130
dummyTx := types.NewTx(&types.LegacyTx{Nonce: 1})
131131
// PoL tx placed second.
132132
block := makeBlock(chain.CurrentHeader(), types.Transactions{dummyTx, polTx}, 1)
@@ -147,7 +147,7 @@ func TestValidateBody_PrePrague1_PoLProhibited(t *testing.T) {
147147
cfg.Berachain.Prague1.PoLDistributorAddress = distributor
148148
chain, validator := buildTestChain(t, &cfg)
149149

150-
polTx, _ := types.NewPoLTx(cfg.ChainID, distributor, big.NewInt(0), params.PoLTxGasLimit, big.NewInt(1000000000), samplePubkey())
150+
polTx, _ := types.NewPoLTx(cfg.ChainID, distributor, big.NewInt(1), params.PoLTxGasLimit, big.NewInt(1000000000), samplePubkey())
151151
block := makeBlock(chain.CurrentHeader(), types.Transactions{polTx}, 1) // timestamp 1 < future
152152

153153
if err := validator.ValidateBody(block); err == nil {

core/types/tx_pol.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type PoLTx struct {
4545
func NewPoLTx(
4646
chainID *big.Int,
4747
distributorAddress common.Address,
48-
distributionBlockNumber *big.Int,
48+
currentBlockNumber *big.Int,
4949
gasLimit uint64,
5050
baseFee *big.Int,
5151
pubkey *common.Pubkey,
@@ -54,11 +54,14 @@ func NewPoLTx(
5454
if err != nil {
5555
return nil, err
5656
}
57+
if currentBlockNumber.Sign() <= 0 {
58+
return nil, errors.New("PoL tx must only be created for a block number greater than 0")
59+
}
5760
return NewTx(&PoLTx{
5861
ChainID: chainID,
5962
From: params.SystemAddress,
6063
To: distributorAddress,
61-
Nonce: distributionBlockNumber.Uint64(),
64+
Nonce: currentBlockNumber.Uint64() - 1, // PoL txs are distributing for the previous block
6265
GasLimit: gasLimit,
6366
GasPrice: baseFee,
6467
Data: data,

core/types/tx_pol_test.go

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,59 @@ func TestNewPoLTx_NilPubkey(t *testing.T) {
8686
}
8787
}
8888

89-
// TestNewPoLTx_NegativeBlockNumber ensures negative block numbers are handled
90-
// without panicking (uint64 wrap-around is expected).
91-
func TestNewPoLTx_NegativeBlockNumber(t *testing.T) {
89+
// TestNewPoLTx_InvalidBlockNumber ensures that block numbers <= 0 are rejected.
90+
func TestNewPoLTx_InvalidBlockNumber(t *testing.T) {
9291
chainID := big.NewInt(1)
9392
distributor := common.Address{}
94-
negBlock := big.NewInt(-1)
9593
baseFee := big.NewInt(1000000000)
96-
97-
tx, err := NewPoLTx(chainID, distributor, negBlock, params.PoLTxGasLimit, baseFee, samplePubkey())
98-
if err != nil {
99-
t.Fatalf("unexpected error: %v", err)
100-
}
101-
if got, want := tx.Nonce(), negBlock.Uint64(); got != want {
102-
t.Fatalf("nonce mismatch: have %d, want %d", got, want)
103-
}
94+
expectedErr := "PoL tx must only be created for a block number greater than 0"
95+
96+
testCases := []struct {
97+
name string
98+
blockNumber *big.Int
99+
}{
100+
{"negative block number", big.NewInt(-1)},
101+
{"zero block number", big.NewInt(0)},
102+
}
103+
104+
for _, tc := range testCases {
105+
t.Run(tc.name, func(t *testing.T) {
106+
tx, err := NewPoLTx(chainID, distributor, tc.blockNumber, params.PoLTxGasLimit, baseFee, samplePubkey())
107+
if err == nil {
108+
t.Fatalf("expected error for block number %v, but got nil", tc.blockNumber)
109+
}
110+
if tx != nil {
111+
t.Fatalf("expected nil transaction when error occurs, but got %v", tx)
112+
}
113+
if err.Error() != expectedErr {
114+
t.Fatalf("error message mismatch: have %q, want %q", err.Error(), expectedErr)
115+
}
116+
})
117+
}
118+
119+
// Test that positive block numbers work correctly
120+
t.Run("positive block number", func(t *testing.T) {
121+
positiveBlock := big.NewInt(123)
122+
tx, err := NewPoLTx(chainID, distributor, positiveBlock, params.PoLTxGasLimit, baseFee, samplePubkey())
123+
if err != nil {
124+
t.Fatalf("unexpected error for positive block number: %v", err)
125+
}
126+
if tx == nil {
127+
t.Fatalf("expected transaction for positive block number, but got nil")
128+
}
129+
// Nonce should be blockNumber - 1 (as per the implementation)
130+
expectedNonce := positiveBlock.Uint64() - 1
131+
if got := tx.Nonce(); got != expectedNonce {
132+
t.Fatalf("nonce mismatch: have %d, want %d", got, expectedNonce)
133+
}
134+
})
104135
}
105136

106137
// TestIsPoLDistribution exercises positive and negative cases for the helper.
107138
func TestIsPoLDistribution(t *testing.T) {
108139
distributor := common.HexToAddress("0x1000000000000000000000000000000000000001")
109140
baseFee := big.NewInt(1000000000)
110-
tx, err := NewPoLTx(big.NewInt(1), distributor, big.NewInt(0), params.PoLTxGasLimit, baseFee, samplePubkey())
141+
tx, err := NewPoLTx(big.NewInt(1), distributor, big.NewInt(1), params.PoLTxGasLimit, baseFee, samplePubkey())
111142
if err != nil {
112143
t.Fatalf("failed to build PoL tx: %v", err)
113144
}
@@ -143,7 +174,7 @@ func TestIsPoLDistribution(t *testing.T) {
143174
// TestPoLTx_RawSignatureValues confirms that PoLTx reports no signature.
144175
func TestPoLTx_RawSignatureValues(t *testing.T) {
145176
baseFee := big.NewInt(1000000000)
146-
tx, err := NewPoLTx(big.NewInt(1), common.Address{}, big.NewInt(0), params.PoLTxGasLimit, baseFee, samplePubkey())
177+
tx, err := NewPoLTx(big.NewInt(1), common.Address{}, big.NewInt(1), params.PoLTxGasLimit, baseFee, samplePubkey())
147178
if err != nil {
148179
t.Fatalf("failed to create PoL tx: %v", err)
149180
}

internal/ethapi/simulate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (sim *simulator) execute(ctx context.Context, blocks []simBlock) ([]*simBlo
207207
polTx, err := types.NewPoLTx(
208208
sim.chainConfig.ChainID,
209209
sim.chainConfig.Berachain.Prague1.PoLDistributorAddress,
210-
new(big.Int).Sub(result.Number(), big.NewInt(1)),
210+
result.Number(),
211211
params.PoLTxGasLimit,
212212
result.BaseFee(),
213213
result.ProposerPubkey(),

miner/worker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ func (miner *Miner) fillTransactions(interrupt *atomic.Int32, env *environment)
502502
tx, err := types.NewPoLTx(
503503
miner.chainConfig.ChainID,
504504
miner.chainConfig.Berachain.Prague1.PoLDistributorAddress,
505-
new(big.Int).Sub(env.header.Number, big.NewInt(1)),
505+
env.header.Number,
506506
params.PoLTxGasLimit,
507507
env.header.BaseFee,
508508
env.header.ParentProposerPubkey,

0 commit comments

Comments
 (0)