Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix linter #41

Draft
wants to merge 15 commits into
base: dev/iavl_data_locality
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ on:
pull_request:
push:
branches:
- master
- "**"
jobs:
golangci-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: golangci/golangci-lint-action@v2.5.2
- uses: golangci/golangci-lint-action@2
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.32
version: v3.1.0
args: --timeout 10m
github-token: ${{ secrets.github_token }}
2 changes: 1 addition & 1 deletion benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func randBytes(length int) []byte {
key := make([]byte, length)
// math.rand.Read always returns err=nil
// we do not need cryptographic randomness for this test:
//nolint:gosec

rand.Read(key)
return key
}
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/hash_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// nolint: errcheck,scopelint
// nolint: scopelint
package benchmarks

import (
Expand Down
10 changes: 5 additions & 5 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func (c *lruCache) Add(node Node) Node {
return nil
}

func (nc *lruCache) Get(key []byte) Node {
if ele, hit := nc.dict[string(key)]; hit {
nc.ll.MoveToFront(ele)
func (c *lruCache) Get(key []byte) Node {
if ele, hit := c.dict[string(key)]; hit {
c.ll.MoveToFront(ele)
return ele.Value.(Node)
}
return nil
Expand All @@ -78,8 +78,8 @@ func (c *lruCache) Has(key []byte) bool {
return exists
}

func (nc *lruCache) Len() int {
return nc.ll.Len()
func (c *lruCache) Len() int {
return c.ll.Len()
}

func (c *lruCache) Remove(key []byte) Node {
Expand Down
3 changes: 2 additions & 1 deletion cache/cache_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func BenchmarkAdd(b *testing.B) {
},
}

for name, tc := range testcases {
for name, testcase := range testcases {
tc := testcase
cache := cache.New(tc.cacheLimit)
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down
10 changes: 6 additions & 4 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ func Test_Cache_Add(t *testing.T) {
},
}

for name, tc := range testcases {
for name, testcase := range testcases {
tc := testcase
t.Run(name, func(t *testing.T) {
cache := cache.New(tc.cacheLimit)

expectedCurSize := 0

for _, op := range tc.cacheOps {

actualResult := cache.Add(testNodes[op.testNodexIdx])
Expand Down Expand Up @@ -259,7 +259,8 @@ func Test_Cache_Remove(t *testing.T) {
},
}

for name, tc := range testcases {
for name, testcase := range testcases {
tc := testcase
t.Run(name, func(t *testing.T) {
cache := cache.New(tc.cacheLimit)

Expand Down Expand Up @@ -305,7 +306,8 @@ func randBytes(length int) []byte {
key := make([]byte, length)
// math.rand.Read always returns err=nil
// we do not need cryptographic randomness for this test:
//nolint:gosec

// nolint: errcheck
rand.Read(key)
return key
}
2 changes: 1 addition & 1 deletion cmd/iaviewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func OpenDB(dir string) (dbm.DB, error) {
return db, nil
}

// nolint: unused,deadcode
// nolint: deadcode
func PrintDBStats(db dbm.DB) {
count := 0
prefix := map[string]int{}
Expand Down
2 changes: 1 addition & 1 deletion cmd/iavlserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func openDB() (dbm.DB, error) {
// trapSignal will listen for any OS signal and invokes a callback function to
// perform any necessary cleanup.
func trapSignal(cb func()) {
var sigCh = make(chan os.Signal)
var sigCh = make(chan os.Signal, 2)

signal.Notify(sigCh, syscall.SIGTERM)
signal.Notify(sigCh, syscall.SIGINT)
Expand Down
4 changes: 2 additions & 2 deletions fast_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func DeserializeFastNode(key []byte, buf []byte) (*FastNode, error) {
return fastNode, nil
}

func (fn *FastNode) GetKey() []byte {
return fn.key
func (node *FastNode) GetKey() []byte {
return node.key
}

func (node *FastNode) encodedSize() int {
Expand Down
15 changes: 7 additions & 8 deletions immutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ func (t *ImmutableTree) renderNode(node *Node, indent string, depth int, encoder
return []string{here}
}

// recurse on inner node
here := fmt.Sprintf("%s%s", prefix, encoder(node.hash, depth, false))
left := t.renderNode(node.getLeftNode(t), indent, depth+1, encoder)
right := t.renderNode(node.getRightNode(t), indent, depth+1, encoder)
result := append(left, here)
result = append(result, right...)
// left
result := t.renderNode(node.getLeftNode(t), indent, depth+1, encoder)
// here
result = append(result, fmt.Sprintf("%s%s", prefix, encoder(node.hash, depth, false)))
// right
result = append(result, t.renderNode(node.getRightNode(t), indent, depth+1, encoder)...)
return result
}

Expand Down Expand Up @@ -227,9 +227,8 @@ func (t *ImmutableTree) Iterate(fn func(key []byte, value []byte) bool) bool {
func (t *ImmutableTree) Iterator(start, end []byte, ascending bool) dbm.Iterator {
if t.IsFastCacheEnabled() {
return NewFastIterator(start, end, ascending, t.ndb)
} else {
return NewIterator(start, end, ascending, t)
}
return NewIterator(start, end, ascending, t)
}

// IterateRange makes a callback for all nodes with key between start and end non-inclusive.
Expand Down
8 changes: 6 additions & 2 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func TestIterator_NewIterator_NilTree_Failure(t *testing.T) {
var start, end []byte = []byte{'a'}, []byte{'c'}
var start, end = []byte{'a'}, []byte{'c'}
ascending := true

performTest := func(t *testing.T, itr dbm.Iterator) {
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestIterator_NewIterator_NilTree_Failure(t *testing.T) {
}

func TestUnsavedFastIterator_NewIterator_NilAdditions_Failure(t *testing.T) {
var start, end []byte = []byte{'a'}, []byte{'c'}
var start, end = []byte{'a'}, []byte{'c'}
ascending := true

performTest := func(t *testing.T, itr dbm.Iterator) {
Expand Down Expand Up @@ -115,6 +115,7 @@ func TestIterator_Empty_Invalid(t *testing.T) {
})
}

// nolint: dupl
func TestIterator_Basic_Ranged_Ascending_Success(t *testing.T) {
config := &iteratorTestConfig{
startByteToSet: 'a',
Expand Down Expand Up @@ -153,6 +154,7 @@ func TestIterator_Basic_Ranged_Ascending_Success(t *testing.T) {
})
}

// nolint: dupl
func TestIterator_Basic_Ranged_Descending_Success(t *testing.T) {
config := &iteratorTestConfig{
startByteToSet: 'a',
Expand Down Expand Up @@ -386,8 +388,10 @@ func setupUnsavedFastIterator(t *testing.T, config *iteratorTestConfig) (dbm.Ite
// Merge the two halves
var mergedMirror [][]string
if config.ascending {
// nolint: gocritic
mergedMirror = append(firstHalfMirror, secondHalfMirror...)
} else {
// nolint: gocritic
mergedMirror = append(secondHalfMirror, firstHalfMirror...)
}

Expand Down
2 changes: 1 addition & 1 deletion key_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

// Provides a fixed-width lexicographically sortable []byte key format
type KeyFormat struct {
prefix byte
layout []int
length int
prefix byte
unbounded bool
}

Expand Down
30 changes: 16 additions & 14 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ func (tree *MutableTree) Set(key, value []byte) (updated bool) {

// Get returns the value of the specified key if it exists, or nil otherwise.
// The returned value must not be modified, since it may point to data stored within IAVL.
func (t *MutableTree) Get(key []byte) []byte {
if t.root == nil {
func (tree *MutableTree) Get(key []byte) []byte {
if tree.root == nil {
return nil
}

if fastNode, ok := t.unsavedFastNodeAdditions[string(key)]; ok {
if fastNode, ok := tree.unsavedFastNodeAdditions[string(key)]; ok {
return fastNode.value
}

return t.ImmutableTree.Get(key)
return tree.ImmutableTree.Get(key)
}

// Import returns an importer for tree nodes previously exported by ImmutableTree.Export(),
Expand All @@ -160,16 +160,16 @@ func (tree *MutableTree) Import(version int64) (*Importer, error) {

// Iterate iterates over all keys of the tree. The keys and values must not be modified,
// since they may point to data stored within IAVL. Returns true if stopped by callnack, false otherwise
func (t *MutableTree) Iterate(fn func(key []byte, value []byte) bool) (stopped bool) {
if t.root == nil {
func (tree *MutableTree) Iterate(fn func(key []byte, value []byte) bool) (stopped bool) {
if tree.root == nil {
return false
}

if !t.IsFastCacheEnabled() {
return t.ImmutableTree.Iterate(fn)
if !tree.IsFastCacheEnabled() {
return tree.ImmutableTree.Iterate(fn)
}

itr := NewUnsavedFastIterator(nil, nil, true, t.ndb, t.unsavedFastNodeAdditions, t.unsavedFastNodeRemovals)
itr := NewUnsavedFastIterator(nil, nil, true, tree.ndb, tree.unsavedFastNodeAdditions, tree.unsavedFastNodeRemovals)
for ; itr.Valid(); itr.Next() {
if fn(itr.Key(), itr.Value()) {
return true
Expand All @@ -181,11 +181,11 @@ func (t *MutableTree) Iterate(fn func(key []byte, value []byte) bool) (stopped b

// Iterator returns an iterator over the mutable tree.
// CONTRACT: no updates are made to the tree while an iterator is active.
func (t *MutableTree) Iterator(start, end []byte, ascending bool) dbm.Iterator {
if t.IsFastCacheEnabled() {
return NewUnsavedFastIterator(start, end, ascending, t.ndb, t.unsavedFastNodeAdditions, t.unsavedFastNodeRemovals)
func (tree *MutableTree) Iterator(start, end []byte, ascending bool) dbm.Iterator {
if tree.IsFastCacheEnabled() {
return NewUnsavedFastIterator(start, end, ascending, tree.ndb, tree.unsavedFastNodeAdditions, tree.unsavedFastNodeRemovals)
}
return t.ImmutableTree.Iterator(start, end, ascending)
return tree.ImmutableTree.Iterator(start, end, ascending)
}

func (tree *MutableTree) set(key []byte, value []byte) (orphans []*Node, updated bool) {
Expand Down Expand Up @@ -822,7 +822,9 @@ func (tree *MutableTree) saveFastNodeRemovals() error {
sort.Strings(keysToSort)

for _, key := range keysToSort {
tree.ndb.DeleteFastNode([]byte(key))
if err := tree.ndb.DeleteFastNode([]byte(key)); err != nil {
return err
}
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ func setupTreeAndMirrorForUpgrade(t *testing.T) (*MutableTree, [][]string) {
tree, _ := NewMutableTree(db, 0)

const numEntries = 100
var keyPrefix, valPrefix string = "key", "val"
var keyPrefix, valPrefix = "key", "val"

mirror := make([][]string, 0, numEntries)
for i := 0; i < numEntries; i++ {
Expand Down
4 changes: 2 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func MakeNode(buf []byte) (*Node, error) {
return node, nil
}

func (n *Node) GetKey() []byte {
return n.hash
func (node *Node) GetKey() []byte {
return node.hash
}

// String returns a string representation of the node.
Expand Down
Loading