Skip to content

Commit

Permalink
update init message accordning to EIP-7251: Increase the MAX_EFFECTIV…
Browse files Browse the repository at this point in the history
…E_BALANCE
  • Loading branch information
pavelkrolevets committed Sep 24, 2024
1 parent 24147ae commit 0dda5b1
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 69 deletions.
6 changes: 4 additions & 2 deletions crypto/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
)

const (
MaxEffectiveBalanceInGwei phase0.Gwei = 32000000000
ETH1WithdrawalPrefixByte = byte(1)
// https://eips.ethereum.org/EIPS/eip-7251
MIN_ACTIVATION_BALANCE phase0.Gwei = 32000000000
MAX_EFFECTIVE_BALANCE phase0.Gwei = 2048000000000
ETH1WithdrawalPrefixByte = byte(1)
)

// GetNetworkByFork translates the network fork bytes into name
Expand Down
14 changes: 13 additions & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package spec
import (
"bytes"
"fmt"

"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ssvlabs/dkg-spec/crypto"
)

// ValidateInitMessage returns nil if init message is valid
Expand All @@ -13,7 +16,9 @@ func ValidateInitMessage(init *Init) error {
if !ValidThresholdSet(init.T, init.Operators) {
return fmt.Errorf("threshold set is invalid")
}

if !ValidAmountSet(phase0.Gwei(init.Amount)) {
return fmt.Errorf("amount should be in range between 32 ETH and 2048 ETH")
}
return nil
}

Expand Down Expand Up @@ -78,3 +83,10 @@ func EqualOperators(a, b []*Operator) bool {
}
return true
}

func ValidAmountSet(amount phase0.Gwei) bool {
if amount >= crypto.MIN_ACTIVATION_BALANCE && amount <= crypto.MAX_EFFECTIVE_BALANCE {
return true
}
return false
}
4 changes: 4 additions & 0 deletions initiator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spec

