Skip to content

Commit

Permalink
Merge branch 'master' into gas_golf_stamp
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCardinalError authored Sep 4, 2023
2 parents 9a07565 + acab296 commit 47f96e6
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 93 deletions.
43 changes: 42 additions & 1 deletion src/PostageStamp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,43 @@ contract PostageStamp is AccessControl, Pausable {
error OnlyPauser(); // Only Pauser role can pause or unpause contracts
error OnlyRedistributor(); // Only redistributor role can withdraw from the contract



// ----------------------------- Events ------------------------------

/**
* @dev Emitted when a new batch is created.
*/
event BatchCreated(
bytes32 indexed batchId,
uint256 totalAmount,
uint256 normalisedBalance,
address owner,
uint8 depth,
uint8 bucketDepth,
bool immutableFlag
);

/**
* @dev Emitted when an pot is Withdrawn.
*/
event PotWithdrawn(address recipient, uint256 totalAmount);

/**
* @dev Emitted when an existing batch is topped up.
*/
event BatchTopUp(bytes32 indexed batchId, uint256 topupAmount, uint256 normalisedBalance);

/**
* @dev Emitted when the depth of an existing batch increases.
*/
event BatchDepthIncrease(bytes32 indexed batchId, uint8 newDepth, uint256 normalisedBalance);

/**
*@dev Emitted on every price update.
*/
event PriceUpdate(uint256 price);

// ----------------------------- CONSTRUCTOR ------------------------------

/**
Expand Down Expand Up @@ -222,7 +259,7 @@ contract PostageStamp is AccessControl, Pausable {
}

/**
* @notice Manually create a new batch when faciliatating migration, can only be called by the Admin role.
* @notice Manually create a new batch when facilitating migration, can only be called by the Admin role.
* @dev At least `_initialBalancePerChunk*2^depth` tokens must be approved in the ERC20 token contract.
* @param _owner Owner of the new batch.
* @param _initialBalancePerChunk Initial balance per chunk of the batch.
Expand Down Expand Up @@ -264,6 +301,7 @@ contract PostageStamp is AccessControl, Pausable {
revert ZeroBalance();
}


validChunkCount += 1 << _depth;

batches[_batchId] = Batch({
Expand Down Expand Up @@ -391,6 +429,7 @@ contract PostageStamp is AccessControl, Pausable {
}

minimumValidityBlocks = uint64(_value);

}

/**
Expand Down Expand Up @@ -488,6 +527,7 @@ contract PostageStamp is AccessControl, Pausable {
* The contract can be provably stopped by renouncing the pauser role and the admin role once paused.
*/
function pause() public {

if (!hasRole(PAUSER_ROLE, msg.sender)) {
revert OnlyPauser();
}
Expand All @@ -502,6 +542,7 @@ contract PostageStamp is AccessControl, Pausable {
if (!hasRole(PAUSER_ROLE, msg.sender)) {
revert OnlyPauser();
}

_unpause();
}

Expand Down
54 changes: 32 additions & 22 deletions src/PriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ import "./interface/IPostageStamp.sol";
*/

contract PriceOracle is AccessControl {
/**
*@dev Emitted on every price update.
*/
event PriceUpdate(uint256 price);
// ----------------------------- State variables ------------------------------

// Role allowed to update price
bytes32 public constant PRICE_UPDATER_ROLE = keccak256("PRICE_UPDATER");
Expand All @@ -36,11 +33,24 @@ contract PriceOracle is AccessControl {
// The address of the linked PostageStamp contract
IPostageStamp public postageStamp;

// ----------------------------- Events ------------------------------

/**
*@dev Emitted on every price update.
*/
event PriceUpdate(uint256 price);

// ----------------------------- CONSTRUCTOR ------------------------------

constructor(address _postageStamp, address multisig) {
_setupRole(DEFAULT_ADMIN_ROLE, multisig);
postageStamp = IPostageStamp(_postageStamp);
}

////////////////////////////////////////
// SETTERS //
////////////////////////////////////////

/**
* @notice Manually set the price.
* @dev Can only be called by the admin role.
Expand All @@ -59,24 +69,6 @@ contract PriceOracle is AccessControl {
emit PriceUpdate(currentPrice);
}

/**
* @notice Pause the contract.
* @dev Can only be called by the admin role.
*/
function pause() external {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "caller is not the admin");
isPaused = true;
}

/**
* @notice Unpause the contract.
* @dev Can only be called by the admin role.
*/
function unPause() external {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "caller is not the admin");
isPaused = false;
}

/**
* @notice Automatically adjusts the price, called from the Redistribution contract
* @dev The ideal redundancy in Swarm is 4 nodes per neighbourhood. Each round, the
Expand Down Expand Up @@ -126,4 +118,22 @@ contract PriceOracle is AccessControl {
emit PriceUpdate(currentPrice);
}
}

/**
* @notice Pause the contract.
* @dev Can only be called by the admin role.
*/
function pause() external {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "caller is not the admin");
isPaused = true;
}

/**
* @notice Unpause the contract.
* @dev Can only be called by the admin role.
*/
function unPause() external {
require(hasRole(DEFAULT_ADMIN_ROLE, msg.sender), "caller is not the admin");
isPaused = false;
}
}
157 changes: 87 additions & 70 deletions src/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,7 @@ import "@openzeppelin/contracts/security/Pausable.sol";
*/

