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"
|
|
|
|
|
2020-09-22 10:56:32 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-08-05 12:20:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-15 23:03:58 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2020-09-24 13:06:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/conformance"
|
2020-08-05 12:20:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/blockstore"
|
|
|
|
"github.com/ipld/go-car"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
|
2020-09-14 19:50:50 +00:00
|
|
|
"github.com/filecoin-project/test-vectors/schema"
|
2020-08-05 12:20:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2020-08-07 12:47:16 +00:00
|
|
|
tv schema.TestVector
|
2020-08-05 18:59:57 +00:00
|
|
|
)
|
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 {
|
2020-08-07 12:47:16 +00:00
|
|
|
var tv schema.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-07 12:47:16 +00:00
|
|
|
func executeTestVector(tv schema.TestVector) error {
|
2020-08-10 14:38:34 +00:00
|
|
|
fmt.Println("executing test vector:", tv.Meta.Desc)
|
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)
|
|
|
|
|
2020-09-24 13:06:34 +00:00
|
|
|
driver := conformance.NewDriver(ctx, tv.Selector)
|
2020-08-05 12:20:13 +00:00
|
|
|
|
2020-08-05 15:00:14 +00:00
|
|
|
for i, m := range tv.ApplyMessages {
|
|
|
|
fmt.Printf("decoding message %v\n", i)
|
2020-08-06 16:57:15 +00:00
|
|
|
msg, err := types.DecodeMessage(m.Bytes)
|
2020-08-05 15:00:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 16:57:15 +00:00
|
|
|
// add an epoch if we have set one
|
|
|
|
if m.Epoch != nil {
|
|
|
|
epoch = *m.Epoch
|
|
|
|
}
|
|
|
|
|
2020-08-05 15:00:14 +00:00
|
|
|
fmt.Printf("executing message %v\n", i)
|
2020-08-15 23:03:58 +00:00
|
|
|
var ret *vm.ApplyRet
|
2020-09-24 13:06:34 +00:00
|
|
|
ret, root, err = driver.ExecuteMessage(bs, root, abi.ChainEpoch(epoch), msg)
|
2020-08-05 15:00:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-15 23:03:58 +00:00
|
|
|
|
2020-09-14 19:50:50 +00:00
|
|
|
if expected, actual := tv.Post.Receipts[i].ExitCode, ret.ExitCode; expected != int64(actual) {
|
2020-08-15 23:03:58 +00:00
|
|
|
return fmt.Errorf("exit code of msg %d did not match; expected: %s, got: %s", i, expected, actual)
|
|
|
|
}
|
|
|
|
if expected, actual := tv.Post.Receipts[i].GasUsed, ret.GasUsed; expected != actual {
|
|
|
|
return fmt.Errorf("gas used of msg %d did not match; expected: %d, got: %d", i, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO assert return value
|
|
|
|
fmt.Printf("✅ message %d passed expectations\n", i)
|
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")
|
|
|
|
}
|
|
|
|
}
|