Skip to content

Commit

Permalink
use beaconID from metadata rather than passing it around
Browse files Browse the repository at this point in the history
  • Loading branch information
CluEleSsUK committed Jul 4, 2023
1 parent f9efcc1 commit 5919617
Show file tree
Hide file tree
Showing 17 changed files with 310 additions and 443 deletions.
7 changes: 6 additions & 1 deletion internal/dkg/actions_active.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func (d *Process) Command(ctx context.Context, command *drand.DKGCommand) (*dran
if command == nil {
return nil, errors.New("command cannot be nil")
}
if command.Metadata == nil {
return nil, errors.New("command metadata cannot be nil")
}
beaconID := command.Metadata.BeaconID
commandName := commandType(command)
d.lock.Lock()
Expand Down Expand Up @@ -115,7 +118,7 @@ func (d *Process) StartNetwork(

// remap the CLI payload into one useful for applying to the DKG state
terms := drand.ProposalTerms{
BeaconID: options.BeaconID,
BeaconID: beaconID,
Threshold: options.Threshold,
Epoch: 1,
Timeout: options.Timeout,
Expand Down Expand Up @@ -144,6 +147,8 @@ func (d *Process) StartNetwork(
Packet: &drand.GossipPacket_Proposal{
Proposal: &terms,
},
// this gets set in the enclosing scope
Metadata: nil,
}, nil
}

Expand Down
10 changes: 3 additions & 7 deletions internal/dkg/actions_active_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ func TestInitialDKG(t *testing.T) {
{
name: "valid proposal with successful gossip sends to all parties except leader",
proposal: &drand.FirstProposalOptions{
BeaconID: beaconID,
Timeout: timestamppb.New(time.Now().Add(1 * time.Hour)),
Threshold: 1,
PeriodSeconds: 10,
Expand All @@ -64,7 +63,6 @@ func TestInitialDKG(t *testing.T) {
{
name: "database get failure does not attempt to call network",
proposal: &drand.FirstProposalOptions{
BeaconID: beaconID,
Timeout: timestamppb.New(time.Now().Add(1 * time.Hour)),
Threshold: 1,
PeriodSeconds: 10,
Expand All @@ -82,7 +80,6 @@ func TestInitialDKG(t *testing.T) {
{
name: "database store failure does not attempt to call network",
proposal: &drand.FirstProposalOptions{
BeaconID: beaconID,
Timeout: timestamppb.New(time.Now().Add(1 * time.Hour)),
Threshold: 1,
PeriodSeconds: 10,
Expand Down Expand Up @@ -147,7 +144,6 @@ func TestReshare(t *testing.T) {
beaconID := "someBeaconID"
currentState := NewCompleteDKGEntry(t, beaconID, Complete, alice, bob)
validProposal := drand.ProposalOptions{
BeaconID: beaconID,
Timeout: timestamppb.New(time.Now().Add(1 * time.Hour)),
TransitionTime: timestamppb.New(time.Now().Add(10 * time.Minute)),
Threshold: 1,
Expand Down Expand Up @@ -336,7 +332,7 @@ func TestJoin(t *testing.T) {
}{
{
name: "join on first epoch succeeds without group file but does not gossip anything",
joinOptions: &drand.JoinOptions{BeaconID: beaconID},
joinOptions: &drand.JoinOptions{},
prepareMocks: func(store *MockStore, client *MockDKGClient, expectedError error) {
current := NewCompleteDKGEntry(t, beaconID, Proposed, bob)
current.Joining = []*drand.Participant{alice, bob, carol}
Expand All @@ -352,7 +348,7 @@ func TestJoin(t *testing.T) {
},
{
name: "join on second epoch succeeds with group file but does not gossip anything",
joinOptions: &drand.JoinOptions{BeaconID: beaconID, GroupFile: groupFile.Bytes()},
joinOptions: &drand.JoinOptions{GroupFile: groupFile.Bytes()},
prepareMocks: func(store *MockStore, client *MockDKGClient, expectedError error) {
current := NewCompleteDKGEntry(t, beaconID, Proposed, bob, carol)
current.Epoch = 2
Expand All @@ -368,7 +364,7 @@ func TestJoin(t *testing.T) {
},
{
name: "join on second epoch succeeds without group file fails",
joinOptions: &drand.JoinOptions{BeaconID: beaconID, GroupFile: nil},
joinOptions: &drand.JoinOptions{GroupFile: nil},
prepareMocks: func(store *MockStore, client *MockDKGClient, expectedError error) {
current := NewCompleteDKGEntry(t, beaconID, Proposed, alice, bob)
current.Epoch = 2
Expand Down
4 changes: 2 additions & 2 deletions internal/dkg/actions_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dkg

import (
"errors"
"google.golang.org/protobuf/proto"

"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -66,8 +67,7 @@ func (d *Process) verifyMessage(packet *drand.GossipPacket, metadata *drand.Goss
func messageForProto(proposal *drand.ProposalTerms, packet *drand.GossipPacket, beaconID string) []byte {
// we remove the metadata for verification of the packet, as the signer hasn't created the metadta
// upon signing
//nolint:govet
packetWithoutMetadata := *packet
packetWithoutMetadata := proto.Clone(packet).(*drand.GossipPacket)
packetWithoutMetadata.Metadata = nil
return []byte(proposal.String() + packetWithoutMetadata.String() + beaconID)
}
Expand Down
16 changes: 12 additions & 4 deletions internal/dkg/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ func (d *Process) setupDKG(ctx context.Context, beaconID string) (*dkg.Config, e
return config, nil
}

// this is done rarely and is a shared object: no good reason not to use a clone (and it makes the race checker happy :))
// this is done rarely and is a shared object: no good reason not to use a clone (and it makes the race checker happy)
//
//nolint:gocritic
//nolint:funlen
func (d *Process) executeAndFinishDKG(ctx context.Context, beaconID string, config dkg.Config) error {
ctx, span := metrics.NewSpan(ctx, "dkg.executeAndFinishDKG")
defer span.End()
Expand Down Expand Up @@ -152,7 +152,10 @@ func (d *Process) executeAndFinishDKG(ctx context.Context, beaconID string, conf
}

leaveNetwork := func(err error) {
d.log.Errorw("There was an error during the DKG - we were likely evicted. Will attempt to store failed state", "error", err)
d.log.Errorw(
"There was an error during the DKG - we were likely evicted. Will attempt to store failed state",
"error", err,
)
// could this also be a timeout? is that semantically the same as eviction after DKG execution was triggered?
evictedState, err := current.Evicted()
if err != nil {
Expand All @@ -174,7 +177,12 @@ func (d *Process) executeAndFinishDKG(ctx context.Context, beaconID string, conf
return err
}

func (d *Process) startDKGExecution(ctx context.Context, beaconID string, current *DBState, config *dkg.Config) (*ExecutionOutput, error) {
func (d *Process) startDKGExecution(
ctx context.Context,
beaconID string,
current *DBState,
config *dkg.Config,
) (*ExecutionOutput, error) {
ctx, span := metrics.NewSpan(ctx, "dkg.startDKGExecution")
defer span.End()
phaser := dkg.NewTimePhaser(d.config.TimeBetweenDKGPhases)
Expand Down
26 changes: 8 additions & 18 deletions internal/drand-cli/dkg_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ func makeProposal(c *cli.Context, l log.Logger) error {
return err
}

beaconID := withDefault(c.String(beaconIDFlag.Name), common.DefaultBeaconID)

if isInitialProposal(c) {
proposal, err := parseInitialProposal(c)
if err != nil {
Expand All @@ -175,7 +177,7 @@ func makeProposal(c *cli.Context, l log.Logger) error {
_, err = client.Command(c.Context, &drand.DKGCommand{
Command: &drand.DKGCommand_Initial{Initial: proposal},
Metadata: &drand.CommandMetadata{
BeaconID: proposal.BeaconID,
BeaconID: beaconID,
},
})
if err != nil {
Expand All @@ -190,7 +192,7 @@ func makeProposal(c *cli.Context, l log.Logger) error {
_, err = client.Command(c.Context, &drand.DKGCommand{
Command: &drand.DKGCommand_Resharing{Resharing: proposal},
Metadata: &drand.CommandMetadata{
BeaconID: proposal.BeaconID,
BeaconID: beaconID,
},
})
if err != nil {
Expand All @@ -215,7 +217,6 @@ func withDefault(first, second string) string {
}

func parseInitialProposal(c *cli.Context) (*drand.FirstProposalOptions, error) {
beaconID := withDefault(c.String(beaconIDFlag.Name), common.DefaultBeaconID)
requiredFlags := []*cli.StringFlag{proposalFlag, periodFlag, schemeFlag, catchupPeriodFlag, transitionTimeFlag}

for _, flag := range requiredFlags {
Expand Down Expand Up @@ -245,7 +246,6 @@ func parseInitialProposal(c *cli.Context) (*drand.FirstProposalOptions, error) {
genesisTime := time.Now().Add(c.Duration(transitionTimeFlag.Name))

return &drand.FirstProposalOptions{
BeaconID: beaconID,
Timeout: timestamppb.New(timeout),
Threshold: uint32(c.Int(thresholdFlag.Name)),
PeriodSeconds: uint32(period.Seconds()),
Expand Down Expand Up @@ -333,7 +333,6 @@ func parseProposal(c *cli.Context, l log.Logger) (*drand.ProposalOptions, error)
actualTransitionTime := chain.TimeOfRound(time.Duration(info.Period)*time.Second, info.GenesisTime, transitionRound)

return &drand.ProposalOptions{
BeaconID: beaconID,
Timeout: timestamppb.New(timeout),
TransitionTime: timestamppb.New(time.Unix(actualTransitionTime, 0)),
Threshold: uint32(c.Int(thresholdFlag.Name)),
Expand Down Expand Up @@ -364,7 +363,6 @@ func joinNetwork(c *cli.Context) error {
}
_, err = client.Command(c.Context, &drand.DKGCommand{
Command: &drand.DKGCommand_Join{Join: &drand.JoinOptions{
BeaconID: beaconID,
GroupFile: groupFile,
}},
Metadata: &drand.CommandMetadata{
Expand All @@ -381,9 +379,7 @@ func joinNetwork(c *cli.Context) error {
func executeDKG(c *cli.Context) error {
err := runSimpleAction(c, func(beaconID string, client drand.DKGControlClient) error {
_, err := client.Command(c.Context, &drand.DKGCommand{
Command: &drand.DKGCommand_Execute{Execute: &drand.ExecutionOptions{
BeaconID: beaconID,
}},
Command: &drand.DKGCommand_Execute{Execute: &drand.ExecutionOptions{}},
Metadata: &drand.CommandMetadata{
BeaconID: beaconID,
},
Expand All @@ -400,9 +396,7 @@ func executeDKG(c *cli.Context) error {
func acceptDKG(c *cli.Context) error {
err := runSimpleAction(c, func(beaconID string, client drand.DKGControlClient) error {
_, err := client.Command(c.Context, &drand.DKGCommand{
Command: &drand.DKGCommand_Accept{Accept: &drand.AcceptOptions{
BeaconID: beaconID,
}},
Command: &drand.DKGCommand_Accept{Accept: &drand.AcceptOptions{}},
Metadata: &drand.CommandMetadata{
BeaconID: beaconID,
},
Expand All @@ -419,9 +413,7 @@ func acceptDKG(c *cli.Context) error {
func rejectDKG(c *cli.Context) error {
err := runSimpleAction(c, func(beaconID string, client drand.DKGControlClient) error {
_, err := client.Command(c.Context, &drand.DKGCommand{
Command: &drand.DKGCommand_Reject{Reject: &drand.RejectOptions{
BeaconID: beaconID,
}},
Command: &drand.DKGCommand_Reject{Reject: &drand.RejectOptions{}},
Metadata: &drand.CommandMetadata{
BeaconID: beaconID,
},
Expand All @@ -439,9 +431,7 @@ func rejectDKG(c *cli.Context) error {
func abortDKG(c *cli.Context) error {
err := runSimpleAction(c, func(beaconID string, client drand.DKGControlClient) error {
_, err := client.Command(c.Context, &drand.DKGCommand{
Command: &drand.DKGCommand_Abort{Abort: &drand.AbortOptions{
BeaconID: beaconID,
}},
Command: &drand.DKGCommand_Abort{Abort: &drand.AbortOptions{}},
Metadata: &drand.CommandMetadata{
BeaconID: beaconID,
},
Expand Down
12 changes: 3 additions & 9 deletions internal/test/dkg_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func (r *DKGRunner) StartNetwork(
) error {
_, err := r.Client.Command(context.Background(), &drand.DKGCommand{Command: &drand.DKGCommand_Initial{
Initial: &drand.FirstProposalOptions{
BeaconID: r.BeaconID,
Timeout: timestamppb.New(time.Now().Add(timeout)),
Threshold: uint32(threshold),
PeriodSeconds: uint32(period),
Expand All @@ -57,7 +56,6 @@ func (r *DKGRunner) StartProposal(
) error {
_, err := r.Client.Command(context.Background(), &drand.DKGCommand{Command: &drand.DKGCommand_Resharing{
Resharing: &drand.ProposalOptions{
BeaconID: r.BeaconID,
Threshold: uint32(threshold),
CatchupPeriodSeconds: uint32(catchupPeriod),
Timeout: timestamppb.New(time.Now().Add(1 * time.Minute)),
Expand All @@ -73,7 +71,7 @@ func (r *DKGRunner) StartProposal(

func (r *DKGRunner) StartExecution() error {
_, err := r.Client.Command(context.Background(), &drand.DKGCommand{Command: &drand.DKGCommand_Execute{
Execute: &drand.ExecutionOptions{BeaconID: r.BeaconID}},
Execute: &drand.ExecutionOptions{}},
Metadata: &drand.CommandMetadata{BeaconID: r.BeaconID},
})
return err
Expand All @@ -82,7 +80,6 @@ func (r *DKGRunner) StartExecution() error {
func (r *DKGRunner) JoinDKG() error {
_, err := r.Client.Command(context.Background(), &drand.DKGCommand{Command: &drand.DKGCommand_Join{
Join: &drand.JoinOptions{
BeaconID: r.BeaconID,
GroupFile: nil,
}},
Metadata: &drand.CommandMetadata{BeaconID: r.BeaconID},
Expand All @@ -99,7 +96,6 @@ func (r *DKGRunner) JoinReshare(oldGroup *key.Group) error {
}
_, err = r.Client.Command(context.Background(), &drand.DKGCommand{Command: &drand.DKGCommand_Join{
Join: &drand.JoinOptions{
BeaconID: r.BeaconID,
GroupFile: groupFileBytes.Bytes(),
}},
Metadata: &drand.CommandMetadata{BeaconID: r.BeaconID},
Expand All @@ -110,17 +106,15 @@ func (r *DKGRunner) JoinReshare(oldGroup *key.Group) error {

func (r *DKGRunner) Accept() error {
_, err := r.Client.Command(context.Background(), &drand.DKGCommand{Command: &drand.DKGCommand_Accept{
Accept: &drand.AcceptOptions{
BeaconID: r.BeaconID,
}},
Accept: &drand.AcceptOptions{}},
Metadata: &drand.CommandMetadata{BeaconID: r.BeaconID},
})
return err
}

func (r *DKGRunner) Abort() error {
_, err := r.Client.Command(context.Background(), &drand.DKGCommand{Command: &drand.DKGCommand_Abort{
Abort: &drand.AbortOptions{BeaconID: r.BeaconID}},
Abort: &drand.AbortOptions{}},
Metadata: &drand.CommandMetadata{BeaconID: r.BeaconID},
})

Expand Down
4 changes: 2 additions & 2 deletions protobuf/common/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions protobuf/crypto/dkg/dkg.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions protobuf/drand/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5919617

Please sign in to comment.