Skip to content

Commit

Permalink
refactor utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jun 22, 2022
1 parent e4d84b2 commit aa85a22
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
37 changes: 4 additions & 33 deletions chain/vm/fvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/state"
cbor "github.com/ipfs/go-ipld-cbor"
car "github.com/ipld/go-car"
cbg "github.com/whyrusleeping/cbor-gen"

"github.com/filecoin-project/go-state-types/abi"
Expand All @@ -38,6 +37,7 @@ import (
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/node/bundle"
)

var _ Interface = (*FVM)(nil)
Expand Down Expand Up @@ -347,41 +347,12 @@ func NewDebugFVM(ctx context.Context, opts *VMOpts) (*FVM, error) {
Debug: true,
}

// TODO move these two util functions somewhere more general
loadBundle := func(bundle string) (cid.Cid, error) {
f, err := os.Open(bundle)
if err != nil {
return cid.Undef, xerrors.Errorf("error opening debug bundle: %w", err)
}
defer f.Close() //nolint

hdr, err := car.LoadCar(ctx, overlayBstore, f)
if err != nil {
return cid.Undef, xerrors.Errorf("error loading debug bundle: %w", err)
}

return hdr.Roots[0], nil
loadBundle := func(path string) (cid.Cid, error) {
return bundle.LoadBundleData(ctx, path, overlayBstore)
}

loadManifest := func(mfCid cid.Cid) (manifest.ManifestData, error) {
adtStore := adt.WrapStore(ctx, cbor.NewCborStore(overlayBstore))

var mf manifest.Manifest
var mfData manifest.ManifestData

if err := adtStore.Get(ctx, mfCid, &mf); err != nil {
return mfData, xerrors.Errorf("error reading debug manifest: %w", err)
}

if err := mf.Load(ctx, adtStore); err != nil {
return mfData, xerrors.Errorf("error loading debug manifest: %w", err)
}

if err := adtStore.Get(ctx, mf.Data, &mfData); err != nil {
return mfData, xerrors.Errorf("error fetching manifest data: %w", err)
}

return mfData, nil
return bundle.LoadManifestData(ctx, mfCid, overlayBstore)
}

putMapping := func(ar map[cid.Cid]cid.Cid) (cid.Cid, error) {
Expand Down
52 changes: 52 additions & 0 deletions node/bundle/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package bundle

import (
"context"
"os"

"golang.org/x/xerrors"

"github.com/filecoin-project/go-state-types/manifest"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/chain/actors/adt"

"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
car "github.com/ipld/go-car"
)

func LoadBundleData(ctx context.Context, bundle string, bs blockstore.Blockstore) (cid.Cid, error) {
f, err := os.Open(bundle)
if err != nil {
return cid.Undef, xerrors.Errorf("error opening debug bundle: %w", err)
}
defer f.Close() //nolint

hdr, err := car.LoadCar(ctx, bs, f)
if err != nil {
return cid.Undef, xerrors.Errorf("error loading debug bundle: %w", err)
}

return hdr.Roots[0], nil
}

func LoadManifestData(ctx context.Context, mfCid cid.Cid, bs blockstore.Blockstore) (manifest.ManifestData, error) {
adtStore := adt.WrapStore(ctx, cbor.NewCborStore(bs))

var mf manifest.Manifest
var mfData manifest.ManifestData

if err := adtStore.Get(ctx, mfCid, &mf); err != nil {
return mfData, xerrors.Errorf("error reading debug manifest: %w", err)
}

if err := mf.Load(ctx, adtStore); err != nil {
return mfData, xerrors.Errorf("error loading debug manifest: %w", err)
}

if err := adtStore.Get(ctx, mf.Data, &mfData); err != nil {
return mfData, xerrors.Errorf("error fetching manifest data: %w", err)
}

return mfData, nil
}

0 comments on commit aa85a22

Please sign in to comment.