diff --git a/api/api_full.go b/api/api_full.go index 6e6796476..6b3dfa6fd 100644 --- a/api/api_full.go +++ b/api/api_full.go @@ -41,6 +41,7 @@ type FullNode interface { SyncState(context.Context) (*SyncState, error) SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) error SyncIncomingBlocks(ctx context.Context) (<-chan *types.BlockHeader, error) + SyncMarkBad(ctx context.Context, bcid cid.Cid) error // messages MpoolPending(context.Context, *types.TipSet) ([]*types.SignedMessage, error) diff --git a/api/apistruct/struct.go b/api/apistruct/struct.go index 7a95f318c..1f7c97f95 100644 --- a/api/apistruct/struct.go +++ b/api/apistruct/struct.go @@ -58,6 +58,7 @@ type FullNodeStruct struct { SyncState func(context.Context) (*api.SyncState, error) `perm:"read"` SyncSubmitBlock func(ctx context.Context, blk *types.BlockMsg) error `perm:"write"` SyncIncomingBlocks func(ctx context.Context) (<-chan *types.BlockHeader, error) `perm:"read"` + SyncMarkBad func(ctx context.Context, bcid cid.Cid) error `perm:"write"` MpoolPending func(context.Context, *types.TipSet) ([]*types.SignedMessage, error) `perm:"read"` MpoolPush func(context.Context, *types.SignedMessage) error `perm:"write"` @@ -360,6 +361,10 @@ func (c *FullNodeStruct) SyncIncomingBlocks(ctx context.Context) (<-chan *types. return c.Internal.SyncIncomingBlocks(ctx) } +func (c *FullNodeStruct) SyncMarkBad(ctx context.Context, bcid cid.Cid) error { + return c.Internal.SyncMarkBad(ctx, bcid) +} + func (c *FullNodeStruct) StateMinerSectors(ctx context.Context, addr address.Address, ts *types.TipSet) ([]*api.ChainSectorInfo, error) { return c.Internal.StateMinerSectors(ctx, addr, ts) } diff --git a/chain/sync.go b/chain/sync.go index 19d83df27..9065bc099 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -1181,3 +1181,7 @@ func (syncer *Syncer) State() []SyncerState { } return out } + +func (syncer *Syncer) MarkBad(blk cid.Cid) { + syncer.bad.Add(blk) +} diff --git a/cli/sync.go b/cli/sync.go index 1b051769a..d3d425967 100644 --- a/cli/sync.go +++ b/cli/sync.go @@ -19,6 +19,7 @@ var syncCmd = &cli.Command{ Subcommands: []*cli.Command{ syncStatusCmd, syncWaitCmd, + syncMarkBadCmd, }, } @@ -90,6 +91,30 @@ var syncWaitCmd = &cli.Command{ }, } +var syncMarkBadCmd = &cli.Command{ + Name: "mark-bad", + Usage: "Mark the given block as bad, will prevent syncing to a chain that contains it", + Action: func(cctx *cli.Context) error { + napi, closer, err := GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer closer() + ctx := ReqContext(cctx) + + if !cctx.Args().Present() { + return fmt.Errorf("must specify block cid to mark") + } + + bcid, err := cid.Decode(cctx.Args().First()) + if err != nil { + return fmt.Errorf("failed to decode input as a cid: %s", err) + } + + return napi.SyncMarkBad(ctx, bcid) + }, +} + func SyncWait(ctx context.Context, napi api.FullNode) error { for { state, err := napi.SyncState(ctx) diff --git a/node/impl/full/sync.go b/node/impl/full/sync.go index 0ebdad536..250837ab3 100644 --- a/node/impl/full/sync.go +++ b/node/impl/full/sync.go @@ -6,6 +6,8 @@ import ( "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/chain" "github.com/filecoin-project/lotus/chain/types" + cid "github.com/ipfs/go-cid" + "github.com/prometheus/common/log" pubsub "github.com/libp2p/go-libp2p-pubsub" "go.uber.org/fx" @@ -80,3 +82,9 @@ func (a *SyncAPI) SyncSubmitBlock(ctx context.Context, blk *types.BlockMsg) erro func (a *SyncAPI) SyncIncomingBlocks(ctx context.Context) (<-chan *types.BlockHeader, error) { return a.Syncer.IncomingBlocks(ctx) } + +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 +}