Refactor the governance code a bit (#359)

This commit is contained in:
Ali Behjati 2022-10-21 17:04:19 +02:00 committed by GitHub
parent 0a8db01c10
commit d9e94b284d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 21 deletions

View File

@ -44,15 +44,15 @@ abstract contract PythGovernance is PythGetters, PythSetters, PythGovernanceInst
if (gi.action == GovernanceAction.UpgradeContract) { if (gi.action == GovernanceAction.UpgradeContract) {
require(gi.targetChainId != 0, "upgrade with chain id 0 is not possible"); require(gi.targetChainId != 0, "upgrade with chain id 0 is not possible");
upgradeContract(gi.payload); upgradeContract(parseUpgradeContractPayload(gi.payload));
} else if (gi.action == GovernanceAction.AuthorizeGovernanceDataSourceTransfer) { } else if (gi.action == GovernanceAction.AuthorizeGovernanceDataSourceTransfer) {
AuthorizeGovernanceDataSourceTransfer(gi.payload); AuthorizeGovernanceDataSourceTransfer(parseAuthorizeGovernanceDataSourceTransferPayload(gi.payload));
} else if (gi.action == GovernanceAction.SetDataSources) { } else if (gi.action == GovernanceAction.SetDataSources) {
setDataSources(gi.payload); setDataSources(parseSetDataSourcesPayload(gi.payload));
} else if (gi.action == GovernanceAction.SetFee) { } else if (gi.action == GovernanceAction.SetFee) {
setFee(gi.payload); setFee(parseSetFeePayload(gi.payload));
} else if (gi.action == GovernanceAction.SetValidPeriod) { } else if (gi.action == GovernanceAction.SetValidPeriod) {
setValidPeriod(gi.payload); setValidPeriod(parseSetValidPeriodPayload(gi.payload));
} else if (gi.action == GovernanceAction.RequestGovernanceDataSourceTransfer) { } else if (gi.action == GovernanceAction.RequestGovernanceDataSourceTransfer) {
revert("RequestGovernanceDataSourceTransfer can be only part of AuthorizeGovernanceDataSourceTransfer message"); revert("RequestGovernanceDataSourceTransfer can be only part of AuthorizeGovernanceDataSourceTransfer message");
} else { } else {
@ -60,10 +60,9 @@ abstract contract PythGovernance is PythGetters, PythSetters, PythGovernanceInst
} }
} }
function upgradeContract(bytes memory encodedPayload) internal { function upgradeContract(UpgradeContractPayload memory payload) internal {
UpgradeContractPayload memory payload = parseUpgradeContractPayload(encodedPayload); // This method on this contract does not have enough access to execute this, it should be executed on the
// This contract does not have enough access to execute this, it should be executed on the // upgradable contract.
// upgradable
upgradeUpgradableContract(payload); upgradeUpgradableContract(payload);
} }
@ -71,11 +70,9 @@ abstract contract PythGovernance is PythGetters, PythSetters, PythGovernanceInst
// Transfer the governance data source to a new value with sanity checks // Transfer the governance data source to a new value with sanity checks
// to ensure the new governance data source can manage the contract. // to ensure the new governance data source can manage the contract.
function AuthorizeGovernanceDataSourceTransfer(bytes memory encodedPayload) internal { function AuthorizeGovernanceDataSourceTransfer(AuthorizeGovernanceDataSourceTransferPayload memory payload) internal {
PythInternalStructs.DataSource memory oldGovernanceDatSource = governanceDataSource(); PythInternalStructs.DataSource memory oldGovernanceDatSource = governanceDataSource();
AuthorizeGovernanceDataSourceTransferPayload memory payload = parseAuthorizeGovernanceDataSourceTransferPayload(encodedPayload);
// Make sure the claimVaa is a valid VAA with RequestGovernanceDataSourceTransfer governance message // Make sure the claimVaa is a valid VAA with RequestGovernanceDataSourceTransfer governance message
// If it's valid then its emitter can take over the governance from the current emitter. // If it's valid then its emitter can take over the governance from the current emitter.
// The VAA is checked here to ensure that the new governance data source is valid and can send message // The VAA is checked here to ensure that the new governance data source is valid and can send message
@ -106,9 +103,7 @@ abstract contract PythGovernance is PythGetters, PythSetters, PythGovernanceInst
emit GovernanceDataSourceSet(oldGovernanceDatSource, governanceDataSource(), lastExecutedGovernanceSequence()); emit GovernanceDataSourceSet(oldGovernanceDatSource, governanceDataSource(), lastExecutedGovernanceSequence());
} }
function setDataSources(bytes memory encodedPayload) internal { function setDataSources(SetDataSourcesPayload memory payload) internal {
SetDataSourcesPayload memory payload = parseSetDataSourcesPayload(encodedPayload);
PythInternalStructs.DataSource[] memory oldDataSources = validDataSources(); PythInternalStructs.DataSource[] memory oldDataSources = validDataSources();
for (uint i = 0; i < oldDataSources.length; i += 1) { for (uint i = 0; i < oldDataSources.length; i += 1) {
@ -124,18 +119,14 @@ abstract contract PythGovernance is PythGetters, PythSetters, PythGovernanceInst
emit DataSourcesSet(oldDataSources, validDataSources()); emit DataSourcesSet(oldDataSources, validDataSources());
} }
function setFee(bytes memory encodedPayload) internal { function setFee(SetFeePayload memory payload) internal {
SetFeePayload memory payload = parseSetFeePayload(encodedPayload);
uint oldFee = singleUpdateFeeInWei(); uint oldFee = singleUpdateFeeInWei();
setSingleUpdateFeeInWei(payload.newFee); setSingleUpdateFeeInWei(payload.newFee);
emit FeeSet(oldFee, singleUpdateFeeInWei()); emit FeeSet(oldFee, singleUpdateFeeInWei());
} }
function setValidPeriod(bytes memory encodedPayload) internal { function setValidPeriod(SetValidPeriodPayload memory payload) internal {
SetValidPeriodPayload memory payload = parseSetValidPeriodPayload(encodedPayload);
uint oldValidPeriod = validTimePeriodSeconds(); uint oldValidPeriod = validTimePeriodSeconds();
setValidTimePeriodSeconds(payload.newValidPeriod); setValidTimePeriodSeconds(payload.newValidPeriod);