2020-10-23 13:35:30 +00:00
|
|
|
package drand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
2020-10-28 17:35:56 +00:00
|
|
|
"github.com/Secured-Finance/dione/beacon"
|
2021-08-24 19:41:40 +00:00
|
|
|
"github.com/asaskevich/EventBus"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
type DrandBeacon struct {
|
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
|
2021-07-15 20:51:22 +00:00
|
|
|
bus EventBus.Bus
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 20:51:22 +00:00
|
|
|
func NewDrandBeacon(ps *pubsub.PubSub, bus EventBus.Bus) (*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))
|
|
|
|
}
|
|
|
|
|
|
|
|
drandClient, err := client.Wrap(clients, opts...)
|
|
|
|
if err != nil {
|
2021-08-24 19:41:40 +00:00
|
|
|
logrus.Fatal(fmt.Errorf("cannot create drand client: %w", err))
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db := &DrandBeacon{
|
2021-07-11 23:23:00 +00:00
|
|
|
DrandClient: drandClient,
|
|
|
|
localCache: make(map[uint64]types.BeaconEntry),
|
2021-07-15 20:51:22 +00:00
|
|
|
bus: bus,
|
2021-07-21 21:56:58 +00:00
|
|
|
PublicKey: drandChain.PublicKey,
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:41:40 +00:00
|
|
|
logrus.Info("DRAND beacon subsystem has been initialized!")
|
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
return db, nil
|
|
|
|
}
|
2021-06-04 21:07:04 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
func (db *DrandBeacon) Run(ctx context.Context) error {
|
|
|
|
db.drandResultChannel = db.DrandClient.Watch(ctx)
|
|
|
|
err := db.getLatestDrandResult()
|
2021-06-04 21:14:24 +00:00
|
|
|
if err != nil {
|
2021-07-21 21:56:58 +00:00
|
|
|
return err
|
2021-06-04 21:14:24 +00:00
|
|
|
}
|
2021-07-21 21:56:58 +00:00
|
|
|
go db.loop(ctx)
|
2020-10-23 13:35:30 +00:00
|
|
|
|
2021-07-21 21:56:58 +00:00
|
|
|
return nil
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 21:14:24 +00:00
|
|
|
func (db *DrandBeacon) getLatestDrandResult() error {
|
|
|
|
latestDround, err := db.DrandClient.Get(context.TODO(), 0)
|
|
|
|
if err != nil {
|
2021-08-24 19:41:40 +00:00
|
|
|
logrus.Errorf("failed to get latest drand round: %v", err)
|
2021-06-04 21:14:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-11 23:23:00 +00:00
|
|
|
db.cacheValue(newBeaconEntryFromDrandResult(latestDround))
|
2021-06-04 21:14:24 +00:00
|
|
|
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():
|
|
|
|
{
|
2021-07-21 21:56:58 +00:00
|
|
|
logrus.Debug("Stopping watching new DRAND entries...")
|
2021-06-04 21:07:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case res := <-db.drandResultChannel:
|
|
|
|
{
|
2021-07-11 23:23:00 +00:00
|
|
|
db.cacheValue(newBeaconEntryFromDrandResult(res))
|
2021-06-04 21:14:24 +00:00
|
|
|
db.updateLatestDrandRound(res.Round())
|
2021-07-15 20:51:22 +00:00
|
|
|
db.bus.Publish("beacon:newEntry", types.NewBeaconEntry(res.Round(), res.Randomness(), map[string]interface{}{"signature": res.Signature()}))
|
2021-06-04 21:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 23:23:00 +00:00
|
|
|
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) (types.BeaconEntry, error) {
|
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 {
|
2021-07-11 23:23:00 +00:00
|
|
|
return *be, nil
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 23:23:00 +00:00
|
|
|
start := lib.Clock.Now()
|
2021-08-24 19:41:40 +00:00
|
|
|
logrus.Infof("start fetching randomness: round %v", round)
|
2021-07-11 23:23:00 +00:00
|
|
|
resp, err := db.DrandClient.Get(ctx, round)
|
|
|
|
if err != nil {
|
|
|
|
return types.BeaconEntry{}, fmt.Errorf("drand failed Get request: %w", err)
|
|
|
|
}
|
2021-08-24 19:41:40 +00:00
|
|
|
logrus.Infof("done fetching randomness: round %v, took %v", round, lib.Clock.Since(start))
|
2021-07-11 23:23:00 +00:00
|
|
|
return newBeaconEntryFromDrandResult(resp), nil
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-11 23:23:00 +00:00
|
|
|
func newBeaconEntryFromDrandResult(res client.Result) types.BeaconEntry {
|
2021-06-04 21:14:24 +00:00
|
|
|
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)
|