Skip to content

Commit

Permalink
Merge pull request #157 from MichaelBuhler/add-tx-pending-route
Browse files Browse the repository at this point in the history
implement the `/tx/pending` route
  • Loading branch information
cedriking authored Mar 21, 2024
2 parents c96c49b + 9cb99b5 commit 1e2637c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions __tests__/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ describe('', () => {
expect(res.text).toEqual('Pending');
});

it('returns the list of pending tx ids', async () => {
const txId1 = await createTransaction(blockweave, 'test1');
const txId2 = await createTransaction(blockweave, 'test2');

const res = await request(server).get(`/tx/pending`);

expect(res.body.sort()).toEqual([ txId1, txId2 ].sort());
});

it('returns the height of the block', async () => {
const txid = await createTransaction(blockweave, 'test');
await mine(blockweave);
Expand Down
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
txRawDataRoute,
deleteTxRoute,
txDataRoute,
txPendingRoute,
} from './routes/transaction';
import { txAccessMiddleware, txValidateMiddleware } from './middlewares/transaction';
import { Utils } from './utils/utils';
Expand Down Expand Up @@ -134,6 +135,8 @@ export default class ArLocal {
async (ctx) => (ctx.body = Math.round((+ctx.params.bytes / 1000) * 65595508).toString()),
);

this.router.get('/tx/pending', txPendingRoute);

// tx filter endpoint to restrict ans-104 txs
this.router.get(/^\/tx(?:\/|$)/, txAccessMiddleware);

Expand Down
23 changes: 23 additions & 0 deletions src/routes/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,29 @@ export async function txDataRoute(ctx: Router.RouterContext, next: Next) {
}
}

export async function txPendingRoute(ctx: Router.RouterContext) {
try {
if (
oldDbPath !== ctx.dbPath ||
!transactionDB ||
connectionSettings !== ctx.connection.client.connectionSettings.filename
) {
transactionDB = new TransactionDB(ctx.connection);
oldDbPath = ctx.dbPath;
connectionSettings = ctx.connection.client.connectionSettings.filename;
}

const txIds = await transactionDB.getUnminedTxs();

ctx.status = 200
ctx.body = txIds
} catch (error) {
console.error({ error })
ctx.status = 500;
ctx.body = { error: error.message };
}
}

export async function deleteTxRoute(ctx: Router.RouterContext) {
try {
if (
Expand Down

0 comments on commit 1e2637c

Please sign in to comment.