2019-09-06 06:26:02 +00:00
|
|
|
package stmgr
|
2019-08-12 18:30:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-19 20:25:18 +00:00
|
|
|
"fmt"
|
2020-03-03 00:36:01 +00:00
|
|
|
|
2020-07-20 17:48:30 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-08 02:18:32 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-07-20 17:48:30 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
2019-10-15 04:33:29 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2019-10-28 09:29:16 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-10-15 04:33:29 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-01-15 21:24:01 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-07-17 17:54:39 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
2019-08-12 18:30:20 +00:00
|
|
|
)
|
|
|
|
|
2020-03-08 00:46:12 +00:00
|
|
|
func (sm *StateManager) CallRaw(ctx context.Context, msg *types.Message, bstate cid.Cid, r vm.Rand, bheight abi.ChainEpoch) (*api.InvocResult, error) {
|
2019-10-28 09:29:16 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "statemanager.CallRaw")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
vmopt := &vm.VMOpts{
|
|
|
|
StateBase: bstate,
|
|
|
|
Epoch: bheight,
|
|
|
|
Rand: r,
|
|
|
|
Bstore: sm.cs.Blockstore(),
|
|
|
|
Syscalls: sm.cs.VMSys(),
|
|
|
|
VestedCalc: sm.GetVestedFunds,
|
|
|
|
BaseFee: types.NewInt(0),
|
|
|
|
}
|
|
|
|
|
|
|
|
vmi, err := vm.NewVM(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()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
fromActor, err := vmi.StateTree().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-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-03-03 23:32:17 +00:00
|
|
|
func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*api.InvocResult, error) {
|
2019-08-16 00:17:09 +00:00
|
|
|
if ts == nil {
|
2019-09-06 06:26:02 +00:00
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
2019-08-16 00:17:09 +00:00
|
|
|
}
|
2019-08-16 19:39:09 +00:00
|
|
|
|
2019-10-02 20:03:27 +00:00
|
|
|
state := ts.ParentState()
|
2019-08-16 00:17:09 +00:00
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
r := store.NewChainRand(sm.cs, ts.Cids(), ts.Height())
|
2019-09-20 02:54:52 +00:00
|
|
|
|
|
|
|
return sm.CallRaw(ctx, msg, state, r, ts.Height())
|
2019-08-12 18:30:20 +00:00
|
|
|
}
|
2019-09-19 20:25:18 +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()
|
|
|
|
|
|
|
|
if ts == nil {
|
|
|
|
ts = sm.cs.GetHeaviestTipSet()
|
|
|
|
}
|
|
|
|
|
2020-08-08 20:51:24 +00:00
|
|
|
state, _, 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
|
|
|
|
|
|
|
r := store.NewChainRand(sm.cs, ts.Cids(), ts.Height())
|
|
|
|
|
|
|
|
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()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-06 17:09:03 +00:00
|
|
|
vmopt := &vm.VMOpts{
|
|
|
|
StateBase: state,
|
2020-08-08 20:51:24 +00:00
|
|
|
Epoch: ts.Height() + 1,
|
2020-08-06 17:09:03 +00:00
|
|
|
Rand: r,
|
|
|
|
Bstore: sm.cs.Blockstore(),
|
|
|
|
Syscalls: sm.cs.VMSys(),
|
|
|
|
VestedCalc: sm.GetVestedFunds,
|
|
|
|
BaseFee: ts.Blocks()[0].ParentBaseFee,
|
|
|
|
}
|
|
|
|
vmi, err := vm.NewVM(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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 01:41:18 +00:00
|
|
|
fromActor, err := vmi.StateTree().GetActor(msg.From)
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := vmi.ApplyMessage(ctx, msgApply)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("apply message failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs string
|
|
|
|
if ret.ActorErr != nil {
|
|
|
|
errs = ret.ActorErr.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.InvocResult{
|
|
|
|
Msg: msg,
|
|
|
|
MsgRct: &ret.MessageReceipt,
|
|
|
|
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) {
|
|
|
|
var outm *types.Message
|
|
|
|
var outr *vm.ApplyRet
|
|
|
|
|
2019-09-27 23:55:15 +00:00
|
|
|
_, _, err := sm.computeTipSetState(ctx, ts.Blocks(), func(c cid.Cid, m *types.Message, ret *vm.ApplyRet) error {
|
2019-09-19 20:25:18 +00:00
|
|
|
if c == mcid {
|
|
|
|
outm = m
|
|
|
|
outr = ret
|
|
|
|
return errHaltExecution
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil && err != errHaltExecution {
|
|
|
|
return nil, nil, xerrors.Errorf("unexpected error during execution: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-03-10 19:44:50 +00:00
|
|
|
if outr == nil {
|
|
|
|
return nil, nil, xerrors.Errorf("given message not found in tipset")
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:25:18 +00:00
|
|
|
return outm, outr, nil
|
|
|
|
}
|