mirror of
https://github.com/arnaucube/go-merkletree-iden3.git
synced 2026-02-07 03:26:46 +01:00
Upgrade linter rules
This commit is contained in:
2
.github/workflows/lint.yml
vendored
2
.github/workflows/lint.yml
vendored
@@ -13,4 +13,4 @@ jobs:
|
|||||||
- name: Lint
|
- name: Lint
|
||||||
run: |
|
run: |
|
||||||
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.30.0
|
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
|
$(go env GOPATH)/bin/golangci-lint run --timeout=5m -c .golangci.yml
|
||||||
|
|||||||
17
.golangci.yml
Normal file
17
.golangci.yml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
issues:
|
||||||
|
max-same-issues: 0
|
||||||
|
exclude-use-default: false
|
||||||
|
linters:
|
||||||
|
enable:
|
||||||
|
- whitespace
|
||||||
|
- gosec
|
||||||
|
- gci
|
||||||
|
- misspell
|
||||||
|
- gomnd
|
||||||
|
- gofmt
|
||||||
|
- goimports
|
||||||
|
- lll
|
||||||
|
- golint
|
||||||
|
linters-settings:
|
||||||
|
lll:
|
||||||
|
line-length: 100
|
||||||
@@ -11,20 +11,20 @@ import (
|
|||||||
"github.com/syndtr/goleveldb/leveldb/util"
|
"github.com/syndtr/goleveldb/leveldb/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LevelDbStorage implements the db.Storage interface
|
// Storage implements the db.Storage interface
|
||||||
type LevelDbStorage struct {
|
type Storage struct {
|
||||||
ldb *leveldb.DB
|
ldb *leveldb.DB
|
||||||
prefix []byte
|
prefix []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// LevelDbStorageTx implements the db.Tx interface
|
// StorageTx implements the db.Tx interface
|
||||||
type LevelDbStorageTx struct {
|
type StorageTx struct {
|
||||||
*LevelDbStorage
|
*Storage
|
||||||
cache db.KvMap
|
cache db.KvMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewLevelDbStorage returns a new LevelDbStorage
|
// NewLevelDbStorage returns a new Storage
|
||||||
func NewLevelDbStorage(path string, errorIfMissing bool) (*LevelDbStorage, error) {
|
func NewLevelDbStorage(path string, errorIfMissing bool) (*Storage, error) {
|
||||||
o := &opt.Options{
|
o := &opt.Options{
|
||||||
ErrorIfMissing: errorIfMissing,
|
ErrorIfMissing: errorIfMissing,
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,7 @@ func NewLevelDbStorage(path string, errorIfMissing bool) (*LevelDbStorage, error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &LevelDbStorage{ldb, []byte{}}, nil
|
return &Storage{ldb, []byte{}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type storageInfo struct {
|
type storageInfo struct {
|
||||||
@@ -41,7 +41,7 @@ type storageInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Info implements the method Info of the interface db.Storage
|
// Info implements the method Info of the interface db.Storage
|
||||||
func (l *LevelDbStorage) Info() string {
|
func (l *Storage) Info() string {
|
||||||
snapshot, err := l.ldb.GetSnapshot()
|
snapshot, err := l.ldb.GetSnapshot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return err.Error()
|
||||||
@@ -72,17 +72,17 @@ func (l *LevelDbStorage) Info() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
||||||
func (l *LevelDbStorage) WithPrefix(prefix []byte) db.Storage {
|
func (l *Storage) WithPrefix(prefix []byte) db.Storage {
|
||||||
return &LevelDbStorage{l.ldb, db.Concat(l.prefix, prefix)}
|
return &Storage{l.ldb, db.Concat(l.prefix, prefix)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTx implements the method NewTx of the interface db.Storage
|
// NewTx implements the method NewTx of the interface db.Storage
|
||||||
func (l *LevelDbStorage) NewTx() (db.Tx, error) {
|
func (l *Storage) NewTx() (db.Tx, error) {
|
||||||
return &LevelDbStorageTx{l, make(db.KvMap)}, nil
|
return &StorageTx{l, make(db.KvMap)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get retreives a value from a key in the db.Storage
|
// Get retreives a value from a key in the db.Storage
|
||||||
func (l *LevelDbStorage) Get(key []byte) ([]byte, error) {
|
func (l *Storage) Get(key []byte) ([]byte, error) {
|
||||||
v, err := l.ldb.Get(db.Concat(l.prefix, key[:]), nil)
|
v, err := l.ldb.Get(db.Concat(l.prefix, key[:]), nil)
|
||||||
if err == errors.ErrNotFound {
|
if err == errors.ErrNotFound {
|
||||||
return nil, db.ErrNotFound
|
return nil, db.ErrNotFound
|
||||||
@@ -91,7 +91,7 @@ func (l *LevelDbStorage) Get(key []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterate implements the method Iterate of the interface db.Storage
|
// Iterate implements the method Iterate of the interface db.Storage
|
||||||
func (l *LevelDbStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
func (l *Storage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||||
// FIXME: Use the prefix!
|
// FIXME: Use the prefix!
|
||||||
snapshot, err := l.ldb.GetSnapshot()
|
snapshot, err := l.ldb.GetSnapshot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -112,7 +112,7 @@ func (l *LevelDbStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get retreives a value from a key in the interface db.Tx
|
// Get retreives a value from a key in the interface db.Tx
|
||||||
func (tx *LevelDbStorageTx) Get(key []byte) ([]byte, error) {
|
func (tx *StorageTx) Get(key []byte) ([]byte, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
fullkey := db.Concat(tx.prefix, key)
|
fullkey := db.Concat(tx.prefix, key)
|
||||||
@@ -130,14 +130,14 @@ func (tx *LevelDbStorageTx) Get(key []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Put saves a key:value into the db.Storage
|
// Put saves a key:value into the db.Storage
|
||||||
func (tx *LevelDbStorageTx) Put(k, v []byte) error {
|
func (tx *StorageTx) Put(k, v []byte) error {
|
||||||
tx.cache.Put(db.Concat(tx.prefix, k[:]), v)
|
tx.cache.Put(db.Concat(tx.prefix, k[:]), v)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add implements the method Add of the interface db.Tx
|
// Add implements the method Add of the interface db.Tx
|
||||||
func (tx *LevelDbStorageTx) Add(atx db.Tx) error {
|
func (tx *StorageTx) Add(atx db.Tx) error {
|
||||||
ldbtx := atx.(*LevelDbStorageTx)
|
ldbtx := atx.(*StorageTx)
|
||||||
for _, v := range ldbtx.cache {
|
for _, v := range ldbtx.cache {
|
||||||
tx.cache.Put(v.K, v.V)
|
tx.cache.Put(v.K, v.V)
|
||||||
}
|
}
|
||||||
@@ -145,7 +145,7 @@ func (tx *LevelDbStorageTx) Add(atx db.Tx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Commit implements the method Commit of the interface db.Tx
|
// Commit implements the method Commit of the interface db.Tx
|
||||||
func (tx *LevelDbStorageTx) Commit() error {
|
func (tx *StorageTx) Commit() error {
|
||||||
var batch leveldb.Batch
|
var batch leveldb.Batch
|
||||||
for _, v := range tx.cache {
|
for _, v := range tx.cache {
|
||||||
batch.Put(v.K, v.V)
|
batch.Put(v.K, v.V)
|
||||||
@@ -156,12 +156,12 @@ func (tx *LevelDbStorageTx) Commit() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close implements the method Close of the interface db.Tx
|
// Close implements the method Close of the interface db.Tx
|
||||||
func (tx *LevelDbStorageTx) Close() {
|
func (tx *StorageTx) Close() {
|
||||||
tx.cache = nil
|
tx.cache = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements the method Close of the interface db.Storage
|
// Close implements the method Close of the interface db.Storage
|
||||||
func (l *LevelDbStorage) Close() {
|
func (l *Storage) Close() {
|
||||||
if err := l.ldb.Close(); err != nil {
|
if err := l.ldb.Close(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -169,12 +169,12 @@ func (l *LevelDbStorage) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LevelDB is an extra method that returns the *leveldb.DB
|
// LevelDB is an extra method that returns the *leveldb.DB
|
||||||
func (l *LevelDbStorage) LevelDB() *leveldb.DB {
|
func (l *Storage) LevelDB() *leveldb.DB {
|
||||||
return l.ldb
|
return l.ldb
|
||||||
}
|
}
|
||||||
|
|
||||||
// List implements the method List of the interface db.Storage
|
// List implements the method List of the interface db.Storage
|
||||||
func (l *LevelDbStorage) List(limit int) ([]db.KV, error) {
|
func (l *Storage) List(limit int) ([]db.KV, error) {
|
||||||
ret := []db.KV{}
|
ret := []db.KV{}
|
||||||
err := l.Iterate(func(key []byte, value []byte) (bool, error) {
|
err := l.Iterate(func(key []byte, value []byte) (bool, error) {
|
||||||
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ func TestLevelDbInterface(t *testing.T) {
|
|||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
result := m.Run()
|
result := m.Run()
|
||||||
for _, dir := range rmDirs {
|
for _, dir := range rmDirs {
|
||||||
os.RemoveAll(dir)
|
os.RemoveAll(dir) //nolint:errcheck,gosec
|
||||||
}
|
}
|
||||||
os.Exit(result)
|
os.Exit(result)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,41 +7,41 @@ import (
|
|||||||
"github.com/iden3/go-merkletree/db"
|
"github.com/iden3/go-merkletree/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MemoryStorage implements the db.Storage interface
|
// Storage implements the db.Storage interface
|
||||||
type MemoryStorage struct {
|
type Storage struct {
|
||||||
prefix []byte
|
prefix []byte
|
||||||
kv db.KvMap
|
kv db.KvMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// MemoryStorageTx implements the db.Tx interface
|
// StorageTx implements the db.Tx interface
|
||||||
type MemoryStorageTx struct {
|
type StorageTx struct {
|
||||||
s *MemoryStorage
|
s *Storage
|
||||||
kv db.KvMap
|
kv db.KvMap
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMemoryStorage returns a new MemoryStorage
|
// NewMemoryStorage returns a new Storage
|
||||||
func NewMemoryStorage() *MemoryStorage {
|
func NewMemoryStorage() *Storage {
|
||||||
kvmap := make(db.KvMap)
|
kvmap := make(db.KvMap)
|
||||||
return &MemoryStorage{[]byte{}, kvmap}
|
return &Storage{[]byte{}, kvmap}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info implements the method Info of the interface db.Storage
|
// Info implements the method Info of the interface db.Storage
|
||||||
func (m *MemoryStorage) Info() string {
|
func (m *Storage) Info() string {
|
||||||
return "in-memory"
|
return "in-memory"
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
||||||
func (m *MemoryStorage) WithPrefix(prefix []byte) db.Storage {
|
func (m *Storage) WithPrefix(prefix []byte) db.Storage {
|
||||||
return &MemoryStorage{db.Concat(m.prefix, prefix), m.kv}
|
return &Storage{db.Concat(m.prefix, prefix), m.kv}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTx implements the method NewTx of the interface db.Storage
|
// NewTx implements the method NewTx of the interface db.Storage
|
||||||
func (m *MemoryStorage) NewTx() (db.Tx, error) {
|
func (m *Storage) NewTx() (db.Tx, error) {
|
||||||
return &MemoryStorageTx{m, make(db.KvMap)}, nil
|
return &StorageTx{m, make(db.KvMap)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get retreives a value from a key in the db.Storage
|
// Get retreives a value from a key in the db.Storage
|
||||||
func (m *MemoryStorage) Get(key []byte) ([]byte, error) {
|
func (m *Storage) Get(key []byte) ([]byte, error) {
|
||||||
if v, ok := m.kv.Get(db.Concat(m.prefix, key[:])); ok {
|
if v, ok := m.kv.Get(db.Concat(m.prefix, key[:])); ok {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
@@ -49,16 +49,19 @@ func (m *MemoryStorage) Get(key []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterate implements the method Iterate of the interface db.Storage
|
// Iterate implements the method Iterate of the interface db.Storage
|
||||||
func (m *MemoryStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
func (m *Storage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
||||||
kvs := make([]db.KV, 0)
|
kvs := make([]db.KV, 0)
|
||||||
for _, v := range m.kv {
|
for _, v := range m.kv {
|
||||||
if len(v.K) < len(m.prefix) || !bytes.Equal(v.K[:len(m.prefix)], m.prefix) {
|
if len(v.K) < len(m.prefix) ||
|
||||||
|
!bytes.Equal(v.K[:len(m.prefix)], m.prefix) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
localkey := v.K[len(m.prefix):]
|
localkey := v.K[len(m.prefix):]
|
||||||
kvs = append(kvs, db.KV{K: localkey, V: v.V})
|
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 })
|
sort.SliceStable(kvs, func(i, j int) bool {
|
||||||
|
return bytes.Compare(kvs[i].K, kvs[j].K) < 0
|
||||||
|
})
|
||||||
|
|
||||||
for _, kv := range kvs {
|
for _, kv := range kvs {
|
||||||
if cont, err := f(kv.K, kv.V); err != nil {
|
if cont, err := f(kv.K, kv.V); err != nil {
|
||||||
@@ -71,7 +74,7 @@ func (m *MemoryStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get implements the method Get of the interface db.Tx
|
// Get implements the method Get of the interface db.Tx
|
||||||
func (tx *MemoryStorageTx) Get(key []byte) ([]byte, error) {
|
func (tx *StorageTx) Get(key []byte) ([]byte, error) {
|
||||||
if v, ok := tx.kv.Get(db.Concat(tx.s.prefix, key)); ok {
|
if v, ok := tx.kv.Get(db.Concat(tx.s.prefix, key)); ok {
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
@@ -83,13 +86,13 @@ func (tx *MemoryStorageTx) Get(key []byte) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Put implements the method Put of the interface db.Tx
|
// Put implements the method Put of the interface db.Tx
|
||||||
func (tx *MemoryStorageTx) Put(k, v []byte) error {
|
func (tx *StorageTx) Put(k, v []byte) error {
|
||||||
tx.kv.Put(db.Concat(tx.s.prefix, k), v)
|
tx.kv.Put(db.Concat(tx.s.prefix, k), v)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit implements the method Commit of the interface db.Tx
|
// Commit implements the method Commit of the interface db.Tx
|
||||||
func (tx *MemoryStorageTx) Commit() error {
|
func (tx *StorageTx) Commit() error {
|
||||||
for _, v := range tx.kv {
|
for _, v := range tx.kv {
|
||||||
tx.s.kv.Put(v.K, v.V)
|
tx.s.kv.Put(v.K, v.V)
|
||||||
}
|
}
|
||||||
@@ -98,8 +101,8 @@ func (tx *MemoryStorageTx) Commit() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add implements the method Add of the interface db.Tx
|
// Add implements the method Add of the interface db.Tx
|
||||||
func (tx *MemoryStorageTx) Add(atx db.Tx) error {
|
func (tx *StorageTx) Add(atx db.Tx) error {
|
||||||
mstx := atx.(*MemoryStorageTx)
|
mstx := atx.(*StorageTx)
|
||||||
for _, v := range mstx.kv {
|
for _, v := range mstx.kv {
|
||||||
tx.kv.Put(v.K, v.V)
|
tx.kv.Put(v.K, v.V)
|
||||||
}
|
}
|
||||||
@@ -107,16 +110,16 @@ func (tx *MemoryStorageTx) Add(atx db.Tx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Close implements the method Close of the interface db.Tx
|
// Close implements the method Close of the interface db.Tx
|
||||||
func (tx *MemoryStorageTx) Close() {
|
func (tx *StorageTx) Close() {
|
||||||
tx.kv = nil
|
tx.kv = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements the method Close of the interface db.Storage
|
// Close implements the method Close of the interface db.Storage
|
||||||
func (m *MemoryStorage) Close() {
|
func (m *Storage) Close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// List implements the method List of the interface db.Storage
|
// List implements the method List of the interface db.Storage
|
||||||
func (m *MemoryStorage) List(limit int) ([]db.KV, error) {
|
func (m *Storage) List(limit int) ([]db.KV, error) {
|
||||||
ret := []db.KV{}
|
ret := []db.KV{}
|
||||||
err := m.Iterate(func(key []byte, value []byte) (bool, error) {
|
err := m.Iterate(func(key []byte, value []byte) (bool, error) {
|
||||||
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
||||||
|
|||||||
@@ -8,20 +8,20 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PebbleStorage implements the db.Storage interface
|
// Storage implements the db.Storage interface
|
||||||
type PebbleStorage struct {
|
type Storage struct {
|
||||||
pdb *pebble.DB
|
pdb *pebble.DB
|
||||||
prefix []byte
|
prefix []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// PebbleStorageTx implements the db.Tx interface
|
// StorageTx implements the db.Tx interface
|
||||||
type PebbleStorageTx struct {
|
type StorageTx struct {
|
||||||
*PebbleStorage
|
*Storage
|
||||||
batch *pebble.Batch
|
batch *pebble.Batch
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPebbleStorage returns a new PebbleStorage
|
// NewPebbleStorage returns a new Storage
|
||||||
func NewPebbleStorage(path string, errorIfMissing bool) (*PebbleStorage, error) {
|
func NewPebbleStorage(path string, errorIfMissing bool) (*Storage, error) {
|
||||||
o := &pebble.Options{
|
o := &pebble.Options{
|
||||||
ErrorIfNotExists: errorIfMissing,
|
ErrorIfNotExists: errorIfMissing,
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ func NewPebbleStorage(path string, errorIfMissing bool) (*PebbleStorage, error)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &PebbleStorage{rdb, []byte{}}, nil
|
return &Storage{rdb, []byte{}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type storageInfo struct {
|
type storageInfo struct {
|
||||||
@@ -38,7 +38,7 @@ type storageInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Info implements the method Info of the interface db.Storage
|
// Info implements the method Info of the interface db.Storage
|
||||||
func (p *PebbleStorage) Info() string {
|
func (p *Storage) Info() string {
|
||||||
keycount := 0
|
keycount := 0
|
||||||
claimcount := 0
|
claimcount := 0
|
||||||
err := p.Iterate(func(key []byte, value []byte) (bool, error) {
|
err := p.Iterate(func(key []byte, value []byte) (bool, error) {
|
||||||
@@ -63,25 +63,29 @@ func (p *PebbleStorage) Info() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
// WithPrefix implements the method WithPrefix of the interface db.Storage
|
||||||
func (p *PebbleStorage) WithPrefix(prefix []byte) db.Storage {
|
func (p *Storage) WithPrefix(prefix []byte) db.Storage {
|
||||||
return &PebbleStorage{p.pdb, db.Concat(p.prefix, prefix)}
|
return &Storage{p.pdb, db.Concat(p.prefix, prefix)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTx implements the method NewTx of the interface db.Storage
|
// NewTx implements the method NewTx of the interface db.Storage
|
||||||
func (p *PebbleStorage) NewTx() (db.Tx, error) {
|
func (p *Storage) NewTx() (db.Tx, error) {
|
||||||
return &PebbleStorageTx{p, p.pdb.NewIndexedBatch()}, nil
|
return &StorageTx{p, p.pdb.NewIndexedBatch()}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get retreives a value from a key in the db.Storage
|
// Get retreives a value from a key in the db.Storage
|
||||||
func (p *PebbleStorage) Get(key []byte) ([]byte, error) {
|
func (p *Storage) Get(key []byte) ([]byte, error) {
|
||||||
v, closer, err := p.pdb.Get(db.Concat(p.prefix, key[:]))
|
v, closer, err := p.pdb.Get(db.Concat(p.prefix, key[:]))
|
||||||
if err == pebble.ErrNotFound {
|
if err == pebble.ErrNotFound {
|
||||||
return nil, db.ErrNotFound
|
return nil, db.ErrNotFound
|
||||||
}
|
}
|
||||||
closer.Close()
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = closer.Close()
|
||||||
return v, err
|
return v, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//nolint:lll
|
||||||
// https://github.com/cockroachdb/pebble/pull/923/files#diff-c2ade2f386c41794d5ebc57ee49b57a5fca8082e03255e5bff13977cbc061287R39
|
// https://github.com/cockroachdb/pebble/pull/923/files#diff-c2ade2f386c41794d5ebc57ee49b57a5fca8082e03255e5bff13977cbc061287R39
|
||||||
func keyUpperBound(b []byte) []byte {
|
func keyUpperBound(b []byte) []byte {
|
||||||
end := make([]byte, len(b))
|
end := make([]byte, len(b))
|
||||||
@@ -102,7 +106,7 @@ func prefixIterOptions(prefix []byte) *pebble.IterOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterate implements the method Iterate of the interface db.Storage
|
// Iterate implements the method Iterate of the interface db.Storage
|
||||||
func (p *PebbleStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
func (p *Storage) Iterate(f func([]byte, []byte) (bool, error)) (err error) {
|
||||||
// NewIter already provides a point-in-time view of the current DB
|
// NewIter already provides a point-in-time view of the current DB
|
||||||
// state, but if is used for long term (is not the case), should use an
|
// state, but if is used for long term (is not the case), should use an
|
||||||
// iterator over an snapshot:
|
// iterator over an snapshot:
|
||||||
@@ -110,7 +114,13 @@ func (p *PebbleStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
|||||||
// defer snapshot.Close()
|
// defer snapshot.Close()
|
||||||
// iter := snapshot.NewIter(nil)
|
// iter := snapshot.NewIter(nil)
|
||||||
iter := p.pdb.NewIter(prefixIterOptions(p.prefix))
|
iter := p.pdb.NewIter(prefixIterOptions(p.prefix))
|
||||||
defer iter.Close()
|
defer func() {
|
||||||
|
err1 := iter.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = err1
|
||||||
|
}()
|
||||||
|
|
||||||
for iter.First(); iter.Valid(); iter.Next() {
|
for iter.First(); iter.Valid(); iter.Next() {
|
||||||
localKey := iter.Key()[len(p.prefix):]
|
localKey := iter.Key()[len(p.prefix):]
|
||||||
@@ -124,7 +134,7 @@ func (p *PebbleStorage) Iterate(f func([]byte, []byte) (bool, error)) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get retreives a value from a key in the interface db.Tx
|
// Get retreives a value from a key in the interface db.Tx
|
||||||
func (tx *PebbleStorageTx) Get(key []byte) ([]byte, error) {
|
func (tx *StorageTx) Get(key []byte) ([]byte, error) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
fullkey := db.Concat(tx.prefix, key)
|
fullkey := db.Concat(tx.prefix, key)
|
||||||
@@ -133,34 +143,36 @@ func (tx *PebbleStorageTx) Get(key []byte) ([]byte, error) {
|
|||||||
if err == pebble.ErrNotFound {
|
if err == pebble.ErrNotFound {
|
||||||
return nil, db.ErrNotFound
|
return nil, db.ErrNotFound
|
||||||
}
|
}
|
||||||
closer.Close()
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
err = closer.Close()
|
||||||
return v, err
|
return v, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put saves a key:value into the db.Storage
|
// Put saves a key:value into the db.Storage
|
||||||
func (tx *PebbleStorageTx) Put(k, v []byte) error {
|
func (tx *StorageTx) Put(k, v []byte) error {
|
||||||
return tx.batch.Set(db.Concat(tx.prefix, k[:]), v, nil)
|
return tx.batch.Set(db.Concat(tx.prefix, k[:]), v, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add implements the method Add of the interface db.Tx
|
// Add implements the method Add of the interface db.Tx
|
||||||
func (tx *PebbleStorageTx) Add(atx db.Tx) error {
|
func (tx *StorageTx) Add(atx db.Tx) error {
|
||||||
patx := atx.(*PebbleStorageTx)
|
patx := atx.(*StorageTx)
|
||||||
return tx.batch.Apply(patx.batch, nil)
|
return tx.batch.Apply(patx.batch, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commit implements the method Commit of the interface db.Tx
|
// Commit implements the method Commit of the interface db.Tx
|
||||||
func (tx *PebbleStorageTx) Commit() error {
|
func (tx *StorageTx) Commit() error {
|
||||||
return tx.batch.Commit(nil)
|
return tx.batch.Commit(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements the method Close of the interface db.Tx
|
// Close implements the method Close of the interface db.Tx
|
||||||
func (tx *PebbleStorageTx) Close() {
|
func (tx *StorageTx) Close() {
|
||||||
_ = tx.batch.Close()
|
_ = tx.batch.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements the method Close of the interface db.Storage
|
// Close implements the method Close of the interface db.Storage
|
||||||
func (p *PebbleStorage) Close() {
|
func (p *Storage) Close() {
|
||||||
if err := p.pdb.Close(); err != nil {
|
if err := p.pdb.Close(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -168,12 +180,12 @@ func (p *PebbleStorage) Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Pebble is an extra method that returns the *pebble.DB
|
// Pebble is an extra method that returns the *pebble.DB
|
||||||
func (p *PebbleStorage) Pebble() *pebble.DB {
|
func (p *Storage) Pebble() *pebble.DB {
|
||||||
return p.pdb
|
return p.pdb
|
||||||
}
|
}
|
||||||
|
|
||||||
// List implements the method List of the interface db.Storage
|
// List implements the method List of the interface db.Storage
|
||||||
func (p *PebbleStorage) List(limit int) ([]db.KV, error) {
|
func (p *Storage) List(limit int) ([]db.KV, error) {
|
||||||
ret := []db.KV{}
|
ret := []db.KV{}
|
||||||
err := p.Iterate(func(key []byte, value []byte) (bool, error) {
|
err := p.Iterate(func(key []byte, value []byte) (bool, error) {
|
||||||
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
ret = append(ret, db.KV{K: db.Clone(key), V: db.Clone(value)})
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ func TestPebbleInterface(t *testing.T) {
|
|||||||
func TestMain(m *testing.M) {
|
func TestMain(m *testing.M) {
|
||||||
result := m.Run()
|
result := m.Run()
|
||||||
for _, dir := range rmDirs {
|
for _, dir := range rmDirs {
|
||||||
os.RemoveAll(dir)
|
os.RemoveAll(dir) //nolint:errcheck,gosec
|
||||||
}
|
}
|
||||||
os.Exit(result)
|
os.Exit(result)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//nolint:gomnd
|
//nolint:gomnd,golint
|
||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
118
merkletree.go
118
merkletree.go
@@ -54,7 +54,8 @@ var (
|
|||||||
|
|
||||||
dbKeyRootNode = []byte("currentroot")
|
dbKeyRootNode = []byte("currentroot")
|
||||||
// HashZero is used at Empty nodes
|
// HashZero is used at Empty nodes
|
||||||
HashZero = Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
HashZero = Hash{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Hash is the generic type stored in the MerkleTree
|
// Hash is the generic type stored in the MerkleTree
|
||||||
@@ -114,11 +115,13 @@ func (h *Hash) Bytes() []byte {
|
|||||||
// method.
|
// method.
|
||||||
func NewBigIntFromHashBytes(b []byte) (*big.Int, error) {
|
func NewBigIntFromHashBytes(b []byte) (*big.Int, error) {
|
||||||
if len(b) != ElemBytesLen {
|
if len(b) != ElemBytesLen {
|
||||||
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b))
|
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes",
|
||||||
|
len(b))
|
||||||
}
|
}
|
||||||
bi := new(big.Int).SetBytes(b[:ElemBytesLen])
|
bi := new(big.Int).SetBytes(b[:ElemBytesLen])
|
||||||
if !cryptoUtils.CheckBigIntInField(bi) {
|
if !cryptoUtils.CheckBigIntInField(bi) {
|
||||||
return nil, fmt.Errorf("NewBigIntFromHashBytes: Value not inside the Finite Field")
|
return nil,
|
||||||
|
fmt.Errorf("NewBigIntFromHashBytes: Value not inside the Finite Field")
|
||||||
}
|
}
|
||||||
return bi, nil
|
return bi, nil
|
||||||
}
|
}
|
||||||
@@ -135,7 +138,8 @@ func NewHashFromBigInt(b *big.Int) *Hash {
|
|||||||
// that previously has ben generated by the Hash.Bytes() method.
|
// that previously has ben generated by the Hash.Bytes() method.
|
||||||
func NewHashFromBytes(b []byte) (*Hash, error) {
|
func NewHashFromBytes(b []byte) (*Hash, error) {
|
||||||
if len(b) != ElemBytesLen {
|
if len(b) != ElemBytesLen {
|
||||||
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes", len(b))
|
return nil, fmt.Errorf("Expected 32 bytes, found %d bytes",
|
||||||
|
len(b))
|
||||||
}
|
}
|
||||||
var h Hash
|
var h Hash
|
||||||
copy(h[:], common.SwapEndianness(b))
|
copy(h[:], common.SwapEndianness(b))
|
||||||
@@ -281,7 +285,8 @@ func (mt *MerkleTree) Add(k, v *big.Int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AddAndGetCircomProof does an Add, and returns a CircomProcessorProof
|
// AddAndGetCircomProof does an Add, and returns a CircomProcessorProof
|
||||||
func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof, error) {
|
func (mt *MerkleTree) AddAndGetCircomProof(k,
|
||||||
|
v *big.Int) (*CircomProcessorProof, error) {
|
||||||
var cp CircomProcessorProof
|
var cp CircomProcessorProof
|
||||||
cp.Fnc = 2
|
cp.Fnc = 2
|
||||||
cp.OldRoot = mt.rootKey
|
cp.OldRoot = mt.rootKey
|
||||||
@@ -315,24 +320,25 @@ func (mt *MerkleTree) AddAndGetCircomProof(k, v *big.Int) (*CircomProcessorProof
|
|||||||
// pushLeaf recursively pushes an existing oldLeaf down until its path diverges
|
// pushLeaf recursively pushes an existing oldLeaf down until its path diverges
|
||||||
// from newLeaf, at which point both leafs are stored, all while updating the
|
// from newLeaf, at which point both leafs are stored, all while updating the
|
||||||
// path.
|
// path.
|
||||||
func (mt *MerkleTree) pushLeaf(tx db.Tx, newLeaf *Node, oldLeaf *Node,
|
func (mt *MerkleTree) pushLeaf(tx db.Tx, newLeaf *Node, oldLeaf *Node, lvl int,
|
||||||
lvl int, pathNewLeaf []bool, pathOldLeaf []bool) (*Hash, error) {
|
pathNewLeaf []bool, pathOldLeaf []bool) (*Hash, error) {
|
||||||
if lvl > mt.maxLevels-2 {
|
if lvl > mt.maxLevels-2 {
|
||||||
return nil, ErrReachedMaxLevel
|
return nil, ErrReachedMaxLevel
|
||||||
}
|
}
|
||||||
var newNodeMiddle *Node
|
var newNodeMiddle *Node
|
||||||
if pathNewLeaf[lvl] == pathOldLeaf[lvl] { // We need to go deeper!
|
if pathNewLeaf[lvl] == pathOldLeaf[lvl] { // We need to go deeper!
|
||||||
nextKey, err := mt.pushLeaf(tx, newLeaf, oldLeaf, lvl+1, pathNewLeaf, pathOldLeaf)
|
nextKey, err := mt.pushLeaf(tx, newLeaf, oldLeaf, lvl+1,
|
||||||
|
pathNewLeaf, pathOldLeaf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if pathNewLeaf[lvl] {
|
if pathNewLeaf[lvl] { // go right
|
||||||
newNodeMiddle = NewNodeMiddle(&HashZero, nextKey) // go right
|
newNodeMiddle = NewNodeMiddle(&HashZero, nextKey)
|
||||||
} else {
|
} else { // go left
|
||||||
newNodeMiddle = NewNodeMiddle(nextKey, &HashZero) // go left
|
newNodeMiddle = NewNodeMiddle(nextKey, &HashZero)
|
||||||
}
|
}
|
||||||
return mt.addNode(tx, newNodeMiddle)
|
return mt.addNode(tx, newNodeMiddle)
|
||||||
} else {
|
}
|
||||||
oldLeafKey, err := oldLeaf.Key()
|
oldLeafKey, err := oldLeaf.Key()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -347,14 +353,14 @@ func (mt *MerkleTree) pushLeaf(tx db.Tx, newLeaf *Node, oldLeaf *Node,
|
|||||||
} else {
|
} else {
|
||||||
newNodeMiddle = NewNodeMiddle(newLeafKey, oldLeafKey)
|
newNodeMiddle = NewNodeMiddle(newLeafKey, oldLeafKey)
|
||||||
}
|
}
|
||||||
// We can add newLeaf now. We don't need to add oldLeaf because it's already in the tree.
|
// We can add newLeaf now. We don't need to add oldLeaf because it's
|
||||||
|
// already in the tree.
|
||||||
_, err = mt.addNode(tx, newLeaf)
|
_, err = mt.addNode(tx, newLeaf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return mt.addNode(tx, newNodeMiddle)
|
return mt.addNode(tx, newNodeMiddle)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// addLeaf recursively adds a newLeaf in the MT while updating the path.
|
// addLeaf recursively adds a newLeaf in the MT while updating the path.
|
||||||
func (mt *MerkleTree) addLeaf(tx db.Tx, newLeaf *Node, key *Hash,
|
func (mt *MerkleTree) addLeaf(tx db.Tx, newLeaf *Node, key *Hash,
|
||||||
@@ -374,23 +380,27 @@ func (mt *MerkleTree) addLeaf(tx db.Tx, newLeaf *Node, key *Hash,
|
|||||||
return mt.addNode(tx, newLeaf)
|
return mt.addNode(tx, newLeaf)
|
||||||
case NodeTypeLeaf:
|
case NodeTypeLeaf:
|
||||||
nKey := n.Entry[0]
|
nKey := n.Entry[0]
|
||||||
// Check if leaf node found contains the leaf node we are trying to add
|
// Check if leaf node found contains the leaf node we are
|
||||||
|
// trying to add
|
||||||
newLeafKey := newLeaf.Entry[0]
|
newLeafKey := newLeaf.Entry[0]
|
||||||
if bytes.Equal(nKey[:], newLeafKey[:]) {
|
if bytes.Equal(nKey[:], newLeafKey[:]) {
|
||||||
return nil, ErrEntryIndexAlreadyExists
|
return nil, ErrEntryIndexAlreadyExists
|
||||||
}
|
}
|
||||||
pathOldLeaf := getPath(mt.maxLevels, nKey[:])
|
pathOldLeaf := getPath(mt.maxLevels, nKey[:])
|
||||||
// We need to push newLeaf down until its path diverges from n's path
|
// We need to push newLeaf down until its path diverges from
|
||||||
|
// n's path
|
||||||
return mt.pushLeaf(tx, newLeaf, n, lvl, path, pathOldLeaf)
|
return mt.pushLeaf(tx, newLeaf, n, lvl, path, pathOldLeaf)
|
||||||
case NodeTypeMiddle:
|
case NodeTypeMiddle:
|
||||||
// We need to go deeper, continue traversing the tree, left or
|
// We need to go deeper, continue traversing the tree, left or
|
||||||
// right depending on path
|
// right depending on path
|
||||||
var newNodeMiddle *Node
|
var newNodeMiddle *Node
|
||||||
if path[lvl] {
|
if path[lvl] { // go right
|
||||||
nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildR, lvl+1, path) // go right
|
nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildR, lvl+1,
|
||||||
|
path)
|
||||||
newNodeMiddle = NewNodeMiddle(n.ChildL, nextKey)
|
newNodeMiddle = NewNodeMiddle(n.ChildL, nextKey)
|
||||||
} else {
|
} else { // go left
|
||||||
nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildL, lvl+1, path) // go left
|
nextKey, err = mt.addLeaf(tx, newLeaf, n.ChildL, lvl+1,
|
||||||
|
path)
|
||||||
newNodeMiddle = NewNodeMiddle(nextKey, n.ChildR)
|
newNodeMiddle = NewNodeMiddle(nextKey, n.ChildR)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -449,7 +459,8 @@ func (mt *MerkleTree) updateNode(tx db.Tx, n *Node) (*Hash, error) {
|
|||||||
func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) {
|
func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) {
|
||||||
// verfy that k is valid and fit inside the Finite Field.
|
// verfy that k is valid and fit inside the Finite Field.
|
||||||
if !cryptoUtils.CheckBigIntInField(k) {
|
if !cryptoUtils.CheckBigIntInField(k) {
|
||||||
return nil, nil, nil, errors.New("Key not inside the Finite Field")
|
return nil, nil, nil,
|
||||||
|
errors.New("Key not inside the Finite Field")
|
||||||
}
|
}
|
||||||
|
|
||||||
kHash := NewHashFromBigInt(k)
|
kHash := NewHashFromBigInt(k)
|
||||||
@@ -468,9 +479,8 @@ func (mt *MerkleTree) Get(k *big.Int) (*big.Int, *big.Int, []*Hash, error) {
|
|||||||
case NodeTypeLeaf:
|
case NodeTypeLeaf:
|
||||||
if bytes.Equal(kHash[:], n.Entry[0][:]) {
|
if bytes.Equal(kHash[:], n.Entry[0][:]) {
|
||||||
return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, nil
|
return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, nil
|
||||||
} else {
|
|
||||||
return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, ErrKeyNotFound
|
|
||||||
}
|
}
|
||||||
|
return n.Entry[0].BigInt(), n.Entry[1].BigInt(), siblings, ErrKeyNotFound
|
||||||
case NodeTypeMiddle:
|
case NodeTypeMiddle:
|
||||||
if path[i] {
|
if path[i] {
|
||||||
nextKey = n.ChildR
|
nextKey = n.ChildR
|
||||||
@@ -541,7 +551,8 @@ func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
newRootKey, err := mt.recalculatePathUntilRoot(tx, path, newNodeLeaf, siblings)
|
newRootKey, err :=
|
||||||
|
mt.recalculatePathUntilRoot(tx, path, newNodeLeaf, siblings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -555,9 +566,8 @@ func (mt *MerkleTree) Update(k, v *big.Int) (*CircomProcessorProof, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &cp, nil
|
return &cp, nil
|
||||||
} else {
|
|
||||||
return nil, ErrKeyNotFound
|
|
||||||
}
|
}
|
||||||
|
return nil, ErrKeyNotFound
|
||||||
case NodeTypeMiddle:
|
case NodeTypeMiddle:
|
||||||
if path[i] {
|
if path[i] {
|
||||||
nextKey = n.ChildR
|
nextKey = n.ChildR
|
||||||
@@ -619,9 +629,8 @@ func (mt *MerkleTree) Delete(k *big.Int) error {
|
|||||||
// remove and go up with the sibling
|
// remove and go up with the sibling
|
||||||
err = mt.rmAndUpload(tx, path, kHash, siblings)
|
err = mt.rmAndUpload(tx, path, kHash, siblings)
|
||||||
return err
|
return err
|
||||||
} else {
|
|
||||||
return ErrKeyNotFound
|
|
||||||
}
|
}
|
||||||
|
return ErrKeyNotFound
|
||||||
case NodeTypeMiddle:
|
case NodeTypeMiddle:
|
||||||
if path[i] {
|
if path[i] {
|
||||||
nextKey = n.ChildR
|
nextKey = n.ChildR
|
||||||
@@ -638,7 +647,8 @@ func (mt *MerkleTree) Delete(k *big.Int) error {
|
|||||||
return ErrKeyNotFound
|
return ErrKeyNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// rmAndUpload removes the key, and goes up until the root updating all the nodes with the new values.
|
// rmAndUpload removes the key, and goes up until the root updating all the
|
||||||
|
// nodes with the new values.
|
||||||
func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings []*Hash) error {
|
func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings []*Hash) error {
|
||||||
if len(siblings) == 0 {
|
if len(siblings) == 0 {
|
||||||
mt.rootKey = &HashZero
|
mt.rootKey = &HashZero
|
||||||
@@ -671,7 +681,8 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// go up until the root
|
// go up until the root
|
||||||
newRootKey, err := mt.recalculatePathUntilRoot(tx, path, newNode, siblings[:i])
|
newRootKey, err := mt.recalculatePathUntilRoot(tx, path, newNode,
|
||||||
|
siblings[:i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -682,7 +693,8 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// if i==0 (root position), stop and store the sibling of the deleted leaf as root
|
// if i==0 (root position), stop and store the sibling of the
|
||||||
|
// deleted leaf as root
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
mt.rootKey = toUpload
|
mt.rootKey = toUpload
|
||||||
err := mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:])
|
err := mt.dbInsert(tx, dbKeyRootNode, DBEntryTypeRoot, mt.rootKey[:])
|
||||||
@@ -700,7 +712,8 @@ func (mt *MerkleTree) rmAndUpload(tx db.Tx, path []bool, kHash *Hash, siblings [
|
|||||||
}
|
}
|
||||||
|
|
||||||
// recalculatePathUntilRoot recalculates the nodes until the Root
|
// recalculatePathUntilRoot recalculates the nodes until the Root
|
||||||
func (mt *MerkleTree) recalculatePathUntilRoot(tx db.Tx, path []bool, node *Node, siblings []*Hash) (*Hash, error) {
|
func (mt *MerkleTree) recalculatePathUntilRoot(tx db.Tx, path []bool, node *Node,
|
||||||
|
siblings []*Hash) (*Hash, error) {
|
||||||
for i := len(siblings) - 1; i >= 0; i-- {
|
for i := len(siblings) - 1; i >= 0; i-- {
|
||||||
nodeKey, err := node.Key()
|
nodeKey, err := node.Key()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -757,9 +770,11 @@ type NodeAux struct {
|
|||||||
Value *Hash
|
Value *Hash
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proof defines the required elements for a MT proof of existence or non-existence.
|
// Proof defines the required elements for a MT proof of existence or
|
||||||
|
// non-existence.
|
||||||
type Proof struct {
|
type Proof struct {
|
||||||
// existence indicates wether this is a proof of existence or non-existence.
|
// existence indicates wether this is a proof of existence or
|
||||||
|
// non-existence.
|
||||||
Existence bool
|
Existence bool
|
||||||
// depth indicates how deep in the tree the proof goes.
|
// depth indicates how deep in the tree the proof goes.
|
||||||
depth uint
|
depth uint
|
||||||
@@ -872,7 +887,8 @@ type CircomProcessorProof struct {
|
|||||||
NewKey *Hash `json:"newKey"`
|
NewKey *Hash `json:"newKey"`
|
||||||
NewValue *Hash `json:"newValue"`
|
NewValue *Hash `json:"newValue"`
|
||||||
IsOld0 bool `json:"isOld0"`
|
IsOld0 bool `json:"isOld0"`
|
||||||
Fnc int `json:"fnc"` // 0: NOP, 1: Update, 2: Insert, 3: Delete
|
// 0: NOP, 1: Update, 2: Insert, 3: Delete
|
||||||
|
Fnc int `json:"fnc"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a human readable string representation of the
|
// String returns a human readable string representation of the
|
||||||
@@ -912,7 +928,8 @@ type CircomVerifierProof struct {
|
|||||||
// GenerateCircomVerifierProof returns the CircomVerifierProof for a certain
|
// GenerateCircomVerifierProof returns the CircomVerifierProof for a certain
|
||||||
// key in the MerkleTree. If the rootKey is nil, the current merkletree root
|
// key in the MerkleTree. If the rootKey is nil, the current merkletree root
|
||||||
// is used.
|
// is used.
|
||||||
func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, rootKey *Hash) (*CircomVerifierProof, error) {
|
func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int,
|
||||||
|
rootKey *Hash) (*CircomVerifierProof, error) {
|
||||||
if rootKey == nil {
|
if rootKey == nil {
|
||||||
rootKey = mt.Root()
|
rootKey = mt.Root()
|
||||||
}
|
}
|
||||||
@@ -939,7 +956,8 @@ func (mt *MerkleTree) GenerateCircomVerifierProof(k *big.Int, rootKey *Hash) (*C
|
|||||||
// GenerateProof generates the proof of existence (or non-existence) of an
|
// GenerateProof generates the proof of existence (or non-existence) of an
|
||||||
// Entry's hash Index for a Merkle Tree given the root.
|
// Entry's hash Index for a Merkle Tree given the root.
|
||||||
// If the rootKey is nil, the current merkletree root is used
|
// If the rootKey is nil, the current merkletree root is used
|
||||||
func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof, *big.Int, error) {
|
func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof,
|
||||||
|
*big.Int, error) {
|
||||||
p := &Proof{}
|
p := &Proof{}
|
||||||
var siblingKey *Hash
|
var siblingKey *Hash
|
||||||
|
|
||||||
@@ -961,11 +979,10 @@ func (mt *MerkleTree) GenerateProof(k *big.Int, rootKey *Hash) (*Proof, *big.Int
|
|||||||
if bytes.Equal(kHash[:], n.Entry[0][:]) {
|
if bytes.Equal(kHash[:], n.Entry[0][:]) {
|
||||||
p.Existence = true
|
p.Existence = true
|
||||||
return p, n.Entry[1].BigInt(), nil
|
return p, n.Entry[1].BigInt(), nil
|
||||||
} else {
|
}
|
||||||
// We found a leaf whose entry didn't match hIndex
|
// We found a leaf whose entry didn't match hIndex
|
||||||
p.NodeAux = &NodeAux{Key: n.Entry[0], Value: n.Entry[1]}
|
p.NodeAux = &NodeAux{Key: n.Entry[0], Value: n.Entry[1]}
|
||||||
return p, n.Entry[1].BigInt(), nil
|
return p, n.Entry[1].BigInt(), nil
|
||||||
}
|
|
||||||
case NodeTypeMiddle:
|
case NodeTypeMiddle:
|
||||||
if path[p.depth] {
|
if path[p.depth] {
|
||||||
nextKey = n.ChildR
|
nextKey = n.ChildR
|
||||||
@@ -1013,7 +1030,8 @@ func RootFromProof(proof *Proof, k, v *big.Int) (*Hash, error) {
|
|||||||
midKey = &HashZero
|
midKey = &HashZero
|
||||||
} else {
|
} else {
|
||||||
if bytes.Equal(kHash[:], proof.NodeAux.Key[:]) {
|
if bytes.Equal(kHash[:], proof.NodeAux.Key[:]) {
|
||||||
return nil, fmt.Errorf("Non-existence proof being checked against hIndex equal to nodeAux")
|
return nil,
|
||||||
|
fmt.Errorf("Non-existence proof being checked against hIndex equal to nodeAux")
|
||||||
}
|
}
|
||||||
midKey, err = LeafKey(proof.NodeAux.Key, proof.NodeAux.Value)
|
midKey, err = LeafKey(proof.NodeAux.Key, proof.NodeAux.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1071,10 +1089,10 @@ func (mt *MerkleTree) walk(key *Hash, f func(*Node)) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Walk iterates over all the branches of a MerkleTree with the given rootKey
|
// Walk iterates over all the branches of a MerkleTree with the given rootKey
|
||||||
// if rootKey is nil, it will get the current RootKey of the current state of the MerkleTree.
|
// if rootKey is nil, it will get the current RootKey of the current state of
|
||||||
// For each node, it calls the f function given in the parameters.
|
// the MerkleTree. For each node, it calls the f function given in the
|
||||||
// See some examples of the Walk function usage in the merkletree.go and
|
// parameters. See some examples of the Walk function usage in the
|
||||||
// merkletree_test.go
|
// merkletree.go and merkletree_test.go
|
||||||
func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error {
|
func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error {
|
||||||
if rootKey == nil {
|
if rootKey == nil {
|
||||||
rootKey = mt.Root()
|
rootKey = mt.Root()
|
||||||
@@ -1083,8 +1101,8 @@ func (mt *MerkleTree) Walk(rootKey *Hash, f func(*Node)) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GraphViz uses Walk function to generate a string GraphViz representation of the
|
// GraphViz uses Walk function to generate a string GraphViz representation of
|
||||||
// tree and writes it to w
|
// the tree and writes it to w
|
||||||
func (mt *MerkleTree) GraphViz(w io.Writer, rootKey *Hash) error {
|
func (mt *MerkleTree) GraphViz(w io.Writer, rootKey *Hash) error {
|
||||||
fmt.Fprintf(w, `digraph hierarchy {
|
fmt.Fprintf(w, `digraph hierarchy {
|
||||||
node [fontname=Monospace,fontsize=10,shape=box]
|
node [fontname=Monospace,fontsize=10,shape=box]
|
||||||
@@ -1128,12 +1146,14 @@ func (mt *MerkleTree) PrintGraphViz(rootKey *Hash) error {
|
|||||||
rootKey = mt.Root()
|
rootKey = mt.Root()
|
||||||
}
|
}
|
||||||
w := bytes.NewBufferString("")
|
w := bytes.NewBufferString("")
|
||||||
fmt.Fprintf(w, "--------\nGraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n")
|
fmt.Fprintf(w,
|
||||||
|
"--------\nGraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n")
|
||||||
err := mt.GraphViz(w, nil)
|
err := mt.GraphViz(w, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "End of GraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n--------\n")
|
fmt.Fprintf(w,
|
||||||
|
"End of GraphViz of the MerkleTree with RootKey "+rootKey.BigInt().String()+"\n--------\n")
|
||||||
|
|
||||||
fmt.Println(w)
|
fmt.Println(w)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -43,10 +43,10 @@ func TestHashParsers(t *testing.T) {
|
|||||||
h8l := NewHashFromBigInt(big.NewInt(12345678))
|
h8l := NewHashFromBigInt(big.NewInt(12345678))
|
||||||
assert.Equal(t, "12345678...", h8l.String())
|
assert.Equal(t, "12345678...", h8l.String())
|
||||||
|
|
||||||
b, ok := new(big.Int).SetString("4932297968297298434239270129193057052722409868268166443802652458940273154854", 10)
|
b, ok := new(big.Int).SetString("4932297968297298434239270129193057052722409868268166443802652458940273154854", 10) //nolint:lll
|
||||||
assert.True(t, ok)
|
assert.True(t, ok)
|
||||||
h := NewHashFromBigInt(b)
|
h := NewHashFromBigInt(b)
|
||||||
assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154854", h.BigInt().String())
|
assert.Equal(t, "4932297968297298434239270129193057052722409868268166443802652458940273154854", h.BigInt().String()) //nolint:lll
|
||||||
assert.Equal(t, "49322979...", h.String())
|
assert.Equal(t, "49322979...", h.String())
|
||||||
assert.Equal(t, "265baaf161e875c372d08e50f52abddc01d32efc93e90290bb8b3d9ceb94e70a", h.Hex())
|
assert.Equal(t, "265baaf161e875c372d08e50f52abddc01d32efc93e90290bb8b3d9ceb94e70a", h.Hex())
|
||||||
|
|
||||||
@@ -98,15 +98,15 @@ func TestNewTree(t *testing.T) {
|
|||||||
// test vectors generated using https://github.com/iden3/circomlib smt.js
|
// test vectors generated using https://github.com/iden3/circomlib smt.js
|
||||||
err = mt.Add(big.NewInt(1), big.NewInt(2))
|
err = mt.Add(big.NewInt(1), big.NewInt(2))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "6449712043256457369579901840927028403950625973089336675272087704159094984964", mt.Root().BigInt().String())
|
assert.Equal(t, "6449712043256457369579901840927028403950625973089336675272087704159094984964", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
err = mt.Add(big.NewInt(33), big.NewInt(44))
|
err = mt.Add(big.NewInt(33), big.NewInt(44))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String())
|
assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
err = mt.Add(big.NewInt(1234), big.NewInt(9876))
|
err = mt.Add(big.NewInt(1234), big.NewInt(9876))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "12841932325181810040554102151615400973767747666110051836366805309524360490677", mt.Root().BigInt().String())
|
assert.Equal(t, "12841932325181810040554102151615400973767747666110051836366805309524360490677", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
dbRoot, err := mt.dbGetRoot()
|
dbRoot, err := mt.dbGetRoot()
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
@@ -142,7 +142,7 @@ func TestAddDifferentOrder(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, mt1.Root().Hex(), mt2.Root().Hex())
|
assert.Equal(t, mt1.Root().Hex(), mt2.Root().Hex())
|
||||||
assert.Equal(t, "268e25964aa9d6ba42d66ae9eb44b5528540acb19a3644d1367d8c6f7cb23006", mt1.Root().Hex())
|
assert.Equal(t, "268e25964aa9d6ba42d66ae9eb44b5528540acb19a3644d1367d8c6f7cb23006", mt1.Root().Hex()) //nolint:lll
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddRepeatedIndex(t *testing.T) {
|
func TestAddRepeatedIndex(t *testing.T) {
|
||||||
@@ -299,12 +299,24 @@ func TestSiblingsFromProof(t *testing.T) {
|
|||||||
|
|
||||||
siblings := SiblingsFromProof(proof)
|
siblings := SiblingsFromProof(proof)
|
||||||
assert.Equal(t, 6, len(siblings))
|
assert.Equal(t, 6, len(siblings))
|
||||||
assert.Equal(t, "5b478bdd58595ead03ebf494a74014cbb576ba0d9456aa0916885b9eefae592f", siblings[0].Hex())
|
assert.Equal(t,
|
||||||
assert.Equal(t, "c1e8ab120a4e475ea1bf00633228bfb9d248f7ddec2aa6367f98d0defb9fb22e", siblings[1].Hex())
|
"5b478bdd58595ead03ebf494a74014cbb576ba0d9456aa0916885b9eefae592f",
|
||||||
assert.Equal(t, "f4dafd8ac2b9165adc3f6d125af67d5a4d8a7a263dcc90a373d0338929e16e0c", siblings[2].Hex())
|
siblings[0].Hex())
|
||||||
assert.Equal(t, "a94aa346bd85f96aba2e85b67920e44fe6ed767b0e13bea602784e0b8b897515", siblings[3].Hex())
|
assert.Equal(t,
|
||||||
assert.Equal(t, "54791d7514030ded79301dbf221f5bf186facbc5800912411852fdc101b7151d", siblings[4].Hex())
|
"c1e8ab120a4e475ea1bf00633228bfb9d248f7ddec2aa6367f98d0defb9fb22e",
|
||||||
assert.Equal(t, "435d28bc0511f8feb93b5f1649a049b460947702ce0baaefcf596175370fe01e", siblings[5].Hex())
|
siblings[1].Hex())
|
||||||
|
assert.Equal(t,
|
||||||
|
"f4dafd8ac2b9165adc3f6d125af67d5a4d8a7a263dcc90a373d0338929e16e0c",
|
||||||
|
siblings[2].Hex())
|
||||||
|
assert.Equal(t,
|
||||||
|
"a94aa346bd85f96aba2e85b67920e44fe6ed767b0e13bea602784e0b8b897515",
|
||||||
|
siblings[3].Hex())
|
||||||
|
assert.Equal(t,
|
||||||
|
"54791d7514030ded79301dbf221f5bf186facbc5800912411852fdc101b7151d",
|
||||||
|
siblings[4].Hex())
|
||||||
|
assert.Equal(t,
|
||||||
|
"435d28bc0511f8feb93b5f1649a049b460947702ce0baaefcf596175370fe01e",
|
||||||
|
siblings[5].Hex())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVerifyProofCases(t *testing.T) {
|
func TestVerifyProofCases(t *testing.T) {
|
||||||
@@ -318,14 +330,13 @@ func TestVerifyProofCases(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Existence proof
|
// Existence proof
|
||||||
|
|
||||||
proof, _, err := mt.GenerateProof(big.NewInt(4), nil)
|
proof, _, err := mt.GenerateProof(big.NewInt(4), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
assert.Equal(t, proof.Existence, true)
|
assert.Equal(t, proof.Existence, true)
|
||||||
assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(4), big.NewInt(0)))
|
assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(4), big.NewInt(0)))
|
||||||
assert.Equal(t, "0003000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730211ddfcc8d30ebd157d1d6912769b8e4abdca41e5dc2b57b026a361c091a8c14c748530e61bf8ea80c987657c3d24b134ece1ef8e2d4bd3f74437bf4392a6b1e", hex.EncodeToString(proof.Bytes()))
|
assert.Equal(t, "0003000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730211ddfcc8d30ebd157d1d6912769b8e4abdca41e5dc2b57b026a361c091a8c14c748530e61bf8ea80c987657c3d24b134ece1ef8e2d4bd3f74437bf4392a6b1e", hex.EncodeToString(proof.Bytes())) //nolint:lll
|
||||||
|
|
||||||
for i := 8; i < 32; i++ {
|
for i := 8; i < 32; i++ {
|
||||||
proof, _, err = mt.GenerateProof(big.NewInt(int64(i)), nil)
|
proof, _, err = mt.GenerateProof(big.NewInt(int64(i)), nil)
|
||||||
@@ -342,7 +353,7 @@ func TestVerifyProofCases(t *testing.T) {
|
|||||||
assert.Equal(t, proof.Existence, false)
|
assert.Equal(t, proof.Existence, false)
|
||||||
// assert.True(t, proof.nodeAux == nil)
|
// assert.True(t, proof.nodeAux == nil)
|
||||||
assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(12), big.NewInt(0)))
|
assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(12), big.NewInt(0)))
|
||||||
assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730211ddfcc8d30ebd157d1d6912769b8e4abdca41e5dc2b57b026a361c091a8c14c748530e61bf8ea80c987657c3d24b134ece1ef8e2d4bd3f74437bf4392a6b1e04000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes()))
|
assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730211ddfcc8d30ebd157d1d6912769b8e4abdca41e5dc2b57b026a361c091a8c14c748530e61bf8ea80c987657c3d24b134ece1ef8e2d4bd3f74437bf4392a6b1e04000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes())) //nolint:lll
|
||||||
|
|
||||||
// Non-existence proof, diff. node aux
|
// Non-existence proof, diff. node aux
|
||||||
proof, _, err = mt.GenerateProof(big.NewInt(10), nil)
|
proof, _, err = mt.GenerateProof(big.NewInt(10), nil)
|
||||||
@@ -352,7 +363,7 @@ func TestVerifyProofCases(t *testing.T) {
|
|||||||
assert.Equal(t, proof.Existence, false)
|
assert.Equal(t, proof.Existence, false)
|
||||||
assert.True(t, proof.NodeAux != nil)
|
assert.True(t, proof.NodeAux != nil)
|
||||||
assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(10), big.NewInt(0)))
|
assert.True(t, VerifyProof(mt.Root(), proof, big.NewInt(10), big.NewInt(0)))
|
||||||
assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730e667e2ca15909c4a23beff18e3cc74348fbd3c1a4c765a5bbbca126c9607a42b77e008a73926f1280f8531b139dc1cacf8d83fcec31d405f5c51b7cbddfe152902000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes()))
|
assert.Equal(t, "0303000000000000000000000000000000000000000000000000000000000007a6d6b46fefe213a6b579844a1bb7ab5c2db4a13f8662d9c5e729c36728f42730e667e2ca15909c4a23beff18e3cc74348fbd3c1a4c765a5bbbca126c9607a42b77e008a73926f1280f8531b139dc1cacf8d83fcec31d405f5c51b7cbddfe152902000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(proof.Bytes())) //nolint:lll
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestVerifyProofFalse(t *testing.T) {
|
func TestVerifyProofFalse(t *testing.T) {
|
||||||
@@ -383,7 +394,8 @@ func TestVerifyProofFalse(t *testing.T) {
|
|||||||
// Now we change the proof from existence to non-existence, and add e's
|
// Now we change the proof from existence to non-existence, and add e's
|
||||||
// data as auxiliary node.
|
// data as auxiliary node.
|
||||||
proof.Existence = false
|
proof.Existence = false
|
||||||
proof.NodeAux = &NodeAux{Key: NewHashFromBigInt(big.NewInt(int64(4))), Value: NewHashFromBigInt(big.NewInt(4))}
|
proof.NodeAux = &NodeAux{Key: NewHashFromBigInt(big.NewInt(int64(4))),
|
||||||
|
Value: NewHashFromBigInt(big.NewInt(4))}
|
||||||
assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(int64(4)), big.NewInt(0)))
|
assert.True(t, !VerifyProof(mt.Root(), proof, big.NewInt(int64(4)), big.NewInt(0)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,22 +447,22 @@ func TestDelete(t *testing.T) {
|
|||||||
// test vectors generated using https://github.com/iden3/circomlib smt.js
|
// test vectors generated using https://github.com/iden3/circomlib smt.js
|
||||||
err = mt.Add(big.NewInt(1), big.NewInt(2))
|
err = mt.Add(big.NewInt(1), big.NewInt(2))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "6449712043256457369579901840927028403950625973089336675272087704159094984964", mt.Root().BigInt().String())
|
assert.Equal(t, "6449712043256457369579901840927028403950625973089336675272087704159094984964", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
err = mt.Add(big.NewInt(33), big.NewInt(44))
|
err = mt.Add(big.NewInt(33), big.NewInt(44))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String())
|
assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
err = mt.Add(big.NewInt(1234), big.NewInt(9876))
|
err = mt.Add(big.NewInt(1234), big.NewInt(9876))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "12841932325181810040554102151615400973767747666110051836366805309524360490677", mt.Root().BigInt().String())
|
assert.Equal(t, "12841932325181810040554102151615400973767747666110051836366805309524360490677", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
// mt.PrintGraphViz(nil)
|
// mt.PrintGraphViz(nil)
|
||||||
|
|
||||||
err = mt.Delete(big.NewInt(33))
|
err = mt.Delete(big.NewInt(33))
|
||||||
// mt.PrintGraphViz(nil)
|
// mt.PrintGraphViz(nil)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "16195585003843604118922861401064871511855368913846540536604351220077317790615", mt.Root().BigInt().String())
|
assert.Equal(t, "16195585003843604118922861401064871511855368913846540536604351220077317790615", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
err = mt.Delete(big.NewInt(1234))
|
err = mt.Delete(big.NewInt(1234))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@@ -507,10 +519,10 @@ func TestDelete3(t *testing.T) {
|
|||||||
err = mt.Add(big.NewInt(2), big.NewInt(2))
|
err = mt.Add(big.NewInt(2), big.NewInt(2))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "6701939280963330813043570145125351311131831356446202146710280245621673558344", mt.Root().BigInt().String())
|
assert.Equal(t, "6701939280963330813043570145125351311131831356446202146710280245621673558344", mt.Root().BigInt().String()) //nolint:lll
|
||||||
err = mt.Delete(big.NewInt(1))
|
err = mt.Delete(big.NewInt(1))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "10304354743004778619823249005484018655542356856535590307973732141291410579841", mt.Root().BigInt().String())
|
assert.Equal(t, "10304354743004778619823249005484018655542356856535590307973732141291410579841", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
mt2 := newTestingMerkle(t, 140)
|
mt2 := newTestingMerkle(t, 140)
|
||||||
defer mt2.db.Close()
|
defer mt2.db.Close()
|
||||||
@@ -532,10 +544,10 @@ func TestDelete4(t *testing.T) {
|
|||||||
err = mt.Add(big.NewInt(3), big.NewInt(3))
|
err = mt.Add(big.NewInt(3), big.NewInt(3))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "6989694633650442615746486460134957295274675622748484439660143938730686550248", mt.Root().BigInt().String())
|
assert.Equal(t, "6989694633650442615746486460134957295274675622748484439660143938730686550248", mt.Root().BigInt().String()) //nolint:lll
|
||||||
err = mt.Delete(big.NewInt(1))
|
err = mt.Delete(big.NewInt(1))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "1192610901536912535888866440319084773171371421781091005185759505381507049136", mt.Root().BigInt().String())
|
assert.Equal(t, "1192610901536912535888866440319084773171371421781091005185759505381507049136", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
mt2 := newTestingMerkle(t, 140)
|
mt2 := newTestingMerkle(t, 140)
|
||||||
defer mt2.db.Close()
|
defer mt2.db.Close()
|
||||||
@@ -554,11 +566,11 @@ func TestDelete5(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
err = mt.Add(big.NewInt(33), big.NewInt(44))
|
err = mt.Add(big.NewInt(33), big.NewInt(44))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String())
|
assert.Equal(t, "11404118908468506234838877883514126008995570353394659302846433035311596046064", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
err = mt.Delete(big.NewInt(1))
|
err = mt.Delete(big.NewInt(1))
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, "12802904154263054831102426711825443668153853847661287611768065280921698471037", mt.Root().BigInt().String())
|
assert.Equal(t, "12802904154263054831102426711825443668153853847661287611768065280921698471037", mt.Root().BigInt().String()) //nolint:lll
|
||||||
|
|
||||||
mt2 := newTestingMerkle(t, 140)
|
mt2 := newTestingMerkle(t, 140)
|
||||||
defer mt2.db.Close()
|
defer mt2.db.Close()
|
||||||
@@ -689,7 +701,9 @@ func TestUpdateCircomProcessorProof(t *testing.T) {
|
|||||||
assert.Equal(t, "10", cpp.NewKey.String())
|
assert.Equal(t, "10", cpp.NewKey.String())
|
||||||
assert.Equal(t, "1024", cpp.NewValue.String())
|
assert.Equal(t, "1024", cpp.NewValue.String())
|
||||||
assert.Equal(t, false, cpp.IsOld0)
|
assert.Equal(t, false, cpp.IsOld0)
|
||||||
assert.Equal(t, "[19625419... 46910949... 18399594... 20473908... 0 0 0 0 0 0 0]", fmt.Sprintf("%v", cpp.Siblings))
|
assert.Equal(t,
|
||||||
|
"[19625419... 46910949... 18399594... 20473908... 0 0 0 0 0 0 0]",
|
||||||
|
fmt.Sprintf("%v", cpp.Siblings))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTypesMarshalers(t *testing.T) {
|
func TestTypesMarshalers(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user