2019-09-06 06:26:02 +00:00
|
|
|
package stmgr
|
2019-08-12 18:30:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-07 21:35:07 +00:00
|
|
|
"errors"
|
2019-09-19 20:25:18 +00:00
|
|
|
"fmt"
|
2020-03-03 00:36:01 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2022-02-24 17:19:59 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2022-06-14 15:00:51 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
"golang.org/x/xerrors"
|
2021-09-18 17:57:04 +00:00
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2021-08-27 00:23:04 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-07-07 20:06:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
2019-10-15 04:33:29 +00:00
|
|
|
|
2020-01-15 21:24:01 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
2020-07-17 17:54:39 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/rand"
|
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2019-08-12 18:30:20 +00:00
|
|
|
)
|
|
|
|
|
2020-10-07 23:14:11 +00:00
|
|
|
var ErrExpensiveFork = errors.New("refusing explicit call due to state fork at epoch")
|
2020-10-07 21:35:07 +00:00
|
|
|
|
2021-08-27 00:23:04 +00:00
|
|
|
// Call applies the given message to the given tipset's parent state, at the epoch following the
|
|
|
|
// tipset's parent. In the presence of null blocks, the height at which the message is invoked may
|
|
|
|
// be less than the specified tipset.
|
|
|
|
//
|
|
|
|
// - If no tipset is specified, the first tipset without an expensive migration is used.
|
|
|
|
// - If executing a message at a given tipset would trigger an expensive migration, the call will
|
|
|
|
// fail with ErrExpensiveFork.
|
2020-09-30 21:56:16 +00:00
|
|
|
func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "statemanager.Call")
|
2019-10-28 09:29:16 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2021-08-27 00:23:04 +00:00
|
|
|
var pheight abi.ChainEpoch = -1
|
|
|
|
|
2020-10-07 21:35:07 +00:00
|
|
|
// If no tipset is provided, try to find one without a fork.
|
2020-09-30 21:56:16 +00:00
|
|
|
if ts == nil {
|
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
2020-10-07 21:35:07 +00:00
|
|
|
// Search back till we find a height with no fork, or we reach the beginning.
|
2021-08-27 00:23:04 +00:00
|
|
|
for ts.Height() > 0 {
|
2021-12-11 21:03:00 +00:00
|
|
|
pts, err := sm.cs.GetTipSetFromKey(ctx, ts.Parents())
|
2020-10-07 21:35:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to find a non-forking epoch: %w", err)
|
|
|
|
}
|
2021-08-27 00:23:04 +00:00
|
|
|
if !sm.hasExpensiveFork(pts.Height()) {
|
|
|
|
pheight = pts.Height()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
ts = pts
|
|
|
|
}
|
2021-08-30 23:23:13 +00:00
|
|
|
} else if ts.Height() > 0 {
|
2021-12-11 21:03:00 +00:00
|
|
|
pts, err := sm.cs.LoadTipSet(ctx, ts.Parents())
|
2021-08-27 00:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to load parent tipset: %w", err)
|
|
|
|
}
|
|
|
|
pheight = pts.Height()
|
|
|
|
if sm.hasExpensiveFork(pheight) {
|
|
|
|
return nil, ErrExpensiveFork
|
2020-10-07 21:35:07 +00:00
|
|
|
}
|
2021-08-30 23:23:13 +00:00
|
|
|
} else {
|
|
|
|
// We can't get the parent tipset in this case.
|
|
|
|
pheight = ts.Height() - 1
|
2020-09-30 21:56:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 23:34:59 +00:00
|
|
|
// Since we're simulating a future message, pretend we're applying it in the "next" tipset
|
2022-02-24 17:19:59 +00:00
|
|
|
vmHeight := pheight + 1
|
2020-09-30 21:56:16 +00:00
|
|
|
bstate := ts.ParentState()
|
2020-10-07 21:35:07 +00:00
|
|
|
|
2020-10-07 23:14:11 +00:00
|
|
|
// Run the (not expensive) migration.
|
2021-09-02 16:07:23 +00:00
|
|
|
bstate, err := sm.HandleStateForks(ctx, bstate, pheight, nil, ts)
|
2020-09-30 21:56:16 +00:00
|
|
|
if err != nil {
|
2020-09-30 22:51:11 +00:00
|
|
|
return nil, fmt.Errorf("failed to handle fork: %w", err)
|
2020-09-30 21:56:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
vmopt := &vm.VMOpts{
|
2020-08-09 22:49:38 +00:00
|
|
|
StateBase: bstate,
|
2022-02-24 17:19:59 +00:00
|
|
|
Epoch: vmHeight,
|
2021-12-17 23:54:17 +00:00
|
|
|
Rand: rand.NewStateRand(sm.cs, ts.Cids(), sm.beacon, sm.GetNetworkVersion),
|
2021-02-28 22:48:36 +00:00
|
|
|
Bstore: sm.cs.StateBlockstore(),
|
2021-09-02 16:07:23 +00:00
|
|
|
Actors: sm.tsExec.NewActorRegistry(),
|
|
|
|
Syscalls: sm.Syscalls,
|
2020-10-11 22:17:28 +00:00
|
|
|
CircSupplyCalc: sm.GetVMCirculatingSupply,
|
2021-12-18 00:05:59 +00:00
|
|
|
NetworkVersion: sm.GetNetworkVersion(ctx, pheight+1),
|
2020-08-09 22:49:38 +00:00
|
|
|
BaseFee: types.NewInt(0),
|
2020-10-24 09:57:17 +00:00
|
|
|
LookbackState: LookbackStateGetterForTipset(sm, ts),
|
2022-07-14 19:09:31 +00:00
|
|
|
Tracing: true,
|
2020-08-06 17:09:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 22:26:08 +00:00
|
|
|
vmi, err := sm.newVM(ctx, vmopt)
|
2019-08-12 18:30:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to set up vm: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-03-18 20:45:37 +00:00
|
|
|
if msg.GasLimit == 0 {
|
2020-07-17 17:54:39 +00:00
|
|
|
msg.GasLimit = build.BlockGasLimit
|
2019-08-12 18:30:20 +00:00
|
|
|
}
|
2020-08-06 21:08:42 +00:00
|
|
|
if msg.GasFeeCap == types.EmptyInt {
|
|
|
|
msg.GasFeeCap = types.NewInt(0)
|
2019-08-12 18:30:20 +00:00
|
|
|
}
|
2020-08-06 21:08:42 +00:00
|
|
|
if msg.GasPremium == types.EmptyInt {
|
|
|
|
msg.GasPremium = types.NewInt(0)
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:30:20 +00:00
|
|
|
if msg.Value == types.EmptyInt {
|
|
|
|
msg.Value = types.NewInt(0)
|
|
|
|
}
|
|
|
|
|
2019-10-28 09:29:16 +00:00
|
|
|
if span.IsRecordingEvents() {
|
|
|
|
span.AddAttributes(
|
2020-03-18 20:45:37 +00:00
|
|
|
trace.Int64Attribute("gas_limit", msg.GasLimit),
|
2020-08-06 21:08:42 +00:00
|
|
|
trace.StringAttribute("gas_feecap", msg.GasFeeCap.String()),
|
2019-10-28 09:29:16 +00:00
|
|
|
trace.StringAttribute("value", msg.Value.String()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-23 03:43:14 +00:00
|
|
|
stTree, err := sm.StateTree(bstate)
|
|
|
|
if err != nil {
|
2022-03-15 23:34:59 +00:00
|
|
|
return nil, xerrors.Errorf("failed to load state tree: %w", err)
|
2022-02-23 03:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fromActor, err := stTree.GetActor(msg.From)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
2019-09-30 23:55:35 +00:00
|
|
|
return nil, xerrors.Errorf("call raw get actor: %s", err)
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.Nonce = fromActor.Nonce
|
|
|
|
|
2019-08-12 18:30:20 +00:00
|
|
|
// TODO: maybe just use the invoker directly?
|
2020-03-25 08:46:42 +00:00
|
|
|
ret, err := vmi.ApplyImplicitMessage(ctx, msg)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("apply message failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-01-15 21:24:01 +00:00
|
|
|
var errs string
|
2019-08-12 18:30:20 +00:00
|
|
|
if ret.ActorErr != nil {
|
2020-01-15 21:24:01 +00:00
|
|
|
errs = ret.ActorErr.Error()
|
2019-08-12 18:30:20 +00:00
|
|
|
log.Warnf("chain call failed: %s", ret.ActorErr)
|
|
|
|
}
|
2020-03-04 01:33:55 +00:00
|
|
|
|
2020-03-03 23:32:17 +00:00
|
|
|
return &api.InvocResult{
|
2020-10-14 03:45:47 +00:00
|
|
|
MsgCid: msg.Cid(),
|
2020-06-11 00:47:28 +00:00
|
|
|
Msg: msg,
|
|
|
|
MsgRct: &ret.MessageReceipt,
|
|
|
|
ExecutionTrace: ret.ExecutionTrace,
|
|
|
|
Error: errs,
|
|
|
|
Duration: ret.Duration,
|
2020-01-15 21:24:01 +00:00
|
|
|
}, nil
|
2019-08-16 00:17:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:46:13 +00:00
|
|
|
func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, priorMsgs []types.ChainMsg, ts *types.TipSet) (*api.InvocResult, error) {
|
2020-07-20 17:48:30 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "statemanager.CallWithGas")
|
|
|
|
defer span.End()
|
|
|
|
|
2022-07-07 20:06:51 +00:00
|
|
|
// Copy the message as we'll be modifying the nonce.
|
|
|
|
msgCopy := *msg
|
|
|
|
msg = &msgCopy
|
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
if ts == nil {
|
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
2020-10-07 21:35:07 +00:00
|
|
|
|
|
|
|
// Search back till we find a height with no fork, or we reach the beginning.
|
|
|
|
// We need the _previous_ height to have no fork, because we'll
|
|
|
|
// run the fork logic in `sm.TipSetState`. We need the _current_
|
|
|
|
// height to have no fork, because we'll run it inside this
|
|
|
|
// function before executing the given message.
|
2021-08-27 00:23:04 +00:00
|
|
|
for ts.Height() > 0 {
|
2021-12-11 21:03:00 +00:00
|
|
|
pts, err := sm.cs.GetTipSetFromKey(ctx, ts.Parents())
|
2020-10-07 21:35:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to find a non-forking epoch: %w", err)
|
|
|
|
}
|
2021-08-27 00:23:04 +00:00
|
|
|
if !sm.hasExpensiveForkBetween(pts.Height(), ts.Height()+1) {
|
|
|
|
break
|
|
|
|
}
|
2020-10-07 21:35:07 +00:00
|
|
|
|
2021-08-27 00:23:04 +00:00
|
|
|
ts = pts
|
|
|
|
}
|
|
|
|
} else if ts.Height() > 0 {
|
2021-12-11 21:03:00 +00:00
|
|
|
pts, err := sm.cs.GetTipSetFromKey(ctx, ts.Parents())
|
2021-08-27 00:23:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to find a non-forking epoch: %w", err)
|
|
|
|
}
|
|
|
|
if sm.hasExpensiveForkBetween(pts.Height(), ts.Height()+1) {
|
|
|
|
return nil, ErrExpensiveFork
|
|
|
|
}
|
2020-07-20 17:48:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 23:34:59 +00:00
|
|
|
// Since we're simulating a future message, pretend we're applying it in the "next" tipset
|
2022-02-24 17:19:59 +00:00
|
|
|
vmHeight := ts.Height() + 1
|
|
|
|
|
|
|
|
stateCid, _, err := sm.TipSetState(ctx, ts)
|
2020-08-08 22:27:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("computing tipset state: %w", err)
|
|
|
|
}
|
2020-07-20 17:48:30 +00:00
|
|
|
|
2021-08-27 00:23:04 +00:00
|
|
|
// Technically, the tipset we're passing in here should be ts+1, but that may not exist.
|
2022-02-24 17:19:59 +00:00
|
|
|
stateCid, err = sm.HandleStateForks(ctx, stateCid, ts.Height(), nil, ts)
|
2021-06-03 16:10:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to handle fork: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-12-17 23:54:17 +00:00
|
|
|
r := rand.NewStateRand(sm.cs, ts.Cids(), sm.beacon, sm.GetNetworkVersion)
|
2020-07-20 17:48:30 +00:00
|
|
|
|
|
|
|
if span.IsRecordingEvents() {
|
|
|
|
span.AddAttributes(
|
|
|
|
trace.Int64Attribute("gas_limit", msg.GasLimit),
|
2020-08-06 21:08:42 +00:00
|
|
|
trace.StringAttribute("gas_feecap", msg.GasFeeCap.String()),
|
2020-07-20 17:48:30 +00:00
|
|
|
trace.StringAttribute("value", msg.Value.String()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:32:18 +00:00
|
|
|
buffStore := blockstore.NewTieredBstore(sm.cs.StateBlockstore(), blockstore.NewMemorySync())
|
2020-08-06 17:09:03 +00:00
|
|
|
vmopt := &vm.VMOpts{
|
2022-02-24 17:19:59 +00:00
|
|
|
StateBase: stateCid,
|
|
|
|
Epoch: vmHeight,
|
2020-08-09 22:49:38 +00:00
|
|
|
Rand: r,
|
2022-03-21 23:32:18 +00:00
|
|
|
Bstore: buffStore,
|
2021-09-02 16:07:23 +00:00
|
|
|
Actors: sm.tsExec.NewActorRegistry(),
|
|
|
|
Syscalls: sm.Syscalls,
|
2020-10-11 22:17:28 +00:00
|
|
|
CircSupplyCalc: sm.GetVMCirculatingSupply,
|
2021-12-18 00:05:59 +00:00
|
|
|
NetworkVersion: sm.GetNetworkVersion(ctx, ts.Height()+1),
|
2020-08-09 22:49:38 +00:00
|
|
|
BaseFee: ts.Blocks()[0].ParentBaseFee,
|
2020-10-24 09:57:17 +00:00
|
|
|
LookbackState: LookbackStateGetterForTipset(sm, ts),
|
2022-07-14 19:09:31 +00:00
|
|
|
Tracing: true,
|
2020-08-06 17:09:03 +00:00
|
|
|
}
|
2020-10-07 22:26:08 +00:00
|
|
|
vmi, err := sm.newVM(ctx, vmopt)
|
2020-07-21 01:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to set up vm: %w", err)
|
|
|
|
}
|
2020-07-22 15:46:13 +00:00
|
|
|
for i, m := range priorMsgs {
|
|
|
|
_, err := vmi.ApplyMessage(ctx, m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("applying prior message (%d, %s): %w", i, m.Cid(), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 23:34:59 +00:00
|
|
|
// We flush to get the VM's view of the state tree after applying the above messages
|
|
|
|
// This is needed to get the correct nonce from the actor state to match the VM
|
2022-02-24 17:19:59 +00:00
|
|
|
stateCid, err = vmi.Flush(ctx)
|
2022-02-23 03:43:14 +00:00
|
|
|
if err != nil {
|
2022-02-24 17:19:59 +00:00
|
|
|
return nil, xerrors.Errorf("flushing vm: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-21 23:32:18 +00:00
|
|
|
stTree, err := state.LoadStateTree(cbor.NewCborStore(buffStore), stateCid)
|
2022-02-24 17:19:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("loading state tree: %w", err)
|
2022-02-23 03:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fromActor, err := stTree.GetActor(msg.From)
|
2020-07-21 01:41:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("call raw get actor: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Nonce = fromActor.Nonce
|
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
fromKey, err := sm.ResolveToKeyAddress(ctx, msg.From, ts)
|
2020-07-20 19:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("could not resolve key: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
var msgApply types.ChainMsg
|
2020-07-20 19:41:05 +00:00
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
switch fromKey.Protocol() {
|
|
|
|
case address.BLS:
|
|
|
|
msgApply = msg
|
|
|
|
case address.SECP256K1:
|
|
|
|
msgApply = &types.SignedMessage{
|
|
|
|
Message: *msg,
|
|
|
|
Signature: crypto.Signature{
|
|
|
|
Type: crypto.SigTypeSecp256k1,
|
|
|
|
Data: make([]byte, 65),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-07 20:06:51 +00:00
|
|
|
// If the fee cap is set to zero, make gas free.
|
|
|
|
if msg.GasFeeCap.NilOrZero() {
|
|
|
|
// Now estimate with a new VM with no base fee.
|
|
|
|
vmopt.BaseFee = big.Zero()
|
|
|
|
vmopt.StateBase = stateCid
|
|
|
|
|
|
|
|
vmi, err = sm.newVM(ctx, vmopt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to set up estimation vm: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
ret, err := vmi.ApplyMessage(ctx, msgApply)
|
|
|
|
if err != nil {
|
2022-07-07 20:06:51 +00:00
|
|
|
return nil, xerrors.Errorf("gas estimation failed: %w", err)
|
2020-07-20 17:48:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var errs string
|
|
|
|
if ret.ActorErr != nil {
|
|
|
|
errs = ret.ActorErr.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.InvocResult{
|
2020-10-14 03:45:47 +00:00
|
|
|
MsgCid: msg.Cid(),
|
2020-07-20 17:48:30 +00:00
|
|
|
Msg: msg,
|
|
|
|
MsgRct: &ret.MessageReceipt,
|
2020-10-15 22:48:51 +00:00
|
|
|
GasCost: MakeMsgGasCost(msg, ret),
|
2020-07-20 17:48:30 +00:00
|
|
|
ExecutionTrace: ret.ExecutionTrace,
|
|
|
|
Error: errs,
|
|
|
|
Duration: ret.Duration,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
var errHaltExecution = fmt.Errorf("halt")
|
|
|
|
|
|
|
|
func (sm *StateManager) Replay(ctx context.Context, ts *types.TipSet, mcid cid.Cid) (*types.Message, *vm.ApplyRet, error) {
|
2021-06-04 00:44:38 +00:00
|
|
|
var finder messageFinder
|
|
|
|
// message to find
|
|
|
|
finder.mcid = mcid
|
|
|
|
|
2022-07-14 19:09:31 +00:00
|
|
|
_, _, err := sm.tsExec.ExecuteTipSet(ctx, sm, ts, &finder, true)
|
2021-03-12 15:21:20 +00:00
|
|
|
if err != nil && !xerrors.Is(err, errHaltExecution) {
|
2019-09-19 20:25:18 +00:00
|
|
|
return nil, nil, xerrors.Errorf("unexpected error during execution: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-06-04 00:44:38 +00:00
|
|
|
if finder.outr == nil {
|
2020-03-10 19:44:50 +00:00
|
|
|
return nil, nil, xerrors.Errorf("given message not found in tipset")
|
|
|
|
}
|
|
|
|
|
2021-06-04 00:44:38 +00:00
|
|
|
return finder.outm, finder.outr, nil
|
2019-09-19 20:25:18 +00:00
|
|
|
}
|