Flash unwinder contracts and scripts, relay lane tuning, trustless bridge and token-aggregation updates.
Made-with: Cursor
This commit is contained in:
@@ -9,6 +9,8 @@ export class MessageQueue {
|
||||
this.processed = new Set();
|
||||
this.failed = new Set();
|
||||
this.retryCounts = new Map();
|
||||
this.messageStore = new Map();
|
||||
this.inFlight = new Map();
|
||||
}
|
||||
|
||||
async add(messageData) {
|
||||
@@ -27,6 +29,7 @@ export class MessageQueue {
|
||||
return;
|
||||
}
|
||||
|
||||
this.messageStore.set(messageId, messageData);
|
||||
// Add to queue
|
||||
this.queue.push(messageData);
|
||||
this.logger.info(`Added message ${messageId} to queue. Queue size: ${this.queue.length}`);
|
||||
@@ -37,17 +40,25 @@ export class MessageQueue {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.queue.shift();
|
||||
const messageData = this.queue.shift();
|
||||
if (messageData && messageData.messageId) {
|
||||
this.inFlight.set(messageData.messageId, messageData);
|
||||
}
|
||||
return messageData;
|
||||
}
|
||||
|
||||
async markProcessed(messageId) {
|
||||
this.processed.add(messageId);
|
||||
this.retryCounts.delete(messageId);
|
||||
this.inFlight.delete(messageId);
|
||||
this.messageStore.delete(messageId);
|
||||
this.logger.info(`Message ${messageId} marked as processed`);
|
||||
}
|
||||
|
||||
async markFailed(messageId) {
|
||||
this.failed.add(messageId);
|
||||
this.retryCounts.delete(messageId);
|
||||
this.inFlight.delete(messageId);
|
||||
this.logger.error(`Message ${messageId} marked as failed`);
|
||||
}
|
||||
|
||||
@@ -58,10 +69,22 @@ export class MessageQueue {
|
||||
async retry(messageId) {
|
||||
const count = this.retryCounts.get(messageId) || 0;
|
||||
this.retryCounts.set(messageId, count + 1);
|
||||
|
||||
// Find message in queue or re-add it
|
||||
// In a production system, you'd store the original message data
|
||||
this.logger.info(`Message ${messageId} retry count: ${count + 1}`);
|
||||
|
||||
const existingIndex = this.queue.findIndex(m => m.messageId === messageId);
|
||||
if (existingIndex >= 0) {
|
||||
this.logger.info(`Message ${messageId} retry count: ${count + 1}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const messageData = this.inFlight.get(messageId) || this.messageStore.get(messageId);
|
||||
if (!messageData) {
|
||||
this.logger.warn(`Cannot requeue ${messageId}; original message payload is unavailable`);
|
||||
return;
|
||||
}
|
||||
|
||||
this.inFlight.delete(messageId);
|
||||
this.queue.push(messageData);
|
||||
this.logger.info(`Message ${messageId} requeued. Retry count: ${count + 1}. Queue size: ${this.queue.length}`);
|
||||
}
|
||||
|
||||
getStats() {
|
||||
@@ -72,4 +95,3 @@ export class MessageQueue {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import { ethers } from 'ethers';
|
||||
import { MessageSentABI, RelayRouterABI, RelayBridgeABI } from './abis.js';
|
||||
import { MessageSentABI, RelayRouterABI, RelayBridgeABI, ERC20ABI } from './abis.js';
|
||||
import { MessageQueue } from './MessageQueue.js';
|
||||
import {
|
||||
isRelayShedding,
|
||||
@@ -161,7 +161,7 @@ export class RelayService {
|
||||
const lastSuccessMs = this.lastRelaySuccess && this.lastRelaySuccess.at ? Date.parse(this.lastRelaySuccess.at) : 0;
|
||||
if (
|
||||
this.lastError &&
|
||||
this.lastError.scope === 'relay_message' &&
|
||||
(this.lastError.scope === 'relay_message' || this.lastError.scope === 'bridge_inventory') &&
|
||||
Number.isFinite(lastErrorMs) &&
|
||||
lastErrorMs > 0 &&
|
||||
lastErrorMs >= lastSuccessMs
|
||||
@@ -172,6 +172,35 @@ export class RelayService {
|
||||
return 'operational';
|
||||
}
|
||||
|
||||
async ensureTargetBridgeInventory(messageId, targetBridge, tokenAmounts) {
|
||||
if (process.env.RELAY_ENFORCE_BRIDGE_TOKEN_BALANCE !== '1') {
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
for (const tokenAmount of tokenAmounts) {
|
||||
const tokenAddress = ethers.getAddress(tokenAmount.token);
|
||||
const requiredAmount = typeof tokenAmount.amount === 'bigint'
|
||||
? tokenAmount.amount
|
||||
: BigInt(tokenAmount.amount.toString());
|
||||
|
||||
const tokenContract = new ethers.Contract(tokenAddress, ERC20ABI, this.destinationProvider);
|
||||
const availableAmount = await tokenContract.balanceOf(targetBridge);
|
||||
if (availableAmount < requiredAmount) {
|
||||
const shortfall = requiredAmount - availableAmount;
|
||||
return {
|
||||
ok: false,
|
||||
token: tokenAddress,
|
||||
requiredAmount,
|
||||
availableAmount,
|
||||
shortfall,
|
||||
message: `Insufficient bridge inventory for ${messageId}: ${tokenAddress} available=${availableAmount.toString()} required=${requiredAmount.toString()} shortfall=${shortfall.toString()}`
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
getHealthSnapshot() {
|
||||
const queueStats = this.messageQueue.getStats();
|
||||
const status = this.getHealthStatus();
|
||||
@@ -359,7 +388,13 @@ export class RelayService {
|
||||
return (
|
||||
msg.includes('maximum RPC range') ||
|
||||
msg.includes('exceeds maximum') ||
|
||||
(msg.includes('-32000') && msg.includes('range'))
|
||||
msg.includes('requested too many blocks') ||
|
||||
msg.includes('maximum is set to') ||
|
||||
(msg.includes('-32000') && (
|
||||
msg.includes('range') ||
|
||||
msg.includes('too many blocks') ||
|
||||
msg.includes('maximum is set to')
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -771,6 +806,27 @@ export class RelayService {
|
||||
amountType: Number(ta.amountType) // Ensure it's a number (uint8)
|
||||
};
|
||||
});
|
||||
|
||||
const inventoryCheck = await this.ensureTargetBridgeInventory(
|
||||
messageId,
|
||||
targetBridge,
|
||||
mappedTokenAmounts
|
||||
);
|
||||
if (!inventoryCheck.ok) {
|
||||
const inventoryError = new Error(inventoryCheck.message);
|
||||
this.logger.warn(inventoryCheck.message);
|
||||
this.recordError('bridge_inventory', inventoryError, {
|
||||
message_id: messageId,
|
||||
target_bridge: targetBridge,
|
||||
token: inventoryCheck.token,
|
||||
available_amount: inventoryCheck.availableAmount.toString(),
|
||||
required_amount: inventoryCheck.requiredAmount.toString(),
|
||||
shortfall: inventoryCheck.shortfall.toString()
|
||||
});
|
||||
await this.messageQueue.retry(messageId);
|
||||
await new Promise(resolve => setTimeout(resolve, this.config.retry.retryDelay));
|
||||
return null;
|
||||
}
|
||||
|
||||
// Optional normalization for legacy bridges that decode 4-field payloads:
|
||||
// (recipient, amount, sender, nonce). TwoWayTokenBridgeL1/L2 decode 2-field payloads
|
||||
@@ -840,7 +896,7 @@ export class RelayService {
|
||||
target_bridge: targetBridge,
|
||||
tx_hash: receipt.hash
|
||||
};
|
||||
if (this.lastError && this.lastError.scope === 'relay_message') {
|
||||
if (this.lastError && (this.lastError.scope === 'relay_message' || this.lastError.scope === 'bridge_inventory')) {
|
||||
this.lastError = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,3 +19,7 @@ export const RelayBridgeABI = [
|
||||
"function processed(bytes32) view returns (bool)",
|
||||
"function processedTransfers(bytes32) view returns (bool)"
|
||||
];
|
||||
|
||||
export const ERC20ABI = [
|
||||
"function balanceOf(address account) view returns (uint256)"
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user