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
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var CheckpointKey = datastore.NewKey("/chain/checks")
|
|
|
|
|
|
|
|
func loadCheckpoint(ds dtypes.MetadataDS) (types.TipSetKey, error) {
|
|
|
|
haveChks, err := ds.Has(CheckpointKey)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyTSK, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !haveChks {
|
|
|
|
return types.EmptyTSK, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tskBytes, err := ds.Get(CheckpointKey)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyTSK, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tsk types.TipSetKey
|
|
|
|
err = json.Unmarshal(tskBytes, &tsk)
|
|
|
|
if err != nil {
|
|
|
|
return types.EmptyTSK, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tsk, err
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := syncer.ChainStore().LoadTipSet(tsk)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
hts := syncer.ChainStore().GetHeaviestTipSet()
|
|
|
|
anc, err := syncer.ChainStore().IsAncestorOf(ts, hts)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("cannot determine whether checkpoint tipset is in main-chain: %w", err)
|
|
|
|
}
|
|
|
|
if !hts.Equals(ts) && !anc {
|
2021-04-26 10:50:29 +00:00
|
|
|
if err := syncer.collectChain(ctx, ts, hts); err != nil {
|
|
|
|
return xerrors.Errorf("failed to collect chain for checkpoint: %w", err)
|
|
|
|
}
|
|
|
|
if err := syncer.ChainStore().SetHead(ts); err != nil {
|
|
|
|
return xerrors.Errorf("failed to set the chain head: %w", err)
|
|
|
|
}
|
2020-09-09 05:14:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 10:50:29 +00:00
|
|
|
syncer.checkptLk.Lock()
|
|
|
|
defer syncer.checkptLk.Unlock()
|
|
|
|
|
2020-09-09 05:14:01 +00:00
|
|
|
tskBytes, err := json.Marshal(tsk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = syncer.ds.Put(CheckpointKey, tskBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-26 10:50:29 +00:00
|
|
|
// TODO: This is racy. as there may be a concurrent sync in progress.
|
|
|
|
// The only real solution is to checkpoint inside the chainstore, not here.
|
2020-09-09 05:14:01 +00:00
|
|
|
syncer.checkpt = tsk
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (syncer *Syncer) GetCheckpoint() types.TipSetKey {
|
|
|
|
syncer.checkptLk.Lock()
|
|
|
|
defer syncer.checkptLk.Unlock()
|
|
|
|
return syncer.checkpt
|
|
|
|
}
|