2020-10-23 13:35:30 +00:00
|
|
|
package drand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2020-10-28 17:35:56 +00:00
|
|
|
// DrandRes structure representing response from drand network
|
2020-10-23 13:35:30 +00:00
|
|
|
type DrandRes struct {
|
|
|
|
// PreviousSig is the previous signature generated
|
|
|
|
PreviousSig []byte
|
|
|
|
// Round is the round number this beacon is tied to
|
|
|
|
Round uint64
|
|
|
|
// Signature is the BLS deterministic signature over Round || PreviousRand
|
|
|
|
Signature []byte
|
|
|
|
// Randomness for specific round generated by Drand
|
|
|
|
Randomness []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type DrandBeacon struct {
|
|
|
|
DrandClient client.Client
|
|
|
|
PublicKey kyber.Point
|
|
|
|
Interval time.Duration
|
2020-10-28 17:35:56 +00:00
|
|
|
chainGenesisTime uint64
|
|
|
|
chainRoundTime uint64
|
|
|
|
|
2020-10-23 13:35:30 +00:00
|
|
|
drandGenesisTime uint64
|
|
|
|
cacheLock sync.Mutex
|
2020-10-28 17:35:56 +00:00
|
|
|
localCache map[uint64]types.BeaconEntry
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 17:35:56 +00:00
|
|
|
func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub) (*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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if ps != nil {
|
|
|
|
opts = append(opts, libp2pClient.WithPubsub(ps))
|
|
|
|
} else {
|
|
|
|
logrus.Info("Initiated drand with PubSub")
|
|
|
|
}
|
|
|
|
|
|
|
|
drandClient, err := client.Wrap(clients, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Couldn't create Drand clients")
|
|
|
|
}
|
|
|
|
|
|
|
|
db := &DrandBeacon{
|
|
|
|
DrandClient: drandClient,
|
2020-10-28 17:35:56 +00:00
|
|
|
localCache: make(map[uint64]types.BeaconEntry),
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
db.PublicKey = drandChain.PublicKey
|
|
|
|
db.Interval = drandChain.Period
|
|
|
|
db.drandGenesisTime = uint64(drandChain.GenesisTime)
|
2020-10-28 17:35:56 +00:00
|
|
|
db.chainRoundTime = interval
|
|
|
|
db.chainGenesisTime = genesisTs
|
2020-10-23 13:35:30 +00:00
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:35:56 +00:00
|
|
|
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Response {
|
|
|
|
out := make(chan beacon.Response, 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 {
|
|
|
|
out <- beacon.Response{Entry: *be}
|
2020-10-23 13:35:30 +00:00
|
|
|
close(out)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
start := lib.Clock.Now()
|
|
|
|
logrus.Info("start fetching randomness", "round", round)
|
|
|
|
resp, err := db.DrandClient.Get(ctx, round)
|
|
|
|
|
2020-10-28 17:35:56 +00:00
|
|
|
var br beacon.Response
|
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
|
|
|
}
|
|
|
|
logrus.Info("done fetching randomness", "round", round, "took", 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
|
|
|
|
}
|
|
|
|
|
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{
|
2020-10-28 17:35:56 +00:00
|
|
|
PreviousSig: prev.Data,
|
2020-10-23 13:35:30 +00:00
|
|
|
Round: curr.Round,
|
2020-10-28 17:35:56 +00:00
|
|
|
Signature: curr.Data,
|
2020-10-23 13:35:30 +00:00
|
|
|
}
|
|
|
|
err := chain.VerifyBeacon(db.PublicKey, b)
|
|
|
|
if err == nil {
|
|
|
|
db.cacheValue(curr)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2020-10-28 17:35:56 +00:00
|
|
|
|
|
|
|
func (db *DrandBeacon) MaxBeaconRoundForEpoch(taskEpoch types.TaskEpoch) uint64 {
|
|
|
|
var latestTs uint64
|
|
|
|
if taskEpoch == 0 {
|
|
|
|
latestTs = db.chainGenesisTime
|
|
|
|
} else {
|
|
|
|
latestTs = ((uint64(taskEpoch) * db.chainRoundTime) + db.chainGenesisTime) - db.chainRoundTime
|
|
|
|
}
|
|
|
|
|
|
|
|
dround := (latestTs - db.drandGenesisTime) / uint64(db.Interval.Seconds())
|
|
|
|
return dround
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ beacon.RandomBeacon = (*DrandBeacon)(nil)
|