2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-02-10 19:16:36 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-03-08 02:31:36 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-02-27 00:42:39 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/crypto"
|
2020-02-27 22:17:08 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/runtime/exitcode"
|
2020-03-03 01:24:07 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-02-10 19:16:36 +00:00
|
|
|
|
2020-01-13 20:47:27 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
2019-10-17 04:53:25 +00:00
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
vtypes "github.com/filecoin-project/chain-validation/chain/types"
|
|
|
|
vstate "github.com/filecoin-project/chain-validation/state"
|
2019-10-17 04:53:25 +00:00
|
|
|
|
2020-03-03 01:24:07 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2019-10-17 04:53:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Applier applies messages to state trees and storage.
|
|
|
|
type Applier struct {
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
var _ vstate.Applier = &Applier{}
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
func NewApplier() *Applier {
|
|
|
|
return &Applier{}
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (a *Applier) ApplyMessage(eCtx *vtypes.ExecutionContext, state vstate.VMWrapper, message *vtypes.Message) (vtypes.MessageReceipt, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
ctx := context.TODO()
|
|
|
|
st := state.(*StateWrapper)
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
base := st.Root()
|
2019-10-17 04:53:25 +00:00
|
|
|
randSrc := &vmRand{eCtx}
|
2020-02-27 22:17:08 +00:00
|
|
|
lotusVM, err := vm.NewVM(base, abi.ChainEpoch(eCtx.Epoch), randSrc, eCtx.Miner, st.bs, vm.Syscalls(sectorbuilder.ProofVerifier))
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
2020-02-27 22:17:08 +00:00
|
|
|
return vtypes.MessageReceipt{}, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
ret, err := lotusVM.ApplyMessage(ctx, toLotusMsg(message))
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
2020-02-27 22:17:08 +00:00
|
|
|
return vtypes.MessageReceipt{}, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
st.stateRoot, err = lotusVM.Flush(ctx)
|
|
|
|
if err != nil {
|
2020-02-27 22:17:08 +00:00
|
|
|
return vtypes.MessageReceipt{}, err
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
mr := vtypes.MessageReceipt{
|
|
|
|
ExitCode: exitcode.ExitCode(ret.ExitCode),
|
2019-10-17 04:53:25 +00:00
|
|
|
ReturnValue: ret.Return,
|
2020-02-27 22:17:08 +00:00
|
|
|
GasUsed: ret.GasUsed,
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-28 23:14:21 +00:00
|
|
|
return mr, nil
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func (a *Applier) ApplyTipSetMessages(state vstate.VMWrapper, blocks []vtypes.BlockMessagesInfo, epoch abi.ChainEpoch, rnd vstate.RandomnessSource) ([]vtypes.MessageReceipt, error) {
|
2020-03-03 01:24:07 +00:00
|
|
|
sw := state.(*StateWrapper)
|
|
|
|
cs := store.NewChainStore(sw.bs, sw.ds, nil)
|
|
|
|
sm := stmgr.NewStateManager(cs)
|
|
|
|
|
|
|
|
var bms []stmgr.BlockMessages
|
|
|
|
for _, b := range blocks {
|
|
|
|
bm := stmgr.BlockMessages{
|
|
|
|
Miner: b.Miner,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range b.BLSMessages {
|
|
|
|
bm.BlsMessages = append(bm.BlsMessages, toLotusMsg(m))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range b.SECPMessages {
|
|
|
|
bm.SecpkMessages = append(bm.SecpkMessages, toLotusMsg(&m.Message))
|
|
|
|
}
|
|
|
|
|
|
|
|
bms = append(bms, bm)
|
|
|
|
}
|
|
|
|
|
|
|
|
var receipts []vtypes.MessageReceipt
|
2020-03-03 23:01:35 +00:00
|
|
|
sroot, _, err := sm.ApplyBlocks(context.TODO(), state.Root(), bms, epoch, &randWrapper{rnd}, func(c cid.Cid, msg *types.Message, ret *vm.ApplyRet) error {
|
2020-03-08 02:31:36 +00:00
|
|
|
if msg.From == builtin.SystemActorAddr {
|
|
|
|
return nil // ignore reward and cron calls
|
|
|
|
}
|
2020-03-03 01:24:07 +00:00
|
|
|
receipts = append(receipts, vtypes.MessageReceipt{
|
|
|
|
ExitCode: exitcode.ExitCode(ret.ExitCode),
|
|
|
|
ReturnValue: ret.Return,
|
|
|
|
|
|
|
|
GasUsed: ret.GasUsed,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:01:35 +00:00
|
|
|
state.(*StateWrapper).stateRoot = sroot
|
|
|
|
|
2020-03-03 01:24:07 +00:00
|
|
|
return receipts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type randWrapper struct {
|
|
|
|
rnd vstate.RandomnessSource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *randWrapper) GetRandomness(ctx context.Context, pers crypto.DomainSeparationTag, round int64, entropy []byte) ([]byte, error) {
|
|
|
|
return w.rnd.Randomness(ctx, pers, abi.ChainEpoch(round), entropy)
|
2020-02-27 22:17:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-17 04:53:25 +00:00
|
|
|
type vmRand struct {
|
2020-02-27 22:17:08 +00:00
|
|
|
eCtx *vtypes.ExecutionContext
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 00:42:39 +00:00
|
|
|
func (*vmRand) GetRandomness(ctx context.Context, dst crypto.DomainSeparationTag, h int64, input []byte) ([]byte, error) {
|
2019-10-17 04:53:25 +00:00
|
|
|
panic("implement me")
|
|
|
|
}
|
2020-02-27 22:17:08 +00:00
|
|
|
|
|
|
|
func toLotusMsg(msg *vtypes.Message) *types.Message {
|
|
|
|
return &types.Message{
|
|
|
|
To: msg.To,
|
|
|
|
From: msg.From,
|
|
|
|
|
|
|
|
Nonce: uint64(msg.CallSeqNum),
|
|
|
|
Method: msg.Method,
|
|
|
|
|
2020-03-06 06:46:07 +00:00
|
|
|
Value: types.BigInt{Int: msg.Value.Int},
|
|
|
|
GasPrice: types.BigInt{Int: msg.GasPrice.Int},
|
2020-02-27 22:17:08 +00:00
|
|
|
GasLimit: types.NewInt(uint64(msg.GasLimit)),
|
|
|
|
|
|
|
|
Params: msg.Params,
|
|
|
|
}
|
|
|
|
}
|