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
var DrandCoeffs = []string{
"a2a34cf9a6be2f66b5385caa520364f994ae7dbac08371ffaca575dfb3e04c8e149b32dc78f077322c613a151dc07440",
"b0c5baca062191f13099229c9a229a9946204f74fc28baa212745243553ab1f50b581b2086e24374ceb40fe34bd23ca2",
"a9c6449cf647e0a0ffaf1e01277e2821213c80310165990daf77610208abfa0ce56c7e40995e26aff3873c624362ca78",
"82c279cce744450e68de98ee08f9698a01dd38f8e3be3c53f2b840fb9d09ad62a0b6b87981e179e1b14bc9a2d284c985",
"82d51308ad346c686f81b8094551597d7b963295cbf313401a93df9baf52d5ae98a87745bee70839a4d6e65c342bd15b",
"94eebfd53f4ba6a3b8304236400a12e73885e5a781509a5c8d41d2e8b476923d8ea6052649b3c17282f596217f96c5de",
"8dc4231e42b4edf39e86ef1579401692480647918275da767d3e558c520d6375ad953530610fd27daf110187877a65d0",
}

View File

@ -2,6 +2,7 @@ package drand
import (
"context"
"math/rand"
"sync"
"time"
@ -23,7 +24,13 @@ import (
var log = logging.Logger("drand")
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
@ -51,7 +58,10 @@ func (dp *drandPeer) IsTLS() bool {
type DrandBeacon struct {
client dnet.Client
peers []dnet.Peer
peers []dnet.Peer
peersIndex int
peersIndexMtx sync.Mutex
pubkey *dkey.DistPublic
@ -78,7 +88,9 @@ func NewDrandBeacon(genesisTs, interval uint64) (*DrandBeacon, error) {
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 {
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.
// 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
}
func (db *DrandBeacon) rotatePeersIndex() {
db.peersIndexMtx.Lock()
defer db.peersIndexMtx.Unlock()
db.peersIndex = (db.peersIndex + 1) % len(db.peers)
}
func (db *DrandBeacon) handleStreamingUpdates() {
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 {
log.Warnf("failed to get public rand stream: %s", err)
log.Warnf("trying again in 10 seconds")
db.rotatePeersIndex()
time.Sleep(time.Second * 10)
continue
}
@ -122,7 +141,9 @@ func (db *DrandBeacon) handleStreamingUpdates() {
Data: e.Signature,
})
}
log.Warn("drand beacon stream broke, reconnecting in 10 seconds")
db.rotatePeersIndex()
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)
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
if err != nil {
db.rotatePeersIndex()
br.Err = err
} else {
br.Entry.Round = resp.GetRound()