Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StoyanD committed Jan 24, 2025
1 parent 205fb47 commit f60cf01
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 166 deletions.
3 changes: 3 additions & 0 deletions src/chain-monitor/chain-sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export class ChainSyncService implements OnApplicationBootstrap {
address: source.sourceAddress,
abi: IntentSourceAbi,
eventName: 'IntentCreated',
args: {
prover: source.provers,
},
fromBlock,
toBlock,
})
Expand Down
56 changes: 32 additions & 24 deletions src/chain-monitor/tests/chain-sync.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ describe('ChainSyncService', () => {
describe('on syncTxsPerSource', () => {
let mockGetContractEvents: jest.Mock

const IntentSource = {
const intentSource = {
chainID: 123,
sourceAddress: '0x123',
network: 'network1',
provers: ['0x456', '0x789', '0xabc'],
} as unknown as IntentSource

const solvers = {
Expand All @@ -94,10 +95,8 @@ describe('ChainSyncService', () => {
},
} as any as Solver[]

const solverSupportedChains = entries(solvers).map(([chainID]) => BigInt(chainID))

const model = { event: { blockNumber: 50n, sourceChainID: IntentSource.chainID } }

const model = { event: { blockNumber: 50n, sourceChainID: intentSource.chainID } }
const supportedChains = Object.keys(solvers).map((key) => BigInt(key))
beforeEach(() => {
mockGetContractEvents = jest.fn().mockResolvedValue([])

Expand All @@ -109,14 +108,14 @@ describe('ChainSyncService', () => {
})

it('should set fromBlock to 0x0 when no transactions in db', async () => {
await chainSyncService.syncTxsPerSource(IntentSource)
await chainSyncService.syncTxsPerSource(intentSource)
expect(mockGetContractEvents).toHaveBeenCalledTimes(1)
expect(mockGetContractEvents).toHaveBeenCalledWith({
address: IntentSource.sourceAddress,
address: intentSource.sourceAddress,
abi: IntentSourceAbi,
eventName: 'IntentCreated',
args: {
_destinationChain: solverSupportedChains,
prover: intentSource.provers,
},
fromBlock: 0n,
toBlock: 'latest',
Expand All @@ -126,14 +125,14 @@ describe('ChainSyncService', () => {
it('should set fromBlock to the block of the db transaction', async () => {
chainSyncService['getLastRecordedTx'] = jest.fn().mockResolvedValueOnce([model])

await chainSyncService.syncTxsPerSource(IntentSource)
await chainSyncService.syncTxsPerSource(intentSource)
expect(mockGetContractEvents).toHaveBeenCalledTimes(1)
expect(mockGetContractEvents).toHaveBeenCalledWith({
address: IntentSource.sourceAddress,
address: intentSource.sourceAddress,
abi: IntentSourceAbi,
eventName: 'IntentCreated',
args: {
_destinationChain: solverSupportedChains,
prover: intentSource.provers,
},
fromBlock: model.event.blockNumber + 1n, // we search from the next block
toBlock: 'latest',
Expand All @@ -144,39 +143,48 @@ describe('ChainSyncService', () => {
chainSyncService['getLastRecordedTx'] = jest.fn().mockResolvedValueOnce([model])
const mockLog = jest.fn()
chainSyncService['logger'].log = mockLog
await chainSyncService.syncTxsPerSource(IntentSource)
await chainSyncService.syncTxsPerSource(intentSource)
expect(mockGetContractEvents).toHaveBeenCalledTimes(1)
expect(mockLog).toHaveBeenCalledTimes(1)
// we search from the next block
const searchFromBlock = model.event.blockNumber + 1n
expect(mockLog).toHaveBeenCalledWith({
msg: `No transactions found for source ${IntentSource.network} to sync from block ${searchFromBlock}`,
msg: `No transactions found for source ${intentSource.network} to sync from block ${searchFromBlock}`,
chainID: model.event.sourceChainID,
fromBlock: searchFromBlock,
})
})

it('should process all the txs since the last saved blockNumber', async () => {
it('should process all the txs that are to a supported destination since the last saved blockNumber', async () => {
const unsupportedChain = 1000n
chainSyncService['getLastRecordedTx'] = jest.fn().mockResolvedValueOnce([model])
const logs = [{ msg: 'firstlog' }, { msg: 'secondlog' }, { msg: 'thirdlog' }]
const returnLogs = logs.map((log) => {
return {
...log,
sourceNetwork: IntentSource.network,
sourceChainID: IntentSource.chainID,
}
})
ecoConfigService.getSupportedChains.mockReturnValue(supportedChains)
const logs = [
{ msg: 'firstlog', args: { destination: supportedChains[0] } },
{ msg: 'secondlog', args: { destination: supportedChains[1] } },
{ msg: 'thirdlog', args: { destination: unsupportedChain } },
]
const returnLogs = logs
.filter((log) => supportedChains.includes(log.args.destination))
.map((log) => {
return {
...log,
sourceNetwork: intentSource.network,
sourceChainID: intentSource.chainID,
}
})
const mockProcessJob = jest.fn()
const mockAddJob = jest.fn(() => mockProcessJob)
watchIntentService.addJob = mockAddJob as any
mockGetContractEvents.mockResolvedValueOnce(logs)
ecoConfigService.getIntentSources.mockReturnValue([IntentSource])
ecoConfigService.getIntentSources.mockReturnValue([intentSource])

await chainSyncService.syncTxs()
expect(mockAddJob).toHaveBeenCalledTimes(1)
expect(mockProcessJob).toHaveBeenCalledTimes(1)
expect(mockAddJob).toHaveBeenCalledWith(IntentSource)
expect(mockAddJob).toHaveBeenCalledWith(intentSource)
expect(mockProcessJob).toHaveBeenCalledWith(returnLogs)
expect(returnLogs).toHaveLength(2)
})
})
})
12 changes: 3 additions & 9 deletions src/intent/create-intent.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ export class CreateIntentService implements OnModuleInit {
)
: true

// check if the destination chain is supported
const validDestination = this.ecoConfigService
.getSupportedChains()
.includes(intent.route.destination)

//create db record
const record = await this.intentModel.create({
event: intentWs,
Expand All @@ -97,23 +92,22 @@ export class CreateIntentService implements OnModuleInit {
})

const jobId = getIntentJobId('create', intent.hash as Hex, intent.logIndex)
if (validWallet && validDestination) {
if (validWallet) {
//add to processing queue
await this.intentQueue.add(QUEUES.SOURCE_INTENT.jobs.validate_intent, intent.hash, {
jobId,
...this.intentJobConfig,
})
}

this.logger.debug(
this.logger.log(
EcoLogMessage.fromDefault({
message: `Recorded intent ${record.intent.hash}`,
properties: {
intentHash: intent.hash,
intent: record.intent,
validWallet,
validDestination,
...(validWallet && validDestination ? { jobId } : {}),
...(validWallet ? { jobId } : {}),
},
}),
)
Expand Down
13 changes: 9 additions & 4 deletions src/intent/tests/create-intent.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ describe('CreateIntentService', () => {

describe('on createIntent', () => {
const mockEvent = {
creator: '0xaaa',
data: '0xda',
transactionHash: '0x123',
topics: ['0x456'],
sourceChainID: 85432,
}
const mockIntent = { creator: '0xaaa', hash: mockEvent.transactionHash, logIndex: 1 }
const mockIntent = {
reward: { creator: '0xaaa' },
hash: mockEvent.transactionHash,
logIndex: 1,
}
beforeEach(() => {
mockDecodeCreateIntentLog.mockReturnValue({ hash: mockEvent.transactionHash })
const mockIntentSourceEvent = jest.fn()
Expand Down Expand Up @@ -129,7 +134,7 @@ describe('CreateIntentService', () => {
await createIntentService.createIntent(mockEvent as any)
expect(mockValidateSmartWallet).toHaveBeenCalledTimes(1)
expect(mockValidateSmartWallet).toHaveBeenCalledWith(
mockIntent.creator,
mockIntent.reward.creator,
mockEvent.sourceChainID,
)
})
Expand Down Expand Up @@ -172,7 +177,7 @@ describe('CreateIntentService', () => {

await createIntentService.createIntent(mockEvent as any)
expect(mockQueueAdd).not.toHaveBeenCalled()
expect(mockLogDebug).toHaveBeenNthCalledWith(2, {
expect(mockLogLog).toHaveBeenNthCalledWith(1, {
msg: `Recorded intent ${mockEvent.transactionHash}`,
intentHash: mockIntent.hash,
intent: mockIntent,
Expand All @@ -198,7 +203,7 @@ describe('CreateIntentService', () => {
{ jobId },
)

expect(mockLogDebug).toHaveBeenNthCalledWith(2, {
expect(mockLogLog).toHaveBeenNthCalledWith(1, {
msg: `Recorded intent ${mockEvent.transactionHash}`,
intentHash: mockIntent.hash,
intent: mockIntent,
Expand Down
Loading

0 comments on commit f60cf01

Please sign in to comment.