Skip to content

Commit

Permalink
trimmed claim api response
Browse files Browse the repository at this point in the history
  • Loading branch information
rachit77 committed Feb 21, 2025
1 parent 322e35e commit d29f407
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 41 deletions.
2 changes: 1 addition & 1 deletion bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func newBridgeSync(
func (s *BridgeSync) GetClaimsPaged(
ctx context.Context,
page, pageSize uint32,
) ([]*Claim, int, error) {
) ([]*ClaimResponse, int, error) {
if s.processor.isHalted() {
return nil, 0, sync.ErrInconsistentState
}
Expand Down
35 changes: 33 additions & 2 deletions bridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@ type Claim struct {
FromAddress common.Address `meddler:"from_address,address"`
}

// ClaimResponse is the representation of a claim event with trimmed fields
type ClaimResponse struct {
GlobalIndex *big.Int
DestinationNetwork uint32
TxHash common.Hash
Amount *big.Int
BlockNum uint64
FromAddress common.Address
DestinationAddress common.Address
OriginAddress common.Address
OriginNetwork uint32
BlockTimestamp uint64
}

// TokenMapping representation of a NewWrappedToken event, that is emitted by the bridge contract
type TokenMapping struct {
BlockNum uint64 `meddler:"block_num"`
Expand Down Expand Up @@ -283,7 +297,7 @@ func (p *processor) GetBridgesPaged(

func (p *processor) GetClaimsPaged(
ctx context.Context, pageNumber, pageSize uint32,
) ([]*Claim, int, error) {
) ([]*ClaimResponse, int, error) {
tx, err := p.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
if err != nil {
return nil, 0, err
Expand Down Expand Up @@ -316,7 +330,24 @@ func (p *processor) GetClaimsPaged(
if err = meddler.ScanAll(rows, &claimPtrs); err != nil {
return nil, 0, err
}
return claimPtrs, count, nil

claimResponsePtrs := make([]*ClaimResponse, len(claimPtrs))
for i, bridgePtr := range claimPtrs {
claimResponsePtrs[i] = &ClaimResponse{
GlobalIndex: bridgePtr.GlobalIndex,
DestinationNetwork: bridgePtr.DestinationNetwork,
TxHash: bridgePtr.TxHash,
Amount: bridgePtr.Amount,
BlockNum: bridgePtr.BlockNum,
FromAddress: bridgePtr.FromAddress,
DestinationAddress: bridgePtr.DestinationAddress,
OriginAddress: bridgePtr.OriginAddress,
OriginNetwork: bridgePtr.OriginNetwork,
BlockTimestamp: bridgePtr.BlockTimestamp,
}
}

return claimResponsePtrs, count, nil
}

func (p *processor) queryBlockRange(tx db.Querier, fromBlock, toBlock uint64, table string) (*sql.Rows, error) {
Expand Down
42 changes: 21 additions & 21 deletions bridgesync/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1077,12 +1077,12 @@ func TestGetClaimsPaged(t *testing.T) {

claims :=
[]Claim{
{BlockNum: 1, BlockPos: 1, GlobalIndex: big.NewInt(1), Amount: big.NewInt(1)},
{BlockNum: 2, BlockPos: 2, GlobalIndex: big.NewInt(2), Amount: big.NewInt(1)},
{BlockNum: 3, BlockPos: 3, GlobalIndex: big.NewInt(3), Amount: big.NewInt(1)},
{BlockNum: 4, BlockPos: 4, GlobalIndex: big.NewInt(4), Amount: big.NewInt(1)},
{BlockNum: 5, BlockPos: 5, GlobalIndex: uint64Max, Amount: big.NewInt(1)},
{BlockNum: 6, BlockPos: 6, GlobalIndex: uint256Max, Amount: big.NewInt(1)},
{BlockNum: 1, GlobalIndex: big.NewInt(1), Amount: big.NewInt(1)},
{BlockNum: 2, GlobalIndex: big.NewInt(2), Amount: big.NewInt(1)},
{BlockNum: 3, GlobalIndex: big.NewInt(3), Amount: big.NewInt(1)},
{BlockNum: 4, GlobalIndex: big.NewInt(4), Amount: big.NewInt(1)},
{BlockNum: 5, GlobalIndex: uint64Max, Amount: big.NewInt(1)},
{BlockNum: 6, GlobalIndex: uint256Max, Amount: big.NewInt(1)},
}

path := path.Join(t.TempDir(), "bridgesyncGetClaimsPaged.sqlite")
Expand All @@ -1109,16 +1109,16 @@ func TestGetClaimsPaged(t *testing.T) {
pageSize uint32
page uint32
expectedCount int
expectedClaims []*Claim
expectedClaims []*ClaimResponse
expectedError error
}{
{
name: "t1",
pageSize: 1,
page: 2,
expectedCount: 6,
expectedClaims: []*Claim{
{BlockNum: 5, BlockPos: 5, GlobalIndex: uint64Max, Amount: big.NewInt(1)},
expectedClaims: []*ClaimResponse{
{BlockNum: 5, GlobalIndex: uint64Max, Amount: big.NewInt(1)},
},
expectedError: nil,
},
Expand All @@ -1127,13 +1127,13 @@ func TestGetClaimsPaged(t *testing.T) {
pageSize: 20,
page: 1,
expectedCount: 6,
expectedClaims: []*Claim{
{BlockNum: 6, BlockPos: 6, GlobalIndex: uint256Max, Amount: big.NewInt(1)},
{BlockNum: 5, BlockPos: 5, GlobalIndex: uint64Max, Amount: big.NewInt(1)},
{BlockNum: 4, BlockPos: 4, GlobalIndex: big.NewInt(4), Amount: big.NewInt(1)},
{BlockNum: 3, BlockPos: 3, GlobalIndex: big.NewInt(3), Amount: big.NewInt(1)},
{BlockNum: 2, BlockPos: 2, GlobalIndex: big.NewInt(2), Amount: big.NewInt(1)},
{BlockNum: 1, BlockPos: 1, GlobalIndex: big.NewInt(1), Amount: big.NewInt(1)},
expectedClaims: []*ClaimResponse{
{BlockNum: 6, GlobalIndex: uint256Max, Amount: big.NewInt(1)},
{BlockNum: 5, GlobalIndex: uint64Max, Amount: big.NewInt(1)},
{BlockNum: 4, GlobalIndex: big.NewInt(4), Amount: big.NewInt(1)},
{BlockNum: 3, GlobalIndex: big.NewInt(3), Amount: big.NewInt(1)},
{BlockNum: 2, GlobalIndex: big.NewInt(2), Amount: big.NewInt(1)},
{BlockNum: 1, GlobalIndex: big.NewInt(1), Amount: big.NewInt(1)},
},
expectedError: nil,
},
Expand All @@ -1142,10 +1142,10 @@ func TestGetClaimsPaged(t *testing.T) {
pageSize: 3,
page: 2,
expectedCount: 6,
expectedClaims: []*Claim{
{BlockNum: 3, BlockPos: 3, GlobalIndex: big.NewInt(3), Amount: big.NewInt(1)},
{BlockNum: 2, BlockPos: 2, GlobalIndex: big.NewInt(2), Amount: big.NewInt(1)},
{BlockNum: 1, BlockPos: 1, GlobalIndex: big.NewInt(1), Amount: big.NewInt(1)},
expectedClaims: []*ClaimResponse{
{BlockNum: 3, GlobalIndex: big.NewInt(3), Amount: big.NewInt(1)},
{BlockNum: 2, GlobalIndex: big.NewInt(2), Amount: big.NewInt(1)},
{BlockNum: 1, GlobalIndex: big.NewInt(1), Amount: big.NewInt(1)},
},
expectedError: nil,
},
Expand All @@ -1154,7 +1154,7 @@ func TestGetClaimsPaged(t *testing.T) {
pageSize: 3,
page: 4,
expectedCount: 6,
expectedClaims: []*Claim{},
expectedClaims: []*ClaimResponse{},
expectedError: db.ErrNotFound,
},
}
Expand Down
6 changes: 3 additions & 3 deletions rpc/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ func (b *BridgeEndpoints) GetBridges(

// ClaimsResult contains the claims and the total count of claims
type ClaimsResult struct {
Claims []*bridgesync.Claim `json:"claims"`
Count int `json:"count"`
Claims []*bridgesync.ClaimResponse `json:"claims"`
Count int `json:"count"`
}

func (b *BridgeEndpoints) GetClaims(networkID uint32,
Expand All @@ -286,7 +286,7 @@ func (b *BridgeEndpoints) GetClaims(networkID uint32,
c.Add(ctx, 1)

var (
claims []*bridgesync.Claim
claims []*bridgesync.ClaimResponse
count int
)

Expand Down
2 changes: 1 addition & 1 deletion rpc/bridge_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Bridger interface {
depositCount *uint64,
) ([]*bridgesync.BridgeResponse, int, error)
GetTokenMappings(ctx context.Context, pageNumber, pageSize uint32) ([]*bridgesync.TokenMapping, int, error)
GetClaimsPaged(ctx context.Context, page, pageSize uint32) ([]*bridgesync.Claim, int, error)
GetClaimsPaged(ctx context.Context, page, pageSize uint32) ([]*bridgesync.ClaimResponse, int, error)
}

type LastGERer interface {
Expand Down
8 changes: 2 additions & 6 deletions rpc/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,17 +614,15 @@ func TestGetClaims(t *testing.T) {
t.Run("GetClaims for L1 network", func(t *testing.T) {
page := uint32(1)
pageSize := uint32(10)
claims := []*bridgesync.Claim{
claims := []*bridgesync.ClaimResponse{
{
BlockNum: 1,
BlockPos: 1,
GlobalIndex: big.NewInt(1),
OriginNetwork: 0,
OriginAddress: common.HexToAddress("0x1"),
DestinationNetwork: 10,
DestinationAddress: common.HexToAddress("0x2"),
Amount: common.Big0,
Metadata: []byte("metadata"),
},
}

Expand All @@ -646,17 +644,15 @@ func TestGetClaims(t *testing.T) {
t.Run("GetClaims for L2 network", func(t *testing.T) {
page := uint32(1)
pageSize := uint32(10)
Claims := []*bridgesync.Claim{
Claims := []*bridgesync.ClaimResponse{
{
BlockNum: 1,
BlockPos: 1,
GlobalIndex: big.NewInt(1),
OriginNetwork: 0,
OriginAddress: common.HexToAddress("0x1"),
DestinationNetwork: 10,
DestinationAddress: common.HexToAddress("0x2"),
Amount: common.Big0,
Metadata: []byte("metadata"),
},
}

Expand Down
14 changes: 7 additions & 7 deletions rpc/mocks/mock_bridger.go

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

0 comments on commit d29f407

Please sign in to comment.