github.com/turingchain2020/turingchain@v1.1.21/types/tx_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 types
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/turingchain2020/turingchain/common"
    14  	"github.com/turingchain2020/turingchain/common/crypto"
    15  
    16  	"strings"
    17  
    18  	_ "github.com/turingchain2020/turingchain/system/crypto/init"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  func TestCreateGroupTx(t *testing.T) {
    23  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
    24  	tx1 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
    25  	tx2 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630de92c3828ad194b26d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
    26  	tx3 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630b0d6c895c4d28efe5d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
    27  	tx11, _ := hex.DecodeString(tx1)
    28  	tx21, _ := hex.DecodeString(tx2)
    29  	tx31, _ := hex.DecodeString(tx3)
    30  	var tx12 Transaction
    31  	Decode(tx11, &tx12)
    32  	var tx22 Transaction
    33  	Decode(tx21, &tx22)
    34  	var tx32 Transaction
    35  	Decode(tx31, &tx32)
    36  
    37  	group, err := CreateTxGroup([]*Transaction{&tx12, &tx22, &tx32}, cfg.GetMinTxFeeRate())
    38  	if err != nil {
    39  		t.Error(err)
    40  		return
    41  	}
    42  	err = group.Check(cfg, 0, cfg.GetMinTxFeeRate(), cfg.GetMaxTxFee())
    43  	if err != nil {
    44  		for i := 0; i < len(group.Txs); i++ {
    45  			t.Log(group.Txs[i].JSON())
    46  		}
    47  		t.Error(err)
    48  		return
    49  	}
    50  	newtx := group.Tx()
    51  	grouptx := hex.EncodeToString(Encode(newtx))
    52  	t.Log(grouptx)
    53  }
    54  
    55  func TestCreateParaGroupTx(t *testing.T) {
    56  	str := GetDefaultCfgstring()
    57  	new := strings.Replace(str, "Title=\"local\"", "Title=\"turingchain\"", 1)
    58  	cfg := NewTuringchainConfig(new)
    59  
    60  	testHeight := int64(1687250 + 1)
    61  	tx1 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
    62  	tx2 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630de92c3828ad194b26d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
    63  	tx3 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630b0d6c895c4d28efe5d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
    64  	tx11, _ := hex.DecodeString(tx1)
    65  	tx21, _ := hex.DecodeString(tx2)
    66  	tx31, _ := hex.DecodeString(tx3)
    67  	var tx12 Transaction
    68  	Decode(tx11, &tx12)
    69  	var tx22 Transaction
    70  	Decode(tx21, &tx22)
    71  	var tx32 Transaction
    72  	Decode(tx31, &tx32)
    73  
    74  	tx12.Execer = []byte("user.p.test.token")
    75  	tx22.Execer = []byte("token")
    76  	tx32.Execer = []byte("user.p.test.ticket")
    77  
    78  	feeRate := cfg.GetMinTxFeeRate()
    79  	//SetFork("", "ForkTxGroupPara", 0)
    80  	group, err := CreateTxGroup([]*Transaction{&tx12, &tx22, &tx32}, feeRate)
    81  	if err != nil {
    82  		t.Error(err)
    83  		return
    84  	}
    85  	err = group.Check(cfg, testHeight, cfg.GetMinTxFeeRate(), cfg.GetMaxTxFee())
    86  	if err != nil {
    87  		for i := 0; i < len(group.Txs); i++ {
    88  			t.Log(group.Txs[i].JSON())
    89  		}
    90  		//t.Error(err)
    91  
    92  	}
    93  	assert.Equal(t, ErrTxGroupParaMainMixed, err)
    94  
    95  	tx22.Execer = []byte("user.p.para.token")
    96  	group, err = CreateTxGroup([]*Transaction{&tx12, &tx22, &tx32}, feeRate)
    97  	if err != nil {
    98  		t.Error(err)
    99  		return
   100  	}
   101  	err = group.Check(cfg, testHeight, cfg.GetMinTxFeeRate(), cfg.GetMaxTxFee())
   102  	assert.Equal(t, ErrTxGroupParaCount, err)
   103  
   104  	tx22.Execer = []byte("user.p.test.paracross")
   105  	group, err = CreateTxGroup([]*Transaction{&tx12, &tx22, &tx32}, feeRate)
   106  	if err != nil {
   107  		t.Error(err)
   108  		return
   109  	}
   110  	err = group.Check(cfg, testHeight, cfg.GetMinTxFeeRate(), cfg.GetMaxTxFee())
   111  	assert.Nil(t, err)
   112  	newtx := group.Tx()
   113  	grouptx := hex.EncodeToString(Encode(newtx))
   114  	t.Log(grouptx)
   115  }
   116  
   117  func TestCreateGroupTxWithSize(t *testing.T) {
   118  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
   119  	tx1 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   120  	tx2 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630de92c3828ad194b26d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   121  	tx3 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630b0d6c895c4d28efe5d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   122  	tx11, _ := hex.DecodeString(tx1)
   123  	tx21, _ := hex.DecodeString(tx2)
   124  	tx31, _ := hex.DecodeString(tx3)
   125  
   126  	len150str := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b5366761122"
   127  	len130str := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c76364339715734617678733245375"
   128  	len105str := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c7633"
   129  
   130  	var tx12 Transaction
   131  	Decode(tx11, &tx12)
   132  	tx12.Fee = 1
   133  	//构造临界size, fee=1,构建时候计算出size是998, 构建之前fee是100000,构建之后tx[0].fee=200000,原代码会出错
   134  	extSize := []byte(len150str + len150str + len150str + len105str)
   135  	tx12.Payload = append(tx12.Payload, extSize...)
   136  
   137  	var tx22 Transaction
   138  	Decode(tx21, &tx22)
   139  	//构造临界size, 有没有header的场景
   140  	extSize = []byte(len150str + len150str + len150str + len130str)
   141  	tx22.Payload = append(tx22.Payload, extSize...)
   142  	var tx32 Transaction
   143  	Decode(tx31, &tx32)
   144  
   145  	group, err := CreateTxGroup([]*Transaction{&tx12, &tx22, &tx32}, cfg.GetMinTxFeeRate())
   146  	if err != nil {
   147  		t.Error(err)
   148  		return
   149  	}
   150  
   151  	err = group.Check(cfg, 0, cfg.GetMinTxFeeRate(), cfg.GetMaxTxFee())
   152  	if err != nil {
   153  		for i := 0; i < len(group.Txs); i++ {
   154  			t.Log(group.Txs[i].JSON())
   155  		}
   156  		t.Error(err)
   157  		return
   158  	}
   159  	newtx := group.Tx()
   160  	grouptx := hex.EncodeToString(Encode(newtx))
   161  	t.Log(grouptx)
   162  }
   163  
   164  func TestDecodeTx(t *testing.T) {
   165  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
   166  	signtx := "0a05636f696e73120e18010a0a1080c2d72f1a036f74651a6d0801122102504fa1c28caaf1d5a20fefb87c50a49724ff401043420cb3ba271997eb5a43871a46304402200e566613679e8fe645990adb8ed6aa8c46060d944f5bab358e2c78443c3eed53022049d671e596d48f091dae3558b6fd811250412101765ba0dec5cc4188a180088720e0a71230f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b53667640034ada050afe010a05636f696e73120e18010a0a1080c2d72f1a036f74651a6d0801122102504fa1c28caaf1d5a20fefb87c50a49724ff401043420cb3ba271997eb5a43871a46304402200e566613679e8fe645990adb8ed6aa8c46060d944f5bab358e2c78443c3eed53022049d671e596d48f091dae3558b6fd811250412101765ba0dec5cc4188a180088720e0a71230f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b53667640034a20a9ef5454033a9ab080360470291e9b1f63881ff9a03c3c09a06e7200688d019852209a56c5dbff8b246e32d3ad0534f42dbbae88cf4b0bed24fe6420e06d59187c690afb010a05636f696e73120e18010a0a1080c2d72f1a036f74651a6e0801122102504fa1c28caaf1d5a20fefb87c50a49724ff401043420cb3ba271997eb5a43871a473045022100ac7acba851854179f0d574428e8c5a4c69d4431604e8626fd7ace87a8abe1f6c022039eb3f7ec190030b2c7e32457972482b3d074521856ea0d09820071b39ffb4b930de92c3828ad194b26d3a22313271796f6361794e46374c7636433971573461767873324537553431664b53667640034a20a9ef5454033a9ab080360470291e9b1f63881ff9a03c3c09a06e7200688d0198522036bb9ca17aeef20b6e9afcd5ba52d89f5109db5b5d2aee200723b2bb0c7e9aa30ad8010a05636f696e73120e18010a0a1080c2d72f1a036f74651a6d0801122102504fa1c28caaf1d5a20fefb87c50a49724ff401043420cb3ba271997eb5a43871a4630440220094e12621f235ea46e99d21f30e8be510a52e6d92410b35e307936ce61aafe9602207fcdeb51825af222159c82b74ab2386d01263e4903d4a0cf96426c1b48bd083130b0d6c895c4d28efe5d3a22313271796f6361794e46374c7636433971573461767873324537553431664b53667640034a20a9ef5454033a9ab080360470291e9b1f63881ff9a03c3c09a06e7200688d019852209a56c5dbff8b246e32d3ad0534f42dbbae88cf4b0bed24fe6420e06d59187c69"
   167  	var tx Transaction
   168  	txhex, _ := hex.DecodeString(signtx)
   169  	Decode(txhex, &tx)
   170  	group, err := tx.GetTxGroup()
   171  	if err != nil {
   172  		t.Error(err)
   173  		return
   174  	}
   175  	if !group.CheckSign() {
   176  		t.Error("group: sign should be no err")
   177  	}
   178  	//txs[0] 的hash 应该和 整体的hash相同
   179  	if string(tx.Hash()) != string(group.Txs[0].Hash()) {
   180  		t.Error("group: tx.Hash ==  group.Txs[0].Hash()")
   181  	}
   182  	for i := 0; i < len(group.Txs); i++ {
   183  		//t.Log(group.Txs[i].Json())
   184  		if group.Txs[i].IsExpire(cfg, 10, Now().Unix()) {
   185  			t.Error("group txs[i]: Expire not set so, no exprie forever")
   186  		}
   187  		if !group.Txs[i].CheckSign() {
   188  			t.Error("group txs[i]: sign should be no err")
   189  		}
   190  	}
   191  	if group.IsExpire(cfg, 10, Now().Unix()) {
   192  		t.Error("group: Expire not set so, no exprie forever")
   193  	}
   194  }
   195  
   196  func getprivkey(key string) crypto.PrivKey {
   197  	cr, err := crypto.New(GetSignName("", SECP256K1))
   198  	if err != nil {
   199  		panic(err)
   200  	}
   201  	bkey, err := common.FromHex(key)
   202  	if err != nil {
   203  		panic(err)
   204  	}
   205  	priv, err := cr.PrivKeyFromBytes(bkey)
   206  	if err != nil {
   207  		panic(err)
   208  	}
   209  	return priv
   210  }
   211  
   212  func TestSignGroupTx(t *testing.T) {
   213  	var err error
   214  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
   215  	privkey := getprivkey("CC38546E9E659D15E6B4893F0AB32A06D103931A8230B0BDE71459D2B27D6944")
   216  	unsignTx := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520e0a71230f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b53667640034a8b030a8f010a05636f696e73120e18010a0a1080c2d72f1a036f746520e0a71230f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b53667640034a20a9ef5454033a9ab080360470291e9b1f63881ff9a03c3c09a06e7200688d019852209a56c5dbff8b246e32d3ad0534f42dbbae88cf4b0bed24fe6420e06d59187c690a8b010a05636f696e73120e18010a0a1080c2d72f1a036f746530de92c3828ad194b26d3a22313271796f6361794e46374c7636433971573461767873324537553431664b53667640034a20a9ef5454033a9ab080360470291e9b1f63881ff9a03c3c09a06e7200688d0198522036bb9ca17aeef20b6e9afcd5ba52d89f5109db5b5d2aee200723b2bb0c7e9aa30a690a05636f696e73120e18010a0a1080c2d72f1a036f746530b0d6c895c4d28efe5d3a22313271796f6361794e46374c7636433971573461767873324537553431664b53667640034a20a9ef5454033a9ab080360470291e9b1f63881ff9a03c3c09a06e7200688d019852209a56c5dbff8b246e32d3ad0534f42dbbae88cf4b0bed24fe6420e06d59187c69"
   217  	var tx Transaction
   218  	txhex, _ := hex.DecodeString(unsignTx)
   219  	Decode(txhex, &tx)
   220  	group, err := tx.GetTxGroup()
   221  	if err != nil {
   222  		t.Error(err)
   223  		return
   224  	}
   225  	if group == nil {
   226  		t.Errorf("signN sign a not group tx")
   227  		return
   228  	}
   229  	for i := 0; i < len(group.GetTxs()); i++ {
   230  		err = group.SignN(i, SECP256K1, privkey)
   231  		if err != nil {
   232  			t.Error(err)
   233  			return
   234  		}
   235  	}
   236  	err = group.Check(cfg, 0, cfg.GetMinTxFeeRate(), cfg.GetMaxTxFee())
   237  	if err != nil {
   238  		t.Error(err)
   239  		return
   240  	}
   241  	newtx := group.Tx()
   242  	signedtx := hex.EncodeToString(Encode(newtx))
   243  	t.Log(signedtx)
   244  }
   245  
   246  func BenchmarkTxHash(b *testing.B) {
   247  	tx1 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   248  	tx11, _ := hex.DecodeString(tx1)
   249  	var tx12 Transaction
   250  	Decode(tx11, &tx12)
   251  	for i := 0; i < b.N; i++ {
   252  		tx12.Hash()
   253  	}
   254  }
   255  
   256  func TestParseExpire(t *testing.T) {
   257  	expire := ""
   258  	_, err := ParseExpire(expire)
   259  	assert.Equal(t, ErrInvalidParam, err)
   260  
   261  	expire = "H:123"
   262  	exp, _ := ParseExpire(expire)
   263  	assert.Equal(t, int64(4611686018427396727), exp)
   264  
   265  	expire = "H:-2"
   266  	_, err = ParseExpire(expire)
   267  	assert.Equal(t, ErrHeightLessZero, err)
   268  
   269  	expire = "123"
   270  	exp, err = ParseExpire(expire)
   271  	assert.Nil(t, err)
   272  	assert.Equal(t, int64(123), exp)
   273  
   274  	expire = "123s"
   275  	exp, err = ParseExpire(expire)
   276  	assert.Nil(t, err)
   277  	assert.Equal(t, int64(123000000000), exp)
   278  
   279  }
   280  
   281  func BenchmarkHash(b *testing.B) {
   282  	tx := &Transaction{Payload: []byte("xxxxxxxxxxxxdggrgrgrgrgrgrgrrhthththhth"), Execer: []byte("hello")}
   283  	for i := 0; i < b.N; i++ {
   284  		tx.Hash()
   285  	}
   286  }
   287  
   288  func TestSetGroupExpire(t *testing.T) {
   289  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
   290  	rawtx := "0a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6720c0843d30aab4d59684b5cce7143a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4ab50c0aa3010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6720c0843d30aab4d59684b5cce7143a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522008217c413b035fddd8f34a303e90a29e661746ed9b23a97768c1f25817c2c3450a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a673094fbcabe96c99ea7163a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f552203c6a2b11cce466891f084b49450472b1d4c39213f63117d3d4ce2a3851304ebc0a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730c187fb80fe88ce9e3c3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522066419d70492f757d7285fd226dff62da8d803c8121ded95242d222dbb10f2d9b0a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a673098aa929ab292b3f0023a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f552202bab08051d24fe923f66c8aeea4ce3f425d47a72f7c5c230a2b1427e04e2eb510a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730bfe9abb3edc6d9cb163a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f55220e1ba0493aa431ea3071026bd8dfa8280efab53ce86441fc474a1c19550a554ba0a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730d2e196a8ecada9d53e3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522016600fbfa23b3f0e8f9a14b716ce8f4064c091fbf6fa94489bc9d14b5b6049a60a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730a0b7b1b1dda2f4c5743a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522089d0442d76713369022499d054db65ccacbf5c627a525bd5454e0a30d23fa2990a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730c5838f94e2f49acb4b3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522018f208938606b390d752898332a84a9fbb900c2ed55ec33cd54d09b1970043b90a9f010a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a67308dfddb82faf7dfc4113a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522013002bab7a9c65881bd937a6fded4c3959bb631fa84434572970c1ec3e6fccf90a7d0a0a757365722e7772697465121d236d642368616b6468676f7177656a6872676f716a676f6a71776c6a6730b8b082d799a4ddc93a3a2231444e615344524739524431397335396d65416f654e34613246365248393766536f400a4a201f533ac07c3fc4c716f65cdb0f1f02e7f5371b5164277210dafb1dbdd4a5f4f5522008217c413b035fddd8f34a303e90a29e661746ed9b23a97768c1f25817c2c345"
   291  	var tx Transaction
   292  	txhex, _ := hex.DecodeString(rawtx)
   293  	Decode(txhex, &tx)
   294  	group, err := tx.GetTxGroup()
   295  	if err != nil {
   296  		t.Error(err)
   297  		return
   298  	}
   299  	for _, tmptx := range group.GetTxs() {
   300  		if tmptx.GetExpire() != 0 {
   301  			t.Error("TestSetGroupExpire Expire !=0", "tx", tmptx)
   302  		}
   303  	}
   304  
   305  	//设置交易组过期时间
   306  	for i := 0; i < len(group.Txs); i++ {
   307  		group.SetExpire(cfg, i, time.Duration(120))
   308  	}
   309  	group.RebuiltGroup()
   310  
   311  	//校验重组后的交易组
   312  	firsttxhash := group.GetTxs()[0].Hash()
   313  	for _, tmptx := range group.GetTxs() {
   314  		if string(tmptx.GetHeader()) != string(firsttxhash) {
   315  			t.Error("TestSetGroupExpire group: tx.Hash !=  group.Txs[0].Hash()")
   316  		}
   317  	}
   318  
   319  	for _, tmptx := range group.GetTxs() {
   320  		if tmptx.GetExpire() == 0 {
   321  			t.Error("TestSetGroupExpire Expire == 0", "tx", tmptx)
   322  		}
   323  	}
   324  }
   325  
   326  func TestSortTxList(t *testing.T) {
   327  	cfg := NewTuringchainConfig(GetDefaultCfgstring())
   328  
   329  	tx1 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   330  	tx2 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630de92c3828ad194b26d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   331  	tx3 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630b0d6c895c4d28efe5d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   332  	tx11, _ := hex.DecodeString(tx1)
   333  	tx21, _ := hex.DecodeString(tx2)
   334  	tx31, _ := hex.DecodeString(tx3)
   335  
   336  	var txList Transactions
   337  	var tx12 Transaction
   338  	Decode(tx11, &tx12)
   339  	var tx22 Transaction
   340  	Decode(tx21, &tx22)
   341  	var tx32 Transaction
   342  	Decode(tx31, &tx32)
   343  
   344  	//构建三笔单个交易并添加到交易列表中
   345  	tx12.Execer = []byte("hashlock")
   346  	tx22.Execer = []byte("voken")
   347  	tx32.Execer = []byte("coins")
   348  	txList.Txs = append(txList.Txs, &tx12)
   349  	txList.Txs = append(txList.Txs, &tx22)
   350  	txList.Txs = append(txList.Txs, &tx32)
   351  
   352  	//构建主链的交易组并添加到交易列表中
   353  	tx111, tx221, tx321 := modifyTxExec(tx12, tx22, tx32, "paracross", "game", "guess")
   354  	group, err := CreateTxGroup([]*Transaction{&tx111, &tx221, &tx321}, cfg.GetMinTxFeeRate())
   355  	if err != nil {
   356  		t.Error(err)
   357  		return
   358  	}
   359  	groupTx := group.Tx()
   360  	txGroup, err := groupTx.GetTxGroup()
   361  	if err != nil {
   362  		t.Error(err)
   363  		return
   364  	}
   365  	txList.Txs = append(txList.Txs, txGroup.GetTxs()...)
   366  
   367  	//构建三笔不同平行链的单笔交易
   368  	tx1111, tx2211, tx3211 := modifyTxExec(tx12, tx22, tx32, "user.p.test.js", "user.p.para.lottery", "user.p.fuzamei.norm")
   369  
   370  	txList.Txs = append(txList.Txs, &tx1111)
   371  	txList.Txs = append(txList.Txs, &tx2211)
   372  	txList.Txs = append(txList.Txs, &tx3211)
   373  
   374  	//构建user.p.test.平行链的交易组并添加到交易列表中
   375  	tx1112, tx2212, tx3212 := modifyTxExec(tx12, tx22, tx32, "user.p.test.evm", "user.p.test.relay", "user.p.test.ticket")
   376  	group, err = CreateTxGroup([]*Transaction{&tx1112, &tx2212, &tx3212}, cfg.GetMinTxFeeRate())
   377  	if err != nil {
   378  		t.Error(err)
   379  		return
   380  	}
   381  	groupTx = group.Tx()
   382  	txGroup, err = groupTx.GetTxGroup()
   383  	if err != nil {
   384  		t.Error(err)
   385  		return
   386  	}
   387  	txList.Txs = append(txList.Txs, txGroup.GetTxs()...)
   388  
   389  	//构建user.p.para.平行链的交易组并添加到交易列表中
   390  	tx1113, tx2213, tx3213 := modifyTxExec(tx12, tx22, tx32, "user.p.para.coins", "user.p.para.paracross", "user.p.para.pokerbull")
   391  
   392  	group, err = CreateTxGroup([]*Transaction{&tx1113, &tx2213, &tx3213}, cfg.GetMinTxFeeRate())
   393  	if err != nil {
   394  		t.Error(err)
   395  		return
   396  	}
   397  	groupTx = group.Tx()
   398  	txGroup, err = groupTx.GetTxGroup()
   399  	if err != nil {
   400  		t.Error(err)
   401  		return
   402  	}
   403  	txList.Txs = append(txList.Txs, txGroup.GetTxs()...)
   404  
   405  	//构建user.p.fuzamei.平行链的交易组并添加到交易列表中
   406  	tx1114, tx2214, tx3214 := modifyTxExec(tx12, tx22, tx32, "user.p.fuzamei.norm", "user.p.fuzamei.coins", "user.p.fuzamei.retrieve")
   407  
   408  	group, err = CreateTxGroup([]*Transaction{&tx1114, &tx2214, &tx3214}, cfg.GetMinTxFeeRate())
   409  	if err != nil {
   410  		t.Error(err)
   411  		return
   412  	}
   413  	groupTx = group.Tx()
   414  	txGroup, err = groupTx.GetTxGroup()
   415  	if err != nil {
   416  		t.Error(err)
   417  		return
   418  	}
   419  	txList.Txs = append(txList.Txs, txGroup.GetTxs()...)
   420  
   421  	//构造一些主链交易的合约名排序在user后面的交易
   422  	tx1115, tx2215, tx3215 := modifyTxExec(tx12, tx22, tx32, "varacross", "wame", "zuess")
   423  	txList.Txs = append(txList.Txs, &tx1115)
   424  	txList.Txs = append(txList.Txs, &tx2215)
   425  	txList.Txs = append(txList.Txs, &tx3215)
   426  
   427  	//交易只分类不排序,保证子链内部交易的顺序不变
   428  	sorTxList := TransactionSort(txList.Txs)
   429  	assert.Equal(t, len(txList.Txs), len(sorTxList))
   430  
   431  	for _, tx := range txList.Txs {
   432  		var equal bool
   433  		txHash := tx.Hash()
   434  		for _, sorttx := range sorTxList {
   435  			sortHash := sorttx.Hash()
   436  			if bytes.Equal(sortHash, txHash) {
   437  				equal = true
   438  				break
   439  			}
   440  		}
   441  		assert.Equal(t, equal, true)
   442  	}
   443  	//校验每个子链中交易的顺序没有变化
   444  	//计算期望的子主链交易的子roothash
   445  	var prevmainhashes [][]byte
   446  	prevmainhashes = append(prevmainhashes, tx12.Hash(), tx22.Hash(), tx32.Hash(), tx111.Hash(), tx221.Hash(), tx321.Hash(), tx1115.Hash(), tx2215.Hash(), tx3215.Hash())
   447  
   448  	var prevfuzameihashes [][]byte
   449  	prevfuzameihashes = append(prevfuzameihashes, tx3211.Hash(), tx1114.Hash(), tx2214.Hash(), tx3214.Hash())
   450  
   451  	var prevparahashes [][]byte
   452  	prevparahashes = append(prevparahashes, tx2211.Hash(), tx1113.Hash(), tx2213.Hash(), tx3213.Hash())
   453  
   454  	var prevtesthashes [][]byte
   455  	prevtesthashes = append(prevtesthashes, tx1111.Hash(), tx1112.Hash(), tx2212.Hash(), tx3212.Hash())
   456  
   457  	// 校验分类之后各个子链中交易的顺序是否是我们期望的
   458  	for j, sorttx := range sorTxList {
   459  		if j < 9 {
   460  			assert.Equal(t, prevmainhashes[j], sorttx.Hash())
   461  		} else if j >= 9 && j <= 12 {
   462  			assert.Equal(t, prevfuzameihashes[j-9], sorttx.Hash())
   463  		} else if j >= 13 && j <= 16 {
   464  			assert.Equal(t, prevparahashes[j-13], sorttx.Hash())
   465  		} else {
   466  			assert.Equal(t, prevtesthashes[j-17], sorttx.Hash())
   467  		}
   468  	}
   469  	//构建只有主链交易
   470  	var txSingleList Transactions
   471  	tx51111, tx52211, tx53211 := modifyTxExec(tx12, tx22, tx32, "coins", "token", "hashlock")
   472  	txSingleList.Txs = append(txSingleList.Txs, &tx51111)
   473  	txSingleList.Txs = append(txSingleList.Txs, &tx52211)
   474  	txSingleList.Txs = append(txSingleList.Txs, &tx53211)
   475  
   476  	tx61111, tx62211, tx63211 := modifyTxExec(tx12, tx22, tx32, "ajs", "zottery", "norm")
   477  	txSingleList.Txs = append(txSingleList.Txs, &tx61111)
   478  	txSingleList.Txs = append(txSingleList.Txs, &tx62211)
   479  	txSingleList.Txs = append(txSingleList.Txs, &tx63211)
   480  
   481  	sorTxSingleList := TransactionSort(txSingleList.Txs)
   482  	assert.Equal(t, len(txSingleList.Txs), len(sorTxSingleList))
   483  	//分类前后交易的顺序一致
   484  	for i, tx := range txSingleList.Txs {
   485  		assert.Equal(t, tx.Hash(), sorTxSingleList[i].Hash())
   486  	}
   487  }
   488  
   489  func modifyTxExec(tx1, tx2, tx3 Transaction, tx1exec, tx2exec, tx3exec string) (Transaction, Transaction, Transaction) {
   490  	tx11 := tx1
   491  	tx12 := tx2
   492  	tx13 := tx3
   493  
   494  	tx11.Execer = []byte(tx1exec)
   495  	tx12.Execer = []byte(tx2exec)
   496  	tx13.Execer = []byte(tx3exec)
   497  
   498  	return tx11, tx12, tx13
   499  }