github.com/aergoio/aergo@v1.3.1/mempool/txlist_test.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  package mempool
     6  
     7  import (
     8  	"math/big"
     9  	"math/rand"
    10  	"testing"
    11  
    12  	"github.com/aergoio/aergo/fee"
    13  	"github.com/aergoio/aergo/types"
    14  )
    15  
    16  func NewState(nonce uint64, bal uint64) *types.State {
    17  	return &types.State{Nonce: nonce, Balance: new(big.Int).SetUint64(bal).Bytes()}
    18  }
    19  func TestListPutBasic(t *testing.T) {
    20  	initTest(t)
    21  	defer deinitTest()
    22  	mpl := NewTxList(nil, NewState(0, 0))
    23  
    24  	count := 100
    25  	nonce := make([]int, count)
    26  	for i := 0; i < count; i++ {
    27  		nonce[i] = i + 1
    28  	}
    29  	rand.Shuffle(count, func(i, j int) {
    30  		nonce[i], nonce[j] = nonce[j], nonce[i]
    31  	})
    32  	for i := 0; i < count; i++ {
    33  		mpl.Put(genTx(0, 0, uint64(nonce[i]), 0))
    34  	}
    35  
    36  	ret := mpl.Get()
    37  	if len(ret) != count {
    38  		t.Error("put failed", len(ret), count)
    39  	}
    40  }
    41  
    42  func TestListPutBasicOrphan(t *testing.T) {
    43  	initTest(t)
    44  	defer deinitTest()
    45  	mpl := NewTxList(nil, NewState(0, 0))
    46  
    47  	count := 20
    48  	nonce := make([]int, count)
    49  	for i := 0; i < count; i++ {
    50  		nonce[i] = i + 1
    51  	}
    52  	rand.Shuffle(count, func(i, j int) {
    53  		nonce[i], nonce[j] = nonce[j], nonce[i]
    54  	})
    55  	nonce = nonce[count/2:]
    56  
    57  	for i := 0; i < len(nonce); i++ {
    58  		mpl.Put(genTx(0, 0, uint64(nonce[i]), 0))
    59  	}
    60  
    61  	ret := mpl.GetAll()
    62  	if len(ret) != len(nonce) {
    63  		t.Error("put failed", len(ret), len(nonce))
    64  	}
    65  }
    66  
    67  func TestListPutErrors(t *testing.T) {
    68  	initTest(t)
    69  	defer deinitTest()
    70  	mpl := NewTxList(nil, NewState(9, 0))
    71  	added, err := mpl.Put(genTx(0, 0, uint64(1), 0))
    72  	if added != 0 || err != types.ErrTxNonceTooLow {
    73  		t.Errorf("put should be failed with ErrTxNonceTooLow, but %s", err)
    74  	}
    75  
    76  	added, err = mpl.Put(genTx(0, 0, uint64(10), 0))
    77  	if added != 0 || err != nil || len(mpl.list) != 1 {
    78  		t.Errorf("put should be not failed, but (%d)%s", added, err)
    79  	}
    80  
    81  	added, err = mpl.Put(genTx(0, 0, uint64(10), 0))
    82  	if added != 0 || err != types.ErrSameNonceAlreadyInMempool {
    83  		t.Errorf("put should be failed with ErrSameNonceAlreadyInMempool, but %s", err)
    84  	}
    85  
    86  }
    87  func TestListDel(t *testing.T) {
    88  	initTest(t)
    89  	defer deinitTest()
    90  	mpl := NewTxList(nil, NewState(0, 0))
    91  
    92  	fee.EnableZeroFee()
    93  	ret, txs := mpl.FilterByState(NewState(2, 100))
    94  	if ret != 0 || mpl.Len() != 0 || len(txs) != 0 {
    95  		t.Error(ret, mpl.Len(), len(txs))
    96  	}
    97  
    98  	ret, txs = mpl.FilterByState(NewState(0, 100))
    99  	if ret != 0 || mpl.Len() != 0 || len(txs) != 0 {
   100  		t.Error(ret, mpl.Len(), len(txs))
   101  	}
   102  
   103  	count := 100
   104  
   105  	//put 1~10 excpet 4 6 8
   106  	for i := 0; i < count; i++ {
   107  		if i == 3 || i == 5 || i == 7 {
   108  			continue
   109  		}
   110  		mpl.Put(genTx(0, 0, uint64(i+1), 0))
   111  	}
   112  	// 1, |2, 3, | x, 5, x, 7, | x, 9... 14, |15... 100
   113  	ret, txs = mpl.FilterByState(NewState(0, 100))
   114  	if ret != 0 || mpl.Len() != 3 || len(txs) != 0 {
   115  		t.Error(ret, mpl.Len(), len(txs))
   116  	}
   117  
   118  	ret, txs = mpl.FilterByState(NewState(1, 100))
   119  	if ret != 0 || mpl.Len() != 2 || len(txs) != 1 {
   120  		t.Error(ret, mpl.Len(), len(txs))
   121  	}
   122  
   123  	ret, txs = mpl.FilterByState(NewState(3, 100))
   124  	if ret != 0 || mpl.Len() != 0 || len(txs) != 2 {
   125  		t.Error(ret, mpl.Len(), len(txs))
   126  	}
   127  
   128  	ret, txs = mpl.FilterByState(NewState(7, 100))
   129  	if ret != 2 || mpl.Len() != 0 || len(txs) != 2 {
   130  		t.Error(ret, mpl.Len(), len(txs))
   131  	}
   132  
   133  	ret, txs = mpl.FilterByState(NewState(14, 100))
   134  	if ret != 92 || mpl.Len() != count-14 || len(txs) != 6 {
   135  		t.Error(ret, mpl.Len(), len(txs))
   136  	}
   137  
   138  	txs = mpl.Get()
   139  	if txs[0].GetBody().GetNonce() != 15 {
   140  		t.Error(txs[0].GetBody().GetNonce())
   141  	}
   142  
   143  }
   144  
   145  func TestListDelMiddle(t *testing.T) {
   146  	initTest(t)
   147  	defer deinitTest()
   148  	mpl := NewTxList(nil, NewState(3, 0))
   149  
   150  	mpl.Put(genTx(0, 0, uint64(4), 0))
   151  	mpl.Put(genTx(0, 0, uint64(5), 0))
   152  	mpl.Put(genTx(0, 0, uint64(6), 0))
   153  
   154  	if mpl.Len() != 3 {
   155  		t.Error("should be 3 not ", len(mpl.list))
   156  	}
   157  	fee.EnableZeroFee()
   158  	ret, txs := mpl.FilterByState(NewState(1, 100))
   159  	if ret != -3 || mpl.Len() != 0 || len(txs) != 0 {
   160  		t.Error(ret, mpl.Len(), len(txs))
   161  	}
   162  	ret, txs = mpl.FilterByState(NewState(4, 100))
   163  	if ret != 3 || mpl.Len() != 2 || len(txs) != 1 {
   164  		t.Error(ret, mpl.Len(), len(txs))
   165  	}
   166  
   167  }
   168  
   169  func TestListPutRandom(t *testing.T) {
   170  	initTest(t)
   171  	defer deinitTest()
   172  	mpl := NewTxList(nil, NewState(0, 0))
   173  
   174  	count := 100
   175  	txs := make([]types.Transaction, count)
   176  	for i := 0; i < count; i++ {
   177  		txs[i] = genTx(0, 0, uint64(i+1), 0)
   178  	}
   179  	rand.Shuffle(count, func(i, j int) {
   180  		txs[i], txs[j] = txs[j], txs[i]
   181  	})
   182  	for i := 0; i < count; i++ {
   183  		mpl.Put(txs[i])
   184  		if !sameTxs(mpl.GetAll(), txs[:i+1]) {
   185  			t.Error("GetAll returns unproperly")
   186  		}
   187  	}
   188  	ret := mpl.Get()
   189  	if len(ret) != count {
   190  		t.Error("put failed", len(ret), count)
   191  	}
   192  }