github.com/turingchain2020/turingchain@v1.1.21/blockchain/chain_duptx_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 blockchain_test
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  
    11  	_ "github.com/turingchain2020/turingchain/system"
    12  	"github.com/turingchain2020/turingchain/types"
    13  	"github.com/turingchain2020/turingchain/util"
    14  	"github.com/turingchain2020/turingchain/util/testnode"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func addTestBlock(t *testing.T, mock33 *testnode.TuringchainMock, blockNum int, isTxHeight bool) []*types.Transaction {
    20  
    21  	var txs []*types.Transaction
    22  	for i := 0; i < blockNum; i++ {
    23  		var txList []*types.Transaction
    24  		var err error
    25  		currHeight := mock33.GetBlockChain().GetBlockHeight()
    26  		if isTxHeight {
    27  			txList, _, err = addTxTxHeight(mock33.GetAPI().GetConfig(), mock33.GetGenesisKey(), mock33.GetAPI(), currHeight)
    28  		} else {
    29  			txList, _, err = addTx(mock33.GetAPI().GetConfig(), mock33.GetGenesisKey(), mock33.GetAPI())
    30  		}
    31  		txHeight := types.GetTxHeight(mock33.GetAPI().GetConfig(), txList[0].Expire, currHeight+1)
    32  		require.Nilf(t, err, "currHeight:%d, txHeight=%d", currHeight, txHeight)
    33  		txs = append(txs, txList...)
    34  		for mock33.GetBlockChain().GetBlockHeight() != currHeight+1 {
    35  			time.Sleep(time.Millisecond)
    36  		}
    37  	}
    38  	return txs
    39  }
    40  
    41  //构造10个区块,10笔交易不带TxHeight,缓存size128
    42  func TestCheckDupTxHashList01(t *testing.T) {
    43  	mock33 := testnode.New("", nil)
    44  	defer func() {
    45  		defer mock33.Close()
    46  	}()
    47  	cfg := mock33.GetClient().GetConfig()
    48  	chainlog.Debug("TestCheckDupTxHashList01 begin --------------------")
    49  
    50  	blockchain := mock33.GetBlockChain()
    51  	txs := addTestBlock(t, mock33, 10, false)
    52  
    53  	//重复交易
    54  	duptxhashlist, err := checkDupTx(txs, blockchain)
    55  	assert.Nil(t, err)
    56  	assert.Equal(t, len(duptxhashlist.Hashes), len(txs))
    57  	//非重复交易
    58  	txs = util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 50)
    59  	duptxhashlist, err = checkDupTx(txs, blockchain)
    60  	assert.Nil(t, err)
    61  	assert.Equal(t, len(duptxhashlist.Hashes), 0)
    62  
    63  	txlist := util.GenTxsTxHeight(cfg, mock33.GetGenesisKey(), 50, 10)
    64  	txs = append(txs, txlist...)
    65  	duptxhashlist, err = checkDupTxHeight(txs, blockchain)
    66  	assert.Nil(t, err)
    67  	assert.Equal(t, len(duptxhashlist.Hashes), 0)
    68  	chainlog.Debug("TestCheckDupTxHashList01 end --------------------")
    69  }
    70  
    71  //构造10个区块,10笔交易带TxHeight,缓存size128
    72  func TestCheckDupTxHashList02(t *testing.T) {
    73  	mock33 := testnode.New("", nil)
    74  	defer func() {
    75  		defer mock33.Close()
    76  	}()
    77  	cfg := mock33.GetClient().GetConfig()
    78  	chainlog.Debug("TestCheckDupTxHashList02 begin --------------------")
    79  	blockchain := mock33.GetBlockChain()
    80  	txs := addTestBlock(t, mock33, 10, true)
    81  	//重复交易
    82  	duptxhashlist, err := checkDupTxHeight(txs, blockchain)
    83  	assert.Nil(t, err)
    84  	assert.Equal(t, len(duptxhashlist.Hashes), len(txs))
    85  
    86  	//非重复交易
    87  	txs = util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 50)
    88  	duptxhashlist, err = checkDupTxHeight(txs, blockchain)
    89  	assert.Nil(t, err)
    90  	assert.Equal(t, len(duptxhashlist.Hashes), 0)
    91  
    92  	txList := util.GenTxsTxHeight(cfg, mock33.GetGenesisKey(), 50, 10)
    93  	txs = append(txs, txList...)
    94  	duptxhashlist, err = checkDupTxHeight(txs, blockchain)
    95  	assert.Nil(t, err)
    96  	assert.Equal(t, len(duptxhashlist.Hashes), 0)
    97  
    98  	chainlog.Debug("TestCheckDupTxHashList02 end --------------------")
    99  }
   100  
   101  //构造130个区块,130笔交易不带TxHeight,缓存满
   102  func TestCheckDupTxHashList03(t *testing.T) {
   103  	mock33 := testnode.New("", nil)
   104  	defer func() {
   105  		defer mock33.Close()
   106  	}()
   107  	cfg := mock33.GetClient().GetConfig()
   108  	chainlog.Debug("TestCheckDupTxHashList03 begin --------------------")
   109  	blockchain := mock33.GetBlockChain()
   110  	txs := addTestBlock(t, mock33, 130, false)
   111  
   112  	//重复交易,不带TxHeight,cache没有会检查db
   113  	duptxhashlist, err := checkDupTx(txs, blockchain)
   114  	assert.Nil(t, err)
   115  	assert.Equal(t, len(duptxhashlist.Hashes), len(txs))
   116  
   117  	txs = util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 50)
   118  	duptxhashlist, err = checkDupTx(txs, blockchain)
   119  	assert.Nil(t, err)
   120  	assert.Equal(t, len(duptxhashlist.Hashes), 0)
   121  
   122  	txlist := util.GenTxsTxHeight(cfg, mock33.GetGenesisKey(), 50, 10)
   123  	txs = append(txs, txlist...)
   124  	duptxhashlist, err = checkDupTxHeight(txs, blockchain)
   125  	assert.Nil(t, err)
   126  	assert.Equal(t, len(duptxhashlist.Hashes), 0)
   127  	chainlog.Debug("TestCheckDupTxHashList03 end --------------------")
   128  }
   129  
   130  //构造130个区块,130笔交易带TxHeight,缓存满
   131  func TestCheckDupTxHashList04(t *testing.T) {
   132  	mock33 := testnode.New("", nil)
   133  	defer func() {
   134  		defer mock33.Close()
   135  	}()
   136  	cfg := mock33.GetClient().GetConfig()
   137  	chainlog.Debug("TestCheckDupTxHashList04 begin --------------------")
   138  	blockchain := mock33.GetBlockChain()
   139  
   140  	txs := addTestBlock(t, mock33, 130, true)
   141  	time.Sleep(time.Second)
   142  	duptxhashlist, err := checkDupTx(txs, blockchain)
   143  	assert.Nil(t, err)
   144  	assert.Equal(t, len(duptxhashlist.Hashes), len(txs))
   145  
   146  	//非重复交易
   147  	txs = util.GenCoinsTxs(cfg, mock33.GetGenesisKey(), 50)
   148  	duptxhashlist, err = checkDupTx(txs, blockchain)
   149  	assert.Nil(t, err)
   150  	assert.Equal(t, len(duptxhashlist.Hashes), 0)
   151  
   152  	txlist := util.GenTxsTxHeight(cfg, mock33.GetGenesisKey(), 50, 10)
   153  	txs = append(txs, txlist...)
   154  	duptxhashlist, err = checkDupTxHeight(txs, blockchain)
   155  	assert.Nil(t, err)
   156  	assert.Equal(t, len(duptxhashlist.Hashes), 0)
   157  
   158  	chainlog.Debug("TestCheckDupTxHashList04 end --------------------")
   159  }
   160  
   161  //异常:构造10个区块,10笔交易带TxHeight,TxHeight不满足条件 size128
   162  func TestCheckDupTxHashList05(t *testing.T) {
   163  	mock33 := testnode.New("", nil)
   164  	defer func() {
   165  		defer mock33.Close()
   166  	}()
   167  	cfg := mock33.GetClient().GetConfig()
   168  	chainlog.Debug("TestCheckDupTxHashList05 begin --------------------")
   169  	//发送带TxHeight交易且TxHeight不满足条件
   170  	for i := 2; i < 10; i++ {
   171  		_, _, err := addTxTxHeight(cfg, mock33.GetGenesisKey(), mock33.GetAPI(), int64(i)+types.LowAllowPackHeight)
   172  		require.EqualErrorf(t, err, types.ErrTxExpire.Error(), "index-%d", i)
   173  		time.Sleep(sendTxWait)
   174  	}
   175  	chainlog.Debug("TestCheckDupTxHashList05 end --------------------")
   176  }