2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-03-27 22:26:34 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-02-10 19:16:36 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-03-18 20:45:37 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
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-03-03 01:24:07 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-02-10 19:16:36 +00:00
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
vtypes "github.com/filecoin-project/chain-validation/chain/types"
|
2020-03-12 17:22:45 +00:00
|
|
|
vdrivers "github.com/filecoin-project/chain-validation/drivers"
|
2020-02-27 22:17:08 +00:00
|
|
|
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-03-26 10:41:02 +00:00
|
|
|
func (a *Applier) ApplyMessage(eCtx *vtypes.ExecutionContext, state vstate.VMWrapper, message *vtypes.Message) (vtypes.MessageReceipt, abi.TokenAmount, abi.TokenAmount, error) {
|
2020-03-25 11:29:35 +00:00
|
|
|
lm := toLotusMsg(message)
|
2020-03-27 22:26:34 +00:00
|
|
|
return a.applyMessage(eCtx, state, lm)
|
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)
|
2020-03-12 17:22:45 +00:00
|
|
|
cs := store.NewChainStore(sw.bs, sw.ds, vdrivers.NewChainValidationSyscalls())
|
2020-03-03 01:24:07 +00:00
|
|
|
sm := stmgr.NewStateManager(cs)
|
|
|
|
|
|
|
|
var bms []stmgr.BlockMessages
|
|
|
|
for _, b := range blocks {
|
|
|
|
bm := stmgr.BlockMessages{
|
2020-03-10 05:43:43 +00:00
|
|
|
Miner: b.Miner,
|
|
|
|
TicketCount: 1,
|
2020-03-03 01:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range b.BLSMessages {
|
|
|
|
bm.BlsMessages = append(bm.BlsMessages, toLotusMsg(m))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range b.SECPMessages {
|
2020-03-25 11:29:35 +00:00
|
|
|
bm.SecpkMessages = append(bm.SecpkMessages, toLotusSignedMsg(m))
|
2020-03-03 01:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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-24 20:09:04 +00:00
|
|
|
rval := ret.Return
|
|
|
|
if rval == nil {
|
|
|
|
rval = []byte{} // chain validation tests expect empty arrays to not be nil...
|
|
|
|
}
|
2020-03-03 01:24:07 +00:00
|
|
|
receipts = append(receipts, vtypes.MessageReceipt{
|
2020-03-27 22:26:34 +00:00
|
|
|
ExitCode: ret.ExitCode,
|
2020-03-24 20:09:04 +00:00
|
|
|
ReturnValue: rval,
|
2020-03-03 01:24:07 +00:00
|
|
|
|
2020-03-26 10:41:02 +00:00
|
|
|
GasUsed: vtypes.GasUnits(ret.GasUsed),
|
2020-03-03 01:24:07 +00:00
|
|
|
})
|
|
|
|
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
|
|
|
|
}
|
2020-03-27 22:26:34 +00:00
|
|
|
func (a *Applier) ApplySignedMessage(eCtx *vtypes.ExecutionContext, state vstate.VMWrapper, msg *vtypes.SignedMessage) (vtypes.MessageReceipt, abi.TokenAmount, abi.TokenAmount, error) {
|
|
|
|
var lm types.ChainMsg
|
|
|
|
switch msg.Signature.Type {
|
|
|
|
case crypto.SigTypeSecp256k1:
|
|
|
|
lm = toLotusSignedMsg(msg)
|
|
|
|
case crypto.SigTypeBLS:
|
|
|
|
lm = toLotusMsg(&msg.Message)
|
|
|
|
default:
|
|
|
|
return vtypes.MessageReceipt{}, big.Zero(), big.Zero(), xerrors.New("Unknown signature type")
|
|
|
|
}
|
|
|
|
// TODO: Validate the sig first
|
|
|
|
return a.applyMessage(eCtx, state, lm)
|
|
|
|
}
|
2020-03-03 01:24:07 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-03-27 22:26:34 +00:00
|
|
|
func (a *Applier) applyMessage(eCtx *vtypes.ExecutionContext, state vstate.VMWrapper, lm types.ChainMsg) (vtypes.MessageReceipt, abi.TokenAmount, abi.TokenAmount, error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
st := state.(*StateWrapper)
|
|
|
|
|
|
|
|
base := st.Root()
|
|
|
|
randSrc := &vmRand{eCtx}
|
|
|
|
lotusVM, err := vm.NewVM(base, eCtx.Epoch, randSrc, eCtx.Miner, st.bs, vdrivers.NewChainValidationSyscalls())
|
|
|
|
if err != nil {
|
|
|
|
return vtypes.MessageReceipt{}, big.Zero(), big.Zero(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := lotusVM.ApplyMessage(ctx, lm)
|
|
|
|
if err != nil {
|
|
|
|
return vtypes.MessageReceipt{}, big.Zero(), big.Zero(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
rval := ret.Return
|
|
|
|
if rval == nil {
|
|
|
|
rval = []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
st.stateRoot, err = lotusVM.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return vtypes.MessageReceipt{}, big.Zero(), big.Zero(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
mr := vtypes.MessageReceipt{
|
|
|
|
ExitCode: ret.ExitCode,
|
|
|
|
ReturnValue: rval,
|
|
|
|
GasUsed: vtypes.GasUnits(ret.GasUsed),
|
|
|
|
}
|
|
|
|
|
|
|
|
return mr, ret.Penalty, abi.NewTokenAmount(ret.GasUsed), nil
|
|
|
|
}
|
|
|
|
|
2020-02-27 22:17:08 +00:00
|
|
|
func toLotusMsg(msg *vtypes.Message) *types.Message {
|
|
|
|
return &types.Message{
|
|
|
|
To: msg.To,
|
|
|
|
From: msg.From,
|
|
|
|
|
2020-03-27 22:26:34 +00:00
|
|
|
Nonce: msg.CallSeqNum,
|
2020-02-27 22:17:08 +00:00
|
|
|
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-03-18 20:45:37 +00:00
|
|
|
GasLimit: msg.GasLimit,
|
2020-02-27 22:17:08 +00:00
|
|
|
|
|
|
|
Params: msg.Params,
|
|
|
|
}
|
|
|
|
}
|
2020-03-25 11:29:35 +00:00
|
|
|
|
|
|
|
func toLotusSignedMsg(msg *vtypes.SignedMessage) *types.SignedMessage {
|
|
|
|
return &types.SignedMessage{
|
|
|
|
Message: *toLotusMsg(&msg.Message),
|
|
|
|
Signature: msg.Signature,
|
|
|
|
}
|
|
|
|
}
|