|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +import chai, { expect } from 'chai'; |
| 4 | +import chaiAsPromised from 'chai-as-promised'; |
| 5 | +import { pino } from 'pino'; |
| 6 | +import { createClient, RedisClientType } from 'redis'; |
| 7 | + |
| 8 | +import { RedisPendingTransactionStorage } from '../../../../src/lib/services/transactionPoolService/RedisPendingTransactionStorage'; |
| 9 | +import { useInMemoryRedisServer } from '../../../helpers'; |
| 10 | + |
| 11 | +chai.use(chaiAsPromised); |
| 12 | + |
| 13 | +describe('RedisPendingTransactionStorage Test Suite', function () { |
| 14 | + this.timeout(10000); |
| 15 | + |
| 16 | + const logger = pino({ level: 'silent' }); |
| 17 | + |
| 18 | + let redisClient: RedisClientType; |
| 19 | + let storage: RedisPendingTransactionStorage; |
| 20 | + |
| 21 | + useInMemoryRedisServer(logger, 6390); |
| 22 | + |
| 23 | + before(async () => { |
| 24 | + redisClient = createClient({ url: 'redis://127.0.0.1:6390' }); |
| 25 | + await redisClient.connect(); |
| 26 | + storage = new RedisPendingTransactionStorage(redisClient); |
| 27 | + }); |
| 28 | + |
| 29 | + beforeEach(async () => { |
| 30 | + await redisClient.flushAll(); |
| 31 | + }); |
| 32 | + |
| 33 | + const addr1 = '0x1111111111111111111111111111111111111111'; |
| 34 | + const addr2 = '0x2222222222222222222222222222222222222222'; |
| 35 | + const tx1 = '0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; |
| 36 | + const tx2 = '0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'; |
| 37 | + const tx3 = '0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'; |
| 38 | + |
| 39 | + describe('addToList (Set-based)', () => { |
| 40 | + it('adds first transaction and returns size 1', async () => { |
| 41 | + const res = await storage.addToList(addr1, tx1); |
| 42 | + expect(res.ok).to.equal(true); |
| 43 | + if (res.ok) expect(res.newValue).to.equal(1); |
| 44 | + const count = await storage.getList(addr1); |
| 45 | + expect(count).to.equal(1); |
| 46 | + }); |
| 47 | + |
| 48 | + it('deduplicates the same transaction hash', async () => { |
| 49 | + await storage.addToList(addr1, tx1); |
| 50 | + const res = await storage.addToList(addr1, tx1); |
| 51 | + expect(res.ok).to.equal(true); |
| 52 | + if (res.ok) expect(res.newValue).to.equal(1); |
| 53 | + const count = await storage.getList(addr1); |
| 54 | + expect(count).to.equal(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it('adds multiple distinct tx hashes and returns correct size', async () => { |
| 58 | + await storage.addToList(addr1, tx1); |
| 59 | + const r2 = await storage.addToList(addr1, tx2); |
| 60 | + expect(r2.ok).to.equal(true); |
| 61 | + if (r2.ok) expect(r2.newValue).to.equal(2); |
| 62 | + const count = await storage.getList(addr1); |
| 63 | + expect(count).to.equal(2); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('getList (Set-based)', () => { |
| 68 | + it('returns 0 for empty/non-existent key', async () => { |
| 69 | + const count = await storage.getList(addr2); |
| 70 | + expect(count).to.equal(0); |
| 71 | + }); |
| 72 | + |
| 73 | + it('returns size after multiple adds', async () => { |
| 74 | + await storage.addToList(addr1, tx1); |
| 75 | + await storage.addToList(addr1, tx2); |
| 76 | + const count = await storage.getList(addr1); |
| 77 | + expect(count).to.equal(2); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('removeFromList (Set-based)', () => { |
| 82 | + it('removes existing tx and returns new size', async () => { |
| 83 | + await storage.addToList(addr1, tx1); |
| 84 | + await storage.addToList(addr1, tx2); |
| 85 | + const remaining = await storage.removeFromList(addr1, tx1); |
| 86 | + expect(remaining).to.equal(1); |
| 87 | + const count = await storage.getList(addr1); |
| 88 | + expect(count).to.equal(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it('is idempotent when removing non-existent tx', async () => { |
| 92 | + await storage.addToList(addr1, tx1); |
| 93 | + const remaining = await storage.removeFromList(addr1, tx2); |
| 94 | + expect(remaining).to.equal(1); |
| 95 | + const count = await storage.getList(addr1); |
| 96 | + expect(count).to.equal(1); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('removeAll', () => { |
| 101 | + after(async () => { |
| 102 | + await redisClient.quit(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('deletes all pending:* keys', async () => { |
| 106 | + await storage.addToList(addr1, tx1); |
| 107 | + await storage.addToList(addr1, tx2); |
| 108 | + await storage.addToList(addr2, tx3); |
| 109 | + |
| 110 | + await storage.removeAll(); |
| 111 | + |
| 112 | + const c1 = await storage.getList(addr1); |
| 113 | + const c2 = await storage.getList(addr2); |
| 114 | + expect(c1).to.equal(0); |
| 115 | + expect(c2).to.equal(0); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments