mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Add StateDB & LocalStateDB
This commit is contained in:
215
db/statedb/statedb.go
Normal file
215
db/statedb/statedb.go
Normal file
@@ -0,0 +1,215 @@
|
||||
package statedb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/iden3/go-merkletree"
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
"github.com/iden3/go-merkletree/db/leveldb"
|
||||
"github.com/iden3/go-merkletree/db/memory"
|
||||
)
|
||||
|
||||
// ErrStateDBWithoutMT is used when a method that requires a MerkleTree is called in a StateDB that does not have a MerkleTree defined
|
||||
var ErrStateDBWithoutMT = errors.New("Can not call method to use MerkleTree in a StateDB without MerkleTree")
|
||||
|
||||
// ErrAccountAlreadyExists is used when CreateAccount is called and the Account already exists
|
||||
var ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account already exists")
|
||||
|
||||
// StateDB represents the StateDB object
|
||||
type StateDB struct {
|
||||
db db.Storage
|
||||
mt *merkletree.MerkleTree
|
||||
}
|
||||
|
||||
// NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk
|
||||
// storage
|
||||
func NewStateDB(path string, inDisk bool, withMT bool, nLevels int) (*StateDB, error) {
|
||||
var sto db.Storage
|
||||
var err error
|
||||
if inDisk {
|
||||
sto, err = leveldb.NewLevelDbStorage(path, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
sto = memory.NewMemoryStorage()
|
||||
}
|
||||
var mt *merkletree.MerkleTree = nil
|
||||
if withMT {
|
||||
mt, err = merkletree.NewMerkleTree(sto, nLevels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &StateDB{
|
||||
db: sto,
|
||||
mt: mt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// CheckPointAt does a checkpoint at the given batchNum in the defined path
|
||||
func (s *StateDB) CheckPointAt(batchNum int, path string) error {
|
||||
// TODO
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reset resets the StateDB to the checkpoint at the given batchNum
|
||||
func (s *StateDB) Reset(batchNum int) error {
|
||||
// TODO
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Checkpoints returns a list of the checkpoints (batchNums)
|
||||
func (s *StateDB) Checkpoints() ([]int, error) {
|
||||
// TODO
|
||||
|
||||
//batchnums, err
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// GetAccount returns the account for the given Idx
|
||||
func (s *StateDB) GetAccount(idx common.Idx) (*common.Account, error) {
|
||||
vBytes, err := s.db.Get(idx.Bytes())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accBytes, err := s.db.Get(vBytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var b [32 * common.NLEAFELEMS]byte
|
||||
copy(b[:], accBytes)
|
||||
return common.AccountFromBytes(b)
|
||||
}
|
||||
|
||||
// CreateAccount creates a new Account in the StateDB for the given Idx.
|
||||
// MerkleTree is not affected.
|
||||
func (s *StateDB) CreateAccount(idx common.Idx, account *common.Account) error {
|
||||
// store at the DB the key: v, and value: leaf.Bytes()
|
||||
v, err := account.HashValue()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
accountBytes, err := account.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// store the Leaf value
|
||||
tx, err := s.db.NewTx()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.Get(idx.Bytes())
|
||||
if err != db.ErrNotFound {
|
||||
return ErrAccountAlreadyExists
|
||||
}
|
||||
|
||||
tx.Put(v.Bytes(), accountBytes[:])
|
||||
tx.Put(idx.Bytes(), v.Bytes())
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// UpdateAccount updates the Account in the StateDB for the given Idx.
|
||||
// MerkleTree is not affected.
|
||||
func (s *StateDB) UpdateAccount(idx common.Idx, account *common.Account) error {
|
||||
// store at the DB the key: v, and value: leaf.Bytes()
|
||||
v, err := account.HashValue()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
accountBytes, err := account.Bytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tx, err := s.db.NewTx()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx.Put(v.Bytes(), accountBytes[:])
|
||||
tx.Put(idx.Bytes(), v.Bytes())
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// MTCreateAccount creates a new the Account in the StateDB for the given Idx,
|
||||
// and updates the MerkleTree, returning a CircomProcessorProof
|
||||
func (s *StateDB) MTCreateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
|
||||
if s.mt == nil {
|
||||
return nil, ErrStateDBWithoutMT
|
||||
}
|
||||
err := s.CreateAccount(idx, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, err := account.HashValue() // already computed in s.CreateAccount, next iteration reuse first computation
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Add k & v into the MT
|
||||
return s.mt.AddAndGetCircomProof(idx.BigInt(), v)
|
||||
}
|
||||
|
||||
// MTUpdateAccount updates the Account in the StateDB for the given Idx, and
|
||||
// updates the MerkleTree, returning a CircomProcessorProof
|
||||
func (s *StateDB) MTUpdateAccount(idx common.Idx, account *common.Account) (*merkletree.CircomProcessorProof, error) {
|
||||
if s.mt == nil {
|
||||
return nil, ErrStateDBWithoutMT
|
||||
}
|
||||
err := s.UpdateAccount(idx, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
v, err := account.HashValue() // already computed in s.CreateAccount, next iteration reuse first computation
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Add k & v into the MT
|
||||
return s.mt.Update(idx.BigInt(), v)
|
||||
}
|
||||
|
||||
// MTGetProof returns the CircomVerifierProof for a given Idx
|
||||
func (s *StateDB) MTGetProof(idx common.Idx) (*merkletree.CircomVerifierProof, error) {
|
||||
if s.mt == nil {
|
||||
return nil, ErrStateDBWithoutMT
|
||||
}
|
||||
return s.mt.GenerateCircomVerifierProof(idx.BigInt(), s.mt.Root())
|
||||
}
|
||||
|
||||
// LocalStateDB represents the local StateDB which allows to make copies from
|
||||
// the synchronizer StateDB, and is used by the tx-selector and the
|
||||
// batch-builder. LocalStateDB is an in-memory storage.
|
||||
type LocalStateDB struct {
|
||||
*StateDB
|
||||
synchronizerStateDB *StateDB
|
||||
}
|
||||
|
||||
// NewLocalStateDB returns a new LocalStateDB connected to the given
|
||||
// synchronizerDB
|
||||
func NewLocalStateDB(synchronizerDB *StateDB, withMT bool, nLevels int) (*LocalStateDB, error) {
|
||||
s, err := NewStateDB("", false, withMT, nLevels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &LocalStateDB{
|
||||
s,
|
||||
synchronizerDB,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Reset performs a reset, getting the state from
|
||||
// LocalStateDB.synchronizerStateDB for the given batchNum
|
||||
func (l *LocalStateDB) Reset(batchNum int, fromSynchronizer bool) error {
|
||||
// TODO
|
||||
|
||||
return nil
|
||||
}
|
||||
144
db/statedb/statedb_test.go
Normal file
144
db/statedb/statedb_test.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package statedb
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"io/ioutil"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
ethCrypto "github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/iden3/go-iden3-crypto/babyjub"
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func newAccount(t *testing.T, i int) *common.Account {
|
||||
var sk babyjub.PrivateKey
|
||||
_, err := hex.Decode(sk[:], []byte("0001020304050607080900010203040506070809000102030405060708090001"))
|
||||
require.Nil(t, err)
|
||||
pk := sk.Public()
|
||||
|
||||
key, err := ethCrypto.GenerateKey()
|
||||
require.Nil(t, err)
|
||||
address := ethCrypto.PubkeyToAddress(key.PublicKey)
|
||||
|
||||
return &common.Account{
|
||||
TokenID: common.TokenID(i),
|
||||
Nonce: uint64(i),
|
||||
Balance: big.NewInt(1000),
|
||||
PublicKey: pk,
|
||||
EthAddr: address,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestStateDBWithoutMT(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "tmpdb")
|
||||
require.Nil(t, err)
|
||||
|
||||
sdb, err := NewStateDB(dir, false, false, 0)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// create test accounts
|
||||
var accounts []*common.Account
|
||||
for i := 0; i < 100; i++ {
|
||||
accounts = append(accounts, newAccount(t, i))
|
||||
}
|
||||
|
||||
// get non-existing account, expecting an error
|
||||
_, err = sdb.GetAccount(common.Idx(1))
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, db.ErrNotFound, err)
|
||||
|
||||
// add test accounts
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
err = sdb.CreateAccount(common.Idx(i), accounts[i])
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
accGetted, err := sdb.GetAccount(common.Idx(i))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, accounts[i], accGetted)
|
||||
}
|
||||
|
||||
// try already existing idx and get error
|
||||
_, err = sdb.GetAccount(common.Idx(1)) // check that exist
|
||||
assert.Nil(t, err)
|
||||
err = sdb.CreateAccount(common.Idx(1), accounts[1]) // check that can not be created twice
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrAccountAlreadyExists, err)
|
||||
|
||||
// update accounts
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
accounts[i].Nonce = accounts[i].Nonce + 1
|
||||
err = sdb.UpdateAccount(common.Idx(i), accounts[i])
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
// check that can not call MerkleTree methods of the StateDB
|
||||
_, err = sdb.MTCreateAccount(common.Idx(1), accounts[1])
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrStateDBWithoutMT, err)
|
||||
|
||||
_, err = sdb.MTUpdateAccount(common.Idx(1), accounts[1])
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrStateDBWithoutMT, err)
|
||||
|
||||
_, err = sdb.MTGetProof(common.Idx(1))
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrStateDBWithoutMT, err)
|
||||
}
|
||||
|
||||
func TestStateDBWithMT(t *testing.T) {
|
||||
dir, err := ioutil.TempDir("", "tmpdb")
|
||||
require.Nil(t, err)
|
||||
|
||||
sdb, err := NewStateDB(dir, false, true, 32)
|
||||
assert.Nil(t, err)
|
||||
|
||||
// create test accounts
|
||||
var accounts []*common.Account
|
||||
for i := 0; i < 100; i++ {
|
||||
accounts = append(accounts, newAccount(t, i))
|
||||
}
|
||||
|
||||
// get non-existing account, expecting an error
|
||||
_, err = sdb.GetAccount(common.Idx(1))
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, db.ErrNotFound, err)
|
||||
|
||||
// add test accounts
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
_, err = sdb.MTCreateAccount(common.Idx(i), accounts[i])
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
accGetted, err := sdb.GetAccount(common.Idx(i))
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, accounts[i], accGetted)
|
||||
}
|
||||
|
||||
// try already existing idx and get error
|
||||
_, err = sdb.GetAccount(common.Idx(1)) // check that exist
|
||||
assert.Nil(t, err)
|
||||
_, err = sdb.MTCreateAccount(common.Idx(1), accounts[1]) // check that can not be created twice
|
||||
assert.NotNil(t, err)
|
||||
assert.Equal(t, ErrAccountAlreadyExists, err)
|
||||
|
||||
_, err = sdb.MTGetProof(common.Idx(1))
|
||||
assert.Nil(t, err)
|
||||
|
||||
// update accounts
|
||||
for i := 0; i < len(accounts); i++ {
|
||||
accounts[i].Nonce = accounts[i].Nonce + 1
|
||||
_, err = sdb.MTUpdateAccount(common.Idx(i), accounts[i])
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
a, err := sdb.GetAccount(common.Idx(1)) // check that account value has been updated
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, accounts[1].Nonce, a.Nonce)
|
||||
}
|
||||
Reference in New Issue
Block a user