Skip to content

Commit c5fb5ce

Browse files
authored
Update to Go 1.20 (#40)
1 parent d85048b commit c5fb5ce

File tree

7 files changed

+27
-40
lines changed

7 files changed

+27
-40
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
name: CI
22

3-
env:
4-
go-version: "1.19"
5-
63
# Controls when the action will run. Triggers the workflow on push or pull request
74
# events but only for the develop branch
85
on:
@@ -25,7 +22,8 @@ jobs:
2522
- name: set up go
2623
uses: actions/setup-go@v4
2724
with:
28-
go-version: ${{ env.go-version }}
25+
check-latest: true
26+
go-version-file: "go.mod"
2927

3028
- name: build
3129
run: go build -v ./...

cache/cache_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func TestCache_ValidateStructure(t *testing.T) {
2828
treeCache := &cache{layers: readers}
2929
err := treeCache.validateStructure()
3030

31-
r.Error(err)
32-
r.Equal("reader for base layer must be included", err.Error())
31+
r.Error(err,"reader for base layer must be included")
3332
}
3433

3534
func TestCache_ValidateStructure2(t *testing.T) {
@@ -39,8 +38,7 @@ func TestCache_ValidateStructure2(t *testing.T) {
3938
treeCache := &cache{layers: readers}
4039
err := treeCache.validateStructure()
4140

42-
r.Error(err)
43-
r.Equal("reader for base layer must be included", err.Error())
41+
r.Error(err,"reader for base layer must be included")
4442
}
4543

4644
func TestCache_ValidateStructureSuccess(t *testing.T) {
@@ -66,8 +64,7 @@ func TestCache_ValidateStructureFail(t *testing.T) {
6664
treeCache := &cache{layers: readers}
6765
err := treeCache.validateStructure()
6866

69-
r.Error(err)
70-
r.Equal("reader at layer 1 has width 2 instead of 1", err.Error())
67+
r.Error(err,"reader at layer 1 has width 2 instead of 1")
7168
}
7269

7370
func TestCache_ValidateStructureFail2(t *testing.T) {
@@ -80,6 +77,5 @@ func TestCache_ValidateStructureFail2(t *testing.T) {
8077
treeCache := &cache{layers: readers}
8178
err := treeCache.validateStructure()
8279

83-
r.Error(err)
84-
r.Equal("reader at layer 1 has width 1 instead of 2", err.Error())
80+
r.Error(err,"reader at layer 1 has width 1 instead of 2")
8581
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/spacemeshos/merkle-tree
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/minio/sha256-simd v1.0.1

merkle.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package merkle
22

33
import (
4-
"errors"
4+
"fmt"
55

66
"github.com/minio/sha256-simd"
77

@@ -117,7 +117,7 @@ func (t *Tree) AddLeaf(value []byte) error {
117117
if l.cache != nil {
118118
_, err := l.cache.Append(n.value)
119119
if err != nil {
120-
lastCachingError = errors.New("error while caching: " + err.Error())
120+
lastCachingError = fmt.Errorf("error while caching: %w", err)
121121
}
122122
}
123123

proving.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ func calcSubtreeProof(c CacheReader, leavesToProve Set, subtreeStart Position, w
8484
reader := c.GetLayerReader(0)
8585
err = reader.Seek(subtreeStart.Index)
8686
if err != nil {
87-
return nil, nil, errors.New("while preparing to traverse subtree: " + err.Error())
87+
return nil, nil, fmt.Errorf("while preparing to traverse subtree: %w", err)
8888
}
8989

9090
_, additionalProof, additionalLeaves, err = traverseSubtree(reader, width, c.GetHashFunc(), relativeLeavesToProve, nil)
9191
if err != nil {
92-
return nil, nil, errors.New("while traversing subtree: " + err.Error())
92+
return nil, nil, fmt.Errorf("while traversing subtree: %w", err)
9393
}
9494

9595
return additionalProof, additionalLeaves, err
@@ -105,7 +105,7 @@ func traverseSubtree(leafReader LayerReader, width uint64, hash HashFunc, leaves
105105
WithMinHeight(RootHeightFromWidth(width)). // This ensures the correct size tree, even if padding is needed.
106106
Build()
107107
if err != nil {
108-
return nil, nil, nil, errors.New("while building a tree: " + err.Error())
108+
return nil, nil, nil, fmt.Errorf("while building a tree: %w", err)
109109
}
110110
for i := uint64(0); i < width; i++ {
111111
leaf, err := leafReader.ReadNext()
@@ -117,11 +117,11 @@ func traverseSubtree(leafReader LayerReader, width uint64, hash HashFunc, leaves
117117
leaf = externalPadding
118118
shouldUseExternalPadding = false
119119
} else if err != nil {
120-
return nil, nil, nil, errors.New("while reading a leaf: " + err.Error())
120+
return nil, nil, nil, fmt.Errorf("while reading a leaf: %w", err)
121121
}
122122
err = t.AddLeaf(leaf)
123123
if err != nil {
124-
return nil, nil, nil, errors.New("while adding a leaf: " + err.Error())
124+
return nil, nil, nil, fmt.Errorf("while adding a leaf: %w", err)
125125
}
126126
if leavesToProve[i] {
127127
provenLeaves = append(provenLeaves, leaf)
@@ -145,11 +145,11 @@ func GetNode(c CacheReader, nodePos Position) ([]byte, error) {
145145
return calcNode(c, nodePos)
146146
}
147147
if err != nil {
148-
return nil, errors.New("while seeking to Position " + nodePos.String() + " in cache: " + err.Error())
148+
return nil, fmt.Errorf("while seeking to Position %s in cache: %w", nodePos, err)
149149
}
150150
currentVal, err := reader.ReadNext()
151151
if err != nil {
152-
return nil, errors.New("while reading from cache: " + err.Error())
152+
return nil, fmt.Errorf("while reading from cache: %w", err)
153153
}
154154
return currentVal, nil
155155
}
@@ -173,7 +173,7 @@ func calcNode(c CacheReader, nodePos Position) ([]byte, error) {
173173
break
174174
}
175175
if err != nil && err != io.EOF {
176-
return nil, errors.New("while seeking to Position " + subtreeStart.String() + " in cache: " + err.Error())
176+
return nil, fmt.Errorf("while seeking to Position %s in cache: %w", subtreeStart, err)
177177
}
178178
if subtreeStart.Height == 0 {
179179
return PaddingValue.value, nil
@@ -184,7 +184,7 @@ func calcNode(c CacheReader, nodePos Position) ([]byte, error) {
184184
width := uint64(1) << (nodePos.Height - subtreeStart.Height)
185185
readerWidth, err := reader.Width()
186186
if err != nil {
187-
return nil, fmt.Errorf("while getting reader width: %v", err)
187+
return nil, fmt.Errorf("while getting reader width: %w", err)
188188
}
189189
if readerWidth < subtreeStart.Index+width {
190190
paddingPos := Position{
@@ -195,14 +195,14 @@ func calcNode(c CacheReader, nodePos Position) ([]byte, error) {
195195
if err == ErrMissingValueAtBaseLayer {
196196
paddingValue = PaddingValue.value
197197
} else if err != nil {
198-
return nil, errors.New("while calculating ephemeral node at Position " + paddingPos.String() + ": " + err.Error())
198+
return nil, fmt.Errorf("while calculating ephemeral node at Position %s: %w", paddingPos, err)
199199
}
200200
}
201201

202202
// Traverse the subtree.
203203
currentVal, _, _, err := traverseSubtree(reader, width, c.GetHashFunc(), nil, paddingValue)
204204
if err != nil {
205-
return nil, errors.New("while traversing subtree for root: " + err.Error())
205+
return nil, fmt.Errorf("while traversing subtree for root: %w", err)
206206
}
207207
return currentVal, nil
208208
}

proving_test.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,7 @@ func TestGetNode(t *testing.T) {
494494
nodePos := position{}
495495
node, err := GetNode(cacheReader, nodePos)
496496

497-
r.Error(err)
498-
r.Equal("while seeking to Position <h: 0 i: 0> in cache: some error", err.Error())
497+
r.EqualError(err, "while seeking to Position <h: 0 i: 0> in cache: some error")
499498
r.Nil(node)
500499
}
501500

@@ -509,8 +508,7 @@ func TestGetNode2(t *testing.T) {
509508
nodePos := position{}
510509
node, err := GetNode(cacheReader, nodePos)
511510

512-
r.Error(err)
513-
r.Equal("while reading from cache: some error", err.Error())
511+
r.EqualError(err, "while reading from cache: some error")
514512
r.Nil(node)
515513
}
516514

@@ -525,8 +523,7 @@ func TestGetNode3(t *testing.T) {
525523
nodePos := position{Height: 1}
526524
node, err := GetNode(cacheReader, nodePos)
527525

528-
r.Error(err)
529-
r.Equal("while seeking to Position <h: 0 i: 0> in cache: some error", err.Error())
526+
r.EqualError(err, "while seeking to Position <h: 0 i: 0> in cache: some error")
530527
r.Nil(node)
531528
}
532529

@@ -542,8 +539,7 @@ func TestGetNode4(t *testing.T) {
542539
nodePos := position{Height: 2}
543540
node, err := GetNode(cacheReader, nodePos)
544541

545-
r.Error(err)
546-
r.Equal("while calculating ephemeral node at Position <h: 1 i: 1>: while seeking to Position <h: 0 i: 10> in cache: some error", err.Error())
542+
r.EqualError(err, "while calculating ephemeral node at Position <h: 1 i: 1>: while seeking to Position <h: 0 i: 10> in cache: some error")
547543
r.Nil(node)
548544
}
549545

@@ -558,8 +554,7 @@ func TestGetNode5(t *testing.T) {
558554
nodePos := position{Height: 1}
559555
node, err := GetNode(cacheReader, nodePos)
560556

561-
r.Error(err)
562-
r.Equal("while traversing subtree for root: while reading a leaf: some error", err.Error())
557+
r.EqualError(err, "while traversing subtree for root: while reading a leaf: some error")
563558
r.Nil(node)
564559
}
565560

@@ -568,7 +563,6 @@ func TestCache_ValidateStructure(t *testing.T) {
568563
cacheWriter := cache.NewWriter(nil, nil)
569564
cacheReader, err := cacheWriter.GetReader()
570565

571-
r.Error(err)
572-
r.Equal("reader for base layer must be included", err.Error())
566+
r.EqualError(err, "reader for base layer must be included")
573567
r.Nil(cacheReader)
574568
}

validation_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ func TestValidator_calcRoot(t *testing.T) {
321321

322322
root, _, err := v.CalcRoot(0)
323323

324-
r.Error(err)
325-
r.Equal("no more items", err.Error())
324+
r.EqualError(err, "no more items")
326325
r.Nil(root)
327326
}

0 commit comments

Comments
 (0)