Skip to content

Commit

Permalink
Merge pull request #2 from iden3/no_transactions
Browse files Browse the repository at this point in the history
Remove transactions
  • Loading branch information
OBrezhniev authored Nov 18, 2021
2 parents f069a77 + ef9db66 commit 5f1aba5
Show file tree
Hide file tree
Showing 17 changed files with 648 additions and 823 deletions.
46 changes: 44 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
# matrix strategy from: https://github.com/mvdan/github-actions-golang/blob/master/.github/workflows/test.yml
strategy:
matrix:
go-version: [1.13.x, 1.14.x]
go-version: [1.16.x, 1.17.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand All @@ -15,5 +15,47 @@ jobs:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: |
~/.cache/go-build
/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run tests
run: go test -race ./...
sql-test:
strategy:
matrix:
containers:
- 1.16-bullseye
- 1.17-bullseye
runs-on: ubuntu-latest
container: golang:${{ matrix.containers }}
env:
PGPASSWORD: pgpwd
PGHOST: postgres
PGUSER: postgres
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: |
~/.cache/go-build
/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run tests
run: go test ./...
run: cd db/sql && go test -race ./...
services:
postgres:
image: postgres:13.3
env:
POSTGRES_PASSWORD: pgpwd
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ linters:
- gosec
- gci
- misspell
- gomnd
- gofmt
- goimports
- lll
- golint
# - revive
linters-settings:
lll:
line-length: 100
29 changes: 7 additions & 22 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package merkletree

import (
"bytes"
"context"
"crypto/sha256"
"errors"
)
Expand All @@ -14,29 +15,13 @@ var ErrNotFound = errors.New("key not found")
// the merkletree. Examples of the interface implementation can be found at
// db/memory and db/leveldb directories.
type Storage interface {
NewTx() (Tx, error)
WithPrefix(prefix []byte) Storage
Get([]byte) (*Node, error)
GetRoot() (*Hash, error)
List(int) ([]KV, error)
Close()
Iterate(func([]byte, *Node) (bool, error)) error
}

// Tx is the interface that defines the methods for the db transaction used in
// the merkletree storage. Examples of the interface implementation can be
// found at db/memory and db/leveldb directories.
type Tx interface {
// Get retrieves the value for the given key
// looking first in the content of the Tx, and
// then into the content of the Storage
Get([]byte) (*Node, error)
GetRoot() (*Hash, error)
SetRoot(*Hash) error
// Put sets the key & value into the Tx
Put(k []byte, v *Node) error
Commit() error
Close()
Get(context.Context, []byte) (*Node, error)
Put(ctx context.Context, k []byte, v *Node) error
GetRoot(context.Context) (*Hash, error)
SetRoot(context.Context, *Hash) error
List(context.Context, int) ([]KV, error)
Iterate(context.Context, func([]byte, *Node) (bool, error)) error
}

// KV contains a key (K) and a value (V)
Expand Down
114 changes: 31 additions & 83 deletions db/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package memory

import (
"bytes"
"context"
"sort"

"github.com/iden3/go-merkletree-sql"
Expand All @@ -14,13 +15,6 @@ type Storage struct {
currentRoot *merkletree.Hash
}

// StorageTx implements the db.Tx interface
type StorageTx struct {
s *Storage
kv merkletree.KvMap
currentRoot *merkletree.Hash
}

// NewMemoryStorage returns a new Storage
func NewMemoryStorage() *Storage {
kvmap := make(merkletree.KvMap)
Expand All @@ -32,20 +26,23 @@ func (m *Storage) WithPrefix(prefix []byte) merkletree.Storage {
return &Storage{merkletree.Concat(m.prefix, prefix), m.kv, nil}
}

// NewTx implements the method NewTx of the interface db.Storage
func (m *Storage) NewTx() (merkletree.Tx, error) {
return &StorageTx{m, make(merkletree.KvMap), nil}, nil
}

// Get retrieves a value from a key in the db.Storage
func (m *Storage) Get(key []byte) (*merkletree.Node, error) {
func (m *Storage) Get(_ context.Context, key []byte) (*merkletree.Node, error) {
if v, ok := m.kv.Get(merkletree.Concat(m.prefix, key[:])); ok {
return &v, nil
}
return nil, merkletree.ErrNotFound
}

func (m *Storage) GetRoot() (*merkletree.Hash, error) {
// Put inserts new node into merkletree
func (m *Storage) Put(_ context.Context, key []byte,
node *merkletree.Node) error {
m.kv.Put(merkletree.Concat(m.prefix, key), *node)
return nil
}

// GetRoot returns current merkletree root
func (m *Storage) GetRoot(_ context.Context) (*merkletree.Hash, error) {
if m.currentRoot != nil {
hash := merkletree.Hash{}
copy(hash[:], m.currentRoot[:])
Expand All @@ -54,8 +51,17 @@ func (m *Storage) GetRoot() (*merkletree.Hash, error) {
return nil, merkletree.ErrNotFound
}

// SetRoot updates current merkletree root
func (m *Storage) SetRoot(_ context.Context, hash *merkletree.Hash) error {
root := &merkletree.Hash{}
copy(root[:], hash[:])
m.currentRoot = root
return nil
}

// Iterate implements the method Iterate of the interface db.Storage
func (m *Storage) Iterate(f func([]byte, *merkletree.Node) (bool, error)) error {
func (m *Storage) Iterate(_ context.Context,
f func([]byte, *merkletree.Node) (bool, error)) error {
kvs := make([]merkletree.KV, 0)
for _, v := range m.kv {
if len(v.K) < len(m.prefix) ||
Expand All @@ -79,75 +85,17 @@ func (m *Storage) Iterate(f func([]byte, *merkletree.Node) (bool, error)) error
return nil
}

// Get implements the method Get of the interface db.Tx
func (tx *StorageTx) Get(key []byte) (*merkletree.Node, error) {
if v, ok := tx.kv.Get(merkletree.Concat(tx.s.prefix, key)); ok {
return &v, nil
}
if v, ok := tx.s.kv.Get(merkletree.Concat(tx.s.prefix, key)); ok {
return &v, nil
}

return nil, merkletree.ErrNotFound
}

// Put implements the method Put of the interface db.Tx
func (tx *StorageTx) Put(k []byte, v *merkletree.Node) error {
tx.kv.Put(merkletree.Concat(tx.s.prefix, k), *v)
return nil
}

func (tx *StorageTx) GetRoot() (*merkletree.Hash, error) {
if tx.currentRoot != nil {
hash := merkletree.Hash{}
copy(hash[:], tx.currentRoot[:])
return &hash, nil
}
return nil, merkletree.ErrNotFound
}

// SetRoot sets a hash of merkle tree root in the interface db.Tx
func (tx *StorageTx) SetRoot(hash *merkletree.Hash) error {

// TODO: do tx.Put('currentroot', hash) here ?

root := &merkletree.Hash{}
copy(root[:], hash[:])
tx.currentRoot = root
return nil
}

// Commit implements the method Commit of the interface db.Tx
func (tx *StorageTx) Commit() error {
for _, v := range tx.kv {
tx.s.kv.Put(v.K, v.V)
}
//if tx.currentRoot == nil {
// tx.currentRoot = &merkletree.Hash{}
//}
tx.s.currentRoot = tx.currentRoot
tx.kv = nil
return nil
}

// Close implements the method Close of the interface db.Tx
func (tx *StorageTx) Close() {
tx.kv = nil
}

// Close implements the method Close of the interface db.Storage
func (m *Storage) Close() {
}

// List implements the method List of the interface db.Storage
func (m *Storage) List(limit int) ([]merkletree.KV, error) {
func (m *Storage) List(ctx context.Context,
limit int) ([]merkletree.KV, error) {
ret := []merkletree.KV{}
err := m.Iterate(func(key []byte, value *merkletree.Node) (bool, error) {
ret = append(ret, merkletree.KV{K: merkletree.Clone(key), V: *value})
if len(ret) == limit {
return false, nil
}
return true, nil
})
err := m.Iterate(ctx,
func(key []byte, value *merkletree.Node) (bool, error) {
ret = append(ret, merkletree.KV{K: merkletree.Clone(key), V: *value})
if len(ret) == limit {
return false, nil
}
return true, nil
})
return ret, err
}
2 changes: 2 additions & 0 deletions db/sql/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require (
github.com/iden3/go-merkletree-sql v1.0.0-pre5
github.com/jmoiron/sqlx v1.3.4
github.com/lib/pq v1.2.0
github.com/olomix/go-test-pg v1.0.1
github.com/stretchr/testify v1.6.1
)

replace github.com/iden3/go-merkletree-sql => ../../
Loading

0 comments on commit 5f1aba5

Please sign in to comment.