mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-10 05:47:59 -05:00
Compare commits
9 Commits
devnet-3
...
withdrawal
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7f200e6b89 | ||
|
|
0f151d9b5a | ||
|
|
8ab0a0f978 | ||
|
|
f892c1a88f | ||
|
|
49e050c621 | ||
|
|
d0978c61f2 | ||
|
|
f08bef4b62 | ||
|
|
f44edd3d34 | ||
|
|
83c5dfcec4 |
@@ -223,6 +223,7 @@ func (s *Service) Start() {
|
||||
BeaconDB: s.cfg.BeaconDB,
|
||||
ProposerSlotIndexCache: s.cfg.ProposerIdsCache,
|
||||
BlockBuilder: s.cfg.BlockBuilder,
|
||||
BLSChangesPool: s.cfg.BLSChangesPool,
|
||||
}
|
||||
validatorServerV1 := &validator.Server{
|
||||
HeadFetcher: s.cfg.HeadFetcher,
|
||||
|
||||
@@ -144,7 +144,7 @@ type withdrawalJSON struct {
|
||||
Index *hexutil.Uint64 `json:"index"`
|
||||
Validator *hexutil.Uint64 `json:"validatorIndex"`
|
||||
Address *common.Address `json:"address"`
|
||||
Amount string `json:"amount"`
|
||||
Amount *hexutil.Uint64 `json:"amount"`
|
||||
}
|
||||
|
||||
func (j *withdrawalJSON) ToWithdrawal() (*Withdrawal, error) {
|
||||
@@ -162,14 +162,13 @@ func (j *withdrawalJSON) ToWithdrawal() (*Withdrawal, error) {
|
||||
func (w *Withdrawal) MarshalJSON() ([]byte, error) {
|
||||
index := hexutil.Uint64(w.Index)
|
||||
validatorIndex := hexutil.Uint64(w.ValidatorIndex)
|
||||
gwei := hexutil.Uint64(w.Amount)
|
||||
address := common.BytesToAddress(w.Address)
|
||||
wei := new(big.Int).SetUint64(1000000000)
|
||||
amountWei := new(big.Int).Mul(new(big.Int).SetUint64(w.Amount), wei)
|
||||
return json.Marshal(withdrawalJSON{
|
||||
Index: &index,
|
||||
Validator: &validatorIndex,
|
||||
Address: &address,
|
||||
Amount: hexutil.EncodeBig(amountWei),
|
||||
Amount: &gwei,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -184,23 +183,17 @@ func (w *Withdrawal) UnmarshalJSON(enc []byte) error {
|
||||
if dec.Validator == nil {
|
||||
return errors.New("missing validator index")
|
||||
}
|
||||
if dec.Amount == nil {
|
||||
return errors.New("missing withdrawal amount")
|
||||
}
|
||||
if dec.Address == nil {
|
||||
return errors.New("missing execution address")
|
||||
}
|
||||
*w = Withdrawal{}
|
||||
w.Index = uint64(*dec.Index)
|
||||
w.ValidatorIndex = types.ValidatorIndex(*dec.Validator)
|
||||
w.Amount = uint64(*dec.Amount)
|
||||
w.Address = dec.Address.Bytes()
|
||||
wei := new(big.Int).SetUint64(1000000000)
|
||||
amountWei, err := hexutil.DecodeBig(dec.Amount)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
amount := new(big.Int).Div(amountWei, wei)
|
||||
if !amount.IsUint64() {
|
||||
return errors.New("withdrawal amount overflow")
|
||||
}
|
||||
w.Amount = amount.Uint64()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -221,12 +214,12 @@ type executionPayloadJSON struct {
|
||||
Transactions []hexutil.Bytes `json:"transactions"`
|
||||
}
|
||||
|
||||
type getPayloadV2ResponseJson struct {
|
||||
ExecutionPayload *executionPayloadCapellaJSON `json:"executionPayload"`
|
||||
type GetPayloadV2ResponseJson struct {
|
||||
ExecutionPayload *ExecutionPayloadCapellaJSON `json:"executionPayload"`
|
||||
BlockValue string `json:"blockValue"`
|
||||
}
|
||||
|
||||
type executionPayloadCapellaJSON struct {
|
||||
type ExecutionPayloadCapellaJSON struct {
|
||||
ParentHash *common.Hash `json:"parentHash"`
|
||||
FeeRecipient *common.Address `json:"feeRecipient"`
|
||||
StateRoot *common.Hash `json:"stateRoot"`
|
||||
@@ -303,7 +296,7 @@ func (e *ExecutionPayloadCapella) MarshalJSON() ([]byte, error) {
|
||||
if e.Withdrawals == nil {
|
||||
e.Withdrawals = make([]*Withdrawal, 0)
|
||||
}
|
||||
return json.Marshal(executionPayloadCapellaJSON{
|
||||
return json.Marshal(ExecutionPayloadCapellaJSON{
|
||||
ParentHash: &pHash,
|
||||
FeeRecipient: &recipient,
|
||||
StateRoot: &sRoot,
|
||||
@@ -397,7 +390,7 @@ func (e *ExecutionPayload) UnmarshalJSON(enc []byte) error {
|
||||
|
||||
// UnmarshalJSON --
|
||||
func (e *ExecutionPayloadCapella) UnmarshalJSON(enc []byte) error {
|
||||
dec := getPayloadV2ResponseJson{}
|
||||
dec := GetPayloadV2ResponseJson{}
|
||||
if err := json.Unmarshal(enc, &dec); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package enginev1_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
@@ -21,7 +22,7 @@ type withdrawalJSON struct {
|
||||
Index *hexutil.Uint64 `json:"index"`
|
||||
Validator *hexutil.Uint64 `json:"validatorIndex"`
|
||||
Address *common.Address `json:"address"`
|
||||
Amount string `json:"amount"`
|
||||
Amount *hexutil.Uint64 `json:"amount"`
|
||||
}
|
||||
|
||||
func TestJsonMarshalUnmarshal(t *testing.T) {
|
||||
@@ -140,55 +141,63 @@ func TestJsonMarshalUnmarshal(t *testing.T) {
|
||||
require.DeepEqual(t, [][]byte{[]byte("hi")}, payloadPb.Transactions)
|
||||
})
|
||||
t.Run("execution payload Capella", func(t *testing.T) {
|
||||
baseFeePerGas := big.NewInt(1770307273)
|
||||
parentHash := bytesutil.PadTo([]byte("parent"), fieldparams.RootLength)
|
||||
feeRecipient := bytesutil.PadTo([]byte("feeRecipient"), fieldparams.FeeRecipientLength)
|
||||
stateRoot := bytesutil.PadTo([]byte("stateRoot"), fieldparams.RootLength)
|
||||
receiptsRoot := bytesutil.PadTo([]byte("receiptsRoot"), fieldparams.RootLength)
|
||||
logsBloom := bytesutil.PadTo([]byte("logs"), fieldparams.LogsBloomLength)
|
||||
random := bytesutil.PadTo([]byte("random"), fieldparams.RootLength)
|
||||
extra := bytesutil.PadTo([]byte("extraData"), fieldparams.RootLength)
|
||||
hash := bytesutil.PadTo([]byte("hash"), fieldparams.RootLength)
|
||||
jsonPayload := &enginev1.ExecutionPayloadCapella{
|
||||
ParentHash: parentHash,
|
||||
FeeRecipient: feeRecipient,
|
||||
StateRoot: stateRoot,
|
||||
ReceiptsRoot: receiptsRoot,
|
||||
LogsBloom: logsBloom,
|
||||
PrevRandao: random,
|
||||
BlockNumber: 1,
|
||||
GasLimit: 2,
|
||||
GasUsed: 3,
|
||||
Timestamp: 4,
|
||||
ExtraData: extra,
|
||||
BaseFeePerGas: baseFeePerGas.Bytes(),
|
||||
BlockHash: hash,
|
||||
Transactions: [][]byte{[]byte("hi")},
|
||||
Withdrawals: []*enginev1.Withdrawal{{
|
||||
Index: 1,
|
||||
ValidatorIndex: 1,
|
||||
Address: bytesutil.PadTo([]byte("address"), 20),
|
||||
Amount: 1,
|
||||
}},
|
||||
parentHash := common.BytesToHash([]byte("parent"))
|
||||
feeRecipient := common.BytesToAddress([]byte("feeRecipient"))
|
||||
stateRoot := common.BytesToHash([]byte("stateRoot"))
|
||||
receiptsRoot := common.BytesToHash([]byte("receiptsRoot"))
|
||||
logsBloom := hexutil.Bytes(bytesutil.PadTo([]byte("logs"), fieldparams.LogsBloomLength))
|
||||
random := common.BytesToHash([]byte("random"))
|
||||
extra := common.BytesToHash([]byte("extra"))
|
||||
hash := common.BytesToHash([]byte("hash"))
|
||||
bn := hexutil.Uint64(1)
|
||||
gl := hexutil.Uint64(2)
|
||||
gu := hexutil.Uint64(3)
|
||||
ts := hexutil.Uint64(4)
|
||||
|
||||
resp := &enginev1.GetPayloadV2ResponseJson{
|
||||
BlockValue: fmt.Sprint(123),
|
||||
ExecutionPayload: &enginev1.ExecutionPayloadCapellaJSON{
|
||||
ParentHash: &parentHash,
|
||||
FeeRecipient: &feeRecipient,
|
||||
StateRoot: &stateRoot,
|
||||
ReceiptsRoot: &receiptsRoot,
|
||||
LogsBloom: &logsBloom,
|
||||
PrevRandao: &random,
|
||||
BlockNumber: &bn,
|
||||
GasLimit: &gl,
|
||||
GasUsed: &gu,
|
||||
Timestamp: &ts,
|
||||
ExtraData: hexutil.Bytes(extra[:]),
|
||||
BaseFeePerGas: "0x123",
|
||||
BlockHash: &hash,
|
||||
Transactions: []hexutil.Bytes{{}},
|
||||
Withdrawals: []*enginev1.Withdrawal{{
|
||||
Index: 1,
|
||||
ValidatorIndex: 1,
|
||||
Address: bytesutil.PadTo([]byte("address"), 20),
|
||||
Amount: 1,
|
||||
}},
|
||||
},
|
||||
}
|
||||
enc, err := json.Marshal(jsonPayload)
|
||||
enc, err := json.Marshal(resp)
|
||||
require.NoError(t, err)
|
||||
payloadPb := &enginev1.ExecutionPayloadCapella{}
|
||||
require.NoError(t, json.Unmarshal(enc, payloadPb))
|
||||
require.DeepEqual(t, parentHash, payloadPb.ParentHash)
|
||||
require.DeepEqual(t, feeRecipient, payloadPb.FeeRecipient)
|
||||
require.DeepEqual(t, stateRoot, payloadPb.StateRoot)
|
||||
require.DeepEqual(t, receiptsRoot, payloadPb.ReceiptsRoot)
|
||||
require.DeepEqual(t, logsBloom, payloadPb.LogsBloom)
|
||||
require.DeepEqual(t, random, payloadPb.PrevRandao)
|
||||
require.DeepEqual(t, parentHash.Bytes(), payloadPb.ParentHash)
|
||||
require.DeepEqual(t, feeRecipient.Bytes(), payloadPb.FeeRecipient)
|
||||
require.DeepEqual(t, stateRoot.Bytes(), payloadPb.StateRoot)
|
||||
require.DeepEqual(t, receiptsRoot.Bytes(), payloadPb.ReceiptsRoot)
|
||||
require.DeepEqual(t, logsBloom, hexutil.Bytes(payloadPb.LogsBloom))
|
||||
require.DeepEqual(t, random.Bytes(), payloadPb.PrevRandao)
|
||||
require.DeepEqual(t, uint64(1), payloadPb.BlockNumber)
|
||||
require.DeepEqual(t, uint64(2), payloadPb.GasLimit)
|
||||
require.DeepEqual(t, uint64(3), payloadPb.GasUsed)
|
||||
require.DeepEqual(t, uint64(4), payloadPb.Timestamp)
|
||||
require.DeepEqual(t, extra, payloadPb.ExtraData)
|
||||
require.DeepEqual(t, bytesutil.PadTo(baseFeePerGas.Bytes(), fieldparams.RootLength), payloadPb.BaseFeePerGas)
|
||||
require.DeepEqual(t, hash, payloadPb.BlockHash)
|
||||
require.DeepEqual(t, [][]byte{[]byte("hi")}, payloadPb.Transactions)
|
||||
require.DeepEqual(t, extra.Bytes(), payloadPb.ExtraData)
|
||||
feePerGas := new(big.Int).SetBytes(payloadPb.BaseFeePerGas)
|
||||
require.Equal(t, "15832716547479101977395928904157292820330083199902421483727713169783165812736", feePerGas.String())
|
||||
require.DeepEqual(t, hash.Bytes(), payloadPb.BlockHash)
|
||||
require.DeepEqual(t, [][]byte{{}}, payloadPb.Transactions)
|
||||
require.Equal(t, 1, len(payloadPb.Withdrawals))
|
||||
withdrawal := payloadPb.Withdrawals[0]
|
||||
require.Equal(t, uint64(1), withdrawal.Index)
|
||||
@@ -410,6 +419,8 @@ func TestJsonMarshalUnmarshal(t *testing.T) {
|
||||
|
||||
withdrawalIndex1 := hexutil.Uint64(1)
|
||||
withdrawalIndex2 := hexutil.Uint64(2)
|
||||
withdrawalAmount1 := hexutil.Uint64(100)
|
||||
withdrawalAmount2 := hexutil.Uint64(200)
|
||||
withdrawalValidator1 := hexutil.Uint64(1)
|
||||
withdrawalValidator2 := hexutil.Uint64(2)
|
||||
address1 := common.Address(bytesutil.ToBytes20([]byte("address1")))
|
||||
@@ -419,13 +430,13 @@ func TestJsonMarshalUnmarshal(t *testing.T) {
|
||||
Index: &withdrawalIndex1,
|
||||
Validator: &withdrawalValidator1,
|
||||
Address: &address1,
|
||||
Amount: "0x3b9aca00",
|
||||
Amount: &withdrawalAmount1,
|
||||
},
|
||||
{
|
||||
Index: &withdrawalIndex2,
|
||||
Validator: &withdrawalValidator2,
|
||||
Address: &address2,
|
||||
Amount: "0x77359400",
|
||||
Amount: &withdrawalAmount2,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -457,11 +468,11 @@ func TestJsonMarshalUnmarshal(t *testing.T) {
|
||||
require.Equal(t, uint64(1), payloadPb.Withdrawals[0].Index)
|
||||
require.Equal(t, types.ValidatorIndex(1), payloadPb.Withdrawals[0].ValidatorIndex)
|
||||
require.DeepEqual(t, bytesutil.PadTo([]byte("address1"), 20), payloadPb.Withdrawals[0].Address)
|
||||
require.Equal(t, uint64(1), payloadPb.Withdrawals[0].Amount)
|
||||
require.Equal(t, uint64(100), payloadPb.Withdrawals[0].Amount)
|
||||
require.Equal(t, uint64(2), payloadPb.Withdrawals[1].Index)
|
||||
require.Equal(t, types.ValidatorIndex(2), payloadPb.Withdrawals[1].ValidatorIndex)
|
||||
require.DeepEqual(t, bytesutil.PadTo([]byte("address2"), 20), payloadPb.Withdrawals[1].Address)
|
||||
require.Equal(t, uint64(2), payloadPb.Withdrawals[1].Amount)
|
||||
require.Equal(t, uint64(200), payloadPb.Withdrawals[1].Amount)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user