mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 19:36:44 +01:00
Impl historyDB methods for sync main loop
This commit is contained in:
@@ -41,22 +41,18 @@ func NewHistoryDB(port int, host, user, password, dbname string) (*HistoryDB, er
|
||||
}
|
||||
|
||||
// AddBlock insert a block into the DB
|
||||
func (hdb *HistoryDB) AddBlock(blocks *common.Block) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// addBlocks insert blocks into the DB. TODO: move method to test
|
||||
func (hdb *HistoryDB) addBlocks(blocks []common.Block) error {
|
||||
return db.BulkInsert(
|
||||
hdb.db,
|
||||
"INSERT INTO block (eth_block_num, timestamp, hash) VALUES %s",
|
||||
blocks[:],
|
||||
)
|
||||
func (hdb *HistoryDB) AddBlock(block *common.Block) error {
|
||||
return meddler.Insert(hdb.db, "block", block)
|
||||
}
|
||||
|
||||
// GetBlock retrieve a block from the DB, given a block number
|
||||
func (hdb *HistoryDB) GetBlock(blockNum uint64) (*common.Block, error) {
|
||||
return nil, nil
|
||||
block := &common.Block{}
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, block,
|
||||
"SELECT * FROM block WHERE eth_block_num = $1;", blockNum,
|
||||
)
|
||||
return block, err
|
||||
}
|
||||
|
||||
// GetBlocks retrieve blocks from the DB, given a range of block numbers defined by from and to
|
||||
@@ -72,16 +68,58 @@ func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) {
|
||||
|
||||
// GetLastBlock retrieve the block with the highest block number from the DB
|
||||
func (hdb *HistoryDB) GetLastBlock() (*common.Block, error) {
|
||||
return nil, nil
|
||||
block := &common.Block{}
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, block, "SELECT * FROM block ORDER BY eth_block_num DESC LIMIT 1;",
|
||||
)
|
||||
return block, err
|
||||
}
|
||||
|
||||
// addBatches insert Bids into the DB
|
||||
func (hdb *HistoryDB) addBatches(batches []common.Batch) error {
|
||||
return db.BulkInsert(
|
||||
hdb.db,
|
||||
`INSERT INTO batch (
|
||||
batch_num,
|
||||
eth_block_num,
|
||||
forger_addr,
|
||||
fees_collected,
|
||||
state_root,
|
||||
num_accounts,
|
||||
exit_root,
|
||||
forge_l1_txs_num,
|
||||
slot_num
|
||||
) VALUES %s;`,
|
||||
batches[:],
|
||||
)
|
||||
}
|
||||
|
||||
// GetBatches retrieve batches from the DB, given a range of batch numbers defined by from and to
|
||||
func (hdb *HistoryDB) GetBatches(from, to common.BatchNum) ([]*common.Batch, error) {
|
||||
var batches []*common.Batch
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &batches,
|
||||
"SELECT * FROM batch WHERE $1 <= batch_num AND batch_num < $2",
|
||||
from, to,
|
||||
)
|
||||
return batches, err
|
||||
}
|
||||
|
||||
// GetLastBatchNum returns the BatchNum of the latest forged batch
|
||||
func (hdb *HistoryDB) GetLastBatchNum() (*common.BatchNum, error) {
|
||||
return nil, nil
|
||||
func (hdb *HistoryDB) GetLastBatchNum() (common.BatchNum, error) {
|
||||
row := hdb.db.QueryRow("SELECT batch_num FROM batch ORDER BY batch_num DESC LIMIT 1;")
|
||||
var batchNum common.BatchNum
|
||||
return batchNum, row.Scan(&batchNum)
|
||||
}
|
||||
|
||||
// GetLastL1TxsNum returns the greatest ForgeL1TxsNum in the DB
|
||||
func (hdb *HistoryDB) GetLastL1TxsNum() (uint32, error) {
|
||||
row := hdb.db.QueryRow("SELECT MAX(forge_l1_txs_num) FROM batch;")
|
||||
var lastL1TxsNum uint32
|
||||
return lastL1TxsNum, row.Scan(&lastL1TxsNum)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
_, err := hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
|
||||
return err
|
||||
@@ -93,12 +131,16 @@ func (hdb *HistoryDB) SyncRollup(
|
||||
l1txs []common.L1Tx,
|
||||
l2txs []common.L2Tx,
|
||||
registeredAccounts []common.Account,
|
||||
exitTree common.ExitTreeLeaf,
|
||||
withdrawals common.ExitTreeLeaf,
|
||||
exitTree common.ExitInfo,
|
||||
withdrawals common.ExitInfo,
|
||||
registeredTokens []common.Token,
|
||||
batch *common.Batch,
|
||||
batches []common.Batch,
|
||||
vars *common.RollupVars,
|
||||
) error {
|
||||
// TODO: make all in a single DB commit
|
||||
if err := hdb.addBatches(batches); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -107,7 +149,7 @@ func (hdb *HistoryDB) SyncPoD(
|
||||
blockNum uint64,
|
||||
bids []common.Bid,
|
||||
coordinators []common.Coordinator,
|
||||
vars *common.PoDVars,
|
||||
vars *common.AuctionVars,
|
||||
) error {
|
||||
return nil
|
||||
}
|
||||
@@ -122,13 +164,13 @@ func (hdb *HistoryDB) addBids(bids []common.Bid) error {
|
||||
)
|
||||
}
|
||||
|
||||
// GetBidsByBlock return the bids done between the block from and to
|
||||
func (hdb *HistoryDB) GetBidsByBlock(from, to uint64) ([]*common.Bid, error) {
|
||||
// GetBidsBySlot return the bids for a specific slot
|
||||
func (hdb *HistoryDB) GetBidsBySlot(slotNum common.SlotNum) ([]*common.Bid, error) {
|
||||
var bids []*common.Bid
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &bids,
|
||||
"SELECT * FROM bid WHERE $1 <= eth_block_num AND eth_block_num < $2",
|
||||
from, to,
|
||||
"SELECT * FROM bid WHERE $1 = slot_num;",
|
||||
slotNum,
|
||||
)
|
||||
return bids, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user