Skip to content

Commit

Permalink
Refactor (txaty#30)
Browse files Browse the repository at this point in the history
Refactor:
1. Removed goroutine pool (gool). Using goroutine pool causes overhead and increases the complexity of the code base.
2. Changed the proof generation method. Now proofGen parallelized algorithm doesn't require two buffers.
3. Added fuzzing test.
4. More unit tests were added to achieve 100% coverage.
5. Refactored many methods and functions.
6. Extracted methods and functions to separate files to increase readability.

Signed-off-by: txaty <[email protected]>
  • Loading branch information
txaty authored Nov 23, 2023
1 parent e399a58 commit 3b445a0
Show file tree
Hide file tree
Showing 21 changed files with 2,460 additions and 1,742 deletions.
85 changes: 81 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,114 @@
name: Go Build
name: Go Build, Test, and Analyze Pipeline

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
GO_VERSION: 1.21

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: './go.mod'
go-version: ${{ env.GO_VERSION }}

- name: Cache Go modules
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Build
run: make build

- name: Test
run: make test_race
test:
name: Unit Test
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Unit Test with Mocking
run: make test_with_mock

lint:
name: Linting with golangci-lint
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

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

coverage:
name: Coverage and Codecov Upload
needs: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Run coverage
run: make test_ci_coverage

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
if: success()

analysis:
name: Codacy Analysis
needs: coverage
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Codacy Analysis CLI
uses: codacy/codacy-analysis-cli-action@master
if: success()

fuzz:
name: Fuzzing Test
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Run Fuzzing Tests
run: make test_fuzz
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ coverage.*
# Dependency directories (remove the comment below to include it)
vendor/

# Go fuzzing data
testdata/

# IDEs
.idea/
.vscode/
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test test_race test_with_mock test_ci_coverage format bench report_bench cpu_report mem_report build
.PHONY: test test_race test_with_mock test_fuzz test_ci_coverage format bench report_bench cpu_report mem_report build

COVER_OUT := coverage.out
COVER_HTML := coverage.html
Expand All @@ -10,6 +10,9 @@ test_with_mock: COVER_OPTS = -race -gcflags=all=-l -covermode atomic
test test_race test_with_mock:
go test -v $(COVER_OPTS) -coverprofile=$(COVER_OUT) && go tool cover -html=$(COVER_OUT) -o $(COVER_HTML) && go tool cover -func=$(COVER_OUT) -o $(COVER_OUT)

test_fuzz:
go test -v -race -fuzz=FuzzMerkleTreeNew -fuzztime=30m -run ^FuzzMerkleTreeNew$

test_ci_coverage:
go test -race -gcflags=all=-l -coverprofile=coverage.txt -covermode=atomic

Expand Down
35 changes: 35 additions & 0 deletions data_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// MIT License
//
// 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
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// 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

// DataBlock is the interface for input data blocks used to generate the Merkle Tree.
// Implementations of DataBlock should provide a serialization method
// that converts the data block into a byte slice for hashing purposes.
type DataBlock interface {
// Serialize converts the data block into a byte slice.
// It returns the serialized byte slice and an error, if any occurs during the serialization process.
Serialize() ([]byte, error)
}
44 changes: 44 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// MIT License
//
// 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
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// 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 "errors"

var (
// ErrInvalidNumOfDataBlocks is the error for an invalid number of data blocks.
ErrInvalidNumOfDataBlocks = errors.New("the number of data blocks must be greater than 1")
// ErrInvalidConfigMode is the error for an invalid configuration mode.
ErrInvalidConfigMode = errors.New("invalid configuration mode")
// ErrProofIsNil is the error for a nil proof.
ErrProofIsNil = errors.New("proof is nil")
// ErrDataBlockIsNil is the error for a nil data block.
ErrDataBlockIsNil = errors.New("data block is nil")
// ErrProofInvalidModeTreeNotBuilt is the error for an invalid mode in Proof() function.
// Proof() function requires a built tree to generate the proof.
ErrProofInvalidModeTreeNotBuilt = errors.New("merkle tree is not in built, could not generate proof by this method")
// ErrProofInvalidDataBlock is the error for an invalid data block in Proof() function.
ErrProofInvalidDataBlock = errors.New("data block is not a member of the merkle tree")
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.21

require (
github.com/agiledragon/gomonkey/v2 v2.11.0
github.com/txaty/gool v0.1.5
golang.org/x/sync v0.5.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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=
Expand All @@ -10,6 +8,8 @@ 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/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
89 changes: 89 additions & 0 deletions leaf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// MIT License
//
// 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
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// 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 "golang.org/x/sync/errgroup"

// computeLeafNodes compute the leaf nodes from the data blocks.
func (m *MerkleTree) computeLeafNodes(blocks []DataBlock) ([][]byte, error) {
var (
leaves = make([][]byte, m.NumLeaves)
hashFunc = m.HashFunc
disableLeafHashing = m.DisableLeafHashing
err error
)
for i := 0; i < m.NumLeaves; i++ {
if leaves[i], err = dataBlockToLeaf(blocks[i], hashFunc, disableLeafHashing); err != nil {
return nil, err
}
}
return leaves, nil
}

// computeLeafNodesParallel compute the leaf nodes from the data blocks in parallel.
func (m *MerkleTree) computeLeafNodesParallel(blocks []DataBlock) ([][]byte, error) {
var (
lenLeaves = len(blocks)
leaves = make([][]byte, lenLeaves)
numRoutines = m.NumRoutines
hashFunc = m.HashFunc
disableLeafHashing = m.DisableLeafHashing
eg = new(errgroup.Group)
)
numRoutines = min(numRoutines, lenLeaves)
for startIdx := 0; startIdx < numRoutines; startIdx++ {
startIdx := startIdx
eg.Go(func() error {
var err error
for i := startIdx; i < lenLeaves; i += numRoutines {
if leaves[i], err = dataBlockToLeaf(blocks[i], hashFunc, disableLeafHashing); err != nil {
return err
}
}
return nil
})
}
if err := eg.Wait(); err != nil {
return nil, err
}
return leaves, nil
}

// dataBlockToLeaf generates the leaf from the data block.
// If the leaf hashing is disabled, the data block is returned as the leaf.
func dataBlockToLeaf(block DataBlock, hashFunc TypeHashFunc, disableLeafHashing bool) ([]byte, error) {
blockBytes, err := block.Serialize()
if err != nil {
return nil, err
}
if disableLeafHashing {
// copy the value so that the original byte slice is not modified
leaf := make([]byte, len(blockBytes))
copy(leaf, blockBytes)
return leaf, nil
}
return hashFunc(blockBytes)
}
Loading

0 comments on commit 3b445a0

Please sign in to comment.