Skip to content

Commit

Permalink
Merge pull request #3434 from filecoin-project/feat/shed-import-objec…
Browse files Browse the repository at this point in the history
…t-cmd

add a command to import an ipld object into the chainstore
  • Loading branch information
magik6k authored Sep 29, 2020
2 parents edf1875 + cac848c commit 867ca2f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cmd/lotus-shed/import-car.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package main

import (
"encoding/hex"
"fmt"
"io"
"os"

block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
Expand Down Expand Up @@ -81,3 +84,57 @@ var importCarCmd = &cli.Command{
}
},
}

var importObjectCmd = &cli.Command{
Name: "import-obj",
Usage: "import a raw ipld object into your datastore",
Action: func(cctx *cli.Context) error {
r, err := repo.NewFS(cctx.String("repo"))
if err != nil {
return xerrors.Errorf("opening fs repo: %w", err)
}

exists, err := r.Exists()
if err != nil {
return err
}
if !exists {
return xerrors.Errorf("lotus repo doesn't exist")
}

lr, err := r.Lock(repo.FullNode)
if err != nil {
return err
}
defer lr.Close() //nolint:errcheck

ds, err := lr.Datastore("/chain")
if err != nil {
return err
}

bs := blockstore.NewBlockstore(ds)

c, err := cid.Decode(cctx.Args().Get(0))
if err != nil {
return err
}

data, err := hex.DecodeString(cctx.Args().Get(1))
if err != nil {
return err
}

blk, err := block.NewBlockWithCid(data, c)
if err != nil {
return err
}

if err := bs.Put(blk); err != nil {
return err
}

return nil

},
}
1 change: 1 addition & 0 deletions cmd/lotus-shed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {
staterootCmd,
auditsCmd,
importCarCmd,
importObjectCmd,
commpToCidCmd,
fetchParamCmd,
proofsCmd,
Expand Down

0 comments on commit 867ca2f

Please sign in to comment.