New drand network

This commit is contained in:
Travis Person 2020-04-30 04:47:12 +00:00
parent fce2ec0470
commit a29f19e482
2 changed files with 32 additions and 9 deletions

View File

@ -100,7 +100,8 @@ const BlsSignatureCacheSize = 40000
const BlockMessageLimit = 512 const BlockMessageLimit = 512
var DrandCoeffs = []string{ var DrandCoeffs = []string{
"a2a34cf9a6be2f66b5385caa520364f994ae7dbac08371ffaca575dfb3e04c8e149b32dc78f077322c613a151dc07440", "82c279cce744450e68de98ee08f9698a01dd38f8e3be3c53f2b840fb9d09ad62a0b6b87981e179e1b14bc9a2d284c985",
"b0c5baca062191f13099229c9a229a9946204f74fc28baa212745243553ab1f50b581b2086e24374ceb40fe34bd23ca2", "82d51308ad346c686f81b8094551597d7b963295cbf313401a93df9baf52d5ae98a87745bee70839a4d6e65c342bd15b",
"a9c6449cf647e0a0ffaf1e01277e2821213c80310165990daf77610208abfa0ce56c7e40995e26aff3873c624362ca78", "94eebfd53f4ba6a3b8304236400a12e73885e5a781509a5c8d41d2e8b476923d8ea6052649b3c17282f596217f96c5de",
"8dc4231e42b4edf39e86ef1579401692480647918275da767d3e558c520d6375ad953530610fd27daf110187877a65d0",
} }

View File

@ -2,6 +2,7 @@ package drand
import ( import (
"context" "context"
"math/rand"
"sync" "sync"
"time" "time"
@ -23,7 +24,13 @@ import (
var log = logging.Logger("drand") var log = logging.Logger("drand")
var drandServers = []string{ var drandServers = []string{
"drand-test3.nikkolasg.xyz:5003", "nicolas.drand.fil-test.net:443",
"philipp.drand.fil-test.net:443",
"mathilde.drand.fil-test.net:443",
"ludovic.drand.fil-test.net:443",
"gabbi.drand.fil-test.net:443",
"linus.drand.fil-test.net:443",
"jeff.drand.fil-test.net:443",
} }
var drandPubKey *dkey.DistPublic var drandPubKey *dkey.DistPublic
@ -51,7 +58,10 @@ func (dp *drandPeer) IsTLS() bool {
type DrandBeacon struct { type DrandBeacon struct {
client dnet.Client client dnet.Client
peers []dnet.Peer
peers []dnet.Peer
peersIndex int
peersIndexMtx sync.Mutex
pubkey *dkey.DistPublic pubkey *dkey.DistPublic
@ -78,7 +88,9 @@ func NewDrandBeacon(genesisTs, interval uint64) (*DrandBeacon, error) {
db.peers = append(db.peers, &drandPeer{addr: ds, tls: true}) db.peers = append(db.peers, &drandPeer{addr: ds, tls: true})
} }
groupResp, err := db.client.Group(context.TODO(), db.peers[0], &dproto.GroupRequest{}) db.peersIndex = rand.Intn(len(db.peers))
groupResp, err := db.client.Group(context.TODO(), db.peers[db.peersIndex], &dproto.GroupRequest{})
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to get group response from beacon peer: %w", err) return nil, xerrors.Errorf("failed to get group response from beacon peer: %w", err)
} }
@ -101,17 +113,24 @@ func NewDrandBeacon(genesisTs, interval uint64) (*DrandBeacon, error) {
// TODO: the stream currently gives you back *all* values since drand genesis. // TODO: the stream currently gives you back *all* values since drand genesis.
// Having the stream in the background is merely an optimization, so not a big deal to disable it for now // Having the stream in the background is merely an optimization, so not a big deal to disable it for now
//go db.handleStreamingUpdates() // go db.handleStreamingUpdates()
return db, nil return db, nil
} }
func (db *DrandBeacon) rotatePeersIndex() {
db.peersIndexMtx.Lock()
defer db.peersIndexMtx.Unlock()
db.peersIndex = (db.peersIndex + 1) % len(db.peers)
}
func (db *DrandBeacon) handleStreamingUpdates() { func (db *DrandBeacon) handleStreamingUpdates() {
for { for {
ch, err := db.client.PublicRandStream(context.Background(), db.peers[0], &dproto.PublicRandRequest{}) ch, err := db.client.PublicRandStream(context.Background(), db.peers[db.peersIndex], &dproto.PublicRandRequest{})
if err != nil { if err != nil {
log.Warnf("failed to get public rand stream: %s", err) log.Warnf("failed to get public rand stream: %s", err)
log.Warnf("trying again in 10 seconds") log.Warnf("trying again in 10 seconds")
db.rotatePeersIndex()
time.Sleep(time.Second * 10) time.Sleep(time.Second * 10)
continue continue
} }
@ -122,7 +141,9 @@ func (db *DrandBeacon) handleStreamingUpdates() {
Data: e.Signature, Data: e.Signature,
}) })
} }
log.Warn("drand beacon stream broke, reconnecting in 10 seconds") log.Warn("drand beacon stream broke, reconnecting in 10 seconds")
db.rotatePeersIndex()
time.Sleep(time.Second * 10) time.Sleep(time.Second * 10)
} }
} }
@ -140,10 +161,11 @@ func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Re
out := make(chan beacon.Response, 1) out := make(chan beacon.Response, 1)
go func() { go func() {
resp, err := db.client.PublicRand(ctx, db.peers[0], &dproto.PublicRandRequest{Round: round}) resp, err := db.client.PublicRand(ctx, db.peers[db.peersIndex], &dproto.PublicRandRequest{Round: round})
var br beacon.Response var br beacon.Response
if err != nil { if err != nil {
db.rotatePeersIndex()
br.Err = err br.Err = err
} else { } else {
br.Entry.Round = resp.GetRound() br.Entry.Round = resp.GetRound()