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

eth2util: new deposit message with amount #2822

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/addvalidators.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func writeDepositDatas(ctx context.Context, clusterDir string, numOps int, secre

var depositDatas []eth2p0.DepositData
for i, val := range vals {
depositMsg, err := deposit.NewMessage(eth2p0.BLSPubKey(val.PublicKey), val.WithdrawalAddress)
depositMsg, err := deposit.NewMessage(eth2p0.BLSPubKey(val.PublicKey), val.WithdrawalAddress, deposit.MaxValidatorAmount)
if err != nil {
return errors.Wrap(err, "new deposit message")
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/createcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func signDepositDatas(secrets []tbls.PrivateKey, withdrawalAddresses []string, n
return nil, errors.Wrap(err, "secret to pubkey")
}

msg, err := deposit.NewMessage(eth2p0.BLSPubKey(pk), withdrawalAddr)
msg, err := deposit.NewMessage(eth2p0.BLSPubKey(pk), withdrawalAddr, deposit.MaxValidatorAmount)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion dkg/dkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ func signDepositMsgs(shares []share, shareIdx int, withdrawalAddresses []string,
return nil, nil, err
}

msg, err := deposit.NewMessage(pubkey, withdrawalHex)
msg, err := deposit.NewMessage(pubkey, withdrawalHex, deposit.MaxValidatorAmount)
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion dkg/dkg_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestValidSignatures(t *testing.T) {
withdrawalAddr := testutil.RandomETHAddress()
network := eth2util.Goerli.Name

msg, err := deposit.NewMessage(eth2Pubkey, withdrawalAddr)
msg, err := deposit.NewMessage(eth2Pubkey, withdrawalAddr, deposit.MaxValidatorAmount)
require.NoError(t, err)
sigRoot, err := deposit.GetMessageSigningRoot(msg, network)
require.NoError(t, err)
Expand Down
23 changes: 17 additions & 6 deletions eth2util/deposit/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import (
)

var (
// Minimum allowed deposit amount (1ETH).
MinValidatorAmount = eth2p0.Gwei(1000000000)

// Maximum allowed deposit amount (32ETH).
MaxValidatorAmount = eth2p0.Gwei(32000000000)

Comment on lines +22 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to update here after merging #2820

// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/validator.md#eth1_address_withdrawal_prefix
eth1AddressWithdrawalPrefix = []byte{0x01}

// the amount of ether in gwei required to activate a validator.
validatorAmt = eth2p0.Gwei(32000000000)

// DOMAIN_DEPOSIT. See spec: https://benjaminion.xyz/eth2-annotated-spec/phase0/beacon-chain/#domain-types
depositDomainType = eth2p0.DomainType([4]byte{0x03, 0x00, 0x00, 0x00})

Expand All @@ -33,16 +36,24 @@ var (
)

// NewMessage returns a deposit message created using the provided parameters.
func NewMessage(pubkey eth2p0.BLSPubKey, withdrawalAddr string) (eth2p0.DepositMessage, error) {
func NewMessage(pubkey eth2p0.BLSPubKey, withdrawalAddr string, amount eth2p0.Gwei) (eth2p0.DepositMessage, error) {
creds, err := withdrawalCredsFromAddr(withdrawalAddr)
if err != nil {
return eth2p0.DepositMessage{}, err
}

if amount < MinValidatorAmount {
return eth2p0.DepositMessage{}, errors.New("deposit message minimum amount must be >= 1ETH", z.U64("amount", uint64(amount)))
}

if amount > MaxValidatorAmount {
return eth2p0.DepositMessage{}, errors.New("deposit message maximum amount must <= 32ETH", z.U64("amount", uint64(amount)))
}

return eth2p0.DepositMessage{
PublicKey: pubkey,
WithdrawalCredentials: creds[:],
Amount: validatorAmt,
Amount: amount,
}, nil
}

Expand Down Expand Up @@ -87,7 +98,7 @@ func MarshalDepositData(depositDatas []eth2p0.DepositData, network string) ([]by
ddList = append(ddList, depositDataJSON{
PubKey: fmt.Sprintf("%x", depositData.PublicKey),
WithdrawalCredentials: fmt.Sprintf("%x", depositData.WithdrawalCredentials),
Amount: uint64(validatorAmt),
Amount: uint64(depositData.Amount),
Signature: fmt.Sprintf("%x", depositData.Signature),
DepositMessageRoot: fmt.Sprintf("%x", msgRoot),
DepositDataRoot: fmt.Sprintf("%x", dataRoot),
Expand Down
27 changes: 26 additions & 1 deletion eth2util/deposit/deposit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ import (

//go:generate go test . -run=TestMarshalDepositData -update -clean

func TestNewMessage(t *testing.T) {
const privKey = "01477d4bfbbcebe1fef8d4d6f624ecbb6e3178558bb1b0d6286c816c66842a6d"
const addr = "0x321dcb529f3945bc94fecea9d3bc5caf35253b94"
amount := deposit.MaxValidatorAmount
_, pubKey := GetKeys(t, privKey)

msg, err := deposit.NewMessage(pubKey, addr, amount)

require.NoError(t, err)
require.Equal(t, pubKey, msg.PublicKey)
require.Equal(t, amount, msg.Amount)

t.Run("amount below minimum", func(t *testing.T) {
_, err := deposit.NewMessage(pubKey, addr, deposit.MinValidatorAmount-1)

require.ErrorContains(t, err, "deposit message minimum amount must be >= 1ETH")
})

t.Run("amount above maximum", func(t *testing.T) {
_, err := deposit.NewMessage(pubKey, addr, deposit.MaxValidatorAmount+1)

require.ErrorContains(t, err, "deposit message maximum amount must <= 32ETH")
})
}

func TestMarshalDepositData(t *testing.T) {
privKeys := []string{
"01477d4bfbbcebe1fef8d4d6f624ecbb6e3178558bb1b0d6286c816c66842a6d",
Expand All @@ -39,7 +64,7 @@ func TestMarshalDepositData(t *testing.T) {
for i := 0; i < len(privKeys); i++ {
sk, pk := GetKeys(t, privKeys[i])

msg, err := deposit.NewMessage(pk, withdrawalAddrs[i])
msg, err := deposit.NewMessage(pk, withdrawalAddrs[i], deposit.MaxValidatorAmount)
require.NoError(t, err)

sigRoot, err := deposit.GetMessageSigningRoot(msg, network)
Expand Down
Loading