import (
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/ssvlabs/dkg-spec/eip1271"

"github.com/google/uuid"
Expand All @@ -22,6 +23,7 @@ func RunDKG(init *Init) ([]*Result, error) {
init.Fork,
init.Owner,
init.Nonce,
phase0.Gwei(init.Amount),
id,
results)
return results, err
Expand All @@ -48,6 +50,7 @@ func RunReshare(
fork,
signedReshare.Reshare.Owner,
signedReshare.Reshare.Nonce,
phase0.Gwei(signedReshare.Reshare.Amount),
id,
results)
return results, err
Expand Down Expand Up @@ -77,6 +80,7 @@ func RunResign(
fork,
signedResign.Resign.Owner,
signedResign.Resign.Nonce,
phase0.Gwei(signedResign.Resign.Amount),
id,
results)
return results, err
Expand Down
5 changes: 4 additions & 1 deletion operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package spec
import (
"crypto/rsa"

"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/herumi/bls-eth-go-binary/bls"
"github.com/ssvlabs/dkg-spec/crypto"
"github.com/ssvlabs/dkg-spec/eip1271"
Expand Down Expand Up @@ -30,7 +31,7 @@ func (op *Operator) Init(
init.Fork,
validatorPK,
init.WithdrawalCredentials,
crypto.MaxEffectiveBalanceInGwei,
phase0.Gwei(init.Amount),
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -106,6 +107,7 @@ func (op *Operator) Reshare(
signedReshare.Reshare.WithdrawalCredentials,
signedReshare.Reshare.Fork,
signedReshare.Reshare.Nonce,
phase0.Gwei(signedReshare.Reshare.Amount),
)
}

Expand Down Expand Up @@ -140,5 +142,6 @@ func (op *Operator) Resign(
signedResign.Resign.WithdrawalCredentials,
signedResign.Resign.Fork,
signedResign.Resign.Nonce,
phase0.Gwei(signedResign.Resign.Amount),
)
}
6 changes: 5 additions & 1 deletion reshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package spec
import (
"fmt"
"sort"

"github.com/attestantio/go-eth2-client/spec/phase0"
)

// ValidateReshareMessage returns nil if re-share message is valid
Expand Down Expand Up @@ -31,7 +33,9 @@ func ValidateReshareMessage(
if !ValidThresholdSet(reshare.NewT, reshare.NewOperators) {
return fmt.Errorf("new threshold set is invalid")
}

if !ValidAmountSet(phase0.Gwei(reshare.Amount)) {
return fmt.Errorf("amount should be in range between 32 ETH and 2048 ETH")
}
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions resign.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
package spec

import (
"fmt"

"github.com/attestantio/go-eth2-client/spec/phase0"
)

// ValidateResignMessage returns nil if re-sign message is valid
func ValidateResignMessage(
resign *Resign,
operator *Operator,
proof *SignedProof,
) error {
if !ValidAmountSet(phase0.Gwei(resign.Amount)) {
return fmt.Errorf("amount should be in range between 32 ETH and 2048 ETH")
}
if err := ValidateCeremonyProof(resign.Owner, resign.ValidatorPubKey, operator, *proof); err != nil {
return err
}
Expand Down
16 changes: 11 additions & 5 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ func BuildResult(
withdrawalCredentials []byte,
fork [4]byte,
nonce uint64,
amount phase0.Gwei,
) (*Result, error) {
// sign deposit data
depositDataRoot, err := crypto.DepositDataRootForFork(
fork,
validatorPK,
withdrawalCredentials,
crypto.MaxEffectiveBalanceInGwei,
phase0.Gwei(amount),
)
if err != nil {
return nil, err
}
depositDataSig := share.SignByte(depositDataRoot[:])

// sign proof
encryptedShare, err := crypto.Encrypt(&sk.PublicKey, []byte(share.SerializeToHexStr()))
if err != nil {
Expand Down Expand Up @@ -75,6 +75,7 @@ func ValidateResults(
fork [4]byte,
ownerAddress [20]byte,
nonce uint64,
amount phase0.Gwei,
requestID [24]byte,
results []*Result,
) (*bls.PublicKey, *phase0.DepositData, *bls.Sign, error) {
Expand Down Expand Up @@ -102,7 +103,7 @@ func ValidateResults(

// validate individual result
for _, result := range results {
if err := ValidateResult(operators, ownerAddress, requestID, withdrawalCredentials, validatorPK, fork, nonce, result); err != nil {
if err := ValidateResult(operators, ownerAddress, requestID, withdrawalCredentials, validatorPK, fork, nonce, amount, result); err != nil {
return nil, nil, nil, err
}
pub, deposit, ownerNonce, err := GetPartialSigsFromResult(result)
Expand Down Expand Up @@ -130,7 +131,7 @@ func ValidateResults(
}
depositData := &phase0.DepositData{
PublicKey: phase0.BLSPubKey(validatorRecoveredPK.Serialize()),
Amount: crypto.MaxEffectiveBalanceInGwei,
Amount: phase0.Gwei(amount),
WithdrawalCredentials: crypto.ETH1WithdrawalCredentials(withdrawalCredentials),
Signature: phase0.BLSSignature(masterDepositSig.Serialize()),
}
Expand All @@ -155,6 +156,7 @@ func ValidateResult(
validatorPK []byte,
fork [4]byte,
nonce uint64,
amount phase0.Gwei,
result *Result,
) error {
// verify operator
Expand All @@ -173,6 +175,7 @@ func ValidateResult(
fork,
ownerAddress,
nonce,
amount,
result,
); err != nil {
return fmt.Errorf("failed to verify partial signatures: %v", err)
Expand Down Expand Up @@ -218,6 +221,7 @@ func VerifyPartialSignatures(
fork [4]byte,
ownerAddress [20]byte,
nonce uint64,
amount phase0.Gwei,
result *Result,
) error {
pk, err := BLSPKEncode(result.SignedProof.Proof.SharePubKey)
Expand All @@ -241,6 +245,7 @@ func VerifyPartialSignatures(
result.SignedProof.Proof.ValidatorPubKey,
[]*bls.Sign{depositSig},
[]*bls.PublicKey{pk},
amount,
); err != nil {
return err
}
Expand Down Expand Up @@ -280,6 +285,7 @@ func VerifyPartialDepositDataSignatures(
validatorPubKey []byte,
sigs []*bls.Sign,
pks []*bls.PublicKey,
amount phase0.Gwei,
) error {
network, err := crypto.GetNetworkByFork(fork)
if err != nil {
Expand All @@ -288,7 +294,7 @@ func VerifyPartialDepositDataSignatures(

shareRoot, err := crypto.ComputeDepositMessageSigningRoot(network, &phase0.DepositMessage{
PublicKey: phase0.BLSPubKey(validatorPubKey),
Amount: crypto.MaxEffectiveBalanceInGwei,
Amount: amount,
WithdrawalCredentials: crypto.ETH1WithdrawalCredentials(withdrawalCredentials)})
if err != nil {
return fmt.Errorf("failed to compute deposit data root: %w", err)
Expand Down
1 change: 1 addition & 0 deletions testing/fixtures/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
TestNonce = uint64(0)
TestOwnerAddress = common.Address{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
TestRequestID = [24]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
TestAmount = crypto.MIN_ACTIVATION_BALANCE
)

func GenerateOperators(amount int) []*spec.Operator {
Expand Down
2 changes: 1 addition & 1 deletion testing/fixtures/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestSignDeposit(t *testing.T) {
require.NoError(t, err)
shareRoot, err := crypto.ComputeDepositMessageSigningRoot(network, &phase0.DepositMessage{
PublicKey: phase0.BLSPubKey(ShareSK(TestValidator13Operators).GetPublicKey().Serialize()),
Amount: crypto.MaxEffectiveBalanceInGwei,
Amount: crypto.MIN_ACTIVATION_BALANCE,
WithdrawalCredentials: crypto.ETH1WithdrawalCredentials(TestWithdrawalCred)})
require.NoError(t, err)

Expand Down
41 changes: 24 additions & 17 deletions testing/fixtures/reshare.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package fixtures

import spec "github.com/ssvlabs/dkg-spec"
import (
spec "github.com/ssvlabs/dkg-spec"
"github.com/ssvlabs/dkg-spec/crypto"
)

var (
TestReshare4Operators = spec.Reshare{
Expand All @@ -12,10 +15,11 @@ var (
GenerateOperators(4)[2],
GenerateOperators(7)[4],
},
OldT: 3,
NewT: 3,
Owner: TestOwnerAddress,
Nonce: 1,
OldT: 3,
NewT: 3,
Owner: TestOwnerAddress,
Nonce: 1,
Amount: uint64(crypto.MIN_ACTIVATION_BALANCE),
}
TestReshare7Operators = spec.Reshare{
ValidatorPubKey: ShareSK(TestValidator7Operators).GetPublicKey().Serialize(),
Expand All @@ -29,10 +33,11 @@ var (
GenerateOperators(7)[5],
GenerateOperators(10)[7],
},
OldT: 5,
NewT: 5,
Owner: TestOwnerAddress,
Nonce: 1,
OldT: 5,
NewT: 5,
Owner: TestOwnerAddress,
Nonce: 1,
Amount: uint64(crypto.MIN_ACTIVATION_BALANCE),
}
TestReshare10Operators = spec.Reshare{
ValidatorPubKey: ShareSK(TestValidator10Operators).GetPublicKey().Serialize(),
Expand All @@ -49,10 +54,11 @@ var (
GenerateOperators(10)[8],
GenerateOperators(13)[10],
},
OldT: 7,
NewT: 7,
Owner: TestOwnerAddress,
Nonce: 1,
OldT: 7,
NewT: 7,
Owner: TestOwnerAddress,
Nonce: 1,
Amount: uint64(crypto.MIN_ACTIVATION_BALANCE),
}
TestReshare13Operators = spec.Reshare{
ValidatorPubKey: ShareSK(TestValidator13Operators).GetPublicKey().Serialize(),
Expand All @@ -75,9 +81,10 @@ var (
PubKey: EncodedOperatorPK(TestOperator14SK),
},
},
OldT: 9,
NewT: 9,
Owner: TestOwnerAddress,
Nonce: 1,
OldT: 9,
NewT: 9,
Owner: TestOwnerAddress,
Nonce: 1,
Amount: uint64(crypto.MIN_ACTIVATION_BALANCE),
}
)
2 changes: 2 additions & 0 deletions testing/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

spec "github.com/ssvlabs/dkg-spec"
"github.com/ssvlabs/dkg-spec/crypto"
"github.com/ssvlabs/dkg-spec/testing/fixtures"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -41,6 +42,7 @@ func TestValidateInitMessage(t *testing.T) {
Fork: fixtures.TestFork,
Owner: fixtures.TestOwnerAddress,
Nonce: 0,
Amount: uint64(crypto.MIN_ACTIVATION_BALANCE),
}))
})

Expand Down
Loading

0 comments on commit 0dda5b1

Please sign in to comment.