Skip to content

Commit

Permalink
Allow to create filtering policy rules by conditional order id or tra…
Browse files Browse the repository at this point in the history
…nsaction hashes (#146)

# Description
It's possible that we need to quickly ignore one specific order, but we
don't want to neither ignore the whole handler (i.e. all TWAP orders),
nor a whole user.

For this reason, I included in this PR support for creating rules to
ignore or drop orders by ID.


## Test
One way to test this is to check how, current version of watch tower
shows a ton of errors and can't process blocks in Sepolia with this
config:

```json
{
  "networks": [
    {
      "name": "sepolia",
      "rpc": "wss://eth-sepolia.g.alchemy.com/v2/bUZqvmR5GCbUsE1meSoBNVlN-IqQs-_r",
      "deploymentBlock": 5072748,
      "filterPolicy": {
        "defaultAction": "DROP",
        "handlers": {
          "0x44569Cbd4E10dd5e97293337964Eff32d58ed352": "ACCEPT",
          "0x519BA24e959E33b3B6220CA98bd353d8c2D89920": "ACCEPT",
          "0x6cF1e9cA41f7611dEf408122793c358a3d11E5a5": "ACCEPT",
          "0xd3338f21c89745e46af56aeaf553cf96ba9bc66f": "ACCEPT",
          "0xE8212F30C28B4AAB467DF3725C14d6e89C2eB967": "ACCEPT"
        }
      },
      "watchdogTimeout": 120
    }
  ]
}

```

However, with this config it works:

```json
{
  "networks": [
    {
      "name": "sepolia",
      "rpc": "wss://eth-sepolia.g.alchemy.com/v2/bUZqvmR5GCbUsE1meSoBNVlN-IqQs-_r",
      "deploymentBlock": 5072748,
      "filterPolicy": {
        "defaultAction": "DROP",
        "conditionalOrderIds": {
          "0x5b3cdb6ffa3c95507cbfc459162609007865c2e87340312d3cd469c4ffbfae81": "SKIP"
        },
        "handlers": {
          "0x44569Cbd4E10dd5e97293337964Eff32d58ed352": "ACCEPT",
          "0x519BA24e959E33b3B6220CA98bd353d8c2D89920": "ACCEPT",
          "0x6cF1e9cA41f7611dEf408122793c358a3d11E5a5": "ACCEPT",
          "0xd3338f21c89745e46af56aeaf553cf96ba9bc66f": "ACCEPT",
          "0xE8212F30C28B4AAB467DF3725C14d6e89C2eB967": "ACCEPT"
        }
      },
      "watchdogTimeout": 120
    }
  ]
}

```

<img width="1091" alt="image"
src="https://github.com/cowprotocol/watch-tower/assets/2352112/e735e77d-a1e4-445f-9a2c-2ef0d0d06faf">
  • Loading branch information
anxolin authored Feb 14, 2024
1 parent b1a8b5a commit 8226fc0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 80 deletions.
14 changes: 13 additions & 1 deletion config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
"rpc": "ws://172.20.0.5:8546",
"deploymentBlock": 17883049,
"filterPolicy": {
"defaultAction": "ACCEPT"
"defaultAction": "ACCEPT",
"conditionalOrderIds": {
"0x5b3cdb6ffa3c95507cbfc459162609007865c2e87340312d3cd469c4ffbfae81": "DROP",
},
"transactions": {
"0x33ef06af308d1e4f94dd61fa8df43fe52b67e8a485f4e4fff75235080e663bfa": "DROP",
},
"handlers": {
"0xd3338f21c89745e46af56aeaf553cf96ba9bc66f": "DROP",
},
"owners": {
"0xd3338f21c89745e46af56aeaf553cf96ba9bc66f": "DROP",
}
}
}
]
Expand Down
26 changes: 23 additions & 3 deletions src/domain/polling/filtering/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ interface PolicyConfig {
defaultAction: FilterAction;
owners: Map<string, FilterAction>;
handlers: Map<string, FilterAction>;
transactions: Map<string, FilterAction>;
conditionalOrderIds: Map<string, FilterAction>;
}

export interface FilterParams {
conditionalOrderId: string;
transaction: string;
owner: string;
conditionalOrderParams: ConditionalOrderParams;
}
Expand All @@ -26,6 +30,8 @@ export class FilterPolicy {
defaultAction: FilterAction[config.defaultAction],
owners: this.convertToMap(config.owners),
handlers: this.convertToMap(config.handlers),
transactions: this.convertToMap(config.transactions),
conditionalOrderIds: this.convertToMap(config.conditionalOrderIds),
};
}

