2020-08-05 12:20:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2020-08-05 14:52:57 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-08-05 12:20:13 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
init_ "github.com/filecoin-project/specs-actors/actors/builtin/init"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/oni/tvx/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
func trimQuotes(s string) string {
|
|
|
|
if len(s) >= 2 {
|
|
|
|
if s[0] == '"' && s[len(s)-1] == '"' {
|
|
|
|
return s[1 : len(s)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
var examineFlags struct {
|
|
|
|
file string
|
|
|
|
pre bool
|
|
|
|
post bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var examineCmd = &cli.Command{
|
|
|
|
Name: "examine",
|
|
|
|
Description: "examine an exported state root as represented in a test vector",
|
|
|
|
Action: runExamineCmd,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "file",
|
|
|
|
Usage: "test vector file",
|
|
|
|
Required: true,
|
|
|
|
Destination: &examineFlags.file,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "pre",
|
|
|
|
Usage: "examine the precondition state tree",
|
|
|
|
Destination: &examineFlags.pre,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "post",
|
|
|
|
Usage: "examine the postcondition state tree",
|
|
|
|
Destination: &examineFlags.post,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runExamineCmd(_ *cli.Context) error {
|
|
|
|
file, err := os.Open(examineFlags.file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tv TestVector
|
|
|
|
if err := json.NewDecoder(file).Decode(&tv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:52:57 +00:00
|
|
|
examine := func(root cid.Cid) error {
|
|
|
|
encoded := tv.CAR
|
|
|
|
tree, err := state.RecoverStateTree(context.TODO(), encoded, root)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
initActor, err := tree.GetActor(builtin.InitActorAddr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot recover init actor: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var ias init_.State
|
|
|
|
if err := tree.Store.Get(context.TODO(), initActor.Head, &ias); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
adtStore := adt.WrapStore(context.TODO(), tree.Store)
|
|
|
|
m, err := adt.AsMap(adtStore, ias.AddressMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actors, err := m.CollectKeys()
|
|
|
|
for _, actor := range actors {
|
|
|
|
fmt.Printf("%s\n", actor)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if examineFlags.pre {
|
|
|
|
log.Print("examining precondition tree")
|
2020-08-05 14:52:57 +00:00
|
|
|
if err := examine(tv.Pre.StateTree.RootCID); err != nil {
|
2020-08-05 12:20:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if examineFlags.post {
|
|
|
|
log.Print("examining postcondition tree")
|
2020-08-05 14:52:57 +00:00
|
|
|
if err := examine(tv.Post.StateTree.RootCID); err != nil {
|
2020-08-05 12:20:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|