github.com/turingchain2020/turingchain@v1.1.21/wallet/wallet_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package wallet
     6  
     7  import (
     8  	"encoding/hex"
     9  	"errors"
    10  	"fmt"
    11  	"os"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/turingchain2020/turingchain/common"
    16  	"github.com/turingchain2020/turingchain/common/address"
    17  	"github.com/turingchain2020/turingchain/common/crypto"
    18  	"github.com/turingchain2020/turingchain/queue"
    19  	"github.com/turingchain2020/turingchain/store"
    20  	_ "github.com/turingchain2020/turingchain/system"
    21  	"github.com/turingchain2020/turingchain/types"
    22  	"github.com/turingchain2020/turingchain/util"
    23  	"github.com/turingchain2020/turingchain/wallet/bipwallet"
    24  	wcom "github.com/turingchain2020/turingchain/wallet/common"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func init() {
    30  	queue.DisableLog()
    31  	SetLogLevel("err")
    32  }
    33  
    34  func initEnv() (*Wallet, queue.Module, queue.Queue, string) {
    35  	cfg := types.NewTuringchainConfig(types.ReadFile("../cmd/turingchain/turingchain.test.toml"))
    36  	var q = queue.New("channel")
    37  	q.SetConfig(cfg)
    38  	wallet := New(cfg)
    39  	wallet.SetQueueClient(q.Client())
    40  	store := store.New(cfg)
    41  	store.SetQueueClient(q.Client())
    42  
    43  	return wallet, store, q, cfg.GetModuleConfig().Wallet.DbPath
    44  }
    45  
    46  var (
    47  	Statehash      []byte
    48  	CutHeight      int64
    49  	FromAddr       string
    50  	ToAddr1        string
    51  	ToAddr2        string
    52  	AddrPrivKey    string
    53  	addr           string
    54  	priv           crypto.PrivKey
    55  	AllAccountlist *types.WalletAccounts
    56  )
    57  
    58  func blockchainModProc(q queue.Queue) {
    59  	//store
    60  	go func() {
    61  		client := q.Client()
    62  		client.Sub("blockchain")
    63  		for msg := range client.Recv() {
    64  			walletlog.Error("blockchain", "msg.Ty", msg.Ty, "name", types.GetEventName(int(msg.Ty)))
    65  			if msg.Ty == types.EventGetLastHeader {
    66  				header := &types.Header{StateHash: Statehash}
    67  				msg.Reply(client.NewMessage("account", types.EventHeader, header))
    68  			} else if msg.Ty == types.EventGetTransactionByAddr {
    69  				addr := (msg.Data).(*types.ReqAddr)
    70  
    71  				var replyTxInfos types.ReplyTxInfos
    72  				total := 10
    73  				replyTxInfos.TxInfos = make([]*types.ReplyTxInfo, total)
    74  				for index := 0; index < total; index++ {
    75  					var replyTxInfo types.ReplyTxInfo
    76  					hashstr := fmt.Sprintf("hash:%s:%d", addr.Addr, index)
    77  					replyTxInfo.Hash = []byte(hashstr)
    78  					replyTxInfo.Height = CutHeight + 1
    79  					replyTxInfo.Index = int64(index)
    80  					replyTxInfos.TxInfos[index] = &replyTxInfo
    81  					CutHeight++
    82  				}
    83  				msg.Reply(client.NewMessage("rpc", types.EventReplyTxInfo, &replyTxInfos))
    84  
    85  			} else if msg.Ty == types.EventGetTransactionByHash {
    86  				txhashs := (msg.Data).(*types.ReqHashes)
    87  
    88  				var txDetails types.TransactionDetails
    89  				txDetails.Txs = make([]*types.TransactionDetail, len(txhashs.Hashes))
    90  				for index, txhash := range txhashs.Hashes {
    91  					var txDetail types.TransactionDetail
    92  					txDetail.Index = int64(index)
    93  					txDetail.Receipt = &types.ReceiptData{Ty: 2, Logs: nil}
    94  					txDetail.Tx = &types.Transaction{Execer: []byte("coins"), Payload: txhash, To: "14ZTV2wHG3uPHnA5cBJmNxAxxvbzS7Z5mE"}
    95  					txDetails.Txs[index] = &txDetail
    96  				}
    97  				msg.Reply(client.NewMessage("rpc", types.EventTransactionDetails, &txDetails))
    98  			} else if msg.Ty == types.EventGetBlockHeight {
    99  				msg.Reply(client.NewMessage("", types.EventReplyBlockHeight, &types.ReplyBlockHeight{Height: 1}))
   100  			} else if msg.Ty == types.EventIsSync {
   101  				msg.Reply(client.NewMessage("", types.EventReplyIsSync, &types.IsCaughtUp{Iscaughtup: true}))
   102  			} else if msg.Ty == types.EventQueryTx {
   103  				msg.Reply(client.NewMessage("", types.EventTransactionDetail, &types.TransactionDetail{Receipt: &types.ReceiptData{Ty: types.ExecOk}}))
   104  			}
   105  		}
   106  	}()
   107  	go func() {
   108  		client := q.Client()
   109  		client.Sub("exec")
   110  		for msg := range client.Recv() {
   111  			walletlog.Error("execs", "msg.Ty", msg.Ty)
   112  			if msg.Ty == types.EventBlockChainQuery {
   113  				msg.Reply(client.NewMessage("", types.EventReplyQuery, types.ErrActionNotSupport))
   114  			}
   115  		}
   116  	}()
   117  	go func() {
   118  		client := q.Client()
   119  		client.Sub("consensus")
   120  		for msg := range client.Recv() {
   121  			walletlog.Error("execs", "msg.Ty", msg.Ty)
   122  			if msg.Ty == types.EventConsensusQuery {
   123  				msg.Reply(client.NewMessage("", types.EventReplyQuery, types.ErrActionNotSupport))
   124  			}
   125  		}
   126  	}()
   127  }
   128  
   129  func mempoolModProc(q queue.Queue) {
   130  	//store
   131  	go func() {
   132  		client := q.Client()
   133  		client.Sub("mempool")
   134  		for msg := range client.Recv() {
   135  			//walletlog.Info("mempool", "msg.Ty", msg.Ty)
   136  			if msg.Ty == types.EventTx {
   137  				msg.Reply(client.NewMessage("wallet", types.EventReply, &types.Reply{IsOk: true}))
   138  			} else if msg.Ty == types.EventGetProperFee {
   139  				msg.Reply(client.NewMessage("wallet", types.EventReply, &types.ReplyProperFee{ProperFee: 1000000}))
   140  			}
   141  		}
   142  	}()
   143  }
   144  
   145  func SaveAccountTomavl(wallet *Wallet, client queue.Client, prevStateRoot []byte, accs []*types.Account) []byte {
   146  	var kvset []*types.KeyValue
   147  
   148  	for _, acc := range accs {
   149  		kvs := wallet.accountdb.GetKVSet(acc)
   150  		kvset = append(kvset, kvs...)
   151  	}
   152  	hash, err := util.ExecKVMemSet(client, prevStateRoot, 0, kvset, true, false)
   153  	if err != nil {
   154  		panic(err)
   155  	}
   156  	Statehash = hash
   157  	util.ExecKVSetCommit(client, Statehash, false)
   158  	return hash
   159  }
   160  
   161  func TestAll(t *testing.T) {
   162  	testAllWallet(t)
   163  	testWalletImportPrivkeysFile(t)
   164  	testWalletImportPrivkeysFile2(t)
   165  }
   166  
   167  func testAllWallet(t *testing.T) {
   168  	wallet, store, q, datapath := initEnv()
   169  	defer os.RemoveAll("datadir") // clean up
   170  	defer wallet.Close()
   171  	defer store.Close()
   172  
   173  	//启动blockchain模块
   174  	blockchainModProc(q)
   175  	mempoolModProc(q)
   176  
   177  	testSeed(t, wallet)
   178  
   179  	testProcCreateNewAccount(t, wallet)
   180  
   181  	testProcImportPrivKey(t, wallet)
   182  
   183  	testProcDumpPrivkeysFile(t, wallet)
   184  
   185  	//wait data sync
   186  	testProcWalletTxList(t, wallet)
   187  
   188  	testProcSendToAddress(t, wallet)
   189  
   190  	testProcWalletSetFee(t, wallet)
   191  
   192  	testProcWalletSetLabel(t, wallet)
   193  
   194  	testProcMergeBalance(t, wallet)
   195  
   196  	testProcWalletSetPasswd(t, wallet)
   197  
   198  	testProcWalletLock(t, wallet)
   199  
   200  	testProcWalletAddBlock(t, wallet)
   201  	testSignRawTx(t, wallet)
   202  	testsetFatalFailure(t, wallet)
   203  	testgetFatalFailure(t, wallet)
   204  
   205  	testWallet(t, wallet)
   206  	testSendTx(t, wallet)
   207  	testCreateNewAccountByIndex(t, wallet)
   208  
   209  	t.Log(datapath)
   210  }
   211  
   212  func testWalletImportPrivkeysFile(t *testing.T) {
   213  	wallet, store, q, _ := initEnv()
   214  	defer os.RemoveAll("datadir") // clean up
   215  	defer wallet.Close()
   216  	defer store.Close()
   217  
   218  	//启动blockchain模块
   219  	blockchainModProc(q)
   220  	mempoolModProc(q)
   221  
   222  	testSeed(t, wallet)
   223  	testProcImportPrivkeysFile(t, wallet)
   224  }
   225  
   226  func testWalletImportPrivkeysFile2(t *testing.T) {
   227  	wallet, store, q, _ := initEnv()
   228  	defer os.RemoveAll("datadir") // clean up
   229  	defer wallet.Close()
   230  	defer store.Close()
   231  
   232  	//启动blockchain模块
   233  	blockchainModProc(q)
   234  	mempoolModProc(q)
   235  
   236  	testSeed(t, wallet)
   237  	testProcImportPrivkeysFile2(t, wallet)
   238  }
   239  
   240  //ProcWalletLock
   241  func testSeed(t *testing.T, wallet *Wallet) {
   242  	println("TestSeed begin")
   243  
   244  	seedRes, _ := wallet.GetAPI().ExecWalletFunc("wallet", "GenSeed", &types.GenSeedLang{Lang: 1})
   245  	seed := seedRes.(*types.ReplySeed).Seed
   246  	println("seed: ", seed)
   247  
   248  	password := "password123"
   249  	saveSeedByPw := &types.SaveSeedByPw{Seed: "", Passwd: ""}
   250  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "SaveSeed", saveSeedByPw)
   251  	assert.Nil(t, err)
   252  	assert.Equal(t, string(resp.(*types.Reply).GetMsg()), types.ErrInvalidParam.Error())
   253  
   254  	saveSeedByPw.Seed = "a b c d"
   255  	saveSeedByPw.Passwd = password
   256  	resp, _ = wallet.GetAPI().ExecWalletFunc("wallet", "SaveSeed", saveSeedByPw)
   257  	if string(resp.(*types.Reply).GetMsg()) != types.ErrSeedWordNum.Error() {
   258  		t.Error("test input not enough seed failed")
   259  	}
   260  
   261  	saveSeedByPw.Seed = "a b c d e f g h i j k l m n"
   262  	resp, _ = wallet.GetAPI().ExecWalletFunc("wallet", "SaveSeed", saveSeedByPw)
   263  	if string(resp.(*types.Reply).GetMsg()) != types.ErrSeedWord.Error() {
   264  		t.Error("test input invalid seed failed")
   265  	}
   266  
   267  	saveSeedByPw.Seed = seed
   268  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "SaveSeed", saveSeedByPw)
   269  	assert.Nil(t, err)
   270  
   271  	seedstr, err := GetSeed(wallet.walletStore.GetDB(), password)
   272  	require.NoError(t, err)
   273  	if seed != seedstr {
   274  		t.Error("testSaveSeed failed")
   275  	}
   276  
   277  	resp, _ = wallet.GetAPI().ExecWalletFunc("wallet", "SaveSeed", saveSeedByPw)
   278  	if string(resp.(*types.Reply).GetMsg()) != types.ErrSeedExist.Error() {
   279  		t.Error("test seedExists failed")
   280  	}
   281  
   282  	walletUnLock := &types.WalletUnLock{
   283  		Passwd:         password,
   284  		Timeout:        0,
   285  		WalletOrTicket: false,
   286  	}
   287  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletUnLock", walletUnLock)
   288  	require.NoError(t, err)
   289  
   290  	resp, err = wallet.GetAPI().ExecWalletFunc("wallet", "GetSeed", &types.GetSeedByPw{Passwd: password})
   291  	require.NoError(t, err)
   292  
   293  	reply := resp.(*types.ReplySeed)
   294  	if reply.GetSeed() != seed {
   295  		t.Error("testGetSeed failed")
   296  	}
   297  
   298  	println("TestSeed end")
   299  	println("--------------------------")
   300  }
   301  
   302  func testProcCreateNewAccount(t *testing.T, wallet *Wallet) {
   303  	println("TestProcCreateNewAccount begin")
   304  	total := 10
   305  	addrlist := make([]string, total)
   306  	accs := make([]*types.Account, total+1)
   307  	for i := 0; i < total; i++ {
   308  		reqNewAccount := &types.ReqNewAccount{Label: fmt.Sprintf("account:%d", i)}
   309  		resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "NewAccount", reqNewAccount)
   310  		require.NoError(t, err)
   311  		time.Sleep(time.Microsecond * 100)
   312  		walletAcc := resp.(*types.WalletAccount)
   313  		addrlist[i] = walletAcc.Acc.Addr
   314  		walletAcc.Acc.Balance = int64(i)
   315  		walletAcc.Acc.Currency = int32(i)
   316  		walletAcc.Acc.Frozen = int64(i)
   317  		accs[i] = walletAcc.Acc
   318  		//FromAddr = Walletacc.Acc.Addr
   319  		if i == 0 {
   320  			ToAddr1 = walletAcc.Acc.Addr
   321  		}
   322  		if i == 1 {
   323  			ToAddr2 = walletAcc.Acc.Addr
   324  		}
   325  	}
   326  
   327  	//通过privkey生成一个pubkey然后换算成对应的addr
   328  	cr, err := crypto.New(types.GetSignName("", wallet.SignType))
   329  	require.NoError(t, err)
   330  
   331  	Privkey := "0xb94ae286a508e4bb3fbbcb61997822fea6f0a534510597ef8eb60a19d6b219a0"
   332  	privkeybyte, _ := common.FromHex(Privkey)
   333  	priv, err := cr.PrivKeyFromBytes(privkeybyte)
   334  	require.NoError(t, err)
   335  
   336  	addr := address.PubKeyToAddress(priv.PubKey().Bytes())
   337  	FromAddr = addr.String()
   338  	var acc types.Account
   339  	acc.Addr = addr.String()
   340  	acc.Balance = int64(1e16)
   341  	acc.Currency = int32(10)
   342  	acc.Frozen = int64(10)
   343  	accs[total] = &acc
   344  
   345  	//存入账户信息到mavl树中
   346  	SaveAccountTomavl(wallet, wallet.client, nil, accs)
   347  
   348  	//测试ProcGetAccountList函数
   349  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletGetAccountList", &types.ReqAccountList{})
   350  	assert.Nil(t, err)
   351  	accountlist := resp.(*types.WalletAccounts)
   352  	for _, acc1 := range accountlist.Wallets {
   353  		exist := false
   354  		for _, acc2 := range accs[:10] {
   355  			if equal(*acc1.Acc, *acc2) {
   356  				exist = true
   357  				break
   358  			}
   359  		}
   360  		if !exist {
   361  			t.Error("account list not match")
   362  			return
   363  		}
   364  	}
   365  	println("TestProcCreateNewAccount end")
   366  	println("--------------------------")
   367  }
   368  
   369  func equal(acc1 types.Account, acc2 types.Account) bool {
   370  	if acc1.Currency != acc2.Currency {
   371  		return false
   372  	}
   373  	if acc1.Balance != acc2.Balance {
   374  		return false
   375  	}
   376  	if acc1.Frozen != acc2.Frozen {
   377  		return false
   378  	}
   379  	if acc1.Addr != acc2.Addr {
   380  		return false
   381  	}
   382  	return true
   383  }
   384  
   385  func testProcImportPrivKey(t *testing.T, wallet *Wallet) {
   386  	println("TestProcImportPrivKey begin")
   387  
   388  	//生成一个pubkey然后换算成对应的addr
   389  	cr, err := crypto.New(types.GetSignName("", wallet.SignType))
   390  	require.NoError(t, err)
   391  
   392  	priv, err := cr.GenKey()
   393  	require.NoError(t, err)
   394  
   395  	AddrPrivKey = common.ToHex(priv.Bytes())
   396  
   397  	privKey := &types.ReqWalletImportPrivkey{Privkey: AddrPrivKey}
   398  	privKey.Label = "account:0"
   399  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletImportPrivkey", privKey)
   400  	assert.Equal(t, err.Error(), types.ErrLabelHasUsed.Error())
   401  
   402  	privKey.Label = "ImportPrivKey-Label"
   403  	resp, _ := wallet.GetAPI().ExecWalletFunc("wallet", "WalletImportPrivkey", privKey)
   404  	importedAcc := resp.(*types.WalletAccount)
   405  	if importedAcc.Label != "ImportPrivKey-Label" || importedAcc.Acc.Addr != address.PubKeyToAddress(priv.PubKey().Bytes()).String() {
   406  		t.Error("testProcImportPrivKey failed")
   407  		return
   408  	}
   409  
   410  	//import privkey="0xb94ae286a508e4bb3fbbcb61997822fea6f0a534510597ef8eb60a19d6b219a0"
   411  	privKey.Privkey = "0xb94ae286a508e4bb3fbbcb61997822fea6f0a534510597ef8eb60a19d6b219a0"
   412  	privKey.Label = "ImportPrivKey-Label-hyb"
   413  	walletlog.Info("TestProcImportPrivKey", "Privkey", privKey.Privkey, "Label", privKey.Label)
   414  
   415  	wallet.GetAPI().ExecWalletFunc("wallet", "WalletImportPrivkey", privKey)
   416  
   417  	addr := &types.ReqString{Data: address.PubKeyToAddress(priv.PubKey().Bytes()).String()}
   418  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "DumpPrivkey", &types.ReqString{Data: "wrongaddr"})
   419  	assert.Equal(t, err.Error(), types.ErrAddrNotExist.Error())
   420  
   421  	resp, _ = wallet.GetAPI().ExecWalletFunc("wallet", "DumpPrivkey", addr)
   422  	if resp.(*types.ReplyString).Data != common.ToHex(priv.Bytes()) {
   423  		t.Error("testDumpPrivKey failed")
   424  	}
   425  
   426  	//入参测试
   427  	_, err = wallet.ProcImportPrivKey(nil)
   428  	assert.Equal(t, err, types.ErrInvalidParam)
   429  	privKey.Label = "hyb123"
   430  	privKey.Privkey = "0xb694ae286a508e4bb3fbbcb61997822fea6f0a534510597ef8eb60a19d6b219a0"
   431  
   432  	_, err = wallet.ProcImportPrivKey(privKey)
   433  	assert.Equal(t, err, types.ErrPrivkeyToPub)
   434  
   435  	_, err = wallet.ProcImportPrivKey(nil)
   436  	assert.Equal(t, err, types.ErrInvalidParam)
   437  
   438  	privKey.Label = "hyb1234"
   439  	privKey.Privkey = "lfllfllflflfllf"
   440  	_, err = wallet.ProcImportPrivKey(privKey)
   441  	assert.Equal(t, err, types.ErrFromHex)
   442  	println("TestProcImportPrivKey end")
   443  	println("--------------------------")
   444  }
   445  
   446  func testProcWalletTxList(t *testing.T, wallet *Wallet) {
   447  	println("TestProcWalletTxList begin")
   448  
   449  	//倒序获取最新的三笔交易
   450  	txList := &types.ReqWalletTransactionList{
   451  		Count:     3,
   452  		Direction: 0,
   453  		FromTx:    []byte(""),
   454  	}
   455  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletTransactionList", txList)
   456  	require.NoError(t, err)
   457  	walletTxDetails := resp.(*types.WalletTxDetails)
   458  
   459  	var FromTxstr string
   460  	index := make([]int64, 3)
   461  
   462  	if len(walletTxDetails.TxDetails) != 3 {
   463  		t.Error("testProcWalletTxList failed")
   464  	}
   465  	println("TestProcWalletTxList dir last-------")
   466  	for i, walletTxDetail := range walletTxDetails.TxDetails {
   467  		println("TestProcWalletTxList", "Direction", txList.Direction, "WalletTxDetail", walletTxDetail.String())
   468  		index[i] = walletTxDetail.GetHeight()*100000 + walletTxDetail.GetIndex()
   469  		FromTxstr = fmt.Sprintf("%018d", walletTxDetail.GetHeight()*100000+walletTxDetail.GetIndex())
   470  	}
   471  	//倒序index值的判断,index[0]>index[1]>index[2]
   472  	if index[0] <= index[1] {
   473  		println("TestProcWalletTxList", "index[0]", index[0], "index[1]", index[1])
   474  		t.Error("testProcWalletTxList:Reverse check fail!")
   475  	}
   476  	if index[1] <= index[2] {
   477  		println("TestProcWalletTxList", "index[1]", index[1], "index[2]", index[2])
   478  		t.Error("testProcWalletTxList:Reverse check fail!")
   479  	}
   480  
   481  	txList.Direction = 1
   482  	txList.Count = 2
   483  	txList.FromTx = []byte(FromTxstr)
   484  
   485  	println("TestProcWalletTxList dir next-------")
   486  	resp, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletTransactionList", txList)
   487  	require.NoError(t, err)
   488  	walletTxDetails = resp.(*types.WalletTxDetails)
   489  	if len(walletTxDetails.TxDetails) != 2 {
   490  		t.Error("testProcWalletTxList failed")
   491  	}
   492  	for _, walletTxDetail := range walletTxDetails.TxDetails {
   493  		println("TestProcWalletTxList", "Direction", txList.Direction, "WalletTxDetail", walletTxDetail.String())
   494  	}
   495  
   496  	println("TestProcWalletTxList dir prv------")
   497  	txList.Direction = 0
   498  	resp, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletTransactionList", txList)
   499  	require.NoError(t, err)
   500  	walletTxDetails = resp.(*types.WalletTxDetails)
   501  	if len(walletTxDetails.TxDetails) != 2 {
   502  		t.Error("testProcWalletTxList failed")
   503  	}
   504  	for _, walletTxDetail := range walletTxDetails.TxDetails {
   505  		println("TestProcWalletTxList", "Direction", txList.Direction, "WalletTxDetail", walletTxDetail.String())
   506  	}
   507  
   508  	//正序获取最早的三笔交易
   509  	txList = &types.ReqWalletTransactionList{
   510  		Count:     3,
   511  		Direction: 1,
   512  		FromTx:    []byte(""),
   513  	}
   514  	resp, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletTransactionList", txList)
   515  	require.NoError(t, err)
   516  	walletTxDetails = resp.(*types.WalletTxDetails)
   517  
   518  	if len(walletTxDetails.TxDetails) != 3 {
   519  		t.Error("testProcWalletTxList failed")
   520  	}
   521  	for i, walletTxDetail := range walletTxDetails.TxDetails {
   522  		index[i] = walletTxDetail.GetHeight()*100000 + walletTxDetail.GetIndex()
   523  	}
   524  	//正序index值的判断,index[0]<index[1]<index[2]
   525  	if index[0] >= index[1] {
   526  		println("TestProcWalletTxList", "index[0]", index[0], "index[1]", index[1])
   527  		t.Error("testProcWalletTxList:positive check fail!")
   528  	}
   529  	if index[1] >= index[2] {
   530  		println("TestProcWalletTxList", "index[1]", index[1], "index[2]", index[2])
   531  		t.Error("testProcWalletTxList:positive check fail!")
   532  	}
   533  
   534  	//count 大于1000个报错
   535  	txList.Count = 1001
   536  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletTransactionList", txList)
   537  	assert.Equal(t, err, types.ErrMaxCountPerTime)
   538  
   539  	//入参测试
   540  	_, err = wallet.ProcWalletTxList(nil)
   541  	assert.Equal(t, err, types.ErrInvalidParam)
   542  
   543  	txList.Direction = 2
   544  	_, err = wallet.ProcWalletTxList(txList)
   545  	assert.Equal(t, err, types.ErrInvalidParam)
   546  	println("TestProcWalletTxList end")
   547  	println("--------------------------")
   548  }
   549  
   550  //(SendToAddress *types.ReqWalletSendToAddress) (*types.ReplyHash, error) {
   551  func testProcSendToAddress(t *testing.T, wallet *Wallet) {
   552  	println("TestProcSendToAddress begin")
   553  	transfer := &types.ReqWalletSendToAddress{
   554  		Amount: 1000,
   555  		From:   FromAddr,
   556  		Note:   "test",
   557  		To:     "1L1zEgVcjqdM2KkQixENd7SZTaudKkcyDu",
   558  	}
   559  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletSendToAddress", transfer)
   560  	require.NoError(t, err)
   561  	replyHash := resp.(*types.ReplyHash)
   562  	println("transfer tx", "ReplyHash", common.ToHex(replyHash.Hash))
   563  	withdraw := &types.ReqWalletSendToAddress{
   564  		Amount: -1000,
   565  		From:   FromAddr,
   566  		Note:   "test",
   567  		To:     "16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp",
   568  	}
   569  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletSendToAddress", withdraw)
   570  	//返回ErrAmount错误
   571  	assert.Equal(t, err.Error(), types.ErrAmount.Error())
   572  	require.Error(t, err)
   573  	println("TestProcSendToAddress end")
   574  	println("--------------------------")
   575  }
   576  
   577  //ProcWalletSetFee(WalletSetFee *types.ReqWalletSetFee) error {
   578  func testProcWalletSetFee(t *testing.T, wallet *Wallet) {
   579  	println("TestProcWalletSetFee begin")
   580  	var fee int64 = 1000000
   581  	walletSetFee := &types.ReqWalletSetFee{Amount: fee}
   582  	wallet.GetAPI().ExecWalletFunc("wallet", "WalletSetFee", walletSetFee)
   583  	if wallet.FeeAmount != fee {
   584  		t.Error("testProcWalletSetFee failed")
   585  	}
   586  	println("TestProcWalletSetFee end")
   587  	println("--------------------------")
   588  }
   589  
   590  //ProcWalletSetLabel(SetLabel *types.ReqWalletSetLabel) (*types.WalletAccount, error)
   591  func testProcWalletSetLabel(t *testing.T, wallet *Wallet) {
   592  	println("TestProcWalletSetLabel begin")
   593  	setLabel := &types.ReqWalletSetLabel{
   594  		Addr:  FromAddr,
   595  		Label: "account:000",
   596  	}
   597  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletSetLabel", setLabel)
   598  	require.NoError(t, err)
   599  	walletAcc := resp.(*types.WalletAccount)
   600  	if walletAcc.Acc.Addr != FromAddr || walletAcc.Label != "account:000" {
   601  		t.Error("testProcWalletSetLabel failed")
   602  	}
   603  
   604  	//再次设置
   605  	setLabel.Label = "account:000"
   606  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletSetLabel", setLabel)
   607  	assert.Equal(t, err.Error(), types.ErrLabelHasUsed.Error())
   608  
   609  	setLabel.Label = "account:001"
   610  	wallet.GetAPI().ExecWalletFunc("wallet", "WalletSetLabel", setLabel)
   611  	setLabel.Label = "account:000"
   612  	resp, _ = wallet.GetAPI().ExecWalletFunc("wallet", "WalletSetLabel", setLabel)
   613  	walletAcc = resp.(*types.WalletAccount)
   614  	if walletAcc.Acc.Addr != FromAddr || walletAcc.Label != "account:000" {
   615  		t.Error("testProcWalletSetLabel failed")
   616  	}
   617  
   618  	println("TestProcWalletSetLabel end")
   619  	println("--------------------------")
   620  }
   621  
   622  //ProcMergeBalance(MergeBalance *types.ReqWalletMergeBalance) (*types.ReplyHashes, error) {
   623  func testProcMergeBalance(t *testing.T, wallet *Wallet) {
   624  	println("TestProcMergeBalance begin")
   625  	mergeBalance := &types.ReqWalletMergeBalance{To: ToAddr2}
   626  	resp, _ := wallet.GetAPI().ExecWalletFunc("wallet", "WalletMergeBalance", mergeBalance)
   627  	replyHashes := resp.(*types.ReplyHashes)
   628  
   629  	for _, hash := range replyHashes.Hashes {
   630  		println("hash:", common.ToHex(hash))
   631  	}
   632  
   633  	println("TestProcMergeBalance end")
   634  	println("--------------------------")
   635  }
   636  
   637  //ProcWalletSetPasswd(Passwd *types.ReqWalletSetPasswd) error {
   638  func testProcWalletSetPasswd(t *testing.T, wallet *Wallet) {
   639  	println("TestProcWalletSetPasswd begin")
   640  	passwd := &types.ReqWalletSetPasswd{
   641  		OldPass: "wrongpassword",
   642  		NewPass: "Newpass123",
   643  	}
   644  	resp, _ := wallet.GetAPI().ExecWalletFunc("wallet", "WalletSetPasswd", passwd)
   645  	if string(resp.(*types.Reply).GetMsg()) != types.ErrVerifyOldpasswdFail.Error() {
   646  		t.Error("testProcWalletSetPasswd failed")
   647  	}
   648  
   649  	passwd.OldPass = "password123"
   650  	_, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletSetPasswd", passwd)
   651  	require.NoError(t, err)
   652  	println("TestProcWalletSetPasswd end")
   653  	println("--------------------------")
   654  }
   655  
   656  //ProcWalletLock
   657  func testProcWalletLock(t *testing.T, wallet *Wallet) {
   658  	println("TestProcWalletLock begin")
   659  	_, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletLock", &types.ReqNil{})
   660  	require.NoError(t, err)
   661  
   662  	transfer := &types.ReqWalletSendToAddress{
   663  		Amount: 1000,
   664  		From:   FromAddr,
   665  		Note:   "test",
   666  		To:     "1L1zEgVcjqdM2KkQixENd7SZTaudKkcyDu",
   667  	}
   668  
   669  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "WalletSendToAddress", transfer)
   670  	if err.Error() != types.ErrWalletIsLocked.Error() {
   671  		t.Error("testProcWalletLock failed")
   672  	}
   673  
   674  	//解锁
   675  	walletUnLock := &types.WalletUnLock{
   676  		Passwd:         "",
   677  		Timeout:        3,
   678  		WalletOrTicket: false,
   679  	}
   680  	resp, _ := wallet.GetAPI().ExecWalletFunc("wallet", "WalletUnLock", walletUnLock)
   681  	if string(resp.(*types.Reply).GetMsg()) != types.ErrInputPassword.Error() {
   682  		t.Error("test input wrong password failed")
   683  	}
   684  
   685  	walletUnLock.Passwd = "Newpass123"
   686  	wallet.GetAPI().ExecWalletFunc("wallet", "WalletUnLock", walletUnLock)
   687  
   688  	resp, _ = wallet.GetAPI().ExecWalletFunc("wallet", "GetSeed", &types.GetSeedByPw{Passwd: "Newpass123"})
   689  	println("seed:", resp.(*types.ReplySeed).Seed)
   690  	time.Sleep(time.Second * 5)
   691  	wallet.GetAPI().ExecWalletFunc("wallet", "GetSeed", &types.GetSeedByPw{Passwd: "Newpass123"})
   692  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "GetSeed", &types.GetSeedByPw{Passwd: "Newpass123"})
   693  	if err.Error() != types.ErrWalletIsLocked.Error() {
   694  		t.Error("testProcWalletLock failed")
   695  	}
   696  
   697  	resp, _ = wallet.GetAPI().ExecWalletFunc("wallet", "GetWalletStatus", &types.ReqNil{})
   698  	status := resp.(*types.WalletStatus)
   699  	if !status.IsHasSeed || status.IsAutoMining || !status.IsWalletLock || (walletUnLock.GetWalletOrTicket() && status.IsTicketLock) {
   700  		t.Error("testGetWalletStatus failed")
   701  	}
   702  
   703  	walletUnLock.Timeout = 0
   704  	walletUnLock.WalletOrTicket = true
   705  	err = wallet.ProcWalletUnLock(walletUnLock)
   706  	require.NoError(t, err)
   707  	resp, _ = wallet.GetAPI().ExecWalletFunc("wallet", "GetWalletStatus", &types.ReqNil{})
   708  	status = resp.(*types.WalletStatus)
   709  	if !status.IsHasSeed || status.IsAutoMining || !status.IsWalletLock {
   710  		t.Error("testGetWalletStatus failed")
   711  	}
   712  
   713  	walletUnLock.WalletOrTicket = false
   714  	err = wallet.ProcWalletUnLock(walletUnLock)
   715  	require.NoError(t, err)
   716  
   717  	println("TestProcWalletLock end")
   718  	println("--------------------------")
   719  }
   720  
   721  // ProcWalletAddBlock
   722  func testProcWalletAddBlock(t *testing.T, wallet *Wallet) {
   723  	println("TestProcWalletAddBlock & TestProcWalletDelBlock begin")
   724  	tx := &types.Transaction{Execer: []byte(types.NoneX), To: ToAddr1}
   725  	blk := &types.Block{
   726  		Version:    1,
   727  		ParentHash: []byte("parent hash"),
   728  		TxHash:     []byte("tx hash"),
   729  		Height:     2,
   730  		BlockTime:  1,
   731  		Txs:        []*types.Transaction{tx},
   732  	}
   733  	blkDetail := &types.BlockDetail{
   734  		Block:    blk,
   735  		Receipts: []*types.ReceiptData{{Ty: types.ExecOk}},
   736  	}
   737  	msgAdd := wallet.client.NewMessage("wallet", types.EventAddBlock, blkDetail)
   738  	err := wallet.client.Send(msgAdd, false)
   739  	require.NoError(t, err)
   740  	time.Sleep(time.Second * 10)
   741  	msgDel := wallet.client.NewMessage("wallet", types.EventDelBlock, blkDetail)
   742  	err = wallet.client.Send(msgDel, false)
   743  	require.NoError(t, err)
   744  	println("TestProcWalletAddBlock & TestProcWalletDelBlock end")
   745  	println("--------------------------")
   746  }
   747  
   748  // SignRawTx
   749  func testSignRawTx(t *testing.T, wallet *Wallet) {
   750  	println("TestSignRawTx begin")
   751  	unsigned := &types.ReqSignRawTx{
   752  		Addr:   FromAddr,
   753  		TxHex:  "0a05636f696e73120c18010a081080c2d72f1a01312080897a30c0e2a4a789d684ad443a0131",
   754  		Expire: "0",
   755  	}
   756  	_, err := wallet.GetAPI().ExecWalletFunc("wallet", "SignRawTx", unsigned)
   757  	require.NoError(t, err)
   758  
   759  	unsigned.Privkey = AddrPrivKey
   760  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "SignRawTx", unsigned)
   761  	require.NoError(t, err)
   762  
   763  	//交易组的签名
   764  	group1 := "0a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6720c09a0c308dfddb82faf7dfc4113a2231444e615344524739524431397335396d65416f654e34613246365248393766536f40024aa5020aa3010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6720c09a0c308dfddb82faf7dfc4113a2231444e615344524739524431397335396d65416f654e34613246365248393766536f40024a204d14e67e6123d8efee02bf0d707380e9b82e5bd8972d085974879a41190eba7c5220d41e1ba3a374424254f3f417de8175a34671238798a2c63b28a90ff0233679960a7d0a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730b8b082d799a4ddc93a3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f40024a204d14e67e6123d8efee02bf0d707380e9b82e5bd8972d085974879a41190eba7c5220d41e1ba3a374424254f3f417de8175a34671238798a2c63b28a90ff023367996"
   765  	unsigned.TxHex = group1
   766  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "SignRawTx", unsigned)
   767  	require.NoError(t, err)
   768  
   769  	unsigned.TxHex = "0a05636f696e73120c18010a081080c2d72f1a01312080897a30c0e2a4a789d684ad443a0131"
   770  
   771  	//重新设置toaddr和fee
   772  	unsigned.NewToAddr = "1JzFKyrvSP5xWUkCMapUvrKDChgPDX1EN6"
   773  	unsigned.Fee = 10000
   774  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "SignRawTx", unsigned)
   775  	require.NoError(t, err)
   776  
   777  	//地址和私钥都为空
   778  	unsigned.Privkey = ""
   779  	unsigned.Addr = ""
   780  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "SignRawTx", unsigned)
   781  	assert.Equal(t, err, types.ErrNoPrivKeyOrAddr)
   782  
   783  	unsigned.Privkey = "0x"
   784  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "SignRawTx", unsigned)
   785  	assert.Equal(t, err, types.ErrPrivateKeyLen)
   786  
   787  	unsigned.Privkey = "0x5Z"
   788  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "SignRawTx", unsigned)
   789  	assert.NotNil(t, err)
   790  
   791  	signTy := wallet.SignType
   792  	wallet.SignType = 0xff
   793  	unsigned.Privkey = "0x55"
   794  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "SignRawTx", unsigned)
   795  	assert.NotNil(t, err)
   796  	wallet.SignType = signTy
   797  
   798  	println("TestSignRawTx end")
   799  	println("--------------------------")
   800  }
   801  
   802  // setFatalFailure
   803  func testsetFatalFailure(t *testing.T, wallet *Wallet) {
   804  	println("testsetFatalFailure begin")
   805  	var reportErrEvent types.ReportErrEvent
   806  	reportErrEvent.Frommodule = "wallet"
   807  	reportErrEvent.Tomodule = "wallet"
   808  	reportErrEvent.Error = "ErrDataBaseDamage"
   809  
   810  	wallet.GetAPI().ExecWalletFunc("wallet", "ErrToFront", &reportErrEvent)
   811  	println("testsetFatalFailure end")
   812  	println("--------------------------")
   813  }
   814  
   815  // getFatalFailure
   816  func testgetFatalFailure(t *testing.T, wallet *Wallet) {
   817  	println("testgetFatalFailure begin")
   818  	_, err := wallet.GetAPI().ExecWalletFunc("wallet", "FatalFailure", &types.ReqNil{})
   819  	require.NoError(t, err)
   820  	println("testgetFatalFailure end")
   821  	println("--------------------------")
   822  }
   823  
   824  func testWallet(t *testing.T, wallet *Wallet) {
   825  	println("test wallet begin")
   826  	addr, priv = util.Genaddress()
   827  	bpriv := wcom.CBCEncrypterPrivkey([]byte(wallet.Password), priv.Bytes())
   828  	was := &types.WalletAccountStore{Privkey: common.ToHex(bpriv), Label: "test", Addr: addr, TimeStamp: time.Now().String()}
   829  	err := wallet.SetWalletAccount(false, addr, was)
   830  	assert.NoError(t, err)
   831  	was1, err := wallet.GetAccountByAddr(addr)
   832  	assert.NoError(t, err)
   833  	assert.Equal(t, was.Privkey, was1.Privkey)
   834  	was2, err := wallet.GetAccountByLabel("test")
   835  	assert.NoError(t, err)
   836  	assert.Equal(t, was.Privkey, was2.Privkey)
   837  	priv2, err := wallet.GetPrivKeyByAddr(addr)
   838  	assert.NoError(t, err)
   839  	assert.Equal(t, priv, priv2)
   840  	_, err = wallet.GetWalletAccounts()
   841  	assert.NoError(t, err)
   842  	t.Log("password:", wallet.Password)
   843  
   844  	wallet.walletStore.SetWalletPassword("Newpass2")
   845  	assert.Equal(t, "Newpass2", wallet.walletStore.GetWalletPassword())
   846  
   847  	err = wallet.walletStore.SetFeeAmount(1e5)
   848  	assert.NoError(t, err)
   849  	fee := wallet.walletStore.GetFeeAmount(1e4)
   850  	assert.Equal(t, int64(1e5), fee)
   851  
   852  	println("test wallet end")
   853  
   854  	wallet.GetConfig()
   855  	wallet.GetDBStore()
   856  	wallet.GetSignType()
   857  	wallet.GetPassword()
   858  	wallet.Nonce()
   859  	wallet.GetRandom()
   860  	wallet.GetBlockHeight()
   861  	wallet.GetWalletDone()
   862  	wallet.GetLastHeader()
   863  	wallet.IsClose()
   864  	wallet.AddWaitGroup(1)
   865  	wallet.WaitGroupDone()
   866  	report := &WalletReport{}
   867  	err = wallet.RegisterMineStatusReporter(report)
   868  	assert.NoError(t, err)
   869  
   870  }
   871  
   872  func testSendTx(t *testing.T, wallet *Wallet) {
   873  	ok := wallet.IsCaughtUp()
   874  	assert.True(t, ok)
   875  
   876  	_, err := wallet.GetBalance(addr, "coins")
   877  	assert.NoError(t, err)
   878  
   879  	_, err = wallet.GetAllPrivKeys()
   880  	assert.NoError(t, err)
   881  	hash, err := wallet.SendTransaction(&types.ReceiptAccountTransfer{}, []byte("coins"), priv, ToAddr1)
   882  	assert.NoError(t, err)
   883  
   884  	//wallet.WaitTx(hash)
   885  	wallet.WaitTxs([][]byte{hash})
   886  	hash, err = wallet.SendTransaction(&types.ReceiptAccountTransfer{}, []byte("test"), priv, ToAddr1)
   887  	assert.NoError(t, err)
   888  	t.Log(common.ToHex(hash))
   889  
   890  	err = wallet.sendTransactionWait(&types.ReceiptAccountTransfer{}, []byte("test"), priv, ToAddr1)
   891  	assert.NoError(t, err)
   892  
   893  	_, err = wallet.getMinerColdAddr(addr)
   894  	assert.Equal(t, types.ErrActionNotSupport, err)
   895  
   896  }
   897  
   898  func testCreateNewAccountByIndex(t *testing.T, wallet *Wallet) {
   899  	println("testCreateNewAccountByIndex begin")
   900  
   901  	//首先创建一个airdropaddr标签的账户
   902  	reqNewAccount := &types.ReqNewAccount{Label: "airdropaddr"}
   903  	respp, err := wallet.GetAPI().ExecWalletFunc("wallet", "NewAccount", reqNewAccount)
   904  	require.NoError(t, err)
   905  	walletAcc := respp.(*types.WalletAccount)
   906  	addrtmp := walletAcc.GetAcc().Addr
   907  	if walletAcc.GetLabel() != "airdropaddr" {
   908  		t.Error("testCreateNewAccountByIndex", "walletAcc.GetLabel()", walletAcc.GetLabel(), "Label", "airdropaddr")
   909  	}
   910  
   911  	//index参数的校验。目前只支持10000000
   912  	reqIndex := &types.Int32{Data: 0}
   913  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "NewAccountByIndex", reqIndex)
   914  	assert.Equal(t, types.ErrInvalidParam, err)
   915  
   916  	//创建一个空投地址
   917  	reqIndex = &types.Int32{Data: 100000000}
   918  	resp1, err := wallet.GetAPI().ExecWalletFunc("wallet", "NewAccountByIndex", reqIndex)
   919  
   920  	require.NoError(t, err)
   921  	pubkey := resp1.(*types.ReplyString)
   922  
   923  	//通过pubkey换算成addr然后获取账户信息
   924  	privkeybyte, err := common.FromHex(pubkey.Data)
   925  	require.NoError(t, err)
   926  	pub, err := bipwallet.PrivkeyToPub(wallet.CoinType, uint32(wallet.SignType), privkeybyte)
   927  	require.NoError(t, err)
   928  
   929  	addr, err := bipwallet.PubToAddress(pub)
   930  	require.NoError(t, err)
   931  	if addr != "" {
   932  		//测试ProcGetAccountList函数
   933  		resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletGetAccountList", &types.ReqAccountList{})
   934  		assert.Nil(t, err)
   935  		accountlist := resp.(*types.WalletAccounts)
   936  		for _, acc := range accountlist.Wallets {
   937  			if addr == acc.Acc.Addr && addr != addrtmp {
   938  				if acc.GetLabel() != ("airdropaddr" + fmt.Sprintf("%d", 1)) {
   939  					t.Error("testCreateNewAccountByIndex", "addr", addr, "acc.GetLabel()", acc.GetLabel())
   940  				}
   941  			}
   942  		}
   943  	}
   944  
   945  	//已经存在,和上一次获取的地址是一致的
   946  	reqIndex = &types.Int32{Data: 100000000}
   947  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "NewAccountByIndex", reqIndex)
   948  
   949  	require.NoError(t, err)
   950  	pubkey = resp.(*types.ReplyString)
   951  
   952  	//通过pubkey换算成addr然后获取账户信息
   953  	privkeybyte, err = common.FromHex(pubkey.Data)
   954  	require.NoError(t, err)
   955  	pub2, err := bipwallet.PrivkeyToPub(wallet.CoinType, uint32(wallet.SignType), privkeybyte)
   956  	require.NoError(t, err)
   957  	addr2, err := bipwallet.PubToAddress(pub2)
   958  	require.NoError(t, err)
   959  	if addr != addr2 {
   960  		t.Error("TestProcCreateNewAccount", "addr", addr, "addr2", addr2)
   961  	}
   962  
   963  	privstr := "0x78a8c993abf85d2a452233033c19fac6b3bd4fe2c805615b337ef75dacd86ac9"
   964  	pubstr := "0277786ddef164b594f7db40d9a563f1ef1733cf34f1592f4c3bf1b344bd8f059b"
   965  	addrstr := "19QtNuUS9UN4hQPLrnYr3UhJsQYy4z4TMT"
   966  	privkeybyte, err = common.FromHex(privstr)
   967  	require.NoError(t, err)
   968  	pub3, err := bipwallet.PrivkeyToPub(wallet.CoinType, uint32(wallet.SignType), privkeybyte)
   969  	require.NoError(t, err)
   970  	pubtmp := hex.EncodeToString(pub3)
   971  	if pubtmp != pubstr {
   972  		t.Error("TestProcCreateNewAccount", "pubtmp", pubtmp, "pubstr", pubstr)
   973  	}
   974  	addr3, err := bipwallet.PubToAddress(pub3)
   975  	require.NoError(t, err)
   976  	if addr3 != addrstr {
   977  		t.Error("TestProcCreateNewAccount", "addr3", addr3, "addrstr", addrstr)
   978  	}
   979  	println("TestProcCreateNewAccount end")
   980  	println("--------------------------")
   981  }
   982  
   983  func TestInitSeedLibrary(t *testing.T) {
   984  	wallet, store, q, _ := initEnv()
   985  	defer os.RemoveAll("datadir") // clean up
   986  	defer wallet.Close()
   987  	defer store.Close()
   988  
   989  	//启动blockchain模块
   990  	blockchainModProc(q)
   991  	mempoolModProc(q)
   992  
   993  	InitSeedLibrary()
   994  	you := ChineseSeedCache["有"]
   995  	assert.Equal(t, "有", you)
   996  	abandon := EnglishSeedCache["abandon"]
   997  	assert.Equal(t, "abandon", abandon)
   998  
   999  	_, err := wallet.IsTransfer("1JzFKyrvSP5xWUkCMapUvrKDChgPDX1EN6")
  1000  	assert.Equal(t, err, types.ErrWalletIsLocked)
  1001  	//获取随机种子
  1002  	replySeed, err := wallet.GenSeed(0)
  1003  	require.NoError(t, err)
  1004  	replySeed, err = wallet.GenSeed(1)
  1005  	require.NoError(t, err)
  1006  	password := "heyubin123"
  1007  	wallet.SaveSeed(password, replySeed.Seed)
  1008  	wallet.ProcWalletUnLock(&types.WalletUnLock{Passwd: password})
  1009  
  1010  	//16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp ticket合约地址
  1011  	_, err = wallet.IsTransfer("16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp")
  1012  	require.NoError(t, err)
  1013  
  1014  	_, err = GetPrivkeyBySeed(wallet.walletStore.GetDB(), replySeed.Seed, 0, 2, wallet.CoinType)
  1015  	require.NoError(t, err)
  1016  
  1017  	// 指定生成私钥的index值
  1018  	_, err = GetPrivkeyBySeed(wallet.walletStore.GetDB(), replySeed.Seed, 10000, 2, wallet.CoinType)
  1019  	require.NoError(t, err)
  1020  
  1021  	// cointype不支持
  1022  	_, err = GetPrivkeyBySeed(wallet.walletStore.GetDB(), replySeed.Seed, 10000, 50, wallet.CoinType)
  1023  	assert.Equal(t, err, types.ErrNotSupport)
  1024  
  1025  	acc, err := wallet.GetBalance("1JzFKyrvSP5xWUkCMapUvrKDChgPDX1EN6", "token")
  1026  	require.NoError(t, err)
  1027  	assert.Equal(t, acc.Addr, "1JzFKyrvSP5xWUkCMapUvrKDChgPDX1EN6")
  1028  	assert.Equal(t, int64(0), acc.Balance)
  1029  
  1030  	err = wallet.RegisterMineStatusReporter(nil)
  1031  	assert.Equal(t, err, types.ErrInvalidParam)
  1032  
  1033  	in := wallet.AddrInWallet("")
  1034  	assert.Equal(t, in, false)
  1035  	policy := wcom.PolicyContainer[walletBizPolicyX]
  1036  	if policy != nil {
  1037  		policy.OnAddBlockTx(nil, nil, 0, nil)
  1038  		policy.OnDeleteBlockTx(nil, nil, 0, nil)
  1039  		need, _, _ := policy.SignTransaction(nil, nil)
  1040  		assert.Equal(t, true, need)
  1041  		account := types.Account{
  1042  			Addr:     "1JzFKyrvSP5xWUkCMapUvrKDChgPDX1EN6",
  1043  			Currency: 0,
  1044  			Balance:  0,
  1045  			Frozen:   0,
  1046  		}
  1047  		policy.OnCreateNewAccount(&account)
  1048  		policy.OnImportPrivateKey(&account)
  1049  		policy.OnAddBlockFinish(nil)
  1050  		policy.OnDeleteBlockFinish(nil)
  1051  		policy.OnClose()
  1052  		policy.OnSetQueueClient()
  1053  	}
  1054  }
  1055  
  1056  func testProcDumpPrivkeysFile(t *testing.T, wallet *Wallet) {
  1057  	println("testProcDumpPrivkeysFile begin")
  1058  	fileName := "Privkeys"
  1059  	_, err := os.Stat(fileName)
  1060  	if err == nil {
  1061  		os.Remove(fileName)
  1062  	}
  1063  
  1064  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "DumpPrivkeysFile", &types.ReqPrivkeysFile{FileName: fileName, Passwd: "123456"})
  1065  	require.NoError(t, err)
  1066  
  1067  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletGetAccountList", &types.ReqAccountList{})
  1068  	assert.Nil(t, err)
  1069  
  1070  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "DumpPrivkeysFile", &types.ReqPrivkeysFile{FileName: fileName, Passwd: "123456"})
  1071  	assert.Equal(t, err, types.ErrFileExists)
  1072  
  1073  	// 后面要对比
  1074  	AllAccountlist = resp.(*types.WalletAccounts)
  1075  	println("testProcDumpPrivkeysFile end")
  1076  	println("--------------------------")
  1077  }
  1078  
  1079  func testProcImportPrivkeysFile(t *testing.T, wallet *Wallet) {
  1080  	println("testProcImportPrivkeysFile begin")
  1081  	fileName := "Privkeys"
  1082  
  1083  	_, err := wallet.GetAPI().ExecWalletFunc("wallet", "ImportPrivkeysFile", &types.ReqPrivkeysFile{FileName: fileName, Passwd: "123456"})
  1084  	assert.Nil(t, err)
  1085  
  1086  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletGetAccountList", &types.ReqAccountList{})
  1087  	assert.Nil(t, err)
  1088  
  1089  	// 与之前的 AllAccountlist 对比
  1090  	accountlist := resp.(*types.WalletAccounts)
  1091  	assert.Equal(t, len(accountlist.GetWallets()), len(AllAccountlist.GetWallets()))
  1092  
  1093  	for _, acc1 := range AllAccountlist.GetWallets() {
  1094  		isEqual := false
  1095  		for _, acc2 := range accountlist.GetWallets() {
  1096  			if acc1.Acc.Addr == acc2.Acc.Addr {
  1097  				isEqual = true
  1098  				break
  1099  			}
  1100  		}
  1101  		if !isEqual {
  1102  			assert.Nil(t, errors.New(acc1.Acc.Addr+" not find in new list."))
  1103  		}
  1104  	}
  1105  
  1106  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "ImportPrivkeysFile", &types.ReqPrivkeysFile{FileName: fileName, Passwd: "12345678"})
  1107  	assert.Equal(t, err, types.ErrVerifyOldpasswdFail)
  1108  
  1109  	println("testProcImportPrivkeysFile end")
  1110  	println("--------------------------")
  1111  }
  1112  
  1113  func testProcImportPrivkeysFile2(t *testing.T, wallet *Wallet) {
  1114  	println("testProcImportPrivkeysFile begin")
  1115  	reqNewAccount := &types.ReqNewAccount{Label: "account:0"}
  1116  	_, err := wallet.GetAPI().ExecWalletFunc("wallet", "NewAccount", reqNewAccount)
  1117  	assert.NoError(t, err)
  1118  
  1119  	privKey := &types.ReqWalletImportPrivkey{}
  1120  	privKey.Privkey = "0xb94ae286a508e4bb3fbbcb61997822fea6f0a534510597ef8eb60a19d6b219a0"
  1121  	privKey.Label = "ImportPrivKey-Label-hyb-new"
  1122  	wallet.GetAPI().ExecWalletFunc("wallet", "WalletImportPrivkey", privKey)
  1123  
  1124  	fileName := "Privkeys"
  1125  	_, err = wallet.GetAPI().ExecWalletFunc("wallet", "ImportPrivkeysFile", &types.ReqPrivkeysFile{FileName: fileName, Passwd: "123456"})
  1126  	assert.Nil(t, err)
  1127  
  1128  	resp, err := wallet.GetAPI().ExecWalletFunc("wallet", "WalletGetAccountList", &types.ReqAccountList{})
  1129  	assert.Nil(t, err)
  1130  
  1131  	// 与之前的 AllAccountlist 对比
  1132  	accountlist := resp.(*types.WalletAccounts)
  1133  	assert.Equal(t, len(accountlist.GetWallets()), len(AllAccountlist.GetWallets())+1)
  1134  
  1135  	os.Remove(fileName)
  1136  	println("testProcImportPrivkeysFile end")
  1137  	println("--------------------------")
  1138  }
  1139  
  1140  type WalletReport struct {
  1141  }
  1142  
  1143  func (report WalletReport) IsAutoMining() bool {
  1144  	return true
  1145  
  1146  }
  1147  func (report WalletReport) IsTicketLocked() bool {
  1148  	return true
  1149  
  1150  }
  1151  func (report WalletReport) PolicyName() string {
  1152  	return "ticket"
  1153  }