Expand All @@ -35,15 +41,29 @@ export class FilterPolicy {
* @param filterParams params required for the pre-filtering, including the conditional order params, chainId and the owner contract
* @returns The action that should be performed with the conditional order
*/
preFilter({ owner, conditionalOrderParams }: FilterParams): FilterAction {
preFilter({
conditionalOrderId: programmaticOrderId,
transaction,
owner,
conditionalOrderParams,
}: FilterParams): FilterAction {
if (!this.config) {
return FilterAction.ACCEPT;
}

const { owners, handlers } = this.config;
const {
owners,
handlers,
conditionalOrderIds: programmaticOrderIds,
transactions,
} = this.config;

// Find the first matching rule
const action =
handlers.get(conditionalOrderParams.handler) || owners.get(owner);
programmaticOrderIds.get(programmaticOrderId) ||
transactions.get(transaction) ||
owners.get(owner) ||
handlers.get(conditionalOrderParams.handler);

if (action) {
return action;
Expand Down
4 changes: 3 additions & 1 deletion src/domain/polling/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ export async function checkForAndPlaceOrder(
// Apply filtering policy
if (filterPolicy) {
const filterResult = filterPolicy.preFilter({
conditionalOrderId: conditionalOrder.id,
transaction: conditionalOrder.tx,
owner,
conditionalOrderParams: conditionalOrder.params,
});

switch (filterResult) {
case policy.FilterAction.DROP:
log.debug("Dropping conditional order. Reason: AcceptPolicy: DROP");
log.info("Dropping conditional order. Reason: AcceptPolicy: DROP");
ordersPendingDelete.push(conditionalOrder);

continue;
Expand Down
149 changes: 74 additions & 75 deletions src/types/config.schema.json
Original file line number Diff line number Diff line change
@@ -1,79 +1,78 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"networks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"rpc": {
"type": "string",
"format": "uri"
},
"deploymentBlock": {
"type": "integer"
},
"watchdogTimeout": {
"type": "integer"
},
"orderBookApi": {
"type": "string",
"format": "uri"
},
"pageSize": {
"type": "integer"
},
"filterPolicy": {
"type": "object",
"properties": {
"defaultAction": {
"$ref": "#/$defs/filterAction"
},
"owners": {
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/filterAction"
}
},
"handlers": {
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/filterAction"
}
}
},
"required": [
"defaultAction"
],
"additionalProperties": false
}
},
"required": [
"name",
"rpc",
"deploymentBlock",
"filterPolicy"
],
"additionalProperties": false
}
}
},
"required": [
"networks"
],
"additionalProperties": false,
"$defs": {
"filterAction": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"networks": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"rpc": {
"type": "string",
"format": "uri"
},
"deploymentBlock": {
"type": "integer"
},
"watchdogTimeout": {
"type": "integer"
},
"orderBookApi": {
"type": "string",
"enum": [
"ACCEPT",
"DROP",
"SKIP"
]
}
"format": "uri"
},
"pageSize": {
"type": "integer"
},
"filterPolicy": {
"type": "object",
"properties": {
"defaultAction": {
"$ref": "#/$defs/filterAction"
},
"conditionalOrderIds": {
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/filterAction"
}
},
"transactions": {
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/filterAction"
}
},
"owners": {
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/filterAction"
}
},
"handlers": {
"type": "object",
"additionalProperties": {
"$ref": "#/$defs/filterAction"
}
}
},
"required": ["defaultAction"],
"additionalProperties": false
}
},
"required": ["name", "rpc", "deploymentBlock", "filterPolicy"],
"additionalProperties": false
}
}
},
"required": ["networks"],
"additionalProperties": false,
"$defs": {
"filterAction": {
"type": "string",
"enum": ["ACCEPT", "DROP", "SKIP"]
}
}
}
6 changes: 6 additions & 0 deletions src/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export interface Config {
handlers?: {
[k: string]: FilterAction;
};
transactions?: {
[k: string]: FilterAction;
};
conditionalOrderIds?: {
[k: string]: FilterAction;
};
};
}[];
}

0 comments on commit 8226fc0

Please sign in to comment.