Skip to content

Commit

Permalink
remove failed orders from pool, cache them separately and match with …
Browse files Browse the repository at this point in the history
…order hash
  • Loading branch information
zale144 committed Feb 28, 2025
1 parent bffdbff commit 70cb1ed
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion eibc/order_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewOrderClient(cfg config.Config, logger *zap.Logger) (*orderClient, error)
//nolint:gosec
subscriberID := fmt.Sprintf("eibc-client-%d", rand.Int())
orderCh := make(chan []*demandOrder, config.NewOrderBufferSize)
processedCh := make(chan []string, config.NewOrderBufferSize)
processedCh := make(chan []orderFulfillResult, config.NewOrderBufferSize)

hubClient, err := getHubClient(cfg)
if err != nil {
Expand Down
23 changes: 20 additions & 3 deletions eibc/order_fulfiller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eibc

import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"slices"
Expand Down Expand Up @@ -29,7 +30,7 @@ type orderFulfiller struct {
releaseAllReservedOrdersFunds func(demandOrder ...*demandOrder)
debitAllReservedOrdersFunds func(demandOrder ...*demandOrder)
newOrdersCh chan []*demandOrder
processedCh chan []string
processedCh chan []orderFulfillResult
maxOrdersPerTx int
}

Expand All @@ -45,7 +46,7 @@ func newOrderFulfiller(
policyAddress string,
cClient cosmosClient,
newOrdersCh chan []*demandOrder,
processedCh chan []string,
processedCh chan []orderFulfillResult,
releaseAllReservedOrdersFunds func(demandOrder ...*demandOrder),
debitAllReservedOrdersFunds func(demandOrder ...*demandOrder),
maxOrdersPerTx int,
Expand Down Expand Up @@ -98,6 +99,9 @@ func (ol *orderFulfiller) processBatch(orders []*demandOrder) error {
lps, lpsDone []string
)

failResults := make([]orderFulfillResult, 0, len(orders))
successResults := make([]orderFulfillResult, 0, len(orders))

for _, order := range orders {
ids = append(ids, order.id)
if !slices.Contains(lps, order.lpAddress) {
Expand All @@ -111,6 +115,13 @@ func (ol *orderFulfiller) processBatch(orders []*demandOrder) error {
if err := ol.FulfillDemandOrders(batch...); err != nil {
for _, order := range batch {
idsFail = append(idsFail, order.id)

jsn, _ := json.Marshal(&hashableOrder{ID: order.id, Fee: order.fee})
failResults = append(failResults, orderFulfillResult{
orderID: order.id,
failedOrderHash: fmt.Sprintf("%x", sha256.Sum256(jsn)),
})

if !slices.Contains(lpsDone, order.lpAddress) {
lpsDone = append(lpsDone, order.lpAddress)
}
Expand All @@ -123,6 +134,10 @@ func (ol *orderFulfiller) processBatch(orders []*demandOrder) error {

for _, order := range batch {
idsDone = append(idsDone, order.id)
successResults = append(successResults, orderFulfillResult{
orderID: order.id,
})

if !slices.Contains(lpsDone, order.lpAddress) {
lpsDone = append(lpsDone, order.lpAddress)
}
Expand All @@ -131,7 +146,9 @@ func (ol *orderFulfiller) processBatch(orders []*demandOrder) error {
})

ol.logger.Info("orders processed", zap.Strings("ids", idsDone), zap.Strings("failed", idsFail), zap.Strings("lps", lpsDone))
ol.processedCh <- idsDone

ol.processedCh <- successResults
ol.processedCh <- failResults

return nil
}
Expand Down
34 changes: 29 additions & 5 deletions eibc/order_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package eibc

import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"math/rand"
"slices"
Expand Down Expand Up @@ -31,11 +33,13 @@ type orderTracker struct {
fulfilledOrders map[string]*demandOrder
validOrdersCh chan []*demandOrder
outputOrdersCh chan<- []*demandOrder
processedOrdersCh chan []string
processedOrdersCh chan []orderFulfillResult
lps map[string]*lp
minOperatorFeeShare sdk.Dec

pool orderPool
pool orderPool
failedOrderHashes map[string]string
fohmu sync.Mutex

numFulfillers, maxOrdersPerTx int
validation *config.ValidationConfig
Expand All @@ -60,7 +64,7 @@ func newOrderTracker(
maxOrdersPerTx int,
validation *config.ValidationConfig,
ordersCh chan<- []*demandOrder,
processedOrdersCh chan []string,
processedOrdersCh chan []orderFulfillResult,
balanceRefreshInterval,
validateOrdersInterval time.Duration,
resetPoller func(),
Expand All @@ -76,6 +80,7 @@ func newOrderTracker(
minOperatorFeeShare: minOperatorFeeShare,
fullNodeClient: fullNodeClient,
pool: orderPool{orders: make(map[string]*demandOrder)},
failedOrderHashes: make(map[string]string),
lps: make(map[string]*lp),
numFulfillers: numFulfillers,
maxOrdersPerTx: maxOrdersPerTx,
Expand Down Expand Up @@ -161,8 +166,13 @@ func (or *orderTracker) manageProcessed() {
for {
select {
case orders := <-or.processedOrdersCh:
for _, orderID := range orders {
or.pool.removeOrder(orderID)
for _, orderRes := range orders {
if orderRes.failedOrderHash != "" {
or.fohmu.Lock()
or.failedOrderHashes[orderRes.orderID] = orderRes.failedOrderHash
or.fohmu.Unlock()
}
or.pool.removeOrder(orderRes.orderID)
}
}
}
Expand Down Expand Up @@ -272,6 +282,20 @@ func (or *orderTracker) canFulfillOrder(order *demandOrder) bool {
if !or.isRollappSupported(order.rollappId) {
return false
}

or.fohmu.Lock()
defer or.fohmu.Unlock()

if failedHash, ok := or.failedOrderHashes[order.id]; ok {
jsn, _ := json.Marshal(&hashableOrder{ID: order.id, Fee: order.fee})
hash := fmt.Sprintf("%x", sha256.Sum256(jsn))
if hash == failedHash {
return false
}

delete(or.failedOrderHashes, order.id)
}

return true
}

Expand Down
10 changes: 10 additions & 0 deletions eibc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ type demandOrder struct {
checking, valid bool
}

type hashableOrder struct {
ID string `json:"id"`
Fee sdk.Coin `json:"fee"`
}

type orderFulfillResult struct {
orderID string
failedOrderHash string
}

type account struct {
Name string `json:"name"`
Address string `json:"address"`
Expand Down

1 comment on commit 70cb1ed

@djjoeytga
Copy link

Choose a reason for hiding this comment

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

trying to figure out how to release pending eibc transactions. already updated to higher gas fees and trying to find contact info for eibc client. no luck. need some help.

Please sign in to comment.