From 637002828e99cefc333ad5c00e3cfec277d2b206 Mon Sep 17 00:00:00 2001 From: Anxo Rodriguez Date: Thu, 19 Dec 2024 20:03:46 +0000 Subject: [PATCH] feat: apply filter policy before adding an order (#176) # Description We are not respecting the filter policy in order to add or not add orders from events. ## Context I noticed how staging indexed this an order of an owner that shouldn't have. However, in the next block got dropped because we apply the filtering using the policies at the beginning of processing a block. This explain this increment + decreement: ![image](https://github.com/user-attachments/assets/f92eb74c-1ab0-4d15-8002-e3cbd8f7b08b) You can also see this in the logs: ![image](https://github.com/user-attachments/assets/ed7c9c34-f4b9-4e94-8602-f591b503974f) ## Chages This pr applies the filter policy before adding an order, so we don't add it in the first place. ## Test You can create one TWAP using any account and see how now we don't get this increment and decrement in Grafana. --- src/domain/events/index.ts | 81 ++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/src/domain/events/index.ts b/src/domain/events/index.ts index dc1a754..8c0db92 100644 --- a/src/domain/events/index.ts +++ b/src/domain/events/index.ts @@ -17,13 +17,10 @@ import { Proof, Registry, } from "../../types"; -import { - ConditionalOrder, - ConditionalOrderParams, - SupportedChainId, -} from "@cowprotocol/cow-sdk"; +import { ConditionalOrder, ConditionalOrderParams } from "@cowprotocol/cow-sdk"; import { ChainContext } from "../../services/chain"; +import { policy } from "../polling/filtering"; const composableCow = ComposableCoW__factory.createInterface(); @@ -39,10 +36,9 @@ export async function processNewOrderEvent( context: ChainContext, event: ConditionalOrderCreatedEvent ) { - const { chainId } = context; + const { chainId, registry } = context; const action = async () => { - const { registry, chainId } = context; const { transactionHash: tx, blockNumber } = event; const log = getLogger({ @@ -55,10 +51,12 @@ export async function processNewOrderEvent( let hasErrors = false; let numContractsAdded = 0; - const { error, added } = await decodeAndAddOrder(event, registry, chainId); + const { error, added } = await decodeAndAddOrder(event, context); if (added) { - metrics.ownersTotal.labels(context.chainId.toString()).inc(); + const network = context.chainId.toString(); + metrics.ownersTotal.labels(network).inc(); + metrics.activeOrdersTotal.labels(network).inc(); numContractsAdded++; } else { log.error( @@ -97,9 +95,9 @@ export async function processNewOrderEvent( async function decodeAndAddOrder( event: ConditionalOrderCreatedEvent | MerkleRootSetEvent, - registry: Registry, - chainId: SupportedChainId + context: ChainContext ): Promise<{ error: boolean; added: boolean }> { + const { chainId, registry } = context; const log = getLogger({ name: "decodeAndAddOrder", chainId, @@ -123,18 +121,18 @@ async function decodeAndAddOrder( ) as [string, IConditionalOrder.ConditionalOrderParamsStruct]; // Attempt to add the conditional order to the registry - addOrder( + added = addOrder( eventLog.transactionHash, owner, toConditionalOrderParams(params), null, eventLog.address, - registry, - chainId, - event.blockNumber + event.blockNumber, + context ); - added = true; - metrics.singleOrdersTotal.labels(network).inc(); + if (added) { + metrics.singleOrdersTotal.labels(network).inc(); + } } else if ( event.topics[0] == composableCow.getEventTopic("MerkleRootSet") ) { @@ -166,18 +164,19 @@ async function decodeAndAddOrder( order as BytesLike ); // Attempt to add the conditional order to the registry - addOrder( + + added = addOrder( event.transactionHash, owner, toConditionalOrderParams(decodedOrder[1]), { merkleRoot: root, path: decodedOrder[0] }, eventLog.address, - registry, - chainId, - event.blockNumber + event.blockNumber, + context ); - added = true; - metrics.merkleRootTotal.labels(network).inc(); + if (added) { + metrics.merkleRootTotal.labels(network).inc(); + } } } } @@ -208,15 +207,33 @@ function addOrder( params: ConditionalOrderParams, proof: Proof | null, composableCow: string, - registry: Registry, - chainId: SupportedChainId, - blockNumber: number -) { + blockNumber: number, + context: ChainContext +): boolean { + const { chainId, registry, filterPolicy } = context; const log = getLogger({ name: "addOrder", chainId, blockNumber }); const { handler, salt, staticInput } = params; const { network, ownerOrders } = registry; + const conditionalOrderParams = toConditionalOrderParams(params); const conditionalOrderId = ConditionalOrder.leafToId(params); + + // Apply the filter policy, in case this order should be dropped + if (filterPolicy) { + const filterResult = filterPolicy.preFilter({ + conditionalOrderId, + transaction: tx, + owner, + conditionalOrderParams, + }); + + if (filterResult === policy.FilterAction.DROP) { + // Drop the order + log.info("Not adding the conditional order. Reason: AcceptPolicy: DROP"); + return false; + } + } + if (ownerOrders.has(owner)) { const conditionalOrders = ownerOrders.get(owner); log.info(`Adding conditional order to already existing owner ${owner}`, { @@ -240,8 +257,9 @@ function addOrder( } } - // If the params are not in the conditionalOrder, add them + // If the params are not in the conditionalOrder if (!exists) { + // Add the order for existing owner conditionalOrders?.add({ id: conditionalOrderId, tx, @@ -250,9 +268,10 @@ function addOrder( orders: new Map(), composableCow, }); - metrics.activeOrdersTotal.labels(network).inc(); + return true; } } else { + // Add the order for new owner log.info(`Adding conditional order to new owner ${owner}:`, { conditionalOrderId, tx, @@ -275,8 +294,10 @@ function addOrder( ); metrics.activeOwnersTotal.labels(network).inc(); - metrics.activeOrdersTotal.labels(network).inc(); + return true; } + + return false; } /**