From 1cbf54acc75c3ff98bc79ab06714576021e0b4bc Mon Sep 17 00:00:00 2001 From: Arnau B Date: Thu, 20 Aug 2020 16:17:14 +0200 Subject: [PATCH] Add sync interface --- common/exittree.go | 12 ++++++++ common/l1tx.go | 2 -- common/scvars.go | 37 +++++++++++++++++++++++++ db/historydb/historydb.go | 36 ++++++++++++++++++++++-- db/historydb/historydb_test.go | 6 ++-- db/historydb/migrations/001_init.sql | 41 ++++++++++++++++++++++------ 6 files changed, 118 insertions(+), 16 deletions(-) create mode 100644 common/exittree.go create mode 100644 common/scvars.go diff --git a/common/exittree.go b/common/exittree.go new file mode 100644 index 0000000..de295d1 --- /dev/null +++ b/common/exittree.go @@ -0,0 +1,12 @@ +package common + +import ( + "math/big" +) + +type ExitTreeLeaf struct { + AccountIdx Idx + MerkleProof []byte + Amount *big.Int + Nullifier *big.Int +} diff --git a/common/l1tx.go b/common/l1tx.go index f6cdd47..e5353f0 100644 --- a/common/l1tx.go +++ b/common/l1tx.go @@ -32,8 +32,6 @@ func (tx *L1Tx) Tx() *Tx { FromIdx: tx.FromIdx, ToIdx: tx.ToIdx, Amount: tx.Amount, - Nonce: 0, - Fee: 0, Type: tx.Type, } } diff --git a/common/scvars.go b/common/scvars.go new file mode 100644 index 0000000..50da3d3 --- /dev/null +++ b/common/scvars.go @@ -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 +} diff --git a/db/historydb/historydb.go b/db/historydb/historydb.go index 1ad962e..2bd400a 100644 --- a/db/historydb/historydb.go +++ b/db/historydb/historydb.go @@ -40,7 +40,12 @@ func NewHistoryDB(port int, host, user, password, dbname string) (*HistoryDB, er 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 { return db.BulkInsert( hdb.db, @@ -60,13 +65,38 @@ func (hdb *HistoryDB) GetBlocks(from, to uint64) ([]*common.Block, error) { 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 -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) 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 func (hdb *HistoryDB) addBids(bids []common.Bid) error { // TODO: check the coordinator info diff --git a/db/historydb/historydb_test.go b/db/historydb/historydb_test.go index 1e166e8..9a48a25 100644 --- a/db/historydb/historydb_test.go +++ b/db/historydb/historydb_test.go @@ -44,7 +44,7 @@ func TestAddBlock(t *testing.T) { fromBlock = 1 toBlock = 5 // 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 blocks := genBlocks(fromBlock, toBlock) // Insert blocks into DB @@ -95,11 +95,11 @@ func TestBids(t *testing.T) { // setTestBlocks WARNING: this will delete the blocks and recreate them func setTestBlocks(from, to uint64) { if from == 0 { - if err := historyDB.reorg(from); err != nil { + if err := historyDB.Reorg(from); err != nil { panic(err) } } else { - if err := historyDB.reorg(from - 1); err != nil { + if err := historyDB.Reorg(from - 1); err != nil { panic(err) } } diff --git a/db/historydb/migrations/001_init.sql b/db/historydb/migrations/001_init.sql index ee1fcba..24e72c8 100644 --- a/db/historydb/migrations/001_init.sql +++ b/db/historydb/migrations/001_init.sql @@ -5,11 +5,6 @@ CREATE TABLE block ( 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 ( forger_addr BYTEA NOT NULL, 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 ( - batch_num BIGINT NOT NULL REFERENCES batch (batch_num) ON DELETE CASCADE, - account_idx BIGINT NOT NULL, + batch_num BIGINT REFERENCES batch (batch_num) ON DELETE CASCADE, + account_idx BIGINT, merkle_proof BYTEA NOT NULL, amount NUMERIC NOT NULL, nullifier BYTEA NOT NULL, 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 ( slot_num BIGINT NOT NULL, 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 ); +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 +DROP TABLE consensus_vars; +DROP TABLE rollup_vars; DROP TABLE account; DROP TABLE l2tx; DROP TABLE l1tx; @@ -100,5 +126,4 @@ DROP TABLE bid; DROP TABLE exit_tree; DROP TABLE batch; DROP TABLE coordianator; -DROP TABLE slot_min_prices; DROP TABLE block; \ No newline at end of file