Skip to content

Commit

Permalink
addressed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CluEleSsUK committed Jun 20, 2023
1 parent 8c24046 commit 8ff54dc
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 44 deletions.
1 change: 0 additions & 1 deletion demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func main() {
if err != nil {
panic(err)
}
//time.Sleep(10 * time.Second)

err = orch.RunDKG(1 * time.Minute)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions internal/core/drand_daemon_dkg_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func (dd *DrandDaemon) Packet(ctx context.Context, packet *drand.GossipPacket) (
}

func (dd *DrandDaemon) BroadcastDKG(ctx context.Context, packet *drand.DKGPacket) (*drand.EmptyDKGResponse, error) {
if packet.GetDkg() == nil {
return nil, errors.New("DKG was missing from packet")
}
if packet.GetDkg().Metadata == nil {
return nil, errors.New("could not find packet metadata to read beaconID")
}
beaconID := packet.Dkg.Metadata.BeaconID

if !dd.beaconExists(beaconID) {
Expand Down
63 changes: 57 additions & 6 deletions internal/core/drand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import (
"context"
"errors"
"fmt"
"github.com/drand/drand/common"
dkg3 "github.com/drand/drand/protobuf/crypto/dkg"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/timestamppb"
"io"
"os"
"path"
"strings"
"testing"
"time"

"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"

"github.com/drand/drand/common"
chain2 "github.com/drand/drand/common/chain"
"github.com/drand/drand/common/key"
"github.com/drand/drand/crypto"
Expand All @@ -28,6 +26,9 @@ import (
context2 "github.com/drand/drand/internal/test/context"
"github.com/drand/drand/internal/test/testlogger"
"github.com/drand/drand/protobuf/drand"
"github.com/jonboulle/clockwork"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func setFDLimit(t testing.TB) {
Expand Down Expand Up @@ -1215,6 +1216,56 @@ func TestDKGWithMismatchedSchemes(t *testing.T) {
require.ErrorContainsf(t, err, key.ErrInvalidKeyScheme.Error(), "expected node to fail DKG due to mismatch of schemes")
}

func TestPacketWithoutMetadata(t *testing.T) {
t.Setenv("DRAND_TEST_LOGS", "DEBUG")
beaconID := "blah"
scenario := NewDrandTestScenario(t, 2, 2, 1*time.Second, beaconID, clockwork.NewFakeClockAt(time.Now()))

_, err := scenario.RunDKG(t)
require.NoError(t, err)

_, err = scenario.nodes[0].daemon.Packet(context.Background(), &drand.GossipPacket{Packet: &drand.GossipPacket_Proposal{
Proposal: &drand.ProposalTerms{
BeaconID: beaconID,
Epoch: 2,
Leader: nil,
Threshold: uint32(scenario.thr),
Timeout: timestamppb.New(time.Now().Add(1 * time.Minute)),
CatchupPeriodSeconds: 6,
TransitionTime: timestamppb.New(time.Now().Add(10 * time.Second)),
}}, Metadata: nil},
)

// should error but not panic
require.Error(t, err)
}

func TestDKGPacketWithoutMetadata(t *testing.T) {
t.Setenv("DRAND_TEST_LOGS", "DEBUG")
beaconID := "blah"
scenario := NewDrandTestScenario(t, 2, 2, 1*time.Second, beaconID, clockwork.NewFakeClockAt(time.Now()))

_, err := scenario.RunDKG(t)
require.NoError(t, err)

_, err = scenario.nodes[0].daemon.BroadcastDKG(context.Background(), &drand.DKGPacket{
Dkg: &dkg3.Packet{
Bundle: &dkg3.Packet_Deal{
Deal: &dkg3.DealBundle{
DealerIndex: 1,
Commits: nil,
Deals: nil,
SessionId: nil,
Signature: nil,
}},
Metadata: nil,
},
})

// should error but not panic
require.Error(t, err)
}

// AddNodesWithOptions creates new additional nodes that can participate during the initial DKG.
// The options set will overwrite the existing ones.
func (d *DrandTestScenario) AddNodesWithOptions(t *testing.T, n int, beaconID string, opts ...ConfigOption) []*MockNode {
Expand Down
2 changes: 1 addition & 1 deletion internal/dkg/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (d *Process) gossip(
}

func sendToPeer(client net.DKGClient, p *drand.Participant, packet *drand.GossipPacket) error {
retries := 10
retries := 8
backoff := 250 * time.Millisecond

peer := net.CreatePeer(p.Address, p.Tls)
Expand Down
48 changes: 19 additions & 29 deletions internal/dkg/actions_passive.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,53 +85,43 @@ func (d *Process) Packet(ctx context.Context, packet *drand.GossipPacket) (*dran
}

func commandType(command *drand.DKGCommand) string {
if command.GetInitial() != nil {
switch command.Command.(type) {
case *drand.DKGCommand_Initial:
return "Initial DKG"
}
if command.GetReject() != nil {
case *drand.DKGCommand_Resharing:
return "Resharing"
}
if command.GetAccept() != nil {
case *drand.DKGCommand_Accept:
return "Accepting"
}
if command.GetReject() != nil {
case *drand.DKGCommand_Reject:
return "Rejecting"
}
if command.GetJoin() != nil {
case *drand.DKGCommand_Join:
return "Joining"
}
if command.GetExecute() != nil {
//nolint:goconst //the two strings rae in different places
case *drand.DKGCommand_Execute:
return "Executing"
}
if command.GetAbort() != nil {
case *drand.DKGCommand_Abort:
return "Aborting"
default:
return "UnknownCommand"
}

return "UnknownCommand"
}

func packetName(packet *drand.GossipPacket) string {
if packet.GetProposal() != nil {
switch packet.Packet.(type) {
case *drand.GossipPacket_Proposal:
return "Proposal"
}
if packet.GetAccept() != nil {
case *drand.GossipPacket_Accept:
return "Accept"
}
if packet.GetReject() != nil {
case *drand.GossipPacket_Reject:
return "Reject"
}
if packet.GetAbort() != nil {
case *drand.GossipPacket_Abort:
return "Abort"
}
if packet.GetExecute() != nil {
case *drand.GossipPacket_Execute:
return "Execute"
}
if packet.GetDkg() != nil {
case *drand.GossipPacket_Dkg:
return "DKG"
default:
return "Unknown"
}

return "Unknown"
}

// BroadcastDKG gossips internal DKG protocol messages to other nodes (i.e. any messages encapsulated in the Kyber DKG)
Expand Down
2 changes: 0 additions & 2 deletions internal/dkg/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"math/rand"
"sync"

oteltrace "go.opentelemetry.io/otel/trace"

commonutils "github.com/drand/drand/common"
"github.com/drand/drand/common/log"
"github.com/drand/drand/crypto"
Expand Down
8 changes: 3 additions & 5 deletions internal/dkg/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ func (d *Process) executeDKG(ctx context.Context, beaconID string) error {

d.log.Infow("DKG execution setup successful", "beaconID", beaconID)

go func() {
go func(config dkg.Config) {
// wait for `KickOffGracePeriod` to allow other nodes to set up their broadcasters
time.Sleep(d.config.KickoffGracePeriod)
// copy this to avoid any data races with kyber
dkgConfigCopy := *dkgConfig
err := d.executeAndFinishDKG(ctx, beaconID, dkgConfigCopy)
err := d.executeAndFinishDKG(ctx, beaconID, config)
if err != nil {
d.log.Errorw("there was an error during the DKG!", "beaconID", beaconID, "error", err)
}
}()
}(*dkgConfig)
return nil
}

Expand Down

0 comments on commit 8ff54dc

Please sign in to comment.