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

cluster: add pre-generate registrations to lock v1.7 #2205

Merged
merged 3 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 5 additions & 10 deletions app/featureset/featureset.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,16 @@ const (
// QBFTTimersABTest enables a round-robin mixed timer selection for A/B testing
// the affects of different round timers.
QBFTTimersABTest Feature = "qbft_timers_ab_test"

// PregenValidatorRegistrations enables pre-generation of validator registrations data, signed at
// DKG time from the validator's cluster private keys.
PregenValidatorRegistrations Feature = "pregen_registrations"
)

var (
// state defines the current rollout status of each feature.
state = map[Feature]status{
QBFTConsensus: statusStable,
Priority: statusStable,
MockAlpha: statusAlpha,
RelayDiscovery: statusStable,
QBFTTimersABTest: statusAlpha,
PregenValidatorRegistrations: statusAlpha,
QBFTConsensus: statusStable,
Priority: statusStable,
MockAlpha: statusAlpha,
RelayDiscovery: statusStable,
QBFTTimersABTest: statusAlpha,
// Add all features and there status here.
}

Expand Down
36 changes: 24 additions & 12 deletions cluster/cluster_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"math/rand"
"testing"
"time"

k1 "github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -265,18 +266,6 @@ func TestSupportEIP712Sigs(t *testing.T) {
require.True(t, supportEIP712Sigs(supported))
}

func RandomValidatorAddresses(n int) []ValidatorAddresses {
var resp []ValidatorAddresses
for i := 0; i < n; i++ {
resp = append(resp, ValidatorAddresses{
FeeRecipientAddress: testutil.RandomETHAddress(),
WithdrawalAddress: testutil.RandomETHAddress(),
})
}

return resp
}

func RandomDepositData() DepositData {
return DepositData{
PubKey: testutil.RandomBytes48(),
Expand All @@ -285,3 +274,26 @@ func RandomDepositData() DepositData {
Signature: testutil.RandomBytes96(),
}
}

func RandomRegistration() BuilderRegistration {
regDate := time.Date(
2000,
1,
1,
0,
0,
0,
0,
time.UTC,
)

return BuilderRegistration{
Message: Registration{
FeeRecipient: testutil.RandomBytes32()[:20],
GasLimit: 30000000,
Timestamp: regDate,
PubKey: testutil.RandomBytes48(),
},
Signature: testutil.RandomBytes96(),
}
}
14 changes: 12 additions & 2 deletions cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
//go:generate go test . -v -update -clean

const (
v1_7 = "v1.7.0"
v1_6 = "v1.6.0"
v1_5 = "v1.5.0"
v1_4 = "v1.4.0"
Expand Down Expand Up @@ -134,14 +135,16 @@ func TestEncode(t *testing.T) {
testutil.RandomBytes48(),
testutil.RandomBytes48(),
},
DepositData: cluster.RandomDepositData(),
DepositData: cluster.RandomDepositData(),
BuilderRegistration: cluster.RandomRegistration(),
}, {
PubKey: testutil.RandomBytes48(),
PubShares: [][]byte{
testutil.RandomBytes48(),
testutil.RandomBytes48(),
},
DepositData: cluster.RandomDepositData(),
DepositData: cluster.RandomDepositData(),
BuilderRegistration: cluster.RandomRegistration(),
},
},
}
Expand All @@ -153,6 +156,13 @@ func TestEncode(t *testing.T) {
}
}

// Lock version prior to v1.7.0 don't support BuilderRegistration.
if isAnyVersion(version, v1_0, v1_1, v1_2, v1_3, v1_4, v1_5, v1_6) {
for i := range lock.Validators {
lock.Validators[i].BuilderRegistration = cluster.BuilderRegistration{}
}
}

