Skip to content

Commit 9aa7706

Browse files
committed
feat: improve tx logging
1 parent a3e599c commit 9aa7706

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

src/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const token = client.runtime.resolve("Token");
119119

120120
let privateKey: PrivateKey;
121121
let publicKey: PublicKey;
122-
let txer: (txfn: () => Promise<void>) => Promise<CommandResponse>;
122+
let txQueue: TxHandler;
123123

124124
if (!opts.help) {
125125
program.parse();
@@ -131,8 +131,7 @@ if (!opts.help) {
131131

132132
if (opts.debug) console.log("opts", opts);
133133

134-
const txQueue = new TxHandler(client, publicKey, privateKey, opts);
135-
txer = txQueue.submitTx.bind(txQueue);
134+
txQueue = new TxHandler(client, publicKey, privateKey, opts);
136135
}
137136

138137
////////////////////////////////////////////////////////////////////////
@@ -146,6 +145,9 @@ const executeCommand = async (
146145
) => {
147146
const { command, payload, id } = request;
148147

148+
// transaction submission helper
149+
const txer = async (fn: () => Promise<void>) => txQueue.submitTx(fn, request);
150+
149151
// common responses
150152
const responses: Record<string, CommandResponse> = {
151153
IPFS_NOT_STARTED: { id, status: FAILURE, error: "IPFS node not started" },
@@ -714,8 +716,12 @@ const server = net.createServer((socket) => {
714716
});
715717

716718
const req = decoded.value as CommandRequest;
719+
console.log(`\n❯❯❯ [${req.id}] ${req.command}`);
717720
await executeCommand(new Command(), req, (res, debug) => {
718-
console.log(`\n❯ ${req.command} => ${JSON.stringify(res, rep)}`);
721+
console.log(
722+
`❮❮❮ [${req.id}] ${req.command}`,
723+
`❮❮❮ ${JSON.stringify(res, rep)}`,
724+
);
719725
if (opts.debug && debug) console.log("DEBUG:", debug);
720726
const out = cbor.encode(res);
721727
socket.write(out);

src/tx.ts

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { PrivateKey, PublicKey } from "o1js";
2-
import { CommandResponse, FAILURE, PENDING } from "./types";
2+
import { CommandRequest, CommandResponse, FAILURE, PENDING } from "./types";
33
import { qry } from "chain";
44

55
interface TxTask {
66
txfn: () => Promise<void>;
7+
cmdReq: CommandRequest;
8+
timeSubmit: number;
79
resolve: (value: CommandResponse) => void;
810
reject: (reason?: any) => void;
911
}
@@ -39,11 +41,21 @@ export class TxHandler {
3941
) {}
4042

4143
// helper function to send transactions
42-
public async submitTx(txfn: () => Promise<void>): Promise<CommandResponse> {
44+
public async submitTx(
45+
txfn: () => Promise<void>,
46+
cmdReq: CommandRequest,
47+
): Promise<CommandResponse> {
48+
const id = cmdReq.id ?? "?";
4349
return new Promise((resolve, reject) => {
44-
this.txQueue.push({ txfn, resolve, reject });
50+
const timeSubmit = Date.now();
51+
this.txQueue.push({ txfn, cmdReq, timeSubmit, resolve, reject });
52+
console.log(
53+
`--- [${id}] Submission`,
54+
`queue=${this.txQueue.length}`,
55+
`$ ${cmdReq.command}`,
56+
);
4557
this.processQueue().catch((err) => {
46-
console.error("Error processing transaction queue:", err);
58+
console.error(`--- [${id}] ❌ Error processing tx queue:`, err);
4759
while (this.txQueue.length > 0) {
4860
const task = this.txQueue.shift();
4961
task?.reject(err);
@@ -52,10 +64,15 @@ export class TxHandler {
5264
});
5365
}
5466

55-
private async processTx(txfn: () => Promise<void>): Promise<CommandResponse> {
67+
private async processTx(
68+
txfn: () => Promise<void>,
69+
cmdReq: CommandRequest,
70+
timeSubmit: number,
71+
): Promise<CommandResponse> {
72+
const timeProcess = Date.now();
73+
const id = cmdReq.id ?? "?";
5674
const nonce = this.nonce;
5775
const tx = await this.client.transaction(this.publicKey, txfn, { nonce });
58-
console.log("tx.nonce", tx.transaction!.nonce.toString());
5976
tx.transaction = tx.transaction?.sign(this.privateKey);
6077
await tx.send();
6178

@@ -65,14 +82,37 @@ export class TxHandler {
6582
// but enables submission of many txns without waiting for their confirmation.
6683
if (this.opts.nonce) this.nonce = Number(tx.transaction.nonce) + 1;
6784

68-
const { status, statusMessage } = await qry.indexer.getTxStatus(
85+
const h7 = (tx.transaction.hash().toString() as string).substring(0, 7);
86+
87+
const { status, statusMessage } = await qry.processor.getTxStatus(
6988
tx.transaction.hash().toString(),
7089
() => {
71-
console.log("⏳ Waiting for tx status...");
90+
console.log(
91+
`--- [${id}] ⏳ Awaiting status for`,
92+
`tx=${h7}...`,
93+
`n=${tx.transaction.nonce}`,
94+
`q=${this.txQueue.length}`,
95+
`$ ${cmdReq.command}`,
96+
);
7297
},
7398
parseInt(this.opts.txStatusInterval),
7499
parseInt(this.opts.txStatusRetries),
75100
);
101+
102+
console.log(
103+
`--- [${id}] ${status === FAILURE ? "❌" : "✅"} Returning status for`,
104+
`tx=${h7}...`,
105+
`n=${tx.transaction.nonce}`,
106+
`q=${this.txQueue.length}`,
107+
`t=${timeProcess - timeSubmit}` +
108+
"/" +
109+
`${Date.now() - timeProcess}` +
110+
"/" +
111+
`${Date.now() - timeSubmit}` +
112+
"ms",
113+
`$ ${cmdReq.command}`,
114+
);
115+
76116
return {
77117
status,
78118
data: status !== FAILURE ? statusMessage : undefined,
@@ -81,6 +121,7 @@ export class TxHandler {
81121
};
82122
}
83123

124+
console.error(`--- [${id}] ❌ !tx.transaction`);
84125
return { status: PENDING };
85126
}
86127

@@ -89,9 +130,9 @@ export class TxHandler {
89130

90131
this.isProcessing = true;
91132
while (this.txQueue.length > 0) {
92-
const { txfn, resolve, reject } = this.txQueue[0];
133+
const { txfn, cmdReq, timeSubmit, resolve, reject } = this.txQueue[0];
93134
try {
94-
const result = await this.processTx(txfn);
135+
const result = await this.processTx(txfn, cmdReq, timeSubmit);
95136
resolve(result);
96137
} catch (err) {
97138
reject(err);

0 commit comments

Comments
 (0)