diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6d476a4..3bed93d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/README.md b/README.md index 13a7d2a..ad7c11e 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 @@ -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. @@ -190,20 +189,23 @@ Benchmark implementation can be found in [txaty/merkle-tree-bench](https://githu
-> **_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 diff --git a/default_hash.go b/default_hash.go index 86fe058..d12556a 100644 --- a/default_hash.go +++ b/default_hash.go @@ -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" diff --git a/go.mod b/go.mod index cf486fc..6ef2cbc 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 65e1fc3..2798d88 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/merkle_tree.go b/merkle_tree.go index c3e002d..d9ba4b2 100644 --- a/merkle_tree.go +++ b/merkle_tree.go @@ -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 ( @@ -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 ( diff --git a/merkle_tree_test.go b/merkle_tree_test.go index bb85f44..677bddb 100644 --- a/merkle_tree_test.go +++ b/merkle_tree_test.go @@ -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 @@ -32,6 +32,7 @@ import ( "testing" "github.com/agiledragon/gomonkey/v2" + "github.com/txaty/go-merkletree/mock" )