2020-04-09 02:55:17 +00:00
|
|
|
package drand
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-09 12:39:51 +00:00
|
|
|
"sync"
|
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"
|
|
|
|
|
|
|
|
dnet "github.com/drand/drand/net"
|
|
|
|
dproto "github.com/drand/drand/protobuf/drand"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DrandBeacon struct {
|
|
|
|
client dnet.Client
|
|
|
|
|
2020-04-09 12:39:51 +00:00
|
|
|
cacheLk sync.Mutex
|
2020-04-09 02:55:17 +00:00
|
|
|
localCache map[int64]types.BeaconEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDrandBeacon() *DrandBeacon {
|
2020-04-09 12:39:51 +00:00
|
|
|
return &DrandBeacon{
|
|
|
|
client: dnet.NewGrpcClient(),
|
|
|
|
localCache: make(map[int64]types.BeaconEntry),
|
|
|
|
}
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 12:39:51 +00:00
|
|
|
//func (db *DrandBeacon)
|
2020-04-09 02:55:17 +00:00
|
|
|
|
|
|
|
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Response {
|
|
|
|
// check cache, it it if there, otherwise query the endpoint
|
2020-04-09 12:39:51 +00:00
|
|
|
resp, err := db.client.PublicRand(nil, &dproto.PublicRandRequest{Round: round})
|
2020-04-09 02:55:17 +00:00
|
|
|
|
2020-04-09 17:13:09 +00:00
|
|
|
var br beacon.Response
|
|
|
|
if err != nil {
|
|
|
|
br.Err = err
|
|
|
|
} else {
|
|
|
|
br.Entry.Round = resp.GetRound()
|
|
|
|
br.Entry.Data = resp.GetSignature()
|
|
|
|
}
|
|
|
|
|
|
|
|
out := make(chan beacon.Response, 1)
|
|
|
|
out <- br
|
2020-04-09 17:35:43 +00:00
|
|
|
close(out)
|
2020-04-09 17:13:09 +00:00
|
|
|
|
|
|
|
return out
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DrandBeacon) VerifyEntry(types.BeaconEntry, types.BeaconEntry) error {
|
2020-04-09 12:39:51 +00:00
|
|
|
return nil
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *DrandBeacon) MaxBeaconRoundForEpoch(abi.ChainEpoch, types.BeaconEntry) uint64 {
|
|
|
|
// this is just some local math
|
2020-04-09 12:39:51 +00:00
|
|
|
return 0
|
2020-04-09 02:55:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ beacon.RandomBeacon = (*DrandBeacon)(nil)
|