Skip to content

Commit

Permalink
Refactor (#29)
Browse files Browse the repository at this point in the history
* Refactor:
1. Fixed minor formatting issues and typos;
2. Upgraded to Go 1.21 using the built-in min function.
3. Upgraded dependencies.
4. Upgraded GitHub Action pipelines, adding Codacy.
5. Added package comments.

Signed-off-by: txaty <[email protected]>
  • Loading branch information
txaty authored Nov 21, 2023
1 parent 3ddbc81 commit e399a58
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 52 deletions.
36 changes: 18 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,27 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version-file: './go.mod'
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: './go.mod'

- name: Build
run: make build
- name: Build
run: make build

- name: Test
run: make test_race
- name: Test
run: make test_race

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3.2.0
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3

- name: Run coverage
run: make test_ci_coverage
- name: Run coverage
run: make test_ci_coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
# - name: Codacy Analysis CLI
# uses: codacy/codacy-analysis-cli-action@v4.1.0
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3

- name: Codacy Analysis CLI
uses: codacy/codacy-analysis-cli-action@master
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/txaty/go-merkletree.svg)](https://pkg.go.dev/github.com/txaty/go-merkletree)
[![Go Report Card](https://goreportcard.com/badge/github.com/txaty/go-merkletree)](https://goreportcard.com/report/github.com/txaty/go-merkletree)
[![codecov](https://codecov.io/gh/txaty/go-merkletree/branch/main/graph/badge.svg?token=M02CIBSXFR)](https://codecov.io/gh/txaty/go-merkletree)
[![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)

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

Expand Down Expand Up @@ -53,14 +54,14 @@ DisableLeafHashing bool
To define a new Hash function:

```go
func NewHashFunc(data []byte) ([]byte, error) {
func myHashFunc(data []byte) ([]byte, error) {
sha256Func := sha256.New()
sha256Func.Write(data)
return sha256Func.Sum(nil), nil
}
```

> **Important Notice:** please make sure the hash function used by paralleled algorithms is concurrent-safe.
> **Important Notice:** please make sure the hash functions used by paralleled algorithms are __concurrent-safe__.
## Example

Expand Down Expand Up @@ -166,16 +167,14 @@ handleError(err)

Setup:

| CPU | Memory | OS | Hash Function |
|----------------|--------|--------------|---------------|
| Intel i7-9750H | 16GB | Ubuntu 20.04 | SHA256 |
| CPU | Memory | OS | Hash Function |
|:--------------:|:------:|:------------:|:-------------:|
| Intel i7-9750H | 16GB | Ubuntu 20.04 | SHA256 |

Two tasks were performed:
Benchmark tasks:

- Proof generation for all the blocks: at the end we can obtain the Merkle Root and the proofs of all the data blocks.
- Proof verification: verify a single proof.

Benchmark implementation can be found in [txaty/merkle-tree-bench](https://github.com/txaty/merkle-tree-bench).
1. Proof generation for all the blocks: at the end we can obtain the Merkle Root and the proofs of all the data blocks.
2. Proof verification: verify a single proof.

<table>
<tbody>
Expand All @@ -190,20 +189,23 @@ Benchmark implementation can be found in [txaty/merkle-tree-bench](https://githu
</td></tr>
</tbody></table>

> **_Note:_** The size of each data block is determined by the tree depth, which is represented on the x-axis of the
> figures. The y-axis is shown using a logarithmic scale to better visualize the range of values. Please note that the
> real time difference between the data points will be larger than what is visualized on the figure due to the
> logarithmic
> scale.
> **_Note:_** Please note that the size of each data block is determined by the tree depth,
> which is represented on the x-axis of the figures.
> In order to better visualize the full range of values, the y-axis is shown using a logarithmic scale.
> However, it's important to keep in mind that the real time difference between data points
> will be larger than what is depicted on the figure due to the logarithmic scale.
Benchmark implementation can be found in [txaty/merkle-tree-bench](https://github.com/txaty/merkle-tree-bench).

## Dependencies

This project requires the following dependencies:

- [gool](https://github.com/txaty/gool) - a generic goroutine pool. Please make sure your Golang version supports
generics.
- [gomonkey](https://github.com/agiledragon/gomonkey) - a Go library for monkey patching in unit tests. It may have
permission-denied issues on Apple Silicon MacBooks. But it will not affect the use of the Merkle Tree library.
- [gool](https://github.com/txaty/gool) - a generics goroutine pool. Before running the code, make sure that your Golang
version supports generics.
- [gomonkey](https://github.com/agiledragon/gomonkey) - a Go library that allows you to monkey patch in unit tests.
Please note that this library may have permission-denied issues on Apple Silicon MacBooks. However, this will not
affect the use of the Merkle Tree library.

## License

Expand Down
3 changes: 3 additions & 0 deletions default_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Package merkletree implements a high-performance Merkle Tree in Go.
// It supports parallel execution for enhanced performance and
// offers compatibility with OpenZeppelin through sorted sibling pairs.
package merkletree

import "crypto/sha256"
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module github.com/txaty/go-merkletree

go 1.19
go 1.21

require (
github.com/agiledragon/gomonkey/v2 v2.9.0
github.com/txaty/gool v0.1.4
github.com/agiledragon/gomonkey/v2 v2.11.0
github.com/txaty/gool v0.1.5
)
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
github.com/agiledragon/gomonkey/v2 v2.9.0 h1:PDiKKybR596O6FHW+RVSG0Z7uGCBNbmbUXh3uCNQ7Hc=
github.com/agiledragon/gomonkey/v2 v2.9.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/agiledragon/gomonkey/v2 v2.10.1 h1:FPJJNykD1957cZlGhr9X0zjr291/lbazoZ/dmc4mS4c=
github.com/agiledragon/gomonkey/v2 v2.10.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/agiledragon/gomonkey/v2 v2.11.0 h1:5oxSgA+tC1xuGsrIorR+sYiziYltmJyEZ9qA25b6l5U=
github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/txaty/gool v0.1.4 h1:3NwHLjdNsbITl3aqII8n8d4NYvOQE+3qt7cTLkeEAgM=
github.com/txaty/gool v0.1.4/go.mod h1:zhUnrAMYUZXRYBq6dTofbCUn8OgA3OOKCFMeqGV2mu0=
github.com/txaty/gool v0.1.5 h1:yjxie86J1kBBAAsP/xa2K4j1HJoB90RvjDyzuMjlK8k=
github.com/txaty/gool v0.1.5/go.mod h1:zhUnrAMYUZXRYBq6dTofbCUn8OgA3OOKCFMeqGV2mu0=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
10 changes: 3 additions & 7 deletions merkle_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Package merkletree implements a high-performance Merkle Tree in Go.
// It supports parallel execution for enhanced performance and
// offers compatibility with OpenZeppelin through sorted sibling pairs.
package merkletree

import (
Expand Down Expand Up @@ -483,13 +486,6 @@ func (m *MerkleTree) updateProofPairs(buffer [][]byte, idx, batch, step int) {
}
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

// generateLeaves generates the leaves slice from the data blocks.
func (m *MerkleTree) generateLeaves(blocks []DataBlock) ([][]byte, error) {
var (
Expand Down
3 changes: 2 additions & 1 deletion merkle_tree_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIT License
//
// Copyright (c) 2022 Tommy TIAN
// Copyright (c) 2023 Tommy TIAN
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -32,6 +32,7 @@ import (
"testing"

"github.com/agiledragon/gomonkey/v2"

"github.com/txaty/go-merkletree/mock"
)

Expand Down

0 comments on commit e399a58

Please sign in to comment.