2020-09-11 00:28:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-03-08 21:48:08 +00:00
|
|
|
"bytes"
|
2020-09-11 00:28:25 +00:00
|
|
|
"context"
|
2022-03-08 21:48:08 +00:00
|
|
|
"errors"
|
2020-09-11 00:28:25 +00:00
|
|
|
"fmt"
|
2020-11-01 13:03:21 +00:00
|
|
|
"io"
|
2020-09-11 00:28:25 +00:00
|
|
|
"os"
|
2022-03-08 21:48:08 +00:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2020-09-11 00:28:25 +00:00
|
|
|
|
2022-03-08 21:48:08 +00:00
|
|
|
"github.com/dgraph-io/badger/v2"
|
|
|
|
"github.com/dgraph-io/badger/v2/pb"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
"github.com/ipfs/go-blockservice"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
offline "github.com/ipfs/go-ipfs-exchange-offline"
|
2022-06-28 11:09:59 +00:00
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
2023-01-26 15:41:28 +00:00
|
|
|
block "github.com/ipfs/go-libipfs/blocks"
|
2022-03-08 21:48:08 +00:00
|
|
|
"github.com/ipfs/go-merkledag"
|
|
|
|
"github.com/ipld/go-car"
|
|
|
|
"github.com/multiformats/go-base32"
|
|
|
|
mh "github.com/multiformats/go-multihash"
|
2020-09-11 00:28:25 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-03-08 21:48:08 +00:00
|
|
|
"go.uber.org/zap"
|
2020-09-11 00:28:25 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
|
2022-03-08 21:48:08 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2020-09-11 00:28:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2022-03-08 21:48:08 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-shed/shedgen"
|
2020-09-11 00:28:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var exportChainCmd = &cli.Command{
|
|
|
|
Name: "export",
|
|
|
|
Description: "Export chain from repo (requires node to be offline)",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "tipset",
|
|
|
|
Usage: "tipset to export from",
|
|
|
|
},
|
|
|
|
&cli.Int64Flag{
|
|
|
|
Name: "recent-stateroots",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "full-state",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "skip-old-msgs",
|
|
|
|
},
|
|
|
|
},
|
2022-03-08 21:48:08 +00:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
exportRawCmd,
|
|
|
|
},
|
2020-09-11 00:28:25 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if !cctx.Args().Present() {
|
2020-09-11 08:55:10 +00:00
|
|
|
return lcli.ShowHelp(cctx, fmt.Errorf("must specify file name to write export to"))
|
2020-09-11 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
fi, err := os.Create(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening the output file: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:55:10 +00:00
|
|
|
defer fi.Close() //nolint:errcheck
|
2020-09-11 00:28:25 +00:00
|
|
|
|
2021-02-28 22:48:36 +00:00
|
|
|
bs, err := lr.Blockstore(ctx, repo.UniversalBlockstore)
|
2020-09-11 00:28:25 +00:00
|
|
|
if err != nil {
|
2020-11-01 13:03:21 +00:00
|
|
|
return fmt.Errorf("failed to open blockstore: %w", err)
|
2020-09-11 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 13:03:21 +00:00
|
|
|
defer func() {
|
|
|
|
if c, ok := bs.(io.Closer); ok {
|
|
|
|
if err := c.Close(); err != nil {
|
|
|
|
log.Warnf("failed to close blockstore: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-26 10:25:34 +00:00
|
|
|
mds, err := lr.Datastore(context.Background(), "/metadata")
|
2020-09-11 00:28:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-02 16:07:23 +00:00
|
|
|
cs := store.NewChainStore(bs, bs, mds, nil, nil)
|
2020-11-16 22:22:08 +00:00
|
|
|
defer cs.Close() //nolint:errcheck
|
|
|
|
|
2021-12-13 12:35:24 +00:00
|
|
|
if err := cs.Load(context.Background()); err != nil {
|
2020-09-11 00:28:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nroots := abi.ChainEpoch(cctx.Int64("recent-stateroots"))
|
|
|
|
fullstate := cctx.Bool("full-state")
|
|
|
|
skipoldmsgs := cctx.Bool("skip-old-msgs")
|
|
|
|
|
2022-10-05 19:10:19 +00:00
|
|
|
ts, err := lcli.ParseTipSetRefOffline(ctx, cs, cctx.String("tipset"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-09-11 00:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fullstate {
|
|
|
|
nroots = ts.Height() + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cs.Export(ctx, ts, nroots, skipoldmsgs, fi); err != nil {
|
|
|
|
return xerrors.Errorf("export failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2022-03-08 21:48:08 +00:00
|
|
|
|
|
|
|
var exportRawCmd = &cli.Command{
|
|
|
|
Name: "raw",
|
|
|
|
Description: "Export raw blocks from repo (requires node to be offline)",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "car-size",
|
|
|
|
Value: "50M",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return lcli.ShowHelp(cctx, fmt.Errorf("must specify file name to write export to"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
|
|
|
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.LockRO(repo.FullNode)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lr.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
out := cctx.Args().First()
|
|
|
|
err = os.Mkdir(out, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("creating output dir: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
maxSz, err := humanize.ParseBytes(cctx.String("car-size"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("parse --car-size: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cars := 0
|
|
|
|
|
|
|
|
carb := &rawCarb{
|
|
|
|
max: maxSz,
|
|
|
|
blocks: map[cid.Cid]block.Block{},
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
consume := func(c cid.Cid, b block.Block) error {
|
|
|
|
err = carb.consume(c, b)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
2022-03-09 09:26:02 +00:00
|
|
|
case errFullCar:
|
2022-03-08 21:48:08 +00:00
|
|
|
root, err := carb.finalize()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("carb finalize: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := carb.writeCar(ctx, filepath.Join(out, fmt.Sprintf("chain%d.car", cars)), root); err != nil {
|
|
|
|
return xerrors.Errorf("writeCar: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cars++
|
|
|
|
|
|
|
|
if cars > 10 {
|
|
|
|
return xerrors.Errorf("enough")
|
|
|
|
}
|
|
|
|
|
|
|
|
carb = &rawCarb{
|
|
|
|
max: maxSz,
|
|
|
|
blocks: map[cid.Cid]block.Block{},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infow("gc")
|
|
|
|
go runtime.GC()
|
|
|
|
|
|
|
|
default:
|
|
|
|
return xerrors.Errorf("carb consume: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
path := filepath.Join(lr.Path(), "datastore", "chain")
|
|
|
|
opts, err := repo.BadgerBlockstoreOptions(repo.UniversalBlockstore, path, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.Logger = &badgerLog{
|
|
|
|
SugaredLogger: log.Desugar().WithOptions(zap.AddCallerSkip(1)).Sugar(),
|
|
|
|
skip2: log.Desugar().WithOptions(zap.AddCallerSkip(2)).Sugar(),
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infow("open db")
|
|
|
|
|
|
|
|
db, err := badger.Open(opts.Options)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open badger blockstore: %w", err)
|
|
|
|
}
|
2022-03-09 09:26:02 +00:00
|
|
|
defer db.Close() // nolint:errcheck
|
2022-03-08 21:48:08 +00:00
|
|
|
|
|
|
|
log.Infow("new stream")
|
|
|
|
|
|
|
|
var wlk sync.Mutex
|
|
|
|
|
|
|
|
str := db.NewStream()
|
|
|
|
str.NumGo = 16
|
|
|
|
str.LogPrefix = "bstream"
|
|
|
|
str.Send = func(list *pb.KVList) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorw("send error", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for _, kv := range list.Kv {
|
|
|
|
if kv.Key == nil || kv.Value == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(string(kv.Key), "/blocks/") {
|
|
|
|
log.Infow("no blocks prefix", "key", string(kv.Key))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
h, err := base32.RawStdEncoding.DecodeString(string(kv.Key[len("/blocks/"):]))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("decode b32 ds key %x: %w", kv.Key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := cid.NewCidV1(cid.Raw, h)
|
|
|
|
|
|
|
|
b, err := block.NewBlockWithCid(kv.Value, c)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("readblk: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wlk.Lock()
|
|
|
|
err = consume(c, b)
|
|
|
|
wlk.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("consume stream block: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := str.Orchestrate(ctx); err != nil {
|
|
|
|
return xerrors.Errorf("orchestrate stream: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infow("write last")
|
|
|
|
|
|
|
|
root, err := carb.finalize()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("carb finalize: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := carb.writeCar(ctx, filepath.Join(out, fmt.Sprintf("chain%d.car", cars)), root); err != nil {
|
|
|
|
return xerrors.Errorf("writeCar: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-03-09 09:26:02 +00:00
|
|
|
var errFullCar = errors.New("full")
|
2022-03-08 21:48:08 +00:00
|
|
|
|
|
|
|
const maxlinks = 16
|
|
|
|
|
|
|
|
type rawCarb struct {
|
|
|
|
blockstore.Blockstore
|
|
|
|
|
|
|
|
max, cur uint64
|
|
|
|
|
|
|
|
nodes []*shedgen.CarbNode
|
|
|
|
|
|
|
|
blocks map[cid.Cid]block.Block
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rawCarb) Has(ctx context.Context, c cid.Cid) (bool, error) {
|
|
|
|
_, has := rc.blocks[c]
|
|
|
|
return has, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rawCarb) Get(ctx context.Context, c cid.Cid) (block.Block, error) {
|
|
|
|
b, has := rc.blocks[c]
|
|
|
|
if !has {
|
2022-06-28 11:09:59 +00:00
|
|
|
return nil, ipld.ErrNotFound{Cid: c}
|
2022-03-08 21:48:08 +00:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rawCarb) GetSize(ctx context.Context, c cid.Cid) (int, error) {
|
|
|
|
b, has := rc.blocks[c]
|
|
|
|
if !has {
|
2022-06-28 11:09:59 +00:00
|
|
|
return 0, ipld.ErrNotFound{Cid: c}
|
2022-03-08 21:48:08 +00:00
|
|
|
}
|
|
|
|
return len(b.RawData()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rawCarb) checkNodes(maxl int) error {
|
|
|
|
if len(rc.nodes) == 0 {
|
|
|
|
log.Infow("add level", "l", 0)
|
|
|
|
rc.nodes = append(rc.nodes, new(shedgen.CarbNode))
|
|
|
|
}
|
|
|
|
for i := 0; i < len(rc.nodes); i++ {
|
|
|
|
if len(rc.nodes[i].Sub) <= maxl {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if len(rc.nodes) <= i+1 {
|
|
|
|
log.Infow("add level", "l", i+1)
|
|
|
|
rc.nodes = append(rc.nodes, new(shedgen.CarbNode))
|
|
|
|
}
|
|
|
|
|
|
|
|
var bb bytes.Buffer
|
|
|
|
if err := rc.nodes[i].MarshalCBOR(&bb); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c, err := cid.Prefix{
|
|
|
|
Version: 1,
|
|
|
|
Codec: cid.DagCBOR,
|
|
|
|
MhType: mh.SHA2_256,
|
|
|
|
MhLength: -1,
|
|
|
|
}.Sum(bb.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("gen cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := block.NewBlockWithCid(bb.Bytes(), c)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("new block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i > 1 {
|
|
|
|
log.Infow("compact", "from", i, "to", i+1, "sub", c.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.nodes[i+1].Sub = append(rc.nodes[i+1].Sub, c)
|
|
|
|
rc.blocks[c] = b
|
|
|
|
rc.nodes[i] = new(shedgen.CarbNode)
|
|
|
|
rc.cur += uint64(bb.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rawCarb) consume(c cid.Cid, b block.Block) error {
|
|
|
|
if err := rc.checkNodes(maxlinks); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rc.cur+uint64(len(b.RawData())) > rc.max {
|
2022-03-09 09:26:02 +00:00
|
|
|
return errFullCar
|
2022-03-08 21:48:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc.cur += uint64(len(b.RawData()))
|
|
|
|
|
|
|
|
b, err := block.NewBlockWithCid(b.RawData(), c)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("create raw block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rc.blocks[c] = b
|
|
|
|
rc.nodes[0].Sub = append(rc.nodes[0].Sub, c)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rawCarb) finalize() (cid.Cid, error) {
|
|
|
|
if len(rc.nodes) == 0 {
|
|
|
|
rc.nodes = append(rc.nodes, new(shedgen.CarbNode))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(rc.nodes); i++ {
|
|
|
|
var bb bytes.Buffer
|
|
|
|
if err := rc.nodes[i].MarshalCBOR(&bb); err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
c, err := cid.Prefix{
|
|
|
|
Version: 1,
|
|
|
|
Codec: cid.DagCBOR,
|
|
|
|
MhType: mh.SHA2_256,
|
|
|
|
MhLength: -1,
|
|
|
|
}.Sum(bb.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("gen cid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := block.NewBlockWithCid(bb.Bytes(), c)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, xerrors.Errorf("new block: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infow("fin", "level", i, "cid", c.String())
|
|
|
|
|
|
|
|
rc.blocks[c] = b
|
|
|
|
rc.nodes[i] = new(shedgen.CarbNode)
|
|
|
|
rc.cur += uint64(bb.Len())
|
|
|
|
|
|
|
|
if len(rc.nodes[i].Sub) <= 1 && i == len(rc.nodes)-1 {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
if len(rc.nodes) <= i+1 {
|
|
|
|
rc.nodes = append(rc.nodes, new(shedgen.CarbNode))
|
|
|
|
}
|
|
|
|
rc.nodes[i+1].Sub = append(rc.nodes[i+1].Sub, c)
|
|
|
|
}
|
|
|
|
return cid.Undef, xerrors.Errorf("failed to finalize")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *rawCarb) writeCar(ctx context.Context, path string, root cid.Cid) error {
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("create out car: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := rc
|
|
|
|
ds := merkledag.NewDAGService(blockservice.New(bs, offline.Exchange(bs)))
|
|
|
|
|
|
|
|
log.Infow("write car", "path", path, "root", root.String(), "blocks", len(rc.blocks))
|
|
|
|
|
|
|
|
return car.WriteCar(ctx, ds, []cid.Cid{root}, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ blockstore.Blockstore = &rawCarb{}
|
|
|
|
|
|
|
|
type badgerLog struct {
|
|
|
|
*zap.SugaredLogger
|
|
|
|
skip2 *zap.SugaredLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *badgerLog) Warningf(format string, args ...interface{}) {
|
|
|
|
b.skip2.Warnf(format, args...)
|
|
|
|
}
|