Browse Source

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

feature/sql-semaphore1
Eduard S 4 years ago
parent
commit
56604ecc69
5 changed files with 17 additions and 16 deletions
  1. +1
    -1
      common/batch.go
  2. +1
    -1
      common/bid.go
  3. +2
    -1
      common/block.go
  4. +5
    -5
      db/historydb/historydb.go
  5. +8
    -8
      db/historydb/historydb_test.go

+ 1
- 1
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"`

+ 1
- 1
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"`
}

+ 2
- 1
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:"-"`
}

+ 5
- 5
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,

+ 8
- 8
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{

Loading…
Cancel
Save