Skip to content

Commit

Permalink
feat: throw if storing event lower than current
Browse files Browse the repository at this point in the history
  • Loading branch information
andreabadesso committed Oct 2, 2023
1 parent 70f082d commit 59a7bc4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { isAuthority } from '../utils';
import { AddressBalanceRow, AddressTxHistorySumRow, BestBlockRow, LastSyncedEventRow, MinerRow, TokenInformationRow, TransactionRow, TxOutputRow } from './types';
// @ts-ignore
import { walletUtils } from '@hathor/wallet-lib';
import logger from '../logger';

let pool: Pool;

Expand Down Expand Up @@ -1332,6 +1333,8 @@ export const updateLastSyncedEvent = async (
ON DUPLICATE KEY
UPDATE last_event_id = ?`,
[lastEventId, lastEventId]);

logger.info('Updated last synced event', lastEventId);
};

export const getLastSyncedEvent = async ( // OK
Expand Down
12 changes: 12 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Wallet,
DbTxOutput,
DbTransaction,
LastSyncedEvent,
} from '../types';
import {
prepareOutputs,
Expand Down Expand Up @@ -384,8 +385,19 @@ export const handleTxFirstBlock = async (context: Context) => {

export const updateLastSyncedEvent = async (context: Context) => {
const mysql = await getDbConnection();

const lastDbSyncedEvent: LastSyncedEvent | null = await getLastSyncedEvent(mysql);
// @ts-ignore
const lastEventId = context.event.event.id;

if (lastDbSyncedEvent
&& lastDbSyncedEvent.last_event_id > lastEventId) {
logger.error('Tried to store an event lower than the one on the database', {
lastEventId,
lastDbSyncedEvent,
});
throw new Error('Event lower than stored one.');
}
await dbUpdateLastSyncedEvent(mysql, lastEventId);

mysql.destroy();
Expand Down

0 comments on commit 59a7bc4

Please sign in to comment.