2020-10-23 13:35:30 +00:00
|
|
|
package drand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
"github.com/Arceliar/phony"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/consensus"
|
|
|
|
|
2020-10-28 17:35:56 +00:00
|
|
|
"github.com/Secured-Finance/dione/beacon"
|
2020-10-23 13:35:30 +00:00
|
|
|
"github.com/drand/drand/chain"
|
|
|
|
"github.com/drand/drand/client"
|
|
|
|
httpClient "github.com/drand/drand/client/http"
|
|
|
|
libp2pClient "github.com/drand/drand/lp2p/client"
|
|
|
|
"github.com/drand/kyber"
|
2020-11-23 16:53:44 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2020-10-23 13:35:30 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-11-23 16:53:44 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2020-10-23 13:35:30 +00:00
|
|
|
|
2020-11-23 16:53:44 +00:00
|
|
|
dlog "github.com/drand/drand/log"
|
|
|
|
kzap "github.com/go-kit/kit/log/zap"
|
2020-10-23 13:35:30 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/config"
|
|
|
|
"github.com/Secured-Finance/dione/lib"
|
2020-10-28 17:35:56 +00:00
|
|
|
types "github.com/Secured-Finance/dione/types"
|
2020-10-23 13:35:30 +00:00
|
|
|
)
|
|
|
|
|
2021-04-30 19:55:12 +00:00
|
|
|
var log = logrus.WithFields(logrus.Fields{
|
|
|
|
"subsystem": "drand",
|
|
|
|
})
|
2020-11-23 16:53:44 +00:00
|
|
|
|
2020-10-23 13:35:30 +00:00
|
|
|
type DrandBeacon struct {
|
2021-06-08 21:30:23 +00:00
|
|
|
phony.Inbox
|
2021-06-04 21:07:04 +00:00
|
|
|
DrandClient client.Client
|
|
|
|
PublicKey kyber.Point
|
|
|
|
drandResultChannel <-chan client.Result
|
2021-06-08 21:30:23 +00:00
|
|
|
cacheLock sync.Mutex
|
|
|
|
localCache map[uint64]types.BeaconEntry
|
|
|
|
latestDrandRound uint64
|
|
|
|
consensusManager *consensus.PBFTConsensusManager
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 21:30:23 +00:00
|
|
|
func NewDrandBeacon(ps *pubsub.PubSub, pcm *consensus.PBFTConsensusManager) (*DrandBeacon, error) {
|
2020-10-23 13:35:30 +00:00
|
|
|
cfg := config.NewDrandConfig()
|
|
|
|
|
|
|
|
drandChain, err := chain.InfoFromJSON(bytes.NewReader([]byte(cfg.ChainInfo)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to unmarshal drand chain info: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:53:44 +00:00
|
|
|
dlogger := dlog.NewKitLoggerFrom(kzap.NewZapSugarLogger(
|
2021-04-30 19:55:12 +00:00
|
|
|
logging.Logger("drand").SugaredLogger.Desugar(), zapcore.InfoLevel))
|
2020-11-23 16:53:44 +00:00
|
|
|
|
2020-10-23 13:35:30 +00:00
|
|
|
var clients []client.Client
|
|
|
|
for _, url := range cfg.Servers {
|
|
|
|
client, err := httpClient.NewWithInfo(url, drandChain, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create http drand client: %w", err)
|
|
|
|
}
|
|
|
|
clients = append(clients, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := []client.Option{
|
|
|
|
client.WithChainInfo(drandChain),
|
|
|
|
client.WithCacheSize(1024),
|
|
|
|
client.WithAutoWatch(),
|
2020-11-23 16:53:44 +00:00
|
|
|
client.WithLogger(dlogger),
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ps != nil {
|
|
|
|
opts = append(opts, libp2pClient.WithPubsub(ps))
|
|
|
|
} else {
|
2021-04-30 19:55:12 +00:00
|
|
|
log.Info("Initiated drand with PubSub")
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drandClient, err := client.Wrap(clients, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Couldn't create Drand clients")
|
|
|
|
}
|
|
|
|
|
|
|
|
db := &DrandBeacon{
|
2021-06-08 21:30:23 +00:00
|
|
|
DrandClient: drandClient,
|
|
|
|
localCache: make(map[uint64]types.BeaconEntry),
|
|
|
|
consensusManager: pcm,
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.PublicKey = drandChain.PublicKey
|
2021-06-04 21:07:04 +00:00
|
|
|
|
|
|
|
db.drandResultChannel = db.DrandClient.Watch(context.TODO())
|
2021-06-04 21:14:24 +00:00
|
|
|
err = db.getLatestDrandResult()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-04 21:07:04 +00:00
|
|
|
go db.loop(context.TODO())
|
2020-10-23 13:35:30 +00:00
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2021-06-04 21:14:24 +00:00
|
|
|
func (db *DrandBeacon) getLatestDrandResult() error {
|
|
|
|
latestDround, err := db.DrandClient.Get(context.TODO(), 0)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to get latest drand round: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
db.cacheValue(newBeaconResultFromDrandResult(latestDround))
|
|
|
|
db.updateLatestDrandRound(latestDround.Round())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-04 21:07:04 +00:00
|
|
|
func (db *DrandBeacon) loop(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
{
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case res := <-db.drandResultChannel:
|
|
|
|
{
|
2021-06-04 21:14:24 +00:00
|
|
|
db.cacheValue(newBeaconResultFromDrandResult(res))
|
|
|
|
db.updateLatestDrandRound(res.Round())
|
2021-06-08 21:30:23 +00:00
|
|
|
db.consensusManager.NewDrandRound(db, res)
|
2021-06-04 21:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:50:06 +00:00
|
|
|
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.BeaconResult {
|
|
|
|
out := make(chan beacon.BeaconResult, 1)
|
2020-10-23 13:35:30 +00:00
|
|
|
if round != 0 {
|
2020-10-28 17:35:56 +00:00
|
|
|
be := db.getCachedValue(round)
|
|
|
|
if be != nil {
|
2020-11-13 17:50:06 +00:00
|
|
|
out <- beacon.BeaconResult{Entry: *be}
|
2020-10-23 13:35:30 +00:00
|
|
|
close(out)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
start := lib.Clock.Now()
|
2021-04-30 19:55:12 +00:00
|
|
|
log.Infof("start fetching randomness: round %v", round)
|
2020-10-23 13:35:30 +00:00
|
|
|
resp, err := db.DrandClient.Get(ctx, round)
|
|
|
|
|
2020-11-13 17:50:06 +00:00
|
|
|
var br beacon.BeaconResult
|
2020-10-23 13:35:30 +00:00
|
|
|
if err != nil {
|
2020-10-28 17:35:56 +00:00
|
|
|
br.Err = fmt.Errorf("drand failed Get request: %w", err)
|
2020-10-23 13:35:30 +00:00
|
|
|
} else {
|
2020-10-28 17:35:56 +00:00
|
|
|
br.Entry.Round = resp.Round()
|
|
|
|
br.Entry.Data = resp.Signature()
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
2021-04-30 19:55:12 +00:00
|
|
|
log.Infof("done fetching randomness: round %v, took %v", round, lib.Clock.Since(start))
|
2020-10-28 17:35:56 +00:00
|
|
|
out <- br
|
2020-10-23 13:35:30 +00:00
|
|
|
close(out)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
2020-10-28 17:35:56 +00:00
|
|
|
func (db *DrandBeacon) cacheValue(res types.BeaconEntry) {
|
2020-10-23 13:35:30 +00:00
|
|
|
db.cacheLock.Lock()
|
|
|
|
defer db.cacheLock.Unlock()
|
|
|
|
db.localCache[res.Round] = res
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:35:56 +00:00
|
|
|
func (db *DrandBeacon) getCachedValue(round uint64) *types.BeaconEntry {
|
2020-10-23 13:35:30 +00:00
|
|
|
db.cacheLock.Lock()
|
|
|
|
defer db.cacheLock.Unlock()
|
|
|
|
v, ok := db.localCache[round]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
2021-06-04 21:14:24 +00:00
|
|
|
func (db *DrandBeacon) updateLatestDrandRound(round uint64) {
|
|
|
|
db.cacheLock.Lock()
|
|
|
|
defer db.cacheLock.Unlock()
|
|
|
|
db.latestDrandRound = round
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:35:56 +00:00
|
|
|
func (db *DrandBeacon) VerifyEntry(curr, prev types.BeaconEntry) error {
|
2020-10-23 13:35:30 +00:00
|
|
|
if prev.Round == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if be := db.getCachedValue(curr.Round); be != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
b := &chain.Beacon{
|
2021-06-04 21:07:04 +00:00
|
|
|
PreviousSig: prev.Metadata["signature"].([]byte),
|
2020-10-23 13:35:30 +00:00
|
|
|
Round: curr.Round,
|
2021-06-04 21:07:04 +00:00
|
|
|
Signature: curr.Metadata["signature"].([]byte),
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
2021-06-04 21:07:04 +00:00
|
|
|
return chain.VerifyBeacon(db.PublicKey, b)
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
2020-10-28 17:35:56 +00:00
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
func (db *DrandBeacon) LatestBeaconRound() uint64 {
|
2021-06-04 21:14:24 +00:00
|
|
|
db.cacheLock.Lock()
|
|
|
|
defer db.cacheLock.Unlock()
|
|
|
|
return db.latestDrandRound
|
|
|
|
}
|
|
|
|
|
|
|
|
func newBeaconResultFromDrandResult(res client.Result) types.BeaconEntry {
|
|
|
|
return types.NewBeaconEntry(res.Round(), res.Randomness(), map[string]interface{}{"signature": res.Signature()})
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 17:50:06 +00:00
|
|
|
var _ beacon.BeaconAPI = (*DrandBeacon)(nil)
|