github.com/turingchain2020/turingchain@v1.1.21/common/merkle/merkle_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 merkle
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/sha256"
    10  	"encoding/hex"
    11  	"fmt"
    12  	"testing"
    13  
    14  	"github.com/turingchain2020/turingchain/common"
    15  	"github.com/turingchain2020/turingchain/types"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  //测试两个交易的roothash以及branch.获取bitcoin的99997 block作为验证
    20  // "height": 99997,
    21  //  "merkleroot": "5140e5972f672bf8e81bc189894c55a410723b095716eaeec845490aed785f0e",
    22  //  "tx": [
    23  //    "b86f5ef1da8ddbdb29ec269b535810ee61289eeac7bf2b2523b494551f03897c",
    24  //    "80c6f121c3e9fe0a59177e49874d8c703cbadee0700a782e4002e87d862373c6"
    25  
    26  func Test_TwoTxMerkle(t *testing.T) {
    27  	RootHash := "5140e5972f672bf8e81bc189894c55a410723b095716eaeec845490aed785f0e"
    28  
    29  	tx0string := "b86f5ef1da8ddbdb29ec269b535810ee61289eeac7bf2b2523b494551f03897c"
    30  	tx1string := "80c6f121c3e9fe0a59177e49874d8c703cbadee0700a782e4002e87d862373c6"
    31  
    32  	t.Logf("Test_TwoTxMerkle bitcoin roothash :%s", RootHash)
    33  
    34  	rootHash, err := NewHashFromStr(RootHash)
    35  	if err != nil {
    36  		t.Errorf("NewHashFromStr RootHash err:%s", err.Error())
    37  	}
    38  	rootHashbyte := rootHash.CloneBytes()
    39  
    40  	tx0hash, err := NewHashFromStr(tx0string)
    41  	if err != nil {
    42  		t.Errorf("NewHashFromStr tx0string err:%s", err.Error())
    43  	}
    44  	tx0byte := tx0hash.CloneBytes()
    45  
    46  	tx1hash, err := NewHashFromStr(tx1string)
    47  	if err != nil {
    48  		t.Errorf("NewHashFromStr tx1string err:%s", err.Error())
    49  	}
    50  	tx1byte := tx1hash.CloneBytes()
    51  
    52  	leaves := make([][]byte, 2)
    53  
    54  	leaves[0] = tx0byte
    55  	leaves[1] = tx1byte
    56  
    57  	bitroothash := GetMerkleRoot(leaves)
    58  
    59  	bitroothashstr, err := NewHash(bitroothash)
    60  	if err == nil {
    61  		t.Logf("Test_TwoTxMerkle GetMerkleRoot roothash :%s", bitroothashstr.String())
    62  	}
    63  	if !bytes.Equal(rootHashbyte, bitroothash) {
    64  		t.Errorf("Test_TwoTxMerkle  rootHashbyte :%v,GetMerkleRoot :%v", rootHashbyte, bitroothash)
    65  		return
    66  	}
    67  
    68  	for txindex := 0; txindex < 2; txindex++ {
    69  		roothashd, branchs := GetMerkleRootAndBranch(leaves, uint32(txindex))
    70  
    71  		brroothashstr, err := NewHash(roothashd)
    72  		if err == nil {
    73  			t.Logf("Test_TwoTxMerkle GetMerkleRootAndBranch roothash :%s", brroothashstr.String())
    74  		}
    75  
    76  		brroothash := GetMerkleRootFromBranch(branchs, leaves[txindex], uint32(txindex))
    77  		if bytes.Equal(bitroothash, brroothash) && bytes.Equal(rootHashbyte, brroothash) {
    78  			brRoothashstr, err := NewHash(brroothash)
    79  			if err == nil {
    80  				t.Logf("Test_TwoTxMerkle GetMerkleRootFromBranch roothash :%s", brRoothashstr.String())
    81  			}
    82  			t.Logf("Test_TwoTxMerkle bitroothash == brroothash :%d", txindex)
    83  		}
    84  	}
    85  }
    86  
    87  //测试三个交易的roothash以及branch
    88  //"height": 99960,
    89  //  "merkleroot": "34d5a57822efa653019edfee29b9586a0d0d807572275b45f39a7e9c25614bf9",
    90  //  "tx": [
    91  //    "f89c65bdcd695e4acc621256085f20d7c093097e04a1ce34b606a5829cbaf2c6",
    92  //    "1818bef9c6aeed09de0ed999b5f2868b3555084437e1c63f29d5f37b69bb214f",
    93  //    "d43a40a2db5bad2bd176c27911ed86d97bff734425953b19c8cf77910b21020d"
    94  func Test_OddTxMerkle(t *testing.T) {
    95  
    96  	RootHash := "34d5a57822efa653019edfee29b9586a0d0d807572275b45f39a7e9c25614bf9"
    97  
    98  	tx0string := "f89c65bdcd695e4acc621256085f20d7c093097e04a1ce34b606a5829cbaf2c6"
    99  	tx1string := "1818bef9c6aeed09de0ed999b5f2868b3555084437e1c63f29d5f37b69bb214f"
   100  	tx2string := "d43a40a2db5bad2bd176c27911ed86d97bff734425953b19c8cf77910b21020d"
   101  
   102  	t.Logf("Test_OddTxMerkle bitcoin roothash :%s", RootHash)
   103  
   104  	rootHash, err := NewHashFromStr(RootHash)
   105  	if err != nil {
   106  		t.Errorf("NewHashFromStr RootHash err:%s", err.Error())
   107  	}
   108  	rootHashbyte := rootHash.CloneBytes()
   109  
   110  	tx0hash, err := NewHashFromStr(tx0string)
   111  	if err != nil {
   112  		t.Errorf("NewHashFromStr tx0string err:%s", err.Error())
   113  	}
   114  	tx0byte := tx0hash.CloneBytes()
   115  
   116  	tx1hash, err := NewHashFromStr(tx1string)
   117  	if err != nil {
   118  		t.Errorf("NewHashFromStr tx1string err:%s", err.Error())
   119  	}
   120  	tx1byte := tx1hash.CloneBytes()
   121  
   122  	tx2hash, err := NewHashFromStr(tx2string)
   123  	if err != nil {
   124  		t.Errorf("NewHashFromStr tx2string err:%s", err.Error())
   125  	}
   126  	tx2byte := tx2hash.CloneBytes()
   127  
   128  	leaves := make([][]byte, 3)
   129  
   130  	leaves[0] = tx0byte
   131  	leaves[1] = tx1byte
   132  	leaves[2] = tx2byte
   133  
   134  	bitroothash := GetMerkleRoot(leaves)
   135  
   136  	bitroothashstr, err := NewHash(bitroothash)
   137  	if err == nil {
   138  		t.Logf("Test_OddTxMerkle GetMerkleRoot roothash :%s", bitroothashstr.String())
   139  	}
   140  	if !bytes.Equal(rootHashbyte, bitroothash) {
   141  		t.Errorf("Test_OddTxMerkle  rootHashbyte :%v,GetMerkleRoot :%v", rootHashbyte, bitroothash)
   142  		return
   143  	}
   144  
   145  	for txindex := 0; txindex < 3; txindex++ {
   146  		branchs := GetMerkleBranch(leaves, uint32(txindex))
   147  
   148  		brroothash := GetMerkleRootFromBranch(branchs, leaves[txindex], uint32(txindex))
   149  		if bytes.Equal(bitroothash, brroothash) && bytes.Equal(rootHashbyte, brroothash) {
   150  			brRoothashstr, err := NewHash(brroothash)
   151  			if err == nil {
   152  				t.Logf("Test_OddTxMerkle GetMerkleRootFromBranch roothash :%s", brRoothashstr.String())
   153  			}
   154  			t.Logf("Test_OddTxMerkle bitroothash == brroothash :%d", txindex)
   155  		}
   156  	}
   157  }
   158  
   159  //测试六个交易的roothash以及branch
   160  //		"height": 99974,
   161  //  	"merkleroot": "272066470ccf8ee2bb6f48f263c5b2ffc56813be40001c4b33fec0677d69e3cc",
   162  //  	"tx": [
   163  //    "dc5d3f45eeffaab3a71e9930c80a34f5da7ee4cdbc6e7270e1c6d3397f835836",
   164  //    "0ed98b4429bfbf78cfb1e87b9c16cc6641df0a8b2df861c13dc987785912cc48",
   165  //    "8108da17be5960b009de33b60fcdfd5856abbd8b7c543afde31b0601b6e2aeea",
   166  //    "ecffa48ff29b13b295c25d97987f4899ed4f607691c316b0fad685fa1ab268d9",
   167  //    "b57f79efed1d0e999495a34840aa7b41e15b43627225849d83effe56152df4e7",
   168  //    "8ded96ae2a609555df2390faa2f001b04e6e0ba8a60c69f9b432c9637157d766"
   169  
   170  func Test_SixTxMerkle(t *testing.T) {
   171  	RootHash := "272066470ccf8ee2bb6f48f263c5b2ffc56813be40001c4b33fec0677d69e3cc"
   172  
   173  	tx0string := "dc5d3f45eeffaab3a71e9930c80a34f5da7ee4cdbc6e7270e1c6d3397f835836"
   174  	tx1string := "0ed98b4429bfbf78cfb1e87b9c16cc6641df0a8b2df861c13dc987785912cc48"
   175  	tx2string := "8108da17be5960b009de33b60fcdfd5856abbd8b7c543afde31b0601b6e2aeea"
   176  	tx3string := "ecffa48ff29b13b295c25d97987f4899ed4f607691c316b0fad685fa1ab268d9"
   177  	tx4string := "b57f79efed1d0e999495a34840aa7b41e15b43627225849d83effe56152df4e7"
   178  	tx5string := "8ded96ae2a609555df2390faa2f001b04e6e0ba8a60c69f9b432c9637157d766"
   179  
   180  	t.Logf("Test_SixTxMerkle bitcoin roothash :%s", RootHash)
   181  
   182  	rootHash, err := NewHashFromStr(RootHash)
   183  	if err != nil {
   184  		t.Errorf("NewHashFromStr RootHash err:%s", err.Error())
   185  	}
   186  	rootHashbyte := rootHash.CloneBytes()
   187  
   188  	tx0hash, err := NewHashFromStr(tx0string)
   189  	if err != nil {
   190  		t.Errorf("NewHashFromStr tx0string err:%s", err.Error())
   191  	}
   192  	tx0byte := tx0hash.CloneBytes()
   193  
   194  	tx1hash, err := NewHashFromStr(tx1string)
   195  	if err != nil {
   196  		t.Errorf("NewHashFromStr tx1string err:%s", err.Error())
   197  	}
   198  	tx1byte := tx1hash.CloneBytes()
   199  
   200  	tx2hash, err := NewHashFromStr(tx2string)
   201  	if err != nil {
   202  		t.Errorf("NewHashFromStr tx2string err:%s", err.Error())
   203  	}
   204  	tx2byte := tx2hash.CloneBytes()
   205  
   206  	tx3hash, err := NewHashFromStr(tx3string)
   207  	if err != nil {
   208  		t.Errorf("NewHashFromStr tx3string err:%s", err.Error())
   209  	}
   210  	tx3byte := tx3hash.CloneBytes()
   211  
   212  	tx4hash, err := NewHashFromStr(tx4string)
   213  	if err != nil {
   214  		t.Errorf("NewHashFromStr tx4string err:%s", err.Error())
   215  	}
   216  	tx4byte := tx4hash.CloneBytes()
   217  
   218  	tx5hash, err := NewHashFromStr(tx5string)
   219  	if err != nil {
   220  		t.Errorf("NewHashFromStr tx5string err:%s", err.Error())
   221  	}
   222  	tx5byte := tx5hash.CloneBytes()
   223  
   224  	leaves := make([][]byte, 6)
   225  
   226  	leaves[0] = tx0byte
   227  	leaves[1] = tx1byte
   228  	leaves[2] = tx2byte
   229  	leaves[3] = tx3byte
   230  
   231  	leaves[4] = tx4byte
   232  	leaves[5] = tx5byte
   233  
   234  	bitroothash := GetMerkleRoot(leaves)
   235  
   236  	bitroothashstr, err := NewHash(bitroothash)
   237  	if err == nil {
   238  		t.Logf("Test_SixTxMerkle GetMerkleRoot roothash :%s", bitroothashstr.String())
   239  	}
   240  	if !bytes.Equal(rootHashbyte, bitroothash) {
   241  		t.Errorf("Test_SixTxMerkle  rootHashbyte :%v,GetMerkleRoot :%v", rootHashbyte, bitroothash)
   242  		return
   243  	}
   244  
   245  	for txindex := 0; txindex < 6; txindex++ {
   246  		branchs := GetMerkleBranch(leaves, uint32(txindex))
   247  
   248  		brroothash := GetMerkleRootFromBranch(branchs, leaves[txindex], uint32(txindex))
   249  		if bytes.Equal(bitroothash, brroothash) && bytes.Equal(rootHashbyte, brroothash) {
   250  			brRoothashstr, err := NewHash(brroothash)
   251  			if err == nil {
   252  				t.Logf("Test_SixTxMerkle GetMerkleRootFromBranch roothash :%s", brRoothashstr.String())
   253  			}
   254  			t.Logf("Test_SixTxMerkle bitroothash == brroothash :%d", txindex)
   255  		}
   256  	}
   257  }
   258  
   259  func BenchmarkHashTwo(b *testing.B) {
   260  	b.ReportAllocs()
   261  	left := common.GetRandBytes(32, 32)
   262  	right := common.GetRandBytes(32, 32)
   263  	for i := 0; i < b.N; i++ {
   264  		getHashFromTwoHash(left, right)
   265  	}
   266  }
   267  
   268  func BenchmarkHashTwo2(b *testing.B) {
   269  	b.ReportAllocs()
   270  	left := common.GetRandBytes(32, 32)
   271  	right := common.GetRandBytes(32, 32)
   272  	cache := make([]byte, 64)
   273  	for i := 0; i < b.N; i++ {
   274  		GetHashFromTwoHash(cache, left, right)
   275  	}
   276  }
   277  
   278  //原来的版本更快,这个方案只是做一个性能测试的对比
   279  func getHashFromTwoHash(left []byte, right []byte) []byte {
   280  	if left == nil || right == nil {
   281  		return nil
   282  	}
   283  	h := sha256.New()
   284  	h.Write(left)
   285  	h.Write(right)
   286  	hash1 := h.Sum(nil)
   287  	h.Reset()
   288  	h.Write(hash1)
   289  	return h.Sum(nil)
   290  }
   291  
   292  //优化办法:
   293  //1. 减少内存分配
   294  //2. 改进算法
   295  
   296  var benchlen = 100000
   297  
   298  func BenchmarkGetMerkelRoot(b *testing.B) {
   299  	b.ReportAllocs()
   300  	var hashlist [][]byte
   301  	for i := 0; i < benchlen; i++ {
   302  		key := common.GetRandBytes(32, 32)
   303  		hashlist = append(hashlist, key)
   304  	}
   305  	var prevroot []byte
   306  	for i := 0; i < b.N; i++ {
   307  		calc := make([][]byte, len(hashlist))
   308  		copy(calc, hashlist)
   309  		newroot := GetMerkleRoot(calc)
   310  		if prevroot != nil && !bytes.Equal(prevroot, newroot) {
   311  			b.Error("root is not the same")
   312  		}
   313  		prevroot = newroot
   314  	}
   315  }
   316  
   317  func BenchmarkGetMerkelRoot2(b *testing.B) {
   318  	b.ReportAllocs()
   319  	var hashlist [][]byte
   320  	for i := 0; i < benchlen; i++ {
   321  		key := common.GetRandBytes(32, 32)
   322  		hashlist = append(hashlist, key)
   323  	}
   324  	var prevroot []byte
   325  	for i := 0; i < b.N; i++ {
   326  		calc := make([][]byte, len(hashlist))
   327  		copy(calc, hashlist)
   328  		newroot, _, _ := Computation(calc, 1, 0)
   329  		if prevroot != nil && !bytes.Equal(prevroot, newroot) {
   330  			b.Error("root is not the same")
   331  		}
   332  		prevroot = newroot
   333  	}
   334  }
   335  
   336  func TestGetMerkelRoot1(t *testing.T) {
   337  	for i := 0; i < 2000; i++ {
   338  		ok := testGetMerkelRoot1(t, i)
   339  		if !ok {
   340  			t.Error("calc merkel root error", i)
   341  			return
   342  		}
   343  	}
   344  }
   345  
   346  func testGetMerkelRoot1(t *testing.T, testlen int) bool {
   347  	var hashlist [][]byte
   348  	for i := 0; i < testlen; i++ {
   349  		key := sha256.Sum256([]byte(fmt.Sprint(i)))
   350  		hashlist = append(hashlist, key[:])
   351  	}
   352  	hash1 := GetMerkleRoot(hashlist)
   353  
   354  	hashlist = nil
   355  	for i := 0; i < testlen; i++ {
   356  		key := sha256.Sum256([]byte(fmt.Sprint(i)))
   357  		hashlist = append(hashlist, key[:])
   358  	}
   359  	hash2 := getMerkleRoot(hashlist)
   360  	if !bytes.Equal(hash1, hash2) {
   361  		println("failed1")
   362  		return false
   363  	}
   364  
   365  	hashlist = nil
   366  	for i := 0; i < testlen; i++ {
   367  		key := sha256.Sum256([]byte(fmt.Sprint(i)))
   368  		hashlist = append(hashlist, key[:])
   369  	}
   370  	hash3, _, _ := Computation(hashlist, 1, 0)
   371  	if !bytes.Equal(hash1, hash3) {
   372  		println("failed2")
   373  		return false
   374  	}
   375  	return true
   376  }
   377  
   378  func TestLog2(t *testing.T) {
   379  	assert.Equal(t, log2(0), 0)
   380  	assert.Equal(t, log2(1), 1)
   381  	assert.Equal(t, log2(2), 1)
   382  	assert.Equal(t, log2(3), 1)
   383  	assert.Equal(t, log2(4), 2)
   384  	assert.Equal(t, log2(5), 2)
   385  	assert.Equal(t, log2(6), 2)
   386  	assert.Equal(t, log2(7), 2)
   387  	assert.Equal(t, log2(8), 3)
   388  	assert.Equal(t, log2(256), 8)
   389  }
   390  
   391  func TestCalcMainMerkleRoot(t *testing.T) {
   392  
   393  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
   394  
   395  	tx1 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630f1cdebc8f7efa5e9283a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   396  	tx2 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630de92c3828ad194b26d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   397  	tx3 := "0a05636f696e73120e18010a0a1080c2d72f1a036f746520a08d0630b0d6c895c4d28efe5d3a22313271796f6361794e46374c7636433971573461767873324537553431664b536676"
   398  	tx11, _ := hex.DecodeString(tx1)
   399  	tx21, _ := hex.DecodeString(tx2)
   400  	tx31, _ := hex.DecodeString(tx3)
   401  
   402  	var txList types.Transactions
   403  	var tx12 types.Transaction
   404  	types.Decode(tx11, &tx12)
   405  	var tx22 types.Transaction
   406  	types.Decode(tx21, &tx22)
   407  	var tx32 types.Transaction
   408  	types.Decode(tx31, &tx32)
   409  
   410  	//构建三笔单个交易并添加到交易列表中
   411  	tx12.Execer = []byte("hashlock")
   412  	tx22.Execer = []byte("voken")
   413  	tx32.Execer = []byte("coins")
   414  	txList.Txs = append(txList.Txs, &tx12)
   415  	txList.Txs = append(txList.Txs, &tx22)
   416  	txList.Txs = append(txList.Txs, &tx32)
   417  
   418  	//构建主链的交易组并添加到交易列表中
   419  	tx111, tx221, tx321 := modifyTxExec(tx12, tx22, tx32, "paracross", "game", "guess")
   420  	group, err := types.CreateTxGroup([]*types.Transaction{&tx111, &tx221, &tx321}, 1000000)
   421  	if err != nil {
   422  		t.Error(err)
   423  		return
   424  	}
   425  	groupTx := group.Tx()
   426  	txGroup, err := groupTx.GetTxGroup()
   427  	if err != nil {
   428  		t.Error(err)
   429  		return
   430  	}
   431  	txList.Txs = append(txList.Txs, txGroup.GetTxs()...)
   432  
   433  	//构建三笔不同平行链的单笔交易
   434  	tx1111, tx2211, tx3211 := modifyTxExec(tx12, tx22, tx32, "user.p.test.js", "user.p.para.lottery", "user.p.fuzamei.norm")
   435  
   436  	txList.Txs = append(txList.Txs, &tx1111)
   437  	txList.Txs = append(txList.Txs, &tx2211)
   438  	txList.Txs = append(txList.Txs, &tx3211)
   439  
   440  	//构建user.p.test.平行链的交易组并添加到交易列表中
   441  	tx1112, tx2212, tx3212 := modifyTxExec(tx12, tx22, tx32, "user.p.test.evm", "user.p.test.relay", "user.p.test.ticket")
   442  	group, err = types.CreateTxGroup([]*types.Transaction{&tx1112, &tx2212, &tx3212}, 1000000)
   443  	if err != nil {
   444  		t.Error(err)
   445  		return
   446  	}
   447  	groupTx = group.Tx()
   448  	txGroup, err = groupTx.GetTxGroup()
   449  	if err != nil {
   450  		t.Error(err)
   451  		return
   452  	}
   453  	txList.Txs = append(txList.Txs, txGroup.GetTxs()...)
   454  
   455  	//构建user.p.para.平行链的交易组并添加到交易列表中
   456  	tx1113, tx2213, tx3213 := modifyTxExec(tx12, tx22, tx32, "user.p.para.coins", "user.p.para.paracross", "user.p.para.pokerbull")
   457  
   458  	group, err = types.CreateTxGroup([]*types.Transaction{&tx1113, &tx2213, &tx3213}, 1000000)
   459  	if err != nil {
   460  		t.Error(err)
   461  		return
   462  	}
   463  	groupTx = group.Tx()
   464  	txGroup, err = groupTx.GetTxGroup()
   465  	if err != nil {
   466  		t.Error(err)
   467  		return
   468  	}
   469  	txList.Txs = append(txList.Txs, txGroup.GetTxs()...)
   470  
   471  	//构建user.p.fuzamei.平行链的交易组并添加到交易列表中
   472  	tx1114, tx2214, tx3214 := modifyTxExec(tx12, tx22, tx32, "user.p.fuzamei.norm", "user.p.fuzamei.coins", "user.p.fuzamei.retrieve")
   473  
   474  	group, err = types.CreateTxGroup([]*types.Transaction{&tx1114, &tx2214, &tx3214}, 1000000)
   475  	if err != nil {
   476  		t.Error(err)
   477  		return
   478  	}
   479  	groupTx = group.Tx()
   480  	txGroup, err = groupTx.GetTxGroup()
   481  	if err != nil {
   482  		t.Error(err)
   483  		return
   484  	}
   485  	txList.Txs = append(txList.Txs, txGroup.GetTxs()...)
   486  
   487  	//构造一些主链交易的合约名排序在user后面的交易
   488  	tx1115, tx2215, tx3215 := modifyTxExec(tx12, tx22, tx32, "varacross", "wame", "zuess")
   489  	txList.Txs = append(txList.Txs, &tx1115)
   490  	txList.Txs = append(txList.Txs, &tx2215)
   491  	txList.Txs = append(txList.Txs, &tx3215)
   492  
   493  	sorTxList := types.TransactionSort(txList.Txs)
   494  
   495  	assert.Equal(t, len(txList.Txs), len(sorTxList))
   496  
   497  	for _, sorttx := range sorTxList {
   498  		var equal bool
   499  		sortHash := sorttx.Hash()
   500  		for _, tx := range txList.Txs {
   501  			txHash := tx.Hash()
   502  			if bytes.Equal(sortHash, txHash) {
   503  				equal = true
   504  				break
   505  			}
   506  		}
   507  		assert.Equal(t, equal, true)
   508  	}
   509  
   510  	var mixChainHashes [][]byte
   511  
   512  	//主链子roothash[0-9]
   513  	oldMixMainHash := calcSingleLayerMerkleRoot(sorTxList[0:9])
   514  	mixChainHashes = append(mixChainHashes, oldMixMainHash)
   515  
   516  	// fuzamei平行链的子roothash[9-13]
   517  	oldMixTuringHash := calcSingleLayerMerkleRoot(sorTxList[9:13])
   518  	mixChainHashes = append(mixChainHashes, oldMixTuringHash)
   519  
   520  	// para平行链的子roothash
   521  	oldMixParaHash := calcSingleLayerMerkleRoot(sorTxList[13:17])
   522  	mixChainHashes = append(mixChainHashes, oldMixParaHash)
   523  
   524  	// test平行链的子roothash
   525  	oldMixTestHash := calcSingleLayerMerkleRoot(sorTxList[17:21])
   526  	mixChainHashes = append(mixChainHashes, oldMixTestHash)
   527  
   528  	oldMixChainHash := GetMerkleRoot(mixChainHashes)
   529  
   530  	newMixChainHash, childMixChainHash := CalcMultiLayerMerkleInfo(cfg, 1, sorTxList)
   531  
   532  	assert.Equal(t, newMixChainHash, oldMixChainHash)
   533  
   534  	assert.Equal(t, len(childMixChainHash), 4)
   535  	//主链子roothash的检测
   536  	assert.Equal(t, childMixChainHash[0].ChildHash, oldMixMainHash)
   537  	assert.Equal(t, childMixChainHash[0].StartIndex, int32(0))
   538  	assert.Equal(t, childMixChainHash[0].Title, types.MainChainName)
   539  	assert.Equal(t, childMixChainHash[0].GetTxCount(), int32(9))
   540  	//fuzamei平行链子roothash的检测
   541  	assert.Equal(t, childMixChainHash[1].ChildHash, oldMixTuringHash)
   542  	assert.Equal(t, childMixChainHash[1].StartIndex, int32(9))
   543  	assert.Equal(t, childMixChainHash[1].Title, "user.p.fuzamei.")
   544  	assert.Equal(t, childMixChainHash[1].GetTxCount(), int32(4))
   545  	//para平行链子roothash的检测
   546  	assert.Equal(t, childMixChainHash[2].ChildHash, oldMixParaHash)
   547  	assert.Equal(t, childMixChainHash[2].StartIndex, int32(13))
   548  	assert.Equal(t, childMixChainHash[2].Title, "user.p.para.")
   549  	assert.Equal(t, childMixChainHash[2].GetTxCount(), int32(4))
   550  	//test平行链子roothash的检测
   551  	assert.Equal(t, childMixChainHash[3].ChildHash, oldMixTestHash)
   552  	assert.Equal(t, childMixChainHash[3].StartIndex, int32(17))
   553  	assert.Equal(t, childMixChainHash[3].Title, "user.p.test.")
   554  	assert.Equal(t, childMixChainHash[3].GetTxCount(), int32(4))
   555  
   556  	var leaves [][]byte
   557  	for _, childChain := range childMixChainHash {
   558  		leaves = append(leaves, childChain.ChildHash)
   559  	}
   560  
   561  	//获取主链子roothash的MerkleBranch
   562  	for index, childChain := range childMixChainHash {
   563  		childChainBranch := GetMerkleBranch(leaves, uint32(index))
   564  		rootHash := GetMerkleRootFromBranch(childChainBranch, childChain.ChildHash, uint32(index))
   565  		assert.Equal(t, rootHash, newMixChainHash)
   566  	}
   567  
   568  	//构建全是主链的交易列表
   569  	var txMainList types.Transactions
   570  	tx51111, tx52211, tx53211 := modifyTxExec(tx12, tx22, tx32, "user.write", "coins", "ticket")
   571  	txMainList.Txs = append(txMainList.Txs, &tx51111)
   572  	txMainList.Txs = append(txMainList.Txs, &tx52211)
   573  	txMainList.Txs = append(txMainList.Txs, &tx53211)
   574  
   575  	tx61111, tx62211, tx63211 := modifyTxExec(tx12, tx22, tx32, "ajs", "zottery", "norm")
   576  	group, err = types.CreateTxGroup([]*types.Transaction{&tx61111, &tx62211, &tx63211}, 1000000)
   577  	if err != nil {
   578  		t.Error(err)
   579  		return
   580  	}
   581  	groupTx = group.Tx()
   582  	txGroup, err = groupTx.GetTxGroup()
   583  	if err != nil {
   584  		t.Error(err)
   585  		return
   586  	}
   587  	txMainList.Txs = append(txMainList.Txs, txGroup.GetTxs()...)
   588  
   589  	sorTxMainList := types.TransactionSort(txMainList.Txs)
   590  
   591  	newrootHash, childHash := CalcMultiLayerMerkleInfo(cfg, 1, sorTxMainList)
   592  	oldrootHash := calcSingleLayerMerkleRoot(sorTxMainList)
   593  	assert.Equal(t, newrootHash, oldrootHash)
   594  	assert.Equal(t, childHash[0].ChildHash, oldrootHash)
   595  	assert.Equal(t, childHash[0].StartIndex, int32(0))
   596  	assert.Equal(t, childHash[0].Title, types.MainChainName)
   597  	assert.Equal(t, childHash[0].GetTxCount(), int32(6))
   598  
   599  	roothashTem := CalcMerkleRoot(cfg, 1, sorTxMainList)
   600  	assert.Equal(t, roothashTem, oldrootHash)
   601  	//构建全是同一个平行链的交易列表
   602  	var txParaTestList types.Transactions
   603  	tx71111, tx72211, tx73211 := modifyTxExec(tx12, tx22, tx32, "user.p.test.js", "user.p.test.lottery", "user.p.test.norm")
   604  	txParaTestList.Txs = append(txParaTestList.Txs, &tx71111)
   605  	txParaTestList.Txs = append(txParaTestList.Txs, &tx72211)
   606  	txParaTestList.Txs = append(txParaTestList.Txs, &tx73211)
   607  
   608  	tx81111, tx82211, tx83211 := modifyTxExec(tx12, tx22, tx32, "user.p.test.coins", "user.p.test.token", "user.p.test.none")
   609  
   610  	group, err = types.CreateTxGroup([]*types.Transaction{&tx81111, &tx82211, &tx83211}, 1000000)
   611  	if err != nil {
   612  		t.Error(err)
   613  		return
   614  	}
   615  	groupTx = group.Tx()
   616  	txGroup, err = groupTx.GetTxGroup()
   617  	if err != nil {
   618  		t.Error(err)
   619  		return
   620  	}
   621  	txParaTestList.Txs = append(txParaTestList.Txs, txGroup.GetTxs()...)
   622  
   623  	sorTxParaTestList := types.TransactionSort(txParaTestList.Txs)
   624  
   625  	newPararootHash, childParaHash := CalcMultiLayerMerkleInfo(cfg, 1, sorTxParaTestList)
   626  	oldPararootHash := calcSingleLayerMerkleRoot(sorTxParaTestList)
   627  	assert.Equal(t, newPararootHash, oldPararootHash)
   628  
   629  	assert.Equal(t, childParaHash[0].ChildHash, oldPararootHash)
   630  	assert.Equal(t, childParaHash[0].StartIndex, int32(0))
   631  	assert.Equal(t, childParaHash[0].Title, "user.p.test.")
   632  	assert.Equal(t, childParaHash[0].GetTxCount(), int32(6))
   633  
   634  	//构造一个主链和一个平行链的交易列表
   635  	var txMPList types.Transactions
   636  	tx91111, tx92211, tx93211 := modifyTxExec(tx12, tx22, tx32, "coins", "user.p.test.lottery", "token")
   637  	txMPList.Txs = append(txMPList.Txs, &tx91111)
   638  	txMPList.Txs = append(txMPList.Txs, &tx92211)
   639  	txMPList.Txs = append(txMPList.Txs, &tx93211)
   640  
   641  	sorTxMPList := types.TransactionSort(txMPList.Txs)
   642  
   643  	var hashes [][]byte
   644  	oldMrootHash := calcSingleLayerMerkleRoot(sorTxMPList[0:2])
   645  	oldProotHash := calcSingleLayerMerkleRoot(sorTxMPList[2:])
   646  	hashes = append(hashes, oldMrootHash)
   647  	hashes = append(hashes, oldProotHash)
   648  	oldMProotHash := GetMerkleRoot(hashes)
   649  
   650  	newMProotHash, childMainParaHash := CalcMultiLayerMerkleInfo(cfg, 1, sorTxMPList)
   651  
   652  	assert.Equal(t, newMProotHash, oldMProotHash)
   653  
   654  	assert.Equal(t, len(childMainParaHash), 2)
   655  	assert.Equal(t, childMainParaHash[0].ChildHash, oldMrootHash)
   656  	assert.Equal(t, childMainParaHash[0].StartIndex, int32(0))
   657  	assert.Equal(t, childMainParaHash[0].Title, types.MainChainName)
   658  	assert.Equal(t, childMainParaHash[0].GetTxCount(), int32(2))
   659  
   660  	assert.Equal(t, childMainParaHash[1].ChildHash, oldProotHash)
   661  	assert.Equal(t, childMainParaHash[1].StartIndex, int32(2))
   662  	assert.Equal(t, childMainParaHash[1].Title, "user.p.test.")
   663  	assert.Equal(t, childMainParaHash[1].GetTxCount(), int32(1))
   664  
   665  	//构造一个主链和三个平行链的交易列表
   666  	var txMThreePList types.Transactions
   667  	tx111111, tx112211, tx113211 := modifyTxExec(tx12, tx22, tx32, "coins", "user.p.test.none", "user.p.para.oracle")
   668  	txMThreePList.Txs = append(txMThreePList.Txs, &tx111111)
   669  	txMThreePList.Txs = append(txMThreePList.Txs, &tx112211)
   670  	txMThreePList.Txs = append(txMThreePList.Txs, &tx113211)
   671  
   672  	tx211111, tx212211, tx213211 := modifyTxExec(tx12, tx22, tx32, "valnode", "user.p.test.lottery", "user.p.para.relay")
   673  	txMThreePList.Txs = append(txMThreePList.Txs, &tx211111)
   674  	txMThreePList.Txs = append(txMThreePList.Txs, &tx212211)
   675  	txMThreePList.Txs = append(txMThreePList.Txs, &tx213211)
   676  
   677  	tx311111, tx312211, tx313211 := modifyTxExec(tx12, tx22, tx32, "unfreeze", "user.p.test.hashlock", "user.p.para.echo")
   678  	txMThreePList.Txs = append(txMThreePList.Txs, &tx311111)
   679  	txMThreePList.Txs = append(txMThreePList.Txs, &tx312211)
   680  	txMThreePList.Txs = append(txMThreePList.Txs, &tx313211)
   681  
   682  	sorTxMThreePList := types.TransactionSort(txMThreePList.Txs)
   683  
   684  	var mThreePhashes [][]byte
   685  
   686  	//主链的三笔交易的子roothash
   687  	mainRootHash := calcSingleLayerMerkleRoot(sorTxMThreePList[0:3])
   688  	mThreePhashes = append(mThreePhashes, mainRootHash)
   689  
   690  	//para平行链的三笔交易的子roothash
   691  	paraRootHash := calcSingleLayerMerkleRoot(sorTxMThreePList[3:6])
   692  	mThreePhashes = append(mThreePhashes, paraRootHash)
   693  
   694  	//test平行链的三笔交易的子roothash
   695  	testRootHash := calcSingleLayerMerkleRoot(sorTxMThreePList[6:])
   696  	mThreePhashes = append(mThreePhashes, testRootHash)
   697  
   698  	oldMThreeProotHash := GetMerkleRoot(mThreePhashes)
   699  
   700  	newMThreeProotHash, childMThreePHash := CalcMultiLayerMerkleInfo(cfg, 1, sorTxMThreePList)
   701  
   702  	tempRootHash := CalcMerkleRoot(cfg, 1, sorTxMThreePList)
   703  	assert.Equal(t, newMThreeProotHash, tempRootHash)
   704  
   705  	assert.Equal(t, newMThreeProotHash, oldMThreeProotHash)
   706  
   707  	assert.Equal(t, len(childMThreePHash), 3)
   708  	assert.Equal(t, childMThreePHash[0].ChildHash, mainRootHash)
   709  	assert.Equal(t, childMThreePHash[0].StartIndex, int32(0))
   710  	assert.Equal(t, childMThreePHash[0].Title, types.MainChainName)
   711  	assert.Equal(t, childMThreePHash[0].GetTxCount(), int32(3))
   712  
   713  	assert.Equal(t, childMThreePHash[1].ChildHash, paraRootHash)
   714  	assert.Equal(t, childMThreePHash[1].StartIndex, int32(3))
   715  	assert.Equal(t, childMThreePHash[1].Title, "user.p.para.")
   716  	assert.Equal(t, childMThreePHash[1].GetTxCount(), int32(3))
   717  
   718  	assert.Equal(t, childMThreePHash[2].ChildHash, testRootHash)
   719  	assert.Equal(t, childMThreePHash[2].StartIndex, int32(6))
   720  	assert.Equal(t, childMThreePHash[2].Title, "user.p.test.")
   721  	assert.Equal(t, childMThreePHash[2].GetTxCount(), int32(3))
   722  
   723  	roothash1, childHash1 := CalcMultiLayerMerkleInfo(cfg, 0, sorTxMThreePList)
   724  	assert.Nil(t, roothash1)
   725  	assert.Nil(t, childHash1)
   726  }
   727  
   728  func modifyTxExec(tx1, tx2, tx3 types.Transaction, tx1exec, tx2exec, tx3exec string) (types.Transaction, types.Transaction, types.Transaction) {
   729  	tx11 := tx1
   730  	tx12 := tx2
   731  	tx13 := tx3
   732  
   733  	tx11.Execer = []byte(tx1exec)
   734  	tx12.Execer = []byte(tx2exec)
   735  	tx13.Execer = []byte(tx3exec)
   736  
   737  	return tx11, tx12, tx13
   738  }