Skip to content

Commit

Permalink
Fixed issue with transaction subscription (#2189)
Browse files Browse the repository at this point in the history
* fix: transaction subscription mempool mapper and transaction result resolver

* chore: changeset file

* style: comment

* fix: remove console.error - this will log when there are no results

* feat: return undefined transaction when result not found

* feat: added error handling for mempool responses

* fix: add try catch on request end event

* feat: add blockHash validations before searching for signers

* fix: remove unecessary code

* fix: linting
  • Loading branch information
nil-amrutlal-dept committed May 27, 2024
1 parent 7882930 commit 5a9d2de
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-cherries-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kadena/graph": patch
---

Fixed transaction subscription issue with mempool and completed transactions
6 changes: 4 additions & 2 deletions packages/apps/graph/src/graph/mappers/transaction-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function mempoolTransactionMapper(mempoolData: any): Transaction {
// Convert creationTime to milliseconds (mempool has it in epoch format in seconds)
mempoolTx.cmd.meta.creationTime = mempoolTx.cmd.meta.creationTime * 1000;

const transaction = {
const transaction: Transaction = {
requestKey: mempoolTx.hash,
badResult: null,
continuation: null,
Expand All @@ -51,7 +51,9 @@ export function mempoolTransactionMapper(mempoolData: any): Transaction {
rollback: mempoolTx.cmd.payload.rollback,
proof: mempoolTx.cmd.payload.proof,
senderAccount: mempoolTx.cmd.meta.sender,
step: BigInt(mempoolTx.cmd.payload.step),
step: mempoolTx.cmd.payload.step
? BigInt(mempoolTx.cmd.payload.step)
: mempoolTx.cmd.payload.step,
ttl: BigInt(mempoolTx.cmd.meta.ttl),
};

Expand Down
54 changes: 32 additions & 22 deletions packages/apps/graph/src/graph/objects/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Signer } from '@prisma/client';
import { Prisma } from '@prisma/client';
import { getMempoolTransactionStatus } from '@services/chainweb-node/mempool';
import { normalizeError } from '@utils/errors';
Expand All @@ -22,27 +23,33 @@ export default builder.prismaNode(Prisma.ModelName.Transaction, {
sigs: t.field({
type: [TransactionSigs],
async resolve(parent) {
const signers = await signersLoader.load({
blockHash: parent.blockHash,
requestKey: parent.requestKey,
chainId: parent.chainId.toString(),
});
if (!nullishOrEmpty(parent.blockHash)) {
const signers = await signersLoader.load({
blockHash: parent.blockHash,
requestKey: parent.requestKey,
chainId: parent.chainId.toString(),
});

return signers.map((signer) => ({
sig: signer.signature,
}));
return signers.map((signer) => ({
sig: signer.signature,
}));
}
return [];
},
}),
cmd: t.field({
type: TransactionCommand,

async resolve(parent, __args, context) {
async resolve(parent, __args) {
try {
const signers = await signersLoader.load({
blockHash: parent.blockHash,
requestKey: parent.requestKey,
chainId: parent.chainId.toString(),
});
let signers: Signer[] = [];
if (!nullishOrEmpty(parent.blockHash)) {
signers = await signersLoader.load({
blockHash: parent.blockHash,
requestKey: parent.requestKey,
chainId: parent.chainId.toString(),
});
}

return {
nonce: parent.nonce,
Expand Down Expand Up @@ -74,15 +81,18 @@ export default builder.prismaNode(Prisma.ModelName.Transaction, {
type: TransactonInfo,
resolve: async (parent) => {
try {
const status = await getMempoolTransactionStatus(
parent.requestKey,
parent.chainId.toString(),
);
//This check is important because a transaction can be completed, yet we can still be able to fetch a status from the mempool
if (nullishOrEmpty(parent.blockHash)) {
const status = await getMempoolTransactionStatus(
parent.requestKey,
parent.chainId.toString(),
);

if (!nullishOrEmpty(status) && status) {
return {
status,
};
if (!nullishOrEmpty(status) && status) {
return {
status,
};
}
}

return {
Expand Down
2 changes: 2 additions & 0 deletions packages/apps/graph/src/graph/subscription/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ async function* iteratorFn(

if (mempoolResponse) {
yield mempoolResponse;
} else {
yield undefined;
}
}

Expand Down
12 changes: 10 additions & 2 deletions packages/apps/graph/src/services/chainweb-node/mempool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,19 @@ export async function mempoolLookup(
});

res.on('end', () => {
resolve(JSON.parse(data));
try {
resolve(JSON.parse(data));
} catch (error) {
reject(
new MempoolError(
'Error occurred while processing response data.',
error,
),
);
}
});
},
);

req.on('error', (error) => {
reject(
new MempoolError(
Expand Down

0 comments on commit 5a9d2de

Please sign in to comment.