diff --git a/beacon/beacon.go b/beacon/beacon.go index f399181..02336ec 100644 --- a/beacon/beacon.go +++ b/beacon/beacon.go @@ -10,58 +10,56 @@ 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) - if parentBeacon != currBeacon { - if len(t.BeaconEntries) != 2 { - 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 +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)) } + 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 - 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,31 +86,29 @@ 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 { - // Fork logic - round := currBeacon.MaxBeaconRoundForEpoch(epoch) - 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 +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) + 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 } - beacon := bSchedule.BeaconForEpoch(epoch) + beacon := beaconNetworks.BeaconNetworkForEpoch(epoch) start := lib.Clock.Now() diff --git a/consensus/miner.go b/consensus/miner.go index 545cd09..430c97c 100644 --- a/consensus/miner.go +++ b/consensus/miner.go @@ -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) } diff --git a/drand/drand.go b/drand/drand.go index c896a46..21665ea 100644 --- a/drand/drand.go +++ b/drand/drand.go @@ -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) diff --git a/node/beacon.go b/node/beacon.go index c9e8880..79c3196 100644 --- a/node/beacon.go +++ b/node/beacon.go @@ -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 }