Skip to content

Commit b023328

Browse files
author
Mike-CZ
committed
Merge branch 'sonic_sfc' into mike/old_redirects
# Conflicts: # contracts/common/Initializable.sol # contracts/common/ReentrancyGuard.sol # contracts/ownership/Ownable.sol # contracts/sfc/NodeDriver.sol # contracts/sfc/NodeDriverAuth.sol # contracts/sfc/SFCBase.sol # contracts/sfc/Updater.sol # contracts/test/StubEvmWriter.sol # contracts/test/UnitTestSFC.sol # test/NodeDriver.ts # test/SFC.ts
2 parents 9bf3af6 + 846e556 commit b023328

15 files changed

+210
-79
lines changed

contracts/common/Initializable.sol

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// SPDX-License-Identifier: UNLICENSED
22
pragma solidity ^0.8.9;
33

4-
import {IErrors} from "../IErrors.sol";
5-
64
/**
75
* @title Initializable
86
*
@@ -15,7 +13,7 @@ import {IErrors} from "../IErrors.sol";
1513
* a parent initializer twice, or ensure that all initializers are idempotent,
1614
* because this is not dealt with automatically as with constructors.
1715
*/
18-
contract Initializable is IErrors {
16+
contract Initializable {
1917
/**
2018
* @dev Indicates that the contract has been initialized.
2119
*/
@@ -26,12 +24,17 @@ contract Initializable is IErrors {
2624
*/
2725
bool private initializing;
2826

27+
/**
28+
* @dev The contract instance has already been initialized.
29+
*/
30+
error InvalidInitialization();
31+
2932
/**
3033
* @dev Modifier to use in the initializer function of a contract.
3134
*/
3235
modifier initializer() {
3336
if (!initializing && initialized) {
34-
revert ContractInitialized();
37+
revert InvalidInitialization();
3538
}
3639

3740
bool isTopLevelCall = !initializing;

contracts/common/ReentrancyGuard.sol

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
pragma solidity ^0.8.9;
33

44
import {Initializable} from "./Initializable.sol";
5-
import {IErrors} from "../IErrors.sol";
65

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

22+
/**
23+
* @dev Reentrant call.
24+
*/
25+
error ReentrancyGuardReentrantCall();
26+
2327
function initialize() internal initializer {
2428
// The counter starts at one to prevent changing it from zero to a non-zero
2529
// value, which is a more expensive operation.
@@ -38,7 +42,7 @@ contract ReentrancyGuard is IErrors, Initializable {
3842
uint256 localCounter = _guardCounter;
3943
_;
4044
if (localCounter != _guardCounter) {
41-
revert ReentrantCall();
45+
revert ReentrancyGuardReentrantCall();
4246
}
4347
}
4448

contracts/interfaces/IEVMWriter.sol

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.9;
3+
4+
interface IEvmWriter {
5+
function setBalance(address acc, uint256 value) external;
6+
7+
function copyCode(address acc, address from) external;
8+
9+
function swapCode(address acc, address where) external;
10+
11+
function setStorage(address acc, bytes32 key, bytes32 value) external;
12+
13+
function incNonce(address acc, uint256 diff) external;
14+
}

contracts/ownership/Ownable.sol

+13-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
pragma solidity ^0.8.9;
33

44
import {Initializable} from "../common/Initializable.sol";
5-
import {IErrors} from "../IErrors.sol";
65

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

18+
/**
19+
* @dev The caller account is not authorized to perform an operation.
20+
*/
21+
error OwnableUnauthorizedAccount(address account);
22+
23+
/**
24+
* @dev The owner is not a valid owner account. (eg. `address(0)`)
25+
*/
26+
error OwnableInvalidOwner(address owner);
27+
1928
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
2029

2130
/**
@@ -38,7 +47,7 @@ contract Ownable is IErrors, Initializable {
3847
*/
3948
modifier onlyOwner() {
4049
if (!isOwner()) {
41-
revert NotOwner();
50+
revert OwnableUnauthorizedAccount(msg.sender);
4251
}
4352
_;
4453
}
@@ -75,7 +84,7 @@ contract Ownable is IErrors, Initializable {
7584
*/
7685
function _transferOwnership(address newOwner) internal {
7786
if (newOwner == address(0)) {
78-
revert ZeroAddress();
87+
revert OwnableInvalidOwner(address(0));
7988
}
8089
emit OwnershipTransferred(_owner, newOwner);
8190
_owner = newOwner;

contracts/sfc/NodeDriver.sol

+7-16
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,14 @@ pragma solidity ^0.8.9;
33

44
import {Initializable} from "../common/Initializable.sol";
55
import {NodeDriverAuth} from "./NodeDriverAuth.sol";
6-
import {IErrors} from "../IErrors.sol";
6+
import {IEvmWriter} from "../interfaces/IEVMWriter.sol";
77

8-
interface EVMWriter {
9-
function setBalance(address acc, uint256 value) external;
10-
11-
function copyCode(address acc, address from) external;
12-
13-
function swapCode(address acc, address where) external;
14-
15-
function setStorage(address acc, bytes32 key, bytes32 value) external;
16-
17-
function incNonce(address acc, uint256 diff) external;
18-
}
19-
20-
contract NodeDriver is IErrors, Initializable {
8+
contract NodeDriver is Initializable {
219
NodeDriverAuth internal backend;
22-
EVMWriter internal evmWriter;
10+
IEvmWriter internal evmWriter;
11+
12+
error NotNode();
13+
error NotBackend();
2314

2415
event UpdatedBackend(address indexed backend);
2516

@@ -45,7 +36,7 @@ contract NodeDriver is IErrors, Initializable {
4536
function initialize(address _backend, address _evmWriterAddress) external initializer {
4637
backend = NodeDriverAuth(_backend);
4738
emit UpdatedBackend(_backend);
48-
evmWriter = EVMWriter(_evmWriterAddress);
39+
evmWriter = IEvmWriter(_evmWriterAddress);
4940
}
5041

5142
function setBalance(address acc, uint256 value) external onlyBackend {

contracts/sfc/NodeDriverAuth.sol

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ import {Initializable} from "../common/Initializable.sol";
55
import {Ownable} from "../ownership/Ownable.sol";
66
import {SFCI} from "./SFCI.sol";
77
import {NodeDriver} from "./NodeDriver.sol";
8-
import {IErrors} from "../IErrors.sol";
98

109
interface NodeDriverExecutable {
1110
function execute() external;
1211
}
1312

14-
contract NodeDriverAuth is IErrors, Initializable, Ownable {
13+
contract NodeDriverAuth is Initializable, Ownable {
1514
SFCI internal sfc;
1615
NodeDriver internal driver;
1716

17+
error NotSFC();
18+
error NotDriver();
19+
error NotContract();
20+
error SelfCodeHashMismatch();
21+
error DriverCodeHashMismatch();
22+
error RecipientNotSFC();
23+
1824
// Initialize NodeDriverAuth, NodeDriver and SFC in one call to allow fewer genesis transactions
1925
function initialize(address payable _sfc, address _driver, address _owner) external initializer {
2026
Ownable.initialize(_owner);

contracts/sfc/SFC.sol

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ contract SFC is SFCBase, Version {
8282
return getEpochSnapshot[epoch].offlineBlocks[validatorID];
8383
}
8484

85+
function getEpochEndBlock(uint256 epoch) public view returns (uint256) {
86+
return getEpochSnapshot[epoch].endBlock;
87+
}
88+
8589
function rewardsStash(address delegator, uint256 validatorID) public view returns (uint256) {
8690
Rewards memory stash = _rewardsStash[delegator][validatorID];
8791
return stash.lockupBaseReward + stash.lockupExtraReward + stash.unlockedReward;
@@ -389,6 +393,7 @@ contract SFC is SFCBase, Version {
389393

390394
currentSealedEpoch = currentEpoch();
391395
snapshot.endTime = _now();
396+
snapshot.endBlock = block.number;
392397
snapshot.baseRewardPerSecond = c.baseRewardPerSecond();
393398
snapshot.totalSupply = totalSupply;
394399
}

contracts/sfc/SFCBase.sol

+72-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,85 @@ pragma solidity ^0.8.9;
33

44
import {Decimal} from "../common/Decimal.sol";
55
import {SFCState} from "./SFCState.sol";
6-
import {IErrors} from "../IErrors.sol";
76

8-
contract SFCBase is IErrors, SFCState {
7+
contract SFCBase is SFCState {
98
uint256 internal constant OK_STATUS = 0;
109
uint256 internal constant WITHDRAWN_BIT = 1;
1110
uint256 internal constant OFFLINE_BIT = 1 << 3;
1211
uint256 internal constant DOUBLESIGN_BIT = 1 << 7;
1312
uint256 internal constant CHEATER_MASK = DOUBLESIGN_BIT;
1413

14+
// auth
15+
error NotDriverAuth();
16+
error NotAuthorized();
17+
18+
// addresses
19+
error ZeroAddress();
20+
error SameAddress();
21+
22+
// values
23+
error ZeroAmount();
24+
error ZeroRewards();
25+
26+
// pubkeys
27+
error PubkeyExists();
28+
error MalformedPubkey();
29+
error SamePubkey();
30+
error EmptyPubkey();
31+
error PubkeyAllowedOnlyOnce();
32+
33+
// redirections
34+
error SameRedirectionAuthorizer();
35+
error Redirected();
36+
37+
// validators
38+
error ValidatorNotExists();
39+
error ValidatorExists();
40+
error ValidatorNotActive();
41+
error ValidatorDelegationLimitExceeded();
42+
error WrongValidatorStatus();
43+
44+
// requests
45+
error RequestedCompleted();
46+
error RequestExists();
47+
error RequestNotExists();
48+
49+
// transfers
50+
error TransfersNotAllowed();
51+
error TransferFailed();
52+
53+
// updater
54+
error SFCAlreadyUpdated();
55+
error SFCWrongVersion();
56+
error SFCGovAlreadyUpdated();
57+
error SFCWrongGovVersion();
58+
59+
// governance
60+
error GovVotesRecountFailed();
61+
62+
// staking
63+
error LockedStakeGreaterThanTotalStake();
64+
error InsufficientSelfStake();
65+
error NotEnoughUnlockedStake();
66+
error NotEnoughLockedStake();
67+
error NotEnoughTimePassed();
68+
error NotEnoughEpochsPassed();
69+
error StakeIsFullySlashed();
70+
error IncorrectDuration();
71+
error ValidatorLockupTooShort();
72+
error TooManyReLocks();
73+
error TooFrequentReLocks();
74+
error LockupDurationDecreased();
75+
error AlreadyLockedUp();
76+
error NotLockedUp();
77+
78+
// stashing
79+
error NothingToStash();
80+
81+
// slashing
82+
error ValidatorNotSlashed();
83+
error RefundRatioTooHigh();
84+
1585
event DeactivatedValidator(uint256 indexed validatorID, uint256 deactivatedEpoch, uint256 deactivatedTime);
1686
event ChangedValidatorStatus(uint256 indexed validatorID, uint256 status);
1787

contracts/sfc/SFCI.sol

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ interface SFCI {
4444
view
4545
returns (
4646
uint256 endTime,
47+
uint256 endBlock,
4748
uint256 epochFee,
4849
uint256 totalBaseRewardWeight,
4950
uint256 totalTxRewardWeight,
@@ -137,6 +138,8 @@ interface SFCI {
137138

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

141+
function getEpochEndBlock(uint256 epoch) external view returns (uint256);
142+
140143
function rewardsStash(address delegator, uint256 validatorID) external view returns (uint256);
141144

142145
function getLockedStake(address delegator, uint256 toValidatorID) external view returns (uint256);

contracts/sfc/SFCState.sol

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ contract SFCState is Initializable, Ownable {
7272
mapping(uint256 => uint256) offlineBlocks;
7373
uint256[] validatorIDs;
7474
uint256 endTime;
75+
uint256 endBlock;
7576
uint256 epochFee;
7677
uint256 totalBaseRewardWeight;
7778
uint256 totalTxRewardWeight;

contracts/sfc/Updater.sol

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {ConstantsManager} from "./ConstantsManager.sol";
88
import {SFC} from "./SFC.sol";
99
import {SFCI} from "./SFCI.sol";
1010
import {Version} from "../version/Version.sol";
11-
import {IErrors} from "../IErrors.sol";
1211

1312
interface GovI {
1413
function upgrade(address v) external;
@@ -22,7 +21,7 @@ interface GovVersion {
2221
function version() external pure returns (bytes4);
2322
}
2423

25-
contract Updater is IErrors {
24+
contract Updater {
2625
address public sfcFrom;
2726
address public sfcLib;
2827
address public sfcConsts;
@@ -31,6 +30,12 @@ contract Updater is IErrors {
3130
address public voteBook;
3231
address public owner;
3332

33+
error ZeroAddress();
34+
error SFCAlreadyUpdated();
35+
error SFCWrongVersion();
36+
error SFCGovAlreadyUpdated();
37+
error SFCWrongGovVersion();
38+
3439
constructor(
3540
address _sfcFrom,
3641
address _sfcLib,

contracts/test/StubEvmWriter.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// SPDX-License-Identifier: UNLICENSED
22
pragma solidity ^0.8.9;
33

4-
import {EVMWriter} from "../sfc/NodeDriver.sol";
4+
import {IEvmWriter} from "../interfaces/IEVMWriter.sol";
55

6-
contract StubEvmWriter is EVMWriter {
6+
contract StubEvmWriter is IEvmWriter {
77
function setBalance(address acc, uint256 value) external {}
88

99
function copyCode(address acc, address from) external {}

0 commit comments

Comments
 (0)