mirror of
https://github.com/mempool/mempool.git
synced 2025-04-08 20:08:32 +02:00
Merge pull request #3030 from mempool/mononaut/hotfix-gbt-mempool-clone
safer mempool cloning for different GBT algorithms
This commit is contained in:
commit
fd16f5c4c3
@ -19,6 +19,7 @@ import feeApi from './fee-api';
|
||||
import BlocksAuditsRepository from '../repositories/BlocksAuditsRepository';
|
||||
import BlocksSummariesRepository from '../repositories/BlocksSummariesRepository';
|
||||
import Audit from './audit';
|
||||
import { deepClone } from '../utils/clone';
|
||||
|
||||
class WebsocketHandler {
|
||||
private wss: WebSocket.Server | undefined;
|
||||
@ -421,7 +422,7 @@ class WebsocketHandler {
|
||||
let projectedBlocks;
|
||||
// template calculation functions have mempool side effects, so calculate audits using
|
||||
// a cloned copy of the mempool if we're running a different algorithm for mempool updates
|
||||
const auditMempool = (config.MEMPOOL.ADVANCED_GBT_AUDIT === config.MEMPOOL.ADVANCED_GBT_MEMPOOL) ? _memPool : JSON.parse(JSON.stringify(_memPool));
|
||||
const auditMempool = (config.MEMPOOL.ADVANCED_GBT_AUDIT === config.MEMPOOL.ADVANCED_GBT_MEMPOOL) ? _memPool : deepClone(_memPool);
|
||||
if (config.MEMPOOL.ADVANCED_GBT_AUDIT) {
|
||||
projectedBlocks = await mempoolBlocks.makeBlockTemplates(auditMempool, false);
|
||||
} else {
|
||||
|
14
backend/src/utils/clone.ts
Normal file
14
backend/src/utils/clone.ts
Normal file
@ -0,0 +1,14 @@
|
||||
// simple recursive deep clone for literal-type objects
|
||||
// does not preserve Dates, Maps, Sets etc
|
||||
// does not support recursive objects
|
||||
// properties deeper than maxDepth will be shallow cloned
|
||||
export function deepClone(obj: any, maxDepth: number = 50, depth: number = 0): any {
|
||||
let cloned = obj;
|
||||
if (depth < maxDepth && typeof obj === 'object') {
|
||||
cloned = Array.isArray(obj) ? [] : {};
|
||||
for (const key in obj) {
|
||||
cloned[key] = deepClone(obj[key], maxDepth, depth + 1);
|
||||
}
|
||||
}
|
||||
return cloned;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user