2020-08-05 12:20:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-08-05 18:59:57 +00:00
|
|
|
"io"
|
2020-08-05 12:20:13 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/lib/blockstore"
|
|
|
|
"github.com/ipld/go-car"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/oni/tvx/lotus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var execLotusFlags struct {
|
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
|
|
|
var execLotusCmd = &cli.Command{
|
|
|
|
Name: "exec-lotus",
|
|
|
|
Description: "execute a test vector against Lotus",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "file",
|
|
|
|
Usage: "input file",
|
|
|
|
Destination: &execLotusFlags.file,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: runExecLotus,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runExecLotus(_ *cli.Context) error {
|
2020-08-05 18:59:57 +00:00
|
|
|
switch {
|
|
|
|
case execLotusFlags.file != "":
|
|
|
|
file, err := os.Open(execLotusFlags.file)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to open test vector: %w", err)
|
|
|
|
}
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-08-05 18:59:57 +00:00
|
|
|
var (
|
|
|
|
dec = json.NewDecoder(file)
|
|
|
|
tv TestVector
|
|
|
|
)
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-08-05 18:59:57 +00:00
|
|
|
if err = dec.Decode(&tv); err != nil {
|
|
|
|
return fmt.Errorf("failed to decode test vector: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return executeTestVector(tv)
|
|
|
|
default:
|
|
|
|
dec := json.NewDecoder(os.Stdin)
|
|
|
|
for {
|
|
|
|
var tv TestVector
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-08-05 18:59:57 +00:00
|
|
|
err := dec.Decode(&tv)
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = executeTestVector(tv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 12:20:13 +00:00
|
|
|
}
|
2020-08-05 18:59:57 +00:00
|
|
|
}
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-08-05 18:59:57 +00:00
|
|
|
func executeTestVector(tv TestVector) error {
|
|
|
|
fmt.Println("executing test vector")
|
2020-08-05 12:20:13 +00:00
|
|
|
switch tv.Class {
|
|
|
|
case "message":
|
|
|
|
var (
|
2020-08-05 18:59:57 +00:00
|
|
|
ctx = context.Background()
|
|
|
|
epoch = tv.Pre.Epoch
|
|
|
|
root = tv.Pre.StateTree.RootCID
|
2020-08-05 12:20:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
bs := blockstore.NewTemporary()
|
|
|
|
|
2020-08-05 14:52:57 +00:00
|
|
|
buf := bytes.NewReader(tv.CAR)
|
2020-08-05 12:20:13 +00:00
|
|
|
gr, err := gzip.NewReader(buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer gr.Close()
|
|
|
|
|
|
|
|
header, err := car.LoadCar(bs, gr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to load state tree car from test vector: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("roots: ", header.Roots)
|
|
|
|
|
|
|
|
driver := lotus.NewDriver(ctx)
|
|
|
|
|
2020-08-05 15:00:14 +00:00
|
|
|
for i, m := range tv.ApplyMessages {
|
|
|
|
fmt.Printf("decoding message %v\n", i)
|
|
|
|
msg, err := types.DecodeMessage(m)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("executing message %v\n", i)
|
2020-08-05 18:59:57 +00:00
|
|
|
_, root, err = driver.ExecuteMessage(msg, root, bs, epoch)
|
2020-08-05 15:00:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-05 18:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if root != tv.Post.StateTree.RootCID {
|
|
|
|
return fmt.Errorf("wrong post root cid; expected %v , but got %v", tv.Post.StateTree.RootCID, root)
|
2020-08-05 15:00:14 +00:00
|
|
|
}
|
2020-08-05 12:20:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("test vector class not supported")
|
|
|
|
}
|
|
|
|
}
|