Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abf/fix #585

Merged
merged 3 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion claimtxman/claimtxman.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (tm *ClaimTxManager) processDepositStatus(ger *etherman.GlobalExitRoot, dbT
return err
}
for _, deposit := range deposits {
claimHash, err := tm.bridgeService.GetDepositStatus(tm.ctx, deposit.DepositCount, deposit.DestinationNetwork)
claimHash, err := tm.bridgeService.GetDepositStatus(tm.ctx, deposit.DepositCount, deposit.OriginalNetwork, deposit.DestinationNetwork)
if err != nil {
log.Errorf("error getting deposit status for deposit %d. Error: %v", deposit.DepositCount, err)
return err
Expand Down
2 changes: 1 addition & 1 deletion claimtxman/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ type storageInterface interface {

type bridgeServiceInterface interface {
GetClaimProof(depositCnt, networkID uint, dbTx pgx.Tx) (*etherman.GlobalExitRoot, [][bridgectrl.KeyLen]byte, [][bridgectrl.KeyLen]byte, error)
GetDepositStatus(ctx context.Context, depositCount uint, destNetworkID uint) (string, error)
GetDepositStatus(ctx context.Context, depositCount uint, originNetworkID, destNetworkID uint) (string, error)
}
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;
122 changes: 113 additions & 9 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 @@ -198,13 +216,60 @@ func (p *PostgresStorage) AddTrustedGlobalExitRoot(ctx context.Context, trustedE
}

// GetClaim gets a specific claim from the storage.
func (p *PostgresStorage) GetClaim(ctx context.Context, depositCount, networkID uint, dbTx pgx.Tx) (*etherman.Claim, error) {
func (p *PostgresStorage) GetClaim(ctx context.Context, depositCount, originNetworkID, destNetworkID uint, dbTx pgx.Tx) (*etherman.Claim, error) {
var (
claim etherman.Claim
amount string
)
const getClaimSQL = "SELECT index, orig_net, orig_addr, amount, dest_addr, block_id, network_id, tx_hash, rollup_index, mainnet_flag FROM sync.claim WHERE index = $1 AND network_id = $2"
err := p.getExecQuerier(dbTx).QueryRow(ctx, getClaimSQL, depositCount, networkID).Scan(&claim.Index, &claim.OriginalNetwork, &claim.OriginalAddress, &amount, &claim.DestinationAddress, &claim.BlockID, &claim.NetworkID, &claim.TxHash, &claim.RollupIndex, &claim.MainnetFlag)
// if mainnet flag == 0 => origin network = rollup index +1
// else origin network = 0
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 NOT mainnet_flag AND rollup_index + 1 = $3;
`
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;
`
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;
Comment on lines +226 to +244

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove duplicate

`
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 @@ -218,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 @@ -516,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 @@ -529,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
2 changes: 1 addition & 1 deletion db/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestBSStorage(t *testing.T) {
require.NoError(t, err)
require.Equal(t, count, uint64(1))

rClaim, err := pg.GetClaim(ctx, 1, 0, tx)
rClaim, err := pg.GetClaim(ctx, 1, 1, 0, tx)
require.NoError(t, err)
require.Equal(t, rClaim.DestinationAddress, claim.DestinationAddress)
require.Equal(t, rClaim.NetworkID, claim.NetworkID)
Expand Down
3 changes: 2 additions & 1 deletion etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (etherMan *Client) processUpdateGlobalExitRootEvent(ctx context.Context, ma
}

func (etherMan *Client) depositEvent(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
log.Debug("Deposit event detected. Processing...")
log.Debugf("Deposit event detected. From RollupID %d Processing...", etherMan.GetRollupID())
d, err := etherMan.PolygonBridge.ParseBridgeEvent(vLog)
if err != nil {
return err
Expand All @@ -434,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
2 changes: 1 addition & 1 deletion server/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type bridgeServiceStorage interface {
GetRoot(ctx context.Context, depositCnt uint, network uint, dbTx pgx.Tx) ([]byte, error)
GetDepositCountByRoot(ctx context.Context, root []byte, network uint8, dbTx pgx.Tx) (uint, error)
GetLatestExitRoot(ctx context.Context, isRollup bool, dbTx pgx.Tx) (*etherman.GlobalExitRoot, error)
GetClaim(ctx context.Context, index uint, networkID uint, dbTx pgx.Tx) (*etherman.Claim, error)
GetClaim(ctx context.Context, index uint, originNetworkID, destNetworkID uint, dbTx pgx.Tx) (*etherman.Claim, error)
GetClaims(ctx context.Context, destAddr string, limit uint, offset uint, dbTx pgx.Tx) ([]*etherman.Claim, error)
GetClaimCount(ctx context.Context, destAddr string, dbTx pgx.Tx) (uint64, error)
GetDeposit(ctx context.Context, depositCnt uint, networkID uint, dbTx pgx.Tx) (*etherman.Deposit, error)
Expand Down
8 changes: 4 additions & 4 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,12 @@ func emptyProof() [][bridgectrl.KeyLen]byte {
}

// GetDepositStatus returns deposit with ready_for_claim status.
func (s *bridgeService) GetDepositStatus(ctx context.Context, depositCount uint, destNetworkID uint) (string, error) {
func (s *bridgeService) GetDepositStatus(ctx context.Context, depositCount uint, originNetworkID, destNetworkID uint) (string, error) {
var (
claimTxHash string
)
// Get the claim tx hash
claim, err := s.storage.GetClaim(ctx, depositCount, destNetworkID, nil)
claim, err := s.storage.GetClaim(ctx, depositCount, originNetworkID, destNetworkID, nil)
if err != nil {
if err != gerror.ErrStorageNotFound {
return "", err
Expand Down 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.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.DestinationNetwork)
claimTxHash, err := s.GetDepositStatus(ctx, uint(req.DepositCnt), deposit.OriginRollupID, deposit.DestinationNetwork)
if err != nil {
return nil, err
}
Expand Down
18 changes: 1 addition & 17 deletions synchronizer/mock_bridgectrl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading