-
Notifications
You must be signed in to change notification settings - Fork 108
Feat/evm performance optimizations #1532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Thorian1te
wants to merge
11
commits into
master
Choose a base branch
from
feat/evm-performance-optimizations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a10190b
added get fee rate from mayachain
Thorian1te 1c0c55d
feat: optimize EVM performance with parallel execution and caching
Thorian1te 57ddb8e
add changeset and fix CR comment
Thorian1te 853ff60
d28c08d
update from CR comments
Thorian1te ed9d5ff
update from Cr comments
Thorian1te 81c1a85
update from Cr comments
Thorian1te 0745bf7
update from comments
Thorian1te df77ce1
Merge branch 'master' into feat/evm-performance-optimizations
Thorian1te d057799
use bigInit instead of number
Thorian1te bad750a
added changeset
Thorian1te File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
--- | ||
'@xchainjs/xchain-client': patch | ||
'@xchainjs/xchain-utxo': patch | ||
'@xchainjs/xchain-evm': patch | ||
--- | ||
|
||
Updated get fee rates to observe Mayachain as well |
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
import { JsonRpcProvider } from 'ethers' | ||
import { getCachedContract } from '../src/cache' | ||
import erc20ABI from '../src/data/erc20.json' | ||
|
||
describe('Contract Cache', () => { | ||
it('should cache contracts separately for different providers', async () => { | ||
// Create two different providers | ||
const provider1 = new JsonRpcProvider('https://eth.llamarpc.com') | ||
const provider2 = new JsonRpcProvider('https://goerli.infura.io/v3/test') | ||
|
||
const contractAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC address | ||
|
||
// Get contracts from both providers | ||
const contract1 = await getCachedContract(contractAddress, erc20ABI, provider1) | ||
const contract2 = await getCachedContract(contractAddress, erc20ABI, provider2) | ||
|
||
// Contracts should have different providers | ||
expect(contract1.runner).toBe(provider1) | ||
expect(contract2.runner).toBe(provider2) | ||
expect(contract1).not.toBe(contract2) // Different contract instances | ||
|
||
// Getting the same contract again should return the cached instance | ||
const contract1Again = await getCachedContract(contractAddress, erc20ABI, provider1) | ||
expect(contract1).toBe(contract1Again) // Same instance from cache | ||
}) | ||
Thorian1te marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
it('should cache contracts separately for different addresses on same provider', async () => { | ||
const provider = new JsonRpcProvider('https://eth.llamarpc.com') | ||
|
||
const address1 = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC | ||
const address2 = '0xdAC17F958D2ee523a2206206994597C13D831ec7' // USDT | ||
|
||
// Get contracts for different addresses | ||
const contract1 = await getCachedContract(address1, erc20ABI, provider) | ||
const contract2 = await getCachedContract(address2, erc20ABI, provider) | ||
|
||
// Should be different contract instances | ||
expect(contract1).not.toBe(contract2) | ||
expect(contract1.target).toBe(address1) | ||
expect(contract2.target).toBe(address2) | ||
|
||
// Getting the same contract again should return the cached instance | ||
const contract1Again = await getCachedContract(address1, erc20ABI, provider) | ||
expect(contract1).toBe(contract1Again) | ||
}) | ||
Thorian1te marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
}) |
This file contains hidden or 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,62 @@ | ||
import { Contract, Provider } from 'ethers' | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
import BigNumber from 'bignumber.js' | ||
|
||
// Per-provider contract cache to ensure contracts are properly isolated | ||
// Key format: `${providerNetwork}_${chainId}_${address}` | ||
const contractCache = new Map<string, Contract>() | ||
const bigNumberCache = new Map<string, BigNumber>() | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Generate a unique cache key for a contract that includes provider context | ||
*/ | ||
async function getContractCacheKey(address: string, provider: Provider): Promise<string> { | ||
try { | ||
// Get network information from provider to create unique key | ||
const network = await provider.getNetwork() | ||
const chainId = network.chainId.toString() | ||
const networkName = network.name || 'unknown' | ||
return `${networkName}_${chainId}_${address.toLowerCase()}` | ||
} catch { | ||
// Fallback to a provider-specific key if network info unavailable | ||
// Use provider instance as unique identifier | ||
const providerIdentity = provider as any | ||
const connectionUrl = providerIdentity._request?.url || | ||
providerIdentity.connection?.url || | ||
providerIdentity._url || | ||
'unknown' | ||
const hashedKey = Buffer.from(connectionUrl.toString()).toString('base64').replace(/[^a-zA-Z0-9]/g, '').slice(0, 10) | ||
return `provider_${hashedKey}_${address.toLowerCase()}` | ||
} | ||
Thorian1te marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
Thorian1te marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Get a cached Contract instance or create a new one | ||
* Now includes provider/network isolation to prevent cross-network contract reuse | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export async function getCachedContract(address: string, abi: any, provider: Provider): Promise<Contract> { | ||
const key = await getContractCacheKey(address, provider) | ||
if (!contractCache.has(key)) { | ||
contractCache.set(key, new Contract(address, abi, provider)) | ||
} | ||
return contractCache.get(key)! | ||
} | ||
|
||
/** | ||
* Get a cached BigNumber instance or create a new one | ||
*/ | ||
export function getCachedBigNumber(value: string | number): BigNumber { | ||
const stringValue = value.toString() | ||
if (!bigNumberCache.has(stringValue)) { | ||
bigNumberCache.set(stringValue, new BigNumber(stringValue)) | ||
} | ||
return bigNumberCache.get(stringValue)! | ||
} | ||
Thorian1te marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Clear all caches (useful for testing or memory management) | ||
*/ | ||
export function clearCaches(): void { | ||
contractCache.clear() | ||
bigNumberCache.clear() | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.