Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
temaniarpit27 committed Feb 13, 2025
1 parent 3c40709 commit 8525d22
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 54 deletions.
4 changes: 2 additions & 2 deletions bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ func (s *BridgeSync) Start(ctx context.Context) {
s.driver.Sync(ctx)
}

func (s *BridgeSync) GetBridgesPaged(ctx context.Context, page, pageSize, depositCount uint64) ([]Bridge, error) {
func (s *BridgeSync) GetBridgesPaged(ctx context.Context, page, pageSize uint32, depositCount uint64) ([]*Bridge, uint64, error) {
if s.processor.isHalted() {
return nil, sync.ErrInconsistentState
return nil, 0, sync.ErrInconsistentState
}
return s.processor.GetBridgesPaged(ctx, page, pageSize, depositCount)
}
Expand Down
32 changes: 21 additions & 11 deletions bridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ func (p *processor) GetClaims(
}

func (p *processor) GetBridgesPaged(
ctx context.Context, page, pageSize, depositCount uint64,
) ([]Bridge, error) {
ctx context.Context, page, pageSize uint32, depositCount uint64,
) ([]*Bridge, uint64, error) {
tx, err := p.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, err
return nil, 0, err
}
defer func() {
if err := tx.Rollback(); err != nil {
Expand All @@ -208,23 +208,33 @@ func (p *processor) GetBridgesPaged(
}
rows, err := p.queryPaged(tx, page, pageSize, "bridge", orderBy, order, whereClause)
if err != nil {
return nil, err
return nil, 0, err
}
count, err := p.getTotalNumberOfRecords("bridge")
if err != nil {
return nil, 0, err
}
bridgePtrs := []*Bridge{}
if err = meddler.ScanAll(rows, &bridgePtrs); err != nil {
return nil, err
return nil, 0, err
}
bridgesIface := db.SlicePtrsToSlice(bridgePtrs)
bridges, ok := bridgesIface.([]Bridge)
if !ok {
return nil, errors.New("failed to convert from []*Bridge to []Bridge")
return bridgePtrs, count, nil
}

// getTotalNumberOfRecords returns the total number of records in the given table
func (p *processor) getTotalNumberOfRecords(tableName string) (uint64, error) {
count := 0
err := p.db.QueryRow(fmt.Sprintf(`SELECT COUNT(*) AS count FROM %s;`, tableName)).Scan(&count)
if err != nil {
return 0, err
}
return bridges, nil

return uint64(count), nil
}

func (p *processor) queryPaged(
tx db.Querier,
page, pageSize uint64,
page, pageSize uint32,
table, orderBy, order, whereClause string,
) (*sql.Rows, error) {
rows, err := tx.Query(`
Expand Down
5 changes: 0 additions & 5 deletions bridgesync/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -866,10 +866,5 @@ func TestProcessBlockInvalidIndex(t *testing.T) {
require.True(t, errors.Is(err, sync.ErrInconsistentState))
}

func TestGetBridgeProcessor(t *testing.T) {

}

func TestGetBridgesPaged(t *testing.T) {

}
36 changes: 21 additions & 15 deletions rpc/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,35 +149,38 @@ func (b *BridgeEndpoints) InjectedInfoAfterIndex(networkID uint32, l1InfoTreeInd
)
}

func (b *BridgeEndpoints) GetBridges(page, pageSize, depositCount, networkID uint64) (interface{}, rpc.Error) {
// Force valid page: must be at least 1
if page < 1 {
page = 1
}
// BridgesResult contains the bridges and the total count of bridges
type BridgesResult struct {
Bridges []*bridgesync.Bridge `json:"bridges"`
Count uint64 `json:"count"`
}

// pageSize must be in [1..200]; otherwise, default to 20
if pageSize < 1 || pageSize > 200 {
pageSize = 20
}
func (b *BridgeEndpoints) GetBridges(page, pageSize, networkID uint32, depositCount uint64) (interface{}, rpc.Error) {
page, pageSize = ValidatePaginationParams(page, pageSize)

ctx, cancel := context.WithTimeout(context.Background(), b.readTimeout)
defer cancel()

// TODO - Do we need metrics?
c, merr := b.meter.Int64Counter("getBridges")
if merr != nil {
b.logger.Warnf("failed to create get_token_mappings counter: %s", merr)
}
c.Add(ctx, 1)

var (
bridges []bridgesync.Bridge
bridges []*bridgesync.Bridge
err error
count uint64
)

switch {
case networkID == 0:
bridges, err = b.bridgeL1.GetBridgesPaged(ctx, page, pageSize, depositCount)
bridges, count, err = b.bridgeL1.GetBridgesPaged(ctx, page, pageSize, depositCount)
if err != nil {
return nil, rpc.NewRPCError(rpc.DefaultErrorCode, fmt.Sprintf("failed to get deposit, error: %s", err))
}
case networkID == uint64(b.networkID):
bridges, err = b.bridgeL2.GetBridgesPaged(ctx, page, pageSize, depositCount)
case networkID == b.networkID:
bridges, count, err = b.bridgeL2.GetBridgesPaged(ctx, page, pageSize, depositCount)
if err != nil {
return nil, rpc.NewRPCError(rpc.DefaultErrorCode, fmt.Sprintf("failed to get deposit, error: %s", err))
}
Expand All @@ -187,7 +190,10 @@ func (b *BridgeEndpoints) GetBridges(page, pageSize, depositCount, networkID uin
fmt.Sprintf("this client does not support network %d", networkID),
)
}
return bridges, nil
return BridgesResult{
Bridges: bridges,
Count: count,
}, nil
}

// GetProof returns the proofs needed to claim a bridge. NetworkID and depositCount refere to the bridge origin
Expand Down
2 changes: 1 addition & 1 deletion rpc/bridge_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
type Bridger interface {
GetProof(ctx context.Context, depositCount uint32, localExitRoot common.Hash) (tree.Proof, error)
GetRootByLER(ctx context.Context, ler common.Hash) (*tree.Root, error)
GetBridgesPaged(ctx context.Context, page, pageSize, depositCount uint64) ([]bridgesync.Bridge, error)
GetBridgesPaged(ctx context.Context, page, pageSize uint32, depositCount uint64) ([]*bridgesync.Bridge, uint64, error)
}

type LastGERer interface {
Expand Down
5 changes: 1 addition & 4 deletions rpc/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestGetDeposit(t *testing.T) {
}

func TestGetDeposits(t *testing.T) {
func TestGetBridges(t *testing.T) {
}

func TestGetFirstL1InfoTreeIndexForL1Bridge(t *testing.T) {
Expand Down
39 changes: 23 additions & 16 deletions rpc/mocks/mock_bridger.go

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

18 changes: 18 additions & 0 deletions rpc/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package rpc

const DEFAULT_PAGE_SIZE = 20

Check failure on line 3 in rpc/utils.go

View workflow job for this annotation

GitHub Actions / lint

ST1003: should not use ALL_CAPS in Go names; use CamelCase instead (stylecheck)
const MAX_PAGE_SIZE = 200

Check failure on line 4 in rpc/utils.go

View workflow job for this annotation

GitHub Actions / lint

ST1003: should not use ALL_CAPS in Go names; use CamelCase instead (stylecheck)
const DEFAULT_PAGE = 1

Check failure on line 5 in rpc/utils.go

View workflow job for this annotation

GitHub Actions / lint

ST1003: should not use ALL_CAPS in Go names; use CamelCase instead (stylecheck)

func ValidatePaginationParams(page, pageSize uint32) (uint32, uint32) {
// Force valid page: must be at least 1
if page < 1 {
page = DEFAULT_PAGE
}

// pageSize must be in [1..200]; otherwise, default to 20
if pageSize < 1 || pageSize > MAX_PAGE_SIZE {
pageSize = DEFAULT_PAGE_SIZE
}
return page, pageSize
}

0 comments on commit 8525d22

Please sign in to comment.