2019-08-13 04:27:54 +00:00
|
|
|
package vm
|
2019-08-12 18:30:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-08-16 00:17:09 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2019-08-12 18:30:20 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
2019-08-16 00:17:09 +00:00
|
|
|
func CallRaw(ctx context.Context, cs *store.ChainStore, msg *types.Message, bstate cid.Cid, bheight uint64) (*types.MessageReceipt, error) {
|
|
|
|
vmi, err := NewVM(bstate, bheight, actors.NetworkAddress, cs)
|
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)
|
|
|
|
}
|
|
|
|
if msg.Params == nil {
|
|
|
|
msg.Params, err = actors.SerializeParams(struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:27:54 +00:00
|
|
|
fromActor, err := vmi.cstate.GetActor(msg.From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func Call(ctx context.Context, cs *store.ChainStore, msg *types.Message, ts *types.TipSet) (*types.MessageReceipt, error) {
|
|
|
|
if ts == nil {
|
|
|
|
ts = cs.GetHeaviestTipSet()
|
|
|
|
}
|
2019-08-16 19:39:09 +00:00
|
|
|
|
2019-08-16 00:17:09 +00:00
|
|
|
state, err := cs.TipSetState(ts.Cids())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallRaw(ctx, cs, msg, state, ts.Height())
|
2019-08-12 18:30:20 +00:00
|
|
|
}
|