Skip to content

Commit

Permalink
Multi beacon support (#846)
Browse files Browse the repository at this point in the history
Add multi-beacon support

Co-authored-by: Ezequiel Raynaudo <raynaudo.ee@gmail.com>
  • Loading branch information
emmanuelm41 and raynaudoe committed Dec 7, 2021
1 parent e663c6f commit 109200b
Show file tree
Hide file tree
Showing 30 changed files with 1,779 additions and 1,147 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ drand: build
#################### Lint and fmt process ##################

install_lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.41.1
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.43.0

lint:
golangci-lint --version
Expand Down
2 changes: 1 addition & 1 deletion client/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/drand/drand/client/test/result/mock"
)

// MockClient provide a mocked client interface
//nolint:gocritic
// MockClient provide a mocked client interface
type MockClient struct {
sync.Mutex
OptionalInfo *chain.Info
Expand Down
12 changes: 9 additions & 3 deletions cmd/drand-cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"runtime"
"strconv"
"strings"
"sync"

"github.com/drand/drand/core/migration"

Expand Down Expand Up @@ -46,6 +47,8 @@ var (
buildDate = "unknown"
)

var SetVersionPrinter sync.Once

const defaultPort = "8080"

func banner() {
Expand Down Expand Up @@ -498,9 +501,12 @@ func CLI() *cli.App {

app := cli.NewApp()
app.Name = "drand"
cli.VersionPrinter = func(c *cli.Context) {
fmt.Fprintf(output, "drand %s (date %v, commit %v) by nikkolasg\n", version, buildDate, gitCommit)
}

SetVersionPrinter.Do(func() {
cli.VersionPrinter = func(c *cli.Context) {
fmt.Fprintf(output, "drand %s (date %v, commit %v) by nikkolasg\n", version, buildDate, gitCommit)
}
})

app.ExitErrHandler = func(context *cli.Context, err error) {
// override to prevent default behavior of calling OS.exit(1),
Expand Down
59 changes: 49 additions & 10 deletions cmd/drand-cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,21 @@ func TestUtilCheck(t *testing.T) {
ctx, cancel = context.WithCancel(context.Background())
defer cancel()

listen = []string{"drand", "start", "--tls-disable", "--folder", tmp, "--control", test.FreePort()}
go CLI().RunContext(ctx, listen)
listen = []string{"drand", "start", "--tls-disable", "--folder", tmp, "--control", test.FreePort(), "--private-listen", keyAddr}
go func() {
err := CLI().RunContext(ctx, listen)
if err != nil {
t.Errorf(err.Error())
}
}()

time.Sleep(200 * time.Millisecond)

check = []string{"drand", "util", "check", "--verbose", "--tls-disable", keyAddr}
require.NoError(t, CLI().Run(check))
}

//nolint:funlen
func TestStartWithoutGroup(t *testing.T) {
sch := scheme.GetSchemeFromEnv()
beaconID := common.GetBeaconIDFromEnv()
Expand All @@ -313,9 +320,8 @@ func TestStartWithoutGroup(t *testing.T) {
pubPath := path.Join(tmpPath, "pub.key")
port1, _ := strconv.Atoi(test.FreePort())
addr := "127.0.0.1:" + strconv.Itoa(port1)
ctrlPort1 := test.FreePort()
ctrlPort2 := test.FreePort()
metricsPort := test.FreePort()

ctrlPort1, ctrlPort2, metricsPort := test.FreePort(), test.FreePort(), test.FreePort()

priv := key.NewKeyPair(addr)
require.NoError(t, key.Save(pubPath, priv.Public, false))
Expand All @@ -327,13 +333,21 @@ func TestStartWithoutGroup(t *testing.T) {
startArgs := []string{
"drand",
"start",
"--private-listen", priv.Public.Address(),
"--tls-disable",
"--verbose",
"--folder", tmpPath,
"--control", ctrlPort1,
"--metrics", "127.0.0.1:" + metricsPort,
}
go CLI().Run(startArgs)

go func() {
err := CLI().Run(startArgs)
if err != nil {
t.Errorf(err.Error())
}
}()

time.Sleep(500 * time.Millisecond)

fmt.Println("--- DRAND SHARE --- (expected to fail)")
Expand All @@ -342,6 +356,8 @@ func TestStartWithoutGroup(t *testing.T) {

initDKGArgs := []string{"drand", "share", "--control", ctrlPort1, "--id", beaconID}
require.Error(t, CLI().Run(initDKGArgs))

fmt.Println("--- DRAND STOP --- (failing instanace)")
CLI().Run([]string{"drand", "stop", "--control", ctrlPort1})

fmt.Println(" --- DRAND GROUP ---")
Expand Down Expand Up @@ -378,8 +394,24 @@ func TestStartWithoutGroup(t *testing.T) {

fmt.Println(" --- DRAND START --- control ", ctrlPort2)

start2 := []string{"drand", "start", "--control", ctrlPort2, "--tls-disable", "--folder", tmpPath, "--verbose", "--private-rand"}
go CLI().Run(start2)
start2 := []string{
"drand",
"start",
"--control", ctrlPort2,
"--private-listen", priv.Public.Address(),
"--tls-disable",
"--folder", tmpPath,
"--verbose",
"--private-rand",
}

go func() {
err := CLI().Run(start2)
if err != nil {
t.Errorf(err.Error())
}
}()

defer CLI().Run([]string{"drand", "stop", "--control", ctrlPort2})

time.Sleep(500 * time.Millisecond)
Expand Down Expand Up @@ -547,6 +579,7 @@ func TestClientTLS(t *testing.T) {
startArgs := []string{
"drand",
"start",
"--private-listen", priv.Public.Address(),
"--tls-cert", certPath,
"--tls-key", keyPath,
"--control", ctrlPort,
Expand Down Expand Up @@ -711,8 +744,14 @@ func (d *drandInstance) run(t *testing.T) {
"--control", d.ctrlPort,
"--folder", d.path,
"--metrics", d.metrics,
"--private-listen", d.addr,
}
go CLI().Run(startArgs)

go func() {
err := CLI().Run(startArgs)
require.NoError(t, err)
}()

// make sure we run each one sequentially
testStatus(t, d.ctrlPort)
}
Expand All @@ -732,7 +771,7 @@ func launchDrandInstances(t *testing.T, n int) ([]*drandInstance, string) {
require.NoError(t, err)

certPath := path.Join(nodePath, "cert")
keyPath := path.Join(nodePath, "tlskey")
keyPath := path.Join(nodePath, "tls.key")
pubPath := path.Join(tmpPath, "pub.key")

freePort := test.FreePort()
Expand Down
46 changes: 9 additions & 37 deletions cmd/drand-cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,25 @@ import (
"fmt"

"github.com/drand/drand/core"
"github.com/drand/drand/key"
"github.com/drand/drand/metrics"
"github.com/drand/drand/metrics/pprof"
"github.com/urfave/cli/v2"
)

func startCmd(c *cli.Context) error {
conf := contextToConfig(c)
stores, err := key.NewFileStores(conf.ConfigFolderMB())

// Create and start drand daemon
drandDaemon, err := core.NewDrandDaemon(conf)
if err != nil {
return fmt.Errorf("can't read file stores %s", err)
return fmt.Errorf("can't instantiate drand daemon %s", err)
}

var drand *core.Drand

// determine if we already ran a DKG or not
beaconID, fs := key.GetFirstStore(stores)
_, errG := fs.LoadGroup()
_, errS := fs.LoadShare()

// XXX place that logic inside core/ directly with only one method
freshRun := errG != nil || errS != nil

if freshRun {
fmt.Println("drand: will run as fresh install -> expect to run DKG.")
drand, err = core.NewDrand(fs, conf)
if err != nil {
return fmt.Errorf("can't instantiate drand instance %s", err)
}
} else {
fmt.Printf("drand: will already start running randomness beacon. BeaconID: [%s]\n", beaconID)
drand, err = core.LoadDrand(fs, conf)
if err != nil {
return fmt.Errorf("can't load drand instance %s", err)
}
// XXX make it configurable so that new share holder can still start if
// nobody started.
// drand.StartBeacon(!c.Bool(pushFlag.Name))
catchup := true
drand.StartBeacon(catchup)
}
// Start metrics server
if c.IsSet(metricsFlag.Name) {
_ = metrics.Start(c.String(metricsFlag.Name), pprof.WithProfile(), drand.PeerMetrics)
// Check stores and start BeaconProcess
err = drandDaemon.LoadBeacons(c.String(metricsFlag.Name))
if err != nil {
return fmt.Errorf("couldn't load existing beacons: %s", err)
}
<-drand.WaitExit()

<-drandDaemon.WaitExit()
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestBroadcast(t *testing.T) {
n := 5
sch, beaconID := scheme.GetSchemeFromEnv(), common.GetBeaconIDFromEnv()

drands, group, dir, _ := BatchNewDrand(t, n, true, sch, beaconID)
_, drands, group, dir, _ := BatchNewDrand(t, n, true, sch, beaconID)
defer os.RemoveAll(dir)
defer CloseAllDrands(drands)

Expand Down
3 changes: 2 additions & 1 deletion core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
func TestClientPrivate(t *testing.T) {
sch, beaconID := scheme.GetSchemeFromEnv(), common.GetBeaconIDFromEnv()

drands, _, dir, _ := BatchNewDrand(t, 1, false, sch, beaconID, WithPrivateRandomness())
//nolint:dogsled
_, drands, _, dir, _ := BatchNewDrand(t, 1, false, sch, beaconID, WithPrivateRandomness())
defer CloseAllDrands(drands)
defer os.RemoveAll(dir)

Expand Down
Loading

0 comments on commit 109200b

Please sign in to comment.