Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Withdrawal amount in GWei #6578

Merged
merged 7 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/erigon-el/eth1/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,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
5 changes: 4 additions & 1 deletion consensus/serenity/serenity.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"math/big"

"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/consensus/aura"
Expand Down Expand Up @@ -133,7 +135,8 @@ func (s *Serenity) Finalize(config *params.ChainConfig, header *types.Header, st
}
}
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
14 changes: 7 additions & 7 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import (
"math/big"
"testing"

"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/u256"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rlp"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
)

// Tests block header storage and retrieval operations.
Expand Down Expand Up @@ -445,14 +445,14 @@ func TestBlockWithdrawalsStorage(t *testing.T) {
Index: uint64(15),
Validator: uint64(5500),
Address: common.Address{0: 0xff},
Amount: *uint256.NewInt(1000),
Amount: 1000,
}

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

withdrawals := make([]*types.Withdrawal, 0)
Expand Down Expand Up @@ -522,13 +522,13 @@ func TestBlockWithdrawalsStorage(t *testing.T) {
require.Equal(uint64(15), rw.Index)
require.Equal(uint64(5500), rw.Validator)
require.Equal(common.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(common.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 @@ -407,13 +407,13 @@ func TestWithdrawalsEncoding(t *testing.T) {
Index: 44555666,
Validator: 89,
Address: common.HexToAddress("0x690b9a9e9aa1c9db991c7721a92d351db4fac990"),
Amount: *uint256.NewInt(2 * params.Ether),
Amount: 2,
}
withdrawals[1] = &Withdrawal{
Index: 44555667,
Validator: 37,
Address: common.HexToAddress("0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5"),
Amount: *uint256.NewInt(5 * params.Ether),
Amount: 5_000_000_000,
}

block := NewBlock(&header, nil, nil, nil, withdrawals)
Expand Down
10 changes: 4 additions & 6 deletions core/types/gen_withdrawal_json.go

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

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

"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon/cl/cltypes/ssz_utils"
"github.com/ledgerwatch/erigon/cl/merkle_tree"
"github.com/ledgerwatch/erigon/common"
Expand All @@ -38,7 +36,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 common.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 @@ -48,7 +46,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 @@ -75,16 +73,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 @@ -95,7 +92,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 @@ -111,7 +108,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 @@ -137,10 +134,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 @@ -149,7 +145,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,20 +3,18 @@ package types
import (
"testing"

"github.com/holiman/uint256"
"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: common.HexToAddress("0x6295ee1b4f6dd65047762f924ecd367c17eabf8f"),
Amount: *u256.Num1,
Amount: 1,
}
withdrawals := Withdrawals([]*Withdrawal{w})
hash := DeriveSha(withdrawals)
Expand All @@ -32,7 +30,7 @@ var testWithdrawal = &Withdrawal{
Index: 9170781944418253065,
Validator: 16033042974434771745,
Address: common.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 @@ -722,7 +722,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 @@ -735,7 +735,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-20230113043530-7c475cb52ecc
github.com/ledgerwatch/erigon-lib v0.0.0-20230113122151-7f8731e00b6f
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230106211435-2670b273bb55
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-20230113043530-7c475cb52ecc h1:If4hgp4pZKLARqvu6XZIW6tPJxLk13rbf1WPF06My1s=
github.com/ledgerwatch/erigon-lib v0.0.0-20230113043530-7c475cb52ecc/go.mod h1:vFTw08qI/7NHL3z4/vBlIXRylFgZlMbnTehf7WDbcJQ=
github.com/ledgerwatch/erigon-lib v0.0.0-20230113122151-7f8731e00b6f h1:3SFwHCM+iix5CHJGcxhki9zFQ+5y8FpSs8zUGcLA8uE=
github.com/ledgerwatch/erigon-lib v0.0.0-20230113122151-7f8731e00b6f/go.mod h1:hwA9j60yYOSATt3NUVHsBM4cZZq3nz8eMicujA/kKDo=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230106211435-2670b273bb55 h1:4gY6eRXQiX0Te1oKTpsxj6MjXksQSSTHWiSh/j89sEY=
github.com/ledgerwatch/erigon-snapshot v1.1.1-0.20230106211435-2670b273bb55/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
3 changes: 3 additions & 0 deletions tests/exec_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func TestExecutionSpec(t *testing.T) {

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

// TODO(yperbasis): re-fill and re-enable after Wei -> Gwei in geth
bt.skipLoad(`^withdrawals/withdrawals`)

// Failing because the fixture was filled by geth w/o EIP-3860
bt.skipLoad(`^withdrawals/withdrawals/withdrawals_newly_created_contract.json`)

Expand Down