lotus/cli/wallet_test.go

336 lines
9.5 KiB
Go
Raw Normal View History

feat: Add additional test annotations (#8272) * Annotate api,proxy_util,blockstore_badger, policy tests * Annotate splitstore: bsbadger / markset * Annotate splitstore feature * Annotate union/timed blockstore tests * Annotate openrpc, diff_adt tests * Annotate error,drand,events tests * Annotate predicates_test * Fix annotations * Annotate tscache, gen tests * Annotate fundmanager test * Annotate repub and selection tests * Annotate statetree_test * Annotate forks_test * Annotate searchwait_test.go * Fix duplicated @@ symbols * Annotate chain stmgr/store tests * Annotate more (types) tests * More tests annotated * Annotate conformance chaos actor tests * Annotate more integration tests * Annotate journal system tests * Annotate more tests. * Annotate gas,head buffer behaviors * Fix markset annotations * doc: test annotations for the markets dagstore wrapper * Annotate miner_api test in dagstore * Annotate more test files * Remove bad annotations from fsrepo * Annotate wdpost system * Remove bad annotations * Renamce "conformance" to "chaos_actor" tests * doc: stm annotations for blockheader & election proof tests * Annotate remaining "A" tests * annotate: stm for error_test * memrepo_test.go * Annotate "b" file tests * message_test.go * doc: stm annotate for fsrepo_test * Annotate "c" file tests * Annotate "D" test files * message_test.go * doc: stm annotate for chain, node/config & client * docs: stm annotate node_test * Annotate u,v,wl tests * doc: stm annotations for various test files * Annotate "T" test files * doc: stm annotate for proxy_util_test & policy_test * doc: stm annotate for various tests * doc: final few stm annotations * Add mempool unit tests * Add two more memPool Add tests * Update submodules * Add check function tests * Add stm annotations, refactor test helper * Annotate api,proxy_util,blockstore_badger, policy tests * Annotate splitstore: bsbadger / markset solving merge conflicts * Annotate splitstore feature * Annotate union/timed blockstore tests * Annotate openrpc, diff_adt tests * Annotate error,drand,events tests * Annotate predicates_test * Fix annotations * Annotate tscache, gen tests * Annotate fundmanager test * Annotate statetree_test * Annotate forks_test * Annotate searchwait_test.go * Fix duplicated @@ symbols * Annotate chain stmgr/store tests * Annotate more (types) tests * More tests annotated * Annotate conformance chaos actor tests * Annotate more integration tests * Annotate journal system tests * Annotate more tests. * Annotate gas,head buffer behaviors solve merge conflict * Fix markset annotations * Annotate miner_api test in dagstore * Annotate more test files * doc: test annotations for the markets dagstore wrapper * Annotate wdpost system * Renamce "conformance" to "chaos_actor" tests * Annotate remaining "A" tests * doc: stm annotations for blockheader & election proof tests * annotate: stm for error_test * Annotate "b" file tests * memrepo_test.go * Annotate "c" file tests * message_test.go * Annotate "D" test files * doc: stm annotate for fsrepo_test * Annotate u,v,wl tests * message_test.go * doc: stm annotate for chain, node/config & client * docs: stm annotate node_test * Annotate "T" test files * doc: stm annotations for various test files * Add mempool unit tests solve merge conflict * doc: stm annotate for proxy_util_test & policy_test * doc: stm annotate for various tests * doc: final few stm annotations * Add two more memPool Add tests * Update submodules * Add check function tests solve conflict * Add stm annotations, refactor test helper solve merge conflict * Change CLI test kinds to "unit" * Fix double merged test * Fix ccupgrade_test merge * Fix lint issues * Add stm annotation to types_Test * Test vectors submodule * Add file annotation to burn_test Co-authored-by: Nikola Divic <divicnikola@gmail.com> Co-authored-by: TheMenko <themenkoprojects@gmail.com>
2022-03-16 17:37:34 +00:00
//stm: #unit
2022-02-11 15:30:28 +00:00
package cli
import (
"context"
2022-02-22 14:35:46 +00:00
"encoding/hex"
"encoding/json"
"fmt"
2022-02-11 15:30:28 +00:00
"testing"
2022-06-14 15:00:51 +00:00
"github.com/golang/mock/gomock"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/assert"
2022-02-11 15:30:28 +00:00
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
2022-06-14 15:00:51 +00:00
2022-02-11 15:30:28 +00:00
"github.com/filecoin-project/lotus/api"
apitypes "github.com/filecoin-project/lotus/api/types"
types "github.com/filecoin-project/lotus/chain/types"
)
func TestWalletNew(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletNew))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
keyType := types.KeyType("secp256k1")
2022-02-22 14:35:46 +00:00
address, err := address.NewFromString("t0123")
assert.NoError(t, err)
2022-02-11 15:30:28 +00:00
mockApi.EXPECT().WalletNew(ctx, keyType).Return(address, nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_NEW_001
2022-02-22 14:35:46 +00:00
err = app.Run([]string{"wallet", "new"})
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), address.String())
2022-02-11 15:30:28 +00:00
}
func TestWalletList(t *testing.T) {
addr, err := address.NewIDAddress(1234)
addresses := []address.Address{addr}
assert.NoError(t, err)
cid := cid.Cid{}
key := types.NewTipSetKey(cid)
actor := types.Actor{
Code: cid,
Head: cid,
Nonce: 0,
Balance: big.NewInt(100),
}
t.Run("wallet-list-addr-only", func(t *testing.T) {
2022-03-01 01:21:22 +00:00
app, mockApi, buf, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletList))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gomock.InOrder(
mockApi.EXPECT().WalletList(ctx).Return(addresses, nil),
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(addr, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_LIST_001
2022-03-01 01:21:22 +00:00
err := app.Run([]string{"wallet", "list", "--addr-only"})
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-03-01 01:21:22 +00:00
assert.Contains(t, buf.String(), addr.String())
2022-02-11 15:30:28 +00:00
})
t.Run("wallet-list-id", func(t *testing.T) {
app, mockApi, _, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletList))
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
gomock.InOrder(
mockApi.EXPECT().WalletList(ctx).Return(addresses, nil),
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(addr, nil),
mockApi.EXPECT().StateGetActor(ctx, addr, key).Return(&actor, nil),
mockApi.EXPECT().StateLookupID(ctx, addr, key).Return(addr, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_LIST_002
2022-02-11 15:30:28 +00:00
err := app.Run([]string{"wallet", "list", "--id"})
assert.NoError(t, err)
})
t.Run("wallet-list-market", func(t *testing.T) {
app, mockApi, _, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletList))
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
balance := api.MarketBalance{
Escrow: big.NewInt(1234),
Locked: big.NewInt(123),
}
gomock.InOrder(
mockApi.EXPECT().WalletList(ctx).Return(addresses, nil),
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(addr, nil),
mockApi.EXPECT().StateGetActor(ctx, addr, key).Return(&actor, nil),
mockApi.EXPECT().StateMarketBalance(ctx, addr, key).Return(balance, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_LIST_003
2022-02-11 15:30:28 +00:00
err := app.Run([]string{"wallet", "list", "--market"})
assert.NoError(t, err)
})
}
func TestWalletBalance(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletBalance))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
balance := big.NewInt(1234)
mockApi.EXPECT().WalletBalance(ctx, addr).Return(balance, nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_BALANCE_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "balance", "f01234"})
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), balance.String())
2022-02-11 15:30:28 +00:00
}
func TestWalletGetDefault(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletGetDefault))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2022-02-22 14:35:46 +00:00
addr, err := address.NewFromString("t0123")
assert.NoError(t, err)
2022-02-11 15:30:28 +00:00
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(addr, nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_GET_DEFAULT_001
2022-02-22 14:35:46 +00:00
err = app.Run([]string{"wallet", "default"})
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), addr.String())
2022-02-11 15:30:28 +00:00
}
func TestWalletSetDefault(t *testing.T) {
app, mockApi, _, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletSetDefault))
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
mockApi.EXPECT().WalletSetDefault(ctx, addr).Return(nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_SET_DEFAULT_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "set-default", "f01234"})
assert.NoError(t, err)
}
func TestWalletExport(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletExport))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
keyInfo := types.KeyInfo{
Type: types.KTSecp256k1,
PrivateKey: []byte("0x000000000000000000001"),
}
2022-02-11 15:30:28 +00:00
mockApi.EXPECT().WalletExport(ctx, addr).Return(&keyInfo, nil)
2022-02-22 14:35:46 +00:00
ki, err := json.Marshal(keyInfo)
assert.NoError(t, err)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_EXPORT_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "export", "f01234"})
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), hex.EncodeToString(ki))
2022-02-11 15:30:28 +00:00
}
func TestWalletSign(t *testing.T) {
2022-03-01 01:34:52 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletSign))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2022-02-22 14:35:46 +00:00
addr, err := address.NewFromString("f01234")
assert.NoError(t, err)
msg, err := hex.DecodeString("01")
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-03-01 01:34:52 +00:00
signature := crypto.Signature{
Type: crypto.SigTypeSecp256k1,
Data: []byte{0x01},
}
2022-02-11 15:30:28 +00:00
mockApi.EXPECT().WalletSign(ctx, addr, msg).Return(&signature, nil)
2022-03-01 01:34:52 +00:00
sigBytes := append([]byte{byte(signature.Type)}, signature.Data...)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_SIGN_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "sign", "f01234", "01"})
assert.NoError(t, err)
2022-03-01 01:34:52 +00:00
assert.Contains(t, buffer.String(), hex.EncodeToString(sigBytes))
2022-02-11 15:30:28 +00:00
}
func TestWalletVerify(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletVerify))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
msg := []byte{1}
signature := crypto.Signature{
Type: crypto.SigTypeSecp256k1,
Data: []byte{},
}
mockApi.EXPECT().WalletVerify(ctx, addr, msg, &signature).Return(true, nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_VERIFY_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "verify", "f01234", "01", "01"})
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), "valid")
2022-02-11 15:30:28 +00:00
}
func TestWalletDelete(t *testing.T) {
app, mockApi, _, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletDelete))
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
mockApi.EXPECT().WalletDelete(ctx, addr).Return(nil)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_DELETE_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "delete", "f01234"})
assert.NoError(t, err)
}
func TestWalletMarketWithdraw(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletMarket))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr, err := address.NewIDAddress(1234)
assert.NoError(t, err)
balance := api.MarketBalance{
Escrow: big.NewInt(100),
Locked: big.NewInt(10),
}
2022-02-22 14:35:46 +00:00
h, err := hex.DecodeString("12209cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47")
assert.NoError(t, err)
cid := cid.NewCidV0(multihash.Multihash(h))
2022-02-11 15:30:28 +00:00
msgLookup := api.MsgLookup{}
var networkVers apitypes.NetworkVersion
gomock.InOrder(
mockApi.EXPECT().StateMarketBalance(ctx, addr, types.TipSetKey{}).Return(balance, nil),
// mock reserve to 10
mockApi.EXPECT().MarketGetReserved(ctx, addr).Return(big.NewInt(10), nil),
// available should be 80.. escrow - locked - reserve
mockApi.EXPECT().MarketWithdraw(ctx, addr, addr, big.NewInt(80)).Return(cid, nil),
mockApi.EXPECT().StateWaitMsg(ctx, cid, uint64(5), abi.ChainEpoch(int64(-1)), true).Return(&msgLookup, nil),
mockApi.EXPECT().StateNetworkVersion(ctx, types.TipSetKey{}).Return(networkVers, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_MARKET_WITHDRAW_001
2022-02-11 15:30:28 +00:00
err = app.Run([]string{"wallet", "market", "withdraw", "--wallet", addr.String()})
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), fmt.Sprintf("WithdrawBalance message cid: %s", cid))
2022-02-11 15:30:28 +00:00
}
func TestWalletMarketAdd(t *testing.T) {
2022-02-22 14:35:46 +00:00
app, mockApi, buffer, done := NewMockAppWithFullAPI(t, WithCategory("wallet", walletMarket))
2022-02-11 15:30:28 +00:00
defer done()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
toAddr := address.Address{}
defaultAddr := address.Address{}
2022-02-22 14:35:46 +00:00
h, err := hex.DecodeString("12209cbc07c3f991725836a3aa2a581ca2029198aa420b9d99bc0e131d9f3e2cbe47")
assert.NoError(t, err)
cid := cid.NewCidV0(multihash.Multihash(h))
2022-02-11 15:30:28 +00:00
gomock.InOrder(
mockApi.EXPECT().WalletDefaultAddress(ctx).Return(defaultAddr, nil),
mockApi.EXPECT().MarketAddBalance(ctx, defaultAddr, toAddr, big.NewInt(80)).Return(cid, nil),
)
2022-02-22 15:00:01 +00:00
//stm: @CLI_WALLET_MARKET_ADD_001
2022-02-22 14:35:46 +00:00
err = app.Run([]string{"wallet", "market", "add", "0.000000000000000080", "--address", toAddr.String()})
2022-02-11 15:30:28 +00:00
assert.NoError(t, err)
2022-02-22 14:35:46 +00:00
assert.Contains(t, buffer.String(), fmt.Sprintf("AddBalance message cid: %s", cid))
2022-02-11 15:30:28 +00:00
}