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-14 03:05:19 +00:00
|
|
|
"time"
|
2020-04-09 02:55:17 +00:00
|
|
|
|
2020-05-29 13:46:05 +00:00
|
|
|
dchain "github.com/drand/drand/chain"
|
|
|
|
dclient "github.com/drand/drand/client"
|
2020-06-10 18:17:03 +00:00
|
|
|
hclient "github.com/drand/drand/client/http"
|
2024-01-25 00:40:46 +00:00
|
|
|
dcrypto "github.com/drand/drand/crypto"
|
2020-05-29 13:46:05 +00:00
|
|
|
dlog "github.com/drand/drand/log"
|
2020-06-10 18:17:03 +00:00
|
|
|
gclient "github.com/drand/drand/lp2p/client"
|
2020-05-22 14:07:38 +00:00
|
|
|
"github.com/drand/kyber"
|
2023-03-13 22:29:09 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
2021-04-06 13:04:32 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2020-05-29 13:46:05 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2023-03-07 19:43:23 +00:00
|
|
|
"go.uber.org/zap"
|
2022-06-14 15:00:51 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-04-14 03:05:19 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2020-06-23 19:56:03 +00:00
|
|
|
|
2020-07-10 14:43:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-05-29 13:46:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/beacon"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-06-23 20:23:06 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2020-04-09 02:55:17 +00:00
|
|
|
)
|
|
|
|
|
2020-04-14 03:05:19 +00:00
|
|
|
var log = logging.Logger("drand")
|
|
|
|
|
2020-06-23 21:51:25 +00:00
|
|
|
// DrandBeacon connects Lotus with a drand network in order to provide
|
|
|
|
// randomness to the system in a way that's aligned with Filecoin rounds/epochs.
|
|
|
|
//
|
|
|
|
// We connect to drand peers via their public HTTP endpoints. The peers are
|
|
|
|
// enumerated in the drandServers variable.
|
|
|
|
//
|
|
|
|
// The root trust for the Drand chain is configured from build.DrandChain.
|
2020-04-09 02:55:17 +00:00
|
|
|
type DrandBeacon struct {
|
2024-02-28 20:19:00 +00:00
|
|
|
isChained bool
|
|
|
|
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
|
2024-01-25 00:40:46 +00:00
|
|
|
scheme *dcrypto.Scheme
|
2020-04-09 02:55:17 +00:00
|
|
|
|
2023-03-13 22:29:09 +00:00
|
|
|
localCache *lru.Cache[uint64, *types.BeaconEntry]
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 15:26:23 +00:00
|
|
|
// IsChained tells us whether this particular beacon operates in "chained mode". Prior to Drand
|
|
|
|
// quicknet, beacons form a chain. After the introduction of quicknet, they do not, so we need to
|
|
|
|
// change how we interact with beacon entries. (See FIP-0063)
|
2024-02-28 20:19:00 +00:00
|
|
|
func (db *DrandBeacon) IsChained() bool {
|
|
|
|
return db.isChained
|
|
|
|
}
|
|
|
|
|
2020-10-21 23:49:59 +00:00
|
|
|
// DrandHTTPClient interface overrides the user agent used by drand
|
|
|
|
type DrandHTTPClient interface {
|
|
|
|
SetUserAgent(string)
|
|
|
|
}
|
|
|
|
|
2023-03-07 19:43:23 +00:00
|
|
|
type logger struct {
|
|
|
|
*zap.SugaredLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) With(args ...interface{}) dlog.Logger {
|
|
|
|
return &logger{l.SugaredLogger.With(args...)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Named(s string) dlog.Logger {
|
|
|
|
return &logger{l.SugaredLogger.Named(s)}
|
|
|
|
}
|
|
|
|
|
2024-01-25 00:40:46 +00:00
|
|
|
func (l *logger) AddCallerSkip(skip int) dlog.Logger {
|
|
|
|
return &logger{l.SugaredLogger.With(zap.AddCallerSkip(skip))}
|
|
|
|
}
|
|
|
|
|
2020-06-23 20:23:06 +00:00
|
|
|
func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes.DrandConfig) (*DrandBeacon, error) {
|
2020-04-14 03:05:19 +00:00
|
|
|
if genesisTs == 0 {
|
|
|
|
panic("what are you doing this cant be zero")
|
|
|
|
}
|
2020-05-29 15:21:10 +00:00
|
|
|
|
2020-06-23 19:56:03 +00:00
|
|
|
drandChain, err := dchain.InfoFromJSON(bytes.NewReader([]byte(config.ChainInfoJSON)))
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("unable to unmarshal drand chain info: %w", err)
|
2020-06-23 17:23:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 18:17:03 +00:00
|
|
|
var clients []dclient.Client
|
2020-06-23 17:23:04 +00:00
|
|
|
for _, url := range config.Servers {
|
2020-06-10 18:17:03 +00:00
|
|
|
hc, err := hclient.NewWithInfo(url, drandChain, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("could not create http drand client: %w", err)
|
|
|
|
}
|
2020-10-21 23:49:59 +00:00
|
|
|
hc.(DrandHTTPClient).SetUserAgent("drand-client-lotus/" + build.BuildVersion)
|
2020-06-10 18:17:03 +00:00
|
|
|
clients = append(clients, hc)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-29 13:46:05 +00:00
|
|
|
opts := []dclient.Option{
|
2020-05-22 14:07:38 +00:00
|
|
|
dclient.WithChainInfo(drandChain),
|
|
|
|
dclient.WithCacheSize(1024),
|
2023-03-07 19:43:23 +00:00
|
|
|
dclient.WithLogger(&logger{&log.SugaredLogger}),
|
2020-05-29 13:46:05 +00:00
|
|
|
}
|
2020-05-29 15:21:10 +00:00
|
|
|
|
2020-05-29 13:46:05 +00:00
|
|
|
if ps != nil {
|
|
|
|
opts = append(opts, gclient.WithPubsub(ps))
|
|
|
|
} else {
|
|
|
|
log.Info("drand beacon without pubsub")
|
|
|
|
}
|
|
|
|
|
2020-06-10 18:17:03 +00:00
|
|
|
client, err := dclient.Wrap(clients, opts...)
|
2020-04-14 03:05:19 +00:00
|
|
|
if err != nil {
|
2022-11-16 02:39:56 +00:00
|
|
|
return nil, xerrors.Errorf("creating drand client: %w", err)
|
2020-04-14 03:05:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-13 22:29:09 +00:00
|
|
|
lc, err := lru.New[uint64, *types.BeaconEntry](1024)
|
2021-04-23 12:29:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-22 14:07:38 +00:00
|
|
|
db := &DrandBeacon{
|
2024-02-28 20:19:00 +00:00
|
|
|
isChained: config.IsChained,
|
2020-05-22 14:07:38 +00:00
|
|
|
client: client,
|
2021-04-23 12:29:46 +00:00
|
|
|
localCache: lc,
|
2020-04-09 12:39:51 +00:00
|
|
|
}
|
2020-04-14 03:05:19 +00:00
|
|
|
|
2024-01-25 00:40:46 +00:00
|
|
|
sch, err := dcrypto.GetSchemeByIDWithDefault(drandChain.Scheme)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
db.scheme = sch
|
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-07-10 14:43:14 +00:00
|
|
|
start := build.Clock.Now()
|
2022-02-09 12:34:55 +00:00
|
|
|
log.Debugw("start 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
|
|
|
}
|
2022-02-09 12:34:55 +00:00
|
|
|
log.Debugw("done fetching randomness", "round", round, "took", build.Clock.Since(start))
|
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) {
|
2023-03-13 22:29:09 +00:00
|
|
|
db.localCache.Add(e.Round, &e)
|
2020-04-14 03:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DrandBeacon) getCachedValue(round uint64) *types.BeaconEntry {
|
2023-03-13 22:29:09 +00:00
|
|
|
v, _ := db.localCache.Get(round)
|
|
|
|
return v
|
2020-04-14 03:05:19 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 00:40:46 +00:00
|
|
|
func (db *DrandBeacon) VerifyEntry(entry types.BeaconEntry, prevEntrySig []byte) error {
|
|
|
|
if be := db.getCachedValue(entry.Round); be != nil {
|
|
|
|
if !bytes.Equal(entry.Data, be.Data) {
|
2021-04-30 07:35:38 +00:00
|
|
|
return xerrors.New("invalid beacon value, does not match cached good value")
|
|
|
|
}
|
2020-08-11 12:00:23 +00:00
|
|
|
// return no error if the value is in the cache already
|
|
|
|
return nil
|
2020-08-11 11:58:27 +00:00
|
|
|
}
|
2020-05-22 14:07:38 +00:00
|
|
|
b := &dchain.Beacon{
|
2024-01-25 00:40:46 +00:00
|
|
|
PreviousSig: prevEntrySig,
|
|
|
|
Round: entry.Round,
|
|
|
|
Signature: entry.Data,
|
2020-04-14 14:52:18 +00:00
|
|
|
}
|
2024-01-25 00:40:46 +00:00
|
|
|
|
|
|
|
err := db.scheme.VerifyBeacon(b, db.pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to verify beacon: %w", err)
|
2020-04-14 17:55:53 +00:00
|
|
|
}
|
2024-01-24 23:35:46 +00:00
|
|
|
|
2024-01-25 00:40:46 +00:00
|
|
|
db.cacheValue(entry)
|
|
|
|
|
2024-01-24 23:35:46 +00:00
|
|
|
return nil
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 01:23:41 +00:00
|
|
|
func (db *DrandBeacon) MaxBeaconRoundForEpoch(nv network.Version, filEpoch abi.ChainEpoch) uint64 {
|
2020-04-14 03:05:19 +00:00
|
|
|
// TODO: sometimes the genesis time for filecoin is zero and this goes negative
|
|
|
|
latestTs := ((uint64(filEpoch) * db.filRoundTime) + db.filGenTime) - db.filRoundTime
|
2021-09-27 01:23:41 +00:00
|
|
|
|
|
|
|
if nv <= network.Version15 {
|
|
|
|
return db.maxBeaconRoundV1(latestTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.maxBeaconRoundV2(latestTs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DrandBeacon) maxBeaconRoundV1(latestTs uint64) uint64 {
|
2020-04-14 03:05:19 +00:00
|
|
|
dround := (latestTs - db.drandGenTime) / uint64(db.interval.Seconds())
|
|
|
|
return dround
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 01:23:41 +00:00
|
|
|
func (db *DrandBeacon) maxBeaconRoundV2(latestTs uint64) uint64 {
|
|
|
|
if latestTs < db.drandGenTime {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fromGenesis := latestTs - db.drandGenTime
|
|
|
|
// we take the time from genesis divided by the periods in seconds, that
|
|
|
|
// gives us the number of periods since genesis. We also add +1 because
|
|
|
|
// round 1 starts at genesis time.
|
|
|
|
return fromGenesis/uint64(db.interval.Seconds()) + 1
|
|
|
|
}
|
|
|
|
|
2020-04-09 02:55:17 +00:00
|
|
|
var _ beacon.RandomBeacon = (*DrandBeacon)(nil)
|
2023-07-15 15:26:18 +00:00
|
|
|
|
|
|
|
func BeaconScheduleFromDrandSchedule(dcs dtypes.DrandSchedule, genesisTime uint64, ps *pubsub.PubSub) (beacon.Schedule, error) {
|
|
|
|
shd := beacon.Schedule{}
|
|
|
|
for _, dc := range dcs {
|
|
|
|
bc, err := NewDrandBeacon(genesisTime, build.BlockDelaySecs, ps, dc.Config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("creating drand beacon: %w", err)
|
|
|
|
}
|
|
|
|
shd = append(shd, beacon.BeaconPoint{Start: dc.Start, Beacon: bc})
|
|
|
|
}
|
|
|
|
|
|
|
|
return shd, nil
|
|
|
|
}
|