contract StakeRegistry is AccessControl, Pausable {
/**
* @dev Emitted when a stake is created or updated by `owner` of the `overlay` by `stakeamount`, during `lastUpdatedBlock`.
*/
event StakeUpdated(bytes32 indexed overlay, uint256 stakeAmount, address owner, uint256 lastUpdatedBlock);

/**
* @dev Emitted when a stake for overlay `slashed` is slashed by `amount`.
*/
event StakeSlashed(bytes32 slashed, uint256 amount);

/**
* @dev Emitted when a stake for overlay `frozen` for `time` blocks.
*/
event StakeFrozen(bytes32 slashed, uint256 time);
// ----------------------------- Type declarations ------------------------------

struct Stake {
// Overlay of the node that is being staked
Expand All @@ -42,6 +29,8 @@ contract StakeRegistry is AccessControl, Pausable {
bool isValue;
}

// ----------------------------- State variables ------------------------------

// Associate every stake id with overlay data.
mapping(bytes32 => Stake) public stakes;

Expand All @@ -56,75 +45,39 @@ contract StakeRegistry is AccessControl, Pausable {
// Address of the staked ERC20 token
address public bzzToken;

/**
* @param _bzzToken Address of the staked ERC20 token
* @param _NetworkId Swarm network ID
*/
constructor(address _bzzToken, uint64 _NetworkId, address multisig) {
NetworkId = _NetworkId;
bzzToken = _bzzToken;
_setupRole(DEFAULT_ADMIN_ROLE, multisig);
_setupRole(PAUSER_ROLE, msg.sender);
}
// ----------------------------- Events ------------------------------

/**
* @dev Checks to see if `overlay` is frozen.
* @param overlay Overlay of staked overlay
*
* Returns a boolean value indicating whether the operation succeeded.
* @dev Emitted when a stake is created or updated by `owner` of the `overlay` by `stakeamount`, during `lastUpdatedBlock`.
*/
function overlayNotFrozen(bytes32 overlay) internal view returns (bool) {
return stakes[overlay].lastUpdatedBlockNumber < block.number;
}
event StakeUpdated(bytes32 indexed overlay, uint256 stakeAmount, address owner, uint256 lastUpdatedBlock);

/**
* @dev Returns the current `stakeAmount` of `overlay`.
* @param overlay Overlay of node
* @dev Emitted when a stake for overlay `slashed` is slashed by `amount`.
*/
function stakeOfOverlay(bytes32 overlay) public view returns (uint256) {
return stakes[overlay].stakeAmount;
}
event StakeSlashed(bytes32 slashed, uint256 amount);

/**
* @dev Returns the current usable `stakeAmount` of `overlay`.
* Checks whether the stake is currently frozen.
* @param overlay Overlay of node
* @dev Emitted when a stake for overlay `frozen` for `time` blocks.
*/
function usableStakeOfOverlay(bytes32 overlay) public view returns (uint256) {
return overlayNotFrozen(overlay) ? stakes[overlay].stakeAmount : 0;
}
event StakeFrozen(bytes32 slashed, uint256 time);

/**
* @dev Returns the `lastUpdatedBlockNumber` of `overlay`.
*/
function lastUpdatedBlockNumberOfOverlay(bytes32 overlay) public view returns (uint256) {
return stakes[overlay].lastUpdatedBlockNumber;
}
// ----------------------------- CONSTRUCTOR ------------------------------

/**
* @dev Returns the eth address of the owner of `overlay`.
* @param overlay Overlay of node
* @param _bzzToken Address of the staked ERC20 token
* @param _NetworkId Swarm network ID
*/
function ownerOfOverlay(bytes32 overlay) public view returns (address) {
return stakes[overlay].owner;
constructor(address _bzzToken, uint64 _NetworkId, address multisig) {
NetworkId = _NetworkId;
bzzToken = _bzzToken;
_setupRole(DEFAULT_ADMIN_ROLE, multisig);
_setupRole(PAUSER_ROLE, msg.sender);
}

/**
* @dev Please both Endians 🥚.
* @param input Eth address used for overlay calculation.
*/
function reverse(uint64 input) internal pure returns (uint64 v) {
v = input;

// swap bytes
v = ((v & 0xFF00FF00FF00FF00) >> 8) | ((v & 0x00FF00FF00FF00FF) << 8);

// swap 2-byte long pairs
v = ((v & 0xFFFF0000FFFF0000) >> 16) | ((v & 0x0000FFFF0000FFFF) << 16);

// swap 4-byte long pairs
v = (v >> 32) | (v << 32);
}
////////////////////////////////////////
// SETTERS //
////////////////////////////////////////

/**
* @notice Create a new stake or update an existing one.
Expand Down Expand Up @@ -190,8 +143,8 @@ contract StakeRegistry is AccessControl, Pausable {
require(hasRole(REDISTRIBUTOR_ROLE, msg.sender), "only redistributor can freeze stake");

if (stakes[overlay].isValue) {
emit StakeFrozen(overlay, time);
stakes[overlay].lastUpdatedBlockNumber = block.number + time;
emit StakeFrozen(overlay, time);
}
}

Expand All @@ -202,14 +155,15 @@ contract StakeRegistry is AccessControl, Pausable {
*/
function slashDeposit(bytes32 overlay, uint256 amount) external {
require(hasRole(REDISTRIBUTOR_ROLE, msg.sender), "only redistributor can slash stake");
emit StakeSlashed(overlay, amount);

if (stakes[overlay].isValue) {
if (stakes[overlay].stakeAmount > amount) {
stakes[overlay].stakeAmount -= amount;
stakes[overlay].lastUpdatedBlockNumber = block.number;
} else {
delete stakes[overlay];
}
emit StakeSlashed(overlay, amount);
}
}

Expand All @@ -229,4 +183,67 @@ contract StakeRegistry is AccessControl, Pausable {
require(hasRole(PAUSER_ROLE, msg.sender), "only pauser can unpause");
_unpause();
}

////////////////////////////////////////
// GETTERS //
////////////////////////////////////////

/**
* @dev Checks to see if `overlay` is frozen.
* @param overlay Overlay of staked overlay
*
* Returns a boolean value indicating whether the operation succeeded.
*/
function overlayNotFrozen(bytes32 overlay) internal view returns (bool) {
return stakes[overlay].lastUpdatedBlockNumber < block.number;
}

/**
* @dev Returns the current `stakeAmount` of `overlay`.
* @param overlay Overlay of node
*/
function stakeOfOverlay(bytes32 overlay) public view returns (uint256) {
return stakes[overlay].stakeAmount;
}

/**
* @dev Returns the current usable `stakeAmount` of `overlay`.
* Checks whether the stake is currently frozen.
* @param overlay Overlay of node
*/
function usableStakeOfOverlay(bytes32 overlay) public view returns (uint256) {
return overlayNotFrozen(overlay) ? stakes[overlay].stakeAmount : 0;
}

/**
* @dev Returns the `lastUpdatedBlockNumber` of `overlay`.
*/
function lastUpdatedBlockNumberOfOverlay(bytes32 overlay) public view returns (uint256) {
return stakes[overlay].lastUpdatedBlockNumber;
}

/**
* @dev Returns the eth address of the owner of `overlay`.
* @param overlay Overlay of node
*/
function ownerOfOverlay(bytes32 overlay) public view returns (address) {
return stakes[overlay].owner;
}

/**
* @dev Please both Endians 🥚.
* @param input Eth address used for overlay calculation.
*/
function reverse(uint64 input) internal pure returns (uint64 v) {
v = input;

// swap bytes
v = ((v & 0xFF00FF00FF00FF00) >> 8) | ((v & 0x00FF00FF00FF00FF) << 8);

// swap 2-byte long pairs
v = ((v & 0xFFFF0000FFFF0000) >> 16) | ((v & 0x0000FFFF0000FFFF) << 16);

// swap 4-byte long pairs
v = (v >> 32) | (v << 32);
}
}

0 comments on commit 47f96e6

Please sign in to comment.