mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Merge pull request #67 from hermeznetwork/feature/sql-sc-vars
Add sync interface
This commit is contained in:
@@ -10,20 +10,15 @@ import (
|
|||||||
|
|
||||||
// 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
|
BatchNum BatchNum `meddler:"batch_num"`
|
||||||
SlotNum SlotNum // Slot in which the batch is forged
|
EthBlockNum uint64 `meddler:"eth_block_num"` // Ethereum block in which the batch is forged
|
||||||
EthTxHash ethCommon.Hash
|
ForgerAddr ethCommon.Address `meddler:"forger_addr"` // TODO: Should this be retrieved via slot reference?
|
||||||
EthBlockNum uint64 // Ethereum block in which the batch is forged
|
CollectedFees map[TokenID]*big.Int `meddler:"fees_collected,json"`
|
||||||
ExitRoot Hash
|
StateRoot Hash `meddler:"state_root"`
|
||||||
OldStateRoot Hash
|
NumAccounts int `meddler:"num_accounts"`
|
||||||
NewStateRoot Hash
|
ExitRoot Hash `meddler:"exit_root"`
|
||||||
OldNumAccounts int
|
ForgeL1TxsNum uint32 `meddler:"forge_l1_txs_num"` // optional, Only when the batch forges L1 txs. Identifier that corresponds to the group of L1 txs forged in the current batch.
|
||||||
NewNumAccounts int
|
SlotNum SlotNum `meddler:"slot_num"` // Slot in which the batch is forged
|
||||||
ToForgeL1TxsNum uint32 // optional, Only when the batch forges L1 txs. Identifier that corresponds to the group of L1 txs forged in the current batch.
|
|
||||||
ToForgeL1TxsHash ethCommon.Hash // optional, Only when the batch forges L1 txs. Frozen from pendingL1TxsHash (which are the group of L1UserTxs), to be forged in ToForgeL1TxsNum + 1.
|
|
||||||
ForgedL1TxsHash ethCommon.Hash // optional, Only when the batch forges L1 txs. This will be the Hash of the group of L1 txs (L1UserTxs + L1CoordinatorTx) forged in the current batch.
|
|
||||||
CollectedFees map[TokenID]*big.Int
|
|
||||||
ForgerAddr ethCommon.Address // TODO: Should this be retrieved via slot reference?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BatchNum identifies a batch
|
// BatchNum identifies a batch
|
||||||
|
|||||||
12
common/exittree.go
Normal file
12
common/exittree.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExitInfo struct {
|
||||||
|
AccountIdx Idx
|
||||||
|
MerkleProof []byte
|
||||||
|
Balance *big.Int
|
||||||
|
Nullifier *big.Int
|
||||||
|
}
|
||||||
@@ -22,8 +22,7 @@ type L1Tx struct {
|
|||||||
Amount *big.Int `meddler:"amount,bigint"`
|
Amount *big.Int `meddler:"amount,bigint"`
|
||||||
LoadAmount *big.Int `meddler:"load_amount,bigint"`
|
LoadAmount *big.Int `meddler:"load_amount,bigint"`
|
||||||
EthBlockNum uint64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
|
EthBlockNum uint64 `meddler:"eth_block_num"` // Ethereum Block Number in which this L1Tx was added to the queue
|
||||||
// Extra metadata, may be uninitialized
|
Type TxType `meddler:"tx_type"`
|
||||||
Type TxType `meddler:"-"` // optional, descrives which kind of tx it's
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *L1Tx) Tx() *Tx {
|
func (tx *L1Tx) Tx() *Tx {
|
||||||
@@ -32,8 +31,6 @@ func (tx *L1Tx) Tx() *Tx {
|
|||||||
FromIdx: tx.FromIdx,
|
FromIdx: tx.FromIdx,
|
||||||
ToIdx: tx.ToIdx,
|
ToIdx: tx.ToIdx,
|
||||||
Amount: tx.Amount,
|
Amount: tx.Amount,
|
||||||
Nonce: 0,
|
|
||||||
Fee: 0,
|
|
||||||
Type: tx.Type,
|
Type: tx.Type,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,7 @@ type L2Tx struct {
|
|||||||
Amount *big.Int `meddler:"amount,bigint"`
|
Amount *big.Int `meddler:"amount,bigint"`
|
||||||
Fee FeeSelector `meddler:"fee"`
|
Fee FeeSelector `meddler:"fee"`
|
||||||
Nonce Nonce `meddler:"nonce"`
|
Nonce Nonce `meddler:"nonce"`
|
||||||
// Extra metadata, may be uninitialized
|
Type TxType `meddler:"tx_type"`
|
||||||
Type TxType `meddler:"-"` // optional, descrives which kind of tx it's
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *L2Tx) Tx() *Tx {
|
func (tx *L2Tx) Tx() *Tx {
|
||||||
|
|||||||
@@ -62,8 +62,8 @@ type PoolL2Tx struct {
|
|||||||
RqNonce uint64 `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
|
RqNonce uint64 `meddler:"rq_nonce,zeroisnull"` // effective 48 bits used
|
||||||
AbsoluteFee float64 `meddler:"absolute_fee,zeroisnull"`
|
AbsoluteFee float64 `meddler:"absolute_fee,zeroisnull"`
|
||||||
AbsoluteFeeUpdate time.Time `meddler:"absolute_fee_update,utctimez"`
|
AbsoluteFeeUpdate time.Time `meddler:"absolute_fee_update,utctimez"`
|
||||||
|
Type TxType `meddler:"tx_type"`
|
||||||
// Extra metadata, may be uninitialized
|
// Extra metadata, may be uninitialized
|
||||||
Type TxType `meddler:"-"` // optional, descrives which kind of tx it's
|
|
||||||
RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
|
RqTxCompressedData []byte `meddler:"-"` // 253 bits, optional for atomic txs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
37
common/scvars.go
Normal file
37
common/scvars.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
|
||||||
|
eth "github.com/ethereum/go-ethereum/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RollupVars struct {
|
||||||
|
EthBlockNum uint64
|
||||||
|
ForgeL1Timeout *big.Int
|
||||||
|
FeeL1UserTx *big.Int
|
||||||
|
FeeAddToken *big.Int
|
||||||
|
TokensHEZ eth.Address
|
||||||
|
Governance eth.Address
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuctionVars struct {
|
||||||
|
EthBlockNum uint64
|
||||||
|
SlotDeadline uint
|
||||||
|
CloseAuctionSlots uint
|
||||||
|
OpenAuctionSlots uint
|
||||||
|
Governance eth.Address
|
||||||
|
MinBidSlots MinBidSlots
|
||||||
|
Outbidding int
|
||||||
|
DonationAddress eth.Address
|
||||||
|
GovernanceAddress eth.Address
|
||||||
|
AllocationRatio AllocationRatio
|
||||||
|
}
|
||||||
|
|
||||||
|
type MinBidSlots [6]uint
|
||||||
|
|
||||||
|
type AllocationRatio struct {
|
||||||
|
Donation uint
|
||||||
|
Burn uint
|
||||||
|
Forger uint
|
||||||
|
}
|
||||||
@@ -40,16 +40,22 @@ func NewHistoryDB(port int, host, user, password, dbname string) (*HistoryDB, er
|
|||||||
return &HistoryDB{hdb}, nil
|
return &HistoryDB{hdb}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// addBlocks insert blocks into the DB
|
// AddBlock insert a block into the DB
|
||||||
func (hdb *HistoryDB) addBlocks(blocks []common.Block) error {
|
func (hdb *HistoryDB) AddBlock(block *common.Block) error {
|
||||||
return db.BulkInsert(
|
return meddler.Insert(hdb.db, "block", block)
|
||||||
hdb.db,
|
|
||||||
"INSERT INTO block (eth_block_num, timestamp, hash) VALUES %s",
|
|
||||||
blocks[:],
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBlocks retrrieve blocks from the DB
|
// GetBlock retrieve a block from the DB, given a block number
|
||||||
|
func (hdb *HistoryDB) GetBlock(blockNum uint64) (*common.Block, error) {
|
||||||
|
block := &common.Block{}
|
||||||
|
err := meddler.QueryRow(
|
||||||
|
hdb.db, block,
|
||||||
|
"SELECT * FROM block WHERE eth_block_num = $1;", blockNum,
|
||||||
|
)
|
||||||
|
return 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 uint64) ([]*common.Block, error) {
|
func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) {
|
||||||
var blocks []*common.Block
|
var blocks []*common.Block
|
||||||
err := meddler.QueryAll(
|
err := meddler.QueryAll(
|
||||||
@@ -60,13 +66,94 @@ func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) {
|
|||||||
return blocks, err
|
return blocks, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// reorg deletes all the information that was added into the DB after the lastValidBlock
|
// GetLastBlock retrieve the block with the highest block number from the DB
|
||||||
// WARNING: this is a draaft of the function, useful at the moment for tests
|
func (hdb *HistoryDB) GetLastBlock() (*common.Block, error) {
|
||||||
func (hdb *HistoryDB) reorg(lastValidBlock uint64) error {
|
block := &common.Block{}
|
||||||
|
err := meddler.QueryRow(
|
||||||
|
hdb.db, block, "SELECT * FROM block ORDER BY eth_block_num DESC LIMIT 1;",
|
||||||
|
)
|
||||||
|
return block, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// addBatches insert Bids into the DB
|
||||||
|
func (hdb *HistoryDB) addBatches(batches []common.Batch) error {
|
||||||
|
return db.BulkInsert(
|
||||||
|
hdb.db,
|
||||||
|
`INSERT INTO batch (
|
||||||
|
batch_num,
|
||||||
|
eth_block_num,
|
||||||
|
forger_addr,
|
||||||
|
fees_collected,
|
||||||
|
state_root,
|
||||||
|
num_accounts,
|
||||||
|
exit_root,
|
||||||
|
forge_l1_txs_num,
|
||||||
|
slot_num
|
||||||
|
) VALUES %s;`,
|
||||||
|
batches[:],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
err := meddler.QueryAll(
|
||||||
|
hdb.db, &batches,
|
||||||
|
"SELECT * FROM batch WHERE $1 <= batch_num AND batch_num < $2",
|
||||||
|
from, to,
|
||||||
|
)
|
||||||
|
return batches, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLastBatchNum returns the BatchNum of the latest forged batch
|
||||||
|
func (hdb *HistoryDB) GetLastBatchNum() (common.BatchNum, error) {
|
||||||
|
row := hdb.db.QueryRow("SELECT batch_num FROM batch ORDER BY batch_num DESC LIMIT 1;")
|
||||||
|
var batchNum common.BatchNum
|
||||||
|
return batchNum, row.Scan(&batchNum)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLastL1TxsNum returns the greatest ForgeL1TxsNum in the DB
|
||||||
|
func (hdb *HistoryDB) GetLastL1TxsNum() (uint32, error) {
|
||||||
|
row := hdb.db.QueryRow("SELECT MAX(forge_l1_txs_num) FROM batch;")
|
||||||
|
var lastL1TxsNum uint32
|
||||||
|
return lastL1TxsNum, row.Scan(&lastL1TxsNum)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reorg deletes all the information that was added into the DB after the lastValidBlock
|
||||||
|
func (hdb *HistoryDB) Reorg(lastValidBlock uint64) 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
|
||||||
|
func (hdb *HistoryDB) SyncRollup(
|
||||||
|
blockNum uint64,
|
||||||
|
l1txs []common.L1Tx,
|
||||||
|
l2txs []common.L2Tx,
|
||||||
|
registeredAccounts []common.Account,
|
||||||
|
exitTree common.ExitInfo,
|
||||||
|
withdrawals common.ExitInfo,
|
||||||
|
registeredTokens []common.Token,
|
||||||
|
batches []common.Batch,
|
||||||
|
vars *common.RollupVars,
|
||||||
|
) error {
|
||||||
|
// TODO: make all in a single DB commit
|
||||||
|
if err := hdb.addBatches(batches); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SyncPoD stores all the data that can be changed / added on a block in the PoD SC
|
||||||
|
func (hdb *HistoryDB) SyncPoD(
|
||||||
|
blockNum uint64,
|
||||||
|
bids []common.Bid,
|
||||||
|
coordinators []common.Coordinator,
|
||||||
|
vars *common.AuctionVars,
|
||||||
|
) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// addBids insert Bids into the DB
|
// addBids insert Bids into the DB
|
||||||
func (hdb *HistoryDB) addBids(bids []common.Bid) error {
|
func (hdb *HistoryDB) addBids(bids []common.Bid) error {
|
||||||
// TODO: check the coordinator info
|
// TODO: check the coordinator info
|
||||||
@@ -77,13 +164,13 @@ func (hdb *HistoryDB) addBids(bids []common.Bid) error {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBidsByBlock return the bids done between the block from and to
|
// GetBidsBySlot return the bids for a specific slot
|
||||||
func (hdb *HistoryDB) GetBidsByBlock(from, to uint64) ([]*common.Bid, error) {
|
func (hdb *HistoryDB) GetBidsBySlot(slotNum common.SlotNum) ([]*common.Bid, error) {
|
||||||
var bids []*common.Bid
|
var bids []*common.Bid
|
||||||
err := meddler.QueryAll(
|
err := meddler.QueryAll(
|
||||||
hdb.db, &bids,
|
hdb.db, &bids,
|
||||||
"SELECT * FROM bid WHERE $1 <= eth_block_num AND eth_block_num < $2",
|
"SELECT * FROM bid WHERE $1 = slot_num;",
|
||||||
from, to,
|
slotNum,
|
||||||
)
|
)
|
||||||
return bids, err
|
return bids, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
eth "github.com/ethereum/go-ethereum/common"
|
eth "github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/hermeznetwork/hermez-node/common"
|
"github.com/hermeznetwork/hermez-node/common"
|
||||||
|
"github.com/hermeznetwork/hermez-node/db"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,26 +40,92 @@ func TestMain(m *testing.M) {
|
|||||||
os.Exit(result)
|
os.Exit(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddBlock(t *testing.T) {
|
func TestBlocks(t *testing.T) {
|
||||||
var fromBlock, toBlock uint64
|
var fromBlock, toBlock uint64
|
||||||
fromBlock = 1
|
fromBlock = 1
|
||||||
toBlock = 5
|
toBlock = 5
|
||||||
// Delete peviously created rows (clean previous test execs)
|
// Delete peviously created rows (clean previous test execs)
|
||||||
assert.NoError(t, historyDB.reorg(fromBlock-1))
|
assert.NoError(t, historyDB.Reorg(fromBlock-1))
|
||||||
// Generate fake blocks
|
// Generate fake blocks
|
||||||
blocks := genBlocks(fromBlock, toBlock)
|
blocks := genBlocks(fromBlock, toBlock)
|
||||||
// Insert blocks into DB
|
// Insert blocks into DB
|
||||||
err := historyDB.addBlocks(blocks)
|
for _, block := range blocks {
|
||||||
assert.NoError(t, err)
|
err := historyDB.AddBlock(&block)
|
||||||
// Get blocks from DB
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
// Get all blocks from DB
|
||||||
fetchedBlocks, err := historyDB.GetBlocks(fromBlock, toBlock)
|
fetchedBlocks, err := historyDB.GetBlocks(fromBlock, toBlock)
|
||||||
|
assert.Equal(t, len(blocks), len(fetchedBlocks))
|
||||||
// Compare generated vs getted blocks
|
// Compare generated vs getted blocks
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
for i, fetchedBlock := range fetchedBlocks {
|
for i, fetchedBlock := range fetchedBlocks {
|
||||||
assert.Equal(t, blocks[i].EthBlockNum, fetchedBlock.EthBlockNum)
|
assertEqualBlock(t, &blocks[i], fetchedBlock)
|
||||||
assert.Equal(t, blocks[i].Hash, fetchedBlock.Hash)
|
|
||||||
assert.Equal(t, blocks[i].Timestamp.Unix(), fetchedBlock.Timestamp.Unix())
|
|
||||||
}
|
}
|
||||||
|
// 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) {
|
func TestBids(t *testing.T) {
|
||||||
@@ -82,29 +149,31 @@ func TestBids(t *testing.T) {
|
|||||||
err := historyDB.addBids(bids)
|
err := historyDB.addBids(bids)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
// Fetch bids
|
// Fetch bids
|
||||||
fetchedBidsPtr, err := historyDB.GetBidsByBlock(fromBlock, toBlock)
|
var fetchedBids []*common.Bid
|
||||||
assert.NoError(t, err)
|
for i := fromBlock; i < toBlock; i++ {
|
||||||
// Compare fetched bids vs generated bids
|
fetchedBidsSlot, err := historyDB.GetBidsBySlot(common.SlotNum(i))
|
||||||
fetchedBids := make([]common.Bid, 0, (toBlock-fromBlock)*bidsPerSlot)
|
assert.NoError(t, err)
|
||||||
for _, bid := range fetchedBidsPtr {
|
fetchedBids = append(fetchedBids, fetchedBidsSlot...)
|
||||||
fetchedBids = append(fetchedBids, *bid)
|
}
|
||||||
|
// 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
|
// setTestBlocks WARNING: this will delete the blocks and recreate them
|
||||||
func setTestBlocks(from, to uint64) {
|
func setTestBlocks(from, to uint64) {
|
||||||
if from == 0 {
|
if from == 0 {
|
||||||
if err := historyDB.reorg(from); err != nil {
|
if err := historyDB.Reorg(from); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if err := historyDB.reorg(from - 1); err != nil {
|
if err := historyDB.Reorg(from - 1); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
blocks := genBlocks(from, to)
|
blocks := genBlocks(from, to)
|
||||||
if err := historyDB.addBlocks(blocks); err != nil {
|
if err := addBlocks(blocks); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,3 +189,12 @@ func genBlocks(from, to uint64) []common.Block {
|
|||||||
}
|
}
|
||||||
return blocks
|
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[:],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,15 +5,9 @@ CREATE TABLE block (
|
|||||||
hash BYTEA NOT NULL
|
hash BYTEA NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE slot_min_prices (
|
|
||||||
eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
|
||||||
min_prices VARCHAR(200) NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE coordianator (
|
CREATE TABLE coordianator (
|
||||||
forger_addr BYTEA NOT NULL,
|
forger_addr BYTEA NOT NULL,
|
||||||
eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||||
beneficiary_addr BYTEA NOT NULL,
|
|
||||||
withdraw_addr BYTEA NOT NULL,
|
withdraw_addr BYTEA NOT NULL,
|
||||||
url VARCHAR(200) NOT NULL,
|
url VARCHAR(200) NOT NULL,
|
||||||
PRIMARY KEY (forger_addr, eth_block_num)
|
PRIMARY KEY (forger_addr, eth_block_num)
|
||||||
@@ -32,14 +26,22 @@ CREATE TABLE batch (
|
|||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE exit_tree (
|
CREATE TABLE exit_tree (
|
||||||
batch_num BIGINT NOT NULL REFERENCES batch (batch_num) ON DELETE CASCADE,
|
batch_num BIGINT REFERENCES batch (batch_num) ON DELETE CASCADE,
|
||||||
account_idx BIGINT NOT NULL,
|
account_idx BIGINT,
|
||||||
merkle_proof BYTEA NOT NULL,
|
merkle_proof BYTEA NOT NULL,
|
||||||
amount NUMERIC NOT NULL,
|
balance NUMERIC NOT NULL,
|
||||||
nullifier BYTEA NOT NULL,
|
nullifier BYTEA NOT NULL,
|
||||||
PRIMARY KEY (batch_num, account_idx)
|
PRIMARY KEY (batch_num, account_idx)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE withdrawal (
|
||||||
|
batch_num BIGINT,
|
||||||
|
account_idx BIGINT,
|
||||||
|
eth_block_num BIGINT REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (batch_num, account_idx) REFERENCES exit_tree (batch_num, account_idx) ON DELETE CASCADE,
|
||||||
|
PRIMARY KEY (batch_num, account_idx)
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE bid (
|
CREATE TABLE bid (
|
||||||
slot_num BIGINT NOT NULL,
|
slot_num BIGINT NOT NULL,
|
||||||
bid_value BYTEA NOT NULL, -- (check if we can do a max(), if not add float for order purposes)
|
bid_value BYTEA NOT NULL, -- (check if we can do a max(), if not add float for order purposes)
|
||||||
@@ -69,7 +71,8 @@ CREATE TABLE l1tx (
|
|||||||
token_id INT NOT NULL REFERENCES token (token_id),
|
token_id INT NOT NULL REFERENCES token (token_id),
|
||||||
amount NUMERIC NOT NULL,
|
amount NUMERIC NOT NULL,
|
||||||
load_amount BYTEA NOT NULL,
|
load_amount BYTEA NOT NULL,
|
||||||
eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE
|
eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||||
|
tx_type VARCHAR(40) NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE l2tx (
|
CREATE TABLE l2tx (
|
||||||
@@ -80,7 +83,8 @@ CREATE TABLE l2tx (
|
|||||||
to_idx BIGINT NOT NULL,
|
to_idx BIGINT NOT NULL,
|
||||||
amount NUMERIC NOT NULL,
|
amount NUMERIC NOT NULL,
|
||||||
fee INT NOT NULL,
|
fee INT NOT NULL,
|
||||||
nonce BIGINT NOT NULL
|
nonce BIGINT NOT NULL,
|
||||||
|
tx_type VARCHAR(40) NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE account (
|
CREATE TABLE account (
|
||||||
@@ -91,7 +95,30 @@ CREATE TABLE account (
|
|||||||
eth_addr BYTEA NOT NULL
|
eth_addr BYTEA NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE rollup_vars (
|
||||||
|
eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||||
|
forge_l1_timeout BYTEA NOT NULL,
|
||||||
|
fee_l1_user_tx BYTEA NOT NULL,
|
||||||
|
fee_add_token BYTEA NOT NULL,
|
||||||
|
tokens_hez BYTEA NOT NULL,
|
||||||
|
governance BYTEA NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE consensus_vars (
|
||||||
|
eth_block_num BIGINT PRIMARY KEY REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||||
|
slot_deadline INT NOT NULL,
|
||||||
|
close_auction_slots INT NOT NULL,
|
||||||
|
open_auction_slots INT NOT NULL,
|
||||||
|
min_bid_slots VARCHAR(200) NOT NULL,
|
||||||
|
outbidding INT NOT NULL,
|
||||||
|
donation_address BYTEA NOT NULL,
|
||||||
|
governance_address BYTEA NOT NULL,
|
||||||
|
allocation_ratio vARCHAR(200)
|
||||||
|
);
|
||||||
|
|
||||||
-- +migrate Down
|
-- +migrate Down
|
||||||
|
DROP TABLE consensus_vars;
|
||||||
|
DROP TABLE rollup_vars;
|
||||||
DROP TABLE account;
|
DROP TABLE account;
|
||||||
DROP TABLE l2tx;
|
DROP TABLE l2tx;
|
||||||
DROP TABLE l1tx;
|
DROP TABLE l1tx;
|
||||||
@@ -100,5 +127,4 @@ DROP TABLE bid;
|
|||||||
DROP TABLE exit_tree;
|
DROP TABLE exit_tree;
|
||||||
DROP TABLE batch;
|
DROP TABLE batch;
|
||||||
DROP TABLE coordianator;
|
DROP TABLE coordianator;
|
||||||
DROP TABLE slot_min_prices;
|
|
||||||
DROP TABLE block;
|
DROP TABLE block;
|
||||||
@@ -22,7 +22,8 @@ CREATE TABLE tx_pool (
|
|||||||
signature BYTEA NOT NULL,
|
signature BYTEA NOT NULL,
|
||||||
timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,
|
timestamp TIMESTAMP WITHOUT TIME ZONE NOT NULL,
|
||||||
absolute_fee NUMERIC,
|
absolute_fee NUMERIC,
|
||||||
absolute_fee_update TIMESTAMP WITHOUT TIME ZONE
|
absolute_fee_update TIMESTAMP WITHOUT TIME ZONE,
|
||||||
|
tx_type VARCHAR(40) NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE account_creation_auth (
|
CREATE TABLE account_creation_auth (
|
||||||
|
|||||||
1
go.sum
1
go.sum
@@ -544,6 +544,7 @@ github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3
|
|||||||
github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
|
github.com/streadway/handy v0.0.0-20190108123426-d5acb3125c2a/go.mod h1:qNTQ5P5JnDBl6z3cMAg/SywNDC5ABu5ApDIw6lUbRmI=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
|
||||||
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
|||||||
Reference in New Issue
Block a user