Introduce beacon Schedule

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-09-08 22:28:06 +02:00
parent 9e9856bb45
commit a51a466adf
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA
6 changed files with 39 additions and 15 deletions

View File

@ -18,6 +18,23 @@ type Response struct {
Err error Err error
} }
type Schedule []BeaconPoint
func (bs Schedule) BeaconForEpoch(e abi.ChainEpoch) RandomBeacon {
for i := len(bs) - 1; i >= 0; i-- {
bp := bs[i]
if e > bp.Start {
return bp.Beacon
}
}
return bs[0].Beacon
}
type BeaconPoint struct {
Start abi.ChainEpoch
Beacon RandomBeacon
}
// RandomBeacon represents a system that provides randomness to Lotus. // RandomBeacon represents a system that provides randomness to Lotus.
// Other components interrogate the RandomBeacon to acquire randomness that's // Other components interrogate the RandomBeacon 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
@ -28,7 +45,11 @@ type RandomBeacon interface {
MaxBeaconRoundForEpoch(abi.ChainEpoch, types.BeaconEntry) uint64 MaxBeaconRoundForEpoch(abi.ChainEpoch, types.BeaconEntry) uint64
} }
func ValidateBlockValues(b RandomBeacon, h *types.BlockHeader, prevEntry types.BeaconEntry) error { func ValidateBlockValues(bSchedule Schedule, h *types.BlockHeader, parentEpoch abi.ChainEpoch,
prevEntry types.BeaconEntry) error {
// TODO: fork logic
b := bSchedule.BeaconForEpoch(h.Height)
maxRound := b.MaxBeaconRoundForEpoch(h.Height, prevEntry) maxRound := b.MaxBeaconRoundForEpoch(h.Height, prevEntry)
if maxRound == prevEntry.Round { if maxRound == prevEntry.Round {
if len(h.BeaconEntries) != 0 { if len(h.BeaconEntries) != 0 {
@ -56,10 +77,13 @@ func ValidateBlockValues(b RandomBeacon, h *types.BlockHeader, prevEntry types.B
return nil return nil
} }
func BeaconEntriesForBlock(ctx context.Context, beacon RandomBeacon, round abi.ChainEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) { func BeaconEntriesForBlock(ctx context.Context, bSchedule Schedule, epoch abi.ChainEpoch, parentEpoch abi.ChainEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) {
// TODO: fork logic
beacon := bSchedule.BeaconForEpoch(epoch)
start := build.Clock.Now() start := build.Clock.Now()
maxRound := beacon.MaxBeaconRoundForEpoch(round, prev) maxRound := beacon.MaxBeaconRoundForEpoch(epoch, prev)
if maxRound == prev.Round { if maxRound == prev.Round {
return nil, nil return nil, nil
} }
@ -82,7 +106,7 @@ func BeaconEntriesForBlock(ctx context.Context, beacon RandomBeacon, round abi.C
out = append(out, resp.Entry) out = append(out, resp.Entry)
cur = resp.Entry.Round - 1 cur = resp.Entry.Round - 1
case <-ctx.Done(): case <-ctx.Done():
return nil, xerrors.Errorf("context timed out waiting on beacon entry to come back for round %d: %w", round, ctx.Err()) return nil, xerrors.Errorf("context timed out waiting on beacon entry to come back for epoch %d: %w", epoch, ctx.Err())
} }
} }

View File

@ -59,7 +59,7 @@ type ChainGen struct {
cs *store.ChainStore cs *store.ChainStore
beacon beacon.RandomBeacon beacon beacon.Schedule
sm *stmgr.StateManager sm *stmgr.StateManager
@ -252,7 +252,7 @@ func NewGeneratorWithSectors(numSectors int) (*ChainGen, error) {
miners := []address.Address{maddr1, maddr2} miners := []address.Address{maddr1, maddr2}
beac := beacon.NewMockBeacon(time.Second) beac := beacon.Schedule{{Start: 0, Beacon: beacon.NewMockBeacon(time.Second)}}
//beac, err := drand.NewDrandBeacon(tpl.Timestamp, build.BlockDelaySecs) //beac, err := drand.NewDrandBeacon(tpl.Timestamp, build.BlockDelaySecs)
//if err != nil { //if err != nil {
//return nil, xerrors.Errorf("creating drand beacon: %w", err) //return nil, xerrors.Errorf("creating drand beacon: %w", err)
@ -338,7 +338,7 @@ func (cg *ChainGen) nextBlockProof(ctx context.Context, pts *types.TipSet, m add
prev := mbi.PrevBeaconEntry prev := mbi.PrevBeaconEntry
entries, err := beacon.BeaconEntriesForBlock(ctx, cg.beacon, round, prev) entries, err := beacon.BeaconEntriesForBlock(ctx, cg.beacon, round, pts.Height(), prev)
if err != nil { if err != nil {
return nil, nil, nil, xerrors.Errorf("get beacon entries for block: %w", err) return nil, nil, nil, xerrors.Errorf("get beacon entries for block: %w", err)
} }
@ -559,7 +559,7 @@ type mca struct {
w *wallet.Wallet w *wallet.Wallet
sm *stmgr.StateManager sm *stmgr.StateManager
pv ffiwrapper.Verifier pv ffiwrapper.Verifier
bcn beacon.RandomBeacon bcn beacon.Schedule
} }
func (mca mca) ChainGetRandomnessFromTickets(ctx context.Context, tsk types.TipSetKey, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) { func (mca mca) ChainGetRandomnessFromTickets(ctx context.Context, tsk types.TipSetKey, personalization crypto.DomainSeparationTag, randEpoch abi.ChainEpoch, entropy []byte) (abi.Randomness, error) {

View File

@ -502,7 +502,7 @@ func GetLookbackTipSetForRound(ctx context.Context, sm *StateManager, ts *types.
return lbts, nil return lbts, nil
} }
func MinerGetBaseInfo(ctx context.Context, sm *StateManager, bcn beacon.RandomBeacon, tsk types.TipSetKey, round abi.ChainEpoch, maddr address.Address, pv ffiwrapper.Verifier) (*api.MiningBaseInfo, error) { func MinerGetBaseInfo(ctx context.Context, sm *StateManager, bcs beacon.Schedule, tsk types.TipSetKey, round abi.ChainEpoch, maddr address.Address, pv ffiwrapper.Verifier) (*api.MiningBaseInfo, error) {
ts, err := sm.ChainStore().LoadTipSet(tsk) ts, err := sm.ChainStore().LoadTipSet(tsk)
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to load tipset for mining base: %w", err) return nil, xerrors.Errorf("failed to load tipset for mining base: %w", err)
@ -517,7 +517,7 @@ func MinerGetBaseInfo(ctx context.Context, sm *StateManager, bcn beacon.RandomBe
prev = &types.BeaconEntry{} prev = &types.BeaconEntry{}
} }
entries, err := beacon.BeaconEntriesForBlock(ctx, bcn, round, *prev) entries, err := beacon.BeaconEntriesForBlock(ctx, bcs, round, ts.Height(), *prev)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -103,7 +103,7 @@ type Syncer struct {
store *store.ChainStore store *store.ChainStore
// handle to the random beacon for verification // handle to the random beacon for verification
beacon beacon.RandomBeacon beacon beacon.Schedule
// the state manager handles making state queries // the state manager handles making state queries
sm *stmgr.StateManager sm *stmgr.StateManager
@ -141,7 +141,7 @@ type Syncer struct {
} }
// NewSyncer creates a new Syncer object. // NewSyncer creates a new Syncer object.
func NewSyncer(ds dtypes.MetadataDS, sm *stmgr.StateManager, exchange exchange.Client, connmgr connmgr.ConnManager, self peer.ID, beacon beacon.RandomBeacon, verifier ffiwrapper.Verifier) (*Syncer, error) { func NewSyncer(ds dtypes.MetadataDS, sm *stmgr.StateManager, exchange exchange.Client, connmgr connmgr.ConnManager, self peer.ID, beacon beacon.Schedule, verifier ffiwrapper.Verifier) (*Syncer, error) {
gen, err := sm.ChainStore().GetGenesis() gen, err := sm.ChainStore().GetGenesis()
if err != nil { if err != nil {
return nil, xerrors.Errorf("getting genesis block: %w", err) return nil, xerrors.Errorf("getting genesis block: %w", err)
@ -879,7 +879,7 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) (er
return nil return nil
} }
if err := beacon.ValidateBlockValues(syncer.beacon, h, *prevBeacon); err != nil { if err := beacon.ValidateBlockValues(syncer.beacon, h, baseTs.Height(), *prevBeacon); err != nil {
return xerrors.Errorf("failed to validate blocks random beacon values: %w", err) return xerrors.Errorf("failed to validate blocks random beacon values: %w", err)
} }
return nil return nil

View File

@ -53,7 +53,7 @@ type StateAPI struct {
ProofVerifier ffiwrapper.Verifier ProofVerifier ffiwrapper.Verifier
StateManager *stmgr.StateManager StateManager *stmgr.StateManager
Chain *store.ChainStore Chain *store.ChainStore
Beacon beacon.RandomBeacon Beacon beacon.Schedule
} }
func (a *StateAPI) StateNetworkName(ctx context.Context) (dtypes.NetworkName, error) { func (a *StateAPI) StateNetworkName(ctx context.Context) (dtypes.NetworkName, error) {

View File

@ -163,7 +163,7 @@ func NetworkName(mctx helpers.MetricsCtx, lc fx.Lifecycle, cs *store.ChainStore,
return netName, err return netName, err
} }
func NewSyncer(lc fx.Lifecycle, ds dtypes.MetadataDS, sm *stmgr.StateManager, exchange exchange.Client, h host.Host, beacon beacon.RandomBeacon, verifier ffiwrapper.Verifier) (*chain.Syncer, error) { func NewSyncer(lc fx.Lifecycle, ds dtypes.MetadataDS, sm *stmgr.StateManager, exchange exchange.Client, h host.Host, beacon beacon.Schedule, verifier ffiwrapper.Verifier) (*chain.Syncer, error) {
syncer, err := chain.NewSyncer(ds, sm, exchange, h.ConnManager(), h.ID(), beacon, verifier) syncer, err := chain.NewSyncer(ds, sm, exchange, h.ConnManager(), h.ID(), beacon, verifier)
if err != nil { if err != nil {
return nil, err return nil, err