Wip on tests
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
0a37ca0344
commit
f19b8c82f4
@ -87,8 +87,8 @@ var MultiSigMethods = musigMethods{1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||
func (msa MultiSigActor) Exports() []interface{} {
|
||||
return []interface{}{
|
||||
1: msa.MultiSigConstructor,
|
||||
2: msa.Approve,
|
||||
3: msa.Propose,
|
||||
2: msa.Propose,
|
||||
3: msa.Approve,
|
||||
4: msa.Cancel,
|
||||
//5: msa.ClearCompleted,
|
||||
6: msa.AddSigner,
|
||||
|
80
chain/actors/actor_multisig_test.go
Normal file
80
chain/actors/actor_multisig_test.go
Normal file
@ -0,0 +1,80 @@
|
||||
package actors_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
cbor "github.com/ipfs/go-ipld-cbor"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/chain/actors"
|
||||
"github.com/filecoin-project/go-lotus/chain/address"
|
||||
"github.com/filecoin-project/go-lotus/chain/types"
|
||||
"github.com/filecoin-project/go-lotus/chain/vm"
|
||||
)
|
||||
|
||||
func TestMultiSigCreate(t *testing.T) {
|
||||
var creatorAddr, sig1Addr, sig2Addr, outsideAddr address.Address
|
||||
opts := []HarnessOpt{
|
||||
HarnessAddr(&creatorAddr, 10000),
|
||||
HarnessAddr(&sig1Addr, 10000),
|
||||
HarnessAddr(&sig2Addr, 10000),
|
||||
HarnessAddr(&outsideAddr, 10000),
|
||||
}
|
||||
|
||||
h := NewHarness2(t, opts...)
|
||||
ret, _ := h.CreateActor(t, creatorAddr, actors.MultisigActorCodeCid,
|
||||
actors.MultiSigConstructorParams{
|
||||
Signers: []address.Address{creatorAddr, sig1Addr, sig2Addr},
|
||||
Required: 2,
|
||||
})
|
||||
ApplyOK(t, ret)
|
||||
}
|
||||
|
||||
func ApplyOK(t testing.TB, ret *vm.ApplyRet) {
|
||||
t.Helper()
|
||||
if ret.ExitCode != 0 {
|
||||
t.Fatalf("exit code should be 0, got %d, actorErr: %+v", ret.ExitCode, ret.ActorErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultiSigOps(t *testing.T) {
|
||||
var creatorAddr, sig1Addr, sig2Addr, outsideAddr address.Address
|
||||
var multSigAddr address.Address
|
||||
opts := []HarnessOpt{
|
||||
HarnessAddr(&creatorAddr, 10000),
|
||||
HarnessAddr(&sig1Addr, 10000),
|
||||
HarnessAddr(&sig2Addr, 10000),
|
||||
HarnessAddr(&outsideAddr, 0),
|
||||
HarnessActor(&multSigAddr, &creatorAddr, actors.MultisigActorCodeCid,
|
||||
func() interface{} {
|
||||
return actors.MultiSigConstructorParams{
|
||||
Signers: []address.Address{creatorAddr, sig1Addr, sig2Addr},
|
||||
Required: 2,
|
||||
}
|
||||
}),
|
||||
}
|
||||
|
||||
h := NewHarness2(t, opts...)
|
||||
{
|
||||
sendVal := types.NewInt(2000)
|
||||
ret, state := h.SendFunds(t, creatorAddr, multSigAddr, sendVal)
|
||||
ApplyOK(t, ret)
|
||||
ms, err := state.GetActor(multSigAddr)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, sendVal, ms.Balance)
|
||||
}
|
||||
|
||||
{
|
||||
sendVal := types.NewInt(100)
|
||||
ret, _ := h.Invoke(t, creatorAddr, multSigAddr, actors.MultiSigMethods.Propose,
|
||||
actors.MultiSigProposeParams{
|
||||
To: outsideAddr,
|
||||
Value: sendVal,
|
||||
})
|
||||
ApplyOK(t, ret)
|
||||
var txIdParam actors.MultiSigTxID
|
||||
err := cbor.DecodeInto(ret.Return, &txIdParam.TxID)
|
||||
assert.NoError(t, err, "decoding txid")
|
||||
}
|
||||
|
||||
}
|
@ -44,8 +44,8 @@ func TestStorageMarketCreateMiner(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if sminer.String() != "t0102" {
|
||||
t.Fatal("hold up")
|
||||
if sminer.String() != "t0103" {
|
||||
t.Fatalf("hold up got: %s", sminer)
|
||||
}
|
||||
h.Steps[1].M.Params = h.DumpObject(&IsMinerParam{Addr: sminer})
|
||||
h.Steps[2].M.Params = h.DumpObject(&PowerLookupParams{Miner: sminer})
|
||||
|
224
chain/actors/harness2_test.go
Normal file
224
chain/actors/harness2_test.go
Normal file
@ -0,0 +1,224 @@
|
||||
package actors_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/ipfs/go-cid"
|
||||
dstore "github.com/ipfs/go-datastore"
|
||||
hamt "github.com/ipfs/go-hamt-ipld"
|
||||
blockstore "github.com/ipfs/go-ipfs-blockstore"
|
||||
bstore "github.com/ipfs/go-ipfs-blockstore"
|
||||
cbor "github.com/ipfs/go-ipld-cbor"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/filecoin-project/go-lotus/chain/actors"
|
||||
"github.com/filecoin-project/go-lotus/chain/address"
|
||||
"github.com/filecoin-project/go-lotus/chain/gen"
|
||||
"github.com/filecoin-project/go-lotus/chain/state"
|
||||
"github.com/filecoin-project/go-lotus/chain/store"
|
||||
"github.com/filecoin-project/go-lotus/chain/types"
|
||||
"github.com/filecoin-project/go-lotus/chain/vm"
|
||||
)
|
||||
|
||||
type HarnessInit struct {
|
||||
NAddrs uint64
|
||||
Addrs map[address.Address]types.BigInt
|
||||
Miner address.Address
|
||||
}
|
||||
|
||||
type HarnessStage int
|
||||
|
||||
const (
|
||||
HarnessPreInit HarnessStage = iota
|
||||
HarnessPostInit
|
||||
)
|
||||
|
||||
type HarnessOpt func(testing.TB, *Harness2) error
|
||||
|
||||
type Harness2 struct {
|
||||
HI HarnessInit
|
||||
Stage HarnessStage
|
||||
Nonces map[address.Address]uint64
|
||||
|
||||
bs blockstore.Blockstore
|
||||
vm *vm.VM
|
||||
cs *store.ChainStore
|
||||
}
|
||||
|
||||
var HarnessMinerFunds = types.NewInt(1000000)
|
||||
|
||||
func HarnessAddr(addr *address.Address, value uint64) HarnessOpt {
|
||||
return func(t testing.TB, h *Harness2) error {
|
||||
if h.Stage != HarnessPreInit {
|
||||
return nil
|
||||
}
|
||||
hi := &h.HI
|
||||
if addr.Empty() {
|
||||
*addr = blsaddr(hi.NAddrs)
|
||||
hi.NAddrs++
|
||||
}
|
||||
hi.Addrs[*addr] = types.NewInt(value)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func HarnessMiner(addr *address.Address) HarnessOpt {
|
||||
return func(_ testing.TB, h *Harness2) error {
|
||||
if h.Stage != HarnessPreInit {
|
||||
return nil
|
||||
}
|
||||
hi := &h.HI
|
||||
if addr.Empty() {
|
||||
*addr = hi.Miner
|
||||
return nil
|
||||
}
|
||||
delete(hi.Addrs, hi.Miner)
|
||||
hi.Miner = *addr
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func HarnessActor(actor *address.Address, creator *address.Address, code cid.Cid, params func() interface{}) HarnessOpt {
|
||||
return func(t testing.TB, h *Harness2) error {
|
||||
if h.Stage != HarnessPostInit {
|
||||
return nil
|
||||
}
|
||||
if !actor.Empty() {
|
||||
return xerrors.New("actor address should be empty")
|
||||
}
|
||||
|
||||
ret, _ := h.CreateActor(t, *creator, code, params())
|
||||
if ret.ExitCode != 0 {
|
||||
return xerrors.Errorf("creating actor: %w", ret.ActorErr)
|
||||
}
|
||||
var err error
|
||||
*actor, err = address.NewFromBytes(ret.Return)
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func NewHarness2(t *testing.T, options ...HarnessOpt) *Harness2 {
|
||||
h := &Harness2{
|
||||
Stage: HarnessPreInit,
|
||||
Nonces: make(map[address.Address]uint64),
|
||||
}
|
||||
h.bs = bstore.NewBlockstore(dstore.NewMapDatastore())
|
||||
h.HI = HarnessInit{
|
||||
NAddrs: 1,
|
||||
Miner: blsaddr(0),
|
||||
Addrs: map[address.Address]types.BigInt{
|
||||
blsaddr(0): HarnessMinerFunds,
|
||||
},
|
||||
}
|
||||
|
||||
for _, opt := range options {
|
||||
err := opt(t, h)
|
||||
if err != nil {
|
||||
t.Fatalf("Applying options: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
st, err := gen.MakeInitialStateTree(h.bs, h.HI.Addrs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stateroot, err := st.Flush()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h.cs = store.NewChainStore(h.bs, nil)
|
||||
h.vm, err = vm.NewVM(stateroot, 1, h.HI.Miner, h.cs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h.Stage = HarnessPostInit
|
||||
for _, opt := range options {
|
||||
err := opt(t, h)
|
||||
if err != nil {
|
||||
t.Fatalf("Applying options: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *Harness2) Apply(t testing.TB, msg types.Message) (*vm.ApplyRet, *state.StateTree) {
|
||||
t.Helper()
|
||||
if msg.Nonce == 0 {
|
||||
msg.Nonce, _ = h.Nonces[msg.From]
|
||||
h.Nonces[msg.From] = msg.Nonce + 1
|
||||
}
|
||||
|
||||
ret, err := h.vm.ApplyMessage(context.TODO(), &msg)
|
||||
if err != nil {
|
||||
t.Fatalf("Applying message: %+v", err)
|
||||
}
|
||||
stateroot, err := h.vm.Flush(context.TODO())
|
||||
if err != nil {
|
||||
t.Fatalf("Flushing VM: %+v", err)
|
||||
}
|
||||
cst := hamt.CSTFromBstore(h.bs)
|
||||
state, err := state.LoadStateTree(cst, stateroot)
|
||||
if err != nil {
|
||||
t.Fatalf("Loading state tree: %+v", err)
|
||||
}
|
||||
return ret, state
|
||||
}
|
||||
|
||||
func (h *Harness2) CreateActor(t testing.TB, from address.Address,
|
||||
code cid.Cid, params interface{}) (*vm.ApplyRet, *state.StateTree) {
|
||||
t.Helper()
|
||||
|
||||
return h.Apply(t, types.Message{
|
||||
To: actors.InitActorAddress,
|
||||
From: from,
|
||||
Method: actors.IAMethods.Exec,
|
||||
Params: DumpObject(t,
|
||||
&actors.ExecParams{
|
||||
Code: code,
|
||||
Params: DumpObject(t, params),
|
||||
}),
|
||||
GasPrice: types.NewInt(1),
|
||||
GasLimit: types.NewInt(1),
|
||||
Value: types.NewInt(0),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Harness2) SendFunds(t testing.TB, from address.Address, to address.Address,
|
||||
value types.BigInt) (*vm.ApplyRet, *state.StateTree) {
|
||||
t.Helper()
|
||||
return h.Apply(t, types.Message{
|
||||
To: to,
|
||||
From: from,
|
||||
Method: 0,
|
||||
Value: value,
|
||||
GasPrice: types.NewInt(1),
|
||||
GasLimit: types.NewInt(1),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Harness2) Invoke(t testing.TB, from address.Address, to address.Address,
|
||||
method uint64, params interface{}) (*vm.ApplyRet, *state.StateTree) {
|
||||
t.Helper()
|
||||
return h.Apply(t, types.Message{
|
||||
To: to,
|
||||
From: from,
|
||||
Method: method,
|
||||
Value: types.NewInt(0),
|
||||
Params: DumpObject(t, params),
|
||||
GasPrice: types.NewInt(1),
|
||||
GasLimit: types.NewInt(1),
|
||||
})
|
||||
}
|
||||
|
||||
func DumpObject(t testing.TB, obj interface{}) []byte {
|
||||
t.Helper()
|
||||
enc, err := cbor.DumpObject(obj)
|
||||
if err != nil {
|
||||
t.Fatalf("dumping params: %+v", err)
|
||||
}
|
||||
return enc
|
||||
}
|
@ -45,7 +45,7 @@ func NewHarness(t *testing.T) *Harness {
|
||||
|
||||
from := blsaddr(0)
|
||||
maddr := blsaddr(1)
|
||||
third := blsaddr(1)
|
||||
third := blsaddr(2)
|
||||
|
||||
actors := map[address.Address]types.BigInt{
|
||||
from: types.NewInt(1000000),
|
||||
@ -64,7 +64,7 @@ func NewHarness(t *testing.T) *Harness {
|
||||
|
||||
h.cs = store.NewChainStore(h.bs, nil)
|
||||
|
||||
h.vm, err = vm.NewVM(stateroot, IAMethods.Exec, maddr, h.cs)
|
||||
h.vm, err = vm.NewVM(stateroot, 1, maddr, h.cs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -83,7 +83,7 @@ func (h *Harness) Execute() *state.StateTree {
|
||||
} else {
|
||||
h.NoError(h.t, err)
|
||||
}
|
||||
step.Ret(h.t, ret)
|
||||
step.Ret(h.t, &ret.MessageReceipt)
|
||||
}
|
||||
stateroot, err := h.vm.Flush(context.TODO())
|
||||
if err != nil {
|
||||
@ -141,7 +141,7 @@ func TestVMInvokeHarness(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if outaddr.String() != "t0102" {
|
||||
if outaddr.String() != "t0103" {
|
||||
t.Fatal("hold up")
|
||||
}
|
||||
},
|
||||
|
@ -62,7 +62,7 @@ func MinerCreateBlock(ctx context.Context, cs *store.ChainStore, miner address.A
|
||||
return nil, errors.Wrap(err, "apply message failure")
|
||||
}
|
||||
|
||||
receipts = append(receipts, rec)
|
||||
receipts = append(receipts, rec.MessageReceipt)
|
||||
}
|
||||
|
||||
cst := hamt.CSTFromBstore(cs.Blockstore())
|
||||
|
@ -27,6 +27,7 @@ func newInvoker() *invoker {
|
||||
inv.register(actors.InitActorCodeCid, actors.InitActor{})
|
||||
inv.register(actors.StorageMarketActorCodeCid, actors.StorageMarketActor{})
|
||||
inv.register(actors.StorageMinerCodeCid, actors.StorageMinerActor{})
|
||||
inv.register(actors.MultisigActorCodeCid, actors.MultiSigActor{})
|
||||
|
||||
return inv
|
||||
}
|
||||
|
@ -199,7 +199,12 @@ func NewVM(base cid.Cid, height uint64, maddr address.Address, cs *store.ChainSt
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*types.MessageReceipt, error) {
|
||||
type ApplyRet struct {
|
||||
types.MessageReceipt
|
||||
ActorErr aerrors.ActorError
|
||||
}
|
||||
|
||||
func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "vm.ApplyMessage")
|
||||
defer span.End()
|
||||
|
||||
@ -246,15 +251,16 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*types.Mess
|
||||
|
||||
var errcode byte
|
||||
var ret []byte
|
||||
if msg.Method != 0 {
|
||||
var err aerrors.ActorError
|
||||
ret, err = vm.Invoke(toActor, vmctx, msg.Method, msg.Params)
|
||||
var actorError aerrors.ActorError
|
||||
|
||||
if aerrors.IsFatal(err) {
|
||||
return nil, xerrors.Errorf("during invoke: %w", err)
|
||||
if msg.Method != 0 {
|
||||
ret, actorError = vm.Invoke(toActor, vmctx, msg.Method, msg.Params)
|
||||
|
||||
if aerrors.IsFatal(actorError) {
|
||||
return nil, xerrors.Errorf("during invoke: %w", actorError)
|
||||
}
|
||||
|
||||
if errcode = aerrors.RetCode(err); errcode != 0 {
|
||||
if errcode = aerrors.RetCode(actorError); errcode != 0 {
|
||||
// revert all state changes since snapshot
|
||||
if err := st.Revert(); err != nil {
|
||||
return nil, xerrors.Errorf("revert state failed: %w", err)
|
||||
@ -283,10 +289,13 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*types.Mess
|
||||
gasReward := types.BigMul(msg.GasPrice, vmctx.GasUsed())
|
||||
DepositFunds(miner, gasReward)
|
||||
|
||||
return &types.MessageReceipt{
|
||||
ExitCode: errcode,
|
||||
Return: ret,
|
||||
GasUsed: vmctx.GasUsed(),
|
||||
return &ApplyRet{
|
||||
MessageReceipt: types.MessageReceipt{
|
||||
ExitCode: errcode,
|
||||
Return: ret,
|
||||
GasUsed: vmctx.GasUsed(),
|
||||
},
|
||||
ActorErr: actorError,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user