2020-04-09 02:55:17 +00:00
|
|
|
package drand
|
|
|
|
|
|
|
|
import (
|
2020-05-22 14:07:38 +00:00
|
|
|
"bytes"
|
2020-04-09 02:55:17 +00:00
|
|
|
"context"
|
2020-04-09 12:39:51 +00:00
|
|
|
"sync"
|
2020-04-14 03:05:19 +00:00
|
|
|
"time"
|
2020-04-09 02:55:17 +00:00
|
|
|
|
2020-05-22 14:07:38 +00:00
|
|
|
"github.com/drand/kyber"
|
2020-04-14 20:34:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-04-09 02:55:17 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/beacon"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-04-14 03:05:19 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-04-09 02:55:17 +00:00
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
|
2020-05-22 14:07:38 +00:00
|
|
|
dchain "github.com/drand/drand/chain"
|
|
|
|
dclient "github.com/drand/drand/client"
|
2020-04-09 02:55:17 +00:00
|
|
|
)
|
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
var log = logging.Logger("drand")
|
|
|
|
|
|
|
|
var drandServers = []string{
|
2020-05-22 14:07:38 +00:00
|
|
|
"https://pl-eu.testnet.drand.sh",
|
|
|
|
"https://pl-us.testnet.drand.sh",
|
|
|
|
"https://pl-sin.testnet.drand.sh",
|
2020-04-14 03:05:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 14:07:38 +00:00
|
|
|
var drandChain *dchain.Info
|
2020-04-14 20:34:44 +00:00
|
|
|
|
|
|
|
func init() {
|
2020-05-22 14:07:38 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
drandChain, err = dchain.InfoFromJSON(bytes.NewReader([]byte(build.DrandChain)))
|
2020-04-14 20:34:44 +00:00
|
|
|
if err != nil {
|
2020-05-22 14:07:38 +00:00
|
|
|
panic("could not unmarshal chain info: " + err.Error())
|
2020-04-14 20:34:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
type drandPeer struct {
|
|
|
|
addr string
|
|
|
|
tls bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *drandPeer) Address() string {
|
|
|
|
return dp.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dp *drandPeer) IsTLS() bool {
|
|
|
|
return dp.tls
|
|
|
|
}
|
|
|
|
|
2020-04-09 02:55:17 +00:00
|
|
|
type DrandBeacon struct {
|
2020-05-22 14:07:38 +00:00
|
|
|
client dclient.Client
|
2020-04-14 03:05:19 +00:00
|
|
|
|
2020-05-22 14:07:38 +00:00
|
|
|
pubkey kyber.Point
|
2020-04-14 03:05:19 +00:00
|
|
|
|
|
|
|
// seconds
|
|
|
|
interval time.Duration
|
|
|
|
|
|
|
|
drandGenTime uint64
|
|
|
|
filGenTime uint64
|
|
|
|
filRoundTime uint64
|
2020-04-09 02:55:17 +00:00
|
|
|
|
2020-04-09 12:39:51 +00:00
|
|
|
cacheLk sync.Mutex
|
2020-04-14 03:05:19 +00:00
|
|
|
localCache map[uint64]types.BeaconEntry
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
func NewDrandBeacon(genesisTs, interval uint64) (*DrandBeacon, error) {
|
|
|
|
if genesisTs == 0 {
|
|
|
|
panic("what are you doing this cant be zero")
|
|
|
|
}
|
2020-05-22 14:07:38 +00:00
|
|
|
client, err := dclient.New(
|
|
|
|
dclient.WithHTTPEndpoints(drandServers),
|
|
|
|
dclient.WithChainInfo(drandChain),
|
|
|
|
dclient.WithCacheSize(1024),
|
|
|
|
)
|
2020-04-14 03:05:19 +00:00
|
|
|
if err != nil {
|
2020-05-22 14:07:38 +00:00
|
|
|
return nil, xerrors.Errorf("creating drand client")
|
2020-04-14 03:05:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 14:07:38 +00:00
|
|
|
db := &DrandBeacon{
|
|
|
|
client: client,
|
|
|
|
localCache: make(map[uint64]types.BeaconEntry),
|
2020-04-09 12:39:51 +00:00
|
|
|
}
|
2020-04-14 03:05:19 +00:00
|
|
|
|
2020-05-22 14:07:38 +00:00
|
|
|
db.pubkey = drandChain.PublicKey
|
|
|
|
db.interval = drandChain.Period
|
|
|
|
db.drandGenTime = uint64(drandChain.GenesisTime)
|
2020-04-14 03:05:19 +00:00
|
|
|
db.filRoundTime = interval
|
|
|
|
db.filGenTime = genesisTs
|
|
|
|
|
|
|
|
return db, nil
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Response {
|
2020-04-09 17:13:09 +00:00
|
|
|
out := make(chan beacon.Response, 1)
|
2020-05-22 14:07:38 +00:00
|
|
|
if round != 0 {
|
|
|
|
be := db.getCachedValue(round)
|
|
|
|
if be != nil {
|
|
|
|
out <- beacon.Response{Entry: *be}
|
|
|
|
close(out)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 17:55:53 +00:00
|
|
|
|
|
|
|
go func() {
|
2020-05-27 14:16:29 +00:00
|
|
|
log.Infow("fetching randomness", "round", round)
|
2020-05-22 14:07:38 +00:00
|
|
|
resp, err := db.client.Get(ctx, round)
|
2020-04-14 17:55:53 +00:00
|
|
|
|
|
|
|
var br beacon.Response
|
|
|
|
if err != nil {
|
2020-05-22 14:07:38 +00:00
|
|
|
br.Err = xerrors.Errorf("drand failed Get request: %w", err)
|
2020-04-14 17:55:53 +00:00
|
|
|
} else {
|
2020-05-22 14:07:38 +00:00
|
|
|
br.Entry.Round = resp.Round()
|
|
|
|
br.Entry.Data = resp.Signature()
|
2020-04-14 17:55:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out <- br
|
|
|
|
close(out)
|
|
|
|
}()
|
2020-04-09 17:13:09 +00:00
|
|
|
|
|
|
|
return out
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
2020-04-14 17:55:53 +00:00
|
|
|
func (db *DrandBeacon) cacheValue(e types.BeaconEntry) {
|
2020-04-14 03:05:19 +00:00
|
|
|
db.cacheLk.Lock()
|
|
|
|
defer db.cacheLk.Unlock()
|
2020-04-14 17:55:53 +00:00
|
|
|
db.localCache[e.Round] = e
|
2020-04-14 03:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DrandBeacon) getCachedValue(round uint64) *types.BeaconEntry {
|
|
|
|
db.cacheLk.Lock()
|
|
|
|
defer db.cacheLk.Unlock()
|
|
|
|
v, ok := db.localCache[round]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &v
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:52:18 +00:00
|
|
|
func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntry) error {
|
|
|
|
if prev.Round == 0 {
|
|
|
|
// TODO handle genesis better
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-22 14:07:38 +00:00
|
|
|
b := &dchain.Beacon{
|
2020-04-30 02:53:24 +00:00
|
|
|
PreviousSig: prev.Data,
|
|
|
|
Round: curr.Round,
|
|
|
|
Signature: curr.Data,
|
2020-04-14 14:52:18 +00:00
|
|
|
}
|
2020-05-22 14:07:38 +00:00
|
|
|
err := dchain.VerifyBeacon(db.pubkey, b)
|
2020-04-14 17:55:53 +00:00
|
|
|
if err == nil {
|
|
|
|
db.cacheValue(curr)
|
|
|
|
}
|
|
|
|
return err
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
func (db *DrandBeacon) MaxBeaconRoundForEpoch(filEpoch abi.ChainEpoch, prevEntry types.BeaconEntry) uint64 {
|
|
|
|
// TODO: sometimes the genesis time for filecoin is zero and this goes negative
|
|
|
|
latestTs := ((uint64(filEpoch) * db.filRoundTime) + db.filGenTime) - db.filRoundTime
|
|
|
|
dround := (latestTs - db.drandGenTime) / uint64(db.interval.Seconds())
|
|
|
|
return dround
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ beacon.RandomBeacon = (*DrandBeacon)(nil)
|