8000 Minor refactor for projected block transactions by softsimon · Pull Request #1779 · mempool/mempool · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Minor refactor for projected block transactions #1779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 25 additions & 20 deletions backend/src/api/mempool-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MempoolBlocks {
}

public getMempoolBlockDeltas(): MempoolBlockDelta[] {
return this.mempoolBlockDeltas
return this.mempoolBlockDeltas;
}

public updateMempoolBlocks(memPool: { [txid: string]: TransactionExtended }): void {
Expand Down Expand Up @@ -72,11 +72,12 @@ class MempoolBlocks {
logger.debug('Mempool blocks calculated in ' + time / 1000 + ' seconds');

const { blocks, deltas } = this.calculateMempoolBlocks(memPoolArray, this.mempoolBlocks);
this.mempoolBlocks = blocks
this.mempoolBlockDeltas = deltas
this.mempoolBlocks = blocks;
this.mempoolBlockDeltas = deltas;
}

private calculateMempoolBlocks(transactionsSorted: TransactionExtended[], prevBlocks: MempoolBlockWithTransactions[]): { blocks: MempoolBlockWithTransactions[], deltas: MempoolBlockDelta[] } {
private calculateMempoolBlocks(transactionsSorted: TransactionExtended[], prevBlocks: MempoolBlockWithTransactions[]):
{ blocks: MempoolBlockWithTransactions[], deltas: MempoolBlockDelta[] } {
const mempoolBlocks: MempoolBlockWithTransactions[] = [];
const mempoolBlockDeltas: MempoolBlockDelta[] = [];
let blockWeight = 0;
Expand All @@ -100,37 +101,41 @@ class MempoolBlocks {
}
// Calculate change from previous block states
for (let i = 0; i < Math.max(mempoolBlocks.length, prevBlocks.length); i++) {
let added: TransactionStripped[] = []
let removed: string[] = []
let added: TransactionStripped[] = [];
let removed: string[] = [];
if (mempoolBlocks[i] && !prevBlocks[i]) {
added = mempoolBlocks[i].transactions
added = mempoolBlocks[i].transactions;
} else if (!mempoolBlocks[i] && prevBlocks[i]) {
removed = prevBlocks[i].transactions.map(tx => tx.txid)
removed = prevBlocks[i].transactions.map(tx => tx.txid);
} else if (mempoolBlocks[i] && prevBlocks[i]) {
const prevIds = {}
const newIds = {}
const prevIds = {};
const newIds = {};
prevBlocks[i].transactions.forEach(tx => {
prevIds[tx.txid] = true
})
prevIds[tx.txid] = true;
});
mempoolBlocks[i].transactions.forEach(tx => {
newIds[tx.txid] = true
})
newIds[tx.txid] = true;
});
prevBlocks[i].transactions.forEach(tx => {
if (!newIds[tx.txid]) removed.push(tx.txid)
})
if (!newIds[tx.txid]) {
removed.push(tx.txid);
}
});
mempoolBlocks[i].transactions.forEach(tx => {
if (!prevIds[tx.txid]) added.push(tx)
})
if (!prevIds[tx.txid]) {
added.push(tx);
}
});
}
mempoolBlockDeltas.push({
added,
removed
})
});
}
return {
blocks: mempoolBlocks,
deltas: mempoolBlockDeltas
}
};
}

private dataToMempoolBlocks(transactions: TransactionExtended[],
Expand Down
13 changes: 7 additions & 6 deletions backend/src/api/websocket-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logger from '../logger';
import * as WebSocket from 'ws';
import { BlockExtended, TransactionExtended, WebsocketResponse, MempoolBlock, MempoolBlockDelta, OptimizedStatistic, ILoadingIndicators, IConversionRates } from '../mempool.interfaces';
import { BlockExtended, TransactionExtended, WebsocketResponse, MempoolBlock, MempoolBlockDelta,
OptimizedStatistic, ILoadingIndicators, IConversionRates } from '../mempool.interfaces';
import blocks from './blocks';
import memPool from './mempool';
import backendInfo from './backend-info';
Expand Down Expand Up @@ -110,15 +111,15 @@ class WebsocketHandler {
}
}

