From 46c623807a055efcbc102a2e4b0e561a2d6e56c5 Mon Sep 17 00:00:00 2001 From: Jim McDonald Date: Tue, 30 Jan 2024 22:32:58 +0000 Subject: [PATCH] Linting. --- .github/workflows/golangci-lint.yml | 5 +---- .github/workflows/test.yml | 2 +- .golangci.yml | 1 - go.mod | 6 ++--- go.sum | 8 +++---- multiproof.go | 28 ++++++++++++++---------- multiproof_test.go | 34 +++++++++++------------------ 7 files changed, 39 insertions(+), 45 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 050d37e..bab7875 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -15,12 +15,9 @@ jobs: steps: - uses: actions/setup-go@v3 with: - go-version: 1.18 + go-version: '1.20' - uses: actions/checkout@v3 - name: golangci-lint uses: golangci/golangci-lint-action@v3 with: - # https://github.com/golangci/golangci-lint-action/issues/535 - version: v1.47.3 - # version: latest args: --timeout=60m diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8d4c068..8917f10 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,6 +10,6 @@ jobs: steps: - uses: actions/setup-go@v3 with: - go-version: 1.19 + go-version: '1.20' - uses: actions/checkout@v3 - uses: n8maninger/action-golang-test@v1 diff --git a/.golangci.yml b/.golangci.yml index dd44904..d5cb8d4 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -140,7 +140,6 @@ linters: - interfacer - ireturn - maligned - - nestif - nosnakecase - scopelint - structcheck diff --git a/go.mod b/go.mod index c418eda..ca0944b 100644 --- a/go.mod +++ b/go.mod @@ -1,15 +1,15 @@ module github.com/wealdtech/go-merkletree/v2 -go 1.19 +go 1.20 require ( github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.3.0 - golang.org/x/crypto v0.5.0 + golang.org/x/crypto v0.18.0 ) require ( github.com/davecgh/go-spew v1.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/sys v0.4.0 // indirect + golang.org/x/sys v0.16.0 // indirect ) diff --git a/go.sum b/go.sum index 4805fdc..f6f7767 100644 --- a/go.sum +++ b/go.sum @@ -7,7 +7,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= -golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= -golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= -golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc= +golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= diff --git a/multiproof.go b/multiproof.go index 344d398..a006a8d 100644 --- a/multiproof.go +++ b/multiproof.go @@ -70,18 +70,24 @@ func (p *MultiProof) Verify(data [][]byte, root []byte) (bool, error) { // Step 2 calculate values up the tree. for i := p.Values - 1; i > 0; i-- { _, exists := p.Hashes[i] + if exists { + continue + } + + child1, exists := p.Hashes[i*2] + if !exists { + continue + } + + child2, exists := p.Hashes[i*2+1] if !exists { - child1, exists := p.Hashes[i*2] - if exists { - child2, exists := p.Hashes[i*2+1] - if exists { - if p.sorted && bytes.Compare(child1, child2) == 1 { - p.Hashes[i] = p.hash.Hash(child2, child1) - } else { - p.Hashes[i] = p.hash.Hash(child1, child2) - } - } - } + continue + } + + if p.sorted && bytes.Compare(child1, child2) == 1 { + p.Hashes[i] = p.hash.Hash(child2, child1) + } else { + p.Hashes[i] = p.hash.Hash(child1, child2) } } diff --git a/multiproof_test.go b/multiproof_test.go index 92b16bc..4b295b3 100644 --- a/multiproof_test.go +++ b/multiproof_test.go @@ -34,31 +34,23 @@ func TestMultiProof(t *testing.T) { WithSorted(test.sorted), ) assert.Nil(t, err, fmt.Sprintf("failed to create tree at test %d", i)) - // Test proof for each data item individually - for j, data := range test.data { - proof, err := tree.GenerateMultiProof([][]byte{data}) - assert.Nil(t, err, fmt.Sprintf("failed to create multiproof at test %d data %d", i, j)) - proven, err := proof.Verify([][]byte{data}, tree.Root()) - assert.Nil(t, err, fmt.Sprintf("error verifying multiproof at test %d data %d", i, j)) - assert.True(t, proven, fmt.Sprintf("failed to verify multiproof at test %d data %d", i, j)) - } - // Test proof for each data item cumulatively. + + // Test proof for all combinations of data. var proof *MultiProof - for j, data := range test.data { - if j == 0 { - proof, err = tree.GenerateMultiProof([][]byte{data}) - assert.Nil(t, err, fmt.Sprintf("failed to create multiproof at test %d data %d", i, j)) + combinations := 1<>k)&1 == 1 { + items = append(items, test.data[k]) + } } - proven, err := proof.Verify([][]byte{data}, tree.Root()) + proof, err = tree.GenerateMultiProof(items) + assert.Nil(t, err, fmt.Sprintf("failed to create multiproof at test %d data %d", i, j)) + proven, err := proof.Verify(items, tree.Root()) assert.Nil(t, err, fmt.Sprintf("error verifying multiproof at test %d data %d", i, j)) assert.True(t, proven, fmt.Sprintf("failed to verify multiproof at test %d data %d", i, j)) } - // Test proof for all data - proof, err = tree.GenerateMultiProof(test.data) - assert.Nil(t, err, fmt.Sprintf("failed to create multiproof at test %d", i)) - proven, err := proof.Verify(test.data, tree.Root()) - assert.Nil(t, err, fmt.Sprintf("error verifying multiproof at test %d", i)) - assert.True(t, proven, fmt.Sprintf("failed to verify multiproof at test %d", i)) } } } @@ -147,7 +139,7 @@ func TestSavings(t *testing.T) { tree, err := New(data) assert.Nil(t, err, "failed to create tree") - rand.Seed(0) + rand := rand.New(rand.NewSource(0)) proofSize := 0 pollardSize := 0 multiProofSize := 0