rename sync variables.

This commit is contained in:
Łukasz Magiera 2020-07-08 22:02:28 +02:00
parent 24d8a84ad7
commit 8fbeebb86d

View File

@ -1069,17 +1069,14 @@ func extractSyncState(ctx context.Context) *SyncerState {
// collectHeaders collects the headers from the blocks between any two tipsets. // collectHeaders collects the headers from the blocks between any two tipsets.
// //
// `from` is the heaviest/projected/target tipset we have learned about, and // `incoming` is the heaviest/projected/target tipset we have learned about, and
// `to` is usually an anchor tipset we already have in our view of the chain // `known` is usually an anchor tipset we already have in our view of the chain
// (which could be the genesis). // (which could be the genesis).
// //
// collectHeaders checks if portions of the chain are in our ChainStore; falling // collectHeaders checks if portions of the chain are in our ChainStore; falling
// down to the network to retrieve the missing parts. If during the process, any // down to the network to retrieve the missing parts. If during the process, any
// portion we receive is in our denylist (bad list), we short-circuit. // portion we receive is in our denylist (bad list), we short-circuit.
// //
// {hint/naming}: `from` and `to` is in inverse order. `from` is the highest,
// and `to` is the lowest. This method traverses the chain backwards.
//
// {hint/usage}: This is used by collectChain, which is in turn called from the // {hint/usage}: This is used by collectChain, which is in turn called from the
// main Sync method (Syncer#Sync), so it's a pretty central method. // main Sync method (Syncer#Sync), so it's a pretty central method.
// //
@ -1089,7 +1086,7 @@ func extractSyncState(ctx context.Context) *SyncerState {
// bad. // bad.
// 2. Check the consistency of beacon entries in the from tipset. We check // 2. Check the consistency of beacon entries in the from tipset. We check
// total equality of the BeaconEntries in each block. // total equality of the BeaconEntries in each block.
// 3. Travers the chain backwards, for each tipset: // 3. Traverse the chain backwards, for each tipset:
// 3a. Load it from the chainstore; if found, it move on to its parent. // 3a. Load it from the chainstore; if found, it move on to its parent.
// 3b. Query our peers via BlockSync in batches, requesting up to a // 3b. Query our peers via BlockSync in batches, requesting up to a
// maximum of 500 tipsets every time. // maximum of 500 tipsets every time.
@ -1100,40 +1097,40 @@ func extractSyncState(ctx context.Context) *SyncerState {
// //
// All throughout the process, we keep checking if the received blocks are in // All throughout the process, we keep checking if the received blocks are in
// the deny list, and short-circuit the process if so. // the deny list, and short-circuit the process if so.
func (syncer *Syncer) collectHeaders(ctx context.Context, from *types.TipSet, to *types.TipSet) ([]*types.TipSet, error) { func (syncer *Syncer) collectHeaders(ctx context.Context, incoming *types.TipSet, known *types.TipSet) ([]*types.TipSet, error) {
ctx, span := trace.StartSpan(ctx, "collectHeaders") ctx, span := trace.StartSpan(ctx, "collectHeaders")
defer span.End() defer span.End()
ss := extractSyncState(ctx) ss := extractSyncState(ctx)
span.AddAttributes( span.AddAttributes(
trace.Int64Attribute("fromHeight", int64(from.Height())), trace.Int64Attribute("fromHeight", int64(incoming.Height())),
trace.Int64Attribute("toHeight", int64(to.Height())), trace.Int64Attribute("toHeight", int64(known.Height())),
) )
// Check if the parents of the from block are in the denylist. // Check if the parents of the from block are in the denylist.
// i.e. if a fork of the chain has been requested that we know to be bad. // i.e. if a fork of the chain has been requested that we know to be bad.
for _, pcid := range from.Parents().Cids() { for _, pcid := range incoming.Parents().Cids() {
if reason, ok := syncer.bad.Has(pcid); ok { if reason, ok := syncer.bad.Has(pcid); ok {
newReason := reason.Linked("linked to %s", pcid) newReason := reason.Linked("linked to %s", pcid)
for _, b := range from.Cids() { for _, b := range incoming.Cids() {
syncer.bad.Add(b, newReason) syncer.bad.Add(b, newReason)
} }
return nil, xerrors.Errorf("chain linked to block marked previously as bad (%s, %s) (reason: %s)", from.Cids(), pcid, reason) return nil, xerrors.Errorf("chain linked to block marked previously as bad (%s, %s) (reason: %s)", incoming.Cids(), pcid, reason)
} }
} }
{ {
// ensure consistency of beacon entires // ensure consistency of beacon entires
targetBE := from.Blocks()[0].BeaconEntries targetBE := incoming.Blocks()[0].BeaconEntries
sorted := sort.SliceIsSorted(targetBE, func(i, j int) bool { sorted := sort.SliceIsSorted(targetBE, func(i, j int) bool {
return targetBE[i].Round < targetBE[j].Round return targetBE[i].Round < targetBE[j].Round
}) })
if !sorted { if !sorted {
syncer.bad.Add(from.Cids()[0], NewBadBlockReason(from.Cids(), "wrong order of beacon entires")) syncer.bad.Add(incoming.Cids()[0], NewBadBlockReason(incoming.Cids(), "wrong order of beacon entires"))
return nil, xerrors.Errorf("wrong order of beacon entires") return nil, xerrors.Errorf("wrong order of beacon entires")
} }
for _, bh := range from.Blocks()[1:] { for _, bh := range incoming.Blocks()[1:] {
if len(targetBE) != len(bh.BeaconEntries) { if len(targetBE) != len(bh.BeaconEntries) {
// cannot mark bad, I think @Kubuxu // cannot mark bad, I think @Kubuxu
return nil, xerrors.Errorf("tipset contained different number for beacon entires") return nil, xerrors.Errorf("tipset contained different number for beacon entires")
@ -1148,12 +1145,12 @@ func (syncer *Syncer) collectHeaders(ctx context.Context, from *types.TipSet, to
} }
} }
blockSet := []*types.TipSet{from} blockSet := []*types.TipSet{incoming}
at := from.Parents() at := incoming.Parents()
// we want to sync all the blocks until the height above the block we have // we want to sync all the blocks until the height above the block we have
untilHeight := to.Height() + 1 untilHeight := known.Height() + 1
ss.SetHeight(blockSet[len(blockSet)-1].Height()) ss.SetHeight(blockSet[len(blockSet)-1].Height())
@ -1168,7 +1165,7 @@ loop:
syncer.bad.Add(b, newReason) syncer.bad.Add(b, newReason)
} }
return nil, xerrors.Errorf("chain contained block marked previously as bad (%s, %s) (reason: %s)", from.Cids(), bc, reason) return nil, xerrors.Errorf("chain contained block marked previously as bad (%s, %s) (reason: %s)", incoming.Cids(), bc, reason)
} }
} }
@ -1217,7 +1214,7 @@ loop:
syncer.bad.Add(b, newReason) syncer.bad.Add(b, newReason)
} }
return nil, xerrors.Errorf("chain contained block marked previously as bad (%s, %s) (reason: %s)", from.Cids(), bc, reason) return nil, xerrors.Errorf("chain contained block marked previously as bad (%s, %s) (reason: %s)", incoming.Cids(), bc, reason)
} }
} }
blockSet = append(blockSet, b) blockSet = append(blockSet, b)
@ -1229,23 +1226,23 @@ loop:
at = blks[len(blks)-1].Parents() at = blks[len(blks)-1].Parents()
} }
if !types.CidArrsEqual(blockSet[len(blockSet)-1].Parents().Cids(), to.Cids()) { // base is the tipset in the candidate chain at the height equal to our known tipset height.
last := blockSet[len(blockSet)-1] if base := blockSet[len(blockSet)-1]; !types.CidArrsEqual(base.Parents().Cids(), known.Cids()) {
if last.Parents() == to.Parents() { if base.Parents() == known.Parents() {
// common case: receiving a block thats potentially part of the same tipset as our best block // common case: receiving a block thats potentially part of the same tipset as our best block
return blockSet, nil return blockSet, nil
} }
// We have now ascertained that this is *not* a 'fast forward' // We have now ascertained that this is *not* a 'fast forward'
log.Warnf("(fork detected) synced header chain (%s - %d) does not link to our best block (%s - %d)", from.Cids(), from.Height(), to.Cids(), to.Height()) log.Warnf("(fork detected) synced header chain (%s - %d) does not link to our best block (%s - %d)", incoming.Cids(), incoming.Height(), known.Cids(), known.Height())
fork, err := syncer.syncFork(ctx, last, to) fork, err := syncer.syncFork(ctx, base, known)
if err != nil { if err != nil {
if xerrors.Is(err, ErrForkTooLong) { if xerrors.Is(err, ErrForkTooLong) {
// TODO: we're marking this block bad in the same way that we mark invalid blocks bad. Maybe distinguish? // TODO: we're marking this block bad in the same way that we mark invalid blocks bad. Maybe distinguish?
log.Warn("adding forked chain to our bad tipset cache") log.Warn("adding forked chain to our bad tipset cache")
for _, b := range from.Blocks() { for _, b := range incoming.Blocks() {
syncer.bad.Add(b.Cid(), NewBadBlockReason(from.Cids(), "fork past finality")) syncer.bad.Add(b.Cid(), NewBadBlockReason(incoming.Cids(), "fork past finality"))
} }
} }
return nil, xerrors.Errorf("failed to sync fork: %w", err) return nil, xerrors.Errorf("failed to sync fork: %w", err)
@ -1265,13 +1262,13 @@ var ErrForkTooLong = fmt.Errorf("fork longer than threshold")
// If the fork is too long (build.ForkLengthThreshold), we add the entire subchain to the // If the fork is too long (build.ForkLengthThreshold), we add the entire subchain to the
// denylist. Else, we find the common ancestor, and add the missing chain // denylist. Else, we find the common ancestor, and add the missing chain
// fragment until the fork point to the returned []TipSet. // fragment until the fork point to the returned []TipSet.
func (syncer *Syncer) syncFork(ctx context.Context, from *types.TipSet, to *types.TipSet) ([]*types.TipSet, error) { func (syncer *Syncer) syncFork(ctx context.Context, incoming *types.TipSet, known *types.TipSet) ([]*types.TipSet, error) {
tips, err := syncer.Bsync.GetBlocks(ctx, from.Parents(), int(build.ForkLengthThreshold)) tips, err := syncer.Bsync.GetBlocks(ctx, incoming.Parents(), int(build.ForkLengthThreshold))
if err != nil { if err != nil {
return nil, err return nil, err
} }
nts, err := syncer.store.LoadTipSet(to.Parents()) nts, err := syncer.store.LoadTipSet(known.Parents())
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to load next local tipset: %w", err) return nil, xerrors.Errorf("failed to load next local tipset: %w", err)
} }
@ -1281,7 +1278,7 @@ func (syncer *Syncer) syncFork(ctx context.Context, from *types.TipSet, to *type
if !syncer.Genesis.Equals(nts) { if !syncer.Genesis.Equals(nts) {
return nil, xerrors.Errorf("somehow synced chain that linked back to a different genesis (bad genesis: %s)", nts.Key()) return nil, xerrors.Errorf("somehow synced chain that linked back to a different genesis (bad genesis: %s)", nts.Key())
} }
return nil, xerrors.Errorf("synced chain forked at genesis, refusing to sync") return nil, xerrors.Errorf("synced chain forked at genesis, refusing to sync; incoming: %s")
} }
if nts.Equals(tips[cur]) { if nts.Equals(tips[cur]) {