Skip to content

Commit

Permalink
Merge pull request #40 from ironbeer/fix/environment
Browse files Browse the repository at this point in the history
Update Environment and StakeManager
  • Loading branch information
ironbeer authored Aug 16, 2024
2 parents 3b87148 + 7ba5cd1 commit 4b50259
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 217 deletions.
4 changes: 2 additions & 2 deletions artifacts/contracts/Environment.sol/Environment.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions artifacts/contracts/StakeManager.sol/StakeManager.json

Large diffs are not rendered by default.

124 changes: 3 additions & 121 deletions artifacts/contracts/lib/Constants.sol/Constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,127 +2,9 @@
"_format": "hh-sol-artifact-1",
"contractName": "Constants",
"sourceName": "contracts/lib/Constants.sol",
"abi": [
{
"inputs": [],
"name": "MAX_COMMISSION_RATE",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MAX_REWARD_RATE",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MIN_BLOCK_PERIOD",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MIN_EPOCH_PERIOD",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MIN_JAIL_PERIOD",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MIN_JAIL_THRESHOLD",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MIN_VALIDATOR_THRESHOLD",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "REWARD_PRECISION",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "SECONDS_PER_YEAR",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
],
"bytecode": "0x61010561003a600b82828239805160001a60731461002d57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe730000000000000000000000000000000000000000301460806040526004361060925760003560e01c80635e52aadc1160695780635e52aadc1460b0578063718dc3c3146097578063d8b654371460b0578063e6a69ab81460be578063f6cb1a491460c857600080fd5b8063207239c0146097578063331b8dd41460b05780633d6aa5e11460b75780634bb8e66e1460b0575b600080fd5b609e606481565b60405190815260200160405180910390f35b609e600181565b609e601981565b609e6301e1338081565b609e60038156fea264697066735822122009a5dd82b09f8e6d1938bac92ab88cc0dd3e9056c7159e7035a5c22999c5ec4164736f6c634300080c0033",
"deployedBytecode": "0x730000000000000000000000000000000000000000301460806040526004361060925760003560e01c80635e52aadc1160695780635e52aadc1460b0578063718dc3c3146097578063d8b654371460b0578063e6a69ab81460be578063f6cb1a491460c857600080fd5b8063207239c0146097578063331b8dd41460b05780633d6aa5e11460b75780634bb8e66e1460b0575b600080fd5b609e606481565b60405190815260200160405180910390f35b609e600181565b609e601981565b609e6301e1338081565b609e60038156fea264697066735822122009a5dd82b09f8e6d1938bac92ab88cc0dd3e9056c7159e7035a5c22999c5ec4164736f6c634300080c0033",
"abi": [],
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212201f5b4e58b5faa21b79fc2e9b71f1dd4610b4a85a8963741608724187d55caf8664736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212201f5b4e58b5faa21b79fc2e9b71f1dd4610b4a85a8963741608724187d55caf8664736f6c634300080c0033",
"linkReferences": {},
"deployedLinkReferences": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"contractName": "EnvironmentValue",
"sourceName": "contracts/lib/EnvironmentValue.sol",
"abi": [],
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220d9cdc270d58f16b87227989c1a67eae1b6a1cbb0b37df7b759cb6da892f1200e64736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220d9cdc270d58f16b87227989c1a67eae1b6a1cbb0b37df7b759cb6da892f1200e64736f6c634300080c0033",
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220bb82dd87ae27e455792a05f72a4fdc3a89d2709d9214409a3797952c61a4ac1b64736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220bb82dd87ae27e455792a05f72a4fdc3a89d2709d9214409a3797952c61a4ac1b64736f6c634300080c0033",
"linkReferences": {},
"deployedLinkReferences": {}
}
4 changes: 2 additions & 2 deletions artifacts/contracts/lib/Staker.sol/Staker.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"contractName": "Staker",
"sourceName": "contracts/lib/Staker.sol",
"abi": [],
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212203a94bb500b539a4b8099d6364990f9cacfd9a8f27a2035cef9c6b4513541240464736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212203a94bb500b539a4b8099d6364990f9cacfd9a8f27a2035cef9c6b4513541240464736f6c634300080c0033",
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220de7eebb7d91b814c1c1334bd4296ebebd5f619203f1e2033c6e10d3d63a1709f64736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220de7eebb7d91b814c1c1334bd4296ebebd5f619203f1e2033c6e10d3d63a1709f64736f6c634300080c0033",
"linkReferences": {},
"deployedLinkReferences": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"contractName": "UpdateHistories",
"sourceName": "contracts/lib/UpdateHistories.sol",
"abi": [],
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212200b9340dceb113becd2f06b406bf0957bae99604c2f4071e93c2d5484bf8116c264736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212200b9340dceb113becd2f06b406bf0957bae99604c2f4071e93c2d5484bf8116c264736f6c634300080c0033",
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212208b7db56be1126f222f33e1fc0904b6d50ed8a01665b7abdbe55ff85c704a72a364736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea26469706673582212208b7db56be1126f222f33e1fc0904b6d50ed8a01665b7abdbe55ff85c704a72a364736f6c634300080c0033",
"linkReferences": {},
"deployedLinkReferences": {}
}
4 changes: 2 additions & 2 deletions artifacts/contracts/lib/Validator.sol/Validator.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"contractName": "Validator",
"sourceName": "contracts/lib/Validator.sol",
"abi": [],
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220f9855c52eea35fbf8b308f9a25968d65bd7424b01b95c079322cdd39e384ebc264736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220f9855c52eea35fbf8b308f9a25968d65bd7424b01b95c079322cdd39e384ebc264736f6c634300080c0033",
"bytecode": "0x60566037600b82828239805160001a607314602a57634e487b7160e01b600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea264697066735822122065d788285e45b772c46605898776206be8f8ceacd767be53997c32207f4f5dde64736f6c634300080c0033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea264697066735822122065d788285e45b772c46605898776206be8f8ceacd767be53997c32207f4f5dde64736f6c634300080c0033",
"linkReferences": {},
"deployedLinkReferences": {}
}

