Skip to content

Commit

Permalink
Merge branch 'sonic_sfc' into mike/custom_errors
Browse files Browse the repository at this point in the history
# Conflicts:
#	contracts/common/ReentrancyGuard.sol
#	contracts/ownership/Ownable.sol
#	contracts/sfc/NodeDriver.sol
#	contracts/sfc/NodeDriverAuth.sol
#	contracts/sfc/SFCBase.sol
#	contracts/sfc/SFCLib.sol
#	contracts/sfc/Updater.sol
#	contracts/test/StubEvmWriter.sol
#	contracts/test/UnitTestSFC.sol
#	test/NodeDriver.ts
#	test/SFC.ts
  • Loading branch information
Mike-CZ committed Oct 24, 2024
2 parents f216130 + 15407b9 commit fad15b2
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 33 deletions.
5 changes: 5 additions & 0 deletions contracts/common/Initializable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ contract Initializable is IErrors {
*/
bool private initializing;

/**
* @dev The contract instance has already been initialized.
*/
error ContractInitialized();

/**
* @dev Modifier to use in the initializer function of a contract.
*/
Expand Down
8 changes: 6 additions & 2 deletions contracts/common/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.9;

import {Initializable} from "./Initializable.sol";
import {IErrors} from "../IErrors.sol";

/**
* @dev Contract module that helps prevent reentrant calls to a function.
Expand All @@ -16,10 +15,15 @@ import {IErrors} from "../IErrors.sol";
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*/
contract ReentrancyGuard is IErrors, Initializable {
contract ReentrancyGuard is Initializable {
// counter to allow mutex lock with only one SSTORE operation
uint256 private _guardCounter;

/**
* @dev Reentrant call.
*/
error ReentrantCall();

function initialize() internal initializer {
// The counter starts at one to prevent changing it from zero to a non-zero
// value, which is a more expensive operation.
Expand Down
14 changes: 14 additions & 0 deletions contracts/interfaces/IEVMWriter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

interface IEvmWriter {
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;
}
17 changes: 13 additions & 4 deletions contracts/ownership/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.9;

import {Initializable} from "../common/Initializable.sol";
import {IErrors} from "../IErrors.sol";

/**
* @dev Contract module which provides a basic access control mechanism, where
Expand All @@ -13,9 +12,19 @@ import {IErrors} from "../IErrors.sol";
* `onlyOwner`, which can be aplied to your functions to restrict their use to
* the owner.
*/
contract Ownable is IErrors, Initializable {
contract Ownable is Initializable {
address private _owner;

/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);

/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);

event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

/**
Expand All @@ -38,7 +47,7 @@ contract Ownable is IErrors, Initializable {
*/
modifier onlyOwner() {
if (!isOwner()) {
revert NotOwner();
revert OwnableUnauthorizedAccount(msg.sender);
}
_;
}
Expand Down Expand Up @@ -75,7 +84,7 @@ contract Ownable is IErrors, Initializable {
*/
function _transferOwnership(address newOwner) internal {
if (newOwner == address(0)) {
revert ZeroAddress();
revert OwnableInvalidOwner(address(0));
}
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
Expand Down
20 changes: 4 additions & 16 deletions contracts/sfc/NodeDriver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,11 @@ pragma solidity ^0.8.9;

import {Initializable} from "../common/Initializable.sol";
import {NodeDriverAuth} from "./NodeDriverAuth.sol";
import {IErrors} from "../IErrors.sol";
import {IEvmWriter} from "../interfaces/IEVMWriter.sol";

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;
}

contract NodeDriver is IErrors, Initializable {
contract NodeDriver is Initializable {
NodeDriverAuth internal backend;
EVMWriter internal evmWriter;
IEvmWriter internal evmWriter;

event UpdatedBackend(address indexed backend);

Expand All @@ -45,7 +33,7 @@ contract NodeDriver is IErrors, Initializable {
function initialize(address _backend, address _evmWriterAddress) external initializer {
backend = NodeDriverAuth(_backend);
emit UpdatedBackend(_backend);
evmWriter = EVMWriter(_evmWriterAddress);
evmWriter = IEvmWriter(_evmWriterAddress);
}

function setBalance(address acc, uint256 value) external onlyBackend {
Expand Down
5 changes: 5 additions & 0 deletions contracts/sfc/SFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ contract SFC is SFCBase, Version {
return getEpochSnapshot[epoch].offlineBlocks[validatorID];
}

function getEpochEndBlock(uint256 epoch) public view returns (uint256) {
return getEpochSnapshot[epoch].endBlock;
}

function rewardsStash(address delegator, uint256 validatorID) public view returns (uint256) {
Rewards memory stash = _rewardsStash[delegator][validatorID];
return stash.lockupBaseReward + stash.lockupExtraReward + stash.unlockedReward;
Expand Down Expand Up @@ -389,6 +393,7 @@ contract SFC is SFCBase, Version {

currentSealedEpoch = currentEpoch();
snapshot.endTime = _now();
snapshot.endBlock = block.number;
snapshot.baseRewardPerSecond = c.baseRewardPerSecond();
snapshot.totalSupply = totalSupply;
}
Expand Down
3 changes: 3 additions & 0 deletions contracts/sfc/SFCI.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface SFCI {
view
returns (
uint256 endTime,
uint256 endBlock,
uint256 epochFee,
uint256 totalBaseRewardWeight,
uint256 totalTxRewardWeight,
Expand Down Expand Up @@ -137,6 +138,8 @@ interface SFCI {

function getEpochOfflineBlocks(uint256 epoch, uint256 validatorID) external view returns (uint256);

function getEpochEndBlock(uint256 epoch) external view returns (uint256);

function rewardsStash(address delegator, uint256 validatorID) external view returns (uint256);

function getLockedStake(address delegator, uint256 toValidatorID) external view returns (uint256);
Expand Down
1 change: 1 addition & 0 deletions contracts/sfc/SFCState.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ contract SFCState is Initializable, Ownable {
mapping(uint256 => uint256) offlineBlocks;
uint256[] validatorIDs;
uint256 endTime;
uint256 endBlock;
uint256 epochFee;
uint256 totalBaseRewardWeight;
uint256 totalTxRewardWeight;
Expand Down
4 changes: 2 additions & 2 deletions contracts/test/StubEvmWriter.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

import {EVMWriter} from "../sfc/NodeDriver.sol";
import {IEvmWriter} from "../interfaces/IEVMWriter.sol";

contract StubEvmWriter is EVMWriter {
contract StubEvmWriter is IEvmWriter {
function setBalance(address acc, uint256 value) external {}

function copyCode(address acc, address from) external {}
Expand Down
3 changes: 3 additions & 0 deletions contracts/test/UnitTestSFC.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ interface SFCUnitTestI is IErrors {
view
returns (
uint256 endTime,
uint256 endBlock,
uint256 epochFee,
uint256 totalBaseRewardWeight,
uint256 totalTxRewardWeight,
Expand Down Expand Up @@ -219,6 +220,8 @@ interface SFCUnitTestI is IErrors {

function getEpochOfflineBlocks(uint256 epoch, uint256 validatorID) external view returns (uint256);

function getEpochEndBlock(uint256 epoch) external view returns (uint256);

function rewardsStash(address delegator, uint256 validatorID) external view returns (uint256);

function getLockedStake(address delegator, uint256 toValidatorID) external view returns (uint256);
Expand Down
8 changes: 4 additions & 4 deletions test/NodeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('NodeDriver', () => {
const account = ethers.Wallet.createRandom();
await expect(this.nodeDriverAuth.connect(this.nonOwner).migrateTo(account)).to.be.revertedWithCustomError(
this.nodeDriverAuth,
'NotOwner',
'OwnableUnauthorizedAccount',
);
});
});
Expand All @@ -55,7 +55,7 @@ describe('NodeDriver', () => {
const address = ethers.Wallet.createRandom();
await expect(
this.nodeDriverAuth.connect(this.nonOwner).copyCode(this.sfc, address),
).to.be.revertedWithCustomError(this.nodeDriverAuth, 'NotOwner');
).to.be.revertedWithCustomError(this.nodeDriverAuth, 'OwnableUnauthorizedAccount');
});
});

Expand All @@ -69,7 +69,7 @@ describe('NodeDriver', () => {
it('Should revert when not owner', async function () {
await expect(this.nodeDriverAuth.connect(this.nonOwner).updateNetworkVersion(1)).to.be.revertedWithCustomError(
this.nodeDriverAuth,
'NotOwner',
'OwnableUnauthorizedAccount',
);
});
});
Expand All @@ -82,7 +82,7 @@ describe('NodeDriver', () => {
it('Should revert when not owner', async function () {
await expect(this.nodeDriverAuth.connect(this.nonOwner).advanceEpochs(10)).to.be.revertedWithCustomError(
this.nodeDriverAuth,
'NotOwner',
'OwnableUnauthorizedAccount',
);
});
});
Expand Down
19 changes: 14 additions & 5 deletions test/SFC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,14 @@ describe('SFC', () => {
it('Should revert when transferring ownership if not owner', async function () {
await expect(this.sfc.connect(this.user).transferOwnership(ethers.ZeroAddress)).to.be.revertedWithCustomError(
this.nodeDriverAuth,
'NotOwner',
'OwnableUnauthorizedAccount',
);
});

it('Should revert when transferring ownership to zero address', async function () {
await expect(this.sfc.transferOwnership(ethers.ZeroAddress)).to.be.revertedWithCustomError(
this.nodeDriverAuth,
'ZeroAddress',
);
await expect(this.sfc.transferOwnership(ethers.ZeroAddress))
.to.be.revertedWithCustomError(this.nodeDriverAuth, 'OwnableInvalidOwner')
.withArgs(ethers.ZeroAddress);
});
});

Expand Down Expand Up @@ -414,6 +413,16 @@ describe('SFC', () => {
expect(await this.sfc.currentEpoch.call()).to.equal(6);
expect(await this.sfc.currentSealedEpoch()).to.equal(5);
});

it('Should succeed and return endBlock', async function () {
const epochNumber = await this.sfc.currentEpoch();
await this.sfc.enableNonNodeCalls();
await this.sfc.sealEpoch([100, 101, 102], [100, 101, 102], [100, 101, 102], [100, 101, 102], 0);
const lastBlock = await ethers.provider.getBlockNumber();
// endBlock is on second position
expect((await this.sfc.getEpochSnapshot(epochNumber))[1]).to.equal(lastBlock);
expect(await this.sfc.getEpochEndBlock(epochNumber)).to.equal(lastBlock);
});
});
});

Expand Down

0 comments on commit fad15b2

Please sign in to comment.