Skip to content

Commit

Permalink
chore: add more logs for OOM search
Browse files Browse the repository at this point in the history
  • Loading branch information
shoom3301 committed Dec 2, 2024
1 parent 994d99d commit c35c4f3
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 53 deletions.
67 changes: 46 additions & 21 deletions src/domain/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import {
toConditionalOrderParams,
getAreConditionalOrderParamsEqual,
getLogger,
handleExecutionError,
metrics,
toConditionalOrderParams,
} from "../../utils";
import { BytesLike, ethers } from "ethers";

import {
ComposableCoW,
ComposableCoW__factory,
ComposableCoWInterface,
ConditionalOrderCreatedEvent,
IConditionalOrder,
MerkleRootSetEvent,
ComposableCoW__factory,
Owner,
Proof,
Registry,
Expand All @@ -21,6 +22,9 @@ import { ConditionalOrder, ConditionalOrderParams } from "@cowprotocol/cow-sdk";

import { ChainContext } from "../../services/chain";

const composableCow = ComposableCoW__factory.createInterface();
const log = getLogger("addContract:_addContract");

/**
* Listens to these events on the `ComposableCoW` contract:
* - `ConditionalOrderCreated`
Expand All @@ -47,20 +51,19 @@ async function _addContract(
context: ChainContext,
event: ConditionalOrderCreatedEvent
) {
const log = getLogger("addContract:_addContract");
const composableCow = ComposableCoW__factory.createInterface();
const { registry } = context;
const { transactionHash: tx, blockNumber } = event;

// Process the logs
let hasErrors = false;
let numContractsAdded = 0;

const { error, added } = await _registerNewOrder(
const { error, added } = await registerNewOrder(
event,
composableCow,
registry
);

if (added) {
metrics.ownersTotal.labels(context.chainId.toString()).inc();
numContractsAdded++;
Expand All @@ -69,6 +72,7 @@ async function _addContract(
`Failed to register Smart Order from tx ${tx} on block ${blockNumber}. Error: ${error}`
);
}

hasErrors ||= error;

if (numContractsAdded > 0) {
Expand All @@ -86,15 +90,16 @@ async function _addContract(
}
}

export async function _registerNewOrder(
async function registerNewOrder(
event: ConditionalOrderCreatedEvent | MerkleRootSetEvent,
composableCow: ComposableCoWInterface,
registry: Registry
): Promise<{ error: boolean; added: boolean }> {
const log = getLogger("addContract:_registerNewOrder");
const log = getLogger("addContract:registerNewOrder");
const { transactionHash: tx } = event;
const { network } = registry;
let added = false;

try {
// Check if the log is a ConditionalOrderCreated event
if (
Expand Down Expand Up @@ -177,13 +182,14 @@ export async function _registerNewOrder(
/**
* Attempt to add an owner's conditional order to the registry
*
* @param tx transaction that created the conditional order
* @param owner to add the conditional order to
* @param params for the conditional order
* @param proof for the conditional order (if it is part of a merkle root)
* @param composableCow address of the contract that emitted the event
* @param registry of all conditional orders
*/
export function add(
function add(
tx: string,
owner: Owner,
params: ConditionalOrderParams,
Expand All @@ -206,6 +212,23 @@ export function add(
// Iterate over the conditionalOrders to make sure that the params are not already in the registry
for (const conditionalOrder of conditionalOrders?.values() ?? []) {
// Check if the params are in the conditionalOrder
if (conditionalOrder) {
const areConditionalOrderParamsEqual =
getAreConditionalOrderParamsEqual(conditionalOrder.params, params);

if (
areConditionalOrderParamsEqual &&
conditionalOrder.params !== params
) {
log.error(
"Conditional order params are equal but not the same",
conditionalOrder.id,
JSON.stringify(params)
);
}
}

// TODO: this is a shallow comparison, should we do a deep comparison?
if (conditionalOrder.params === params) {
exists = true;
break;
Expand Down Expand Up @@ -245,6 +268,7 @@ export function add(
},
])
);
// TODO: why twice?
metrics.activeOwnersTotal.labels(network).inc();
metrics.activeOrdersTotal.labels(network).inc();
}
Expand All @@ -256,19 +280,20 @@ export function add(
* @param root the merkle root to check against
* @param registry of all conditional orders
*/
export function flush(owner: Owner, root: BytesLike, registry: Registry) {
if (registry.ownerOrders.has(owner)) {
const conditionalOrders = registry.ownerOrders.get(owner);
if (conditionalOrders !== undefined) {
for (const conditionalOrder of conditionalOrders.values()) {
if (
conditionalOrder.proof !== null &&
conditionalOrder.proof.merkleRoot !== root
) {
// Delete the conditional order
conditionalOrders.delete(conditionalOrder);
}
}
function flush(owner: Owner, root: BytesLike, registry: Registry) {
if (!registry.ownerOrders.has(owner)) return;

const conditionalOrders = registry.ownerOrders.get(owner);

if (conditionalOrders === undefined) return;

for (const conditionalOrder of conditionalOrders.values()) {
if (
conditionalOrder.proof !== null &&
conditionalOrder.proof.merkleRoot !== root
) {
// Delete the conditional order
conditionalOrders.delete(conditionalOrder);
}
}
}
12 changes: 10 additions & 2 deletions src/services/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
Multicall3__factory,
Registry,
RegistryBlock,
blockToRegistryBlock,
} from "../types";
import {
LoggerWithMethods,
Expand All @@ -32,7 +31,7 @@ const WATCHDOG_TIMEOUT_DEFAULT_SECS = 30;
const MULTICALL3 = "0xcA11bde05977b3631167028862bE2a173976CA11";
const PAGE_SIZE_DEFAULT = 5000;

export const SDK_BACKOFF_NUM_OF_ATTEMPTS = 5;
const SDK_BACKOFF_NUM_OF_ATTEMPTS = 5;

enum ChainSync {
/** The chain is currently in the warm-up phase, synchronising from contract genesis or lastBlockProcessed */
Expand Down Expand Up @@ -459,6 +458,7 @@ async function processBlock(
let hasErrors = false;
for (const event of events) {
const receipt = await provider.getTransactionReceipt(event.transactionHash);

if (receipt) {
// run action
log.debug(`Running "addContract" action for TX ${event.transactionHash}`);
Expand Down Expand Up @@ -614,3 +614,11 @@ function getProvider(rpcUrl: string): providers.Provider {
async function asyncSleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

function blockToRegistryBlock(block: ethers.providers.Block): RegistryBlock {
return {
number: block.number,
timestamp: block.timestamp,
hash: block.hash,
};
}
Loading

0 comments on commit c35c4f3

Please sign in to comment.