2020-08-05 12:20:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"github.com/urfave/cli/v2"
|
2020-08-28 20:41:16 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-08-05 12:20:13 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/oni/tvx/lotus"
|
2020-09-14 19:50:50 +00:00
|
|
|
"github.com/filecoin-project/test-vectors/schema"
|
2020-08-05 12:20:13 +00:00
|
|
|
"github.com/filecoin-project/oni/tvx/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
var extractMsgFlags struct {
|
|
|
|
cid string
|
|
|
|
file string
|
|
|
|
}
|
|
|
|
|
|
|
|
var extractMsgCmd = &cli.Command{
|
|
|
|
Name: "extract-message",
|
|
|
|
Description: "generate a message-class test vector by extracting it from a network",
|
|
|
|
Action: runExtractMsg,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&apiFlag,
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "cid",
|
|
|
|
Usage: "message CID to generate test vector from",
|
|
|
|
Required: true,
|
|
|
|
Destination: &extractMsgFlags.cid,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "file",
|
|
|
|
Usage: "output file",
|
|
|
|
Required: true,
|
|
|
|
Destination: &extractMsgFlags.file,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func runExtractMsg(c *cli.Context) error {
|
|
|
|
// LOTUS_DISABLE_VM_BUF disables what's called "VM state tree buffering",
|
|
|
|
// which stashes write operations in a BufferedBlockstore
|
|
|
|
// (https://github.com/filecoin-project/lotus/blob/b7a4dbb07fd8332b4492313a617e3458f8003b2a/lib/bufbstore/buf_bstore.go#L21)
|
|
|
|
// such that they're not written until the VM is actually flushed.
|
|
|
|
//
|
|
|
|
// For some reason, the standard behaviour was not working for me (raulk),
|
|
|
|
// and disabling it (such that the state transformations are written immediately
|
|
|
|
// to the blockstore) worked.
|
|
|
|
_ = os.Setenv("LOTUS_DISABLE_VM_BUF", "iknowitsabadidea")
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// get the output file.
|
|
|
|
if extractMsgFlags.file == "" {
|
|
|
|
return fmt.Errorf("output file required")
|
|
|
|
}
|
|
|
|
|
|
|
|
mid, err := cid.Decode(extractMsgFlags.cid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the client.
|
|
|
|
api, err := makeClient(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// locate the message.
|
|
|
|
msgInfo, err := api.StateSearchMsg(ctx, mid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to locate message: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the serialized message.
|
|
|
|
msg, err := api.ChainGetMessage(ctx, mid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a read through store that uses ChainGetObject to fetch unknown CIDs.
|
|
|
|
pst := state.NewProxyingStore(ctx, api)
|
|
|
|
|
|
|
|
g := state.NewSurgeon(ctx, api, pst)
|
|
|
|
|
|
|
|
// Get actors accessed by message.
|
|
|
|
retain, err := g.GetAccessedActors(ctx, api, mid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
retain = append(retain, builtin.RewardActorAddr)
|
2020-08-28 20:41:16 +00:00
|
|
|
retain = append(retain, builtin.BurntFundsActorAddr)
|
2020-08-05 12:20:13 +00:00
|
|
|
|
|
|
|
fmt.Println("accessed actors:")
|
|
|
|
for _, k := range retain {
|
|
|
|
fmt.Println("\t", k.String())
|
|
|
|
}
|
|
|
|
|
2020-08-06 09:43:10 +00:00
|
|
|
// get the tipset on which this message was "executed".
|
|
|
|
// https://github.com/filecoin-project/lotus/issues/2847
|
|
|
|
execTs, err := api.ChainGetTipSet(ctx, msgInfo.TipSet)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 09:43:10 +00:00
|
|
|
// get the previous tipset, on which this message was mined.
|
|
|
|
includedTs, err := api.ChainGetTipSet(ctx, execTs.Parents())
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-28 20:41:16 +00:00
|
|
|
neededPrecursorMsgs := make([]*types.Message, 0)
|
2020-08-06 09:43:10 +00:00
|
|
|
for _, b := range includedTs.Blocks() {
|
|
|
|
messages, err := api.ChainGetBlockMessages(ctx, b.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, other := range messages.BlsMessages {
|
|
|
|
if other.Cid() == mid {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if other.From == msg.From && other.Nonce < msg.Nonce {
|
2020-08-28 20:41:16 +00:00
|
|
|
included := false
|
|
|
|
for _, o := range neededPrecursorMsgs {
|
|
|
|
if o.Cid() == other.Cid() {
|
|
|
|
included = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !included {
|
|
|
|
neededPrecursorMsgs = append(neededPrecursorMsgs, other)
|
|
|
|
}
|
2020-08-06 09:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, m := range messages.SecpkMessages {
|
|
|
|
if m.Message.Cid() == mid {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if m.Message.From == msg.From && m.Message.Nonce < msg.Nonce {
|
2020-08-28 20:41:16 +00:00
|
|
|
included := false
|
|
|
|
for _, o := range neededPrecursorMsgs {
|
|
|
|
if o.Cid() == m.Message.Cid() {
|
|
|
|
included = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !included {
|
|
|
|
neededPrecursorMsgs = append(neededPrecursorMsgs, &m.Message)
|
|
|
|
}
|
2020-08-06 09:43:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:20:13 +00:00
|
|
|
fmt.Println("getting the _before_ filtered state tree")
|
2020-08-28 20:41:16 +00:00
|
|
|
tree, err := g.GetStateTreeRootFromTipset(includedTs.Parents())
|
|
|
|
|
|
|
|
driver := lotus.NewDriver(ctx)
|
|
|
|
|
|
|
|
for _, pm := range neededPrecursorMsgs {
|
|
|
|
_, tree, err = driver.ExecuteMessage(pm, tree, pst.Blockstore, execTs.Height())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to execute preceding message: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
preroot, err := g.GetMaskedStateTree(tree, retain)
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 09:43:10 +00:00
|
|
|
_, postroot, err := driver.ExecuteMessage(msg, preroot, pst.Blockstore, execTs.Height())
|
2020-08-05 12:20:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to execute message: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBytes, err := msg.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-16 19:22:38 +00:00
|
|
|
// don't fetch additional content that wasn't accessed yet during car spidering / generation.
|
|
|
|
type onlineblockstore interface {
|
|
|
|
SetOnline(bool)
|
|
|
|
}
|
|
|
|
if ob, ok := pst.Blockstore.(onlineblockstore); ok {
|
|
|
|
ob.SetOnline(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-05 14:52:57 +00:00
|
|
|
out := new(bytes.Buffer)
|
|
|
|
gw := gzip.NewWriter(out)
|
|
|
|
if err := g.WriteCAR(gw, preroot, postroot); err != nil {
|
2020-08-05 12:20:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-08-05 14:52:57 +00:00
|
|
|
if err = gw.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = gw.Close(); err != nil {
|
2020-08-05 12:20:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
version, err := api.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the test vector.
|
2020-08-07 12:47:16 +00:00
|
|
|
vector := schema.TestVector{
|
|
|
|
Class: schema.ClassMessage,
|
2020-09-14 19:50:50 +00:00
|
|
|
Selector: schema.Selector(map[string]string{}),
|
2020-08-07 12:47:16 +00:00
|
|
|
Meta: &schema.Metadata{
|
2020-08-05 12:20:13 +00:00
|
|
|
ID: "TK",
|
|
|
|
Version: "TK",
|
2020-09-14 19:50:50 +00:00
|
|
|
Gen: []schema.GenerationData{schema.GenerationData{
|
2020-09-16 19:22:38 +00:00
|
|
|
Source: msg.Cid().String(),
|
2020-08-05 12:20:13 +00:00
|
|
|
Version: version.String(),
|
2020-09-14 19:50:50 +00:00
|
|
|
}},
|
2020-08-05 12:20:13 +00:00
|
|
|
},
|
2020-08-05 14:52:57 +00:00
|
|
|
CAR: out.Bytes(),
|
2020-08-07 12:47:16 +00:00
|
|
|
Pre: &schema.Preconditions{
|
2020-09-14 19:50:50 +00:00
|
|
|
Epoch: int64(execTs.Height()),
|
2020-08-07 12:47:16 +00:00
|
|
|
StateTree: &schema.StateTree{
|
2020-08-05 14:52:57 +00:00
|
|
|
RootCID: preroot,
|
2020-08-05 12:20:13 +00:00
|
|
|
},
|
|
|
|
},
|
2020-08-07 12:47:16 +00:00
|
|
|
ApplyMessages: []schema.Message{{Bytes: msgBytes}},
|
|
|
|
Post: &schema.Postconditions{
|
|
|
|
StateTree: &schema.StateTree{
|
2020-08-05 14:52:57 +00:00
|
|
|
RootCID: postroot,
|
2020-08-05 12:20:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create(extractMsgFlags.file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
enc := json.NewEncoder(file)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
if err := enc.Encode(&vector); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|