Skip to content

Commit d5fc10b

Browse files
authored
FIX: CSM Queue (#2285)
* FIX: CSM Queue * FIX: Format
1 parent 6ce7ea2 commit d5fc10b

File tree

2 files changed

+2108
-485
lines changed

2 files changed

+2108
-485
lines changed

launcher/src/backend/web3/CSM.js

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ async function getSigningKeys(contract, nodeOperatorId, startIndex, keysCount) {
145145
}
146146
}
147147

148-
async function getDepositQueue(contract) {
148+
async function getDepositQueue(contract, index) {
149149
try {
150150
if (!contract) {
151151
throw new Error("Contract is not initialized.");
152152
}
153-
const queueInfo = await contract.methods.depositQueue().call();
153+
const queueInfo = await contract.methods.depositQueuePointers(index).call();
154154

155155
const head = Number(queueInfo[0]);
156156
const tail = Number(queueInfo[1]);
@@ -162,13 +162,13 @@ async function getDepositQueue(contract) {
162162
}
163163
}
164164

165-
async function getDepositQueueItem(contract, index) {
165+
async function getDepositQueueItem(contract, queuePriority, index) {
166166
try {
167167
if (!contract) {
168168
throw new Error("Contract is not initialized.");
169169
}
170170
// Fetch the batch item from the contract
171-
const batchItem = await contract.methods.depositQueueItem(index).call();
171+
const batchItem = await contract.methods.depositQueueItem(queuePriority, index).call();
172172

173173
// Function to extract batch information
174174
const extractBatchInfo = (batch) => ({
@@ -188,6 +188,19 @@ async function getDepositQueueItem(contract, index) {
188188
}
189189
}
190190

191+
async function getLowestPriorityQueueIndex(contract) {
192+
try {
193+
if (!contract) {
194+
throw new Error("Contract is not initialized.");
195+
}
196+
const lowestPriority = await contract.methods.QUEUE_LOWEST_PRIORITY().call();
197+
return lowestPriority;
198+
} catch (error) {
199+
log.error("Error calling getLowestPriorityQueueIndex:", error);
200+
return null;
201+
}
202+
}
203+
191204
/**
192205
* Retrieves signing keys with deposit queue information for a given Node Operator.
193206
*
@@ -243,6 +256,7 @@ async function getSigningKeysWithQueueInfo(monitoring) {
243256
// Retrieve enqueued count
244257
const nodeOperatorInfo = await getNodeOperatorInfo(contract, nodeOperatorId);
245258
const enqueuedCount = nodeOperatorInfo.enqueuedCount;
259+
246260
if (enqueuedCount === null || enqueuedCount <= 0) {
247261
log.info("No enqueued validators for this Node Operator.");
248262
}
@@ -261,44 +275,52 @@ async function getSigningKeysWithQueueInfo(monitoring) {
261275
return null;
262276
}
263277

264-
// Initialize queue data
265-
const queueData = await getDepositQueue(contract);
266-
if (!queueData) {
267-
log.error("Failed to retrieve deposit queue data.");
268-
return null;
269-
}
270-
const { head, tail } = queueData;
278+
// Get the number of priority queues that exist (index 0 - LOWEST_PRIORITY)
279+
const LOWEST_PRIORITY = await getLowestPriorityQueueIndex(contract);
271280

272281
// Prepare results
273282
const signingKeysWithQueueInfo = signingKeys.map((key) => ({ key, queuePosition: 0 })); // Default queuePosition to 0
274283

275-
if (enqueuedCount > 0) {
276-
let remainingKeysToMark = Number(enqueuedCount);
284+
// then loop over all queues and get their head/tail pointers
285+
for (let queuePriority = 0; queuePriority <= LOWEST_PRIORITY; queuePriority++) {
286+
// Initialize queue data
287+
const queueData = await getDepositQueue(contract, queuePriority);
288+
if (!queueData) {
289+
log.error("Failed to retrieve deposit queue data.");
290+
return null;
291+
}
292+
const { head, tail } = queueData;
293+
294+
if (enqueuedCount > 0) {
295+
let remainingKeysToMark = Number(enqueuedCount);
277296

278-
// Traverse the deposit queue from tail to head
279-
for (let index = tail; index >= head && remainingKeysToMark > 0; index--) {
280-
const queueItem = await getDepositQueueItem(contract, index);
297+
// Traverse the deposit queue from tail to head
298+
for (let index = tail; index >= head && remainingKeysToMark > 0; index--) {
299+
const queueItem = await getDepositQueueItem(contract, queuePriority, index);
281300

282-
if (queueItem.nodeId === BigInt(nodeOperatorId)) {
283-
const keysCountInQueue = Number(queueItem.keysCount);
301+
if (queueItem.nodeId === BigInt(nodeOperatorId)) {
302+
const keysCountInQueue = Number(queueItem.keysCount);
284303

285-
// Calculate queue position
286-
const queuePosition = queueItem.nextBatch - BigInt(head);
304+
// Calculate queue position
305+
const queuePosition = queueItem.nextBatch - BigInt(head);
287306

288-
// Determine range of keys to mark in reverse order
289-
const startIndex = signingKeysWithQueueInfo.length - remainingKeysToMark;
290-
const endIndex = startIndex + keysCountInQueue;
307+
// Determine range of keys to mark in reverse order
308+
const startIndex = signingKeysWithQueueInfo.length - remainingKeysToMark;
309+
const endIndex = startIndex + keysCountInQueue;
291310

292-
for (let i = endIndex - 1; i >= startIndex; i--) {
293-
signingKeysWithQueueInfo[i].queuePosition = queuePosition;
294-
}
311+
for (let i = endIndex - 1; i >= startIndex; i--) {
312+
signingKeysWithQueueInfo[i].queuePosition = queuePosition;
313+
}
295314

296-
// Reduce the remaining keys to mark
297-
remainingKeysToMark -= keysCountInQueue;
315+
// Reduce the remaining keys to mark
316+
remainingKeysToMark -= keysCountInQueue;
317+
}
298318
}
319+
} else {
320+
log.info("No enqueued validators to process in the deposit queue.");
299321
}
300322
}
301-
323+
log.info(`Retrieved signing keys with queue information for ${signingKeysWithQueueInfo.length} keys. (${enqueuedCount} enqueued)`);
302324
// Return signing keys with queue information
303325
return signingKeysWithQueueInfo;
304326
} catch (error) {

0 commit comments

Comments
 (0)