From c108739c24247dd0340db13bbd41008ee2bc246b Mon Sep 17 00:00:00 2001 From: Arnau B Date: Fri, 21 Aug 2020 16:59:30 +0200 Subject: [PATCH] Add usefull methods for reorg --- common/exittree.go | 4 ++-- common/l1tx.go | 3 +-- common/l2tx.go | 3 +-- common/pooll2tx.go | 2 +- db/historydb/historydb.go | 17 ++++++++++++++++- db/historydb/migrations/001_init.sql | 8 +++++--- db/l2db/migrations/001_init.sql | 3 ++- 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/common/exittree.go b/common/exittree.go index de295d1..201d3da 100644 --- a/common/exittree.go +++ b/common/exittree.go @@ -4,9 +4,9 @@ import ( "math/big" ) -type ExitTreeLeaf struct { +type ExitInfo struct { AccountIdx Idx MerkleProof []byte - Amount *big.Int + Balance *big.Int Nullifier *big.Int } diff --git a/common/l1tx.go b/common/l1tx.go index e5353f0..49ee142 100644 --- a/common/l1tx.go +++ b/common/l1tx.go @@ -22,8 +22,7 @@ type L1Tx struct { Amount *big.Int `meddler:"amount,bigint"` LoadAmount *big.Int `meddler:"load_amount,bigint"` EthBlockNum uint64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue - // Extra metadata, may be uninitialized - Type TxType `meddler:"-"` // optional, descrives which kind of tx it's + Type TxType `meddler:"tx_type"` } func (tx *L1Tx) Tx() *Tx { diff --git a/common/l2tx.go b/common/l2tx.go index c61040d..5690a2c 100644 --- a/common/l2tx.go +++ b/common/l2tx.go @@ -15,8 +15,7 @@ type L2Tx struct { Amount *big.Int `meddler:"amount,bigint"` Fee FeeSelector `meddler:"fee"` Nonce Nonce `meddler:"nonce"` - // Extra metadata, may be uninitialized - Type TxType `meddler:"-"` // optional, descrives which kind of tx it's + Type TxType `meddler:"tx_type"` } func (tx *L2Tx) Tx() *Tx { diff --git a/common/pooll2tx.go b/common/pooll2tx.go index 1180f05..355ad6d 100644 --- a/common/pooll2tx.go +++ b/common/pooll2tx.go @@ -62,8 +62,8 @@ type PoolL2Tx struct { RqNonce uint64 `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used AbsoluteFee float64 `meddler:"absolute_fee,zeroisnull"` AbsoluteFeeUpdate time.Time `meddler:"absolute_fee_update,utctimez"` + Type TxType `meddler:"tx_type"` // Extra metadata, may be uninitialized - Type TxType `meddler:"-"` // optional, descrives which kind of tx it's RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs } diff --git a/db/historydb/historydb.go b/db/historydb/historydb.go index 2bd400a..7deae53 100644 --- a/db/historydb/historydb.go +++ b/db/historydb/historydb.go @@ -54,7 +54,12 @@ func (hdb *HistoryDB) addBlocks(blocks []common.Block) error { ) } -// GetBlocks retrrieve blocks from the DB +// GetBlock retrieve a block from the DB, given a block number +func (hdb *HistoryDB) GetBlock(blockNum uint64) (*common.Block, error) { + return nil, nil +} + +// GetBlocks retrieve blocks from the DB, given a range of block numbers defined by from and to func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) { var blocks []*common.Block err := meddler.QueryAll( @@ -65,6 +70,16 @@ func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) { return blocks, err } +// GetLastBlock retrieve the block with the highest block number from the DB +func (hdb *HistoryDB) GetLastBlock() (*common.Block, error) { + return nil, nil +} + +// GetLastBatchNum returns the BatchNum of the latest forged batch +func (hdb *HistoryDB) GetLastBatchNum() (*common.BatchNum, error) { + return nil, nil +} + // Reorg deletes all the information that was added into the DB after the lastValidBlock // WARNING: this is a draaft of the function, useful at the moment for tests func (hdb *HistoryDB) Reorg(lastValidBlock uint64) error { diff --git a/db/historydb/migrations/001_init.sql b/db/historydb/migrations/001_init.sql index 24e72c8..9a33d8d 100644 --- a/db/historydb/migrations/001_init.sql +++ b/db/historydb/migrations/001_init.sql @@ -30,7 +30,7 @@ CREATE TABLE exit_tree ( batch_num BIGINT REFERENCES batch (batch_num) ON DELETE CASCADE, account_idx BIGINT, merkle_proof BYTEA NOT NULL, - amount NUMERIC NOT NULL, + balance NUMERIC NOT NULL, nullifier BYTEA NOT NULL, PRIMARY KEY (batch_num, account_idx) ); @@ -72,7 +72,8 @@ CREATE TABLE l1tx ( token_id INT NOT NULL REFERENCES token (token_id), amount NUMERIC NOT NULL, load_amount BYTEA NOT NULL, - eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE + eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE, + tx_type VARCHAR(40) NOT NULL ); CREATE TABLE l2tx ( @@ -83,7 +84,8 @@ CREATE TABLE l2tx ( to_idx BIGINT NOT NULL, amount NUMERIC NOT NULL, fee INT NOT NULL, - nonce BIGINT NOT NULL + nonce BIGINT NOT NULL, + tx_type VARCHAR(40) NOT NULL ); CREATE TABLE account ( diff --git a/db/l2db/migrations/001_init.sql b/db/l2db/migrations/001_init.sql index 2e6dab1..80ac665 100644 --- a/db/l2db/migrations/001_init.sql +++ b/db/l2db/migrations/001_init.sql @@ -22,7 +22,8 @@ CREATE TABLE tx_pool ( signature BYTEA NOT NULL, timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL, absolute_fee NUMERIC, - absolute_fee_update TIMESTAMP WITHOUT TIME ZONE + absolute_fee_update TIMESTAMP WITHOUT TIME ZONE, + tx_type VARCHAR(40) NOT NULL ); CREATE TABLE account_creation_auth (