use temp dir for drand state

This commit is contained in:
Yusef Napora 2020-06-25 12:38:40 -04:00
parent fda66f5bd9
commit 3cbf5efddd
2 changed files with 21 additions and 7 deletions

View File

@ -69,10 +69,11 @@ func runMiner(t *TestEnvironment) error {
func runDrandNode(t *TestEnvironment) error { func runDrandNode(t *TestEnvironment) error {
t.RecordMessage("running drand node") t.RecordMessage("running drand node")
_, err := prepareDrandNode(t) dr, err := prepareDrandNode(t)
if err != nil { if err != nil {
return err return err
} }
defer dr.Cleanup()
// TODO add ability to halt / recover on demand // TODO add ability to halt / recover on demand
ctx := context.Background() ctx := context.Background()

View File

@ -6,8 +6,10 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"path"
"time" "time"
"github.com/drand/drand/chain" "github.com/drand/drand/chain"
@ -35,6 +37,12 @@ type DrandRuntimeInfo struct {
type DrandInstance struct { type DrandInstance struct {
Node node.Node Node node.Node
GossipRelay *lp2p.GossipRelayNode GossipRelay *lp2p.GossipRelayNode
stateDir string
}
func (d *DrandInstance) Cleanup() error {
return os.RemoveAll(d.stateDir)
} }
// waitForDrandConfig should be called by filecoin instances before constructing the lotus Node // waitForDrandConfig should be called by filecoin instances before constructing the lotus Node
@ -70,8 +78,13 @@ func prepareDrandNode(t *TestEnvironment) (*DrandInstance, error) {
beaconOffset := 3 beaconOffset := 3
stateDir, err := ioutil.TempDir("", fmt.Sprintf("drand-%d", t.GroupSeq))
if err != nil {
return nil, err
}
// TODO(maybe): use TLS? // TODO(maybe): use TLS?
n := node.NewLocalNode(int(seq), period.String(), "~/", false, myAddr.String()) n := node.NewLocalNode(int(seq), period.String(), stateDir, false, myAddr.String())
// share the node addresses with other nodes // share the node addresses with other nodes
// TODO: if we implement TLS, this is where we'd share public TLS keys // TODO: if we implement TLS, this is where we'd share public TLS keys
@ -105,7 +118,7 @@ func prepareDrandNode(t *TestEnvironment) (*DrandInstance, error) {
t.SyncClient.MustSignalAndWait(ctx, "drand-start", nNodes) t.SyncClient.MustSignalAndWait(ctx, "drand-start", nNodes)
t.RecordMessage("Starting drand sharing ceremony") t.RecordMessage("Starting drand sharing ceremony")
if err := n.Start("~/"); err != nil { if err := n.Start(stateDir); err != nil {
return nil, err return nil, err
} }
@ -159,18 +172,17 @@ func prepareDrandNode(t *TestEnvironment) (*DrandInstance, error) {
var relayAddrs []peer.AddrInfo var relayAddrs []peer.AddrInfo
if runGossipRelay { if runGossipRelay {
_ = os.Mkdir("~/drand-gossip", os.ModePerm) gossipDir := path.Join(stateDir, "gossip-relay")
listenAddr := fmt.Sprintf("/ip4/%s/tcp/7777", myAddr.String()) listenAddr := fmt.Sprintf("/ip4/%s/tcp/7777", myAddr.String())
relayCfg := lp2p.GossipRelayConfig{ relayCfg := lp2p.GossipRelayConfig{
ChainHash: hex.EncodeToString(info.Hash()), ChainHash: hex.EncodeToString(info.Hash()),
Addr: listenAddr, Addr: listenAddr,
DataDir: "~/drand-gossip", DataDir: gossipDir,
IdentityPath: "~/drand-gossip/identity.key", IdentityPath: path.Join(gossipDir, "identity.key"),
Insecure: true, Insecure: true,
Client: client, Client: client,
} }
t.RecordMessage("starting drand gossip relay") t.RecordMessage("starting drand gossip relay")
var err error
gossipRelay, err = lp2p.NewGossipRelayNode(log.DefaultLogger, &relayCfg) gossipRelay, err = lp2p.NewGossipRelayNode(log.DefaultLogger, &relayCfg)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to construct drand gossip relay: %w", err) return nil, fmt.Errorf("failed to construct drand gossip relay: %w", err)
@ -212,6 +224,7 @@ func prepareDrandNode(t *TestEnvironment) (*DrandInstance, error) {
return &DrandInstance{ return &DrandInstance{
Node: n, Node: n,
GossipRelay: gossipRelay, GossipRelay: gossipRelay,
stateDir: stateDir,
}, nil }, nil
} }