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"
|
2019-09-30 23:55:35 +00:00
|
|
|
|
2019-10-15 04:33:29 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2019-09-20 02:54:52 +00:00
|
|
|
func (sm *StateManager) CallRaw(ctx context.Context, msg *types.Message, bstate cid.Cid, r vm.Rand, bheight uint64) (*types.MessageReceipt, error) {
|
2019-10-15 04:33:29 +00:00
|
|
|
vmi, err := vm.NewVM(bstate, bheight, r, actors.NetworkAddress, sm.cs.Blockstore())
|
2019-08-12 18:30:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to set up vm: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.GasLimit == types.EmptyInt {
|
|
|
|
msg.GasLimit = types.NewInt(10000000000)
|
|
|
|
}
|
|
|
|
if msg.GasPrice == types.EmptyInt {
|
|
|
|
msg.GasPrice = types.NewInt(0)
|
|
|
|
}
|
|
|
|
if msg.Value == types.EmptyInt {
|
|
|
|
msg.Value = types.NewInt(0)
|
|
|
|
}
|
|
|
|
|
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?
|
|
|
|
ret, err := vmi.ApplyMessage(ctx, msg)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("apply message failed: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-08-12 18:30:20 +00:00
|
|
|
if ret.ActorErr != nil {
|
|
|
|
log.Warnf("chain call failed: %s", ret.ActorErr)
|
|
|
|
}
|
2019-08-13 04:27:54 +00:00
|
|
|
return &ret.MessageReceipt, nil
|
2019-08-16 00:17:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-09 20:03:10 +00:00
|
|
|
func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, 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-10-15 04:33:29 +00:00
|
|
|
r := store.NewChainRand(sm.cs, ts.Cids(), ts.Height(), nil)
|
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
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
return outm, outr, nil
|
|
|
|
}
|