-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refs/heads/master' into feature/lit-4031-new-listener-sdk
- Loading branch information
Showing
33 changed files
with
370 additions
and
91 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 |
---|---|---|
|
@@ -40,8 +40,8 @@ | |
"@cosmjs/proto-signing": "0.30.1", | ||
"@cosmjs/stargate": "0.30.1", | ||
"@dotenvx/dotenvx": "^1.6.4", | ||
"@lit-protocol/accs-schemas": "^0.0.19", | ||
"@lit-protocol/contracts": "^0.0.63", | ||
"@lit-protocol/accs-schemas": "^0.0.20", | ||
"@lit-protocol/contracts": "^0.0.74", | ||
"@metamask/eth-sig-util": "5.0.2", | ||
"@mysten/sui.js": "^0.37.1", | ||
"@openagenda/verror": "^3.1.4", | ||
|
@@ -84,7 +84,7 @@ | |
"@nx/plugin": "17.3.0", | ||
"@nx/react": "17.3.0", | ||
"@nx/web": "17.3.0", | ||
"@solana/web3.js": "^1.95.3", | ||
"@solana/web3.js": "1.95.3", | ||
"@types/depd": "^1.1.36", | ||
"@types/events": "^3.0.3", | ||
"@types/jest": "27.4.1", | ||
|
@@ -126,6 +126,5 @@ | |
}, | ||
"workspaces": [ | ||
"packages/*" | ||
], | ||
"packageManager": "[email protected]" | ||
] | ||
} |
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
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
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 |
---|---|---|
@@ -1 +1 @@ | ||
export const version = '7.0.0'; | ||
export const version = '7.0.2'; |
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
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,177 @@ | ||
import { InvalidEthBlockhash } from '@lit-protocol/constants'; | ||
|
||
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( | ||
InvalidEthBlockhash | ||
); | ||
}); | ||
|
||
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( | ||
InvalidEthBlockhash | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.