From 56604ecc697861ead13cd22184380f8ffa965911 Mon Sep 17 00:00:00 2001 From: Eduard S Date: Mon, 7 Sep 2020 18:09:27 +0200 Subject: [PATCH] Use int64 for blockNum (like it's sroted in DB) --- common/batch.go | 2 +- common/bid.go | 2 +- common/block.go | 3 ++- db/historydb/historydb.go | 10 +++++----- db/historydb/historydb_test.go | 16 ++++++++-------- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/common/batch.go b/common/batch.go index ef616ef..9a43503 100644 --- a/common/batch.go +++ b/common/batch.go @@ -13,7 +13,7 @@ const batchNumBytesLen = 4 // Batch is a struct that represents Hermez network batch type Batch struct { BatchNum BatchNum `meddler:"batch_num"` - EthBlockNum uint64 `meddler:"eth_block_num"` // Ethereum block in which the batch is forged + EthBlockNum int64 `meddler:"eth_block_num"` // Ethereum block in which the batch is forged ForgerAddr ethCommon.Address `meddler:"forger_addr"` // TODO: Should this be retrieved via slot reference? CollectedFees map[TokenID]*big.Int `meddler:"fees_collected,json"` StateRoot Hash `meddler:"state_root"` diff --git a/common/bid.go b/common/bid.go index 3e349d3..ea0c8f8 100644 --- a/common/bid.go +++ b/common/bid.go @@ -11,5 +11,5 @@ type Bid struct { SlotNum SlotNum `meddler:"slot_num"` ForgerAddr ethCommon.Address `meddler:"forger_addr"` // Coordinator reference BidValue *big.Int `meddler:"bid_value,bigint"` - EthBlockNum uint64 `meddler:"eth_block_num"` + EthBlockNum int64 `meddler:"eth_block_num"` } diff --git a/common/block.go b/common/block.go index 377ad3b..df74d4b 100644 --- a/common/block.go +++ b/common/block.go @@ -8,7 +8,8 @@ import ( // Block represents of an Ethereum block type Block struct { - EthBlockNum uint64 `meddler:"eth_block_num"` + EthBlockNum int64 `meddler:"eth_block_num"` Timestamp time.Time `meddler:"timestamp"` Hash ethCommon.Hash `meddler:"hash"` + ParentHash ethCommon.Hash `meddler:"-"` } diff --git a/db/historydb/historydb.go b/db/historydb/historydb.go index 9a9b249..5033b0c 100644 --- a/db/historydb/historydb.go +++ b/db/historydb/historydb.go @@ -48,7 +48,7 @@ func (hdb *HistoryDB) AddBlock(block *common.Block) error { } // GetBlock retrieve a block from the DB, given a block number -func (hdb *HistoryDB) GetBlock(blockNum uint64) (*common.Block, error) { +func (hdb *HistoryDB) GetBlock(blockNum int64) (*common.Block, error) { block := &common.Block{} err := meddler.QueryRow( hdb.db, block, @@ -58,7 +58,7 @@ func (hdb *HistoryDB) GetBlock(blockNum uint64) (*common.Block, error) { } // 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) { +func (hdb *HistoryDB) GetBlocks(from, to int64) ([]*common.Block, error) { var blocks []*common.Block err := meddler.QueryAll( hdb.db, &blocks, @@ -122,14 +122,14 @@ func (hdb *HistoryDB) GetLastL1TxsNum() (uint32, error) { } // Reorg deletes all the information that was added into the DB after the lastValidBlock -func (hdb *HistoryDB) Reorg(lastValidBlock uint64) error { +func (hdb *HistoryDB) Reorg(lastValidBlock int64) error { _, err := hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock) return err } // SyncRollup stores all the data that can be changed / added on a block in the Rollup SC func (hdb *HistoryDB) SyncRollup( - blockNum uint64, + blockNum int64, l1txs []common.L1Tx, l2txs []common.L2Tx, registeredAccounts []common.Account, @@ -148,7 +148,7 @@ func (hdb *HistoryDB) SyncRollup( // SyncPoD stores all the data that can be changed / added on a block in the PoD SC func (hdb *HistoryDB) SyncPoD( - blockNum uint64, + blockNum int64, bids []common.Bid, coordinators []common.Coordinator, vars *common.AuctionVars, diff --git a/db/historydb/historydb_test.go b/db/historydb/historydb_test.go index b634918..c08143f 100644 --- a/db/historydb/historydb_test.go +++ b/db/historydb/historydb_test.go @@ -41,7 +41,7 @@ func TestMain(m *testing.M) { } func TestBlocks(t *testing.T) { - var fromBlock, toBlock uint64 + var fromBlock, toBlock int64 fromBlock = 1 toBlock = 5 // Delete peviously created rows (clean previous test execs) @@ -80,8 +80,8 @@ func assertEqualBlock(t *testing.T, expected *common.Block, actual *common.Block } func TestBatches(t *testing.T) { - const fromBlock uint64 = 1 - const toBlock uint64 = 3 + const fromBlock int64 = 1 + const toBlock int64 = 3 const nBatchesPerBlock = 3 // Prepare blocks in the DB setTestBlocks(fromBlock, toBlock) @@ -95,7 +95,7 @@ func TestBatches(t *testing.T) { for j := 0; j < nBatchesPerBlock; j++ { batch := common.Batch{ BatchNum: common.BatchNum(int(i-1)*nBatchesPerBlock + j), - EthBlockNum: uint64(i), + EthBlockNum: int64(i), ForgerAddr: eth.BigToAddress(big.NewInt(239457111187)), CollectedFees: collectedFees, StateRoot: common.Hash([]byte("duhdqlwiucgwqeiu")), @@ -129,8 +129,8 @@ func TestBatches(t *testing.T) { } func TestBids(t *testing.T) { - const fromBlock uint64 = 1 - const toBlock uint64 = 5 + const fromBlock int64 = 1 + const toBlock int64 = 5 const bidsPerSlot = 5 // Prepare blocks in the DB setTestBlocks(fromBlock, toBlock) @@ -162,7 +162,7 @@ func TestBids(t *testing.T) { } // setTestBlocks WARNING: this will delete the blocks and recreate them -func setTestBlocks(from, to uint64) { +func setTestBlocks(from, to int64) { if from == 0 { if err := historyDB.Reorg(from); err != nil { panic(err) @@ -178,7 +178,7 @@ func setTestBlocks(from, to uint64) { } } -func genBlocks(from, to uint64) []common.Block { +func genBlocks(from, to int64) []common.Block { var blocks []common.Block for i := from; i < to; i++ { blocks = append(blocks, common.Block{