Skip to content

Commit

Permalink
SQS-377 | Unit test ProcessPool (#493)
Browse files Browse the repository at this point in the history
* SQS-377 | Unit test ProcessPool

Introduces unit tests for Orderbook usecase ProcessPool
method.
  • Loading branch information
deividaspetraitis authored Sep 2, 2024
1 parent 99e5abc commit 14b01e5
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 86 deletions.
18 changes: 18 additions & 0 deletions domain/mocks/orderbook_grpc_client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type OrderbookGRPCClientMock struct {
MockGetOrdersByTickCb func(ctx context.Context, contractAddress string, tick int64) ([]orderbookplugindomain.Order, error)
MockGetActiveOrdersCb func(ctx context.Context, contractAddress string, ownerAddress string) (orderbookdomain.Orders, uint64, error)
MockGetTickUnrealizedCancelsCb func(ctx context.Context, contractAddress string, tickIDs []int64) ([]orderbookgrpcclientdomain.UnrealizedTickCancels, error)
FetchTickUnrealizedCancelsCb func(ctx context.Context, chunkSize int, contractAddress string, tickIDs []int64) ([]orderbookgrpcclientdomain.UnrealizedTickCancels, error)
MockQueryTicksCb func(ctx context.Context, contractAddress string, ticks []int64) ([]orderbookdomain.Tick, error)
FetchTicksCb func(ctx context.Context, chunkSize int, contractAddress string, tickIDs []int64) ([]orderbookdomain.Tick, error)
}

func (o *OrderbookGRPCClientMock) GetOrdersByTick(ctx context.Context, contractAddress string, tick int64) ([]orderbookplugindomain.Order, error) {
Expand All @@ -42,10 +44,26 @@ func (o *OrderbookGRPCClientMock) GetTickUnrealizedCancels(ctx context.Context,
return nil, nil
}

func (o *OrderbookGRPCClientMock) FetchTickUnrealizedCancels(ctx context.Context, chunkSize int, contractAddress string, tickIDs []int64) ([]orderbookgrpcclientdomain.UnrealizedTickCancels, error) {
if o.FetchTickUnrealizedCancelsCb != nil {
return o.FetchTickUnrealizedCancelsCb(ctx, chunkSize, contractAddress, tickIDs)
}

return nil, nil
}

func (o *OrderbookGRPCClientMock) QueryTicks(ctx context.Context, contractAddress string, ticks []int64) ([]orderbookdomain.Tick, error) {
if o.MockQueryTicksCb != nil {
return o.MockQueryTicksCb(ctx, contractAddress, ticks)
}

return nil, nil
}

func (o *OrderbookGRPCClientMock) FetchTicks(ctx context.Context, chunkSize int, contractAddress string, tickIDs []int64) ([]orderbookdomain.Tick, error) {
if o.FetchTicksCb != nil {
return o.FetchTicksCb(ctx, chunkSize, contractAddress, tickIDs)
}

return nil, nil
}
75 changes: 72 additions & 3 deletions domain/orderbook/grpcclient/orderbook_grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package orderbookgrpcclientdomain

import (
"context"
"fmt"

cosmwasmdomain "github.com/osmosis-labs/sqs/domain/cosmwasm"
orderbookdomain "github.com/osmosis-labs/sqs/domain/orderbook"
Expand All @@ -21,8 +22,22 @@ type OrderBookClient interface {
// GetTickUnrealizedCancels fetches unrealized cancels by tick from the orderbook contract.
GetTickUnrealizedCancels(ctx context.Context, contractAddress string, tickIDs []int64) ([]UnrealizedTickCancels, error)

// FetchTickUnrealizedCancels fetches the unrealized cancels for a given tick ID and contract address.
// It returns the unrealized cancels and an error if any.
// Errors if:
// - failed to fetch unrealized cancels
// - mismatch in number of unrealized cancels fetched
FetchTickUnrealizedCancels(ctx context.Context, chunkSize int, contractAddress string, tickIDs []int64) ([]UnrealizedTickCancels, error)

// QueryTicks fetches ticks by tickIDs from the orderbook contract.
QueryTicks(ctx context.Context, contractAddress string, ticks []int64) ([]orderbookdomain.Tick, error)

// FetchTicksForOrderbook fetches the ticks in chunks of maxQueryTicks at the time for a given tick ID and contract address.
// It returns the ticks and an error if any.
// Errors if:
// - failed to fetch ticks
// - mismatch in number of ticks fetched
FetchTicks(ctx context.Context, chunkSize int, contractAddress string, tickIDs []int64) ([]orderbookdomain.Tick, error)
}

// orderbookClientImpl is an implementation of OrderbookCWAPIClient.
Expand All @@ -39,7 +54,7 @@ func New(wasmClient wasmtypes.QueryClient) *orderbookClientImpl {
}
}

// GetOrdersByTick implements OrderbookCWAPIClient.
// GetOrdersByTick implements OrderBookClient.
func (o *orderbookClientImpl) GetOrdersByTick(ctx context.Context, contractAddress string, tick int64) ([]orderbookplugindomain.Order, error) {
ordersByTick := ordersByTick{Tick: tick}

Expand All @@ -51,7 +66,7 @@ func (o *orderbookClientImpl) GetOrdersByTick(ctx context.Context, contractAddre
return orders.Orders, nil
}

// GetActiveOrders implements OrderbookCWAPIClient.
// GetActiveOrders implements OrderBookClient.
func (o *orderbookClientImpl) GetActiveOrders(ctx context.Context, contractAddress string, ownerAddress string) (orderbookdomain.Orders, uint64, error) {
var orders activeOrdersResponse
if err := cosmwasmdomain.QueryCosmwasmContract(ctx, o.wasmClient, contractAddress, activeOrdersRequest{OrdersByOwner: ordersByOwner{Owner: ownerAddress}}, &orders); err != nil {
Expand All @@ -61,7 +76,7 @@ func (o *orderbookClientImpl) GetActiveOrders(ctx context.Context, contractAddre
return orders.Orders, orders.Count, nil
}

// GetTickUnrealizedCancels implements OrderbookCWAPIClient.
// GetTickUnrealizedCancels implements OrderBookClient.
func (o *orderbookClientImpl) GetTickUnrealizedCancels(ctx context.Context, contractAddress string, tickIDs []int64) ([]UnrealizedTickCancels, error) {
var unrealizedCancels unrealizedCancelsResponse
if err := cosmwasmdomain.QueryCosmwasmContract(ctx, o.wasmClient, contractAddress, unrealizedCancelsByTickIdRequest{UnrealizedCancels: unrealizedCancelsRequestPayload{TickIds: tickIDs}}, &unrealizedCancels); err != nil {
Expand All @@ -70,6 +85,33 @@ func (o *orderbookClientImpl) GetTickUnrealizedCancels(ctx context.Context, cont
return unrealizedCancels.Ticks, nil
}

// FetchTickUnrealizedCancels implements OrderBookClient.
func (o *orderbookClientImpl) FetchTickUnrealizedCancels(ctx context.Context, chunkSize int, contractAddress string, tickIDs []int64) ([]UnrealizedTickCancels, error) {
allUnrealizedCancels := make([]UnrealizedTickCancels, 0, len(tickIDs))

for i := 0; i < len(tickIDs); i += chunkSize {
end := i + chunkSize
if end > len(tickIDs) {
end = len(tickIDs)
}

currentTickIDs := tickIDs[i:end]

unrealizedCancels, err := o.GetTickUnrealizedCancels(ctx, contractAddress, currentTickIDs)
if err != nil {
return nil, fmt.Errorf("failed to fetch unrealized cancels for ticks %v: %w", currentTickIDs, err)
}

allUnrealizedCancels = append(allUnrealizedCancels, unrealizedCancels...)
}

if len(allUnrealizedCancels) != len(tickIDs) {
return nil, fmt.Errorf("mismatch in number of unrealized cancels fetched: expected %d, got %d", len(tickIDs), len(allUnrealizedCancels))
}

return allUnrealizedCancels, nil
}

// QueryTicks implements OrderBookClient.
func (o *orderbookClientImpl) QueryTicks(ctx context.Context, contractAddress string, ticks []int64) ([]orderbookdomain.Tick, error) {
var orderbookTicks queryTicksResponse
Expand All @@ -78,3 +120,30 @@ func (o *orderbookClientImpl) QueryTicks(ctx context.Context, contractAddress st
}
return orderbookTicks.Ticks, nil
}

// FetchTicks implements OrderBookClient.
func (o *orderbookClientImpl) FetchTicks(ctx context.Context, chunkSize int, contractAddress string, tickIDs []int64) ([]orderbookdomain.Tick, error) {
finalTickStates := make([]orderbookdomain.Tick, 0, len(tickIDs))

for i := 0; i < len(tickIDs); i += chunkSize {
end := i + chunkSize
if end > len(tickIDs) {
end = len(tickIDs)
}

currentTickIDs := tickIDs[i:end]

tickStates, err := o.QueryTicks(ctx, contractAddress, currentTickIDs)
if err != nil {
return nil, fmt.Errorf("failed to fetch ticks for pool %s: %w", contractAddress, err)
}

finalTickStates = append(finalTickStates, tickStates...)
}

if len(finalTickStates) != len(tickIDs) {
return nil, fmt.Errorf("mismatch in number of ticks fetched: expected %d, got %d", len(tickIDs), len(finalTickStates))
}

return finalTickStates, nil
}
60 changes: 60 additions & 0 deletions orderbook/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,63 @@ type ParsingPlacedAtError struct {
func (e ParsingPlacedAtError) Error() string {
return fmt.Sprintf("error parsing placed_at %s: %v", e.PlacedAt, e.Err)
}

// PoolNilError represents an error when the pool is nil.
type PoolNilError struct{}

func (e PoolNilError) Error() string {
return "pool is nil when processing order book"
}

// CosmWasmPoolModelNilError represents an error when the CosmWasmPoolModel is nil.
type CosmWasmPoolModelNilError struct{}

func (e CosmWasmPoolModelNilError) Error() string {
return "cw pool model is nil when processing order book"
}

// NotAnOrderbookPoolError represents an error when the pool is not an orderbook pool.
type NotAnOrderbookPoolError struct {
PoolID uint64
}

func (e NotAnOrderbookPoolError) Error() string {
return fmt.Sprintf("pool is not an orderbook pool %d", e.PoolID)
}

// FailedToCastPoolModelError represents an error when the pool model cannot be cast to a CosmWasmPool.
type FailedToCastPoolModelError struct{}

func (e FailedToCastPoolModelError) Error() string {
return "failed to cast pool model to CosmWasmPool"
}

// FetchTicksError represents an error when fetching ticks fails.
type FetchTicksError struct {
ContractAddress string
Err error
}

func (e FetchTicksError) Error() string {
return fmt.Sprintf("failed to fetch ticks for pool %s: %v", e.ContractAddress, e.Err)
}

// FetchUnrealizedCancelsError represents an error when fetching unrealized cancels fails.
type FetchUnrealizedCancelsError struct {
ContractAddress string
Err error
}

func (e FetchUnrealizedCancelsError) Error() string {
return fmt.Sprintf("failed to fetch unrealized cancels for pool %s: %v", e.ContractAddress, e.Err)
}

// TickIDMismatchError represents an error when there is a mismatch between tick IDs.
type TickIDMismatchError struct {
ExpectedID int64
ActualID int64
}

func (e TickIDMismatchError) Error() string {
return fmt.Sprintf("tick id mismatch when fetching tick states %d %d", e.ExpectedID, e.ActualID)
}
2 changes: 1 addition & 1 deletion orderbook/usecase/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// OrderBookEntry is an alias of orderBookEntry for testing purposes
func (o *orderbookUseCaseImpl) CreateFormattedLimitOrder(
func (o *OrderbookUseCaseImpl) CreateFormattedLimitOrder(
poolID uint64,
order orderbookdomain.Order,
quoteAsset orderbookdomain.Asset,
Expand Down
Loading

0 comments on commit 14b01e5

Please sign in to comment.