2021-12-17 03:38:13 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
2022-02-15 23:00:39 +00:00
|
|
|
"bytes"
|
2021-12-17 03:38:13 +00:00
|
|
|
"context"
|
2022-05-26 22:33:04 +00:00
|
|
|
"fmt"
|
2022-06-10 11:47:19 +00:00
|
|
|
"io"
|
2022-07-14 21:40:13 +00:00
|
|
|
"math"
|
2022-04-06 07:38:53 +00:00
|
|
|
"os"
|
2022-06-10 11:47:19 +00:00
|
|
|
"sort"
|
|
|
|
"sync"
|
2022-07-18 14:50:58 +00:00
|
|
|
"sync/atomic"
|
2022-02-23 19:23:20 +00:00
|
|
|
"time"
|
2021-12-17 03:38:13 +00:00
|
|
|
|
2022-04-12 00:45:13 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2022-02-15 23:00:39 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2022-06-28 22:42:58 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2021-12-17 03:38:13 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
ffi "github.com/filecoin-project/filecoin-ffi"
|
2022-01-31 10:31:58 +00:00
|
|
|
ffi_cgo "github.com/filecoin-project/filecoin-ffi/cgo"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-09-06 15:49:29 +00:00
|
|
|
actorstypes "github.com/filecoin-project/go-state-types/actors"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/exitcode"
|
2022-12-13 23:02:34 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/manifest"
|
2023-01-13 19:11:13 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
2021-12-17 03:38:13 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
2022-04-06 07:38:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2022-02-15 23:00:39 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/aerrors"
|
2022-02-15 23:00:39 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
2021-12-17 03:38:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/sigs"
|
2022-06-10 11:47:19 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/bundle"
|
2021-12-17 03:38:13 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 22:46:56 +00:00
|
|
|
var _ Interface = (*FVM)(nil)
|
2022-02-15 23:00:39 +00:00
|
|
|
var _ ffi_cgo.Externs = (*FvmExtern)(nil)
|
2021-12-17 03:38:13 +00:00
|
|
|
|
|
|
|
type FvmExtern struct {
|
2022-01-31 10:31:58 +00:00
|
|
|
Rand
|
|
|
|
blockstore.Blockstore
|
2022-02-15 23:00:39 +00:00
|
|
|
epoch abi.ChainEpoch
|
|
|
|
lbState LookbackStateGetter
|
2022-11-22 17:41:03 +00:00
|
|
|
tsGet TipSetGetter
|
2022-02-15 23:00:39 +00:00
|
|
|
base cid.Cid
|
|
|
|
}
|
|
|
|
|
2022-11-22 17:41:03 +00:00
|
|
|
func (x *FvmExtern) TipsetCid(ctx context.Context, epoch abi.ChainEpoch) (cid.Cid, error) {
|
|
|
|
tsk, err := x.tsGet(ctx, epoch)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
return tsk.Cid()
|
|
|
|
}
|
|
|
|
|
2022-03-13 21:24:13 +00:00
|
|
|
// VerifyConsensusFault is similar to the one in syscalls.go used by the Lotus VM, except it never errors
|
2022-02-15 23:00:39 +00:00
|
|
|
// Errors are logged and "no fault" is returned, which is functionally what go-actors does anyway
|
2022-02-17 04:21:06 +00:00
|
|
|
func (x *FvmExtern) VerifyConsensusFault(ctx context.Context, a, b, extra []byte) (*ffi_cgo.ConsensusFault, int64) {
|
|
|
|
totalGas := int64(0)
|
|
|
|
ret := &ffi_cgo.ConsensusFault{
|
|
|
|
Type: ffi_cgo.ConsensusFaultNone,
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note that block syntax is not validated. Any validly signed block will be accepted pursuant to the below conditions.
|
|
|
|
// Whether or not it could ever have been accepted in a chain is not checked/does not matter here.
|
|
|
|
// for that reason when checking block parent relationships, rather than instantiating a Tipset to do so
|
|
|
|
// (which runs a syntactic check), we do it directly on the CIDs.
|
|
|
|
|
|
|
|
// (0) cheap preliminary checks
|
|
|
|
|
|
|
|
// can blocks be decoded properly?
|
|
|
|
var blockA, blockB types.BlockHeader
|
|
|
|
if decodeErr := blockA.UnmarshalCBOR(bytes.NewReader(a)); decodeErr != nil {
|
|
|
|
log.Info("invalid consensus fault: cannot decode first block header: %w", decodeErr)
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if decodeErr := blockB.UnmarshalCBOR(bytes.NewReader(b)); decodeErr != nil {
|
|
|
|
log.Info("invalid consensus fault: cannot decode second block header: %w", decodeErr)
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// are blocks the same?
|
|
|
|
if blockA.Cid().Equals(blockB.Cid()) {
|
|
|
|
log.Info("invalid consensus fault: submitted blocks are the same")
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
// (1) check conditions necessary to any consensus fault
|
|
|
|
|
|
|
|
// were blocks mined by same miner?
|
|
|
|
if blockA.Miner != blockB.Miner {
|
|
|
|
log.Info("invalid consensus fault: blocks not mined by the same miner")
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// block a must be earlier or equal to block b, epoch wise (ie at least as early in the chain).
|
|
|
|
if blockB.Height < blockA.Height {
|
|
|
|
log.Info("invalid consensus fault: first block must not be of higher height than second")
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.Epoch = blockB.Height
|
|
|
|
|
|
|
|
faultType := ffi_cgo.ConsensusFaultNone
|
|
|
|
|
|
|
|
// (2) check for the consensus faults themselves
|
|
|
|
// (a) double-fork mining fault
|
|
|
|
if blockA.Height == blockB.Height {
|
|
|
|
faultType = ffi_cgo.ConsensusFaultDoubleForkMining
|
|
|
|
}
|
|
|
|
|
|
|
|
// (b) time-offset mining fault
|
|
|
|
// strictly speaking no need to compare heights based on double fork mining check above,
|
|
|
|
// but at same height this would be a different fault.
|
|
|
|
if types.CidArrsEqual(blockA.Parents, blockB.Parents) && blockA.Height != blockB.Height {
|
|
|
|
faultType = ffi_cgo.ConsensusFaultTimeOffsetMining
|
|
|
|
}
|
|
|
|
|
|
|
|
// (c) parent-grinding fault
|
|
|
|
// Here extra is the "witness", a third block that shows the connection between A and B as
|
|
|
|
// A's sibling and B's parent.
|
|
|
|
// Specifically, since A is of lower height, it must be that B was mined omitting A from its tipset
|
|
|
|
//
|
|
|
|
// B
|
|
|
|
// |
|
|
|
|
// [A, C]
|
|
|
|
var blockC types.BlockHeader
|
|
|
|
if len(extra) > 0 {
|
|
|
|
if decodeErr := blockC.UnmarshalCBOR(bytes.NewReader(extra)); decodeErr != nil {
|
|
|
|
log.Info("invalid consensus fault: cannot decode extra: %w", decodeErr)
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if types.CidArrsEqual(blockA.Parents, blockC.Parents) && blockA.Height == blockC.Height &&
|
|
|
|
types.CidArrsContains(blockB.Parents, blockC.Cid()) && !types.CidArrsContains(blockB.Parents, blockA.Cid()) {
|
|
|
|
faultType = ffi_cgo.ConsensusFaultParentGrinding
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// (3) return if no consensus fault by now
|
|
|
|
if faultType == ffi_cgo.ConsensusFaultNone {
|
|
|
|
log.Info("invalid consensus fault: no fault detected")
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// else
|
|
|
|
// (4) expensive final checks
|
|
|
|
|
|
|
|
// check blocks are properly signed by their respective miner
|
|
|
|
// note we do not need to check extra's: it is a parent to block b
|
|
|
|
// which itself is signed, so it was willingly included by the miner
|
2022-12-16 21:54:09 +00:00
|
|
|
gasA, sigErr := x.verifyBlockSig(ctx, &blockA)
|
2022-02-17 04:21:06 +00:00
|
|
|
totalGas += gasA
|
2022-02-15 23:00:39 +00:00
|
|
|
if sigErr != nil {
|
|
|
|
log.Info("invalid consensus fault: cannot verify first block sig: %w", sigErr)
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 21:54:09 +00:00
|
|
|
gas2, sigErr := x.verifyBlockSig(ctx, &blockB)
|
2022-02-17 04:21:06 +00:00
|
|
|
totalGas += gas2
|
2022-02-15 23:00:39 +00:00
|
|
|
if sigErr != nil {
|
|
|
|
log.Info("invalid consensus fault: cannot verify second block sig: %w", sigErr)
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret.Type = faultType
|
2022-02-17 04:21:06 +00:00
|
|
|
ret.Target = blockA.Miner
|
2022-02-23 19:23:20 +00:00
|
|
|
|
2022-02-17 04:21:06 +00:00
|
|
|
return ret, totalGas
|
2022-02-15 23:00:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-16 21:54:09 +00:00
|
|
|
func (x *FvmExtern) verifyBlockSig(ctx context.Context, blk *types.BlockHeader) (int64, error) {
|
2022-02-15 23:00:39 +00:00
|
|
|
waddr, gasUsed, err := x.workerKeyAtLookback(ctx, blk.Miner, blk.Height)
|
|
|
|
if err != nil {
|
|
|
|
return gasUsed, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return gasUsed, sigs.CheckBlockSignature(ctx, blk, waddr)
|
2021-12-17 03:38:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 23:00:39 +00:00
|
|
|
func (x *FvmExtern) workerKeyAtLookback(ctx context.Context, minerId address.Address, height abi.ChainEpoch) (address.Address, int64, error) {
|
2022-04-07 16:22:07 +00:00
|
|
|
if height < x.epoch-policy.ChainFinality {
|
|
|
|
return address.Undef, 0, xerrors.Errorf("cannot get worker key (currEpoch %d, height %d)", x.epoch, height)
|
|
|
|
}
|
|
|
|
|
2022-02-15 23:00:39 +00:00
|
|
|
gasUsed := int64(0)
|
|
|
|
gasAdder := func(gc GasCharge) {
|
|
|
|
// technically not overflow safe, but that's fine
|
|
|
|
gasUsed += gc.Total()
|
|
|
|
}
|
|
|
|
|
|
|
|
cstWithoutGas := cbor.NewCborStore(x.Blockstore)
|
2022-05-30 19:19:51 +00:00
|
|
|
cbb := &gasChargingBlocks{gasAdder, PricelistByEpoch(x.epoch), x.Blockstore}
|
2022-02-15 23:00:39 +00:00
|
|
|
cstWithGas := cbor.NewCborStore(cbb)
|
|
|
|
|
|
|
|
lbState, err := x.lbState(ctx, height)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, gasUsed, err
|
|
|
|
}
|
|
|
|
// get appropriate miner actor
|
|
|
|
act, err := lbState.GetActor(minerId)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, gasUsed, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// use that to get the miner state
|
|
|
|
mas, err := miner.Load(adt.WrapStore(ctx, cstWithGas), act)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, gasUsed, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := mas.Info()
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, gasUsed, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stateTree, err := state.LoadStateTree(cstWithoutGas, x.base)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, gasUsed, err
|
|
|
|
}
|
|
|
|
|
2023-01-13 19:11:13 +00:00
|
|
|
raddr, err := ResolveToDeterministicAddr(stateTree, cstWithGas, info.Worker)
|
2022-02-15 23:00:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, gasUsed, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return raddr, gasUsed, nil
|
2021-12-17 03:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FVM struct {
|
2022-01-31 10:31:58 +00:00
|
|
|
fvm *ffi.FVM
|
2023-01-13 19:11:13 +00:00
|
|
|
nv network.Version
|
2023-02-03 03:10:30 +00:00
|
|
|
|
|
|
|
// returnEvents specifies whether to parse and return events when applying messages.
|
|
|
|
returnEvents bool
|
2021-12-17 03:38:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
func defaultFVMOpts(ctx context.Context, opts *VMOpts) (*ffi.FVMOpts, error) {
|
2022-05-17 00:51:08 +00:00
|
|
|
state, err := state.LoadStateTree(cbor.NewCborStore(opts.Bstore), opts.StateBase)
|
|
|
|
if err != nil {
|
2022-06-29 15:46:51 +00:00
|
|
|
return nil, xerrors.Errorf("loading state tree: %w", err)
|
2022-05-17 00:51:08 +00:00
|
|
|
}
|
2022-03-04 18:52:41 +00:00
|
|
|
|
2022-05-17 00:51:08 +00:00
|
|
|
circToReport, err := opts.CircSupplyCalc(ctx, opts.Epoch, state)
|
|
|
|
if err != nil {
|
2022-06-29 15:46:51 +00:00
|
|
|
return nil, xerrors.Errorf("calculating circ supply: %w", err)
|
2022-03-04 18:52:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
return &ffi.FVMOpts{
|
2022-04-06 07:38:53 +00:00
|
|
|
FVMVersion: 0,
|
|
|
|
Externs: &FvmExtern{
|
|
|
|
Rand: opts.Rand,
|
|
|
|
Blockstore: opts.Bstore,
|
|
|
|
lbState: opts.LookbackState,
|
2022-11-22 17:41:03 +00:00
|
|
|
tsGet: opts.TipSetGetter,
|
2022-04-06 07:38:53 +00:00
|
|
|
base: opts.StateBase,
|
|
|
|
epoch: opts.Epoch,
|
|
|
|
},
|
2022-03-27 02:36:32 +00:00
|
|
|
Epoch: opts.Epoch,
|
2023-01-13 19:11:13 +00:00
|
|
|
Timestamp: opts.Timestamp,
|
|
|
|
ChainID: build.Eip155ChainId,
|
2022-03-27 02:36:32 +00:00
|
|
|
BaseFee: opts.BaseFee,
|
|
|
|
BaseCircSupply: circToReport,
|
|
|
|
NetworkVersion: opts.NetworkVersion,
|
|
|
|
StateBase: opts.StateBase,
|
2022-07-14 19:09:31 +00:00
|
|
|
Tracing: opts.Tracing || EnableDetailedTracing,
|
2023-01-13 19:11:13 +00:00
|
|
|
Debug: build.ActorDebugging,
|
2022-06-29 15:46:51 +00:00
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFVM(ctx context.Context, opts *VMOpts) (*FVM, error) {
|
|
|
|
fvmOpts, err := defaultFVMOpts(ctx, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("creating fvm opts: %w", err)
|
2022-03-27 02:36:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
fvm, err := ffi.CreateFVM(fvmOpts)
|
2022-05-30 19:19:51 +00:00
|
|
|
|
2021-12-17 03:38:13 +00:00
|
|
|
if err != nil {
|
2022-09-08 01:25:28 +00:00
|
|
|
return nil, xerrors.Errorf("failed to create FVM: %w", err)
|
2021-12-17 03:38:13 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 03:10:30 +00:00
|
|
|
ret := &FVM{
|
|
|
|
fvm: fvm,
|
|
|
|
nv: opts.NetworkVersion,
|
|
|
|
returnEvents: opts.ReturnEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2021-12-17 03:38:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 11:47:19 +00:00
|
|
|
func NewDebugFVM(ctx context.Context, opts *VMOpts) (*FVM, error) {
|
|
|
|
baseBstore := opts.Bstore
|
|
|
|
overlayBstore := blockstore.NewMemorySync()
|
2022-06-28 22:42:58 +00:00
|
|
|
cborStore := cbor.NewCborStore(overlayBstore)
|
2022-06-10 11:47:19 +00:00
|
|
|
vmBstore := blockstore.NewTieredBstore(overlayBstore, baseBstore)
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
opts.Bstore = vmBstore
|
|
|
|
fvmOpts, err := defaultFVMOpts(ctx, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("creating fvm opts: %w", err)
|
2022-06-10 11:47:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
fvmOpts.Debug = true
|
|
|
|
|
2022-06-10 11:47:19 +00:00
|
|
|
putMapping := func(ar map[cid.Cid]cid.Cid) (cid.Cid, error) {
|
|
|
|
var mapping xMapping
|
|
|
|
|
|
|
|
mapping.redirects = make([]xRedirect, 0, len(ar))
|
|
|
|
for from, to := range ar {
|
|
|
|
mapping.redirects = append(mapping.redirects, xRedirect{from: from, to: to})
|
|
|
|
}
|
|
|
|
sort.Slice(mapping.redirects, func(i, j int) bool {
|
|
|
|
return bytes.Compare(mapping.redirects[i].from.Bytes(), mapping.redirects[j].from.Bytes()) < 0
|
|
|
|
})
|
|
|
|
|
|
|
|
// Passing this as a pointer of structs has proven to be an enormous PiTA; hence this code.
|
|
|
|
mappingCid, err := cborStore.Put(context.TODO(), &mapping)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mappingCid, nil
|
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
createMapping := func(debugBundlePath string) error {
|
|
|
|
mfCid, err := bundle.LoadBundleFromFile(ctx, overlayBstore, debugBundlePath)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading debug bundle: %w", err)
|
|
|
|
}
|
2022-06-10 11:47:19 +00:00
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
mf, err := actors.LoadManifest(ctx, mfCid, adt.WrapStore(ctx, cborStore))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("loading debug manifest: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-21 19:51:28 +00:00
|
|
|
av, err := actorstypes.VersionForNetwork(opts.NetworkVersion)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting actors version: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
// create actor redirect mapping
|
|
|
|
actorRedirect := make(map[cid.Cid]cid.Cid)
|
2022-12-13 23:02:34 +00:00
|
|
|
for _, key := range manifest.GetBuiltinActorsKeys(av) {
|
2022-09-21 19:51:28 +00:00
|
|
|
from, ok := actors.GetActorCodeID(av, key)
|
2022-06-29 15:46:51 +00:00
|
|
|
if !ok {
|
|
|
|
log.Warnf("actor missing in the from manifest %s", key)
|
|
|
|
continue
|
2022-06-10 11:47:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
to, ok := mf.Get(key)
|
|
|
|
if !ok {
|
|
|
|
log.Warnf("actor missing in the to manifest %s", key)
|
|
|
|
continue
|
2022-06-10 11:47:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
actorRedirect[from] = to
|
|
|
|
}
|
2022-06-10 11:47:19 +00:00
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
if len(actorRedirect) > 0 {
|
|
|
|
mappingCid, err := putMapping(actorRedirect)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("error writing redirect mapping: %w", err)
|
2022-06-10 11:47:19 +00:00
|
|
|
}
|
2022-06-29 15:46:51 +00:00
|
|
|
fvmOpts.ActorRedirect = mappingCid
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-10 11:47:19 +00:00
|
|
|
|
2022-09-06 15:49:29 +00:00
|
|
|
av, err := actorstypes.VersionForNetwork(opts.NetworkVersion)
|
2022-06-29 15:46:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("error determining actors version for network version %d: %w", opts.NetworkVersion, err)
|
|
|
|
}
|
|
|
|
|
2022-10-18 16:45:37 +00:00
|
|
|
debugBundlePath := os.Getenv(fmt.Sprintf("LOTUS_FVM_DEBUG_BUNDLE_V%d", av))
|
|
|
|
if debugBundlePath != "" {
|
|
|
|
if err := createMapping(debugBundlePath); err != nil {
|
|
|
|
log.Errorf("failed to create v%d debug mapping", av)
|
2022-06-10 11:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
fvm, err := ffi.CreateFVM(fvmOpts)
|
2022-06-10 11:47:19 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-03 03:10:30 +00:00
|
|
|
ret := &FVM{
|
|
|
|
fvm: fvm,
|
|
|
|
nv: opts.NetworkVersion,
|
|
|
|
returnEvents: opts.ReturnEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
2022-06-10 11:47:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-17 03:38:13 +00:00
|
|
|
func (vm *FVM) ApplyMessage(ctx context.Context, cmsg types.ChainMsg) (*ApplyRet, error) {
|
2022-02-23 19:23:20 +00:00
|
|
|
start := build.Clock.Now()
|
2022-07-18 14:50:58 +00:00
|
|
|
defer atomic.AddUint64(&StatApplied, 1)
|
2022-05-23 18:15:54 +00:00
|
|
|
vmMsg := cmsg.VMMessage()
|
|
|
|
msgBytes, err := vmMsg.Serialize()
|
2021-12-17 03:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("serializing msg: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-02-07 18:33:57 +00:00
|
|
|
ret, err := vm.fvm.ApplyMessage(msgBytes, uint(cmsg.ChainLength()))
|
2021-12-17 03:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("applying msg: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-23 18:15:54 +00:00
|
|
|
duration := time.Since(start)
|
2023-01-13 19:11:13 +00:00
|
|
|
|
|
|
|
var receipt types.MessageReceipt
|
|
|
|
if vm.nv >= network.Version18 {
|
|
|
|
receipt = types.NewMessageReceiptV1(exitcode.ExitCode(ret.ExitCode), ret.Return, ret.GasUsed, ret.EventsRoot)
|
|
|
|
} else {
|
|
|
|
receipt = types.NewMessageReceiptV0(exitcode.ExitCode(ret.ExitCode), ret.Return, ret.GasUsed)
|
2022-03-13 21:24:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 15:15:14 +00:00
|
|
|
var aerr aerrors.ActorError
|
|
|
|
if ret.ExitCode != 0 {
|
|
|
|
amsg := ret.FailureInfo
|
|
|
|
if amsg == "" {
|
|
|
|
amsg = "unknown error"
|
|
|
|
}
|
|
|
|
aerr = aerrors.New(exitcode.ExitCode(ret.ExitCode), amsg)
|
|
|
|
}
|
|
|
|
|
2022-05-23 18:15:54 +00:00
|
|
|
var et types.ExecutionTrace
|
|
|
|
if len(ret.ExecTraceBytes) != 0 {
|
2023-03-02 00:02:18 +00:00
|
|
|
if err = et.UnmarshalCBOR(bytes.NewReader(ret.ExecTraceBytes)); err != nil {
|
2022-05-23 18:15:54 +00:00
|
|
|
return nil, xerrors.Errorf("failed to unmarshal exectrace: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 03:10:30 +00:00
|
|
|
applyRet := &ApplyRet{
|
2022-05-23 18:15:54 +00:00
|
|
|
MessageReceipt: receipt,
|
2021-12-17 03:38:13 +00:00
|
|
|
GasCosts: &GasOutputs{
|
2022-05-04 10:40:07 +00:00
|
|
|
BaseFeeBurn: ret.BaseFeeBurn,
|
|
|
|
OverEstimationBurn: ret.OverEstimationBurn,
|
2021-12-17 03:38:13 +00:00
|
|
|
MinerPenalty: ret.MinerPenalty,
|
|
|
|
MinerTip: ret.MinerTip,
|
2022-05-04 10:40:07 +00:00
|
|
|
Refund: ret.Refund,
|
|
|
|
GasRefund: ret.GasRefund,
|
|
|
|
GasBurned: ret.GasBurned,
|
2021-12-17 03:38:13 +00:00
|
|
|
},
|
2022-04-20 15:15:14 +00:00
|
|
|
ActorErr: aerr,
|
2022-05-23 18:15:54 +00:00
|
|
|
ExecutionTrace: et,
|
|
|
|
Duration: duration,
|
2023-02-03 03:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if vm.returnEvents && len(ret.EventsBytes) > 0 {
|
|
|
|
applyRet.Events, err = types.DecodeEvents(ret.EventsBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode events returned by the FVM: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return applyRet, nil
|
2021-12-17 03:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *FVM) ApplyImplicitMessage(ctx context.Context, cmsg *types.Message) (*ApplyRet, error) {
|
2022-02-23 19:23:20 +00:00
|
|
|
start := build.Clock.Now()
|
2022-07-18 14:50:58 +00:00
|
|
|
defer atomic.AddUint64(&StatApplied, 1)
|
2022-07-14 21:40:13 +00:00
|
|
|
cmsg.GasLimit = math.MaxInt64 / 2
|
2022-05-23 18:15:54 +00:00
|
|
|
vmMsg := cmsg.VMMessage()
|
|
|
|
msgBytes, err := vmMsg.Serialize()
|
2021-12-17 03:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("serializing msg: %w", err)
|
|
|
|
}
|
2022-02-07 18:33:57 +00:00
|
|
|
ret, err := vm.fvm.ApplyImplicitMessage(msgBytes)
|
2021-12-17 03:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("applying msg: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-23 18:15:54 +00:00
|
|
|
duration := time.Since(start)
|
2023-01-13 19:11:13 +00:00
|
|
|
|
|
|
|
var receipt types.MessageReceipt
|
|
|
|
if vm.nv >= network.Version18 {
|
|
|
|
receipt = types.NewMessageReceiptV1(exitcode.ExitCode(ret.ExitCode), ret.Return, ret.GasUsed, ret.EventsRoot)
|
|
|
|
} else {
|
|
|
|
receipt = types.NewMessageReceiptV0(exitcode.ExitCode(ret.ExitCode), ret.Return, ret.GasUsed)
|
2022-03-13 21:24:13 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 15:15:14 +00:00
|
|
|
var aerr aerrors.ActorError
|
|
|
|
if ret.ExitCode != 0 {
|
|
|
|
amsg := ret.FailureInfo
|
|
|
|
if amsg == "" {
|
|
|
|
amsg = "unknown error"
|
|
|
|
}
|
|
|
|
aerr = aerrors.New(exitcode.ExitCode(ret.ExitCode), amsg)
|
|
|
|
}
|
|
|
|
|
2022-05-23 18:15:54 +00:00
|
|
|
var et types.ExecutionTrace
|
|
|
|
if len(ret.ExecTraceBytes) != 0 {
|
2023-03-02 00:02:18 +00:00
|
|
|
if err = et.UnmarshalCBOR(bytes.NewReader(ret.ExecTraceBytes)); err != nil {
|
2022-05-23 18:15:54 +00:00
|
|
|
return nil, xerrors.Errorf("failed to unmarshal exectrace: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:33:04 +00:00
|
|
|
applyRet := &ApplyRet{
|
2022-05-23 18:15:54 +00:00
|
|
|
MessageReceipt: receipt,
|
2022-04-20 15:15:14 +00:00
|
|
|
ActorErr: aerr,
|
2022-05-23 18:15:54 +00:00
|
|
|
ExecutionTrace: et,
|
|
|
|
Duration: duration,
|
2022-05-26 22:33:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 03:10:30 +00:00
|
|
|
if vm.returnEvents && len(ret.EventsBytes) > 0 {
|
|
|
|
applyRet.Events, err = types.DecodeEvents(ret.EventsBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to decode events returned by the FVM: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 22:33:04 +00:00
|
|
|
return applyRet, nil
|
2021-12-17 03:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *FVM) Flush(ctx context.Context) (cid.Cid, error) {
|
2022-01-31 10:31:58 +00:00
|
|
|
return vm.fvm.Flush()
|
2021-12-17 03:38:13 +00:00
|
|
|
}
|
2022-06-10 11:47:19 +00:00
|
|
|
|
|
|
|
type dualExecutionFVM struct {
|
|
|
|
main *FVM
|
|
|
|
debug *FVM
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Interface = (*dualExecutionFVM)(nil)
|
|
|
|
|
|
|
|
func NewDualExecutionFVM(ctx context.Context, opts *VMOpts) (Interface, error) {
|
|
|
|
main, err := NewFVM(ctx, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
debug, err := NewDebugFVM(ctx, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dualExecutionFVM{
|
|
|
|
main: main,
|
|
|
|
debug: debug,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *dualExecutionFVM) ApplyMessage(ctx context.Context, cmsg types.ChainMsg) (ret *ApplyRet, err error) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ret, err = vm.main.ApplyMessage(ctx, cmsg)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if _, err := vm.debug.ApplyMessage(ctx, cmsg); err != nil {
|
|
|
|
log.Errorf("debug execution failed: %w", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *dualExecutionFVM) ApplyImplicitMessage(ctx context.Context, msg *types.Message) (ret *ApplyRet, err error) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ret, err = vm.main.ApplyImplicitMessage(ctx, msg)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if _, err := vm.debug.ApplyImplicitMessage(ctx, msg); err != nil {
|
|
|
|
log.Errorf("debug execution failed: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vm *dualExecutionFVM) Flush(ctx context.Context) (cid.Cid, error) {
|
|
|
|
return vm.main.Flush(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Passing this as a pointer of structs has proven to be an enormous PiTA; hence this code.
|
|
|
|
type xRedirect struct{ from, to cid.Cid }
|
|
|
|
type xMapping struct{ redirects []xRedirect }
|
|
|
|
|
|
|
|
func (m *xMapping) MarshalCBOR(w io.Writer) error {
|
|
|
|
scratch := make([]byte, 9)
|
|
|
|
if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(len(m.redirects))); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range m.redirects {
|
|
|
|
if err := v.MarshalCBOR(w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *xRedirect) MarshalCBOR(w io.Writer) error {
|
|
|
|
scratch := make([]byte, 9)
|
|
|
|
|
|
|
|
if err := cbg.WriteMajorTypeHeaderBuf(scratch, w, cbg.MajArray, uint64(2)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cbg.WriteCidBuf(scratch, w, r.from); err != nil {
|
|
|
|
return xerrors.Errorf("failed to write cid field from: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cbg.WriteCidBuf(scratch, w, r.to); err != nil {
|
|
|
|
return xerrors.Errorf("failed to write cid field from: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|