Commit 1e67bc9f authored by Hamdi Allam's avatar Hamdi Allam

add timestamp to bridge schemas & whooo queries work

parent cd1a0def
...@@ -14,7 +14,8 @@ type Transaction struct { ...@@ -14,7 +14,8 @@ type Transaction struct {
FromAddress common.Address `gorm:"serializer:json"` FromAddress common.Address `gorm:"serializer:json"`
ToAddress common.Address `gorm:"serializer:json"` ToAddress common.Address `gorm:"serializer:json"`
Amount U256 Amount U256
Data hexutil.Bytes `gorom:"serializer:json"` Data hexutil.Bytes `gorm:"serializer:json"`
Timestamp uint64
} }
type TokenPair struct { type TokenPair struct {
...@@ -31,7 +32,7 @@ type Deposit struct { ...@@ -31,7 +32,7 @@ type Deposit struct {
} }
type DepositWithTransactionHash struct { type DepositWithTransactionHash struct {
Deposit *Deposit `gorm:"embedded"` Deposit Deposit `gorm:"embedded"`
L1TransactionHash common.Hash `gorm:"serializer:json"` L1TransactionHash common.Hash `gorm:"serializer:json"`
} }
...@@ -48,7 +49,7 @@ type Withdrawal struct { ...@@ -48,7 +49,7 @@ type Withdrawal struct {
} }
type WithdrawalWithTransactionHashes struct { type WithdrawalWithTransactionHashes struct {
Withdrawal *Withdrawal `gorm:"embedded"` Withdrawal Withdrawal `gorm:"embedded"`
L2TransactionHash common.Hash `gorm:"serializer:json"` L2TransactionHash common.Hash `gorm:"serializer:json"`
ProvenL1TransactionHash *common.Hash `gorm:"serializer:json"` ProvenL1TransactionHash *common.Hash `gorm:"serializer:json"`
...@@ -89,21 +90,19 @@ func (db *bridgeDB) StoreDeposits(deposits []*Deposit) error { ...@@ -89,21 +90,19 @@ func (db *bridgeDB) StoreDeposits(deposits []*Deposit) error {
} }
func (db *bridgeDB) DepositsByAddress(address common.Address) ([]*DepositWithTransactionHash, error) { func (db *bridgeDB) DepositsByAddress(address common.Address) ([]*DepositWithTransactionHash, error) {
// validate this query depositsQuery := db.gorm.Table("deposits").Select("deposits.*, l1_contract_events.transaction_hash AS l1_transaction_hash")
depositsQuery := db.gorm.Table("deposits").Where(&Transaction{FromAddress: address}) eventsJoinQuery := depositsQuery.Joins("LEFT JOIN l1_contract_events ON deposits.initiated_l1_event_guid = l1_contract_events.guid")
joinQuery := depositsQuery.Joins("left join l1_contract_events transaction_hash as l1_transaction_hash ON deposit.initiated_l1_event_guid = l1_contract_events.guid")
deposits := []DepositWithTransactionHash{} // add in cursoring options
result := joinQuery.Limit(100).Scan(&deposits) filteredQuery := eventsJoinQuery.Where(&Transaction{FromAddress: address}).Order("deposits.timestamp DESC").Limit(100)
deposits := make([]*DepositWithTransactionHash, 100)
result := filteredQuery.Scan(&deposits)
if result.Error != nil { if result.Error != nil {
return nil, result.Error return nil, result.Error
} }
depositPtrs := make([]*DepositWithTransactionHash, len(deposits)) return deposits, nil
for i, deposit := range deposits {
depositPtrs[i] = &deposit
}
return depositPtrs, nil
} }
// Withdrawals // Withdrawals
...@@ -136,6 +135,20 @@ func (db *bridgeDB) MarkFinalizedWithdrawalEvent(guid, finalizedL1EventGuid stri ...@@ -136,6 +135,20 @@ func (db *bridgeDB) MarkFinalizedWithdrawalEvent(guid, finalizedL1EventGuid stri
} }
func (db *bridgeDB) WithdrawalsByAddress(address common.Address) ([]*WithdrawalWithTransactionHashes, error) { func (db *bridgeDB) WithdrawalsByAddress(address common.Address) ([]*WithdrawalWithTransactionHashes, error) {
// Implement this query withdrawalsQuery := db.gorm.Debug().Table("withdrawals").Select("withdrawals.*, l2_contract_events.transaction_hash AS l2_transaction_hash, proven_l1_contract_events.transaction_hash AS proven_l1_transaction_hash, finalized_l1_contract_events.transaction_hash AS finalized_l1_transaction_hash")
return nil, nil
eventsJoinQuery := withdrawalsQuery.Joins("LEFT JOIN l2_contract_events ON withdrawals.initiated_l2_event_guid = l2_contract_events.guid")
provenJoinQuery := eventsJoinQuery.Joins("LEFT JOIN l1_contract_events AS proven_l1_contract_events ON withdrawals.proven_l1_event_guid = proven_l1_contract_events.guid")
finalizedJoinQuery := provenJoinQuery.Joins("LEFT JOIN l1_contract_events AS finalized_l1_contract_events ON withdrawals.finalized_l1_event_guid = finalized_l1_contract_events.guid")
// add in cursoring options
filteredQuery := finalizedJoinQuery.Where(&Transaction{FromAddress: address}).Order("withdrawals.timestamp DESC").Limit(100)
withdrawals := make([]*WithdrawalWithTransactionHashes, 100)
result := filteredQuery.Scan(&withdrawals)
if result.Error != nil {
return nil, result.Error
}
return withdrawals, nil
} }
...@@ -2,6 +2,7 @@ package database ...@@ -2,6 +2,7 @@ package database
import ( import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"gorm.io/gorm" "gorm.io/gorm"
) )
...@@ -10,12 +11,13 @@ import ( ...@@ -10,12 +11,13 @@ import (
*/ */
type ContractEvent struct { type ContractEvent struct {
GUID string GUID string `gorm:"primaryKey"`
BlockHash common.Hash `gorm:"serializer:json"` BlockHash common.Hash `gorm:"serializer:json"`
TransactionHash common.Hash `gorm:"serializer:json"` TransactionHash common.Hash `gorm:"serializer:json"`
EventSignature []byte `gorm:"serializer:json"` EventSignature hexutil.Bytes `gorm:"serializer:json"`
LogIndex uint64 LogIndex uint64
Timestamp uint64
} }
type L1ContractEvent struct { type L1ContractEvent struct {
......
...@@ -13,18 +13,6 @@ CREATE TABLE IF NOT EXISTS l1_block_headers ( ...@@ -13,18 +13,6 @@ CREATE TABLE IF NOT EXISTS l1_block_headers (
timestamp INTEGER NOT NULL timestamp INTEGER NOT NULL
); );
CREATE TABLE IF NOT EXISTS l2_block_headers (
-- Block header
hash VARCHAR NOT NULL PRIMARY KEY,
parent_hash VARCHAR NOT NULL,
number UINT256,
timestamp INTEGER NOT NULL,
-- Finalization information
l1_block_hash VARCHAR REFERENCES l1_block_headers(hash),
legacy_state_batch_index INTEGER REFERENCES legacy_state_batches(index)
);
CREATE TABLE IF NOT EXISTS legacy_state_batches ( CREATE TABLE IF NOT EXISTS legacy_state_batches (
index INTEGER NOT NULL PRIMARY KEY, index INTEGER NOT NULL PRIMARY KEY,
root VARCHAR NOT NULL, root VARCHAR NOT NULL,
...@@ -37,6 +25,18 @@ CREATE TABLE IF NOT EXISTS legacy_state_batches ( ...@@ -37,6 +25,18 @@ CREATE TABLE IF NOT EXISTS legacy_state_batches (
l1_block_hash VARCHAR NOT NULL REFERENCES l1_block_headers(hash) l1_block_hash VARCHAR NOT NULL REFERENCES l1_block_headers(hash)
); );
CREATE TABLE IF NOT EXISTS l2_block_headers (
-- Block header
hash VARCHAR NOT NULL PRIMARY KEY,
parent_hash VARCHAR NOT NULL,
number UINT256,
timestamp INTEGER NOT NULL,
-- Finalization information
l1_block_hash VARCHAR REFERENCES l1_block_headers(hash),
legacy_state_batch_index INTEGER REFERENCES legacy_state_batches(index)
);
/** /**
* EVENT DATA * EVENT DATA
*/ */
...@@ -46,7 +46,8 @@ CREATE TABLE IF NOT EXISTS l1_contract_events ( ...@@ -46,7 +46,8 @@ CREATE TABLE IF NOT EXISTS l1_contract_events (
block_hash VARCHAR NOT NULL REFERENCES l1_block_headers(hash), block_hash VARCHAR NOT NULL REFERENCES l1_block_headers(hash),
transaction_hash VARCHAR NOT NULL, transaction_hash VARCHAR NOT NULL,
event_signature VARCHAR NOT NULL, event_signature VARCHAR NOT NULL,
log_index INTEGER NOT NULL log_index INTEGER NOT NULL,
timestamp INTEGER NOT NULL
); );
CREATE TABLE IF NOT EXISTS l2_contract_events ( CREATE TABLE IF NOT EXISTS l2_contract_events (
...@@ -54,7 +55,8 @@ CREATE TABLE IF NOT EXISTS l2_contract_events ( ...@@ -54,7 +55,8 @@ CREATE TABLE IF NOT EXISTS l2_contract_events (
block_hash VARCHAR NOT NULL REFERENCES l2_block_headers(hash), block_hash VARCHAR NOT NULL REFERENCES l2_block_headers(hash),
transaction_hash VARCHAR NOT NULL, transaction_hash VARCHAR NOT NULL,
event_signature VARCHAR NOT NULL, event_signature VARCHAR NOT NULL,
log_index INTEGER NOT NULL log_index INTEGER NOT NULL,
timestamp INTEGER NOT NULL
); );
/** /**
...@@ -73,14 +75,15 @@ CREATE TABLE IF NOT EXISTS deposits ( ...@@ -73,14 +75,15 @@ CREATE TABLE IF NOT EXISTS deposits (
l1_token_address VARCHAR NOT NULL, l1_token_address VARCHAR NOT NULL,
l2_token_address VARCHAR NOT NULL, l2_token_address VARCHAR NOT NULL,
amount UINT256, amount UINT256,
data VARCHAR NOT NULL data VARCHAR NOT NULL,
timestamp INTEGER NOT NULL
); );
CREATE TABLE IF NOT EXISTS withdrawals ( CREATE TABLE IF NOT EXISTS withdrawals (
guid VARCHAR PRIMARY KEY NOT NULL, guid VARCHAR PRIMARY KEY NOT NULL,
-- Event causing this withdrawal -- Event causing this withdrawal
intiated_l2_event_guid VARCHAR NOT NULL REFERENCES l2_contract_events(guid), initiated_l2_event_guid VARCHAR NOT NULL REFERENCES l2_contract_events(guid),
-- Multistep (bedrock) process of a withdrawal -- Multistep (bedrock) process of a withdrawal
withdrawal_hash VARCHAR NOT NULL, withdrawal_hash VARCHAR NOT NULL,
...@@ -95,5 +98,6 @@ CREATE TABLE IF NOT EXISTS withdrawals ( ...@@ -95,5 +98,6 @@ CREATE TABLE IF NOT EXISTS withdrawals (
l1_token_address VARCHAR NOT NULL, l1_token_address VARCHAR NOT NULL,
l2_token_address VARCHAR NOT NULL, l2_token_address VARCHAR NOT NULL,
amount UINT256, amount UINT256,
data VARCHAR NOT NULL data VARCHAR NOT NULL,
timestamp INTEGER NOT NULL
); );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment