feat: sync: harden chain sync (#10756)

* fix: sync: fail sync instead of logging if we sync the wrong chain

* fix: sync: write headers in the correct order

Just in case. This shouldn't be necessary, but we might as well.

* fix: minus minus

* fix: do put the tipset

Put != Persist
This commit is contained in:
Steven Allen 2023-04-26 14:12:45 -07:00 committed by GitHub
parent cf8587522a
commit d2c3e84d54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1193,12 +1193,14 @@ func (syncer *Syncer) collectChain(ctx context.Context, ts *types.TipSet, hts *t
span.AddAttributes(trace.Int64Attribute("syncChainLength", int64(len(headers)))) span.AddAttributes(trace.Int64Attribute("syncChainLength", int64(len(headers))))
if !headers[0].Equals(ts) { if !headers[0].Equals(ts) {
log.Errorf("collectChain headers[0] should be equal to sync target. Its not: %s != %s", headers[0].Cids(), ts.Cids()) return xerrors.Errorf("collectChain synced %s, wanted to sync %s", headers[0].Cids(), ts.Cids())
} }
ss.SetStage(api.StagePersistHeaders) ss.SetStage(api.StagePersistHeaders)
for _, ts := range headers { // Write tipsets from oldest to newest.
for i := len(headers) - 1; i >= 0; i-- {
ts := headers[i]
if err := syncer.store.PersistTipset(ctx, ts); err != nil { if err := syncer.store.PersistTipset(ctx, ts); err != nil {
err = xerrors.Errorf("failed to persist synced tipset to the chainstore: %w", err) err = xerrors.Errorf("failed to persist synced tipset to the chainstore: %w", err)
ss.Error(err) ss.Error(err)