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

Change withdrawal amount unmarshal to uint64 (gwei) #11866

Merged
merged 7 commits into from
Jan 16, 2023
1 change: 1 addition & 0 deletions beacon-chain/rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 7 additions & 14 deletions proto/engine/v1/json_marshal_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
})
}

Expand All @@ -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
}

Expand Down
12 changes: 7 additions & 5 deletions proto/engine/v1/json_marshal_unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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) {
Expand Down Expand Up @@ -353,6 +353,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")))
Expand All @@ -362,13 +364,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,
},
}

Expand Down Expand Up @@ -400,11 +402,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)
})
}

Expand Down