mirror of
https://github.com/arnaucube/go-merkletree-iden3.git
synced 2026-02-07 03:26:46 +01:00
Add golangci-lint to GHA & fix lint warnings
This commit is contained in:
16
.github/workflows/lint.yml
vendored
Normal file
16
.github/workflows/lint.yml
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
name: Lint
|
||||
on: [ push, pull_request ]
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: 1.14.x
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Lint
|
||||
run: |
|
||||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.30.0
|
||||
$(go env GOPATH)/bin/golangci-lint run --timeout=5m -E whitespace -E gosec -E gci -E misspell -E gomnd --max-same-issues 0
|
||||
@@ -3,13 +3,12 @@ package leveldb
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
"github.com/syndtr/goleveldb/leveldb/errors"
|
||||
"github.com/syndtr/goleveldb/leveldb/opt"
|
||||
"github.com/syndtr/goleveldb/leveldb/util"
|
||||
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
)
|
||||
|
||||
// LevelDbStorage implements the db.Storage interface
|
||||
|
||||
@@ -57,7 +57,6 @@ func (m *MemoryStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
}
|
||||
localkey := v.K[len(m.prefix):]
|
||||
kvs = append(kvs, db.KV{K: localkey, V: v.V})
|
||||
|
||||
}
|
||||
sort.SliceStable(kvs, func(i, j int) bool { return bytes.Compare(kvs[i].K, kvs[j].K) < 0 })
|
||||
|
||||
@@ -73,7 +72,6 @@ func (m *MemoryStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||
|
||||
// Get implements the method Get of the interface db.Tx
|
||||
func (tx *MemoryStorageTx) Get(key []byte) ([]byte, error) {
|
||||
|
||||
if v, ok := tx.kv.Get(db.Concat(tx.s.prefix, key)); ok {
|
||||
return v, nil
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ func (tx *PebbleStorageTx) Add(atx db.Tx) {
|
||||
func (tx *PebbleStorageTx) Commit() error {
|
||||
batch := tx.PebbleStorage.pdb.NewBatch()
|
||||
for _, v := range tx.cache {
|
||||
batch.Set(v.K, v.V, nil)
|
||||
_ = batch.Set(v.K, v.V, nil)
|
||||
}
|
||||
|
||||
tx.cache = nil
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//nolint:gomnd
|
||||
package test
|
||||
|
||||
import (
|
||||
@@ -108,17 +109,17 @@ func TestIterate(t *testing.T, sto db.Storage) {
|
||||
err = sto1.Iterate(lister)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 3, len(r))
|
||||
assert.Equal(t, db.KV{[]byte{1}, []byte{4}}, r[0])
|
||||
assert.Equal(t, db.KV{[]byte{2}, []byte{5}}, r[1])
|
||||
assert.Equal(t, db.KV{[]byte{3}, []byte{6}}, r[2])
|
||||
assert.Equal(t, db.KV{K: []byte{1}, V: []byte{4}}, r[0])
|
||||
assert.Equal(t, db.KV{K: []byte{2}, V: []byte{5}}, r[1])
|
||||
assert.Equal(t, db.KV{K: []byte{3}, V: []byte{6}}, r[2])
|
||||
|
||||
r = []db.KV{}
|
||||
err = sto2.Iterate(lister)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 3, len(r))
|
||||
assert.Equal(t, db.KV{[]byte{1}, []byte{7}}, r[0])
|
||||
assert.Equal(t, db.KV{[]byte{2}, []byte{8}}, r[1])
|
||||
assert.Equal(t, db.KV{[]byte{3}, []byte{9}}, r[2])
|
||||
assert.Equal(t, db.KV{K: []byte{1}, V: []byte{7}}, r[0])
|
||||
assert.Equal(t, db.KV{K: []byte{2}, V: []byte{8}}, r[1])
|
||||
assert.Equal(t, db.KV{K: []byte{3}, V: []byte{9}}, r[2])
|
||||
}
|
||||
|
||||
// TestConcatTx checks that the implementation of the db.Storage interface
|
||||
@@ -180,13 +181,13 @@ func TestList(t *testing.T, sto db.Storage) {
|
||||
r, err := sto1.List(100)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 3, len(r))
|
||||
assert.Equal(t, r[0], db.KV{[]byte{1}, []byte{4}})
|
||||
assert.Equal(t, r[1], db.KV{[]byte{2}, []byte{5}})
|
||||
assert.Equal(t, r[2], db.KV{[]byte{3}, []byte{6}})
|
||||
assert.Equal(t, r[0], db.KV{K: []byte{1}, V: []byte{4}})
|
||||
assert.Equal(t, r[1], db.KV{K: []byte{2}, V: []byte{5}})
|
||||
assert.Equal(t, r[2], db.KV{K: []byte{3}, V: []byte{6}})
|
||||
|
||||
r, err = sto1.List(2)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 2, len(r))
|
||||
assert.Equal(t, r[0], db.KV{[]byte{1}, []byte{4}})
|
||||
assert.Equal(t, r[1], db.KV{[]byte{2}, []byte{5}})
|
||||
assert.Equal(t, r[0], db.KV{K: []byte{1}, V: []byte{4}})
|
||||
assert.Equal(t, r[1], db.KV{K: []byte{2}, V: []byte{5}})
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ const (
|
||||
proofFlagsLen = 2
|
||||
// ElemBytesLen is the length of the Hash byte array
|
||||
ElemBytesLen = 32
|
||||
|
||||
numCharPrint = 8
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -60,10 +62,10 @@ type Hash [32]byte
|
||||
// String returns decimal representation in string format of the Hash
|
||||
func (h Hash) String() string {
|
||||
s := h.BigInt().String()
|
||||
if len(s) < 8 {
|
||||
if len(s) < numCharPrint {
|
||||
return s
|
||||
}
|
||||
return s[0:8] + "..."
|
||||
return s[0:numCharPrint] + "..."
|
||||
}
|
||||
|
||||
// Hex returns the hexadecimal representation of the Hash
|
||||
@@ -93,10 +95,10 @@ func (h *Hash) Bytes() []byte {
|
||||
// from a byte array that previously has ben generated by the Hash.Bytes()
|
||||
// method.
|
||||
func NewBigIntFromBytes(b []byte) (*big.Int, error) {
|
||||
if len(b) != 32 {
|
||||
if len(b) != ElemBytesLen {
|
||||
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b))
|
||||
}
|
||||
return new(big.Int).SetBytes(common.SwapEndianness(b[:32])), nil
|
||||
return new(big.Int).SetBytes(common.SwapEndianness(b[:ElemBytesLen])), nil
|
||||
}
|
||||
|
||||
// NewHashFromBigInt returns a *Hash representation of the given *big.Int
|
||||
@@ -110,7 +112,7 @@ func NewHashFromBigInt(b *big.Int) *Hash {
|
||||
// in the process. This is the intended method to get a *Hash from a byte array
|
||||
// that previously has ben generated by the Hash.Bytes() method.
|
||||
func NewHashFromBytes(b []byte) (*Hash, error) {
|
||||
if len(b) != 32 {
|
||||
if len(b) != ElemBytesLen {
|
||||
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b))
|
||||
}
|
||||
var h Hash
|
||||
@@ -224,7 +226,7 @@ func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof
|
||||
var cp CircomProcessorProof
|
||||
cp.Fnc = 2
|
||||
cp.OldRoot = mt.rootKey
|
||||
gettedK, gettedV, siblings, err := mt.Get(k)
|
||||
gettedK, gettedV, _, err := mt.Get(k)
|
||||
if err != nil && err != ErrKeyNotFound {
|
||||
return nil, err
|
||||
}
|
||||
@@ -233,7 +235,7 @@ func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof
|
||||
if bytes.Equal(cp.OldKey[:], HashZero[:]) {
|
||||
cp.IsOld0 = true
|
||||
}
|
||||
_, _, siblings, err = mt.Get(k)
|
||||
_, _, siblings, err := mt.Get(k)
|
||||
if err != nil && err != ErrKeyNotFound {
|
||||
return nil, err
|
||||
}
|
||||
@@ -583,12 +585,12 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [
|
||||
}
|
||||
|
||||
toUpload := siblings[len(siblings)-1]
|
||||
if len(siblings) < 2 {
|
||||
if len(siblings) < 2 { //nolint:gomnd
|
||||
mt.rootKey = siblings[0]
|
||||
mt.dbInsert(tx, rootNodeValue, DBEntryTypeRoot, mt.rootKey[:])
|
||||
return tx.Commit()
|
||||
}
|
||||
for i := len(siblings) - 2; i >= 0; i-- {
|
||||
for i := len(siblings) - 2; i >= 0; i-- { //nolint:gomnd
|
||||
if !bytes.Equal(siblings[i][:], HashZero[:]) {
|
||||
var newNode *Node
|
||||
if path[i] {
|
||||
@@ -646,27 +648,6 @@ func (mt *MerkleTree) recalculatePathUntilRoot(tx db.Tx, path []bool, node *Node
|
||||
return nodeKey, err
|
||||
}
|
||||
|
||||
// dbGet is a helper function to get the node of a key from the internal
|
||||
// storage.
|
||||
func (mt *MerkleTree) dbGet(k []byte) (NodeType, []byte, error) {
|
||||
if bytes.Equal(k, HashZero[:]) {
|
||||
return 0, nil, nil
|
||||
}
|
||||
|
||||
value, err := mt.db.Get(k)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
if len(value) < 2 {
|
||||
return 0, nil, ErrInvalidDBValue
|
||||
}
|
||||
nodeType := value[0]
|
||||
nodeBytes := value[1:]
|
||||
|
||||
return NodeType(nodeType), nodeBytes, nil
|
||||
}
|
||||
|
||||
// dbInsert is a helper function to insert a node into a key in an open db
|
||||
// transaction.
|
||||
func (mt *MerkleTree) dbInsert(tx db.Tx, k []byte, t NodeType, data []byte) {
|
||||
@@ -756,7 +737,7 @@ func NewProofFromBytes(bs []byte) (*Proof, error) {
|
||||
func (p *Proof) Bytes() []byte {
|
||||
bsLen := proofFlagsLen + len(p.notempties) + ElemBytesLen*len(p.Siblings)
|
||||
if p.NodeAux != nil {
|
||||
bsLen += 2 * ElemBytesLen
|
||||
bsLen += 2 * ElemBytesLen //nolint:gomnd
|
||||
}
|
||||
bs := make([]byte, bsLen)
|
||||
|
||||
|
||||
@@ -350,12 +350,12 @@ func TestGraphViz(t *testing.T) {
|
||||
mt, err := NewMerkleTree(memory.NewMemoryStorage(), 10)
|
||||
assert.Nil(t, err)
|
||||
|
||||
mt.Add(big.NewInt(1), big.NewInt(0))
|
||||
mt.Add(big.NewInt(2), big.NewInt(0))
|
||||
mt.Add(big.NewInt(3), big.NewInt(0))
|
||||
mt.Add(big.NewInt(4), big.NewInt(0))
|
||||
mt.Add(big.NewInt(5), big.NewInt(0))
|
||||
mt.Add(big.NewInt(100), big.NewInt(0))
|
||||
_ = mt.Add(big.NewInt(1), big.NewInt(0))
|
||||
_ = mt.Add(big.NewInt(2), big.NewInt(0))
|
||||
_ = mt.Add(big.NewInt(3), big.NewInt(0))
|
||||
_ = mt.Add(big.NewInt(4), big.NewInt(0))
|
||||
_ = mt.Add(big.NewInt(5), big.NewInt(0))
|
||||
_ = mt.Add(big.NewInt(100), big.NewInt(0))
|
||||
|
||||
// mt.PrintGraphViz(nil)
|
||||
|
||||
@@ -381,7 +381,8 @@ node [fontname=Monospace,fontsize=10,shape=box]
|
||||
}
|
||||
`
|
||||
w := bytes.NewBufferString("")
|
||||
mt.GraphViz(w, nil)
|
||||
err = mt.GraphViz(w, nil)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, []byte(expected), w.Bytes())
|
||||
}
|
||||
|
||||
@@ -415,7 +416,6 @@ func TestDelete(t *testing.T) {
|
||||
err = mt.Delete(big.NewInt(1))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "0", mt.Root().String())
|
||||
|
||||
}
|
||||
|
||||
func TestDelete2(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user