2020-09-09 05:14:01 +00:00
|
|
|
package chain
|
|
|
|
|
|
|
|
import (
|
2021-04-26 10:50:29 +00:00
|
|
|
"context"
|
2020-09-09 05:14:01 +00:00
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-09 05:14:01 +00:00
|
|
|
)
|
|
|
|
|
2021-04-26 10:50:29 +00:00
|
|
|
func (syncer *Syncer) SyncCheckpoint(ctx context.Context, tsk types.TipSetKey) error {
|
2020-09-09 05:14:01 +00:00
|
|
|
if tsk == types.EmptyTSK {
|
|
|
|
return xerrors.Errorf("called with empty tsk")
|
|
|
|
}
|
|
|
|
|
2021-12-11 21:03:00 +00:00
|
|
|
ts, err := syncer.ChainStore().LoadTipSet(ctx, tsk)
|
2020-09-09 05:14:01 +00:00
|
|
|
if err != nil {
|
2021-04-26 10:50:29 +00:00
|
|
|
tss, err := syncer.Exchange.GetBlocks(ctx, tsk, 1)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to fetch tipset: %w", err)
|
|
|
|
} else if len(tss) != 1 {
|
|
|
|
return xerrors.Errorf("expected 1 tipset, got %d", len(tss))
|
|
|
|
}
|
|
|
|
ts = tss[0]
|
2020-09-09 05:14:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 19:49:21 +00:00
|
|
|
hts := syncer.ChainStore().GetHeaviestTipSet()
|
2024-05-30 19:45:41 +00:00
|
|
|
if !hts.Equals(ts) {
|
|
|
|
if anc, err := syncer.store.IsAncestorOf(ctx, ts, hts); err != nil {
|
|
|
|
return xerrors.Errorf("failed to walk the chain when checkpointing: %w", err)
|
|
|
|
} else if !anc {
|
|
|
|
if err := syncer.collectChain(ctx, ts, hts, true); err != nil {
|
|
|
|
return xerrors.Errorf("failed to collect chain for checkpoint: %w", err)
|
|
|
|
}
|
|
|
|
} // else new checkpoint is on the current chain, we definitely have the tipsets.
|
|
|
|
} // else current head, no need to switch.
|
2020-09-09 05:14:01 +00:00
|
|
|
|
2024-01-26 16:50:32 +00:00
|
|
|
if err := syncer.ChainStore().SetCheckpoint(ctx, ts); err != nil {
|
|
|
|
return xerrors.Errorf("failed to set the chain checkpoint: %w", err)
|
2021-04-28 19:49:21 +00:00
|
|
|
}
|
2024-01-26 16:50:32 +00:00
|
|
|
|
2020-09-09 05:14:01 +00:00
|
|
|
return nil
|
|
|
|
}
|