Skip to content

Commit f151a52

Browse files
authored
Withdrawal amount in GWei (#6578)
See ethereum/execution-apis#354 & ethereum/EIPs#6325. Prerequisite: ledgerwatch/erigon-lib#832.
1 parent 9e452fe commit f151a52

File tree

12 files changed

+39
-40
lines changed

12 files changed

+39
-40
lines changed

cmd/erigon-el/eth1/execution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (e *Eth1Execution) InsertBodies(ctx context.Context, req *execution.InsertB
8585
Index: withdrawal.Index,
8686
Validator: withdrawal.ValidatorIndex,
8787
Address: gointerfaces.ConvertH160toAddress(withdrawal.Address),
88-
Amount: *gointerfaces.ConvertH256ToUint256Int(withdrawal.Amount),
88+
Amount: withdrawal.Amount,
8989
})
9090
}
9191

consensus/serenity/serenity.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"math/big"
88

9+
"github.com/holiman/uint256"
910
"github.com/ledgerwatch/erigon-lib/chain"
1011
libcommon "github.com/ledgerwatch/erigon-lib/common"
1112

@@ -136,7 +137,8 @@ func (s *Serenity) Finalize(config *chain.Config, header *types.Header, state *s
136137
}
137138
}
138139
for _, w := range withdrawals {
139-
state.AddBalance(w.Address, &w.Amount)
140+
amountInWei := new(uint256.Int).Mul(uint256.NewInt(w.Amount), uint256.NewInt(params.GWei))
141+
state.AddBalance(w.Address, amountInWei)
140142
}
141143
return txs, r, nil
142144
}

core/rawdb/accessors_chain_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"math/big"
2525
"testing"
2626

27-
"github.com/holiman/uint256"
2827
libcommon "github.com/ledgerwatch/erigon-lib/common"
2928
"github.com/ledgerwatch/erigon-lib/kv/memdb"
3029
"github.com/stretchr/testify/require"
@@ -446,14 +445,14 @@ func TestBlockWithdrawalsStorage(t *testing.T) {
446445
Index: uint64(15),
447446
Validator: uint64(5500),
448447
Address: libcommon.Address{0: 0xff},
449-
Amount: *uint256.NewInt(1000),
448+
Amount: 1000,
450449
}
451450

452451
w2 := types.Withdrawal{
453452
Index: uint64(16),
454453
Validator: uint64(5501),
455454
Address: libcommon.Address{0: 0xff},
456-
Amount: *uint256.NewInt(1001),
455+
Amount: 1001,
457456
}
458457

