Skip to content

Commit

Permalink
looks to be working
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Feb 19, 2024
1 parent 355b9ab commit bcf26f3
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 33 deletions.
9 changes: 9 additions & 0 deletions db/pgstorage/migrations/0008.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +migrate Up

ALTER TABLE sync.deposit
ADD COLUMN IF NOT EXISTS origin_rollup_id BIGINT DEFAULT 0;

-- +migrate Down

ALTER TABLE sync.deposit
DROP COLUMN IF EXISTS origin_rollup_id;
134 changes: 105 additions & 29 deletions db/pgstorage/pgstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,28 @@ func (p *PostgresStorage) AddGlobalExitRoot(ctx context.Context, exitRoot *ether

// AddDeposit adds new deposit to the storage.
func (p *PostgresStorage) AddDeposit(ctx context.Context, deposit *etherman.Deposit, dbTx pgx.Tx) (uint64, error) {
const addDepositSQL = "INSERT INTO sync.deposit (leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING id"
const addDepositSQL = `
INSERT INTO sync.deposit (
leaf_type, network_id, orig_net, orig_addr, amount, dest_net, dest_addr, block_id, deposit_cnt, tx_hash, metadata, origin_rollup_id
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING id`
e := p.getExecQuerier(dbTx)
var depositID uint64
err := e.QueryRow(ctx, addDepositSQL, deposit.LeafType, deposit.NetworkID, deposit.OriginalNetwork, deposit.OriginalAddress, deposit.Amount.String(), deposit.DestinationNetwork, deposit.DestinationAddress, deposit.BlockID, deposit.DepositCount, deposit.TxHash, deposit.Metadata).Scan(&depositID)
err := e.QueryRow(
ctx,
addDepositSQL,
deposit.LeafType,
deposit.NetworkID,
deposit.OriginalNetwork,
deposit.OriginalAddress,
deposit.Amount.String(),
deposit.DestinationNetwork,
deposit.DestinationAddress,
deposit.BlockID,
deposit.DepositCount,
deposit.TxHash,
deposit.Metadata,
deposit.OriginRollupID,
).Scan(&depositID)
return depositID, err
}

Expand Down Expand Up @@ -205,34 +223,53 @@ func (p *PostgresStorage) GetClaim(ctx context.Context, depositCount, originNetw
)
// if mainnet flag == 0 => origin network = rollup index +1
// else origin network = 0
const getClaimSQLMainnet = `
SELECT index, orig_net, orig_addr, amount, dest_addr, block_id, network_id, tx_hash, rollup_index, mainnet_flag
const getClaimSQLOriginMainnetDestRollup = `
SELECT index, orig_net, orig_addr, amount, dest_addr, block_id, network_id, tx_hash, rollup_index
FROM sync.claim
WHERE index = $1 AND network_id = $2 AND mainnet_flag AND 0 = $3;
WHERE index = $1 AND network_id = $2 AND NOT mainnet_flag AND rollup_index + 1 = $3;
`
const getClaimSQLRollup = `
SELECT index, orig_net, orig_addr, amount, dest_addr, block_id, network_id, tx_hash, rollup_index, mainnet_flag
const getClaimSQLOriginMainnetDestMainnet = `
SELECT index, orig_net, orig_addr, amount, dest_addr, block_id, network_id, tx_hash, rollup_index
FROM sync.claim
WHERE index = $1 AND network_id = $2 AND NOT mainnet_flag AND rollup_index + 1 = $3;
`
getClaimSQL := getClaimSQLMainnet
if originNetworkID != 0 {
getClaimSQL = getClaimSQLRollup
}
err := p.getExecQuerier(dbTx).
QueryRow(ctx, getClaimSQL, depositCount, destNetworkID, originNetworkID).
Scan(
&claim.Index,
&claim.OriginalNetwork,
&claim.OriginalAddress,
&amount,
&claim.DestinationAddress,
&claim.BlockID,
&claim.NetworkID,
&claim.TxHash,
&claim.RollupIndex,
&claim.MainnetFlag,
)
const getClaimSQLOriginRollupDestMainnet = `
SELECT index, orig_net, orig_addr, amount, dest_addr, block_id, network_id, tx_hash, rollup_index
FROM sync.claim
WHERE index = $1 AND network_id = $2 AND mainnet_flag;
`
const getClaimSQLOriginRollupDestRollup = `
SELECT index, orig_net, orig_addr, amount, dest_addr, block_id, network_id, tx_hash, rollup_index
FROM sync.claim
WHERE index = $1 AND network_id = $2 AND NOT mainnet_flag AND rollup_index + 1 = $3;
`
var row pgx.Row
if originNetworkID != 0 && destNetworkID == 0 {
row = p.getExecQuerier(dbTx).
QueryRow(ctx, getClaimSQLOriginRollupDestMainnet, depositCount, originNetworkID)
claim.MainnetFlag = true
} else if originNetworkID != 0 && destNetworkID != 0 {
row = p.getExecQuerier(dbTx).
QueryRow(ctx, getClaimSQLOriginRollupDestRollup, depositCount, originNetworkID, destNetworkID)
} else if originNetworkID == 0 && destNetworkID != 0 {
row = p.getExecQuerier(dbTx).
QueryRow(ctx, getClaimSQLOriginMainnetDestRollup, depositCount, originNetworkID, destNetworkID)
} else {
row = p.getExecQuerier(dbTx).
QueryRow(ctx, getClaimSQLOriginMainnetDestMainnet, depositCount, originNetworkID)
claim.MainnetFlag = true
}
err := row.Scan(
&claim.Index,
&claim.OriginalNetwork,
&claim.OriginalAddress,
&amount,
&claim.DestinationAddress,
&claim.BlockID,
&claim.NetworkID,
&claim.TxHash,
&claim.RollupIndex,
)
if errors.Is(err, pgx.ErrNoRows) {
return nil, gerror.ErrStorageNotFound
}
Expand All @@ -246,8 +283,28 @@ func (p *PostgresStorage) GetDeposit(ctx context.Context, depositCounterUser uin
deposit etherman.Deposit
amount string
)
const getDepositSQL = "SELECT leaf_type, orig_net, orig_addr, amount, dest_net, dest_addr, deposit_cnt, block_id, b.block_num, d.network_id, tx_hash, metadata, ready_for_claim FROM sync.deposit as d INNER JOIN sync.block as b ON d.network_id = b.network_id AND d.block_id = b.id WHERE d.network_id = $1 AND deposit_cnt = $2"
err := p.getExecQuerier(dbTx).QueryRow(ctx, getDepositSQL, networkID, depositCounterUser).Scan(&deposit.LeafType, &deposit.OriginalNetwork, &deposit.OriginalAddress, &amount, &deposit.DestinationNetwork, &deposit.DestinationAddress, &deposit.DepositCount, &deposit.BlockID, &deposit.BlockNumber, &deposit.NetworkID, &deposit.TxHash, &deposit.Metadata, &deposit.ReadyForClaim)
const getDepositSQL = `
SELECT leaf_type, orig_net, orig_addr, amount, dest_net, dest_addr, deposit_cnt, block_id, b.block_num, d.network_id, tx_hash, metadata, ready_for_claim, origin_rollup_id
FROM sync.deposit as d INNER JOIN sync.block as b ON d.network_id = b.network_id AND d.block_id = b.id
WHERE d.network_id = $1 AND deposit_cnt = $2`
err := p.getExecQuerier(dbTx).
QueryRow(ctx, getDepositSQL, networkID, depositCounterUser).
Scan(
&deposit.LeafType,
&deposit.OriginalNetwork,
&deposit.OriginalAddress,
&amount,
&deposit.DestinationNetwork,
&deposit.DestinationAddress,
&deposit.DepositCount,
&deposit.BlockID,
&deposit.BlockNumber,
&deposit.NetworkID,
&deposit.TxHash,
&deposit.Metadata,
&deposit.ReadyForClaim,
&deposit.OriginRollupID,
)
if errors.Is(err, pgx.ErrNoRows) {
return nil, gerror.ErrStorageNotFound
}
Expand Down Expand Up @@ -544,7 +601,11 @@ func (p *PostgresStorage) GetClaims(ctx context.Context, destAddr string, limit

// GetDeposits gets the deposit list which be smaller than depositCount.
func (p *PostgresStorage) GetDeposits(ctx context.Context, destAddr string, limit uint, offset uint, dbTx pgx.Tx) ([]*etherman.Deposit, error) {
const getDepositsSQL = "SELECT leaf_type, orig_net, orig_addr, amount, dest_net, dest_addr, deposit_cnt, block_id, b.block_num, d.network_id, tx_hash, metadata, ready_for_claim FROM sync.deposit as d INNER JOIN sync.block as b ON d.network_id = b.network_id AND d.block_id = b.id WHERE dest_addr = $1 ORDER BY d.block_id DESC, d.deposit_cnt DESC LIMIT $2 OFFSET $3"
const getDepositsSQL = `
SELECT leaf_type, orig_net, orig_addr, amount, dest_net, dest_addr, deposit_cnt, block_id, b.block_num, d.network_id, tx_hash, metadata, ready_for_claim, origin_rollup_id
FROM sync.deposit as d INNER JOIN sync.block as b ON d.network_id = b.network_id AND d.block_id = b.id
WHERE dest_addr = $1 ORDER BY d.block_id DESC, d.deposit_cnt DESC LIMIT $2 OFFSET $3
`
rows, err := p.getExecQuerier(dbTx).Query(ctx, getDepositsSQL, common.FromHex(destAddr), limit, offset)
if err != nil {
return nil, err
Expand All @@ -557,7 +618,22 @@ func (p *PostgresStorage) GetDeposits(ctx context.Context, destAddr string, limi
deposit etherman.Deposit
amount string
)
err = rows.Scan(&deposit.LeafType, &deposit.OriginalNetwork, &deposit.OriginalAddress, &amount, &deposit.DestinationNetwork, &deposit.DestinationAddress, &deposit.DepositCount, &deposit.BlockID, &deposit.BlockNumber, &deposit.NetworkID, &deposit.TxHash, &deposit.Metadata, &deposit.ReadyForClaim)
err = rows.Scan(
&deposit.LeafType,
&deposit.OriginalNetwork,
&deposit.OriginalAddress,
&amount,
&deposit.DestinationNetwork,
&deposit.DestinationAddress,
&deposit.DepositCount,
&deposit.BlockID,
&deposit.BlockNumber,
&deposit.NetworkID,
&deposit.TxHash,
&deposit.Metadata,
&deposit.ReadyForClaim,
&deposit.OriginRollupID,
)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,6 @@ func (etherMan *Client) depositEvent(ctx context.Context, vLog types.Log, blocks
var deposit Deposit
deposit.Amount = d.Amount
deposit.BlockNumber = vLog.BlockNumber
// TODO: add new column for origin rollupID network?
// TODO: deposit.OriginalNetwork = etherMan.GetRollupID()
deposit.OriginalNetwork = uint(d.OriginNetwork)
deposit.DestinationAddress = d.DestinationAddress
deposit.DestinationNetwork = uint(d.DestinationNetwork)
Expand All @@ -436,6 +434,7 @@ func (etherMan *Client) depositEvent(ctx context.Context, vLog types.Log, blocks
deposit.TxHash = vLog.TxHash
deposit.Metadata = d.Metadata
deposit.LeafType = d.LeafType
deposit.OriginRollupID = etherMan.GetRollupID()

if len(*blocks) == 0 || ((*blocks)[len(*blocks)-1].BlockHash != vLog.BlockHash || (*blocks)[len(*blocks)-1].BlockNumber != vLog.BlockNumber) {
fullBlock, err := etherMan.EtherClient.BlockByHash(ctx, vLog.BlockHash)
Expand Down
2 changes: 2 additions & 0 deletions etherman/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Deposit struct {
Metadata []byte
// it is only used for the bridge service
ReadyForClaim bool
// rollup ID of the network were the deposit was sent
OriginRollupID uint
}

// Claim struct
Expand Down
4 changes: 2 additions & 2 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (s *bridgeService) GetBridges(ctx context.Context, req *pb.GetBridgesReques

var pbDeposits []*pb.Deposit
for _, deposit := range deposits {
claimTxHash, err := s.GetDepositStatus(ctx, deposit.DepositCount, deposit.OriginalNetwork, deposit.DestinationNetwork)
claimTxHash, err := s.GetDepositStatus(ctx, deposit.DepositCount, deposit.OriginRollupID, deposit.NetworkID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -367,7 +367,7 @@ func (s *bridgeService) GetBridge(ctx context.Context, req *pb.GetBridgeRequest)
return nil, err
}

claimTxHash, err := s.GetDepositStatus(ctx, uint(req.DepositCnt), deposit.OriginalNetwork, deposit.DestinationNetwork)
claimTxHash, err := s.GetDepositStatus(ctx, uint(req.DepositCnt), deposit.OriginRollupID, deposit.DestinationNetwork)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit bcf26f3

Please sign in to comment.