Skip to content

Commit e025e7f

Browse files
yperbasisclaude
andauthored
Switch block Difficulty/Number/BaseFee from big.Int to uint256 (#17999)
Also, this PR simplifies `rpc/gasprice/gasprice.go‎`. --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent dcf29cc commit e025e7f

File tree

171 files changed

+900
-1321
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+900
-1321
lines changed

cl/cltypes/beacon_block_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package cltypes
1919
import (
2020
_ "embed"
2121
"encoding/json"
22-
"math/big"
2322
"testing"
2423

2524
"github.com/holiman/uint256"
@@ -54,7 +53,7 @@ func TestBeaconBody(t *testing.T) {
5453
blobKzgCommitments := solid.NewStaticListSSZ[*KZGCommitment](MaxBlobsCommittmentsPerBlock, 48)
5554
version := clparams.DenebVersion
5655
block := types.NewBlock(&types.Header{
57-
BaseFee: big.NewInt(1),
56+
BaseFee: uint256.NewInt(1),
5857
}, []types.Transaction{types.NewTransaction(1, [20]byte{}, uint256.NewInt(1), 5, uint256.NewInt(2), nil)}, nil, nil, types.Withdrawals{&types.Withdrawal{
5958
Index: 69,
6059
}})

cl/cltypes/eth1_block.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package cltypes
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"math/big"
2322

2423
"github.com/holiman/uint256"
2524

@@ -312,7 +311,7 @@ func (b *Eth1Block) RlpHeader(parentRoot *common.Hash, executionReqHash common.H
312311
for i, j := 0, len(reversedBaseFeePerGas)-1; i < j; i, j = i+1, j-1 {
313312
reversedBaseFeePerGas[i], reversedBaseFeePerGas[j] = reversedBaseFeePerGas[j], reversedBaseFeePerGas[i]
314313
}
315-
baseFee := new(big.Int).SetBytes(reversedBaseFeePerGas)
314+
baseFee := new(uint256.Int).SetBytes(reversedBaseFeePerGas)
316315
// If the block version is Capella or later, calculate the withdrawals hash.
317316
var withdrawalsHash *common.Hash
318317
if b.version >= clparams.CapellaVersion {
@@ -338,8 +337,7 @@ func (b *Eth1Block) RlpHeader(parentRoot *common.Hash, executionReqHash common.H
338337
TxHash: types.DeriveSha(types.BinaryTransactions(b.Transactions.UnderlyngReference())),
339338
ReceiptHash: b.ReceiptsRoot,
340339
Bloom: b.LogsBloom,
341-
Difficulty: merge.ProofOfStakeDifficulty,
342-
Number: new(big.Int).SetUint64(b.BlockNumber),
340+
Difficulty: *merge.ProofOfStakeDifficulty,
343341
GasLimit: b.GasLimit,
344342
GasUsed: b.GasUsed,
345343
Time: b.Time,
@@ -350,6 +348,7 @@ func (b *Eth1Block) RlpHeader(parentRoot *common.Hash, executionReqHash common.H
350348
WithdrawalsHash: withdrawalsHash,
351349
ParentBeaconBlockRoot: parentRoot,
352350
}
351+
header.Number.SetUint64(b.BlockNumber)
353352

354353
if b.version >= clparams.DenebVersion {
355354
blobGasUsed := b.BlobGasUsed

cl/phase1/execution_client/execution_client_rpc.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"strings"
2626
"time"
2727

28+
"github.com/holiman/uint256"
29+
2830
"github.com/erigontech/erigon/cl/clparams"
2931
"github.com/erigontech/erigon/cl/cltypes"
3032
"github.com/erigontech/erigon/cl/phase1/execution_client/rpc_helper"
@@ -85,7 +87,7 @@ func (cc *ExecutionClientRpc) NewPayload(
8587
for i, j := 0, len(reversedBaseFeePerGas)-1; i < j; i, j = i+1, j-1 {
8688
reversedBaseFeePerGas[i], reversedBaseFeePerGas[j] = reversedBaseFeePerGas[j], reversedBaseFeePerGas[i]
8789
}
88-
baseFee := new(big.Int).SetBytes(reversedBaseFeePerGas)
90+
baseFee := new(uint256.Int).SetBytes(reversedBaseFeePerGas)
8991
var engineMethod string
9092
// determine the engine method
9193
switch payload.Version() {
@@ -117,8 +119,7 @@ func (cc *ExecutionClientRpc) NewPayload(
117119
BlockHash: payload.BlockHash,
118120
}
119121

120-
request.BaseFeePerGas = new(hexutil.Big)
121-
*request.BaseFeePerGas = hexutil.Big(*baseFee)
122+
request.BaseFeePerGas = (*hexutil.Big)(baseFee.ToBig())
122123
payloadBody := payload.Body()
123124
// Setup transactionbody
124125
request.Withdrawals = payloadBody.Withdrawals

cmd/evm/internal/t8ntool/execution.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
package t8ntool
2121

2222
import (
23-
"math/big"
24-
2523
"github.com/holiman/uint256"
2624

2725
"github.com/erigontech/erigon/common"
@@ -51,17 +49,17 @@ type ommer struct {
5149
//go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go
5250
type stEnv struct {
5351
Coinbase common.Address `json:"currentCoinbase" gencodec:"required"`
54-
Difficulty *big.Int `json:"currentDifficulty"`
55-
Random *big.Int `json:"currentRandom"`
52+
Difficulty *uint256.Int `json:"currentDifficulty"`
53+
Random *uint256.Int `json:"currentRandom"`
5654
MixDigest common.Hash `json:"mixHash,omitempty"`
57-
ParentDifficulty *big.Int `json:"parentDifficulty"`
55+
ParentDifficulty *uint256.Int `json:"parentDifficulty"`
5856
GasLimit uint64 `json:"currentGasLimit" gencodec:"required"`
5957
Number uint64 `json:"currentNumber" gencodec:"required"`
6058
Timestamp uint64 `json:"currentTimestamp" gencodec:"required"`
6159
ParentTimestamp uint64 `json:"parentTimestamp,omitempty"`
6260
BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"`
6361
Ommers []ommer `json:"ommers,omitempty"`
64-
BaseFee *big.Int `json:"currentBaseFee,omitempty"`
62+
BaseFee *uint256.Int `json:"currentBaseFee,omitempty"`
6563
ParentUncleHash common.Hash `json:"parentUncleHash"`
6664
UncleHash common.Hash `json:"uncleHash,omitempty"`
6765
Withdrawals []*types.Withdrawal `json:"withdrawals,omitempty"`
@@ -116,7 +114,7 @@ func MakePreState(chainRules *chain.Rules, tx kv.TemporalRwTx, sd *execctx.Share
116114
// parent timestamp + difficulty.
117115
// Note: this method only works for ethash engine.
118116
func calcDifficulty(config *chain.Config, number, currentTime, parentTime uint64,
119-
parentDifficulty *big.Int, parentUncleHash common.Hash) *big.Int {
117+
parentDifficulty uint256.Int, parentUncleHash common.Hash) *uint256.Int {
120118
uncleHash := parentUncleHash
121119
if uncleHash == (common.Hash{}) {
122120
uncleHash = empty.UncleHash
@@ -125,8 +123,9 @@ func calcDifficulty(config *chain.Config, number, currentTime, parentTime uint64
125123
ParentHash: common.Hash{},
126124
UncleHash: uncleHash,
127125
Difficulty: parentDifficulty,
128-
Number: new(big.Int).SetUint64(number - 1), // nolint:govet
129126
Time: parentTime,
130127
}
131-
return ethash.CalcDifficulty(config, currentTime, parent.Time, parent.Difficulty, number-1, parent.UncleHash)
128+
parent.Number.SetUint64(number - 1)
129+
diff := ethash.CalcDifficulty(config, currentTime, parent.Time, parent.Difficulty, number-1, parent.UncleHash)
130+
return &diff
132131
}

cmd/evm/internal/t8ntool/gen_stenv.go

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/evm/internal/t8ntool/transition.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,18 @@ func Main(ctx *cli.Context) error {
271271
env.Timestamp, env.ParentTimestamp))
272272
}
273273
prestate.Env.Difficulty = calcDifficulty(chainConfig, env.Number, env.Timestamp,
274-
env.ParentTimestamp, env.ParentDifficulty, env.ParentUncleHash)
274+
env.ParentTimestamp, *env.ParentDifficulty, env.ParentUncleHash)
275275
}
276276

277277
// manufacture block from above inputs
278278
header := NewHeader(prestate.Env)
279279

280280
var ommerHeaders = make([]*types.Header, len(prestate.Env.Ommers))
281-
header.Number.Add(header.Number, big.NewInt(int64(len(prestate.Env.Ommers))))
281+
header.Number.AddUint64(&header.Number, uint64(len(prestate.Env.Ommers)))
282282
for i, ommer := range prestate.Env.Ommers {
283-
var ommerN big.Int
283+
var ommerN uint256.Int
284284
ommerN.SetUint64(header.Number.Uint64() - ommer.Delta)
285-
ommerHeaders[i] = &types.Header{Coinbase: ommer.Address, Number: &ommerN}
285+
ommerHeaders[i] = &types.Header{Coinbase: ommer.Address, Number: ommerN}
286286
}
287287
block := types.NewBlock(header, txs, ommerHeaders, nil /* receipts */, prestate.Env.Withdrawals)
288288

@@ -628,9 +628,11 @@ func dispatchOutput(ctx *cli.Context, baseDir string, result *protocol.Ephemeral
628628
func NewHeader(env stEnv) *types.Header {
629629
var header types.Header
630630
header.Coinbase = env.Coinbase
631-
header.Difficulty = env.Difficulty
631+
if env.Difficulty != nil {
632+
header.Difficulty = *env.Difficulty
633+
}
632634
header.GasLimit = env.GasLimit
633-
header.Number = new(big.Int).SetUint64(env.Number)
635+
header.Number.SetUint64(env.Number)
634636
header.Time = env.Timestamp
635637
header.BaseFee = env.BaseFee
636638
header.MixDigest = env.MixDigest

cmd/evm/runner.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"encoding/json"
2626
"fmt"
2727
"io"
28-
"math/big"
2928
"os"
3029
goruntime "runtime"
3130
"runtime/pprof"
@@ -265,9 +264,9 @@ func runCmd(ctx *cli.Context) error {
265264
GasPrice: *gasPrice,
266265
Value: *value,
267266
Difficulty: genesisConfig.Difficulty,
268-
Time: new(big.Int).SetUint64(genesisConfig.Timestamp),
267+
Time: genesisConfig.Timestamp,
269268
Coinbase: accounts.InternAddress(genesisConfig.Coinbase),
270-
BlockNumber: new(big.Int).SetUint64(genesisConfig.Number),
269+
BlockNumber: genesisConfig.Number,
271270
}
272271

273272
if tracer != nil {
@@ -329,8 +328,8 @@ func runCmd(ctx *cli.Context) error {
329328
rules := &chain.Rules{}
330329
if chainConfig != nil {
331330
blockContext := evmtypes.BlockContext{
332-
BlockNumber: runtimeConfig.BlockNumber.Uint64(),
333-
Time: runtimeConfig.Time.Uint64(),
331+
BlockNumber: runtimeConfig.BlockNumber,
332+
Time: runtimeConfig.Time,
334333
}
335334
rules = blockContext.Rules(chainConfig)
336335
}

cmd/rlpgen/testing/testing_struct.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package testing
33
import (
44
"math/big"
55

6+
"github.com/holiman/uint256"
7+
68
"github.com/erigontech/erigon/common"
79
"github.com/erigontech/erigon/execution/types"
8-
"github.com/holiman/uint256"
910
)
1011

1112
type TestingStruct struct {

cmd/rpcdaemon/cli/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"crypto/rand"
2222
"errors"
2323
"fmt"
24-
"math/big"
2524
"net"
2625
"net/http"
2726
"net/url"
@@ -30,6 +29,7 @@ import (
3029
"strings"
3130
"time"
3231

32+
"github.com/holiman/uint256"
3333
"github.com/spf13/cobra"
3434
"golang.org/x/sync/errgroup"
3535
"golang.org/x/sync/semaphore"
@@ -1128,7 +1128,7 @@ func (e *remoteRulesEngine) SealHash(_ *types.Header) common.Hash {
11281128
panic("remoteRulesEngine.SealHash not supported")
11291129
}
11301130

1131-
func (e *remoteRulesEngine) CalcDifficulty(_ rules.ChainHeaderReader, _ uint64, _ uint64, _ *big.Int, _ uint64, _ common.Hash, _ common.Hash, _ uint64) *big.Int {
1131+
func (e *remoteRulesEngine) CalcDifficulty(_ rules.ChainHeaderReader, _ uint64, _ uint64, _ uint256.Int, _ uint64, _ common.Hash, _ common.Hash, _ uint64) uint256.Int {
11321132
panic("remoteRulesEngine.CalcDifficulty not supported")
11331133
}
11341134

cmd/snapshots/genfromrpc/genfromrpc.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ type BlockJson struct {
5959
TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`
6060
ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`
6161
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
62-
Difficulty *hexutil.Big `json:"difficulty" gencodec:"required"`
63-
Number *hexutil.Big `json:"number" gencodec:"required"`
62+
Difficulty uint256.Int `json:"difficulty" gencodec:"required"`
63+
Number uint256.Int `json:"number" gencodec:"required"`
6464
GasLimit hexutil.Uint64 `json:"gasLimit" gencodec:"required"`
6565
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
6666
Time hexutil.Uint64 `json:"timestamp" gencodec:"required"`
6767
Extra hexutil.Bytes `json:"extraData" gencodec:"required"`
6868
MixDigest common.Hash `json:"mixHash"` // prevRandao after EIP-4399
6969
Nonce types.BlockNonce `json:"nonce"`
7070

71-
BaseFee *hexutil.Big `json:"baseFeePerGas"` // EIP-1559
71+
BaseFee *uint256.Int `json:"baseFeePerGas"` // EIP-1559
7272
WithdrawalsHash *common.Hash `json:"withdrawalsRoot"` // EIP-4895
7373
BlobGasUsed *hexutil.Uint64 `json:"blobGasUsed"`
7474
ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas"`
@@ -359,15 +359,15 @@ func getBlockByNumber(client *rpc.Client, blockNumber *big.Int, verify bool) (*t
359359
TxHash: block.TxHash,
360360
ReceiptHash: block.ReceiptHash,
361361
Bloom: block.Bloom,
362-
Difficulty: (*big.Int)(block.Difficulty),
363-
Number: (*big.Int)(block.Number),
362+
Difficulty: block.Difficulty,
363+
Number: block.Number,
364364
GasLimit: block.GasLimit.Uint64(),
365365
GasUsed: block.GasUsed.Uint64(),
366366
Time: block.Time.Uint64(),
367367
Extra: block.Extra,
368368
MixDigest: block.MixDigest,
369369
Nonce: block.Nonce,
370-
BaseFee: (*big.Int)(block.BaseFee),
370+
BaseFee: block.BaseFee,
371371
WithdrawalsHash: block.WithdrawalsHash,
372372
BlobGasUsed: (*uint64)(block.BlobGasUsed),
373373
ExcessBlobGas: (*uint64)(block.ExcessBlobGas),

0 commit comments

Comments
 (0)