Skip to content

Commit

Permalink
Fix solhint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike-CZ committed Oct 11, 2024
1 parent d8fcd6c commit 836f436
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 283 deletions.
3 changes: 1 addition & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
{
"files": "*.sol",
"options": {
"singleQuote": false,
"quoteProps": "consistent"
"singleQuote": false
}
}
],
Expand Down
4 changes: 3 additions & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "solhint:recommended",
"rules": {
"max-line-length": "off"
"func-visibility": ["warn", {"ignoreConstructors": true}],
"max-states-count": ["off"],
"no-inline-assembly": "off"
}
}
1 change: 1 addition & 0 deletions .solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contracts/test
21 changes: 14 additions & 7 deletions contracts/sfc/Migrations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@ pragma solidity ^0.8.9;

contract Migrations {
address public owner;
uint public last_completed_migration;
uint256 public lastCompletedMigration;

/**
* @dev The caller is not the owner.
*/
error NotOwner();

constructor(address contractOwner) {
owner = contractOwner;
}

modifier restricted() {
require(msg.sender == owner, "Not the contract owner");
if (msg.sender != owner) {
revert NotOwner();
}
_;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
function setCompleted(uint256 completed) public restricted {
lastCompletedMigration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
function upgrade(address newAddress) public restricted {
Migrations upgraded = Migrations(newAddress);
upgraded.setCompleted(lastCompletedMigration);
}
}
8 changes: 4 additions & 4 deletions contracts/sfc/NetworkInitializer.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

import "./SFCI.sol";
import "./NodeDriver.sol";
import "./SFCLib.sol";
import "./ConstantsManager.sol";
import {SFCI} from "./SFCI.sol";
import {NodeDriver, NodeDriverAuth} from "./NodeDriver.sol";
import {ConstantsManager} from "./ConstantsManager.sol";
import {Decimal} from "../common/Decimal.sol";

contract NetworkInitializer {
// Initialize NodeDriverAuth, NodeDriver and SFC in one call to allow fewer genesis transactions
Expand Down
239 changes: 8 additions & 231 deletions contracts/sfc/NodeDriver.sol
Original file line number Diff line number Diff line change
@@ -1,230 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

import "../common/Initializable.sol";
import "../ownership/Ownable.sol";
import "./SFCI.sol";
import {Initializable} from "../common/Initializable.sol";
import {NodeDriverAuth} from "./NodeDriverAuth.sol";

interface NodeDriverExecutable {
function execute() external;
}

contract NodeDriverAuth is Initializable, Ownable {
SFCI internal sfc;
NodeDriver internal driver;

// Initialize NodeDriverAuth, NodeDriver and SFC in one call to allow fewer genesis transactions
function initialize(address payable _sfc, address _driver, address _owner) external initializer {
Ownable.initialize(_owner);
driver = NodeDriver(_driver);
sfc = SFCI(_sfc);
}

modifier onlySFC() {
require(msg.sender == address(sfc), "caller is not the SFC contract");
_;
}

modifier onlyDriver() {
require(msg.sender == address(driver), "caller is not the NodeDriver contract");
_;
}

function migrateTo(address newDriverAuth) external onlyOwner {
driver.setBackend(newDriverAuth);
}

function _execute(address executable, address newOwner, bytes32 selfCodeHash, bytes32 driverCodeHash) internal {
_transferOwnership(executable);
NodeDriverExecutable(executable).execute();
_transferOwnership(newOwner);
//require(driver.backend() == address(this), "ownership of driver is lost");
require(_getCodeHash(address(this)) == selfCodeHash, "self code hash doesn't match");
require(_getCodeHash(address(driver)) == driverCodeHash, "driver code hash doesn't match");
}

function execute(address executable) external onlyOwner {
_execute(executable, owner(), _getCodeHash(address(this)), _getCodeHash(address(driver)));
}

function mutExecute(
address executable,
address newOwner,
bytes32 selfCodeHash,
bytes32 driverCodeHash
) external onlyOwner {
_execute(executable, newOwner, selfCodeHash, driverCodeHash);
}

function incBalance(address acc, uint256 diff) external onlySFC {
require(acc == address(sfc), "recipient is not the SFC contract");
driver.setBalance(acc, address(acc).balance + diff);
}

function upgradeCode(address acc, address from) external onlyOwner {
require(isContract(acc) && isContract(from), "not a contract");
driver.copyCode(acc, from);
}

function copyCode(address acc, address from) external onlyOwner {
driver.copyCode(acc, from);
}

function incNonce(address acc, uint256 diff) external onlyOwner {
driver.incNonce(acc, diff);
}

function updateNetworkRules(bytes calldata diff) external onlyOwner {
driver.updateNetworkRules(diff);
}

function updateMinGasPrice(uint256 minGasPrice) external onlySFC {
// prettier-ignore
driver.updateNetworkRules(bytes(strConcat("{\"Economy\":{\"MinGasPrice\":", uint256ToStr(minGasPrice), "}}")));
}

function updateNetworkVersion(uint256 version) external onlyOwner {
driver.updateNetworkVersion(version);
}

function advanceEpochs(uint256 num) external onlyOwner {
driver.advanceEpochs(num);
}

function updateValidatorWeight(uint256 validatorID, uint256 value) external onlySFC {
driver.updateValidatorWeight(validatorID, value);
}

function updateValidatorPubkey(uint256 validatorID, bytes calldata pubkey) external onlySFC {
driver.updateValidatorPubkey(validatorID, pubkey);
}

function setGenesisValidator(
address _auth,
uint256 validatorID,
bytes calldata pubkey,
uint256 status,
uint256 createdEpoch,
uint256 createdTime,
uint256 deactivatedEpoch,
uint256 deactivatedTime
) external onlyDriver {
sfc.setGenesisValidator(
_auth,
validatorID,
pubkey,
status,
createdEpoch,
createdTime,
deactivatedEpoch,
deactivatedTime
);
}

function setGenesisDelegation(
address delegator,
uint256 toValidatorID,
uint256 stake,
uint256 lockedStake,
uint256 lockupFromEpoch,
uint256 lockupEndTime,
uint256 lockupDuration,
uint256 earlyUnlockPenalty,
uint256 rewards
) external onlyDriver {
sfc.setGenesisDelegation(
delegator,
toValidatorID,
stake,
lockedStake,
lockupFromEpoch,
lockupEndTime,
lockupDuration,
earlyUnlockPenalty,
rewards
);
}

function deactivateValidator(uint256 validatorID, uint256 status) external onlyDriver {
sfc.deactivateValidator(validatorID, status);
}

function sealEpochValidators(uint256[] calldata nextValidatorIDs) external onlyDriver {
sfc.sealEpochValidators(nextValidatorIDs);
}

function sealEpoch(
uint256[] calldata offlineTimes,
uint256[] calldata offlineBlocks,
uint256[] calldata uptimes,
uint256[] calldata originatedTxsFee,
uint256 usedGas
) external onlyDriver {
sfc.sealEpoch(offlineTimes, offlineBlocks, uptimes, originatedTxsFee, usedGas);
}

function isContract(address account) internal view returns (bool) {
uint256 size;
// solhint-disable-next-line no-inline-assembly
assembly {
size := extcodesize(account)
}
return size > 0;
}
interface EVMWriter {
function setBalance(address acc, uint256 value) external;

function decimalsNum(uint256 num) internal pure returns (uint256) {
uint decimals;
while (num != 0) {
decimals++;
num /= 10;
}
return decimals;
}
function copyCode(address acc, address from) external;

function uint256ToStr(uint256 num) internal pure returns (string memory) {
if (num == 0) {
return "0";
}
uint decimals = decimalsNum(num);
bytes memory bstr = new bytes(decimals);
uint strIdx = decimals - 1;
while (num != 0) {
bstr[strIdx] = bytes1(uint8(48 + (num % 10)));
num /= 10;
if (strIdx > 0) {
strIdx--;
}
}
return string(bstr);
}
function swapCode(address acc, address where) external;

function strConcat(string memory _a, string memory _b, string memory _c) internal pure returns (string memory) {
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
bytes memory _bc = bytes(_c);
string memory abc = new string(_ba.length + _bb.length + _bc.length);
bytes memory babc = bytes(abc);
uint k = 0;
uint i = 0;
for (i = 0; i < _ba.length; i++) {
babc[k++] = _ba[i];
}
for (i = 0; i < _bb.length; i++) {
babc[k++] = _bb[i];
}
for (i = 0; i < _bc.length; i++) {
babc[k++] = _bc[i];
}
return string(babc);
}
function setStorage(address acc, bytes32 key, bytes32 value) external;

function _getCodeHash(address addr) internal view returns (bytes32) {
bytes32 codeHash;
assembly {
codeHash := extcodehash(addr)
}
return codeHash;
}
function incNonce(address acc, uint256 diff) external;
}

contract NodeDriver is Initializable {
Expand Down Expand Up @@ -376,15 +165,3 @@ contract NodeDriver is Initializable {
backend.sealEpoch(offlineTimes, offlineBlocks, uptimes, originatedTxsFee, usedGas);
}
}

interface EVMWriter {
function setBalance(address acc, uint256 value) external;

function copyCode(address acc, address from) external;

function swapCode(address acc, address where) external;

function setStorage(address acc, bytes32 key, bytes32 value) external;

function incNonce(address acc, uint256 diff) external;
}
Loading

0 comments on commit 836f436

Please sign in to comment.