Commit f988b4e9 authored by Hamdi Allam's avatar Hamdi Allam

latest gorm tag changes

parent 819e591b
......@@ -2,6 +2,7 @@ package database
import (
"database/sql"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
......@@ -15,7 +16,7 @@ import (
*/
type BlockHeader struct {
Hash common.Hash `gorm:"serializer:json"`
Hash common.Hash `gorm:"primaryKey;serializer:json"`
ParentHash common.Hash `gorm:"serializer:json"`
Number pgtype.Numeric
Timestamp uint64
......@@ -30,16 +31,16 @@ type L2BlockHeader struct {
// Marked when the proposed output is finalized on L1.
// All bedrock blocks will have `LegacyStateBatchIndex == NULL`
L1BlockHash *common.Hash
L1BlockHash *common.Hash `gorm:"serializer:json"`
LegacyStateBatchIndex sql.NullInt64
}
type LegacyStateBatch struct {
Index uint64
Root common.Hash
Index uint64 `gorm:"primaryKey"`
Root common.Hash `gorm:"serializer:json"`
Size uint64
PrevTotal uint64
L1BlockHash common.Hash
L1BlockHash common.Hash `gorm:"serializer:json"`
}
type BlocksView interface {
......@@ -95,6 +96,8 @@ func (db *blocksDB) StoreLegacyStateBatch(stateBatch *LegacyStateBatch) error {
result = db.gorm.Where("number BETWEEN ? AND ?", &startHeight, &endHeight).Find(&l2Headers)
if result.Error != nil {
return result.Error
} else if result.RowsAffected != int64(stateBatch.Size) {
return errors.New("state batch size exceeds number of indexed l2 blocks")
}
for _, header := range l2Headers {
......@@ -135,11 +138,14 @@ func (db *blocksDB) LatestL2BlockHeight() (*big.Int, error) {
func (db *blocksDB) MarkFinalizedL1RootForL2Block(l2Root, l1Root common.Hash) error {
var l2Header L2BlockHeader
result := db.gorm.First(&l2Header, "hash = ?", l2Root)
if result.Error == nil {
l2Header.L1BlockHash = &l1Root
db.gorm.Save(&l2Header)
l2Header.Hash = l2Root // set the primary key
result := db.gorm.First(&l2Header)
if result.Error != nil {
return result.Error
}
l2Header.L1BlockHash = &l1Root
result = db.gorm.Save(&l2Header)
return result.Error
}
......@@ -25,7 +25,7 @@ type TokenPair struct {
}
type Deposit struct {
GUID string
GUID string `gorm:"primaryKey"`
InitiatedL1EventGUID string
Tx Transaction `gorm:"embedded"`
......@@ -38,7 +38,7 @@ type DepositWithTransactionHash struct {
}
type Withdrawal struct {
GUID string
GUID string `gorm:"primaryKey"`
InitiatedL2EventGUID string
WithdrawalHash common.Hash `gorm:"serializer:json"`
......@@ -92,11 +92,10 @@ func (db *bridgeDB) StoreDeposits(deposits []*Deposit) error {
func (db *bridgeDB) DepositsByAddress(address common.Address) ([]*DepositWithTransactionHash, error) {
// validate this query
depositsQuery := db.gorm.Table("deposits").Where("from_address = ?", address).Select("deposits.*")
depositsQuery := db.gorm.Table("deposits").Where(&Transaction{FromAddress: address})
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{}
result := joinQuery.Scan(&deposits)
if result.Error != nil {
return nil, result.Error
......
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