From 64d150e215256cab04c0eb52819ca2675330b151 Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Thu, 10 Sep 2020 10:35:06 -0700 Subject: [PATCH 1/4] feat(cli): add chain delete cmd --- api/api_full.go | 3 +++ api/apistruct/struct.go | 5 +++++ cli/chain.go | 27 +++++++++++++++++++++++++++ node/impl/full/chain.go | 4 ++++ 4 files changed, 39 insertions(+) diff --git a/api/api_full.go b/api/api_full.go index 02417bf78db..3dde0e5173c 100644 --- a/api/api_full.go +++ b/api/api_full.go @@ -75,6 +75,9 @@ type FullNode interface { // blockstore and returns raw bytes. ChainReadObj(context.Context, cid.Cid) ([]byte, error) + // ChainDeleteObj deletes node referenced by the given CID + ChainDeleteObj(context.Context, cid.Cid) error + // ChainHasObj checks if a given CID exists in the chain blockstore. ChainHasObj(context.Context, cid.Cid) (bool, error) diff --git a/api/apistruct/struct.go b/api/apistruct/struct.go index ffb83778532..172156c4297 100644 --- a/api/apistruct/struct.go +++ b/api/apistruct/struct.go @@ -85,6 +85,7 @@ type FullNodeStruct struct { ChainGetParentMessages func(context.Context, cid.Cid) ([]api.Message, error) `perm:"read"` ChainGetTipSetByHeight func(context.Context, abi.ChainEpoch, types.TipSetKey) (*types.TipSet, error) `perm:"read"` ChainReadObj func(context.Context, cid.Cid) ([]byte, error) `perm:"read"` + ChainDeleteObj func(context.Context, cid.Cid) error `perm:"admin"` ChainHasObj func(context.Context, cid.Cid) (bool, error) `perm:"read"` ChainStatObj func(context.Context, cid.Cid, cid.Cid) (api.ObjStat, error) `perm:"read"` ChainSetHead func(context.Context, types.TipSetKey) error `perm:"admin"` @@ -658,6 +659,10 @@ func (c *FullNodeStruct) ChainReadObj(ctx context.Context, obj cid.Cid) ([]byte, return c.Internal.ChainReadObj(ctx, obj) } +func (c *FullNodeStruct) ChainDeleteObj(ctx context.Context, obj cid.Cid) error { + return c.Internal.ChainDeleteObj(ctx, obj) +} + func (c *FullNodeStruct) ChainHasObj(ctx context.Context, o cid.Cid) (bool, error) { return c.Internal.ChainHasObj(ctx, o) } diff --git a/cli/chain.go b/cli/chain.go index ce166064179..1c63e37a811 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -193,6 +193,33 @@ var chainReadObjCmd = &cli.Command{ }, } +var chainDeleteObjCmd = &cli.Command{ + Name: "delete-obj", + Usage: "Delete an object", + ArgsUsage: "[objectCid]", + Action: func(cctx *cli.Context) error { + api, closer, err := GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer closer() + ctx := ReqContext(cctx) + + c, err := cid.Decode(cctx.Args().First()) + if err != nil { + return fmt.Errorf("failed to parse cid input: %s", err) + } + + err = api.ChainDeleteObj(ctx, c) + if err != nil { + return err + } + + fmt.Printf("Obj %s deleted\n", c.String()) + return nil + }, +} + var chainStatObjCmd = &cli.Command{ Name: "stat-obj", Usage: "Collect size and ipld link counts for objs", diff --git a/node/impl/full/chain.go b/node/impl/full/chain.go index c5dd5c9a998..84335280ddf 100644 --- a/node/impl/full/chain.go +++ b/node/impl/full/chain.go @@ -197,6 +197,10 @@ func (a *ChainAPI) ChainReadObj(ctx context.Context, obj cid.Cid) ([]byte, error return blk.RawData(), nil } +func (a *ChainAPI) ChainDeleteObj(ctx context.Context, obj cid.Cid) error { + return a.Chain.Blockstore().DeleteBlock(obj) +} + func (a *ChainAPI) ChainHasObj(ctx context.Context, obj cid.Cid) (bool, error) { return a.Chain.Blockstore().Has(obj) } From e86a74156e85c520b7ef63d885ee7b3874815340 Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Thu, 10 Sep 2020 10:39:17 -0700 Subject: [PATCH 2/4] feat(cli): add command to list --- cli/chain.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/chain.go b/cli/chain.go index 1c63e37a811..0f1e36518b4 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -40,6 +40,7 @@ var chainCmd = &cli.Command{ chainHeadCmd, chainGetBlock, chainReadObjCmd, + chainDeleteObjCmd, chainStatObjCmd, chainGetMsgCmd, chainSetHeadCmd, From 5e445f5a4803abfbffa977b9f52da5f72dd94bc9 Mon Sep 17 00:00:00 2001 From: hannahhoward Date: Thu, 10 Sep 2020 10:47:19 -0700 Subject: [PATCH 3/4] docs(apidocs): run docs gen --- documentation/en/api-methods.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/documentation/en/api-methods.md b/documentation/en/api-methods.md index 49582d6f819..203a5f1419d 100644 --- a/documentation/en/api-methods.md +++ b/documentation/en/api-methods.md @@ -9,6 +9,7 @@ * [Beacon](#Beacon) * [BeaconGetEntry](#BeaconGetEntry) * [Chain](#Chain) + * [ChainDeleteObj](#ChainDeleteObj) * [ChainExport](#ChainExport) * [ChainGetBlock](#ChainGetBlock) * [ChainGetBlockMessages](#ChainGetBlockMessages) @@ -279,6 +280,23 @@ The Chain method group contains methods for interacting with the blockchain, but that do not require any form of state computation. +### ChainDeleteObj +ChainDeleteObj deletes node referenced by the given CID + + +Perms: admin + +Inputs: +```json +[ + { + "/": "bafy2bzacea3wsdh6y3a36tb3skempjoxqpuyompjbmfeyf34fi3uy6uue42v4" + } +] +``` + +Response: `{}` + ### ChainExport ChainExport returns a stream of bytes with CAR dump of chain data. The exported chain data includes the header chain from the given tipset From 6c5ed3f07f287e375c171818f001f13e5f0dd7ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 23 Sep 2020 19:31:36 +0200 Subject: [PATCH 4/4] Some safeguards on chain delete-obj --- cli/chain.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cli/chain.go b/cli/chain.go index 0f1e36518b4..bd72c2030b0 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -195,9 +195,15 @@ var chainReadObjCmd = &cli.Command{ } var chainDeleteObjCmd = &cli.Command{ - Name: "delete-obj", - Usage: "Delete an object", - ArgsUsage: "[objectCid]", + Name: "delete-obj", + Usage: "Delete an object from the chain blockstore", + Description: "WARNING: Removing wrong objects from the chain blockstore may lead to sync issues", + ArgsUsage: "[objectCid]", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "really-do-it", + }, + }, Action: func(cctx *cli.Context) error { api, closer, err := GetFullNodeAPI(cctx) if err != nil { @@ -211,6 +217,10 @@ var chainDeleteObjCmd = &cli.Command{ return fmt.Errorf("failed to parse cid input: %s", err) } + if !cctx.Bool("really-do-it") { + return xerrors.Errorf("pass the --really-do-it flag to proceed") + } + err = api.ChainDeleteObj(ctx, c) if err != nil { return err