2020-04-09 02:55:17 +00:00
|
|
|
package drand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-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-04-14 14:52:18 +00:00
|
|
|
dbeacon "github.com/drand/drand/beacon"
|
2020-04-14 03:05:19 +00:00
|
|
|
"github.com/drand/drand/core"
|
|
|
|
dkey "github.com/drand/drand/key"
|
2020-04-09 02:55:17 +00:00
|
|
|
dnet "github.com/drand/drand/net"
|
|
|
|
dproto "github.com/drand/drand/protobuf/drand"
|
|
|
|
)
|
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
var log = logging.Logger("drand")
|
|
|
|
|
|
|
|
var drandServers = []string{
|
|
|
|
"drand-test1.nikkolasg.xyz:5001",
|
|
|
|
}
|
|
|
|
|
2020-04-14 20:34:44 +00:00
|
|
|
var drandPubKey *dkey.DistPublic
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
drandPubKey = new(dkey.DistPublic)
|
|
|
|
err := drandPubKey.FromTOML(&dkey.DistPublicTOML{build.DrandCoeffs})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
client dnet.Client
|
2020-04-14 03:05:19 +00:00
|
|
|
peers []dnet.Peer
|
|
|
|
|
|
|
|
pubkey *dkey.DistPublic
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
db := &DrandBeacon{
|
2020-04-09 12:39:51 +00:00
|
|
|
client: dnet.NewGrpcClient(),
|
2020-04-14 03:05:19 +00:00
|
|
|
localCache: make(map[uint64]types.BeaconEntry),
|
|
|
|
}
|
|
|
|
for _, ds := range drandServers {
|
|
|
|
db.peers = append(db.peers, &drandPeer{addr: ds, tls: true})
|
|
|
|
}
|
|
|
|
|
|
|
|
groupResp, err := db.client.Group(context.TODO(), db.peers[0], &dproto.GroupRequest{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to get group response from beacon peer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kgroup, err := core.ProtoToGroup(groupResp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to parse group response: %w", err)
|
2020-04-09 12:39:51 +00:00
|
|
|
}
|
2020-04-14 03:05:19 +00:00
|
|
|
|
|
|
|
// TODO: verify these values are what we expect them to be
|
2020-04-14 20:34:44 +00:00
|
|
|
if !kgroup.PublicKey.Equal(drandPubKey) {
|
|
|
|
return nil, xerrors.Errorf("public key does not match")
|
|
|
|
}
|
|
|
|
// fmt.Printf("Drand Pubkey:\n%#v\n", kgroup.PublicKey.TOML()) // use to print public key
|
|
|
|
db.pubkey = drandPubKey
|
2020-04-14 03:05:19 +00:00
|
|
|
db.interval = kgroup.Period
|
|
|
|
db.drandGenTime = uint64(kgroup.GenesisTime)
|
|
|
|
db.filRoundTime = interval
|
|
|
|
db.filGenTime = genesisTs
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
return db, nil
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
func (db *DrandBeacon) handleStreamingUpdates() {
|
|
|
|
for {
|
|
|
|
ch, err := db.client.PublicRandStream(context.Background(), db.peers[0], &dproto.PublicRandRequest{})
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed to get public rand stream: %s", err)
|
|
|
|
log.Warnf("trying again in 10 seconds")
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for e := range ch {
|
2020-04-14 17:55:53 +00:00
|
|
|
db.cacheValue(types.BeaconEntry{
|
2020-04-14 03:05:19 +00:00
|
|
|
Round: e.Round,
|
|
|
|
Data: e.Signature,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
log.Warn("drand beacon stream broke, reconnecting in 10 seconds")
|
|
|
|
time.Sleep(time.Second * 10)
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 02:55:17 +00:00
|
|
|
|
|
|
|
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Response {
|
2020-04-14 22:13:52 +00:00
|
|
|
// check cache, it it if there, otherwise query the endpoint
|
2020-04-14 17:55:53 +00:00
|
|
|
cres := db.getCachedValue(round)
|
|
|
|
if cres != nil {
|
|
|
|
out := make(chan beacon.Response, 1)
|
|
|
|
out <- beacon.Response{Entry: *cres}
|
|
|
|
close(out)
|
|
|
|
return out
|
2020-04-09 17:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out := make(chan beacon.Response, 1)
|
2020-04-14 17:55:53 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
resp, err := db.client.PublicRand(ctx, db.peers[0], &dproto.PublicRandRequest{Round: round})
|
|
|
|
|
|
|
|
var br beacon.Response
|
|
|
|
if err != nil {
|
|
|
|
br.Err = err
|
|
|
|
} else {
|
|
|
|
br.Entry.Round = resp.GetRound()
|
|
|
|
br.Entry.Data = resp.GetSignature()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
b := &dbeacon.Beacon{
|
|
|
|
PreviousRound: prev.Round,
|
|
|
|
PreviousSig: prev.Data,
|
|
|
|
Round: curr.Round,
|
|
|
|
Signature: curr.Data,
|
|
|
|
}
|
|
|
|
//log.Warnw("VerifyEntry", "beacon", b)
|
2020-04-14 17:55:53 +00:00
|
|
|
err := dbeacon.VerifyBeacon(db.pubkey.Key(), b)
|
|
|
|
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)
|