Skip to content

Commit

Permalink
style(protocol): linting
Browse files Browse the repository at this point in the history
I always forget to run `yarn prettify` in the top-level directory
  • Loading branch information
arthurgousset committed Jun 4, 2024
1 parent 30de987 commit dcb09e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion packages/protocol/migrations_sol/Migration.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ contract Migration is Script, UsingRegistry, Constants {
(bool)
);
if (!skipTransferOwnership) {
// BlockchainParameters ownership transitioned to governance in a follow-up script.?
// BlockchainParameters ownership transitioned to governance in a follow-up script.?
for (uint256 i = 0; i < contractsInRegistry.length; i++) {
string memory contractToTransfer = contractsInRegistry[i];
console.log("Transfering ownership of: ", contractToTransfer);
Expand Down
34 changes: 18 additions & 16 deletions packages/protocol/test-sol/integration/Integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Constants } from "@test-sol/constants.sol";
import "@celo-contracts/common/interfaces/IRegistry.sol";
import "@celo-contracts/common/interfaces/IProxy.sol";


contract IntegrationTest is Test {
address constant registryAddress = address(0x000000000000000000000000000000000000ce10);
IRegistry registry = IRegistry(registryAddress);
Expand All @@ -21,21 +20,21 @@ contract RegistryIntegrationTest is IntegrationTest, Utils, Constants {
IProxy proxy;

function test_shouldHaveAddressInRegistry() public view {
for (uint256 i = 0; i < contractsInRegistry.length; i++) {
for (uint256 i = 0; i < contractsInRegistry.length; i++) {
string memory contractName = contractsInRegistry[i];
address contractAddress = registry.getAddressFor(keccak256(abi.encodePacked(contractName)));
console2.log(contractName, "address in Registry is: ", contractAddress);
assert(contractAddress != address(0));
}
}

function test_shouldHaveCorrectBytecode() public {
for (uint256 i = 0; i < contractsInRegistry.length; i++) {
function test_shouldHaveCorrectBytecode() public {
for (uint256 i = 0; i < contractsInRegistry.length; i++) {
// Read name from list of core contracts
string memory contractName = contractsInRegistry[i];
console2.log("Checking bytecode of:", contractName);

// Converting contract names to hashes for comparison
// Converting contract names to hashes for comparison
bytes32 hashContractName = keccak256(abi.encodePacked(contractName));
bytes32 hashAccount = keccak256(abi.encodePacked("Accounts"));
bytes32 hashElection = keccak256(abi.encodePacked("Election"));
Expand All @@ -47,14 +46,15 @@ contract RegistryIntegrationTest is IntegrationTest, Utils, Constants {

// Skipping test for contracts that depend on linked libraries
// This is a known limitation in Foundry at the moment:
// Source: https://github.com/foundry-rs/foundry/issues/6120
if (hashContractName != hashAccount
&& hashContractName != hashElection
&& hashContractName != hashEscrow
&& hashContractName != hashFederatedAttestations
&& hashContractName != hashGovernance
&& hashContractName != hashSortedOracles
&& hashContractName != hashValidators
// Source: https://github.com/foundry-rs/foundry/issues/6120
if (
hashContractName != hashAccount &&
hashContractName != hashElection &&
hashContractName != hashEscrow &&
hashContractName != hashFederatedAttestations &&
hashContractName != hashGovernance &&
hashContractName != hashSortedOracles &&
hashContractName != hashValidators
) {
// Get proxy address registered in the Registry
address proxyAddress = registry.getAddressForStringOrDie(contractName);
Expand All @@ -66,14 +66,16 @@ contract RegistryIntegrationTest is IntegrationTest, Utils, Constants {
// Get bytecode from deployed contract
bytes memory actualBytecodeWithMetadata = getCodeAt(implementationAddress);
bytes memory actualBytecode = removeMetadataFromBytecode(actualBytecodeWithMetadata);

// Get bytecode from build artifacts
bytes memory expectedBytecodeWithMetadata = vm.getDeployedCode(string(abi.encodePacked(contractName, ".sol")));
bytes memory expectedBytecodeWithMetadata = vm.getDeployedCode(
string(abi.encodePacked(contractName, ".sol"))
);
bytes memory expectedBytecode = removeMetadataFromBytecode(expectedBytecodeWithMetadata);

// Compare the bytecodes
assertEq(actualBytecode, expectedBytecode, "Bytecode does not match");
}
}
}
}
}
30 changes: 16 additions & 14 deletions packages/protocol/test-sol/utils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ contract Utils is Test {
}

/**
* @notice Gets runtime code (or "deployedBytecode") at a contract address.
* Using the `.code` or `.runtime` property on a contract is only available in Solidity 0.8.0 and later.
* On Solity <0.8.0, inline assembly is necessary to retrieve the bytecode of a contract.
* This implementation is taken from the Solidity documentation.
* Source: https://docs.soliditylang.org/en/v0.4.24/assembly.html#example
* @param _addr Contract address.
* @return Runtime bytecode at contract address.
*/
* @notice Gets runtime code (or "deployedBytecode") at a contract address.
* Using the `.code` or `.runtime` property on a contract is only available in Solidity 0.8.0 and later.
* On Solity <0.8.0, inline assembly is necessary to retrieve the bytecode of a contract.
* This implementation is taken from the Solidity documentation.
* Source: https://docs.soliditylang.org/en/v0.4.24/assembly.html#example
* @param _addr Contract address.
* @return Runtime bytecode at contract address.
*/
function getCodeAt(address _addr) public view returns (bytes memory o_code) {
assembly {
// retrieve the size of the code, this needs assembly
Expand All @@ -102,16 +102,18 @@ contract Utils is Test {
}

/**
* @notice Removes CBOR encoded metadata from the tail of the deployedBytecode.
* @param data Bytecode including the CBOR encoded tail.
* @return Bytecode without the CBOR encoded metadata.
*/
* @notice Removes CBOR encoded metadata from the tail of the deployedBytecode.
* @param data Bytecode including the CBOR encoded tail.
* @return Bytecode without the CBOR encoded metadata.
*/
function removeMetadataFromBytecode(bytes memory data) public pure returns (bytes memory) {
// Ensure the data length is at least enough to contain the length specifier
require(data.length >= 2, "Data too short to contain a valid CBOR length specifier");

// Calculate the length of the CBOR encoded section from the last two bytes
uint16 cborLength = uint16(uint8(data[data.length - 2])) * 256 + uint16(uint8(data[data.length - 1]));
uint16 cborLength = uint16(uint8(data[data.length - 2])) *
256 +
uint16(uint8(data[data.length - 1]));

// Ensure the length is valid (not greater than the data array length minus 2 bytes for the length field)
require(cborLength <= data.length - 2, "Invalid CBOR length");
Expand All @@ -124,7 +126,7 @@ contract Utils is Test {

// Copy data from the original byte array to the new one, excluding the CBOR section and its length field
for (uint i = 0; i < newLength; i++) {
result[i] = data[i];
result[i] = data[i];
}

return result;
Expand Down

0 comments on commit dcb09e1

Please sign in to comment.