- 
                Notifications
    
You must be signed in to change notification settings  - Fork 109
 
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 all 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,5 @@ | ||
| --- | ||
| '@xchainjs/xchain-evm': minor | ||
| --- | ||
| 
     | 
||
| Added caching for better performance | 
  
    
      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,74 @@ | ||
| 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', () => { | ||
| // 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 = getCachedContract(contractAddress, erc20ABI, provider1) | ||
| const contract2 = 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 = getCachedContract(contractAddress, erc20ABI, provider1) | ||
| expect(contract1).toBe(contract1Again) // Same instance from cache | ||
| }) | ||
| 
     | 
||
| it('should cache contracts separately for different addresses on same provider', () => { | ||
| const provider = new JsonRpcProvider('https://eth.llamarpc.com') | ||
| 
     | 
||
| const address1 = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC | ||
| const address2 = '0xdAC17F958D2ee523a2206206994597C13D831ec7' // USDT | ||
| 
     | 
||
| // Get contracts for different addresses | ||
| const contract1 = getCachedContract(address1, erc20ABI, provider) | ||
| const contract2 = 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 = getCachedContract(address1, erc20ABI, provider) | ||
| expect(contract1).toBe(contract1Again) | ||
| }) | ||
| 
     | 
||
| it('should cache contracts separately for different provider instances with same URL', () => { | ||
| // Create two distinct provider instances using the same URL | ||
| const sameUrl = 'https://eth.llamarpc.com' | ||
| const provider1 = new JsonRpcProvider(sameUrl) | ||
| const provider2 = new JsonRpcProvider(sameUrl) | ||
| 
     | 
||
| const contractAddress = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48' // USDC address | ||
| 
     | 
||
| // Get contracts from both provider instances | ||
| const contract1 = getCachedContract(contractAddress, erc20ABI, provider1) | ||
| const contract2 = getCachedContract(contractAddress, erc20ABI, provider2) | ||
| 
     | 
||
| // Contracts should be different instances (cache keys by provider instance, not just URL) | ||
| expect(contract1).not.toBe(contract2) | ||
| 
     | 
||
| // Each contract should be bound to its respective provider | ||
| expect(contract1.runner).toBe(provider1) | ||
| expect(contract2.runner).toBe(provider2) | ||
| expect(contract1.runner).not.toBe(contract2.runner) | ||
| 
     | 
||
| // Calling getCachedContract again with the same provider should return the cached instance | ||
| const contract1Again = getCachedContract(contractAddress, erc20ABI, provider1) | ||
| const contract2Again = getCachedContract(contractAddress, erc20ABI, provider2) | ||
| 
     | 
||
| expect(contract1).toBe(contract1Again) // Same instance from cache for provider1 | ||
| expect(contract2).toBe(contract2Again) // Same instance from cache for provider2 | ||
| }) | ||
| }) | 
  
    
      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,54 @@ | ||
| import { Contract } from 'ethers' | ||
| import type { Provider, InterfaceAbi } from 'ethers' | ||
| import BigNumber from 'bignumber.js' | ||
| 
     | 
||
| // Provider-scoped contract cache using WeakMap for automatic cleanup | ||
| const contractCacheByProvider: WeakMap<Provider, Map<string, Contract>> = new WeakMap() | ||
| const bigNumberCache = new Map<string, BigNumber>() | ||
| 
     | 
||
| /** | ||
| * Get a cached Contract instance or create a new one | ||
| * Uses provider-scoped caching for proper isolation | ||
| */ | ||
| export function getCachedContract(address: string, abi: InterfaceAbi, provider: Provider): Contract { | ||
| // Get or create the contract cache for this provider | ||
| let providerCache = contractCacheByProvider.get(provider) | ||
| if (!providerCache) { | ||
| providerCache = new Map<string, Contract>() | ||
| contractCacheByProvider.set(provider, providerCache) | ||
| } | ||
| 
     | 
||
| // Use normalized address as key | ||
| const normalizedAddress = address.toLowerCase() | ||
| 
     | 
||
| // Get or create the contract for this address | ||
| let contract = providerCache.get(normalizedAddress) | ||
| if (!contract) { | ||
| contract = new Contract(address, abi, provider) | ||
| providerCache.set(normalizedAddress, contract) | ||
| } | ||
| 
     | 
||
| return contract | ||
| } | ||
| 
     | 
||
| /** | ||
| * Get a cached BigNumber instance or create a new one | ||
| * Only accepts string or bigint to preserve precision | ||
| */ | ||
| export function getCachedBigNumber(value: string | bigint): BigNumber { | ||
| const stringValue = typeof value === 'bigint' ? value.toString() : value | ||
| if (!bigNumberCache.has(stringValue)) { | ||
| bigNumberCache.set(stringValue, new BigNumber(stringValue)) | ||
| } | ||
| return bigNumberCache.get(stringValue)! | ||
| } | ||
| 
     | 
||
| /** | ||
| * Clear all caches (useful for testing or memory management) | ||
| * Note: WeakMap-based contract cache will be automatically cleaned up by GC | ||
| */ | ||
| export function clearCaches(): void { | ||
| // Note: WeakMap doesn't have a clear() method, and that's by design | ||
| // The contract cache will be automatically cleaned up when providers are GC'd | ||
| bigNumberCache.clear() | ||
| } | ||
      
      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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this cache trying to optimize? It seems that it only saves the BigNumber instance. Is this really a costly process?