t.Run("lock_json_"+vStr, func(t *testing.T) {
testutil.RequireGoldenJSON(t, lock,
testutil.WithFilename("cluster_lock_"+vStr+".json"))
Expand Down
4 changes: 2 additions & 2 deletions cluster/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (d Definition) MarshalJSON() ([]byte, error) {
return marshalDefinitionV1x2or3(d2)
case isAnyVersion(d2.Version, v1_4):
return marshalDefinitionV1x4(d2)
case isAnyVersion(d2.Version, v1_5, v1_6):
case isAnyVersion(d2.Version, v1_5, v1_6, v1_7):
return marshalDefinitionV1x5(d2)
default:
return nil, errors.New("unsupported version")
Expand Down Expand Up @@ -399,7 +399,7 @@ func (d *Definition) UnmarshalJSON(data []byte) error {
if err != nil {
return err
}
case isAnyVersion(version.Version, v1_5, v1_6):
case isAnyVersion(version.Version, v1_5, v1_6, v1_7):
def, err = unmarshalDefinitionV1x5(data)
if err != nil {
return err
Expand Down
61 changes: 58 additions & 3 deletions cluster/distvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cluster

import (
"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/tbls"
"github.com/obolnetwork/charon/tbls/tblsconv"
)
Expand All @@ -18,6 +19,9 @@ type DistValidator struct {

// DepositData is the validator deposit data.
DepositData DepositData `json:"deposit_data,omitempty" ssz:"Composite" lock_hash:"2"`

// BuilderRegistration is the pre-generated signed validator builder registration.
BuilderRegistration BuilderRegistration `json:"builder_registration,omitempty" ssz:"Composite" lock_hash:"3"`
}

// PublicKey returns the validator BLS group public key.
Expand Down Expand Up @@ -49,13 +53,21 @@ type distValidatorJSONv1x2to5 struct {
FeeRecipientAddress ethHex `json:"fee_recipient_address,omitempty"`
}

// distValidatorJSONv1x6 is the json formatter of DistValidator for versions v1.6.0 or later.
// distValidatorJSONv1x6 is the json formatter of DistValidator for versions v1.6.0.
type distValidatorJSONv1x6 struct {
PubKey ethHex `json:"distributed_public_key"`
PubShares []ethHex `json:"public_shares,omitempty"`
DepositData depositDataJSON `json:"deposit_data,omitempty"`
}

// distValidatorJSONv1x7 is the json formatter of DistValidator for versions v1.7.0 or later.
type distValidatorJSONv1x7 struct {
PubKey ethHex `json:"distributed_public_key"`
PubShares []ethHex `json:"public_shares,omitempty"`
DepositData depositDataJSON `json:"deposit_data,omitempty"`
BuilderRegistration builderRegistrationJSON `json:"builder_registration,omitempty"`
}

func distValidatorsFromV1x1(distValidators []distValidatorJSONv1x1) []DistValidator {
var resp []DistValidator
for _, dv := range distValidators {
Expand Down Expand Up @@ -113,7 +125,7 @@ func distValidatorsToV1x2to5(distValidators []DistValidator) []distValidatorJSON
return resp
}

func distValidatorsFromV1x6orLater(distValidators []distValidatorJSONv1x6) []DistValidator {
func distValidatorsFromV1x6(distValidators []distValidatorJSONv1x6) []DistValidator {
var resp []DistValidator
for _, dv := range distValidators {
var shares [][]byte
Expand All @@ -130,7 +142,7 @@ func distValidatorsFromV1x6orLater(distValidators []distValidatorJSONv1x6) []Dis
return resp
}

func distValidatorsToV1x6OrLater(distValidators []DistValidator) []distValidatorJSONv1x6 {
func distValidatorsToV1x6(distValidators []DistValidator) []distValidatorJSONv1x6 {
var resp []distValidatorJSONv1x6
for _, dv := range distValidators {
var shares []ethHex
Expand All @@ -147,3 +159,46 @@ func distValidatorsToV1x6OrLater(distValidators []DistValidator) []distValidator

return resp
}

func distValidatorsToV1x7OrLater(distValidators []DistValidator) []distValidatorJSONv1x7 {
var resp []distValidatorJSONv1x7
for _, dv := range distValidators {
var shares []ethHex
for _, share := range dv.PubShares {
shares = append(shares, share)
}

resp = append(resp, distValidatorJSONv1x7{
PubKey: dv.PubKey,
PubShares: shares,
DepositData: depositDataToJSON(dv.DepositData),
BuilderRegistration: registrationToJSON(dv.BuilderRegistration),
})
}

return resp
}

func distValidatorsFromV1x7OrLater(distValidators []distValidatorJSONv1x7) ([]DistValidator, error) {
var resp []DistValidator
for _, dv := range distValidators {
var shares [][]byte
for _, share := range dv.PubShares {
shares = append(shares, share)
}

reg, err := registrationFromJSON(dv.BuilderRegistration)
if err != nil {
return nil, errors.Wrap(err, "registration from json")
}

resp = append(resp, DistValidator{
PubKey: dv.PubKey,
PubShares: shares,
DepositData: depositDataFromJSON(dv.DepositData),
BuilderRegistration: reg,
})
}

return resp, nil
}
74 changes: 62 additions & 12 deletions cluster/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func (l Lock) MarshalJSON() ([]byte, error) {
case isAnyVersion(l.Version, v1_2, v1_3, v1_4, v1_5):
return marshalLockV1x2to5(l, lockHash)
case isAnyVersion(l.Version, v1_6):
return marshalLockV1x6orLater(l, lockHash)
return marshalLockV1x6(l, lockHash)
case isAnyVersion(l.Version, v1_7):
return marshalLockV1x7OrLater(l, lockHash)
default:
return nil, errors.New("unsupported version")
}
Expand Down Expand Up @@ -81,7 +83,12 @@ func (l *Lock) UnmarshalJSON(data []byte) error {
return err
}
case isAnyVersion(version.Definition.Version, v1_6):
lock, err = unmarshalLockV1x6orLater(data)
lock, err = unmarshalLockV1x6(data)
if err != nil {
return err
}
case isAnyVersion(version.Definition.Version, v1_7):
lock, err = unmarshalLockV1x7OrLater(data)
if err != nil {
return err
}
Expand Down Expand Up @@ -190,21 +197,35 @@ func marshalLockV1x2to5(lock Lock, lockHash [32]byte) ([]byte, error) {
LockHash: lockHash[:],
})
if err != nil {
return nil, errors.Wrap(err, "marshal definition v1_2")
return nil, errors.Wrap(err, "marshal definition v1_2-5")
}

return resp, nil
}

func marshalLockV1x6(lock Lock, lockHash [32]byte) ([]byte, error) {
resp, err := json.Marshal(lockJSONv1x6{
Definition: lock.Definition,
Validators: distValidatorsToV1x6(lock.Validators),
SignatureAggregate: lock.SignatureAggregate,
LockHash: lockHash[:],
})
if err != nil {
return nil, errors.Wrap(err, "marshal definition v1_6")
}

return resp, nil
}

func marshalLockV1x6orLater(lock Lock, lockHash [32]byte) ([]byte, error) {
resp, err := json.Marshal(lockJSONv1x6orLater{
func marshalLockV1x7OrLater(lock Lock, lockHash [32]byte) ([]byte, error) {
resp, err := json.Marshal(lockJSONv1x7{
Definition: lock.Definition,
Validators: distValidatorsToV1x6OrLater(lock.Validators),
Validators: distValidatorsToV1x7OrLater(lock.Validators),
SignatureAggregate: lock.SignatureAggregate,
LockHash: lockHash[:],
})
if err != nil {
return nil, errors.Wrap(err, "marshal definition v1_2")
return nil, errors.Wrap(err, "marshal definition v1_7")
}

return resp, nil
Expand Down Expand Up @@ -254,15 +275,36 @@ func unmarshalLockV1x2to5(data []byte) (lock Lock, err error) {
return lock, nil
}

func unmarshalLockV1x6orLater(data []byte) (lock Lock, err error) {
var lockJSON lockJSONv1x6orLater
func unmarshalLockV1x6(data []byte) (lock Lock, err error) {
var lockJSON lockJSONv1x6
if err := json.Unmarshal(data, &lockJSON); err != nil {
return Lock{}, errors.Wrap(err, "unmarshal definition")
}

lock = Lock{
Definition: lockJSON.Definition,
Validators: distValidatorsFromV1x6orLater(lockJSON.Validators),
Validators: distValidatorsFromV1x6(lockJSON.Validators),
SignatureAggregate: lockJSON.SignatureAggregate,
LockHash: lockJSON.LockHash,
}

return lock, nil
}

func unmarshalLockV1x7OrLater(data []byte) (lock Lock, err error) {
var lockJSON lockJSONv1x7
if err := json.Unmarshal(data, &lockJSON); err != nil {
return Lock{}, errors.Wrap(err, "unmarshal definition")
}

vals, err := distValidatorsFromV1x7OrLater(lockJSON.Validators)
if err != nil {
return Lock{}, err
}

lock = Lock{
Definition: lockJSON.Definition,
Validators: vals,
SignatureAggregate: lockJSON.SignatureAggregate,
LockHash: lockJSON.LockHash,
}
Expand All @@ -286,10 +328,18 @@ type lockJSONv1x2to5 struct {
LockHash ethHex `json:"lock_hash"`
}

// lockJSONv1x6orLater is the json formatter of Lock for versions v1.6.0 or later.
type lockJSONv1x6orLater struct {
// lockJSONv1x6 is the json formatter of Lock for versions v1.6.0.
type lockJSONv1x6 struct {
Definition Definition `json:"cluster_definition"`
Validators []distValidatorJSONv1x6 `json:"distributed_validators"`
SignatureAggregate ethHex `json:"signature_aggregate"`
LockHash ethHex `json:"lock_hash"`
}

// lockJSONv1x7 is the json formatter of Lock for versions v1.7.0 or later.
type lockJSONv1x7 struct {
Definition Definition `json:"cluster_definition"`
Validators []distValidatorJSONv1x7 `json:"distributed_validators"`
SignatureAggregate ethHex `json:"signature_aggregate"`
LockHash ethHex `json:"lock_hash"`
}
Loading