Slightly refactor Beacon system - rename strange names in this subsystem to fit the context

This commit is contained in:
ChronosX88 2020-11-13 21:50:06 +04:00
parent 1ea482c379
commit 89f3bcda90
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
4 changed files with 57 additions and 61 deletions

View File

@ -10,58 +10,56 @@ import (
"github.com/Secured-Finance/dione/types" "github.com/Secured-Finance/dione/types"
) )
type Response struct { type BeaconResult struct {
Entry types.BeaconEntry Entry types.BeaconEntry
Err error Err error
} }
type Queue []BeaconPoint type BeaconNetworks []BeaconNetwork
func (bs Queue) BeaconForEpoch(e types.TaskEpoch) RandomBeacon { func (bn BeaconNetworks) BeaconNetworkForEpoch(e types.TaskEpoch) BeaconAPI {
for i := len(bs) - 1; i >= 0; i-- { for i := len(bn) - 1; i >= 0; i-- {
bp := bs[i] bp := bn[i]
if e >= bp.Start { if e >= bp.Start {
return bp.Beacon return bp.Beacon
} }
} }
return bs[0].Beacon return bn[0].Beacon
} }
type BeaconPoint struct { type BeaconNetwork struct {
Start types.TaskEpoch Start types.TaskEpoch
Beacon RandomBeacon Beacon BeaconAPI
} }
// RandomBeacon represents a system that provides randomness. // BeaconAPI represents a system that provides randomness.
// Other components interrogate the RandomBeacon to acquire randomness that's // Other components interrogate the BeaconAPI to acquire randomness that's
// valid for a specific chain epoch. Also to verify beacon entries that have // valid for a specific chain epoch. Also to verify beacon entries that have
// been posted on chain. // been posted on chain.
type RandomBeacon interface { type BeaconAPI interface {
Entry(context.Context, uint64) <-chan Response Entry(context.Context, uint64) <-chan BeaconResult
VerifyEntry(types.BeaconEntry, types.BeaconEntry) error VerifyEntry(types.BeaconEntry, types.BeaconEntry) error
MaxBeaconRoundForEpoch(types.TaskEpoch) uint64 MaxBeaconRoundForEpoch(types.TaskEpoch) uint64
} }
func ValidateTaskValues(bQueue Queue, t *types.DioneTask, parentEpoch types.TaskEpoch, prevEntry types.BeaconEntry) error { func ValidateTaskBeacons(beaconNetworks BeaconNetworks, t *types.DioneTask, prevEpoch types.TaskEpoch, prevEntry types.BeaconEntry) error {
{ parentBeacon := beaconNetworks.BeaconNetworkForEpoch(prevEpoch)
parentBeacon := bQueue.BeaconForEpoch(parentEpoch) currBeacon := beaconNetworks.BeaconNetworkForEpoch(t.Epoch)
currBeacon := bQueue.BeaconForEpoch(t.Epoch) if parentBeacon != currBeacon {
if parentBeacon != currBeacon { if len(t.BeaconEntries) != 2 {
if len(t.BeaconEntries) != 2 { return fmt.Errorf("expected two beacon entries at beacon fork, got %d", len(t.BeaconEntries))
return fmt.Errorf("expected two beacon entries at beacon fork, got %d", len(t.BeaconEntries))
}
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
} }
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
} }
// TODO: fork logic // TODO: fork logic
b := bQueue.BeaconForEpoch(t.Epoch) bNetwork := beaconNetworks.BeaconNetworkForEpoch(t.Epoch)
maxRound := b.MaxBeaconRoundForEpoch(t.Epoch) maxRound := bNetwork.MaxBeaconRoundForEpoch(t.Epoch)
if maxRound == prevEntry.Round { if maxRound == prevEntry.Round {
if len(t.BeaconEntries) != 0 { if len(t.BeaconEntries) != 0 {
return fmt.Errorf("expected not to have any beacon entries in this task, got %d", len(t.BeaconEntries)) 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 { 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) return fmt.Errorf("beacon entry %d (%d - %x (%d)) was invalid: %w", i, e.Round, e.Data, len(e.Data), err)
} }
prevEntry = e prevEntry = e
@ -88,31 +86,29 @@ func ValidateTaskValues(bQueue Queue, t *types.DioneTask, parentEpoch types.Task
return nil return nil
} }
func BeaconEntriesForTask(ctx context.Context, bSchedule Queue, epoch types.TaskEpoch, parentEpoch types.TaskEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) { func BeaconEntriesForTask(ctx context.Context, beaconNetworks BeaconNetworks, epoch types.TaskEpoch, prevEpoch types.TaskEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) {
{ prevBeacon := beaconNetworks.BeaconNetworkForEpoch(prevEpoch)
parentBeacon := bSchedule.BeaconForEpoch(parentEpoch) currBeacon := beaconNetworks.BeaconNetworkForEpoch(epoch)
currBeacon := bSchedule.BeaconForEpoch(epoch) if prevBeacon != currBeacon {
if parentBeacon != currBeacon { // Fork logic
// Fork logic round := currBeacon.MaxBeaconRoundForEpoch(epoch)
round := currBeacon.MaxBeaconRoundForEpoch(epoch) out := make([]types.BeaconEntry, 2)
out := make([]types.BeaconEntry, 2) rch := currBeacon.Entry(ctx, round-1)
rch := currBeacon.Entry(ctx, round-1) res := <-rch
res := <-rch if res.Err != nil {
if res.Err != nil { return nil, fmt.Errorf("getting entry %d returned error: %w", round-1, res.Err)
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
} }
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
} }
beacon := bSchedule.BeaconForEpoch(epoch) beacon := beaconNetworks.BeaconNetworkForEpoch(epoch)
start := lib.Clock.Now() start := lib.Clock.Now()

View File

@ -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) mStake, err := c.GetMinerStake(miner)
if err != nil { if err != nil {
@ -94,7 +94,7 @@ func (m *Miner) MineTask(ctx context.Context, base *MiningBase, mb *MinerBase, e
rbase = bvals[len(bvals)-1] 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) return nil, xerrors.Errorf("failed to update miner stake: %w", err)
} }

View File

@ -94,12 +94,12 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub) (*DrandBeacon
return db, nil return db, nil
} }
func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.Response { func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan beacon.BeaconResult {
out := make(chan beacon.Response, 1) out := make(chan beacon.BeaconResult, 1)
if round != 0 { if round != 0 {
be := db.getCachedValue(round) be := db.getCachedValue(round)
if be != nil { if be != nil {
out <- beacon.Response{Entry: *be} out <- beacon.BeaconResult{Entry: *be}
close(out) close(out)
return 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) logrus.Info("start fetching randomness", "round", round)
resp, err := db.DrandClient.Get(ctx, round) resp, err := db.DrandClient.Get(ctx, round)
var br beacon.Response var br beacon.BeaconResult
if err != nil { if err != nil {
br.Err = fmt.Errorf("drand failed Get request: %w", err) br.Err = fmt.Errorf("drand failed Get request: %w", err)
} else { } else {
@ -171,4 +171,4 @@ func (db *DrandBeacon) MaxBeaconRoundForEpoch(taskEpoch types.TaskEpoch) uint64
return dround return dround
} }
var _ beacon.RandomBeacon = (*DrandBeacon)(nil) var _ beacon.BeaconAPI = (*DrandBeacon)(nil)

View File

@ -9,13 +9,13 @@ import (
) )
// NewBeaconQueue creates a new beacon chain schedule // NewBeaconQueue creates a new beacon chain schedule
func (n *Node) NewBeaconQueue() (beacon.Queue, error) { func (n *Node) NewBeaconQueue() (beacon.BeaconNetworks, error) {
schedule := beacon.Queue{} schedule := beacon.BeaconNetworks{}
bc, err := drand.NewDrandBeacon(config.ChainGenesis, config.TaskEpochInterval, n.PubSubRouter.Pubsub) bc, err := drand.NewDrandBeacon(config.ChainGenesis, config.TaskEpochInterval, n.PubSubRouter.Pubsub)
if err != nil { if err != nil {
return nil, fmt.Errorf("creating drand beacon: %w", err) 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 return schedule, nil
} }