Add StateDB compute nonces on ProcessTx L2Tx

Add StateDB compute nonces on ProcessTx L2Tx, and update StateDB for
type TypeSynchronizer, TypeTxSelector, TypeBatchBuilder
This commit is contained in:
arnaucube
2020-10-01 17:59:50 +02:00
parent d71871c8b7
commit 3374a4754d
14 changed files with 192 additions and 94 deletions

View File

@@ -15,31 +15,45 @@ import (
// TODO(Edu): Document here how StateDB is kept consistent
// 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")
var (
// ErrStateDBWithoutMT is used when a method that requires a MerkleTree
// is called in a StateDB that does not have a MerkleTree defined
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")
// ErrAccountAlreadyExists is used when CreateAccount is called and the
// Account already exists
ErrAccountAlreadyExists = errors.New("Can not CreateAccount because Account already exists")
// ErrToIdxNotFound is used when trying to get the ToIdx from ToEthAddr or
// ToEthAddr&ToBJJ
var ErrToIdxNotFound = errors.New("ToIdx can not be found")
// ErrToIdxNotFound is used when trying to get the ToIdx from ToEthAddr
// or ToEthAddr&ToBJJ
ErrToIdxNotFound = errors.New("ToIdx can not be found")
// KeyCurrentBatch is used as key in the db to store the current BatchNum
var KeyCurrentBatch = []byte("currentbatch")
// KeyCurrentBatch is used as key in the db to store the current BatchNum
KeyCurrentBatch = []byte("currentbatch")
)
// PathStateDB defines the subpath of the StateDB
const PathStateDB = "/statedb"
const (
// PathStateDB defines the subpath of the StateDB
PathStateDB = "/statedb"
// PathBatchNum defines the subpath of the Batch Checkpoint in the
// subpath of the StateDB
PathBatchNum = "/BatchNum"
// PathCurrent defines the subpath of the current Batch in the subpath
// of the StateDB
PathCurrent = "/current"
// TypeSynchronizer defines a StateDB used by the Synchronizer, that
// generates the ExitTree when processing the txs
TypeSynchronizer = "synchronizer"
// TypeTxSelector defines a StateDB used by the TxSelector, without
// computing ExitTree neither the ZKInputs
TypeTxSelector = "txselector"
// TypeBatchBuilder defines a StateDB used by the BatchBuilder, that
// generates the ExitTree and the ZKInput when processing the txs
TypeBatchBuilder = "batchbuilder"
)
// PathBatchNum defines the subpath of the Batch Checkpoint in the subpath of
// the StateDB
const PathBatchNum = "/BatchNum"
// PathCurrent defines the subpath of the current Batch in the subpath of the
// StateDB
const PathCurrent = "/current"
// TypeStateDB determines the type of StateDB
type TypeStateDB string
// StateDB represents the StateDB object
type StateDB struct {
@@ -47,6 +61,7 @@ type StateDB struct {
currentBatch common.BatchNum
db *pebble.PebbleStorage
mt *merkletree.MerkleTree
typ TypeStateDB
// idx holds the current Idx that the BatchBuilder is using
idx common.Idx
zki *common.ZKInputs
@@ -55,7 +70,7 @@ type StateDB struct {
// NewStateDB creates a new StateDB, allowing to use an in-memory or in-disk
// storage
func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) {
func NewStateDB(path string, typ TypeStateDB, nLevels int) (*StateDB, error) {
var sto *pebble.PebbleStorage
var err error
sto, err = pebble.NewPebbleStorage(path+PathStateDB+PathCurrent, false)
@@ -64,17 +79,21 @@ func NewStateDB(path string, withMT bool, nLevels int) (*StateDB, error) {
}
var mt *merkletree.MerkleTree = nil
if withMT {
if typ == TypeSynchronizer || typ == TypeBatchBuilder {
mt, err = merkletree.NewMerkleTree(sto, nLevels)
if err != nil {
return nil, err
}
}
if typ == TypeTxSelector && nLevels != 0 {
return nil, fmt.Errorf("invalid StateDB parameters: StateDB type==TypeStateDB can not have nLevels!=0")
}
sdb := &StateDB{
path: path + PathStateDB,
db: sto,
mt: mt,
typ: typ,
}
// load currentBatch
@@ -382,8 +401,8 @@ type LocalStateDB struct {
// NewLocalStateDB returns a new LocalStateDB connected to the given
// synchronizerDB
func NewLocalStateDB(path string, synchronizerDB *StateDB, withMT bool, nLevels int) (*LocalStateDB, error) {
s, err := NewStateDB(path, withMT, nLevels)
func NewLocalStateDB(path string, synchronizerDB *StateDB, typ TypeStateDB, nLevels int) (*LocalStateDB, error) {
s, err := NewStateDB(path, typ, nLevels)
if err != nil {
return nil, err
}

View File

@@ -38,7 +38,7 @@ func TestNewStateDBIntermediateState(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, false, 0)
sdb, err := NewStateDB(dir, TypeTxSelector, 0)
assert.Nil(t, err)
// test values
@@ -60,7 +60,7 @@ func TestNewStateDBIntermediateState(t *testing.T) {
// call NewStateDB which should get the db at the last checkpoint state
// executing a Reset (discarding the last 'testkey0'&'testvalue0' data)
sdb, err = NewStateDB(dir, false, 0)
sdb, err = NewStateDB(dir, TypeTxSelector, 0)
assert.Nil(t, err)
v, err = sdb.db.Get(k0)
assert.NotNil(t, err)
@@ -102,7 +102,7 @@ func TestNewStateDBIntermediateState(t *testing.T) {
// call NewStateDB which should get the db at the last checkpoint state
// executing a Reset (discarding the last 'testkey1'&'testvalue1' data)
sdb, err = NewStateDB(dir, false, 0)
sdb, err = NewStateDB(dir, TypeTxSelector, 0)
assert.Nil(t, err)
v, err = sdb.db.Get(k0)
@@ -119,7 +119,7 @@ func TestStateDBWithoutMT(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, false, 0)
sdb, err := NewStateDB(dir, TypeTxSelector, 0)
assert.Nil(t, err)
// create test accounts
@@ -168,7 +168,7 @@ func TestStateDBWithMT(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, true, 32)
sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
assert.Nil(t, err)
// create test accounts
@@ -219,7 +219,7 @@ func TestCheckpoints(t *testing.T) {
dir, err := ioutil.TempDir("", "sdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, true, 32)
sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
assert.Nil(t, err)
// create test accounts
@@ -285,7 +285,7 @@ func TestCheckpoints(t *testing.T) {
// Create a LocalStateDB from the initial StateDB
dirLocal, err := ioutil.TempDir("", "ldb")
require.Nil(t, err)
ldb, err := NewLocalStateDB(dirLocal, sdb, true, 32)
ldb, err := NewLocalStateDB(dirLocal, sdb, TypeBatchBuilder, 32)
assert.Nil(t, err)
// get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB)
@@ -305,7 +305,7 @@ func TestCheckpoints(t *testing.T) {
// Create a 2nd LocalStateDB from the initial StateDB
dirLocal2, err := ioutil.TempDir("", "ldb2")
require.Nil(t, err)
ldb2, err := NewLocalStateDB(dirLocal2, sdb, true, 32)
ldb2, err := NewLocalStateDB(dirLocal2, sdb, TypeBatchBuilder, 32)
assert.Nil(t, err)
// get checkpoint 4 from sdb (StateDB) to ldb (LocalStateDB)

View File

@@ -30,11 +30,13 @@ type processedExit struct {
}
// 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 if
// cmpExitTree is set to true, returns common.ExitTreeLeaf that is later used
// by the Synchronizer to update the HistoryDB.
func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.PoolL2Tx) (*common.ZKInputs, []*common.ExitInfo, error) {
// the StateDB depending on the transaction Type. If StateDB
// type==TypeBatchBuilder, returns the common.ZKInputs to generate the
// SnarkProof later used by the BatchBuilder. If StateDB
// type==TypeSynchronizer, assumes that the call is done from the Synchronizer,
// returns common.ExitTreeLeaf that is later used by the Synchronizer to update
// the HistoryDB, and adds Nonce & TokenID to the L2Txs.
func (s *StateDB) ProcessTxs(l1usertxs, l1coordinatortxs []*common.L1Tx, l2txs []*common.PoolL2Tx) (*common.ZKInputs, []common.ExitInfo, error) {
var err error
var exitTree *merkletree.MerkleTree
@@ -50,7 +52,7 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin
}
exits := make([]processedExit, nTx)
if cmpZKInputs {
if s.typ == TypeBatchBuilder {
s.zki = common.NewZKInputs(nTx, 24, 32) // TODO this values will be parameters of the function, taken from config file/coordinator call
s.zki.OldLastIdx = (s.idx - 1).BigInt()
s.zki.OldStateRoot = s.mt.Root().BigInt()
@@ -58,7 +60,7 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin
// TBD if ExitTree is only in memory or stored in disk, for the moment
// only needed in memory
if cmpExitTree {
if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
exitTree, err = merkletree.NewMerkleTree(memory.NewMemoryStorage(), s.mt.MaxLevels())
if err != nil {
return nil, nil, err
@@ -71,15 +73,15 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin
if err != nil {
return nil, nil, err
}
if exitIdx != nil && cmpExitTree {
exits[s.i] = processedExit{
exit: true,
newExit: newExit,
idx: *exitIdx,
acc: *exitAccount,
if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
if exitIdx != nil && exitTree != nil {
exits[s.i] = processedExit{
exit: true,
newExit: newExit,
idx: *exitIdx,
acc: *exitAccount,
}
}
}
if s.zki != nil {
s.i++
}
}
@@ -91,15 +93,15 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin
if exitIdx != nil {
log.Error("Unexpected Exit in L1CoordinatorTx")
}
if exitIdx != nil && cmpExitTree {
exits[s.i] = processedExit{
exit: true,
newExit: newExit,
idx: *exitIdx,
acc: *exitAccount,
if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
if exitIdx != nil && exitTree != nil {
exits[s.i] = processedExit{
exit: true,
newExit: newExit,
idx: *exitIdx,
acc: *exitAccount,
}
}
}
if s.zki != nil {
s.i++
}
}
@@ -108,26 +110,26 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin
if err != nil {
return nil, nil, err
}
if exitIdx != nil && cmpExitTree {
exits[s.i] = processedExit{
exit: true,
newExit: newExit,
idx: *exitIdx,
acc: *exitAccount,
if s.typ == TypeSynchronizer || s.typ == TypeBatchBuilder {
if exitIdx != nil && exitTree != nil {
exits[s.i] = processedExit{
exit: true,
newExit: newExit,
idx: *exitIdx,
acc: *exitAccount,
}
}
}
if s.zki != nil {
s.i++
}
}
if !cmpExitTree && !cmpZKInputs {
if s.typ == TypeTxSelector {
return nil, nil, nil
}
// once all txs processed (exitTree root frozen), for each Exit,
// generate common.ExitInfo data
var exitInfos []*common.ExitInfo
var exitInfos []common.ExitInfo
for i := 0; i < nTx; i++ {
if !exits[i].exit {
continue
@@ -141,7 +143,7 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin
return nil, nil, err
}
// 1. generate common.ExitInfo
ei := &common.ExitInfo{
ei := common.ExitInfo{
AccountIdx: exitIdx,
MerkleProof: p,
Balance: exitAccount.Balance,
@@ -168,7 +170,7 @@ func (s *StateDB) ProcessTxs(cmpExitTree, cmpZKInputs bool, l1usertxs, l1coordin
s.zki.OldValue2[i] = p.OldValue.BigInt()
}
}
if !cmpZKInputs {
if s.typ == TypeSynchronizer {
return nil, exitInfos, nil
}
@@ -345,6 +347,17 @@ func (s *StateDB) processL2Tx(exitTree *merkletree.MerkleTree, tx *common.PoolL2
s.zki.R8y[s.i] = tx.Signature.R8.Y
}
// if StateDB type==TypeSynchronizer, will need to add Nonce and
// TokenID to the transaction
if s.typ == TypeSynchronizer {
acc, err := s.GetAccount(tx.ToIdx)
if err != nil {
return nil, nil, false, err
}
tx.Nonce = acc.Nonce
tx.TokenID = acc.TokenID
}
switch tx.Type {
case common.TxTypeTransfer:
// go to the MT account of sender and receiver, and update

View File

@@ -19,7 +19,7 @@ func TestProcessTxs(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, true, 32)
sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
assert.Nil(t, err)
// generate test transactions from test.SetTest0 code
@@ -36,7 +36,7 @@ func TestProcessTxs(t *testing.T) {
for i := 0; i < len(l1Txs); i++ {
// l2Txs := common.PoolL2TxsToL2Txs(poolL2Txs[i])
_, _, err := sdb.ProcessTxs(true, true, l1Txs[i], coordinatorL1Txs[i], poolL2Txs[i])
_, _, err := sdb.ProcessTxs(l1Txs[i], coordinatorL1Txs[i], poolL2Txs[i])
require.Nil(t, err)
}
@@ -45,11 +45,14 @@ func TestProcessTxs(t *testing.T) {
assert.Equal(t, "23", acc.Balance.String())
}
func TestProcessTxsBatchByBatch(t *testing.T) {
func TestProcessTxsSynchronizer(t *testing.T) {
// TODO once TTGL is updated, use the blockchain L2Tx (not PoolL2Tx) for
// the Synchronizer tests
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, true, 32)
sdb, err := NewStateDB(dir, TypeSynchronizer, 32)
assert.Nil(t, err)
// generate test transactions from test.SetTest0 code
@@ -70,8 +73,11 @@ func TestProcessTxsBatchByBatch(t *testing.T) {
// use first batch
// l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0])
_, exitInfos, err := sdb.ProcessTxs(true, true, l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
_, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
require.Nil(t, err)
// TODO once TTGL is updated, add a check that a input poolL2Tx with
// Nonce & TokenID =0, after ProcessTxs call has the expected value
assert.Equal(t, 0, len(exitInfos))
acc, err := sdb.GetAccount(common.Idx(256))
assert.Nil(t, err)
@@ -79,7 +85,7 @@ func TestProcessTxsBatchByBatch(t *testing.T) {
// use second batch
// l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1])
_, exitInfos, err = sdb.ProcessTxs(true, true, l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
_, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
require.Nil(t, err)
assert.Equal(t, 5, len(exitInfos))
acc, err = sdb.GetAccount(common.Idx(256))
@@ -88,7 +94,58 @@ func TestProcessTxsBatchByBatch(t *testing.T) {
// use third batch
// l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2])
_, exitInfos, err = sdb.ProcessTxs(true, true, l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
_, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
require.Nil(t, err)
assert.Equal(t, 1, len(exitInfos))
acc, err = sdb.GetAccount(common.Idx(256))
assert.Nil(t, err)
assert.Equal(t, "23", acc.Balance.String())
}
func TestProcessTxsBatchBuilder(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
assert.Nil(t, err)
// generate test transactions from test.SetTest0 code
parser := test.NewParser(strings.NewReader(test.SetTest0))
instructions, err := parser.Parse()
assert.Nil(t, err)
l1Txs, coordinatorL1Txs, poolL2Txs, _ := test.GenerateTestTxs(t, instructions)
assert.Equal(t, 29, len(l1Txs[0]))
assert.Equal(t, 0, len(coordinatorL1Txs[0]))
assert.Equal(t, 21, len(poolL2Txs[0]))
assert.Equal(t, 5, len(l1Txs[1]))
assert.Equal(t, 1, len(coordinatorL1Txs[1]))
assert.Equal(t, 55, len(poolL2Txs[1]))
assert.Equal(t, 10, len(l1Txs[2]))
assert.Equal(t, 0, len(coordinatorL1Txs[2]))
assert.Equal(t, 7, len(poolL2Txs[2]))
// use first batch
// l2txs := common.PoolL2TxsToL2Txs(poolL2Txs[0])
_, exitInfos, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
require.Nil(t, err)
assert.Equal(t, 0, len(exitInfos))
acc, err := sdb.GetAccount(common.Idx(256))
assert.Nil(t, err)
assert.Equal(t, "28", acc.Balance.String())
// use second batch
// l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[1])
_, exitInfos, err = sdb.ProcessTxs(l1Txs[1], coordinatorL1Txs[1], poolL2Txs[1])
require.Nil(t, err)
assert.Equal(t, 5, len(exitInfos))
acc, err = sdb.GetAccount(common.Idx(256))
require.Nil(t, err)
assert.Equal(t, "48", acc.Balance.String())
// use third batch
// l2txs = common.PoolL2TxsToL2Txs(poolL2Txs[2])
_, exitInfos, err = sdb.ProcessTxs(l1Txs[2], coordinatorL1Txs[2], poolL2Txs[2])
require.Nil(t, err)
assert.Equal(t, 1, len(exitInfos))
acc, err = sdb.GetAccount(common.Idx(256))
@@ -100,7 +157,7 @@ func TestZKInputsGeneration(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, true, 32)
sdb, err := NewStateDB(dir, TypeBatchBuilder, 32)
assert.Nil(t, err)
// generate test transactions from test.SetTest0 code
@@ -113,7 +170,7 @@ func TestZKInputsGeneration(t *testing.T) {
assert.Equal(t, 0, len(coordinatorL1Txs[0]))
assert.Equal(t, 21, len(poolL2Txs[0]))
zki, _, err := sdb.ProcessTxs(false, true, l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
zki, _, err := sdb.ProcessTxs(l1Txs[0], coordinatorL1Txs[0], poolL2Txs[0])
require.Nil(t, err)
s, err := json.Marshal(zki)

View File

@@ -16,7 +16,7 @@ func TestGetIdx(t *testing.T) {
dir, err := ioutil.TempDir("", "tmpdb")
require.Nil(t, err)
sdb, err := NewStateDB(dir, false, 0)
sdb, err := NewStateDB(dir, TypeTxSelector, 0)
assert.Nil(t, err)
var sk babyjub.PrivateKey