2019-07-31 15:37:00 +00:00
|
|
|
package actors_test
|
|
|
|
|
|
|
|
import (
|
2019-09-10 23:03:17 +00:00
|
|
|
"bytes"
|
2019-07-31 15:37:00 +00:00
|
|
|
"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"
|
2019-09-10 23:03:17 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-07-31 15:37:00 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
|
|
|
"github.com/filecoin-project/lotus/chain/address"
|
|
|
|
"github.com/filecoin-project/lotus/chain/gen"
|
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2019-07-31 15:37:00 +00:00
|
|
|
)
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
const testGasLimit = 10000
|
2019-08-15 16:23:28 +00:00
|
|
|
|
2019-07-31 15:37:00 +00:00
|
|
|
type HarnessInit struct {
|
|
|
|
NAddrs uint64
|
|
|
|
Addrs map[address.Address]types.BigInt
|
|
|
|
Miner address.Address
|
|
|
|
}
|
|
|
|
|
|
|
|
type HarnessStage int
|
|
|
|
|
|
|
|
const (
|
|
|
|
HarnessPreInit HarnessStage = iota
|
|
|
|
HarnessPostInit
|
|
|
|
)
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
type HarnessOpt func(testing.TB, *Harness) error
|
2019-07-31 15:37:00 +00:00
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
type Harness struct {
|
2019-08-21 20:47:13 +00:00
|
|
|
HI HarnessInit
|
|
|
|
Stage HarnessStage
|
|
|
|
Nonces map[address.Address]uint64
|
|
|
|
GasCharges map[address.Address]types.BigInt
|
|
|
|
|
|
|
|
lastBalanceCheck map[address.Address]types.BigInt
|
2019-07-31 15:37:00 +00:00
|
|
|
|
2019-07-31 17:30:08 +00:00
|
|
|
ctx context.Context
|
|
|
|
bs blockstore.Blockstore
|
|
|
|
vm *vm.VM
|
|
|
|
cs *store.ChainStore
|
2019-08-09 21:41:50 +00:00
|
|
|
w *wallet.Wallet
|
2019-07-31 15:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var HarnessMinerFunds = types.NewInt(1000000)
|
|
|
|
|
|
|
|
func HarnessAddr(addr *address.Address, value uint64) HarnessOpt {
|
2019-08-29 18:20:59 +00:00
|
|
|
return func(t testing.TB, h *Harness) error {
|
2019-07-31 15:37:00 +00:00
|
|
|
if h.Stage != HarnessPreInit {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
hi := &h.HI
|
|
|
|
if addr.Empty() {
|
2019-08-09 21:41:50 +00:00
|
|
|
k, err := h.w.GenerateKey(types.KTSecp256k1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
*addr = k
|
2019-07-31 15:37:00 +00:00
|
|
|
}
|
|
|
|
hi.Addrs[*addr] = types.NewInt(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func HarnessMiner(addr *address.Address) HarnessOpt {
|
2019-08-29 18:20:59 +00:00
|
|
|
return func(_ testing.TB, h *Harness) error {
|
2019-07-31 15:37:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
func HarnessActor(actor *address.Address, creator *address.Address, code cid.Cid, params func() cbg.CBORMarshaler) HarnessOpt {
|
2019-08-29 18:20:59 +00:00
|
|
|
return func(t testing.TB, h *Harness) error {
|
2019-07-31 15:37:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-31 17:30:08 +00:00
|
|
|
func HarnessCtx(ctx context.Context) HarnessOpt {
|
2019-08-29 18:20:59 +00:00
|
|
|
return func(t testing.TB, h *Harness) error {
|
2019-07-31 17:30:08 +00:00
|
|
|
h.ctx = ctx
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
func NewHarness(t *testing.T, options ...HarnessOpt) *Harness {
|
2019-08-09 21:41:50 +00:00
|
|
|
w, err := wallet.NewWallet(wallet.NewMemKeyStore())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-29 18:20:59 +00:00
|
|
|
h := &Harness{
|
2019-07-31 15:37:00 +00:00
|
|
|
Stage: HarnessPreInit,
|
|
|
|
Nonces: make(map[address.Address]uint64),
|
2019-07-31 17:30:08 +00:00
|
|
|
HI: HarnessInit{
|
|
|
|
NAddrs: 1,
|
|
|
|
Miner: blsaddr(0),
|
|
|
|
Addrs: map[address.Address]types.BigInt{
|
|
|
|
blsaddr(0): HarnessMinerFunds,
|
|
|
|
},
|
2019-07-31 15:37:00 +00:00
|
|
|
},
|
2019-08-21 20:47:13 +00:00
|
|
|
GasCharges: make(map[address.Address]types.BigInt),
|
2019-07-31 15:37:00 +00:00
|
|
|
|
2019-08-21 20:47:13 +00:00
|
|
|
lastBalanceCheck: make(map[address.Address]types.BigInt),
|
|
|
|
w: w,
|
|
|
|
ctx: context.Background(),
|
|
|
|
bs: bstore.NewBlockstore(dstore.NewMapDatastore()),
|
2019-07-31 17:30:08 +00:00
|
|
|
}
|
2019-07-31 15:37:00 +00:00
|
|
|
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)
|
2019-10-15 04:33:29 +00:00
|
|
|
h.vm, err = vm.NewVM(stateroot, 1, nil, h.HI.Miner, h.cs.Blockstore())
|
2019-07-31 15:37:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
func (h *Harness) Apply(t testing.TB, msg types.Message) (*vm.ApplyRet, *state.StateTree) {
|
2019-07-31 15:37:00 +00:00
|
|
|
t.Helper()
|
|
|
|
if msg.Nonce == 0 {
|
|
|
|
msg.Nonce, _ = h.Nonces[msg.From]
|
|
|
|
h.Nonces[msg.From] = msg.Nonce + 1
|
|
|
|
}
|
|
|
|
|
2019-07-31 17:30:08 +00:00
|
|
|
ret, err := h.vm.ApplyMessage(h.ctx, &msg)
|
2019-07-31 15:37:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Applying message: %+v", err)
|
|
|
|
}
|
2019-08-21 20:47:13 +00:00
|
|
|
|
|
|
|
if ret != nil {
|
|
|
|
if prev, ok := h.GasCharges[msg.From]; ok {
|
|
|
|
h.GasCharges[msg.From] = types.BigAdd(prev, ret.GasUsed)
|
|
|
|
} else {
|
|
|
|
h.GasCharges[msg.From] = ret.GasUsed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:37:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
func (h *Harness) CreateActor(t testing.TB, from address.Address,
|
2019-09-10 23:03:17 +00:00
|
|
|
code cid.Cid, params cbg.CBORMarshaler) (*vm.ApplyRet, *state.StateTree) {
|
2019-07-31 15:37:00 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
return h.Apply(t, types.Message{
|
2019-10-21 18:12:11 +00:00
|
|
|
To: actors.InitAddress,
|
2019-07-31 15:37:00 +00:00
|
|
|
From: from,
|
|
|
|
Method: actors.IAMethods.Exec,
|
|
|
|
Params: DumpObject(t,
|
|
|
|
&actors.ExecParams{
|
|
|
|
Code: code,
|
|
|
|
Params: DumpObject(t, params),
|
|
|
|
}),
|
|
|
|
GasPrice: types.NewInt(1),
|
2019-08-15 16:23:28 +00:00
|
|
|
GasLimit: types.NewInt(testGasLimit),
|
2019-07-31 15:37:00 +00:00
|
|
|
Value: types.NewInt(0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
func (h *Harness) SendFunds(t testing.TB, from address.Address, to address.Address,
|
2019-07-31 15:37:00 +00:00
|
|
|
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),
|
2019-08-15 16:23:28 +00:00
|
|
|
GasLimit: types.NewInt(testGasLimit),
|
2019-07-31 15:37:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
func (h *Harness) Invoke(t testing.TB, from address.Address, to address.Address,
|
2019-09-10 23:03:17 +00:00
|
|
|
method uint64, params cbg.CBORMarshaler) (*vm.ApplyRet, *state.StateTree) {
|
2019-07-31 15:37:00 +00:00
|
|
|
t.Helper()
|
2019-09-12 23:48:03 +00:00
|
|
|
return h.InvokeWithValue(t, from, to, method, types.NewInt(0), params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Harness) InvokeWithValue(t testing.TB, from address.Address, to address.Address,
|
|
|
|
method uint64, value types.BigInt, params cbg.CBORMarshaler) (*vm.ApplyRet, *state.StateTree) {
|
|
|
|
t.Helper()
|
2019-07-31 15:37:00 +00:00
|
|
|
return h.Apply(t, types.Message{
|
|
|
|
To: to,
|
|
|
|
From: from,
|
|
|
|
Method: method,
|
2019-09-12 23:48:03 +00:00
|
|
|
Value: value,
|
2019-07-31 15:37:00 +00:00
|
|
|
Params: DumpObject(t, params),
|
|
|
|
GasPrice: types.NewInt(1),
|
2019-08-15 16:23:28 +00:00
|
|
|
GasLimit: types.NewInt(testGasLimit),
|
2019-07-31 15:37:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
func (h *Harness) AssertBalance(t testing.TB, addr address.Address, amt uint64) {
|
2019-08-09 21:41:50 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
b, err := h.vm.ActorBalance(addr)
|
|
|
|
if err != nil {
|
2019-08-15 16:23:28 +00:00
|
|
|
t.Fatalf("%+v", err)
|
2019-08-09 21:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if types.BigCmp(types.NewInt(amt), b) != 0 {
|
2019-08-15 16:23:28 +00:00
|
|
|
t.Errorf("expected %s to have balanced of %d. Instead has %s", addr, amt, b)
|
2019-08-09 21:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-29 18:20:59 +00:00
|
|
|
func (h *Harness) AssertBalanceChange(t testing.TB, addr address.Address, amt int64) {
|
2019-08-21 20:47:13 +00:00
|
|
|
t.Helper()
|
|
|
|
lastBalance, ok := h.lastBalanceCheck[addr]
|
|
|
|
if !ok {
|
|
|
|
lastBalance, ok = h.HI.Addrs[addr]
|
|
|
|
if !ok {
|
|
|
|
lastBalance = types.NewInt(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var expected types.BigInt
|
|
|
|
|
|
|
|
if amt >= 0 {
|
|
|
|
expected = types.BigAdd(lastBalance, types.NewInt(uint64(amt)))
|
|
|
|
} else {
|
|
|
|
expected = types.BigSub(lastBalance, types.NewInt(uint64(-amt)))
|
|
|
|
}
|
|
|
|
|
|
|
|
h.lastBalanceCheck[addr] = expected
|
|
|
|
|
|
|
|
if gasUsed, ok := h.GasCharges[addr]; ok {
|
|
|
|
expected = types.BigSub(expected, gasUsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := h.vm.ActorBalance(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if types.BigCmp(expected, b) != 0 {
|
|
|
|
t.Errorf("expected %s to have balanced of %d. Instead has %s", addr, amt, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
func DumpObject(t testing.TB, obj cbg.CBORMarshaler) []byte {
|
|
|
|
if obj == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-31 15:37:00 +00:00
|
|
|
t.Helper()
|
2019-09-10 23:03:17 +00:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
if err := obj.MarshalCBOR(b); err != nil {
|
2019-07-31 15:37:00 +00:00
|
|
|
t.Fatalf("dumping params: %+v", err)
|
|
|
|
}
|
2019-09-10 23:03:17 +00:00
|
|
|
return b.Bytes()
|
2019-07-31 15:37:00 +00:00
|
|
|
}
|