-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rewrite _syncBlockhash with async/await instead of promise chain…
…s. Verify lit block indexer response success. Use a previous block from public providers to avoid using one nodes haven't received yet
- Loading branch information
1 parent
ded2c3f
commit ea776a4
Showing
3 changed files
with
239 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import { LitCore } from './lit-core'; | ||
|
||
describe('LitCore', () => { | ||
let core: LitCore; | ||
|
||
describe('getLatestBlockhash', () => { | ||
let originalFetch: typeof fetch; | ||
let originalDateNow: typeof Date.now; | ||
const mockBlockhashUrl = | ||
'https://block-indexer-url.com/get_most_recent_valid_block'; | ||
|
||
beforeEach(() => { | ||
core = new LitCore({ | ||
litNetwork: 'custom', | ||
}); | ||
core['_blockHashUrl'] = mockBlockhashUrl; | ||
originalFetch = fetch; | ||
originalDateNow = Date.now; | ||
}); | ||
|
||
afterEach(() => { | ||
global.fetch = originalFetch; | ||
Date.now = originalDateNow; | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return cached blockhash if still valid', async () => { | ||
// Setup | ||
const mockBlockhash = '0x1234'; | ||
const currentTime = 1000000; | ||
core.latestBlockhash = mockBlockhash; | ||
core.lastBlockHashRetrieved = currentTime; | ||
Date.now = jest.fn().mockReturnValue(currentTime + 15000); // 15 seconds later | ||
global.fetch = jest.fn(); | ||
|
||
// Execute | ||
const result = await core.getLatestBlockhash(); | ||
|
||
// Assert | ||
expect(result).toBe(mockBlockhash); | ||
expect(global.fetch).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should fetch new blockhash when cache is expired', async () => { | ||
// Setup | ||
const mockBlockhash = '0x5678'; | ||
const currentTime = 1000000; | ||
core.latestBlockhash = '0x1234'; | ||
core.lastBlockHashRetrieved = currentTime - 31000; // 31 seconds ago currentTime | ||
const blockNumber = 12345; | ||
global.fetch = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
json: () => | ||
Promise.resolve({ | ||
blockhash: mockBlockhash, | ||
timestamp: currentTime, | ||
blockNumber, | ||
}), | ||
}); | ||
Date.now = jest.fn().mockReturnValue(currentTime); | ||
|
||
// Execute | ||
const result = await core.getLatestBlockhash(); | ||
|
||
// Assert | ||
expect(result).toBe(mockBlockhash); | ||
expect(fetch).toHaveBeenCalledWith(mockBlockhashUrl); | ||
}); | ||
|
||
it('should throw error when blockhash is not available', async () => { | ||
// Setup | ||
core.latestBlockhash = null; | ||
core.lastBlockHashRetrieved = null; | ||
global.fetch = jest.fn().mockResolvedValue({ | ||
ok: false, | ||
}); | ||
core['_getProviderWithFallback'] = jest.fn(() => Promise.resolve(null)); | ||
|
||
// Execute & Assert | ||
await expect(core.getLatestBlockhash()).rejects.toThrow( | ||
'latestBlockhash is not available. Received: "null"' | ||
); | ||
}); | ||
|
||
it('should handle fetch failure and use fallback RPC', async () => { | ||
// Setup | ||
const mockBlockhash = '0xabc'; | ||
const currentTime = 1000000; | ||
Date.now = jest.fn().mockReturnValue(currentTime); | ||
global.fetch = jest.fn().mockRejectedValue(new Error('Fetch failed')); | ||
const mockProvider = { | ||
getBlockNumber: jest.fn().mockResolvedValue(12345), | ||
getBlock: jest.fn().mockResolvedValue({ | ||
hash: mockBlockhash, | ||
number: 12345, | ||
timestamp: currentTime, | ||
}), | ||
}; | ||
jest.spyOn(core as any, '_getProviderWithFallback').mockResolvedValue({ | ||
...mockProvider, | ||
}); | ||
|
||
// Execute | ||
const result = await core.getLatestBlockhash(); | ||
|
||
// Assert | ||
expect(fetch).toHaveBeenCalledWith(mockBlockhashUrl); | ||
expect(mockProvider.getBlock).toHaveBeenCalledWith(-1); // safety margin | ||
expect(result).toBe(mockBlockhash); | ||
}); | ||
|
||
it('should handle empty blockhash response with fallback RPC URLs', async () => { | ||
// Setup | ||
const mockBlockhash = '0xabc'; | ||
const currentTime = 1000000; | ||
Date.now = jest.fn().mockReturnValue(currentTime); | ||
global.fetch = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
json: () => | ||
Promise.resolve({ | ||
blockhash: null, | ||
blockNumber: null, | ||
}), | ||
}); | ||
const mockProvider = { | ||
getBlockNumber: jest.fn().mockResolvedValue(12345), | ||
getBlock: jest.fn().mockResolvedValue({ | ||
hash: mockBlockhash, | ||
number: 12345, | ||
timestamp: currentTime, | ||
}), | ||
}; | ||
jest.spyOn(core as any, '_getProviderWithFallback').mockResolvedValue({ | ||
...mockProvider, | ||
}); | ||
|
||
// Execute | ||
const result = await core.getLatestBlockhash(); | ||
|
||
// Assert | ||
expect(fetch).toHaveBeenCalledWith(mockBlockhashUrl); | ||
expect(mockProvider.getBlock).toHaveBeenCalledWith(-1); // safety margin | ||
expect(result).toBe(mockBlockhash); | ||
}); | ||
|
||
it('should handle network timeouts gracefully', async () => { | ||
// Setup | ||
const currentTime = 1000000; | ||
Date.now = jest.fn().mockReturnValue(currentTime); | ||
|
||
global.fetch = jest | ||
.fn() | ||
.mockImplementation( | ||
() => | ||
new Promise((_, reject) => | ||
setTimeout(() => reject(new Error('Network timeout')), 1000) | ||
) | ||
); | ||
|
||
const mockProvider = { | ||
getBlockNumber: jest.fn().mockResolvedValue(12345), | ||
getBlock: jest.fn().mockResolvedValue(null), // Provider also fails | ||
}; | ||
|
||
jest.spyOn(core as any, '_getProviderWithFallback').mockResolvedValue({ | ||
...mockProvider, | ||
}); | ||
|
||
// Execute & Assert | ||
await expect(() => core.getLatestBlockhash()).rejects.toThrow( | ||
'latestBlockhash is not available. Received: "null"' | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters