-
Notifications
You must be signed in to change notification settings - Fork 77
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
Implement caching for parameters and identity storage contracts #2834
Changes from 2 commits
a8c7806
d0870db
ade1090
3176845
829599f
4ac849d
d44ed6d
76ca352
04fbd45
962de73
4810571
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -554,6 +554,7 @@ export const CONTRACTS = { | |
HUB_CONTRACT: 'HubContract', | ||
COMMIT_MANAGER_V1_U1_CONTRACT: 'CommitManagerV1U1Contract', | ||
SERVICE_AGREEMENT_V1_CONTRACT: 'ServiceAgreementV1Contract', | ||
PARAMETERS_STORAGE_CONTRACT: 'ParametersStorageContract', | ||
}; | ||
|
||
export const CONTRACT_EVENTS = { | ||
|
@@ -563,6 +564,7 @@ export const CONTRACT_EVENTS = { | |
PROFILE: ['AskUpdated'], | ||
COMMIT_MANAGER_V1: ['StateFinalized'], | ||
SERVICE_AGREEMENT_V1: ['ServiceAgreementV1Extended', 'ServiceAgreementV1Terminated'], | ||
PARAMETERS_STORAGE: ['ParameterChanged'], | ||
}; | ||
|
||
export const NODE_ENVIRONMENTS = { | ||
|
@@ -586,3 +588,16 @@ export const BLOCK_TIME_MILLIS = { | |
}; | ||
|
||
export const TRANSACTION_CONFIRMATIONS = 1; | ||
|
||
export const CACHED_FUNCTIONS = { | ||
ParametersStorage: [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make it an object, with If we remove types as I suggested in the comment above, we can just make it a |
||
'r0', | ||
'r1', | ||
'r2', | ||
'finalizationCommitsNumber', | ||
'updateCommitWindowDuration', | ||
'commitWindowDurationPerc', | ||
'proofWindowDurationPerc', | ||
'epochLength', | ||
], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import { | |
HTTP_RPC_PROVIDER_PRIORITY, | ||
FALLBACK_PROVIDER_QUORUM, | ||
RPC_PROVIDER_STALL_TIMEOUT, | ||
CACHED_FUNCTIONS, | ||
} from '../../../constants/constants.js'; | ||
|
||
const require = createRequire(import.meta.url); | ||
|
@@ -51,6 +52,7 @@ const ABIs = { | |
const SCORING_FUNCTIONS = { | ||
1: 'Log2PLDSF', | ||
}; | ||
const resultCache = {}; | ||
|
||
class Web3Service { | ||
async initialize(config, logger) { | ||
|
@@ -269,6 +271,10 @@ class Web3Service { | |
} | ||
} | ||
|
||
cacheParameter(parameterName, parameterValue) { | ||
resultCache[parameterName] = parameterValue; | ||
} | ||
|
||
initializeContract(contractName, contractAddress) { | ||
if (ABIs[contractName] != null) { | ||
this[`${contractName}Contract`] = new ethers.Contract( | ||
|
@@ -406,30 +412,42 @@ class Web3Service { | |
|
||
async callContractFunction(contractInstance, functionName, args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're using this function for |
||
let result; | ||
while (result === undefined) { | ||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
result = await contractInstance[functionName](...args); | ||
} catch (error) { | ||
const decodedErrorData = this._decodeErrorData(error, contractInstance.interface); | ||
|
||
const functionFragment = contractInstance.interface.getFunction( | ||
error.transaction.data.slice(0, 10), | ||
); | ||
const inputs = functionFragment.inputs | ||
.map((input, i) => { | ||
const argName = input.name; | ||
const argValue = this._formatArgument(args[i]); | ||
return `${argName}=${argValue}`; | ||
}) | ||
.join(', '); | ||
if ( | ||
CACHED_FUNCTIONS.ParametersStorage.includes(functionName) && | ||
resultCache[functionName] !== undefined | ||
milosstanisavljevic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
result = resultCache[functionName]; | ||
} else { | ||
while (result === undefined) { | ||
try { | ||
// eslint-disable-next-line no-await-in-loop | ||
result = await contractInstance[functionName](...args); | ||
if (CACHED_FUNCTIONS.ParametersStorage.includes(functionName)) { | ||
resultCache[functionName] = result; | ||
} | ||
} catch (error) { | ||
const decodedErrorData = this._decodeErrorData( | ||
error, | ||
contractInstance.interface, | ||
); | ||
|
||
throw new Error( | ||
`Call ${functionName}(${inputs}) failed, reason: ${decodedErrorData}`, | ||
); | ||
const functionFragment = contractInstance.interface.getFunction( | ||
error.transaction.data.slice(0, 10), | ||
); | ||
const inputs = functionFragment.inputs | ||
.map((input, i) => { | ||
const argName = input.name; | ||
const argValue = this._formatArgument(args[i]); | ||
return `${argName}=${argValue}`; | ||
}) | ||
.join(', '); | ||
|
||
throw new Error( | ||
`Call ${functionName}(${inputs}) failed, reason: ${decodedErrorData}`, | ||
); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
|
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.
Could you also please move the cached values from the scoring function ?