if (parsedMessage && parsedMessage['track-mempool-block'] != null) {
if (parsedMessage && parsedMessage['track-mempool-block'] !== undefined) {
if (Number.isInteger(parsedMessage['track-mempool-block']) && parsedMessage['track-mempool-block'] >= 0) {
const index = parsedMessage['track-mempool-block'];
client['track-mempool-block'] = index;
const mBlocksWithTransactions = mempoolBlocks.getMempoolBlocksWithTransactions();
if (mBlocksWithTransactions[index]) {
response['projected-mempool-block'] = {
response['projected-block-transactions'] = {
index: index,
block: mBlocksWithTransactions[index],
blockTransactions: mBlocksWithTransactions[index].transactions
};
}
} else {
Expand Down Expand Up @@ -389,7 +390,7 @@ class WebsocketHandler {
if (client['track-mempool-block'] >= 0) {
const index = client['track-mempool-block'];
if (mBlockDeltas[index]) {
response['projected-mempool-block'] = {
response['projected-block-transactions'] = {
index: index,
delta: mBlockDeltas[index],
};
Expand Down Expand Up @@ -526,7 +527,7 @@ class WebsocketHandler {
if (client['track-mempool-block'] >= 0) {
const index = client['track-mempool-block'];
if (mBlockDeltas && mBlockDeltas[index]) {
response['projected-mempool-block'] = {
response['projected-block-transactions'] = {
index: index,
delta: mBlockDeltas[index],
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { FastVertexArray } from './fast-vertex-array'
import TxSprite from './tx-sprite'
import TxView from './tx-view'
import { TransactionStripped } from 'src/app/interfaces/websocket.interface';
import { Position, Square } from './sprite-types'
Expand Down
CE59
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
or compacting into a smaller Float32Array when there's space to do so.
*/

import TxSprite from './tx-sprite'
import TxSprite from './tx-sprite';

export class FastVertexArray {
length: number;
Expand All @@ -19,85 +19,87 @@ export class FastVertexArray {
freeSlots: number[];
lastSlot: number;

constructor (length, stride) {
this.length = length
this.count = 0
this.stride = stride
this.sprites = []
this.data = new Float32Array(this.length * this.stride)
this.freeSlots = []
this.lastSlot = 0
constructor(length, stride) {
this.length = length;
this.count = 0;
this.stride = stride;
this.sprites = [];
this.data = new Float32Array(this.length * this.stride);
this.freeSlots = [];
this.lastSlot = 0;
}

insert (sprite: TxSprite): number {
this.count++
insert(sprite: TxSprite): number {
this.count++;

let position
let position;
if (this.freeSlots.length) {
position = this.freeSlots.shift()
position = this.freeSlots.shift();
} else {
position = this.lastSlot
this.lastSlot++
position = this.lastSlot;
this.lastSlot++;
if (this.lastSlot > this.length) {
this.expand()
this.expand();
}
}
this.sprites[position] = sprite
return position
this.sprites[position] = sprite;
return position;
}

remove (index: number): void {
this.count--
this.clearData(index)
this.freeSlots.push(index)
this.sprites[index] = null
if (this.length > 2048 && this.count < (this.length * 0.4)) this.compact()
remove(index: number): void {
this.count--;
this.clearData(index);
this.freeSlots.push(index);
this.sprites[index] = null;
if (this.length > 2048 && this.count < (this.length * 0.4)) {
this.compact();
}
}

setData (index: number, dataChunk: number[]): void {
this.data.set(dataChunk, (index * this.stride))
setData(index: number, dataChunk: number[]): void {
this.data.set(dataChunk, (index * this.stride));
}

clearData (index: number): void {
this.data.fill(0, (index * this.stride), ((index+1) * this.stride))
clearData(index: number): void {
this.data.fill(0, (index * this.stride), ((index + 1) * this.stride));
}

getData (index: number): Float32Array {
return this.data.subarray(index, this.stride)
getData(index: number): Float32Array {
return this.data.subarray(index, this.stride);
}

expand (): void {
this.length *= 2
const newData = new Float32Array(this.length * this.stride)
newData.set(this.data)
this.data = newData
expand(): void {
this.length *= 2;
const newData = new Float32Array(this.length * this.stride);
newData.set(this.data);
this.data = newData;
}

compact (): void {
compact(): void {
// New array length is the smallest power of 2 larger than the sprite count (but no smaller than 512)
const newLength = Math.max(512, Math.pow(2, Math.ceil(Math.log2(this.count))))
if (newLength != this.length) {
this.length = newLength
this.data = new Float32Array(this.length * this.stride)
let sprite
const newSprites = []
let i = 0
for (var index in this.sprites) {
sprite = this.sprites[index]
const newLength = Math.max(512, Math.pow(2, Math.ceil(Math.log2(this.count))));
if (newLength !== this.length) {
this.length = newLength;
this.data = new Float32Array(this.length * this.stride);
let sprite;
const newSprites = [];
let i = 0;
for (const index in this.sprites) {
sprite = this.sprites[index];
if (sprite) {
newSprites.push(sprite)
sprite.moveVertexPointer(i)
sprite.compile()
i++
newSprites.push(sprite);
sprite.moveVertexPointer(i);
sprite.compile();
i++;
}
}
this.sprites = newSprites
this.freeSlots = []
this.lastSlot = i
this.sprites = newSprites;
this.freeSlots = [];
this.lastSlot = i;
}
}

getVertexData (): Float32Array {
return this.data
getVertexData(): Float32Array {
return this.data;
}
}
Loading
0