mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Add sync interface
This commit is contained in:
12
common/exittree.go
Normal file
12
common/exittree.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/big"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExitTreeLeaf struct {
|
||||||
|
AccountIdx Idx
|
||||||
|
MerkleProof []byte
|
||||||
|
Amount *big.Int
|
||||||
|
Nullifier *big.Int
|
||||||
|
}
|
||||||
@@ -32,8 +32,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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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 PoDVars 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,7 +40,12 @@ 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) AddBlock(blocks *common.Block) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// addBlocks insert blocks into the DB. TODO: move method to test
|
||||||
func (hdb *HistoryDB) addBlocks(blocks []common.Block) error {
|
func (hdb *HistoryDB) addBlocks(blocks []common.Block) error {
|
||||||
return db.BulkInsert(
|
return db.BulkInsert(
|
||||||
hdb.db,
|
hdb.db,
|
||||||
@@ -60,13 +65,38 @@ 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
|
// Reorg deletes all the information that was added into the DB after the lastValidBlock
|
||||||
// WARNING: this is a draaft of the function, useful at the moment for tests
|
// WARNING: this is a draaft of the function, useful at the moment for tests
|
||||||
func (hdb *HistoryDB) reorg(lastValidBlock uint64) error {
|
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.ExitTreeLeaf,
|
||||||
|
withdrawals common.ExitTreeLeaf,
|
||||||
|
registeredTokens []common.Token,
|
||||||
|
batch *common.Batch,
|
||||||
|
vars *common.RollupVars,
|
||||||
|
) error {
|
||||||
|
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.PoDVars,
|
||||||
|
) 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
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func TestAddBlock(t *testing.T) {
|
|||||||
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
|
||||||
@@ -95,11 +95,11 @@ 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 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,6 @@ 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,
|
||||||
@@ -32,14 +27,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,
|
amount 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)
|
||||||
@@ -91,7 +94,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 +126,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;
|
||||||
Reference in New Issue
Block a user