Skip to content

Commit

Permalink
adding sort order and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Oct 23, 2024
1 parent 030c60f commit 01f0a70
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
24 changes: 23 additions & 1 deletion src/services/actions-service/actions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class ActionsService implements IActionsService {
}
actions.push({
blockInfo,
actionData: actionsData.flat(),
actionData: this.sortActions(actionsData.flat()),
actionState: {
/* eslint-disable */
actionStateOne: action_state_value1!,
Expand All @@ -157,4 +157,26 @@ class ActionsService implements IActionsService {
}
return actions;
}

sortActions(actions: Action[]): Action[] {
return actions.sort((a, b) => {
// Sort by sequence number
if (
a.transactionInfo.sequenceNumber !== b.transactionInfo.sequenceNumber
) {
return (
a.transactionInfo.sequenceNumber - b.transactionInfo.sequenceNumber
);
}

// Sort by account update index if sequence number is the same
const aIndex = a.transactionInfo.zkappAccountUpdateIds.indexOf(
Number(a.accountUpdateId)
);
const bIndex = b.transactionInfo.zkappAccountUpdateIds.indexOf(
Number(b.accountUpdateId)
);
return aIndex - bIndex;
});
}
}
5 changes: 0 additions & 5 deletions tests/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ query getActions($input: ActionFilterOptionsInput!) {
}
`;

console.log('Running tests');

// This is the default connection string provided by the lightnet postgres container
const PG_CONN = 'postgresql://postgres:postgres@localhost:5432/archive ';

Expand All @@ -90,10 +88,7 @@ describe('Query Resolvers', async () => {
let zkAppKeypair: Keypair;
let zkApp: HelloWorld;

console.log('Describe ', 'Query Resolvers');

before(async () => {
console.log('Before');
try {
setNetworkConfig();

Expand Down
86 changes: 86 additions & 0 deletions tests/services/actions-service/actions-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { test, before, describe, after } from 'node:test';
import assert from 'node:assert';
import { ActionsService } from '../../../src/services/actions-service/actions-service.js';
import { Sql } from 'postgres';
import { Action } from '../../../src/blockchain/types.js';

describe('ActionsService', () => {
let actionsService: ActionsService;

before(() => {
const client = {
query: () => {},
CLOSE: () => {},
END: () => {},
PostgresError: class {},
options: {},
} as unknown as Sql<{}>;
actionsService = new ActionsService(client);
});

describe('sortActions', () => {
let actions: Action[];
describe('with actions with different sequence numbers', () => {
before(() => {
actions = [
dummyAction({ sequenceNumber: 2 }),
dummyAction({ sequenceNumber: 1 }),
];
});
test('it sorts actions by their sequence number', () => {
const sortedActions = actionsService.sortActions(actions);
assert.strictEqual(sortedActions[0].transactionInfo.sequenceNumber, 1);
assert.strictEqual(sortedActions[1].transactionInfo.sequenceNumber, 2);
});
});
describe('with actions with the same sequence number', () => {
const sequenceNumber = 1;
describe('with actions with different account update ids', () => {
const zkappAccountUpdateIds = [1, 2];
before(() => {
actions = [
dummyAction({
sequenceNumber,
zkappAccountUpdateIds,
accountUpdateId: '2',
}),
dummyAction({
sequenceNumber,
zkappAccountUpdateIds,
accountUpdateId: '1',
}),
];
});
test('it sorts actions by their account update index', () => {
const sortedActions = actionsService.sortActions(actions);
assert.strictEqual(sortedActions[0].accountUpdateId, '1');
assert.strictEqual(sortedActions[1].accountUpdateId, '2');
});
});
});
});
});

function dummyAction({
sequenceNumber = 1,
accountUpdateId = '1',
zkappAccountUpdateIds = [1],
}: {
sequenceNumber?: number;
accountUpdateId?: string;
zkappAccountUpdateIds?: number[];
}): Action {
return {
accountUpdateId: accountUpdateId,
data: ['dummy'],
transactionInfo: {
sequenceNumber,
zkappAccountUpdateIds,
zkappEventElementIds: [],
authorizationKind: 'dummy',
hash: 'dummy',
memo: 'dummy',
status: 'dummy',
},
};
}

0 comments on commit 01f0a70

Please sign in to comment.