459458
withdrawals := make([]*types.Withdrawal, 0)
@@ -523,13 +522,13 @@ func TestBlockWithdrawalsStorage(t *testing.T) {
523522
require.Equal(uint64(15), rw.Index)
524523
require.Equal(uint64(5500), rw.Validator)
525524
require.Equal(libcommon.Address{0: 0xff}, rw.Address)
526-
require.Equal(*uint256.NewInt(1000), rw.Amount)
525+
require.Equal(uint64(1000), rw.Amount)
527526

528527
require.NotNil(rw2)
529528
require.Equal(uint64(16), rw2.Index)
530529
require.Equal(uint64(5501), rw2.Validator)
531530
require.Equal(libcommon.Address{0: 0xff}, rw2.Address)
532-
require.Equal(*uint256.NewInt(1001), rw2.Amount)
531+
require.Equal(uint64(1001), rw2.Amount)
533532

534533
// Delete the block and verify the execution
535534
if err := TruncateBlocks(context.Background(), tx, block.NumberU64()); err != nil {

core/types/block_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,13 @@ func TestWithdrawalsEncoding(t *testing.T) {
408408
Index: 44555666,
409409
Validator: 89,
410410
Address: libcommon.HexToAddress("0x690b9a9e9aa1c9db991c7721a92d351db4fac990"),
411-
Amount: *uint256.NewInt(2 * params.Ether),
411+
Amount: 2,
412412
}
413413
withdrawals[1] = &Withdrawal{
414414
Index: 44555667,
415415
Validator: 37,
416416
Address: libcommon.HexToAddress("0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5"),
417-
Amount: *uint256.NewInt(5 * params.Ether),
417+
Amount: 5_000_000_000,
418418
}
419419

420420
block := NewBlock(&header, nil, nil, nil, withdrawals)

core/types/gen_withdrawal_json.go

+8-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/withdrawal.go

+8-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"io"
2323

24-
"github.com/holiman/uint256"
2524
libcommon "github.com/ledgerwatch/erigon-lib/common"
2625
"github.com/ledgerwatch/erigon-lib/common/length"
2726

@@ -39,7 +38,7 @@ type Withdrawal struct {
3938
Index uint64 `json:"index"` // monotonically increasing identifier issued by consensus layer
4039
Validator uint64 `json:"validatorIndex"` // index of validator associated with withdrawal
4140
Address libcommon.Address `json:"address"` // target address for withdrawn ether
42-
Amount uint256.Int `json:"amount"` // value of withdrawal in wei
41+
Amount uint64 `json:"amount"` // value of withdrawal in GWei
4342
}
4443

4544
func (obj *Withdrawal) EncodingSize() int {
@@ -49,7 +48,7 @@ func (obj *Withdrawal) EncodingSize() int {
4948
encodingSize++
5049
encodingSize += rlp.IntLenExcludingHead(obj.Validator)
5150
encodingSize++
52-
encodingSize += rlp.Uint256LenExcludingHead(&obj.Amount)
51+
encodingSize += rlp.IntLenExcludingHead(obj.Amount)
5352
return encodingSize
5453
}
5554

@@ -76,16 +75,15 @@ func (obj *Withdrawal) EncodeRLP(w io.Writer) error {
7675
return err
7776
}
7877

79-
return obj.Amount.EncodeRLP(w)
78+
return rlp.EncodeInt(obj.Amount, w, b[:])
8079
}
8180

8281
func (obj *Withdrawal) EncodeSSZ() []byte {
8382
buf := make([]byte, obj.EncodingSizeSSZ())
8483
ssz_utils.MarshalUint64SSZ(buf, obj.Index)
8584
ssz_utils.MarshalUint64SSZ(buf[8:], obj.Validator)
8685
copy(buf[16:], obj.Address[:])
87-
// Supports only GWEI format.
88-
ssz_utils.MarshalUint64SSZ(buf[36:], obj.Amount.Uint64())
86+
ssz_utils.MarshalUint64SSZ(buf[36:], obj.Amount)
8987
return buf
9088
}
9189

@@ -96,7 +94,7 @@ func (obj *Withdrawal) DecodeSSZ(buf []byte) error {
9694
obj.Index = ssz_utils.UnmarshalUint64SSZ(buf)
9795
obj.Validator = ssz_utils.UnmarshalUint64SSZ(buf[8:])
9896
copy(obj.Address[:], buf[16:])
99-
obj.Amount = *uint256.NewInt(ssz_utils.UnmarshalUint64SSZ(buf[36:]))
97+
obj.Amount = ssz_utils.UnmarshalUint64SSZ(buf[36:])
10098
return nil
10199
}
102100

@@ -112,7 +110,7 @@ func (obj *Withdrawal) HashSSZ() ([32]byte, error) { // the [32]byte is temporar
112110
merkle_tree.Uint64Root(obj.Index),
113111
merkle_tree.Uint64Root(obj.Validator),
114112
addressLeaf,
115-
merkle_tree.Uint64Root(obj.Amount.Uint64()),
113+
merkle_tree.Uint64Root(obj.Amount),
116114
}, 4)
117115
}
118116

@@ -138,10 +136,9 @@ func (obj *Withdrawal) DecodeRLP(s *rlp.Stream) error {
138136
}
139137
copy(obj.Address[:], b)
140138

141-
if b, err = s.Uint256Bytes(); err != nil {
139+
if obj.Amount, err = s.Uint(); err != nil {
142140
return fmt.Errorf("read Amount: %w", err)
143141
}
144-
obj.Amount.SetBytes(b)
145142

146143
return s.ListEnd()
147144
}
@@ -150,7 +147,7 @@ func (obj *Withdrawal) DecodeRLP(s *rlp.Stream) error {
150147
type withdrawalMarshaling struct {
151148
Index hexutil.Uint64
152149
Validator hexutil.Uint64
153-
Amount *hexutil.Big
150+
Amount hexutil.Uint64
154151
}
155152

156153
// Withdrawals implements DerivableList for withdrawals.

core/types/withdrawal_test.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,19 @@ package types
33
import (
44
"testing"
55

6-
"github.com/holiman/uint256"
76
libcommon "github.com/ledgerwatch/erigon-lib/common"
87
"github.com/stretchr/testify/assert"
98
"github.com/stretchr/testify/require"
109

1110
"github.com/ledgerwatch/erigon/common"
12-
"github.com/ledgerwatch/erigon/common/u256"
1311
)
1412

1513
func TestWithdrawalsHash(t *testing.T) {
1614
w := &Withdrawal{
1715
Index: 0,
1816
Validator: 0,
1917
Address: libcommon.HexToAddress("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
20-
Amount: *u256.Num1,
18+
Amount: 1,
2119
}
2220
withdrawals := Withdrawals([]*Withdrawal{w})
2321
hash := DeriveSha(withdrawals)
@@ -33,7 +31,7 @@ var testWithdrawal = &Withdrawal{
3331
Index: 9170781944418253065,
3432
Validator: 16033042974434771745,
3533
Address: libcommon.HexToAddress("0xdbbcbcbeee17b2395d5d3f839fc1ba3559d1a73e"),
36-
Amount: *uint256.NewInt(15157676145812061173),
34+
Amount: 15157676145812061173,
3735
}
3836

3937
func TestWithdrawalSSZ(t *testing.T) {

ethdb/privateapi/ethbackend.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ func ConvertWithdrawalsFromRpc(in []*types2.Withdrawal) []*types.Withdrawal {
724724
Index: w.Index,
725725
Validator: w.ValidatorIndex,
726726
Address: gointerfaces.ConvertH160toAddress(w.Address),
727-
Amount: *gointerfaces.ConvertH256ToUint256Int(w.Amount),
727+
Amount: w.Amount,
728728
})
729729
}
730730
return out
@@ -737,7 +737,7 @@ func ConvertWithdrawalsToRpc(in []*types.Withdrawal) []*types2.Withdrawal {
737737
Index: w.Index,
738738
ValidatorIndex: w.Validator,
739739
Address: gointerfaces.ConvertAddressToH160(w.Address),
740-
Amount: gointerfaces.ConvertUint256IntToH256(&w.Amount),
740+
Amount: w.Amount,
741741
})
742742
}
743743
return out

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon
33
go 1.18
44

