mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Impl historyDB methods for sync main loop
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
eth "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,7 @@ func TestMain(m *testing.M) {
|
||||
os.Exit(result)
|
||||
}
|
||||
|
||||
func TestAddBlock(t *testing.T) {
|
||||
func TestBlocks(t *testing.T) {
|
||||
var fromBlock, toBlock uint64
|
||||
fromBlock = 1
|
||||
toBlock = 5
|
||||
@@ -48,17 +49,83 @@ func TestAddBlock(t *testing.T) {
|
||||
// Generate fake blocks
|
||||
blocks := genBlocks(fromBlock, toBlock)
|
||||
// Insert blocks into DB
|
||||
err := historyDB.addBlocks(blocks)
|
||||
assert.NoError(t, err)
|
||||
// Get blocks from DB
|
||||
for _, block := range blocks {
|
||||
err := historyDB.AddBlock(&block)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
// Get all blocks from DB
|
||||
fetchedBlocks, err := historyDB.GetBlocks(fromBlock, toBlock)
|
||||
assert.Equal(t, len(blocks), len(fetchedBlocks))
|
||||
// Compare generated vs getted blocks
|
||||
assert.NoError(t, err)
|
||||
for i, fetchedBlock := range fetchedBlocks {
|
||||
assert.Equal(t, blocks[i].EthBlockNum, fetchedBlock.EthBlockNum)
|
||||
assert.Equal(t, blocks[i].Hash, fetchedBlock.Hash)
|
||||
assert.Equal(t, blocks[i].Timestamp.Unix(), fetchedBlock.Timestamp.Unix())
|
||||
assertEqualBlock(t, &blocks[i], fetchedBlock)
|
||||
}
|
||||
// Get blocks from the DB one by one
|
||||
for i := fromBlock; i < toBlock; i++ {
|
||||
fetchedBlock, err := historyDB.GetBlock(i)
|
||||
assert.NoError(t, err)
|
||||
assertEqualBlock(t, &blocks[i-1], fetchedBlock)
|
||||
}
|
||||
// Get last block
|
||||
lastBlock, err := historyDB.GetLastBlock()
|
||||
assert.NoError(t, err)
|
||||
assertEqualBlock(t, &blocks[len(blocks)-1], lastBlock)
|
||||
}
|
||||
|
||||
func assertEqualBlock(t *testing.T, expected *common.Block, actual *common.Block) {
|
||||
assert.Equal(t, expected.EthBlockNum, actual.EthBlockNum)
|
||||
assert.Equal(t, expected.Hash, actual.Hash)
|
||||
assert.Equal(t, expected.Timestamp.Unix(), actual.Timestamp.Unix())
|
||||
}
|
||||
|
||||
func TestBatches(t *testing.T) {
|
||||
const fromBlock uint64 = 1
|
||||
const toBlock uint64 = 3
|
||||
const nBatchesPerBlock = 3
|
||||
// Prepare blocks in the DB
|
||||
setTestBlocks(fromBlock, toBlock)
|
||||
// Generate fake batches
|
||||
var batches []common.Batch
|
||||
collectedFees := make(map[common.TokenID]*big.Int)
|
||||
for i := 0; i < 64; i++ {
|
||||
collectedFees[common.TokenID(i)] = big.NewInt(int64(i))
|
||||
}
|
||||
for i := fromBlock; i < toBlock; i++ {
|
||||
for j := 0; j < nBatchesPerBlock; j++ {
|
||||
batch := common.Batch{
|
||||
BatchNum: common.BatchNum(int(i-1)*nBatchesPerBlock + j),
|
||||
EthBlockNum: uint64(i),
|
||||
ForgerAddr: eth.BigToAddress(big.NewInt(239457111187)),
|
||||
CollectedFees: collectedFees,
|
||||
StateRoot: common.Hash([]byte("duhdqlwiucgwqeiu")),
|
||||
NumAccounts: j,
|
||||
ExitRoot: common.Hash([]byte("tykertheuhtgenuer3iuw3b")),
|
||||
SlotNum: common.SlotNum(j),
|
||||
}
|
||||
if j%2 == 0 {
|
||||
batch.ForgeL1TxsNum = uint32(i)
|
||||
}
|
||||
batches = append(batches, batch)
|
||||
}
|
||||
}
|
||||
// Add batches to the DB
|
||||
err := historyDB.addBatches(batches)
|
||||
assert.NoError(t, err)
|
||||
// Get batches from the DB
|
||||
fetchedBatches, err := historyDB.GetBatches(0, common.BatchNum(int(toBlock-fromBlock)*nBatchesPerBlock))
|
||||
assert.NoError(t, err)
|
||||
for i, fetchedBatch := range fetchedBatches {
|
||||
assert.Equal(t, batches[i], *fetchedBatch)
|
||||
}
|
||||
// Test GetLastBatchNum
|
||||
fetchedLastBatchNum, err := historyDB.GetLastBatchNum()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, batches[len(batches)-1].BatchNum, fetchedLastBatchNum)
|
||||
// Test GetLastL1TxsNum
|
||||
fetchedLastL1TxsNum, err := historyDB.GetLastL1TxsNum()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, batches[len(batches)-1-(int(toBlock-fromBlock+1)%nBatchesPerBlock)].ForgeL1TxsNum, fetchedLastL1TxsNum)
|
||||
}
|
||||
|
||||
func TestBids(t *testing.T) {
|
||||
@@ -82,14 +149,16 @@ func TestBids(t *testing.T) {
|
||||
err := historyDB.addBids(bids)
|
||||
assert.NoError(t, err)
|
||||
// Fetch bids
|
||||
fetchedBidsPtr, err := historyDB.GetBidsByBlock(fromBlock, toBlock)
|
||||
assert.NoError(t, err)
|
||||
// Compare fetched bids vs generated bids
|
||||
fetchedBids := make([]common.Bid, 0, (toBlock-fromBlock)*bidsPerSlot)
|
||||
for _, bid := range fetchedBidsPtr {
|
||||
fetchedBids = append(fetchedBids, *bid)
|
||||
var fetchedBids []*common.Bid
|
||||
for i := fromBlock; i < toBlock; i++ {
|
||||
fetchedBidsSlot, err := historyDB.GetBidsBySlot(common.SlotNum(i))
|
||||
assert.NoError(t, err)
|
||||
fetchedBids = append(fetchedBids, fetchedBidsSlot...)
|
||||
}
|
||||
// Compare fetched bids vs generated bids
|
||||
for i, bid := range fetchedBids {
|
||||
assert.Equal(t, bids[i], *bid)
|
||||
}
|
||||
assert.Equal(t, bids, fetchedBids)
|
||||
}
|
||||
|
||||
// setTestBlocks WARNING: this will delete the blocks and recreate them
|
||||
@@ -104,7 +173,7 @@ func setTestBlocks(from, to uint64) {
|
||||
}
|
||||
}
|
||||
blocks := genBlocks(from, to)
|
||||
if err := historyDB.addBlocks(blocks); err != nil {
|
||||
if err := addBlocks(blocks); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@@ -120,3 +189,12 @@ func genBlocks(from, to uint64) []common.Block {
|
||||
}
|
||||
return blocks
|
||||
}
|
||||
|
||||
// addBlocks insert blocks into the DB. TODO: move method to test
|
||||
func addBlocks(blocks []common.Block) error {
|
||||
return db.BulkInsert(
|
||||
historyDB.db,
|
||||
"INSERT INTO block (eth_block_num, timestamp, hash) VALUES %s",
|
||||
blocks[:],
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user