Skip to content

Commit

Permalink
Withdrawal amount in GWei (#6578)
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis authored Jan 17, 2023
1 parent 9e452fe commit f151a52
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cmd/erigon-el/eth1/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (e *Eth1Execution) InsertBodies(ctx context.Context, req *execution.InsertB
Index: withdrawal.Index,
Validator: withdrawal.ValidatorIndex,
Address: gointerfaces.ConvertH160toAddress(withdrawal.Address),
Amount: *gointerfaces.ConvertH256ToUint256Int(withdrawal.Amount),
Amount: withdrawal.Amount,
})
}

Expand Down
4 changes: 3 additions & 1 deletion consensus/serenity/serenity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/big"

"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"

Expand Down Expand Up @@ -136,7 +137,8 @@ func (s *Serenity) Finalize(config *chain.Config, header *types.Header, state *s
}
}
for _, w := range withdrawals {
state.AddBalance(w.Address, &w.Amount)
amountInWei := new(uint256.Int).Mul(uint256.NewInt(w.Amount), uint256.NewInt(params.GWei))
state.AddBalance(w.Address, amountInWei)
}
return txs, r, nil
}
Expand Down
9 changes: 4 additions & 5 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"math/big"
"testing"

"github.com/holiman/uint256"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -446,14 +445,14 @@ func TestBlockWithdrawalsStorage(t *testing.T) {
Index: uint64(15),
Validator: uint64(5500),
Address: libcommon.Address{0: 0xff},
Amount: *uint256.NewInt(1000),
Amount: 1000,
}

w2 := types.Withdrawal{
Index: uint64(16),
Validator: uint64(5501),
Address: libcommon.Address{0: 0xff},
Amount: *uint256.NewInt(1001),
Amount: 1001,
}

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

require.NotNil(rw2)
require.Equal(uint64(16), rw2.Index)
require.Equal(uint64(5501), rw2.Validator)
require.Equal(libcommon.Address{0: 0xff}, rw2.Address)
require.Equal(*uint256.NewInt(1001), rw2.Amount)
require.Equal(uint64(1001), rw2.Amount)

// Delete the block and verify the execution
if err := TruncateBlocks(context.Background(), tx, block.NumberU64()); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,13 @@ func TestWithdrawalsEncoding(t *testing.T) {
Index: 44555666,
Validator: 89,
Address: libcommon.HexToAddress("0x690b9a9e9aa1c9db991c7721a92d351db4fac990"),
Amount: *uint256.NewInt(2 * params.Ether),
Amount: 2,
}
withdrawals[1] = &Withdrawal{
Index: 44555667,
Validator: 37,
Address: libcommon.HexToAddress("0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5"),
Amount: *uint256.NewInt(5 * params.Ether),
Amount: 5_000_000_000,
}

block := NewBlock(&header, nil, nil, nil, withdrawals)
Expand Down
17 changes: 8 additions & 9 deletions core/types/gen_withdrawal_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions core/types/withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"

"github.com/holiman/uint256"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"

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

func (obj *Withdrawal) EncodingSize() int {
Expand All @@ -49,7 +48,7 @@ func (obj *Withdrawal) EncodingSize() int {
encodingSize++
encodingSize += rlp.IntLenExcludingHead(obj.Validator)
encodingSize++
encodingSize += rlp.Uint256LenExcludingHead(&obj.Amount)
encodingSize += rlp.IntLenExcludingHead(obj.Amount)
return encodingSize
}

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

return obj.Amount.EncodeRLP(w)
return rlp.EncodeInt(obj.Amount, w, b[:])
}

func (obj *Withdrawal) EncodeSSZ() []byte {
buf := make([]byte, obj.EncodingSizeSSZ())
ssz_utils.MarshalUint64SSZ(buf, obj.Index)
ssz_utils.MarshalUint64SSZ(buf[8:], obj.Validator)
copy(buf[16:], obj.Address[:])
// Supports only GWEI format.
ssz_utils.MarshalUint64SSZ(buf[36:], obj.Amount.Uint64())
ssz_utils.MarshalUint64SSZ(buf[36:], obj.Amount)
return buf
}

Expand All @@ -96,7 +94,7 @@ func (obj *Withdrawal) DecodeSSZ(buf []byte) error {
obj.Index = ssz_utils.UnmarshalUint64SSZ(buf)
obj.Validator = ssz_utils.UnmarshalUint64SSZ(buf[8:])
copy(obj.Address[:], buf[16:])
obj.Amount = *uint256.NewInt(ssz_utils.UnmarshalUint64SSZ(buf[36:]))
obj.Amount = ssz_utils.UnmarshalUint64SSZ(buf[36:])
return nil
}

Expand All @@ -112,7 +110,7 @@ func (obj *Withdrawal) HashSSZ() ([32]byte, error) { // the [32]byte is temporar
merkle_tree.Uint64Root(obj.Index),
merkle_tree.Uint64Root(obj.Validator),
addressLeaf,
merkle_tree.Uint64Root(obj.Amount.Uint64()),
merkle_tree.Uint64Root(obj.Amount),
}, 4)
}

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

