From 404e14d3eb09dc1a4e54914f767611930801105f Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Tue, 1 Oct 2019 10:28:07 -0600 Subject: [PATCH] Add a command to read an object by its cid --- api/api.go | 1 + api/struct.go | 5 +++++ cli/chain.go | 26 ++++++++++++++++++++++++++ node/impl/full/chain.go | 9 +++++++++ 4 files changed, 41 insertions(+) diff --git a/api/api.go b/api/api.go index 330920bf0..b821d01db 100644 --- a/api/api.go +++ b/api/api.go @@ -56,6 +56,7 @@ type FullNode interface { ChainGetBlockMessages(context.Context, cid.Cid) (*BlockMessages, error) ChainGetBlockReceipts(context.Context, cid.Cid) ([]*types.MessageReceipt, error) ChainGetTipSetByHeight(context.Context, uint64, *types.TipSet) (*types.TipSet, error) + ChainReadObj(context.Context, cid.Cid) ([]byte, error) // syncer SyncState(context.Context) (*SyncState, error) diff --git a/api/struct.go b/api/struct.go index b4d548f7f..e6798dfe8 100644 --- a/api/struct.go +++ b/api/struct.go @@ -46,6 +46,7 @@ type FullNodeStruct struct { ChainGetBlockMessages func(context.Context, cid.Cid) (*BlockMessages, error) `perm:"read"` ChainGetBlockReceipts func(context.Context, cid.Cid) ([]*types.MessageReceipt, error) `perm:"read"` ChainGetTipSetByHeight func(context.Context, uint64, *types.TipSet) (*types.TipSet, error) `perm:"read"` + ChainReadObj func(context.Context, cid.Cid) ([]byte, error) `perm:"read"` SyncState func(context.Context) (*SyncState, error) `perm:"read"` @@ -286,6 +287,10 @@ func (c *FullNodeStruct) ChainNotify(ctx context.Context) (<-chan []*store.HeadC return c.Internal.ChainNotify(ctx) } +func (c *FullNodeStruct) ChainReadObj(ctx context.Context, obj cid.Cid) ([]byte, error) { + return c.Internal.ChainReadObj(ctx, obj) +} + func (c *FullNodeStruct) SyncState(ctx context.Context) (*SyncState, error) { return c.Internal.SyncState(ctx) } diff --git a/cli/chain.go b/cli/chain.go index 485964758..4562ed5c1 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -16,6 +16,7 @@ var chainCmd = &cli.Command{ Subcommands: []*cli.Command{ chainHeadCmd, chainGetBlock, + chainReadObjCmd, }, } @@ -113,3 +114,28 @@ var chainGetBlock = &cli.Command{ }, } + +var chainReadObjCmd = &cli.Command{ + Name: "read-obj", + Usage: "Read the raw bytes of an object", + Action: func(cctx *cli.Context) error { + api, err := GetFullNodeAPI(cctx) + if err != nil { + return err + } + ctx := ReqContext(cctx) + + c, err := cid.Parse(cctx.Args().First) + if err != nil { + return fmt.Errorf("failed to parse cid input: %s", err) + } + + obj, err := api.ChainReadObj(ctx, c) + if err != nil { + return err + } + + fmt.Printf("%x\n", obj) + return nil + }, +} diff --git a/node/impl/full/chain.go b/node/impl/full/chain.go index 91a6df36c..fccad5fcd 100644 --- a/node/impl/full/chain.go +++ b/node/impl/full/chain.go @@ -122,3 +122,12 @@ func (a *ChainAPI) ChainGetBlockReceipts(ctx context.Context, bcid cid.Cid) ([]* func (a *ChainAPI) ChainGetTipSetByHeight(ctx context.Context, h uint64, ts *types.TipSet) (*types.TipSet, error) { return a.Chain.GetTipsetByHeight(ctx, h, ts) } + +func (a *ChainAPI) ChainReadObj(ctx context.Context, obj cid.Cid) ([]byte, error) { + blk, err := a.Chain.Blockstore().Get(obj) + if err != nil { + return nil, xerrors.Errorf("blockstore get: %w", err) + } + + return blk.RawData(), nil +}