Skip to content
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

Contract changes for storing addresses #1622

Merged
merged 3 commits into from
Oct 25, 2023

Conversation

BedrockSquirrel
Copy link
Collaborator

Why this change is needed

While we're in testnet it is convenient to store the addresses of important network contracts in the management contract as an accessible source of truth.

What changes were made as part of this PR

Add a list of keys and a map of key -> address. The list is necessary so that callers can 'get all' without having to know them up front, means we can serve them all from the node without having to make changes if new ones are added.

PR checks pre-merging

Please indicate below by ticking the checkbox that you have read and performed the required
PR checks

  • PR checks reviewed and performed

@coderabbitai
Copy link

coderabbitai bot commented Oct 25, 2023

Walkthrough

The changes introduced in the ManagementContract.sol file are primarily focused on managing important contract addresses. This includes the addition of a mapping to store these addresses, an array to store their keys, and functions to set an address and retrieve all keys. An event has also been added to log changes to these addresses.

Changes

File Summary
.../management/ManagementContract.sol Introduced a mapping importantContractAddresses for storing important contract addresses, an array importantContractKeys for storing their keys, and an event ImportantContractAddressUpdated for logging changes. Added functions setImportantContractAddress and getImportantContractKeys for setting an address and retrieving all keys respectively.

Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • If you reply to a review comment from CodeRabbit, the bot will automatically respond.
  • To engage with CodeRabbit bot directly around the specific lines of code in the PR, mention @coderabbitai in your review comment
  • Note: Review comments are made on code diffs or files, not on the PR overview.
  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai help to get help.

Note: For conversation with the bot, please use the review comments on code diffs or files.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.json

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 563c567 and ddc98b6.
Files ignored due to filter (1)
  • contracts/generated/ManagementContract/ManagementContract.go
Files selected for processing (1)
  • contracts/src/management/ManagementContract.sol (2 hunks)
Additional comments: 3
contracts/src/management/ManagementContract.sol (3)
  • 13-14: The addition of the ImportantContractAddressUpdated event is a good practice for logging changes to important contract addresses. This will help in tracking changes and debugging if necessary.

  • 16-26: The addition of importantContractKeys and importantContractAddresses is a practical solution for the testnet phase. However, as noted in the comments, this might not be a long-term solution. It's important to revisit this decision in the future and consider a more scalable and secure way of managing important contract addresses. Also, consider adding a comment to explain the purpose of importantContractKeys and importantContractAddresses for better code readability.

+    // importantContractKeys stores the keys for important contract addresses
+    string[] public importantContractKeys;
+    // importantContractAddresses is a mapping from keys to important contract addresses
+    mapping (string => address) public importantContractAddresses;
Committable suggestion (Beta)
    // importantContractKeys stores the keys for important contract addresses
    string[] public importantContractKeys;
    // importantContractAddresses is a mapping from keys to important contract addresses
    mapping (string => address) public importantContractAddresses;
  • 154-156: The getImportantContractKeys function is a good addition for retrieving all keys of important contract addresses. It returns importantContractKeys, which can be used to fetch the corresponding addresses from importantContractAddresses.

Comment on lines 145 to 152
// Function to set an important contract's address, only callable by owner
function setImportantContractAddress(string memory key, address newAddress) public onlyOwner {
if (importantContractAddresses[key] == address(0)) {
importantContractKeys.push(key);
}
importantContractAddresses[key] = newAddress;
emit ImportantContractAddressUpdated(key, newAddress);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setImportantContractAddress function is a good addition for managing important contract addresses. It checks if the key is new and adds it to importantContractKeys if it is. Then it updates the address in importantContractAddresses and emits an event. This function is only callable by the owner, which is a good security measure. However, consider adding input validation to ensure that the newAddress is not a zero address.

+    function setImportantContractAddress(string memory key, address newAddress) public onlyOwner {
+        require(newAddress != address(0), "New address cannot be a zero address");
+        if (importantContractAddresses[key] == address(0)) {
+            importantContractKeys.push(key);
+        }
+        importantContractAddresses[key] = newAddress;
+        emit ImportantContractAddressUpdated(key, newAddress);
+    }
Committable suggestion (Beta)
Suggested change
// Function to set an important contract's address, only callable by owner
function setImportantContractAddress(string memory key, address newAddress) public onlyOwner {
if (importantContractAddresses[key] == address(0)) {
importantContractKeys.push(key);
}
importantContractAddresses[key] = newAddress;
emit ImportantContractAddressUpdated(key, newAddress);
}
function setImportantContractAddress(string memory key, address newAddress) public onlyOwner {
require(newAddress != address(0), "New address cannot be a zero address");
if (importantContractAddresses[key] == address(0)) {
importantContractKeys.push(key);
}
importantContractAddresses[key] = newAddress;
emit ImportantContractAddressUpdated(key, newAddress);
}

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ddc98b6 and 868de57.
Files selected for processing (1)
  • contracts/src/management/ManagementContract.sol (2 hunks)
Additional comments: 3
contracts/src/management/ManagementContract.sol (3)
  • 13-14: The new event ImportantContractAddressUpdated is introduced to log changes to important contract addresses. This is a good practice for tracking changes and can be useful for debugging and auditing purposes.

  • 21-26: The new mapping importantContractAddresses and the array importantContractKeys are introduced to store important contract addresses and their corresponding keys. This is a temporary solution for testnets and may not be suitable for long-term use. Consider using a more permanent solution for production environments.

  • 154-156: The new function GetImportantContractKeys is introduced to retrieve all keys for the important contract addresses. This function is a good practice for providing access to the keys.

Comment on lines +146 to +152
function SetImportantContractAddress(string memory key, address newAddress) public onlyOwner {
if (importantContractAddresses[key] == address(0)) {
importantContractKeys.push(key);
}
importantContractAddresses[key] = newAddress;
emit ImportantContractAddressUpdated(key, newAddress);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new function SetImportantContractAddress is introduced to update an important contract's address. This function is only callable by the owner, which is a good security practice. However, there is no check for whether the newAddress is a valid address. Consider adding a check to ensure that the newAddress is not the zero address.

-        if (importantContractAddresses[key] == address(0)) {
+        require(newAddress != address(0), "New address cannot be the zero address");
+        if (importantContractAddresses[key] == address(0)) {
Committable suggestion (Beta)
Suggested change
function SetImportantContractAddress(string memory key, address newAddress) public onlyOwner {
if (importantContractAddresses[key] == address(0)) {
importantContractKeys.push(key);
}
importantContractAddresses[key] = newAddress;
emit ImportantContractAddressUpdated(key, newAddress);
}
function SetImportantContractAddress(string memory key, address newAddress) public onlyOwner {
require(newAddress != address(0), "New address cannot be the zero address");
if (importantContractAddresses[key] == address(0)) {
importantContractKeys.push(key);
}
importantContractAddresses[key] = newAddress;
emit ImportantContractAddressUpdated(key, newAddress);
}

Copy link
Contributor

@StefanIliev545 StefanIliev545 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

@BedrockSquirrel BedrockSquirrel merged commit 9eb4dd0 into main Oct 25, 2023
@BedrockSquirrel BedrockSquirrel deleted the matt/expose-contract-addresses branch October 25, 2023 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants