2020-10-28 17:35:56 +00:00
|
|
|
package beacon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/lib"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/types"
|
|
|
|
)
|
|
|
|
|
2020-11-13 17:50:06 +00:00
|
|
|
type BeaconResult struct {
|
2020-10-28 17:35:56 +00:00
|
|
|
Entry types.BeaconEntry
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
2020-11-13 17:50:06 +00:00
|
|
|
type BeaconNetworks []BeaconNetwork
|
2020-10-28 17:35:56 +00:00
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
func (bn BeaconNetworks) BeaconNetworkForRound(e types.DrandRound) BeaconAPI {
|
2020-11-13 17:50:06 +00:00
|
|
|
for i := len(bn) - 1; i >= 0; i-- {
|
|
|
|
bp := bn[i]
|
2020-10-28 17:35:56 +00:00
|
|
|
if e >= bp.Start {
|
|
|
|
return bp.Beacon
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 17:50:06 +00:00
|
|
|
return bn[0].Beacon
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 17:50:06 +00:00
|
|
|
type BeaconNetwork struct {
|
2020-11-14 00:03:47 +00:00
|
|
|
Start types.DrandRound
|
2020-11-13 17:50:06 +00:00
|
|
|
Beacon BeaconAPI
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 17:50:06 +00:00
|
|
|
// BeaconAPI represents a system that provides randomness.
|
|
|
|
// Other components interrogate the BeaconAPI to acquire randomness that's
|
2020-10-28 17:35:56 +00:00
|
|
|
// valid for a specific chain epoch. Also to verify beacon entries that have
|
|
|
|
// been posted on chain.
|
2020-11-13 17:50:06 +00:00
|
|
|
type BeaconAPI interface {
|
|
|
|
Entry(context.Context, uint64) <-chan BeaconResult
|
2020-10-28 17:35:56 +00:00
|
|
|
VerifyEntry(types.BeaconEntry, types.BeaconEntry) error
|
2020-11-14 00:03:47 +00:00
|
|
|
LatestBeaconRound() uint64
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
func ValidateTaskBeacons(beaconNetworks BeaconNetworks, t *types.DioneTask, prevEpoch types.DrandRound, prevEntry types.BeaconEntry) error {
|
|
|
|
parentBeacon := beaconNetworks.BeaconNetworkForRound(prevEpoch)
|
|
|
|
currBeacon := beaconNetworks.BeaconNetworkForRound(t.DrandRound)
|
2020-11-13 17:50:06 +00:00
|
|
|
if parentBeacon != currBeacon {
|
|
|
|
if len(t.BeaconEntries) != 2 {
|
|
|
|
return fmt.Errorf("expected two beacon entries at beacon fork, got %d", len(t.BeaconEntries))
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
2020-11-13 17:50:06 +00:00
|
|
|
err := currBeacon.VerifyEntry(t.BeaconEntries[1], t.BeaconEntries[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("beacon at fork point invalid: (%v, %v): %w",
|
|
|
|
t.BeaconEntries[1], t.BeaconEntries[0], err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: fork logic
|
2020-11-14 00:03:47 +00:00
|
|
|
bNetwork := beaconNetworks.BeaconNetworkForRound(t.DrandRound)
|
|
|
|
if uint64(t.DrandRound) == prevEntry.Round {
|
2020-10-28 17:35:56 +00:00
|
|
|
if len(t.BeaconEntries) != 0 {
|
|
|
|
return fmt.Errorf("expected not to have any beacon entries in this task, got %d", len(t.BeaconEntries))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(t.BeaconEntries) == 0 {
|
|
|
|
return fmt.Errorf("expected to have beacon entries in this task, but didn't find any")
|
|
|
|
}
|
|
|
|
|
|
|
|
last := t.BeaconEntries[len(t.BeaconEntries)-1]
|
2020-11-14 00:03:47 +00:00
|
|
|
if last.Round != uint64(t.DrandRound) {
|
|
|
|
return fmt.Errorf("expected final beacon entry in task to be at round %d, got %d", uint64(t.DrandRound), last.Round)
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i, e := range t.BeaconEntries {
|
2020-11-13 17:50:06 +00:00
|
|
|
if err := bNetwork.VerifyEntry(e, prevEntry); err != nil {
|
2020-10-28 17:35:56 +00:00
|
|
|
return fmt.Errorf("beacon entry %d (%d - %x (%d)) was invalid: %w", i, e.Round, e.Data, len(e.Data), err)
|
|
|
|
}
|
|
|
|
prevEntry = e
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
func BeaconEntriesForTask(ctx context.Context, beaconNetworks BeaconNetworks) ([]types.BeaconEntry, error) {
|
|
|
|
beacon := beaconNetworks.BeaconNetworkForRound(0)
|
|
|
|
round := beacon.LatestBeaconRound()
|
|
|
|
|
|
|
|
//prevBeacon := beaconNetworks.BeaconNetworkForRound(prevRound)
|
|
|
|
//currBeacon := beaconNetworks.BeaconNetworkForRound(round)
|
|
|
|
//if prevBeacon != currBeacon {
|
|
|
|
// // Fork logic
|
|
|
|
// round := currBeacon.LatestBeaconRound()
|
|
|
|
// out := make([]types.BeaconEntry, 2)
|
|
|
|
// rch := currBeacon.Entry(ctx, round-1)
|
|
|
|
// res := <-rch
|
|
|
|
// if res.Err != nil {
|
|
|
|
// return nil, fmt.Errorf("getting entry %d returned error: %w", round-1, res.Err)
|
|
|
|
// }
|
|
|
|
// out[0] = res.Entry
|
|
|
|
// rch = currBeacon.Entry(ctx, round)
|
|
|
|
// res = <-rch
|
|
|
|
// if res.Err != nil {
|
|
|
|
// return nil, fmt.Errorf("getting entry %d returned error: %w", round, res.Err)
|
|
|
|
// }
|
|
|
|
// out[1] = res.Entry
|
|
|
|
// return out, nil
|
|
|
|
//}
|
2020-10-28 17:35:56 +00:00
|
|
|
|
|
|
|
start := lib.Clock.Now()
|
|
|
|
|
2020-11-14 00:03:47 +00:00
|
|
|
//if round == prev.Round {
|
|
|
|
// return nil, nil
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// TODO: this is a sketchy way to handle the genesis block not having a beacon entry
|
|
|
|
//if prev.Round == 0 {
|
|
|
|
// prev.Round = round - 1
|
|
|
|
//}
|
|
|
|
|
|
|
|
out := make([]types.BeaconEntry, 2)
|
|
|
|
rch := beacon.Entry(ctx, round-1)
|
|
|
|
res := <-rch
|
|
|
|
if res.Err != nil {
|
|
|
|
return nil, fmt.Errorf("getting entry %d returned error: %w", round-1, res.Err)
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
2020-11-14 00:03:47 +00:00
|
|
|
out[0] = res.Entry
|
|
|
|
rch = beacon.Entry(ctx, round)
|
|
|
|
res = <-rch
|
|
|
|
if res.Err != nil {
|
|
|
|
return nil, fmt.Errorf("getting entry %d returned error: %w", round, res.Err)
|
2020-10-28 17:35:56 +00:00
|
|
|
}
|
2020-11-14 00:03:47 +00:00
|
|
|
out[1] = res.Entry
|
2020-10-28 17:35:56 +00:00
|
|
|
|
2021-04-30 19:55:12 +00:00
|
|
|
logrus.Debugf("fetching beacon entries: took %v, count of entries: %v", lib.Clock.Since(start), len(out))
|
2020-11-14 00:03:47 +00:00
|
|
|
//reverse(out)
|
2020-10-28 17:35:56 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func reverse(arr []types.BeaconEntry) {
|
|
|
|
for i := 0; i < len(arr)/2; i++ {
|
|
|
|
arr[i], arr[len(arr)-(1+i)] = arr[len(arr)-(1+i)], arr[i]
|
|
|
|
}
|
|
|
|
}
|