if b, err = s.Uint256Bytes(); err != nil {
if obj.Amount, err = s.Uint(); err != nil {
return fmt.Errorf("read Amount: %w", err)
}
obj.Amount.SetBytes(b)

return s.ListEnd()
}
Expand All @@ -150,7 +147,7 @@ func (obj *Withdrawal) DecodeRLP(s *rlp.Stream) error {
type withdrawalMarshaling struct {
Index hexutil.Uint64
Validator hexutil.Uint64
Amount *hexutil.Big
Amount hexutil.Uint64
}

// Withdrawals implements DerivableList for withdrawals.
Expand Down
6 changes: 2 additions & 4 deletions core/types/withdrawal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ package types
import (
"testing"

"github.com/holiman/uint256"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/u256"
)

func TestWithdrawalsHash(t *testing.T) {
w := &Withdrawal{
Index: 0,
Validator: 0,
Address: libcommon.HexToAddress("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
Amount: *u256.Num1,
Amount: 1,
}
withdrawals := Withdrawals([]*Withdrawal{w})
hash := DeriveSha(withdrawals)
Expand All @@ -33,7 +31,7 @@ var testWithdrawal = &Withdrawal{
Index: 9170781944418253065,
Validator: 16033042974434771745,
Address: libcommon.HexToAddress("0xdbbcbcbeee17b2395d5d3f839fc1ba3559d1a73e"),
Amount: *uint256.NewInt(15157676145812061173),
Amount: 15157676145812061173,
}

func TestWithdrawalSSZ(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions ethdb/privateapi/ethbackend.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ func ConvertWithdrawalsFromRpc(in []*types2.Withdrawal) []*types.Withdrawal {
Index: w.Index,
Validator: w.ValidatorIndex,
Address: gointerfaces.ConvertH160toAddress(w.Address),
Amount: *gointerfaces.ConvertH256ToUint256Int(w.Amount),
Amount: w.Amount,
})
}
return out
Expand All @@ -737,7 +737,7 @@ func ConvertWithdrawalsToRpc(in []*types.Withdrawal) []*types2.Withdrawal {
Index: w.Index,
ValidatorIndex: w.Validator,
Address: gointerfaces.ConvertAddressToH160(w.Address),
Amount: gointerfaces.ConvertUint256IntToH256(&w.Amount),
Amount: w.Amount,
})
}
return out
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ledgerwatch/erigon
go 1.18

require (
github.com/ledgerwatch/erigon-lib v0.0.0-20230117064926-4d298d0017b0
github.com/ledgerwatch/erigon-lib v0.0.0-20230117095843-fc3dd4fd2789
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230117072003-4cc5ebded386
github.com/ledgerwatch/log/v3 v3.7.0
github.com/ledgerwatch/secp256k1 v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/godebug v0.0.0-20170224010052-a616ab194758 h1:0D5M2HQSGD3PYPwICLl+/9oulQauOuETfgFvhBDffs0=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/ledgerwatch/erigon-lib v0.0.0-20230117064926-4d298d0017b0 h1:eNvCZ1rQO//pQRoNW7VqwEO3YiP3TW8mcam7k4fTQfo=
github.com/ledgerwatch/erigon-lib v0.0.0-20230117064926-4d298d0017b0/go.mod h1:tsfqxwRd5LYjXQyrC085+61iHp6Vwi1nOhxjro3w0Wo=
github.com/ledgerwatch/erigon-lib v0.0.0-20230117095843-fc3dd4fd2789 h1:y/XlIJal2I99Eu/zQj6aM3oynxmK0Xc89nUG/WCMVsY=
github.com/ledgerwatch/erigon-lib v0.0.0-20230117095843-fc3dd4fd2789/go.mod h1:1UHFnZQCpr37W397IJf68OxYv3iQmBTU9D7t3LUHbPo=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230117072003-4cc5ebded386 h1:dpTtuW3uRhwbS81yWRX1arSLDyCDFfe8MWGhXx5lGas=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230117072003-4cc5ebded386/go.mod h1:3AuPxZc85jkehh/HA9h8gabv5MSi3kb/ddtzBsTVJFo=
github.com/ledgerwatch/log/v3 v3.7.0 h1:aFPEZdwZx4jzA3+/Pf8wNDN5tCI0cIolq/kfvgcM+og=
Expand Down
4 changes: 4 additions & 0 deletions tests/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func TestBlockchain(t *testing.T) {
// For speedier CI-runs those are skipped.
bt.skipLoad(`^GeneralStateTests/`)

// TODO(yperbasis): re-enable after the tests are updated to GWei
bt.skipLoad(`^EIPTests/bc4895-withdrawals/`)
bt.skipLoad(`^TransitionTests/bcMergeToShanghai/`)

// Currently it fails because SpawnStageHeaders doesn't accept any PoW blocks after PoS transition
// TODO(yperbasis): make it work
bt.skipLoad(`^TransitionTests/bcArrowGlacierToMerge/powToPosBlockRejection\.json`)
Expand Down
4 changes: 2 additions & 2 deletions tests/exec_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestExecutionSpec(t *testing.T) {

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

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

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

0 comments on commit f151a52

Please sign in to comment.