Skip to content

Commit

Permalink
fix: conserve value in spam txs
Browse files Browse the repository at this point in the history
  • Loading branch information
yHSJ committed Dec 3, 2024
1 parent 2ded093 commit 45dc0a7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 56 deletions.
84 changes: 42 additions & 42 deletions src/utils/HydraMultiplayer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,48 +83,48 @@ export abstract class HydraMultiplayer {
}

public observeTx(txId: TxHash, tx: any): void {
try {
const body = tx[0];
const outputs = body["1"];
const output = outputs[0];
const datumRaw: Uint8Array | undefined = output?.["2"]?.[1]?.value;
if (!datumRaw) {
return;
}
const packets = decodePackets(datumRaw);
if (!packets) {
// We failed to decode packets, so this might be a new game or join game tx
const game = decodeGame(datumRaw);
if (game.players.length == 1) {
this.gameId = txId;
this.players = game.players;
this.onNewGame?.(
txId,
Number(game.playerCount),
Number(game.botCount),
game.players[0],
);
} else {
if (this.players?.toString() !== game.players.toString()) {
this.players = game.players;
this.onPlayerJoin?.(this.gameId!, game.players);
}
}
return;
}
for (const packet of packets) {
this.onPacket?.(tx, packet);
if (packet.to == this.myIP) {
const buf = this.module._malloc!(packet.data.length);
this.module.HEAPU8!.set(packet.data, buf);
this.module._ReceivePacket!(packet.from, buf, packet.data.length);
this.module._free!(buf);
this.onTxSeen?.(txId, tx);
}
}
} catch (err) {
console.warn(err);
}
// try {
// const body = tx[0];
// const outputs = body["1"];
// const output = outputs[0];
// const datumRaw: Uint8Array | undefined = output?.["2"]?.[1]?.value;
// if (!datumRaw) {
// return;
// }
// const packets = decodePackets(datumRaw);
// if (!packets) {
// // We failed to decode packets, so this might be a new game or join game tx
// const game = decodeGame(datumRaw);
// if (game.players.length == 1) {
// this.gameId = txId;
// this.players = game.players;
// this.onNewGame?.(
// txId,
// Number(game.playerCount),
// Number(game.botCount),
// game.players[0],
// );
// } else {
// if (this.players?.toString() !== game.players.toString()) {
// this.players = game.players;
// this.onPlayerJoin?.(this.gameId!, game.players);
// }
// }
// return;
// }
// for (const packet of packets) {
// this.onPacket?.(tx, packet);
// if (packet.to == this.myIP) {
// const buf = this.module._malloc!(packet.data.length);
// this.module.HEAPU8!.set(packet.data, buf);
// this.module._ReceivePacket!(packet.from, buf, packet.data.length);
// this.module._free!(buf);
// this.onTxSeen?.(txId, tx);
// }
// }
// } catch (err) {
// console.warn(err);
// }
}

protected signData(data: string): string {
Expand Down
14 changes: 0 additions & 14 deletions src/utils/hydra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,6 @@ export class Hydra {
break;
case "TxValid":
{
if (this.startTime === 0) {
this.startTime = performance.now();
}
this.tx_count += 1;
const endTime = performance.now();
if (endTime > this.startTime + 1000) {
console.log(
`Current Data: ${this.tx_count} Transactions | ${this.tx_count / ((endTime - this.startTime) / 1000)} TPS`,
);

this.startTime = performance.now();
this.tx_count = 0;
}
// // Record seen time
// if (this.tx_timings[txid]?.sent) {
// const seenTime = now - this.tx_timings[txid].sent;
Expand Down Expand Up @@ -235,7 +222,6 @@ export class Hydra {
const txId = txParsed.body().hash();
this.tx_timings[txId] = { sent: performance.now() };
this.tx_count++;

this.connection.send(
JSON.stringify({
tag: "NewTx",
Expand Down

0 comments on commit 45dc0a7

Please sign in to comment.