mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Update Synchronizer (node) implementation
- node:
- Extend config to add initial variables of the smart contracts used as
defaults before they are changed via events.
- In stopped channels, set size 1 so that panics are not witheld until the
node stops completely.
- common:
- In Smart Contract variables, comment:
- `WDelayerVariables.HermezRollupAddress` because it's not needed.
- `RollupVariables.Buckets` because there are no events for it, and for
now it's not used.
- historydb:
- Add functions to get and set smart contract variables.
- db:
- Add `Rollback` function in `utils.go` to reduce boilerplate in sql
transaction rollbacks in defers in db functions.
- Update `rollup_vars` and `auction_vars` (renamed from `consensus_vars`)
table, and add `wdelayer_vars` table.
- synchronizer:
- Synchronize WDelayer
- Handle SC variables properly
- test/ethclient:
- Add essential implementation of WDelayer
This commit is contained in:
@@ -1097,6 +1097,72 @@ func (hdb *HistoryDB) GetLastTxsPosition(toForgeL1TxsNum int64) (int, error) {
|
||||
return lastL1TxsPosition, row.Scan(&lastL1TxsPosition)
|
||||
}
|
||||
|
||||
// GetSCVars returns the rollup, auction and wdelayer smart contracts variables at their last update.
|
||||
func (hdb *HistoryDB) GetSCVars() (*common.RollupVariables, *common.AuctionVariables,
|
||||
*common.WDelayerVariables, error) {
|
||||
var rollup common.RollupVariables
|
||||
var auction common.AuctionVariables
|
||||
var wDelayer common.WDelayerVariables
|
||||
if err := meddler.QueryRow(hdb.db, &rollup,
|
||||
"SELECT * FROM rollup_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
if err := meddler.QueryRow(hdb.db, &auction,
|
||||
"SELECT * FROM auction_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
if err := meddler.QueryRow(hdb.db, &wDelayer,
|
||||
"SELECT * FROM wdelayer_vars ORDER BY eth_block_num DESC LIMIT 1;"); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
return &rollup, &auction, &wDelayer, nil
|
||||
}
|
||||
|
||||
func (hdb *HistoryDB) setRollupVars(d meddler.DB, rollup *common.RollupVariables) error {
|
||||
return meddler.Insert(d, "rollup_vars", rollup)
|
||||
}
|
||||
|
||||
func (hdb *HistoryDB) setAuctionVars(d meddler.DB, auction *common.AuctionVariables) error {
|
||||
return meddler.Insert(d, "auction_vars", auction)
|
||||
}
|
||||
|
||||
func (hdb *HistoryDB) setWDelayerVars(d meddler.DB, wDelayer *common.WDelayerVariables) error {
|
||||
return meddler.Insert(d, "wdelayer_vars", wDelayer)
|
||||
}
|
||||
|
||||
// SetInitialSCVars sets the initial state of rollup, auction, wdelayer smart
|
||||
// contract variables. This initial state is stored linked to block 0, which
|
||||
// always exist in the DB and is used to store initialization data that always
|
||||
// exist in the smart contracts.
|
||||
func (hdb *HistoryDB) SetInitialSCVars(rollup *common.RollupVariables,
|
||||
auction *common.AuctionVariables, wDelayer *common.WDelayerVariables) error {
|
||||
txn, err := hdb.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
db.Rollback(txn)
|
||||
}
|
||||
}()
|
||||
// Force EthBlockNum to be 0 because it's the block used to link data
|
||||
// that belongs to the creation of the smart contracts
|
||||
rollup.EthBlockNum = 0
|
||||
auction.EthBlockNum = 0
|
||||
wDelayer.EthBlockNum = 0
|
||||
if err := hdb.setRollupVars(txn, rollup); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := hdb.setAuctionVars(txn, auction); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := hdb.setWDelayerVars(txn, wDelayer); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return txn.Commit()
|
||||
}
|
||||
|
||||
// AddBlockSCData stores all the information of a block retrieved by the
|
||||
// Synchronizer. Blocks should be inserted in order, leaving no gaps because
|
||||
// the pagination system of the API/DB depends on this. Within blocks, all
|
||||
@@ -1108,47 +1174,39 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
errRollback := txn.Rollback()
|
||||
if errRollback != nil {
|
||||
log.Errorw("Rollback", "err", errRollback)
|
||||
}
|
||||
db.Rollback(txn)
|
||||
}
|
||||
}()
|
||||
|
||||
// Add block
|
||||
err = hdb.addBlock(txn, &blockData.Block)
|
||||
if err != nil {
|
||||
if err := hdb.addBlock(txn, &blockData.Block); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add Coordinators
|
||||
if len(blockData.Auction.Coordinators) > 0 {
|
||||
err = hdb.addCoordinators(txn, blockData.Auction.Coordinators)
|
||||
if err != nil {
|
||||
if err := hdb.addCoordinators(txn, blockData.Auction.Coordinators); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Add Bids
|
||||
if len(blockData.Auction.Bids) > 0 {
|
||||
err = hdb.addBids(txn, blockData.Auction.Bids)
|
||||
if err != nil {
|
||||
if err := hdb.addBids(txn, blockData.Auction.Bids); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Add Tokens
|
||||
if len(blockData.Rollup.AddedTokens) > 0 {
|
||||
err = hdb.addTokens(txn, blockData.Rollup.AddedTokens)
|
||||
if err != nil {
|
||||
if err := hdb.addTokens(txn, blockData.Rollup.AddedTokens); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Add l1 Txs
|
||||
if len(blockData.Rollup.L1UserTxs) > 0 {
|
||||
err = hdb.addL1Txs(txn, blockData.Rollup.L1UserTxs)
|
||||
if err != nil {
|
||||
if err := hdb.addL1Txs(txn, blockData.Rollup.L1UserTxs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1158,16 +1216,14 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
batch := &blockData.Rollup.Batches[i]
|
||||
// Add Batch: this will trigger an update on the DB
|
||||
// that will set the batch num of forged L1 txs in this batch
|
||||
err = hdb.addBatch(txn, &batch.Batch)
|
||||
if err != nil {
|
||||
if err = hdb.addBatch(txn, &batch.Batch); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add unforged l1 Txs
|
||||
if batch.L1Batch {
|
||||
if len(batch.L1CoordinatorTxs) > 0 {
|
||||
err = hdb.addL1Txs(txn, batch.L1CoordinatorTxs)
|
||||
if err != nil {
|
||||
if err := hdb.addL1Txs(txn, batch.L1CoordinatorTxs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -1175,29 +1231,39 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *common.BlockData) (err error) {
|
||||
|
||||
// Add l2 Txs
|
||||
if len(batch.L2Txs) > 0 {
|
||||
err = hdb.addL2Txs(txn, batch.L2Txs)
|
||||
if err != nil {
|
||||
if err := hdb.addL2Txs(txn, batch.L2Txs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Add accounts
|
||||
if len(batch.CreatedAccounts) > 0 {
|
||||
err = hdb.addAccounts(txn, batch.CreatedAccounts)
|
||||
if err != nil {
|
||||
if err := hdb.addAccounts(txn, batch.CreatedAccounts); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Add exit tree
|
||||
if len(batch.ExitTree) > 0 {
|
||||
err = hdb.addExitTree(txn, batch.ExitTree)
|
||||
if err != nil {
|
||||
if err := hdb.addExitTree(txn, batch.ExitTree); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: INSERT CONTRACTS VARS
|
||||
}
|
||||
if blockData.Rollup.Vars != nil {
|
||||
if err := hdb.setRollupVars(txn, blockData.Rollup.Vars); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if blockData.Auction.Vars != nil {
|
||||
if err := hdb.setAuctionVars(txn, blockData.Auction.Vars); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if blockData.WDelayer.Vars != nil {
|
||||
if err := hdb.setWDelayerVars(txn, blockData.WDelayer.Vars); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Process withdrawals
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package historydb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"math"
|
||||
"math/big"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
dbUtils "github.com/hermeznetwork/hermez-node/db"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
@@ -429,6 +431,52 @@ func TestGetL1UserTxs(t *testing.T) {
|
||||
assert.Equal(t, 0, len(l1UserTxs))
|
||||
}
|
||||
|
||||
func TestSetInitialSCVars(t *testing.T) {
|
||||
test.WipeDB(historyDB.DB())
|
||||
_, _, _, err := historyDB.GetSCVars()
|
||||
assert.Equal(t, sql.ErrNoRows, err)
|
||||
|
||||
//nolint:govet
|
||||
rollup := &common.RollupVariables{
|
||||
0,
|
||||
big.NewInt(10),
|
||||
12,
|
||||
13,
|
||||
}
|
||||
//nolint:govet
|
||||
auction := &common.AuctionVariables{
|
||||
0,
|
||||
ethCommon.BigToAddress(big.NewInt(2)),
|
||||
ethCommon.BigToAddress(big.NewInt(3)),
|
||||
[6]*big.Int{
|
||||
big.NewInt(1), big.NewInt(2), big.NewInt(3),
|
||||
big.NewInt(4), big.NewInt(5), big.NewInt(6),
|
||||
},
|
||||
2,
|
||||
4320,
|
||||
[3]uint16{10, 11, 12},
|
||||
1000,
|
||||
20,
|
||||
}
|
||||
//nolint:govet
|
||||
wDelayer := &common.WDelayerVariables{
|
||||
0,
|
||||
ethCommon.BigToAddress(big.NewInt(2)),
|
||||
ethCommon.BigToAddress(big.NewInt(3)),
|
||||
ethCommon.BigToAddress(big.NewInt(4)),
|
||||
13,
|
||||
14,
|
||||
false,
|
||||
}
|
||||
err = historyDB.SetInitialSCVars(rollup, auction, wDelayer)
|
||||
require.Nil(t, err)
|
||||
dbRollup, dbAuction, dbWDelayer, err := historyDB.GetSCVars()
|
||||
assert.Nil(t, err)
|
||||
require.Equal(t, rollup, dbRollup)
|
||||
require.Equal(t, auction, dbAuction)
|
||||
require.Equal(t, wDelayer, dbWDelayer)
|
||||
}
|
||||
|
||||
// setTestBlocks WARNING: this will delete the blocks and recreate them
|
||||
func setTestBlocks(from, to int64) []common.Block {
|
||||
test.WipeDB(historyDB.DB())
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/db"
|
||||
"github.com/hermeznetwork/hermez-node/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
//nolint:errcheck // driver for postgres DB
|
||||
@@ -243,10 +242,7 @@ func (l2db *L2DB) CheckNonces(updatedAccounts []common.Account, batchNum common.
|
||||
defer func() {
|
||||
// Rollback the transaction if there was an error.
|
||||
if err != nil {
|
||||
errRollback := txn.Rollback()
|
||||
if errRollback != nil {
|
||||
log.Errorw("Rollback", "err", errRollback)
|
||||
}
|
||||
db.Rollback(txn)
|
||||
}
|
||||
}()
|
||||
for i := 0; i < len(updatedAccounts); i++ {
|
||||
@@ -291,10 +287,7 @@ func (l2db *L2DB) Purge(currentBatchNum common.BatchNum) (err error) {
|
||||
defer func() {
|
||||
// Rollback the transaction if there was an error.
|
||||
if err != nil {
|
||||
errRollback := txn.Rollback()
|
||||
if errRollback != nil {
|
||||
log.Errorw("Rollback", "err", errRollback)
|
||||
}
|
||||
db.Rollback(txn)
|
||||
}
|
||||
}()
|
||||
// Delete pending txs that have been in the pool after the TTL if maxTxs is reached
|
||||
|
||||
@@ -516,23 +516,31 @@ FOR EACH ROW EXECUTE PROCEDURE forge_l1_user_txs();
|
||||
|
||||
CREATE TABLE rollup_vars (
|
||||
eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||
forge_l1_timeout BYTEA NOT NULL,
|
||||
fee_l1_user_tx BYTEA NOT NULL,
|
||||
fee_add_token BYTEA NOT NULL,
|
||||
tokens_hez BYTEA NOT NULL,
|
||||
governance BYTEA NOT NULL
|
||||
forge_l1_timeout BYTEA NOT NULL,
|
||||
withdrawal_delay BIGINT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE consensus_vars (
|
||||
CREATE TABLE auction_vars (
|
||||
eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||
slot_deadline INT NOT NULL,
|
||||
close_auction_slots INT NOT NULL,
|
||||
open_auction_slots INT NOT NULL,
|
||||
min_bid_slots VARCHAR(200) NOT NULL,
|
||||
outbidding INT NOT NULL,
|
||||
donation_address BYTEA NOT NULL,
|
||||
governance_address BYTEA NOT NULL,
|
||||
allocation_ratio VARCHAR(200)
|
||||
boot_coordinator BYTEA NOT NULL,
|
||||
default_slot_set_bid BYTEA NOT NULL,
|
||||
closed_auction_slots INT NOT NULL,
|
||||
open_auction_slots INT NOT NULL,
|
||||
allocation_ratio VARCHAR(200),
|
||||
outbidding INT NOT NULL,
|
||||
slot_deadline INT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE wdelayer_vars (
|
||||
eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||
govdao_address BYTEA NOT NULL,
|
||||
whg_address BYTEA NOT NULL,
|
||||
keeper_address BYTEA NOT NULL,
|
||||
withdrawal_delay BIGINT NOT NULL,
|
||||
emergency_start_time BIGINT NOT NULL,
|
||||
emergency_mode BOOLEAN NOT NULL
|
||||
);
|
||||
|
||||
-- L2
|
||||
@@ -606,8 +614,9 @@ DROP FUNCTION set_pool_tx;
|
||||
-- drop tables
|
||||
DROP TABLE account_creation_auth;
|
||||
DROP TABLE tx_pool;
|
||||
DROP TABLE consensus_vars;
|
||||
DROP TABLE auction_vars;
|
||||
DROP TABLE rollup_vars;
|
||||
DROP TABLE wdelayer_vars;
|
||||
DROP TABLE tx;
|
||||
DROP TABLE exit_tree;
|
||||
DROP TABLE account;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math/big"
|
||||
@@ -192,3 +193,11 @@ type Paginationer interface {
|
||||
GetPagination() *Pagination
|
||||
Len() int
|
||||
}
|
||||
|
||||
// Rollback an sql transaction, and log the error if it's not nil
|
||||
func Rollback(txn *sql.Tx) {
|
||||
err := txn.Rollback()
|
||||
if err != nil {
|
||||
log.Errorw("Rollback", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user