Use int64 for blockNum (like it's sroted in DB)

This commit is contained in:
Eduard S
2020-09-07 18:09:27 +02:00
parent 57fd9bdaed
commit 56604ecc69
5 changed files with 17 additions and 16 deletions

View File

@@ -13,7 +13,7 @@ const batchNumBytesLen = 4
// Batch is a struct that represents Hermez network batch // Batch is a struct that represents Hermez network batch
type Batch struct { type Batch struct {
BatchNum BatchNum `meddler:"batch_num"` 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? ForgerAddr ethCommon.Address `meddler:"forger_addr"` // TODO: Should this be retrieved via slot reference?
CollectedFees map[TokenID]*big.Int `meddler:"fees_collected,json"` CollectedFees map[TokenID]*big.Int `meddler:"fees_collected,json"`
StateRoot Hash `meddler:"state_root"` StateRoot Hash `meddler:"state_root"`

View File

@@ -11,5 +11,5 @@ type Bid struct {
SlotNum SlotNum `meddler:"slot_num"` SlotNum SlotNum `meddler:"slot_num"`
ForgerAddr ethCommon.Address `meddler:"forger_addr"` // Coordinator reference ForgerAddr ethCommon.Address `meddler:"forger_addr"` // Coordinator reference
BidValue *big.Int `meddler:"bid_value,bigint"` BidValue *big.Int `meddler:"bid_value,bigint"`
EthBlockNum uint64 `meddler:"eth_block_num"` EthBlockNum int64 `meddler:"eth_block_num"`
} }

View File

@@ -8,7 +8,8 @@ import (
// Block represents of an Ethereum block // Block represents of an Ethereum block
type Block struct { type Block struct {
EthBlockNum uint64 `meddler:"eth_block_num"` EthBlockNum int64 `meddler:"eth_block_num"`
Timestamp time.Time `meddler:"timestamp"` Timestamp time.Time `meddler:"timestamp"`
Hash ethCommon.Hash `meddler:"hash"` Hash ethCommon.Hash `meddler:"hash"`
ParentHash ethCommon.Hash `meddler:"-"`
} }

View File

@@ -48,7 +48,7 @@ func (hdb *HistoryDB) AddBlock(block *common.Block) error {
} }
// GetBlock retrieve a block from the DB, given a block number // 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{} block := &common.Block{}
err := meddler.QueryRow( err := meddler.QueryRow(
hdb.db, block, 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 // 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 var blocks []*common.Block
err := meddler.QueryAll( err := meddler.QueryAll(
hdb.db, &blocks, 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 // 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) _, err := hdb.db.Exec("DELETE FROM block WHERE eth_block_num > $1;", lastValidBlock)
return err return err
} }
// SyncRollup stores all the data that can be changed / added on a block in the Rollup SC // SyncRollup stores all the data that can be changed / added on a block in the Rollup SC
func (hdb *HistoryDB) SyncRollup( func (hdb *HistoryDB) SyncRollup(
blockNum uint64, blockNum int64,
l1txs []common.L1Tx, l1txs []common.L1Tx,
l2txs []common.L2Tx, l2txs []common.L2Tx,
registeredAccounts []common.Account, 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 // SyncPoD stores all the data that can be changed / added on a block in the PoD SC
func (hdb *HistoryDB) SyncPoD( func (hdb *HistoryDB) SyncPoD(
blockNum uint64, blockNum int64,
bids []common.Bid, bids []common.Bid,
coordinators []common.Coordinator, coordinators []common.Coordinator,
vars *common.AuctionVars, vars *common.AuctionVars,

View File

@@ -41,7 +41,7 @@ func TestMain(m *testing.M) {
} }
func TestBlocks(t *testing.T) { func TestBlocks(t *testing.T) {
var fromBlock, toBlock uint64 var fromBlock, toBlock int64
fromBlock = 1 fromBlock = 1
toBlock = 5 toBlock = 5
// Delete peviously created rows (clean previous test execs) // 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) { func TestBatches(t *testing.T) {
const fromBlock uint64 = 1 const fromBlock int64 = 1
const toBlock uint64 = 3 const toBlock int64 = 3
const nBatchesPerBlock = 3 const nBatchesPerBlock = 3
// Prepare blocks in the DB // Prepare blocks in the DB
setTestBlocks(fromBlock, toBlock) setTestBlocks(fromBlock, toBlock)
@@ -95,7 +95,7 @@ func TestBatches(t *testing.T) {
for j := 0; j < nBatchesPerBlock; j++ { for j := 0; j < nBatchesPerBlock; j++ {
batch := common.Batch{ batch := common.Batch{
BatchNum: common.BatchNum(int(i-1)*nBatchesPerBlock + j), BatchNum: common.BatchNum(int(i-1)*nBatchesPerBlock + j),
EthBlockNum: uint64(i), EthBlockNum: int64(i),
ForgerAddr: eth.BigToAddress(big.NewInt(239457111187)), ForgerAddr: eth.BigToAddress(big.NewInt(239457111187)),
CollectedFees: collectedFees, CollectedFees: collectedFees,
StateRoot: common.Hash([]byte("duhdqlwiucgwqeiu")), StateRoot: common.Hash([]byte("duhdqlwiucgwqeiu")),
@@ -129,8 +129,8 @@ func TestBatches(t *testing.T) {
} }
func TestBids(t *testing.T) { func TestBids(t *testing.T) {
const fromBlock uint64 = 1 const fromBlock int64 = 1
const toBlock uint64 = 5 const toBlock int64 = 5
const bidsPerSlot = 5 const bidsPerSlot = 5
// Prepare blocks in the DB // Prepare blocks in the DB
setTestBlocks(fromBlock, toBlock) setTestBlocks(fromBlock, toBlock)
@@ -162,7 +162,7 @@ func TestBids(t *testing.T) {
} }
// setTestBlocks WARNING: this will delete the blocks and recreate them // setTestBlocks WARNING: this will delete the blocks and recreate them
func setTestBlocks(from, to uint64) { func setTestBlocks(from, to int64) {
if from == 0 { if from == 0 {
if err := historyDB.Reorg(from); err != nil { if err := historyDB.Reorg(from); err != nil {
panic(err) 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 var blocks []common.Block
for i := from; i < to; i++ { for i := from; i < to; i++ {
blocks = append(blocks, common.Block{ blocks = append(blocks, common.Block{