2021-12-17 03:38:13 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-23 14:53:50 +00:00
|
|
|
"fmt"
|
2022-02-23 03:43:14 +00:00
|
|
|
"os"
|
2021-12-17 03:38:13 +00:00
|
|
|
|
2022-06-10 11:47:19 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2022-06-28 22:42:58 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
|
|
|
|
2021-12-17 03:38:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
2022-07-18 14:50:58 +00:00
|
|
|
// stat counters
|
|
|
|
var (
|
|
|
|
StatSends uint64
|
|
|
|
StatApplied uint64
|
|
|
|
)
|
|
|
|
|
2023-03-23 14:53:50 +00:00
|
|
|
type ExecutionLane int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ExecutionLaneDefault signifies a default, non prioritized execution lane.
|
|
|
|
ExecutionLaneDefault ExecutionLane = iota
|
|
|
|
// ExecutionLanePriority signifies a prioritized execution lane with reserved resources.
|
|
|
|
ExecutionLanePriority
|
|
|
|
)
|
|
|
|
|
2022-03-15 22:46:56 +00:00
|
|
|
type Interface interface {
|
|
|
|
// Applies the given message onto the VM's current state, returning the result of the execution
|
2021-12-17 03:38:13 +00:00
|
|
|
ApplyMessage(ctx context.Context, cmsg types.ChainMsg) (*ApplyRet, error)
|
2022-03-15 22:46:56 +00:00
|
|
|
// Same as above but for system messages (the Cron invocation and block reward payments).
|
|
|
|
// Must NEVER fail.
|
2021-12-17 03:38:13 +00:00
|
|
|
ApplyImplicitMessage(ctx context.Context, msg *types.Message) (*ApplyRet, error)
|
2022-03-15 22:46:56 +00:00
|
|
|
// Flush all buffered objects into the state store provided to the VM at construction.
|
2021-12-17 03:38:13 +00:00
|
|
|
Flush(ctx context.Context) (cid.Cid, error)
|
|
|
|
}
|
2022-02-23 03:43:14 +00:00
|
|
|
|
2023-03-24 13:48:58 +00:00
|
|
|
// Executor is the general vm execution interface, which is prioritized according to execution lanes.
|
2023-03-23 14:53:50 +00:00
|
|
|
// User must call Done when it is done with this executor to release resource holds by the execution
|
|
|
|
// environment
|
|
|
|
type Executor interface {
|
|
|
|
Interface
|
|
|
|
|
|
|
|
// Done must be called when done with the executor to release resource holds.
|
|
|
|
// It is an error to invoke Interface methods after Done has been called.
|
|
|
|
Done()
|
|
|
|
}
|
|
|
|
|
2022-06-29 15:46:51 +00:00
|
|
|
// WARNING: You will not affect your node's execution by misusing this feature, but you will confuse yourself thoroughly!
|
|
|
|
// An envvar that allows the user to specify debug actors bundles to be used by the FVM
|
|
|
|
// alongside regular execution. This is basically only to be used to print out specific logging information.
|
|
|
|
// Message failures, unexpected terminations,gas costs, etc. should all be ignored.
|
|
|
|
var useFvmDebug = os.Getenv("LOTUS_FVM_DEVELOPER_DEBUG") == "1"
|
|
|
|
|
2023-03-28 15:05:00 +00:00
|
|
|
func makeVM(ctx context.Context, opts *VMOpts) (Interface, error) {
|
2022-03-01 03:57:40 +00:00
|
|
|
if opts.NetworkVersion >= network.Version16 {
|
2022-06-29 15:46:51 +00:00
|
|
|
if useFvmDebug {
|
2022-06-10 11:47:19 +00:00
|
|
|
return NewDualExecutionFVM(ctx, opts)
|
|
|
|
}
|
2022-03-01 03:57:40 +00:00
|
|
|
return NewFVM(ctx, opts)
|
|
|
|
}
|
|
|
|
|
2022-03-15 23:40:17 +00:00
|
|
|
return NewLegacyVM(ctx, opts)
|
2022-02-23 03:43:14 +00:00
|
|
|
}
|
2023-03-23 14:53:50 +00:00
|
|
|
|
|
|
|
func NewVM(ctx context.Context, opts *VMOpts) (Executor, error) {
|
|
|
|
switch opts.ExecutionLane {
|
|
|
|
case ExecutionLaneDefault, ExecutionLanePriority:
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("invalid execution lane: %d", opts.ExecutionLane)
|
|
|
|
}
|
|
|
|
|
|
|
|
token := execution.getToken(opts.ExecutionLane)
|
|
|
|
|
2023-03-28 15:05:00 +00:00
|
|
|
vmi, err := makeVM(ctx, opts)
|
2023-03-23 14:53:50 +00:00
|
|
|
if err != nil {
|
|
|
|
token.Done()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return newVMExecutor(vmi, token), nil
|
|
|
|
}
|