Update Synchroinzer and tests

- Test L1UserTxs in Synchronizer
- Test Batches in Synchronizer
- Add last_idx in `TABLE batch` in HistoryDB
- Minor updates in til to satisfy blockchain constraints
This commit is contained in:
Eduard S
2020-10-22 18:51:42 +02:00
parent 2aa5598975
commit b5222303c2
8 changed files with 308 additions and 200 deletions

View File

@@ -71,6 +71,16 @@ func (hdb *HistoryDB) GetBlock(blockNum int64) (*common.Block, error) {
return block, err
}
// GetAllBlocks retrieve all blocks from the DB
func (hdb *HistoryDB) GetAllBlocks() ([]common.Block, error) {
var blocks []*common.Block
err := meddler.QueryAll(
hdb.db, &blocks,
"SELECT * FROM block;",
)
return db.SlicePtrsToSlice(blocks).([]common.Block), err
}
// GetBlocks retrieve blocks from the DB, given a range of block numbers defined by from and to
func (hdb *HistoryDB) GetBlocks(from, to int64) ([]common.Block, error) {
var blocks []*common.Block
@@ -257,6 +267,16 @@ func (hdb *HistoryDB) GetBatchesAPI(
}, nil
}
// GetAllBatches retrieve all batches from the DB
func (hdb *HistoryDB) GetAllBatches() ([]common.Batch, error) {
var batches []*common.Batch
err := meddler.QueryAll(
hdb.db, &batches,
"SELECT * FROM batch;",
)
return db.SlicePtrsToSlice(batches).([]common.Batch), err
}
// 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
@@ -898,6 +918,19 @@ func (hdb *HistoryDB) GetExits(
// )
// }
// GetAllL1UserTxs returns all L1UserTxs from the DB
func (hdb *HistoryDB) GetAllL1UserTxs() ([]common.L1Tx, error) {
var txs []*common.L1Tx
err := meddler.QueryAll(
hdb.db, &txs,
`SELECT tx.id, tx.to_forge_l1_txs_num, tx.position, tx.user_origin,
tx.from_idx, tx.from_eth_addr, tx.from_bjj, tx.to_idx, tx.token_id, tx.amount,
tx.load_amount, tx.eth_block_num, tx.type, tx.batch_num
FROM tx WHERE is_l1 = TRUE AND user_origin = TRUE;`,
)
return db.SlicePtrsToSlice(txs).([]common.L1Tx), err
}
// GetL1UserTxs gets L1 User Txs to be forged in the L1Batch with toForgeL1TxsNum.
func (hdb *HistoryDB) GetL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx, error) {
var txs []*common.L1Tx

View File

@@ -1,12 +1,12 @@
package historydb
import (
"fmt"
"math"
"math/big"
"os"
"testing"
ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/hermeznetwork/hermez-node/common"
dbUtils "github.com/hermeznetwork/hermez-node/db"
"github.com/hermeznetwork/hermez-node/log"
@@ -406,25 +406,10 @@ func TestGetL1UserTxs(t *testing.T) {
require.Equal(t, 5, len(blocks[0].L1UserTxs))
// fmt.Printf("DBG Blocks: %+v\n", blocks)
// TODO: Move this logic to `func (tc *TestContext) GenerateBlocks(set string) ([]common.BlockData, error)`
toForgeL1TxsNum := int64(1)
for i := range blocks {
block := &blocks[i]
block.Block.EthBlockNum = int64(i)
for j := range block.AddedTokens {
token := &block.AddedTokens[j]
token.EthAddr = ethCommon.BigToAddress(big.NewInt(int64(i*len(blocks) + j)))
}
for j := range block.L1UserTxs {
l1Tx := &block.L1UserTxs[j]
l1Tx.UserOrigin = true
l1Tx.Position = j
l1Tx.ToForgeL1TxsNum = &toForgeL1TxsNum
}
}
for i := range blocks {
// fmt.Printf("DBG %+v\n", blocks[i])
fmt.Printf("DBG %+v\n", blocks[i])
// fmt.Printf("DBG Batches %+v\n", blocks[i].Batches)
// for _, l1Tx := range blocks[i].L1UserTxs {
// fmt.Printf("DBG l1UserTx %+v\n", l1Tx)
@@ -433,25 +418,6 @@ func TestGetL1UserTxs(t *testing.T) {
require.Nil(t, err)
}
// // TODO: Use til to generate a set with some L1UserTxs
// l1Txs := []common.L1Tx{}
// l1Tx, err := common.NewL1Tx(&common.L1Tx{
// ToForgeL1TxsNum: &toForgeL1TxsNum,
// Position: 0,
// UserOrigin: true,
// FromIdx: 0,
// FromEthAddr: ethCommon.Address{}, // ethCommon.HexToAddress("0xff"),
// FromBJJ: nil,
// ToIdx: 0,
// TokenID: 1,
// Amount: big.NewInt(0),
// LoadAmount: big.NewInt(0),
// })
// require.Nil(t, err)
// l1Txs = append(l1Txs, *l1Tx)
// require.Nil(t, historyDB.AddL1Txs(l1Txs))
l1UserTxs, err := historyDB.GetL1UserTxs(toForgeL1TxsNum)
require.Nil(t, err)
assert.Equal(t, 5, len(l1UserTxs))

View File

@@ -23,6 +23,7 @@ CREATE TABLE batch (
fees_collected BYTEA NOT NULL,
state_root BYTEA NOT NULL,
num_accounts BIGINT NOT NULL,
last_idx BIGINT NOT NULL,
exit_root BYTEA NOT NULL,
forge_l1_txs_num BIGINT,
slot_num BIGINT NOT NULL,