Change withdrawal amount unmarshal to uint64 (gwei) (#11866)

* Change withdrawal amount unmarshal to uint64 (gwei)

* Init server

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
This commit is contained in:
terencechain
2023-01-16 13:25:16 -08:00
committed by GitHub
parent 38f0a81526
commit 5480d607ac
3 changed files with 15 additions and 19 deletions

View File

@@ -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,

View File

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

View File

@@ -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) {
@@ -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")))
@@ -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,
},
}
@@ -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)
})
}