Slightly refactor Beacon system - rename strange names in this subsystem to fit the context
This commit is contained in:
parent
1ea482c379
commit
89f3bcda90
@ -10,42 +10,41 @@ import (
|
||||
"github.com/Secured-Finance/dione/types"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
type BeaconResult struct {
|
||||
Entry types.BeaconEntry
|
||||
Err error
|
||||
}
|
||||
|
||||
type Queue []BeaconPoint
|
||||
type BeaconNetworks []BeaconNetwork
|
||||
|
||||
func (bs Queue) BeaconForEpoch(e types.TaskEpoch) RandomBeacon {
|
||||
for i := len(bs) - 1; i >= 0; i-- {
|
||||
bp := bs[i]
|
||||
func (bn BeaconNetworks) BeaconNetworkForEpoch(e types.TaskEpoch) BeaconAPI {
|
||||
for i := len(bn) - 1; i >= 0; i-- {
|
||||
bp := bn[i]
|
||||
if e >= bp.Start {
|
||||
return bp.Beacon
|
||||
}
|
||||
}
|
||||
return bs[0].Beacon
|
||||
return bn[0].Beacon
|
||||
}
|
||||
|
||||
type BeaconPoint struct {
|
||||
type BeaconNetwork struct {
|
||||
Start types.TaskEpoch
|
||||
Beacon RandomBeacon
|
||||
Beacon BeaconAPI
|
||||
}
|
||||
|
||||
// RandomBeacon represents a system that provides randomness.
|
||||
// Other components interrogate the RandomBeacon to acquire randomness that's
|
||||
// BeaconAPI represents a system that provides randomness.
|
||||
// Other components interrogate the BeaconAPI to acquire randomness that's
|
||||
// valid for a specific chain epoch. Also to verify beacon entries that have
|
||||
// been posted on chain.
|
||||
type RandomBeacon interface {
|
||||
Entry(context.Context, uint64) <-chan Response
|
||||
type BeaconAPI interface {
|
||||
Entry(context.Context, uint64) <-chan BeaconResult
|
||||
VerifyEntry(types.BeaconEntry, types.BeaconEntry) error
|
||||
MaxBeaconRoundForEpoch(types.TaskEpoch) uint64
|
||||
}
|
||||
|
||||
func ValidateTaskValues(bQueue Queue, t *types.DioneTask, parentEpoch types.TaskEpoch, prevEntry types.BeaconEntry) error {
|
||||
{
|
||||
parentBeacon := bQueue.BeaconForEpoch(parentEpoch)
|
||||
currBeacon := bQueue.BeaconForEpoch(t.Epoch)
|
||||
func ValidateTaskBeacons(beaconNetworks BeaconNetworks, t *types.DioneTask, prevEpoch types.TaskEpoch, prevEntry types.BeaconEntry) error {
|
||||
parentBeacon := beaconNetworks.BeaconNetworkForEpoch(prevEpoch)
|
||||
currBeacon := beaconNetworks.BeaconNetworkForEpoch(t.Epoch)
|
||||
if parentBeacon != currBeacon {
|
||||
if len(t.BeaconEntries) != 2 {
|
||||
return fmt.Errorf("expected two beacon entries at beacon fork, got %d", len(t.BeaconEntries))
|
||||
@ -57,11 +56,10 @@ func ValidateTaskValues(bQueue Queue, t *types.DioneTask, parentEpoch types.Task
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: fork logic
|
||||
b := bQueue.BeaconForEpoch(t.Epoch)
|
||||
maxRound := b.MaxBeaconRoundForEpoch(t.Epoch)
|
||||
bNetwork := beaconNetworks.BeaconNetworkForEpoch(t.Epoch)
|
||||
maxRound := bNetwork.MaxBeaconRoundForEpoch(t.Epoch)
|
||||
if maxRound == prevEntry.Round {
|
||||
if len(t.BeaconEntries) != 0 {
|
||||
return fmt.Errorf("expected not to have any beacon entries in this task, got %d", len(t.BeaconEntries))
|
||||
@ -79,7 +77,7 @@ func ValidateTaskValues(bQueue Queue, t *types.DioneTask, parentEpoch types.Task
|
||||
}
|
||||
|
||||
for i, e := range t.BeaconEntries {
|
||||
if err := b.VerifyEntry(e, prevEntry); err != nil {
|
||||
if err := bNetwork.VerifyEntry(e, prevEntry); err != nil {
|
||||
return fmt.Errorf("beacon entry %d (%d - %x (%d)) was invalid: %w", i, e.Round, e.Data, len(e.Data), err)
|
||||
}
|
||||
prevEntry = e
|
||||
@ -88,11 +86,10 @@ func ValidateTaskValues(bQueue Queue, t *types.DioneTask, parentEpoch types.Task
|
||||
return nil
|
||||
}
|
||||
|
||||
func BeaconEntriesForTask(ctx context.Context, bSchedule Queue, epoch types.TaskEpoch, parentEpoch types.TaskEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) {
|
||||
{
|
||||
parentBeacon := bSchedule.BeaconForEpoch(parentEpoch)
|
||||
currBeacon := bSchedule.BeaconForEpoch(epoch)
|
||||
if parentBeacon != currBeacon {
|
||||
func BeaconEntriesForTask(ctx context.Context, beaconNetworks BeaconNetworks, epoch types.TaskEpoch, prevEpoch types.TaskEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) {
|
||||
prevBeacon := beaconNetworks.BeaconNetworkForEpoch(prevEpoch)
|
||||
currBeacon := beaconNetworks.BeaconNetworkForEpoch(epoch)
|
||||
if prevBeacon != currBeacon {
|
||||
// Fork logic
|
||||
round := currBeacon.MaxBeaconRoundForEpoch(epoch)
|
||||
out := make([]types.BeaconEntry, 2)
|
||||
@ -110,9 +107,8 @@ func BeaconEntriesForTask(ctx context.Context, bSchedule Queue, epoch types.Task
|
||||
out[1] = res.Entry
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
|
||||
beacon := bSchedule.BeaconForEpoch(epoch)
|
||||
beacon := beaconNetworks.BeaconNetworkForEpoch(epoch)
|
||||
|
||||
start := lib.Clock.Now()
|
||||
|
||||
|
@ -59,7 +59,7 @@ func NewMiningBase() *MiningBase {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MinerBase) UpdateStake(c *ethclient.EthereumClient, miner common.Address) error {
|
||||
func (m *MinerBase) UpdateCurrentStakeInfo(c *ethclient.EthereumClient, miner common.Address) error {
|
||||
mStake, err := c.GetMinerStake(miner)
|
||||
|
||||
if err != nil {
|
||||
@ -94,7 +94,7 @@ func (m *Miner) MineTask(ctx context.Context, base *MiningBase, mb *MinerBase, e
|
||||
rbase = bvals[len(bvals)-1]
|
||||
}
|
||||
|
||||
if err := mb.UpdateStake(ethClient, m.ethAddress); err != nil {
|
||||
if err := mb.UpdateCurrentStakeInfo(ethClient, m.ethAddress); err != nil {
|
||||
return nil, xerrors.Errorf("failed to update miner stake: %w", err)
|
||||
}
|
||||
|
||||
|
@ -94,12 +94,12 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub) (*DrandBeacon
|
||||
return db, nil
|
||||
}
|
||||
|
||||
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Response {
|
||||
out := make(chan beacon.Response, 1)
|
||||
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.BeaconResult {
|
||||
out := make(chan beacon.BeaconResult, 1)
|
||||
if round != 0 {
|
||||
be := db.getCachedValue(round)
|
||||
if be != nil {
|
||||
out <- beacon.Response{Entry: *be}
|
||||
out <- beacon.BeaconResult{Entry: *be}
|
||||
close(out)
|
||||
return out
|
||||
}
|
||||
@ -110,7 +110,7 @@ func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Re
|
||||
logrus.Info("start fetching randomness", "round", round)
|
||||
resp, err := db.DrandClient.Get(ctx, round)
|
||||
|
||||
var br beacon.Response
|
||||
var br beacon.BeaconResult
|
||||
if err != nil {
|
||||
br.Err = fmt.Errorf("drand failed Get request: %w", err)
|
||||
} else {
|
||||
@ -171,4 +171,4 @@ func (db *DrandBeacon) MaxBeaconRoundForEpoch(taskEpoch types.TaskEpoch) uint64
|
||||
return dround
|
||||
}
|
||||
|
||||
var _ beacon.RandomBeacon = (*DrandBeacon)(nil)
|
||||
var _ beacon.BeaconAPI = (*DrandBeacon)(nil)
|
||||
|
@ -9,13 +9,13 @@ import (
|
||||
)
|
||||
|
||||
// NewBeaconQueue creates a new beacon chain schedule
|
||||
func (n *Node) NewBeaconQueue() (beacon.Queue, error) {
|
||||
schedule := beacon.Queue{}
|
||||
func (n *Node) NewBeaconQueue() (beacon.BeaconNetworks, error) {
|
||||
schedule := beacon.BeaconNetworks{}
|
||||
bc, err := drand.NewDrandBeacon(config.ChainGenesis, config.TaskEpochInterval, n.PubSubRouter.Pubsub)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating drand beacon: %w", err)
|
||||
}
|
||||
schedule = append(schedule, beacon.BeaconPoint{Start: 0, Beacon: bc})
|
||||
schedule = append(schedule, beacon.BeaconNetwork{Start: 0, Beacon: bc})
|
||||
|
||||
return schedule, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user