This file was deleted.

59 changes: 34 additions & 25 deletions contracts/Environment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ contract Environment is IEnvironment, System {
if (isLastBlock()) revert OnlyNotLastBlock();
if (newValue.startEpoch <= epoch()) revert PastEpoch();

EnvironmentValue storage next = _getNext();
if (next.started(block.number)) {
newValue.startBlock = next.nextStartBlock(newValue);
EnvironmentValue storage latest = _latestValue();
if (latest.started(block.number)) {
newValue.startBlock = latest.nextStartBlock(newValue);
} else {
newValue.startBlock = _getCurrent().nextStartBlock(newValue);
newValue.startBlock = _prevValue().nextStartBlock(newValue);
}
_updateValue(newValue);
}
Expand All @@ -64,40 +64,40 @@ contract Environment is IEnvironment, System {
* @inheritdoc IEnvironment
*/
function epoch() public view returns (uint256) {
EnvironmentValue storage next = _getNext();
return next.started(block.number) ? next.epoch() : _getCurrent().epoch();
return _value().epoch();
}

/**
* @inheritdoc IEnvironment
*/
function isFirstBlock() external view returns (bool) {
return (block.number) % value().epochPeriod == 0;
EnvironmentValue storage current = _value();
return (block.number - current.startBlock) % current.epochPeriod == 0;
}

/**
* @inheritdoc IEnvironment
*/
function isLastBlock() public view returns (bool) {
return (block.number + 1) % value().epochPeriod == 0;
EnvironmentValue storage current = _value();
return (block.number - current.startBlock + 1) % current.epochPeriod == 0;
}

/**
* @inheritdoc IEnvironment
*/
function value() public view returns (EnvironmentValue memory) {
EnvironmentValue storage next = _getNext();
return next.started(block.number) ? next : _getCurrent();
return _value();
}

/**
* @inheritdoc IEnvironment
*/
function nextValue() external view returns (EnvironmentValue memory) {
EnvironmentValue memory current = value();
EnvironmentValue storage next = _getNext();
uint256 nextStartBlock = current.startBlock + (epoch() - current.startEpoch + 1) * current.epochPeriod;
return next.started(nextStartBlock) ? next : current;
EnvironmentValue storage current = _value();
EnvironmentValue storage latest = _latestValue();
uint256 nextStartBlock = current.startBlock + (current.epoch() - current.startEpoch + 1) * current.epochPeriod;
return latest.started(nextStartBlock) ? latest : current;
}

/**
Expand All @@ -112,10 +112,19 @@ contract Environment is IEnvironment, System {
*********************/

/**
* Returns the current (or previous) environment value.
* Returns the environment value at the current epoch
* @return Environment value.
*/
function _value() internal view returns (EnvironmentValue storage) {
EnvironmentValue storage latest = _latestValue();
return latest.started(block.number) ? latest : _prevValue();
}

/**
* Returns the previous (or latest) environment value.
* @return Environment value.
*/
function _getCurrent() internal view returns (EnvironmentValue storage) {
function _prevValue() internal view returns (EnvironmentValue storage) {
uint256 length = values.length;
if (length == 1) {
return values[0];
Expand All @@ -124,27 +133,27 @@ contract Environment is IEnvironment, System {
}

/**
* Returns the next (or current) environment value.
* Returns the latest environment value.
* @return Environment value.
*/
function _getNext() internal view returns (EnvironmentValue storage) {
function _latestValue() internal view returns (EnvironmentValue storage) {
return values[values.length - 1];
}

/**
* Validate the new environment value and if there are no problems, save to storage.
* @param _value New environment value.
* @param newValue New environment value.
*/
function _updateValue(EnvironmentValue memory _value) private {
_value.validate();
function _updateValue(EnvironmentValue memory newValue) private {
newValue.validate();

uint256 length = updates.length;
if (length == 0 || values[length - 1].started(block.number)) {
updates.push(_value.startEpoch);
values.push(_value);
updates.push(newValue.startEpoch);
values.push(newValue);
} else {
updates[length - 1] = _value.startEpoch;
values[length - 1] = _value;
updates[length - 1] = newValue.startEpoch;
values[length - 1] = newValue;
}
}
}
3 changes: 2 additions & 1 deletion contracts/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,8 @@ contract StakeManager is IStakeManager, System {
epoch += 1;
IEnvironment.EnvironmentValue memory env = environment.findValue(epoch);
for (uint256 j = 0; j < length; j++) {
rewards += validators[_validators[j]].getRewards(env, epoch);
(uint256 _rewards, ) = validators[_validators[j]].getRewards(env, epoch);
rewards += _rewards;
}
}

Expand Down
18 changes: 9 additions & 9 deletions contracts/lib/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ pragma solidity 0.8.12;
* @title Constants
*/
library Constants {
uint256 public constant REWARD_PRECISION = 25;
uint256 public constant SECONDS_PER_YEAR = 60 * 60 * 24 * 365;
uint256 public constant MIN_BLOCK_PERIOD = 1;
uint256 public constant MIN_EPOCH_PERIOD = 3;
uint256 public constant MAX_REWARD_RATE = 100;
uint256 public constant MIN_VALIDATOR_THRESHOLD = 1;
uint256 public constant MIN_JAIL_THRESHOLD = 1;
uint256 public constant MIN_JAIL_PERIOD = 1;
uint256 public constant MAX_COMMISSION_RATE = 100;
uint256 internal constant REWARD_PRECISION = 25;
uint256 internal constant SECONDS_PER_YEAR = 60 * 60 * 24 * 365;
uint256 internal constant MIN_BLOCK_PERIOD = 1;
uint256 internal constant MIN_EPOCH_PERIOD = 3;
uint256 internal constant MAX_REWARD_RATE = 100;
uint256 internal constant MIN_VALIDATOR_THRESHOLD = 1;
uint256 internal constant MIN_JAIL_THRESHOLD = 1;
uint256 internal constant MIN_JAIL_PERIOD = 1;
uint256 internal constant MAX_COMMISSION_RATE = 100;
}
4 changes: 2 additions & 2 deletions contracts/lib/Staker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ library Staker {
getStake(staker, validator.owner, Token.Type.sOAS, lastClaim);
if (_stake == 0) continue;

uint256 validatorRewards = validator.getRewardsWithoutCommissions(
(uint256 validatorRewards, uint256 validatorTotalStake) = validator.getRewardsWithoutCommissions(
environment.findValue(lastClaim),
lastClaim
);
Expand All @@ -109,7 +109,7 @@ library Staker {
rewards += Math.share(
validatorRewards,
_stake,
validator.getTotalStake(lastClaim),
validatorTotalStake,
Constants.REWARD_PRECISION
);
}
Expand Down
21 changes: 4 additions & 17 deletions contracts/lib/UpdateHistories.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ library UpdateHistories {
}

function find(
uint256[] memory epochs,
IEnvironment.EnvironmentValue[] memory values,
uint256[] storage epochs,
IEnvironment.EnvironmentValue[] storage values,
uint256 epoch
) internal pure returns (IEnvironment.EnvironmentValue memory) {
) internal view returns (IEnvironment.EnvironmentValue storage) {
uint256 length = epochs.length;
if (epochs[length - 1] <= epoch) return values[length - 1];
if (length > 1 && epochs[length - 2] <= epoch) return values[length - 2];
uint256 idx = mBinarySearch(epochs, epoch, 0, length);
uint256 idx = sBinarySearch(epochs, epoch, 0, length);
return values[idx];
}

Expand Down Expand Up @@ -117,17 +117,4 @@ library UpdateHistories {
if (epochs[center] < epoch) return sBinarySearch(epochs, epoch, center + 1, tail);
return center;
}

function mBinarySearch(
uint256[] memory epochs,
uint256 epoch,
uint256 head,
uint256 tail
) internal pure returns (uint256) {
if (head == tail) return tail - 1;
uint256 center = (head + tail) / 2;
if (epochs[center] > epoch) return mBinarySearch(epochs, epoch, head, center);
if (epochs[center] < epoch) return mBinarySearch(epochs, epoch, center + 1, tail);
return center;
}
}
Loading

0 comments on commit 4b50259

Please sign in to comment.