github.com/aergoio/aergo@v1.3.1/internal/merkle/merkle_test.go (about)

     1  package merkle
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/minio/sha256-simd"
     7  	"github.com/mr-tron/base58/base58"
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"encoding/base64"
    11  	"encoding/binary"
    12  	"hash"
    13  	"testing"
    14  )
    15  
    16  var (
    17  	tms []MerkleEntry
    18  )
    19  
    20  func EncodeB64(bs []byte) string {
    21  	return base64.StdEncoding.EncodeToString(bs)
    22  }
    23  
    24  func EncodeB58(bs []byte) string {
    25  	return base58.Encode(bs)
    26  }
    27  
    28  func beforeTest(count int) error {
    29  	tms = make([]MerkleEntry, count)
    30  
    31  	h := sha256.New()
    32  	for i := 0; i < count; i++ {
    33  		tms[i] = genMerkleEntry(h, i)
    34  	}
    35  
    36  	return nil
    37  }
    38  
    39  func afterTest() {
    40  
    41  }
    42  
    43  type testME struct {
    44  	hash []byte
    45  }
    46  
    47  func (me *testME) GetHash() []byte {
    48  	return me.hash
    49  }
    50  
    51  func genMerkleEntry(h hash.Hash, i int) MerkleEntry {
    52  	tm := testME{}
    53  
    54  	h.Reset()
    55  	binary.Write(h, binary.LittleEndian, i)
    56  	tm.hash = h.Sum(nil)
    57  
    58  	return &tm
    59  }
    60  
    61  func TestMerkle0Tx(t *testing.T) {
    62  	t.Log("TestMerkle1Tx")
    63  
    64  	beforeTest(0)
    65  
    66  	merkles := CalculateMerkleTree(tms)
    67  
    68  	nilHash := make([]byte, 32)
    69  
    70  	assert.Equal(t, len(merkles), 1)
    71  	assert.Equal(t, len(merkles[0]), 32)
    72  
    73  	assert.Equal(t, bytes.Compare(merkles[0], nilHash), 0)
    74  }
    75  
    76  func TestMerkle1Tx(t *testing.T) {
    77  	t.Log("TestMerkle1Tx")
    78  	beforeTest(1)
    79  
    80  	t.Logf("lentxs %d", len(tms))
    81  	merkles := CalculateMerkleTree(tms)
    82  
    83  	assert.Equal(t, len(merkles), 1)
    84  	assert.Equal(t, 0, bytes.Compare(merkles[0], tms[0].GetHash()))
    85  }
    86  
    87  func TestMerkle2Tx(t *testing.T) {
    88  	t.Log("TestMerkle2Tx")
    89  	beforeTest(2)
    90  
    91  	merkles := CalculateMerkleTree(tms)
    92  
    93  	totalCount := 3
    94  	assert.Equal(t, len(merkles), totalCount)
    95  	for i, tm := range tms {
    96  		assert.True(t, bytes.Equal(merkles[i], tm.GetHash()))
    97  	}
    98  
    99  	assert.NotNil(t, merkles[2])
   100  	assert.NotEqual(t, merkles[0], merkles[2])
   101  
   102  	for i, merkle := range merkles {
   103  		assert.Equal(t, len(merkle), 32)
   104  		t.Logf("%d:%v", i, EncodeB64(merkle))
   105  	}
   106  }
   107  
   108  func TestMerkle3Tx(t *testing.T) {
   109  	t.Log("TestMerkle3Tx")
   110  	beforeTest(3)
   111  
   112  	t.Logf("lentxs %d", len(tms))
   113  	merkles := CalculateMerkleTree(tms)
   114  
   115  	totalCount := 7
   116  	assert.Equal(t, len(merkles), totalCount)
   117  
   118  	for i, tm := range tms {
   119  		assert.True(t, bytes.Equal(merkles[i], tm.GetHash()))
   120  	}
   121  
   122  	assert.True(t, bytes.Equal(merkles[2], merkles[3]))
   123  
   124  	for i, merkle := range merkles {
   125  		assert.NotNil(t, merkle, "nil=%d", i)
   126  		assert.Equal(t, len(merkle), 32)
   127  		t.Logf("%d:%v", i, EncodeB64(merkle))
   128  	}
   129  }
   130  
   131  func TestMerkle32Tx(t *testing.T) {
   132  	t.Log("TestMerkle32Tx")
   133  	beforeTest(10)
   134  
   135  	t.Logf("lentxs %d", len(tms))
   136  	merkles := CalculateMerkleTree(tms)
   137  
   138  	//totalCount := 10240
   139  	totalCount := 31
   140  	assert.Equal(t, len(merkles), totalCount)
   141  
   142  	for i, tm := range tms {
   143  		assert.True(t, bytes.Equal(merkles[i], tm.GetHash()))
   144  	}
   145  
   146  	//copy lc to rc
   147  	// 21, 27
   148  	assert.True(t, bytes.Equal(merkles[20], merkles[21]))
   149  	assert.True(t, bytes.Equal(merkles[26], merkles[27]))
   150  
   151  	for i := 10; i <= 15; i++ {
   152  		assert.Nil(t, merkles[i])
   153  	}
   154  	assert.Nil(t, merkles[22])
   155  	assert.Nil(t, merkles[23])
   156  
   157  	for i, merkle := range merkles {
   158  		if merkle == nil {
   159  			t.Logf("node:%d is nil", i)
   160  			continue
   161  		}
   162  		assert.Equal(t, len(merkle), 32)
   163  		//t.Logf("%d:%v", i, EncodeB64(merkle))
   164  	}
   165  
   166  	merkleRoot := merkles[len(merkles)-1]
   167  	assert.NotNil(t, merkleRoot)
   168  }
   169  
   170  func BenchmarkMerkle10000Tx(b *testing.B) {
   171  	b.Log("BenchmarkMerkle10000Tx")
   172  	beforeTest(10000)
   173  	b.Logf("lentxs %d", len(tms))
   174  
   175  	b.ResetTimer()
   176  
   177  	for i := 0; i < b.N; i++ {
   178  		CalculateMerkleTree(tms)
   179  	}
   180  }