55
require (
6-
github.com/ledgerwatch/erigon-lib v0.0.0-20230117064926-4d298d0017b0
6+
github.com/ledgerwatch/erigon-lib v0.0.0-20230117095843-fc3dd4fd2789
77
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230117072003-4cc5ebded386
88
github.com/ledgerwatch/log/v3 v3.7.0
99
github.com/ledgerwatch/secp256k1 v1.0.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
565565
github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0=
566566
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
567567
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
568-
github.com/ledgerwatch/erigon-lib v0.0.0-20230117064926-4d298d0017b0 h1:eNvCZ1rQO//pQRoNW7VqwEO3YiP3TW8mcam7k4fTQfo=
569-
github.com/ledgerwatch/erigon-lib v0.0.0-20230117064926-4d298d0017b0/go.mod h1:tsfqxwRd5LYjXQyrC085+61iHp6Vwi1nOhxjro3w0Wo=
568+
github.com/ledgerwatch/erigon-lib v0.0.0-20230117095843-fc3dd4fd2789 h1:y/XlIJal2I99Eu/zQj6aM3oynxmK0Xc89nUG/WCMVsY=
569+
github.com/ledgerwatch/erigon-lib v0.0.0-20230117095843-fc3dd4fd2789/go.mod h1:1UHFnZQCpr37W397IJf68OxYv3iQmBTU9D7t3LUHbPo=
570570
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230117072003-4cc5ebded386 h1:dpTtuW3uRhwbS81yWRX1arSLDyCDFfe8MWGhXx5lGas=
571571
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230117072003-4cc5ebded386/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
572572
github.com/ledgerwatch/log/v3 v3.7.0 h1:aFPEZdwZx4jzA3+/Pf8wNDN5tCI0cIolq/kfvgcM+og=

tests/block_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ func TestBlockchain(t *testing.T) {
3838
// For speedier CI-runs those are skipped.
3939
bt.skipLoad(`^GeneralStateTests/`)
4040

41+
// TODO(yperbasis): re-enable after the tests are updated to GWei
42+
bt.skipLoad(`^EIPTests/bc4895-withdrawals/`)
43+
bt.skipLoad(`^TransitionTests/bcMergeToShanghai/`)
44+
4145
// Currently it fails because SpawnStageHeaders doesn't accept any PoW blocks after PoS transition
4246
// TODO(yperbasis): make it work
4347
bt.skipLoad(`^TransitionTests/bcArrowGlacierToMerge/powToPosBlockRejection\.json`)

tests/exec_spec_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func TestExecutionSpec(t *testing.T) {
1515

1616
dir := filepath.Join(".", "execution-spec-tests")
1717

18-
// Failing because the fixture was filled by geth w/o EIP-3860
19-
bt.skipLoad(`^withdrawals/withdrawals/withdrawals_newly_created_contract.json`)
18+
// TODO(yperbasis): re-fill and re-enable after Wei -> Gwei in geth
19+
bt.skipLoad(`^withdrawals/withdrawals`)
2020

2121
bt.walk(t, dir, func(t *testing.T, name string, test *BlockTest) {
2222
// import pre accounts & construct test genesis block & state root

0 commit comments

Comments
 (0)