Skip to content

Commit ecde1c1

Browse files
authored
Refactor: benchmark, readme, and unit tests (#33)
* Update benchmark and README info. Signed-off-by: txaty <[email protected]> * Fix unit test issues. Signed-off-by: txaty <[email protected]> --------- Signed-off-by: txaty <[email protected]>
1 parent 7b8c313 commit ecde1c1

7 files changed

+32
-10
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[![Go Report Card](https://goreportcard.com/badge/github.com/txaty/go-merkletree)](https://goreportcard.com/report/github.com/txaty/go-merkletree)
55
[![codecov](https://codecov.io/gh/txaty/go-merkletree/branch/main/graph/badge.svg?token=M02CIBSXFR)](https://codecov.io/gh/txaty/go-merkletree)
66
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/3a9bb5ff5cb64dcf83903ca998a9144d)](https://app.codacy.com/gh/txaty/go-merkletree/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
7+
[![MIT license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](https://opensource.org/licenses/MIT)
78

89
High performance Golang Merkle Tree, supporting parallelization and OpenZeppelin sibling-sorting.
910

@@ -167,9 +168,9 @@ handleError(err)
167168

168169
Setup:
169170

170-
| CPU | Memory | OS | Hash Function |
171-
|:--------------:|:------:|:------------:|:-------------:|
172-
| Intel i7-9750H | 16GB | Ubuntu 20.04 | SHA256 |
171+
| AWS EC2 | CPU | Memory | OS | Hash Function | Go Version |
172+
|:----------:|:-------:|:------:|:----------------:|:-------------:|:----------:|
173+
| c5.4xlarge | 16 Core | 32GB | Ubuntu 22.04 LTS | SHA256 | 1.21.4 |
173174

174175
Benchmark tasks:
175176

@@ -180,7 +181,7 @@ Benchmark tasks:
180181
<tbody>
181182
<tr><td>
182183

183-
![Proof Generation](asset/proof_gen.png)
184+
![Proof Generation All](asset/proof_gen_all.png)
184185

185186
</td><td>
186187

@@ -201,12 +202,11 @@ Benchmark implementation can be found in [txaty/merkle-tree-bench](https://githu
201202

202203
This project requires the following dependencies:
203204

204-
- [gool](https://github.com/txaty/gool) - a generics goroutine pool. Before running the code, make sure that your Golang
205-
version supports generics.
205+
- [golang.org/x/sync](golang.org/x/sync) - `errgroup` in this package is used to handle errors from goroutines.
206206
- [gomonkey](https://github.com/agiledragon/gomonkey) - a Go library that allows you to monkey patch in unit tests.
207207
Please note that this library may have permission-denied issues on Apple Silicon MacBooks. However, this will not
208208
affect the use of the Merkle Tree library.
209209

210210
## License
211211

212-
MIT License
212+
Released under the [MIT License](https://github.com/txaty/go-merkletree/blob/master/LICENSE).

asset/proof_gen.png

-123 KB
Binary file not shown.

asset/proof_gen_all.png

122 KB
Loading

asset/proof_verification.png

2.47 KB
Loading

merkle_tree_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
func mockDataBlocks(num int) []DataBlock {
3535
blocks := make([]DataBlock, num)
3636
for i := 0; i < num; i++ {
37-
byteLen := rand.Intn(1 << 15)
37+
byteLen := max(32, rand.Intn(1<<15))
3838
block := &mock.DataBlock{
3939
Data: make([]byte, byteLen),
4040
}

proof_gen_and_tree_build_test.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"crypto/sha256"
2727
"fmt"
2828
"reflect"
29+
"sync/atomic"
2930
"testing"
3031
)
3132

@@ -147,6 +148,7 @@ func TestMerkleTreeNew_modeProofGenAndTreeBuild(t *testing.T) {
147148
}
148149

149150
func TestMerkleTreeNew_modeProofGenAndTreeBuildParallel(t *testing.T) {
151+
var hashFuncCounter atomic.Uint32
150152
type args struct {
151153
blocks []DataBlock
152154
config *Config
@@ -221,12 +223,13 @@ func TestMerkleTreeNew_modeProofGenAndTreeBuildParallel(t *testing.T) {
221223
{
222224
name: "test_tree_build_hash_func_error",
223225
args: args{
224-
blocks: mockDataBlocks(100),
226+
blocks: mockDataBlocks(5),
225227
config: &Config{
226228
HashFunc: func(block []byte) ([]byte, error) {
227-
if len(block) == 64 {
229+
if hashFuncCounter.Load() >= 5 {
228230
return nil, fmt.Errorf("hash func error")
229231
}
232+
hashFuncCounter.Add(1)
230233
sha256Func := sha256.New()
231234
sha256Func.Write(block)
232235
return sha256Func.Sum(nil), nil

tree_build_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,24 @@ func TestMerkleTreeNew_modeTreeBuildParallel(t *testing.T) {
276276
},
277277
wantErr: true,
278278
},
279+
{
280+
name: "test_hash_func_error_when_computing_nodes_parallel",
281+
args: args{
282+
blocks: mockDataBlocks(4),
283+
config: &Config{
284+
HashFunc: func(block []byte) ([]byte, error) {
285+
if hashFuncCounter.Load() == 5 {
286+
return nil, fmt.Errorf("hash func error")
287+
}
288+
hashFuncCounter.Add(1)
289+
sha256Func := sha256.New()
290+
sha256Func.Write(block)
291+
return sha256Func.Sum(nil), nil
292+
},
293+
},
294+
},
295+
wantErr: true,
296+
},
279297
{
280298
name: "test_hash_func_error_when_computing_root_parallel",
281299
args: args{
@@ -299,6 +317,7 @@ func TestMerkleTreeNew_modeTreeBuildParallel(t *testing.T) {
299317
}
300318
for _, tt := range tests {
301319
t.Run(tt.name, func(t *testing.T) {
320+
hashFuncCounter.Store(0)
302321
m, err := New(tt.args.config, tt.args.blocks)
303322
if (err != nil) != tt.wantErr {
304323
t.Errorf("Build() error = %v, wantErr %v", err, tt.wantErr)

0 commit comments

Comments
 (0)