2019-09-30 21:06:47 +00:00
|
|
|
package full
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-24 11:35:45 +00:00
|
|
|
"sync/atomic"
|
2019-09-30 21:06:47 +00:00
|
|
|
|
2022-06-15 10:06:22 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2019-10-14 14:21:37 +00:00
|
|
|
pubsub "github.com/libp2p/go-libp2p-pubsub"
|
2019-09-30 21:06:47 +00:00
|
|
|
"go.uber.org/fx"
|
2019-10-14 14:21:37 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-03-31 23:13:37 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain"
|
2020-08-06 01:16:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/gen/slashfilter"
|
2020-03-31 23:13:37 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-24 11:35:45 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2020-03-31 23:13:37 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/dtypes"
|
2019-09-30 21:06:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SyncAPI struct {
|
|
|
|
fx.In
|
|
|
|
|
2021-09-02 16:07:23 +00:00
|
|
|
SlashFilter *slashfilter.SlashFilter `optional:"true"`
|
2020-08-06 01:16:54 +00:00
|
|
|
Syncer *chain.Syncer
|
|
|
|
PubSub *pubsub.PubSub
|
|
|
|
NetName dtypes.NetworkName
|
2019-09-30 21:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *SyncAPI) SyncState(ctx context.Context) (*api.SyncState, error) {
|
2019-11-16 01:05:16 +00:00
|
|
|
states := a.Syncer.State()
|
|
|
|
|
2020-09-24 11:35:45 +00:00
|
|
|
out := &api.SyncState{
|
|
|
|
VMApplied: atomic.LoadUint64(&vm.StatApplied),
|
|
|
|
}
|
2019-11-16 01:05:16 +00:00
|
|
|
|
2020-03-06 06:46:07 +00:00
|
|
|
for i := range states {
|
|
|
|
ss := &states[i]
|
2019-11-16 01:05:16 +00:00
|
|
|
out.ActiveSyncs = append(out.ActiveSyncs, api.ActiveSync{
|
2020-10-28 19:22:07 +00:00
|
|
|
WorkerID: ss.WorkerID,
|
|
|
|
Base: ss.Base,
|
|
|
|
Target: ss.Target,
|
|
|
|
Stage: ss.Stage,
|
|
|
|
Height: ss.Height,
|
|
|
|
Start: ss.Start,
|
|
|
|
End: ss.End,
|
|
|
|
Message: ss.Message,
|
2019-11-16 01:05:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return out, nil
|
2019-09-30 21:06:47 +00:00
|
|
|
}
|
2019-10-14 14:21:37 +00:00
|
|
|
|
|
|
|
func (a *SyncAPI) SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) error {
|
2021-12-11 21:03:00 +00:00
|
|
|
parent, err := a.Syncer.ChainStore().GetBlock(ctx, blk.Header.Parents[0])
|
2020-08-06 01:14:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading parent block: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-09-02 16:07:23 +00:00
|
|
|
if a.SlashFilter != nil {
|
2021-12-11 21:03:00 +00:00
|
|
|
if err := a.SlashFilter.MinedBlock(ctx, blk.Header, parent.Height); err != nil {
|
2021-09-02 16:07:23 +00:00
|
|
|
log.Errorf("<!!> SLASH FILTER ERROR: %s", err)
|
|
|
|
return xerrors.Errorf("<!!> SLASH FILTER ERROR: %w", err)
|
|
|
|
}
|
2020-08-06 01:14:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 14:21:37 +00:00
|
|
|
// TODO: should we have some sort of fast path to adding a local block?
|
2021-12-17 09:42:09 +00:00
|
|
|
bmsgs, err := a.Syncer.ChainStore().LoadMessagesFromCids(ctx, blk.BlsMessages)
|
2019-10-14 14:21:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to load bls messages: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:42:09 +00:00
|
|
|
smsgs, err := a.Syncer.ChainStore().LoadSignedMessagesFromCids(ctx, blk.SecpkMessages)
|
2019-10-14 14:21:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to load secpk message: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fb := &types.FullBlock{
|
|
|
|
Header: blk.Header,
|
|
|
|
BlsMessages: bmsgs,
|
|
|
|
SecpkMessages: smsgs,
|
|
|
|
}
|
2019-10-15 12:19:10 +00:00
|
|
|
|
|
|
|
if err := a.Syncer.ValidateMsgMeta(fb); err != nil {
|
2019-11-18 21:39:07 +00:00
|
|
|
return xerrors.Errorf("provided messages did not match block: %w", err)
|
2019-10-15 12:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := types.NewTipSet([]*types.BlockHeader{blk.Header})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("somehow failed to make a tipset out of a single block: %w", err)
|
|
|
|
}
|
|
|
|
if err := a.Syncer.Sync(ctx, ts); err != nil {
|
|
|
|
return xerrors.Errorf("sync to submitted block failed: %w", err)
|
|
|
|
}
|
2019-10-14 14:21:37 +00:00
|
|
|
|
|
|
|
b, err := blk.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("serializing block for pubsub publishing failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-08-20 04:49:10 +00:00
|
|
|
return a.PubSub.Publish(build.BlocksTopic(a.NetName), b) //nolint:staticcheck
|
2019-10-14 14:21:37 +00:00
|
|
|
}
|
2019-11-18 21:39:07 +00:00
|
|
|
|
|
|
|
func (a *SyncAPI) SyncIncomingBlocks(ctx context.Context) (<-chan *types.BlockHeader, error) {
|
|
|
|
return a.Syncer.IncomingBlocks(ctx)
|
|
|
|
}
|
2019-12-21 06:10:40 +00:00
|
|
|
|
2020-09-09 05:14:01 +00:00
|
|
|
func (a *SyncAPI) SyncCheckpoint(ctx context.Context, tsk types.TipSetKey) error {
|
2020-09-09 22:17:05 +00:00
|
|
|
log.Warnf("Marking tipset %s as checkpoint", tsk)
|
2021-04-26 10:50:29 +00:00
|
|
|
return a.Syncer.SyncCheckpoint(ctx, tsk)
|
2020-09-09 05:14:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 06:10:40 +00:00
|
|
|
func (a *SyncAPI) SyncMarkBad(ctx context.Context, bcid cid.Cid) error {
|
|
|
|
log.Warnf("Marking block %s as bad", bcid)
|
|
|
|
a.Syncer.MarkBad(bcid)
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-12 07:44:55 +00:00
|
|
|
|
2020-09-09 07:24:09 +00:00
|
|
|
func (a *SyncAPI) SyncUnmarkBad(ctx context.Context, bcid cid.Cid) error {
|
|
|
|
log.Warnf("Unmarking block %s as bad", bcid)
|
|
|
|
a.Syncer.UnmarkBad(bcid)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-10 08:26:42 +00:00
|
|
|
func (a *SyncAPI) SyncUnmarkAllBad(ctx context.Context) error {
|
|
|
|
log.Warnf("Dropping bad block cache")
|
|
|
|
a.Syncer.UnmarkAllBad()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-12 07:44:55 +00:00
|
|
|
func (a *SyncAPI) SyncCheckBad(ctx context.Context, bcid cid.Cid) (string, error) {
|
|
|
|
reason, ok := a.Syncer.CheckBadBlockCache(bcid)
|
|
|
|
if !ok {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return reason, nil
|
|
|
|
}
|
2020-09-27 21:52:26 +00:00
|
|
|
|
|
|
|
func (a *SyncAPI) SyncValidateTipset(ctx context.Context, tsk types.TipSetKey) (bool, error) {
|
2021-12-11 21:03:00 +00:00
|
|
|
ts, err := a.Syncer.ChainStore().LoadTipSet(ctx, tsk)
|
2020-09-27 21:52:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:42:09 +00:00
|
|
|
fts, err := a.Syncer.ChainStore().TryFillTipSet(ctx, ts)
|
2020-09-27 21:52:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-09-30 05:39:06 +00:00
|
|
|
err = a.Syncer.ValidateTipSet(ctx, fts, false)
|
2020-09-27 21:52:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|