2019-10-17 04:53:25 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-13 20:47:27 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
2019-10-17 04:53:25 +00:00
|
|
|
|
|
|
|
vchain "github.com/filecoin-project/chain-validation/pkg/chain"
|
|
|
|
vstate "github.com/filecoin-project/chain-validation/pkg/state"
|
2019-11-26 21:58:43 +00:00
|
|
|
vtypes "github.com/filecoin-project/chain-validation/pkg/state/types"
|
2019-10-17 04:53:25 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
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 {
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ vchain.Applier = &Applier{}
|
|
|
|
|
|
|
|
func NewApplier() *Applier {
|
|
|
|
return &Applier{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Applier) ApplyMessage(eCtx *vchain.ExecutionContext, state vstate.Wrapper, message interface{}) (vchain.MessageReceipt, error) {
|
|
|
|
ctx := context.TODO()
|
|
|
|
st := state.(*StateWrapper)
|
|
|
|
|
|
|
|
base := st.Cid()
|
|
|
|
randSrc := &vmRand{eCtx}
|
2019-11-26 21:58:43 +00:00
|
|
|
minerAddr, err := address.NewFromBytes(eCtx.MinerOwner.Bytes())
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return vchain.MessageReceipt{}, err
|
|
|
|
}
|
2020-01-13 20:47:27 +00:00
|
|
|
lotusVM, err := vm.NewVM(base, eCtx.Epoch, randSrc, minerAddr, st.bs, vm.Syscalls(sectorbuilder.ProofVerifier))
|
2019-10-17 04:53:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return vchain.MessageReceipt{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, err := lotusVM.ApplyMessage(ctx, message.(*types.Message))
|
|
|
|
if err != nil {
|
|
|
|
return vchain.MessageReceipt{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
st.stateRoot, err = lotusVM.Flush(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return vchain.MessageReceipt{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mr := vchain.MessageReceipt{
|
|
|
|
ExitCode: ret.ExitCode,
|
|
|
|
ReturnValue: ret.Return,
|
2019-11-26 21:58:43 +00:00
|
|
|
GasUsed: vtypes.GasUnit(ret.GasUsed.Uint64()),
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:58:43 +00:00
|
|
|
return mr, ret.ActorErr
|
2019-10-17 04:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type vmRand struct {
|
|
|
|
eCtx *vchain.ExecutionContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*vmRand) GetRandomness(ctx context.Context, h int64) ([]byte, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|