github.com/aergoio/aergo@v1.3.1/chain/signverifier_test.go (about)

     1  package chain
     2  
     3  import (
     4  	"encoding/binary"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/aergoio/aergo/account/key"
     9  	"github.com/aergoio/aergo/types"
    10  	"github.com/btcsuite/btcd/btcec"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  const (
    15  	maxAccount   = 2
    16  	maxRecipient = 2
    17  )
    18  
    19  var (
    20  	accs      [maxAccount][]byte
    21  	sign      [maxAccount]*btcec.PrivateKey
    22  	recipient [maxRecipient][]byte
    23  	txs       []*types.Tx
    24  
    25  	verifier *SignVerifier
    26  )
    27  
    28  func TestTXs(t *testing.T) {
    29  }
    30  
    31  func _itobU32(argv uint32) []byte {
    32  	bs := make([]byte, 4)
    33  	binary.LittleEndian.PutUint32(bs, argv)
    34  	return bs
    35  }
    36  
    37  func beforeTest(txCount int) error {
    38  	if verifier == nil {
    39  		verifier = NewSignVerifier(nil /*types.DefaultVerifierCnt*/, nil, 4, false)
    40  	}
    41  
    42  	for i := 0; i < maxAccount; i++ {
    43  		privkey, err := btcec.NewPrivateKey(btcec.S256())
    44  		if err != nil {
    45  			return err
    46  		}
    47  		//gen new address
    48  		accs[i] = key.GenerateAddress(&privkey.PublicKey)
    49  		sign[i] = privkey
    50  		recipient[i] = _itobU32(uint32(i))
    51  	}
    52  
    53  	txCountPerAcc := txCount / maxAccount
    54  	txs = make([]*types.Tx, 0, txCount)
    55  
    56  	// gen Tx
    57  	nonce := make([]uint64, txCountPerAcc)
    58  	for i := 0; i < txCountPerAcc; i++ {
    59  		nonce[i] = uint64(i + 1)
    60  	}
    61  	for i := 0; i < maxAccount; i++ {
    62  		for j := 0; j < txCountPerAcc; j++ {
    63  			tmp := genTx(i, j%maxAccount, nonce[j], uint64(i+1))
    64  			txs = append(txs, tmp)
    65  		}
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func afterTest() {
    72  
    73  }
    74  
    75  func genTx(acc int, rec int, nonce uint64, amount uint64) *types.Tx {
    76  	tx := types.Tx{
    77  		Body: &types.TxBody{
    78  			Nonce:     nonce,
    79  			Account:   accs[acc],
    80  			Recipient: recipient[rec],
    81  			Amount:    new(big.Int).SetUint64(amount).Bytes(),
    82  		},
    83  	}
    84  	//tx.Hash = tx.CalculateTxHash()
    85  	key.SignTx(&tx, sign[acc])
    86  	return &tx
    87  }
    88  
    89  func TestInvalidTransactions(t *testing.T) {
    90  	t.Log("TestInvalidTransactions")
    91  	beforeTest(10)
    92  	//defer afterTest()
    93  
    94  	txslice := make([]*types.Tx, 0)
    95  	tx := genTx(0, 1, 1, 1)
    96  	tx.Body.Amount = new(big.Int).SetUint64(999999).Bytes()
    97  
    98  	txslice = append(txslice, tx)
    99  
   100  	verifier.RequestVerifyTxs(&types.TxList{Txs: txslice})
   101  	failed, errs := verifier.WaitDone()
   102  
   103  	assert.Equal(t, failed, true)
   104  
   105  	if failed {
   106  		for i, err := range errs {
   107  			if err != nil {
   108  				assert.Equal(t, i, 0)
   109  				assert.Equal(t, err, types.ErrSignNotMatch)
   110  			}
   111  		}
   112  	}
   113  }
   114  
   115  // gen sequential transactions
   116  // bench
   117  func TestVerifyValidTxs(t *testing.T) {
   118  	t.Log("TestVerifyValidTxs")
   119  	beforeTest(100)
   120  	defer afterTest()
   121  
   122  	t.Logf("len=%d", len(txs))
   123  
   124  	verifier.RequestVerifyTxs(&types.TxList{Txs: txs})
   125  	failed, errs := verifier.WaitDone()
   126  
   127  	if failed {
   128  		for i, err := range errs {
   129  			if err != nil {
   130  				t.Fatalf("failed tx %d:%s", i, err.Error())
   131  			}
   132  		}
   133  	}
   134  }
   135  
   136  func BenchmarkVerify10000tx(b *testing.B) {
   137  	b.Log("BenchmarkVerify10000tx")
   138  	beforeTest(10000)
   139  	defer afterTest()
   140  
   141  	txslice := make([]*types.Tx, 0)
   142  	for _, tx := range txs {
   143  		txslice = append(txslice, tx)
   144  	}
   145  
   146  	b.ResetTimer()
   147  
   148  	for i := 0; i < b.N; i++ {
   149  		verifier.RequestVerifyTxs(&types.TxList{Txs: txslice})
   150  		failed, errs := verifier.WaitDone()
   151  
   152  		if failed {
   153  			for i, err := range errs {
   154  				if err != nil {
   155  					b.Errorf("failed tx %d:%s", i, err.Error())
   156  				}
   157  			}
   158  		}
   159  	}
   160  }
   161  
   162  func BenchmarkVerify10000txSerial(b *testing.B) {
   163  	b.Log("BenchmarkVerify10000txSerial")
   164  	beforeTest(10000)
   165  	defer afterTest()
   166  
   167  	txslice := make([]*types.Tx, 0)
   168  	for _, tx := range txs {
   169  		txslice = append(txslice, tx)
   170  	}
   171  
   172  	b.ResetTimer()
   173  
   174  	for i := 0; i < b.N; i++ {
   175  		failed, errs := verifier.verifyTxsInplace(&types.TxList{Txs: txslice})
   176  		if failed {
   177  			for i, err := range errs {
   178  				if err != nil {
   179  					b.Errorf("failed tx %d:%s", i, err.Error())
   180  				}
   181  			}
   182  		}
   183  	}
   184  }