Skip to content

Commit a967a36

Browse files
committed
feat: drand: refactor round verification
1 parent 1324822 commit a967a36

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

chain/beacon/beacon.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,21 @@ func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockH
7979
return xerrors.Errorf("expected to have beacon entries in this block, but didn't find any")
8080
}
8181

82+
// Verify that the last beacon entry's round corresponds to the round we expect
8283
last := h.BeaconEntries[len(h.BeaconEntries)-1]
8384
if last.Round != maxRound {
8485
return xerrors.Errorf("expected final beacon entry in block to be at round %d, got %d", maxRound, last.Round)
8586
}
8687

88+
// Verify that all other entries' rounds are as expected for the epochs in between parentEpoch and h.Height
89+
for i, e := range h.BeaconEntries {
90+
correctRound := b.MaxBeaconRoundForEpoch(nv, parentEpoch+abi.ChainEpoch(i)+1)
91+
if e.Round != correctRound {
92+
return xerrors.Errorf("unexpected beacon round %d, expected %d for epoch %d", e.Round, correctRound, parentEpoch+abi.ChainEpoch(i))
93+
}
94+
}
95+
96+
// Verify the beacon entries themselves
8797
for i, e := range h.BeaconEntries {
8898
if err := b.VerifyEntry(e, prevEntry); err != nil {
8999
return xerrors.Errorf("beacon entry %d (%d - %x (%d)) was invalid: %w", i, e.Round, e.Data, len(e.Data), err)
@@ -132,18 +142,17 @@ func BeaconEntriesForBlock(ctx context.Context, bSchedule Schedule, nv network.V
132142
prev.Round = maxRound - 1
133143
}
134144

135-
cur := maxRound
136145
var out []types.BeaconEntry
137-
for cur > prev.Round {
138-
rch := beacon.Entry(ctx, cur)
146+
for currEpoch := epoch; currEpoch > parentEpoch; currEpoch-- {
147+
currRound := beacon.MaxBeaconRoundForEpoch(nv, currEpoch)
148+
rch := beacon.Entry(ctx, currRound)
139149
select {
140150
case resp := <-rch:
141151
if resp.Err != nil {
142152
return nil, xerrors.Errorf("beacon entry request returned error: %w", resp.Err)
143153
}
144154

145155
out = append(out, resp.Entry)
146-
cur = resp.Entry.Round - 1
147156
case <-ctx.Done():
148157
return nil, xerrors.Errorf("context timed out waiting on beacon entry to come back for epoch %d: %w", epoch, ctx.Err())
149158
}

chain/beacon/drand/drand.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@ func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntr
170170
return nil
171171
}
172172

173-
if curr.Round != prev.Round+1 {
174-
return xerrors.Errorf("invalid beacon entry: cur (%d) != prev (%d) + 1", curr.Round, prev.Round)
175-
}
176-
177173
if be := db.getCachedValue(curr.Round); be != nil {
178174
if !bytes.Equal(curr.Data, be.Data) {
179175
return xerrors.New("invalid beacon value, does not match cached good value")
@@ -190,7 +186,8 @@ func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntr
190186
if err == nil {
191187
db.cacheValue(curr)
192188
}
193-
return err
189+
190+
return nil
194191
}
195192

196193
func (db *DrandBeacon) MaxBeaconRoundForEpoch(nv network.Version, filEpoch abi.ChainEpoch) uint64 {

chain/sync.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -704,25 +704,25 @@ func (syncer *Syncer) collectHeaders(ctx context.Context, incoming *types.TipSet
704704
}
705705

706706
{
707-
// ensure consistency of beacon entires
707+
// ensure consistency of beacon entries
708708
targetBE := incoming.Blocks()[0].BeaconEntries
709709
sorted := sort.SliceIsSorted(targetBE, func(i, j int) bool {
710710
return targetBE[i].Round < targetBE[j].Round
711711
})
712712
if !sorted {
713-
syncer.bad.Add(incoming.Cids()[0], NewBadBlockReason(incoming.Cids(), "wrong order of beacon entires"))
714-
return nil, xerrors.Errorf("wrong order of beacon entires")
713+
syncer.bad.Add(incoming.Cids()[0], NewBadBlockReason(incoming.Cids(), "wrong order of beacon entries"))
714+
return nil, xerrors.Errorf("wrong order of beacon entries")
715715
}
716716

717717
for _, bh := range incoming.Blocks()[1:] {
718718
if len(targetBE) != len(bh.BeaconEntries) {
719719
// cannot mark bad, I think @Kubuxu
720-
return nil, xerrors.Errorf("tipset contained different number for beacon entires")
720+
return nil, xerrors.Errorf("tipset contained different number for beacon entries")
721721
}
722722
for i, be := range bh.BeaconEntries {
723723
if targetBE[i].Round != be.Round || !bytes.Equal(targetBE[i].Data, be.Data) {
724724
// cannot mark bad, I think @Kubuxu
725-
return nil, xerrors.Errorf("tipset contained different beacon entires")
725+
return nil, xerrors.Errorf("tipset contained different beacon entries")
726726
}
727727
}
728728

0 commit comments

Comments
 (0)