Skip to content

Commit

Permalink
fix: fix possible panic with batchSize (#703)
Browse files Browse the repository at this point in the history
* fix: fix possible panic with batchSize

* chore: log if offset greater than length
  • Loading branch information
lehainam-dev authored Jan 22, 2025
1 parent 9b7c216 commit 9c66179
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 30 deletions.
13 changes: 10 additions & 3 deletions pkg/liquidity-source/deltaswap-v1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)

pairAddresses, err := u.listPairAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -255,13 +255,20 @@ func (u *PoolsListUpdater) newMetadata(newOffset int) ([]byte, error) {
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": u.config.DexID,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/ether-vista/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)

pairAddresses, err := u.listPairAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -258,13 +258,20 @@ func (u *PoolsListUpdater) newMetadata(newOffset int) ([]byte, error) {
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": u.config.DexID,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/ringswap/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)

pairAddresses, err := u.listPairAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -310,13 +310,20 @@ func (u *PoolsListUpdater) newMetadata(newOffset int) ([]byte, error) {
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": u.config.DexID,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/solidly-v2/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(int(poolFactoryData.AllPairsLength.Int64()), u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(int(poolFactoryData.AllPairsLength.Int64()), u.config.NewPoolLimit, offset)

poolAddresses, err := u.listPoolAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -329,13 +329,20 @@ func (u *PoolsListUpdater) newStaticExtra(poolMetadata PoolMetadata) ([]byte, er
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": u.config.DexID,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/swap-x-v2/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(int(poolFactoryData.AllPairsLength.Int64()), u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(int(poolFactoryData.AllPairsLength.Int64()), u.config.NewPoolLimit, offset)

poolAddresses, err := u.listPoolAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -342,13 +342,20 @@ func (u *PoolsListUpdater) newStaticExtra(poolMetadata velodromev2.PoolMetadata)
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": u.config.DexID,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/uniswap-v1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(totalExchanges, u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(totalExchanges, u.config.NewPoolLimit, offset)

exchanges, err := u.listExchanges(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -236,13 +236,20 @@ func (u *PoolsListUpdater) newMetadata(newOffset int) ([]byte, error) {
return metadataBytes, nil
}

func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": DexType,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/uniswap-v2/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)

pairAddresses, err := u.listPairAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -273,13 +273,20 @@ func (u *PoolsListUpdater) newExtra(fee uint64, feePrecision uint64) ([]byte, er
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": u.config.DexID,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/velodrome-v1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(int(pairFactoryData.AllPairsLength.Int64()), u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(int(pairFactoryData.AllPairsLength.Int64()), u.config.NewPoolLimit, offset)

pairAddresses, err := u.listPairAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -339,13 +339,20 @@ func (u *PoolsListUpdater) newExtra(pairMetadata PairMetadata, factoryData PairF
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": u.config.DexID,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/velodrome-v2/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(int(poolFactoryData.AllPairsLength.Int64()), u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(int(poolFactoryData.AllPairsLength.Int64()), u.config.NewPoolLimit, offset)

poolAddresses, err := u.listPoolAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -337,13 +337,20 @@ func (u *PoolsListUpdater) newStaticExtra(poolMetadata PoolMetadata) ([]byte, er
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": u.config.DexID,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down
13 changes: 10 additions & 3 deletions pkg/liquidity-source/virtual-fun/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Warn("getOffset failed")
}

batchSize := getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)
batchSize := u.getBatchSize(allPairsLength, u.config.NewPoolLimit, offset)

pairAddresses, err := u.listPairAddresses(ctx, offset, batchSize)
if err != nil {
Expand Down Expand Up @@ -286,13 +286,20 @@ func (u *PoolsListUpdater) newMetadata(newOffset int) ([]byte, error) {
// @params limit number of pairs to be fetched in one run
// @params offset index of the last pair has been fetched
// @returns batchSize
func getBatchSize(length int, limit int, offset int) int {
func (u *PoolsListUpdater) getBatchSize(length int, limit int, offset int) int {
if offset == length {
return 0
}

if offset+limit >= length {
return length - offset
if offset > length {
logger.WithFields(logger.Fields{
"dex": DexType,
"offset": offset,
"length": length,
}).Warn("[getBatchSize] offset is greater than length")
}
return max(length-offset, 0)
}

return limit
Expand Down

0 comments on commit 9c66179

Please sign in to comment.