2020-09-14 09:49:58 +00:00
|
|
|
package paychmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
ds "github.com/ipfs/go-datastore"
|
|
|
|
ds_sync "github.com/ipfs/go-datastore/sync"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-08 01:09:33 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
|
|
|
paychtypes "github.com/filecoin-project/go-state-types/builtin/v8/paych"
|
2020-10-08 01:09:33 +00:00
|
|
|
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
|
|
|
|
tutils2 "github.com/filecoin-project/specs-actors/v2/support/testing"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/paych"
|
|
|
|
paychmock "github.com/filecoin-project/lotus/chain/actors/builtin/paych/mock"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-09-14 09:49:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestPaychAddVoucherAfterAddFunds tests adding a voucher to a channel with
|
|
|
|
// insufficient funds, then adding funds to the channel, then adding the
|
|
|
|
// voucher again
|
|
|
|
func TestPaychAddVoucherAfterAddFunds(t *testing.T) {
|
2021-12-14 15:56:16 +00:00
|
|
|
//stm: @TOKEN_PAYCH_WAIT_READY_001
|
2020-09-14 09:49:58 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
store := NewStore(ds_sync.MutexWrap(ds.NewMapDatastore()))
|
|
|
|
|
|
|
|
fromKeyPrivate, fromKeyPublic := testGenerateKeyPair(t)
|
2020-10-08 01:09:33 +00:00
|
|
|
ch := tutils2.NewIDAddr(t, 100)
|
|
|
|
from := tutils2.NewSECP256K1Addr(t, string(fromKeyPublic))
|
|
|
|
to := tutils2.NewSECP256K1Addr(t, "secpTo")
|
|
|
|
fromAcct := tutils2.NewActorAddr(t, "fromAct")
|
|
|
|
toAcct := tutils2.NewActorAddr(t, "toAct")
|
2020-09-14 09:49:58 +00:00
|
|
|
|
|
|
|
mock := newMockManagerAPI()
|
|
|
|
defer mock.close()
|
|
|
|
|
|
|
|
// Add the from signing key to the wallet
|
2020-09-15 18:06:33 +00:00
|
|
|
mock.setAccountAddress(fromAcct, from)
|
|
|
|
mock.setAccountAddress(toAcct, to)
|
2020-09-14 09:49:58 +00:00
|
|
|
mock.addSigningKey(fromKeyPrivate)
|
|
|
|
|
|
|
|
mgr, err := newManager(store, mock)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Send create message for a channel with value 10
|
|
|
|
createAmt := big.NewInt(10)
|
2022-01-06 15:04:39 +00:00
|
|
|
_, createMsgCid, err := mgr.GetPaych(ctx, from, to, createAmt, onChainReserve)
|
2020-09-14 09:49:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Send create channel response
|
|
|
|
response := testChannelResponse(t, ch)
|
|
|
|
mock.receiveMsgResponse(createMsgCid, response)
|
|
|
|
|
|
|
|
// Create an actor in state for the channel with the initial channel balance
|
|
|
|
act := &types.Actor{
|
2020-10-08 01:09:33 +00:00
|
|
|
Code: builtin2.AccountActorCodeID,
|
2020-09-14 09:49:58 +00:00
|
|
|
Head: cid.Cid{},
|
|
|
|
Nonce: 0,
|
|
|
|
Balance: createAmt,
|
|
|
|
}
|
2020-09-15 18:06:33 +00:00
|
|
|
mock.setPaychState(ch, act, paychmock.NewMockPayChState(fromAcct, toAcct, abi.ChainEpoch(0), make(map[uint64]paych.LaneState)))
|
2020-09-14 09:49:58 +00:00
|
|
|
|
|
|
|
// Wait for create response to be processed by manager
|
|
|
|
_, err = mgr.GetPaychWaitReady(ctx, createMsgCid)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Create a voucher with a value equal to the channel balance
|
2022-04-20 21:34:28 +00:00
|
|
|
voucher := paychtypes.SignedVoucher{Amount: createAmt, Lane: 1}
|
2020-09-14 09:49:58 +00:00
|
|
|
res, err := mgr.CreateVoucher(ctx, ch, voucher)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res.Voucher)
|
|
|
|
|
|
|
|
// Create a voucher in a different lane with an amount that exceeds the
|
|
|
|
// channel balance
|
|
|
|
excessAmt := types.NewInt(5)
|
2022-04-20 21:34:28 +00:00
|
|
|
voucher = paychtypes.SignedVoucher{Amount: excessAmt, Lane: 2}
|
2020-09-14 09:49:58 +00:00
|
|
|
res, err = mgr.CreateVoucher(ctx, ch, voucher)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, res.Voucher)
|
|
|
|
require.Equal(t, res.Shortfall, excessAmt)
|
|
|
|
|
|
|
|
// Add funds so as to cover the voucher shortfall
|
2022-01-06 15:04:39 +00:00
|
|
|
_, addFundsMsgCid, err := mgr.GetPaych(ctx, from, to, excessAmt, onChainReserve)
|
2020-09-14 09:49:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Trigger add funds confirmation
|
|
|
|
mock.receiveMsgResponse(addFundsMsgCid, types.MessageReceipt{ExitCode: 0})
|
|
|
|
|
|
|
|
// Update actor test case balance to reflect added funds
|
|
|
|
act.Balance = types.BigAdd(createAmt, excessAmt)
|
|
|
|
|
|
|
|
// Wait for add funds confirmation to be processed by manager
|
|
|
|
_, err = mgr.GetPaychWaitReady(ctx, addFundsMsgCid)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Adding same voucher that previously exceeded channel balance
|
|
|
|
// should succeed now that the channel balance has been increased
|
|
|
|
res, err = mgr.CreateVoucher(ctx, ch, voucher)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, res.Voucher)
|
|
|
|
}
|