Skip to content

Commit

Permalink
Merge branch '425-init-sync-process' into 'dev'
Browse files Browse the repository at this point in the history
fix event synchronization starting point

Closes #425

See merge request ergo/rosen-bridge/guard-service!443
  • Loading branch information
vorujack committed Feb 3, 2025
2 parents a60226e + 4e51184 commit 853c1f3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .changeset/little-cherries-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
11 changes: 10 additions & 1 deletion src/verification/EventVerifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ChainMinimumFee } from '@rosen-bridge/minimum-fee';
import EventBoxes from '../event/EventBoxes';
import { ConfirmedEventEntity } from '../db/entities/ConfirmedEventEntity';
import { EventStatus } from '../utils/constants';
import EventSynchronization from '../synchronization/EventSynchronization';

class EventVerifier {
/**
Expand Down Expand Up @@ -76,7 +77,15 @@ class EventVerifier {
type === TransactionType.reward
)
return true;
else return false;
else {
if (
eventEntity.status === EventStatus.pendingPayment &&
type === TransactionType.reward
) {
EventSynchronization.getInstance().addEventToQueue(eventEntity.id);
}
return false;
}
};
}

Expand Down
46 changes: 46 additions & 0 deletions tests/synchronization/mocked/EventSynchronization.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Mock } from 'vitest';
import EventSynchronization from '../../../src/synchronization/EventSynchronization';

class EventSynchronizationMock {
private static mockedEventSynchronization: Record<string, any>;

/**
* resets all mocked functions of EventSynchronization
*/
static resetMock = () => {
this.mockedEventSynchronization = {};
};

/**
* mocks EventSynchronization.getInstance to return mocked object
*/
static mock = () => {
const functionSpy = vi.spyOn(EventSynchronization, 'getInstance');
functionSpy.mockReturnValue(
this.mockedEventSynchronization as EventSynchronization
);
};

/**
* mocks EventSynchronization.addEventToQueue
*/
static mockAddEventToQueue = () => {
this.mockedEventSynchronization.addEventToQueue = vi.fn();
const functionSpy = vi.spyOn(
this.mockedEventSynchronization,
'addEventToQueue'
);
functionSpy.mockImplementation(() => null);
};

/**
* returns vitest mock object of the corresponding function
* @param name function name
* @returns the mock object
*/
static getMockedFunction = (name: string): Mock<any> => {
return this.mockedEventSynchronization[name];
};
}

export default EventSynchronizationMock;
45 changes: 45 additions & 0 deletions tests/verification/EventVerifier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import DatabaseActionMock from '../db/mocked/DatabaseAction.mock';
import { ChainMinimumFee } from '@rosen-bridge/minimum-fee';
import { ConfirmedEventEntity } from '../../src/db/entities/ConfirmedEventEntity';
import { EventStatus } from '../../src/utils/constants';
import EventSynchronizationMock from '../synchronization/mocked/EventSynchronization.mock';
import TestUtils from '../testUtils/TestUtils';

describe('EventVerifier', () => {
describe('isEventConfirmedEnough', () => {
Expand Down Expand Up @@ -265,6 +267,11 @@ describe('EventVerifier', () => {
});

describe('isEventPendingToType', () => {
beforeEach(async () => {
EventSynchronizationMock.resetMock();
EventSynchronizationMock.mock();
});

/**
* @target EventVerifier.isEventPendingToType should return true when
* type is payment and event status is pending-payment
Expand Down Expand Up @@ -331,8 +338,41 @@ describe('EventVerifier', () => {
it('should return false when type and event status are not compatible', async () => {
// mock an event
const mockedEvent = new ConfirmedEventEntity();
mockedEvent.status = EventStatus.timeout;

// run test
const result = EventVerifier.isEventPendingToType(
mockedEvent,
TransactionType.reward
);

// verify returned value
expect(result).toEqual(false);
});

/**
* @target EventVerifier.isEventPendingToType should return false and add
* event to synchronization queue when a reward transaction for a pending-payment
* event is received
* @dependencies
* @scenario
* - mock an event with `pending-payment` status
* - mock EventSynchronization.addEventToQueue
* - run test with `reward` transaction type
* - verify returned value
* @expected
* - returned value should be false
* - `addEventToQueue` should got called with the mocked event id
*/
it('should return false and add event to synchronization queue when a reward transaction for a pending-payment event is received', async () => {
// mock an event
const mockedEvent = new ConfirmedEventEntity();
mockedEvent.id = TestUtils.generateRandomId();
mockedEvent.status = EventStatus.pendingPayment;

// mock EventSynchronization.addEventToQueue
EventSynchronizationMock.mockAddEventToQueue();

// run test
const result = EventVerifier.isEventPendingToType(
mockedEvent,
Expand All @@ -341,6 +381,11 @@ describe('EventVerifier', () => {

// verify returned value
expect(result).toEqual(false);

// `addEventToQueue` should got called
expect(
EventSynchronizationMock.getMockedFunction('addEventToQueue')
).toHaveBeenCalledWith(mockedEvent.id);
});
});
});

0 comments on commit 853c1f3

Please sign in to comment.