mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Add abstraction method of processTxs to StateDB
- Update GHA lint.yml increasing timeout time to avoid GHA Lint errors
- Update common.BatchNum & common.Idx & common.Nonce usage in StateDB
- Add abstraction method of processTxs to StateDB
- Which will be used by Synchronizer & BatchBuilder
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package statedb
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -31,11 +30,11 @@ const PATHCURRENT = "/current"
|
||||
// StateDB represents the StateDB object
|
||||
type StateDB struct {
|
||||
path string
|
||||
currentBatch uint64
|
||||
currentBatch common.BatchNum
|
||||
db *pebble.PebbleStorage
|
||||
mt *merkletree.MerkleTree
|
||||
// idx holds the current Idx that the BatchBuilder is using
|
||||
idx uint64
|
||||
idx common.Idx
|
||||
}
|
||||
|
||||
// NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk
|
||||
@@ -77,7 +76,7 @@ func (s *StateDB) DB() *pebble.PebbleStorage {
|
||||
}
|
||||
|
||||
// GetCurrentBatch returns the current BatchNum stored in the StateDB
|
||||
func (s *StateDB) GetCurrentBatch() (uint64, error) {
|
||||
func (s *StateDB) GetCurrentBatch() (common.BatchNum, error) {
|
||||
cbBytes, err := s.db.Get(KEYCURRENTBATCH)
|
||||
if err == db.ErrNotFound {
|
||||
return 0, nil
|
||||
@@ -85,8 +84,7 @@ func (s *StateDB) GetCurrentBatch() (uint64, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
cb := binary.LittleEndian.Uint64(cbBytes[:8])
|
||||
return cb, nil
|
||||
return common.BatchNumFromBytes(cbBytes)
|
||||
}
|
||||
|
||||
// setCurrentBatch stores the current BatchNum in the StateDB
|
||||
@@ -95,9 +93,7 @@ func (s *StateDB) setCurrentBatch() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var cbBytes [8]byte
|
||||
binary.LittleEndian.PutUint64(cbBytes[:], s.currentBatch)
|
||||
tx.Put(KEYCURRENTBATCH, cbBytes[:])
|
||||
tx.Put(KEYCURRENTBATCH, s.currentBatch.Bytes())
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func newAccount(t *testing.T, i int) *common.Account {
|
||||
|
||||
return &common.Account{
|
||||
TokenID: common.TokenID(i),
|
||||
Nonce: uint64(i),
|
||||
Nonce: common.Nonce(i),
|
||||
Balance: big.NewInt(1000),
|
||||
PublicKey: pk,
|
||||
EthAddr: address,
|
||||
@@ -159,7 +159,7 @@ func TestCheckpoints(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
cb, err := sdb.GetCurrentBatch()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uint64(1), cb)
|
||||
assert.Equal(t, common.BatchNum(1), cb)
|
||||
|
||||
for i := 1; i < 10; i++ {
|
||||
err = sdb.MakeCheckpoint()
|
||||
@@ -167,7 +167,7 @@ func TestCheckpoints(t *testing.T) {
|
||||
|
||||
cb, err = sdb.GetCurrentBatch()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uint64(i+1), cb)
|
||||
assert.Equal(t, common.BatchNum(i+1), cb)
|
||||
}
|
||||
|
||||
// printCheckpoints(t, sdb.path)
|
||||
@@ -184,14 +184,14 @@ func TestCheckpoints(t *testing.T) {
|
||||
// check that currentBatch is as expected after Reset
|
||||
cb, err = sdb.GetCurrentBatch()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uint64(3), cb)
|
||||
assert.Equal(t, common.BatchNum(3), cb)
|
||||
|
||||
// advance one checkpoint and check that currentBatch is fine
|
||||
err = sdb.MakeCheckpoint()
|
||||
assert.Nil(t, err)
|
||||
cb, err = sdb.GetCurrentBatch()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uint64(4), cb)
|
||||
assert.Equal(t, common.BatchNum(4), cb)
|
||||
|
||||
err = sdb.DeleteCheckpoint(uint64(9))
|
||||
assert.Nil(t, err)
|
||||
@@ -214,13 +214,13 @@ func TestCheckpoints(t *testing.T) {
|
||||
// check that currentBatch is 4 after the Reset
|
||||
cb, err = ldb.GetCurrentBatch()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uint64(4), cb)
|
||||
assert.Equal(t, common.BatchNum(4), cb)
|
||||
// advance one checkpoint in ldb
|
||||
err = ldb.MakeCheckpoint()
|
||||
assert.Nil(t, err)
|
||||
cb, err = ldb.GetCurrentBatch()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uint64(5), cb)
|
||||
assert.Equal(t, common.BatchNum(5), cb)
|
||||
|
||||
// Create a 2nd LocalStateDB from the initial StateDB
|
||||
dirLocal2, err := ioutil.TempDir("", "ldb2")
|
||||
@@ -234,13 +234,13 @@ func TestCheckpoints(t *testing.T) {
|
||||
// check that currentBatch is 4 after the Reset
|
||||
cb, err = ldb2.GetCurrentBatch()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uint64(4), cb)
|
||||
assert.Equal(t, common.BatchNum(4), cb)
|
||||
// advance one checkpoint in ldb2
|
||||
err = ldb2.MakeCheckpoint()
|
||||
assert.Nil(t, err)
|
||||
cb, err = ldb2.GetCurrentBatch()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, uint64(5), cb)
|
||||
assert.Equal(t, common.BatchNum(5), cb)
|
||||
|
||||
debug := false
|
||||
if debug {
|
||||
|
||||
@@ -1,19 +1,46 @@
|
||||
package statedb
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"math/big"
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/iden3/go-merkletree/db"
|
||||
)
|
||||
|
||||
// KEYIDX is used as key in the db to store the current Idx
|
||||
var KEYIDX = []byte("idx")
|
||||
// keyidx is used as key in the db to store the current Idx
|
||||
var keyidx = []byte("idx")
|
||||
|
||||
// ProcessPoolL2Tx process the given PoolL2Tx applying the needed updates to
|
||||
// ProcessTxs process the given L1Txs & L2Txs applying the needed updates
|
||||
// to the StateDB depending on the transaction Type. Returns the
|
||||
// common.ZKInputs to generate the SnarkProof later used by the BatchBuilder,
|
||||
// and returns common.ExitTreeLeaf that is later used by the Synchronizer to
|
||||
// update the HistoryDB.
|
||||
func (s *StateDB) ProcessTxs(l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.L2Tx) (*common.ZKInputs, []*common.ExitTreeLeaf, error) {
|
||||
for _, tx := range l1usertxs {
|
||||
err := s.processL1Tx(tx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
for _, tx := range l1coordinatortxs {
|
||||
err := s.processL1Tx(tx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
for _, tx := range l2txs {
|
||||
err := s.processL2Tx(tx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
// processL2Tx process the given L2Tx applying the needed updates to
|
||||
// the StateDB depending on the transaction Type.
|
||||
func (s *StateDB) ProcessPoolL2Tx(tx *common.PoolL2Tx) error {
|
||||
func (s *StateDB) processL2Tx(tx *common.L2Tx) error {
|
||||
switch tx.Type {
|
||||
case common.TxTypeTransfer:
|
||||
// go to the MT account of sender and receiver, and update
|
||||
@@ -29,9 +56,9 @@ func (s *StateDB) ProcessPoolL2Tx(tx *common.PoolL2Tx) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ProcessL1Tx process the given L1Tx applying the needed updates to the
|
||||
// processL1Tx process the given L1Tx applying the needed updates to the
|
||||
// StateDB depending on the transaction Type.
|
||||
func (s *StateDB) ProcessL1Tx(tx *common.L1Tx) error {
|
||||
func (s *StateDB) processL1Tx(tx *common.L1Tx) error {
|
||||
switch tx.Type {
|
||||
case common.TxTypeForceTransfer, common.TxTypeTransfer:
|
||||
// go to the MT account of sender and receiver, and update balance
|
||||
@@ -170,27 +197,24 @@ func (s *StateDB) applyTransfer(tx *common.Tx) error {
|
||||
|
||||
// getIdx returns the stored Idx from the localStateDB, which is the last Idx
|
||||
// used for an Account in the localStateDB.
|
||||
func (s *StateDB) getIdx() (uint64, error) {
|
||||
idxBytes, err := s.DB().Get(KEYIDX)
|
||||
func (s *StateDB) getIdx() (common.Idx, error) {
|
||||
idxBytes, err := s.DB().Get(keyidx)
|
||||
if err == db.ErrNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
idx := binary.LittleEndian.Uint64(idxBytes[:8])
|
||||
return idx, nil
|
||||
return common.IdxFromBytes(idxBytes[:4])
|
||||
}
|
||||
|
||||
// setIdx stores Idx in the localStateDB
|
||||
func (s *StateDB) setIdx(idx uint64) error {
|
||||
func (s *StateDB) setIdx(idx common.Idx) error {
|
||||
tx, err := s.DB().NewTx()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var idxBytes [8]byte
|
||||
binary.LittleEndian.PutUint64(idxBytes[:], idx)
|
||||
tx.Put(KEYIDX, idxBytes[:])
|
||||
tx.Put(keyidx, idx.Bytes())
|
||||
if err := tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user