Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base-contract extended storage: alternatives #32

Open
wants to merge 17 commits into
base: branch_v.1.1.2r
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions contracts/Gateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// SPDX-License-Identifier: Apache2.0
pragma solidity 0.8.4;

import {BytesToTypes} from "./lib/BytesToTypes.sol";
import {Memory} from "./lib/Memory.sol";
import {IParamSubscriber} from "./interface/IParamSubscriber.sol";
import {System} from "./System.sol";

/* @dev storing latest platform-contract addresses thus allowing a community process
(rather than 'node voting') to determine the platform-contracts to be used
*/
contract Gateway is System, IParamSubscriber {

uint constant private VALUE_LENGTH = 32;

struct PlatformAddresses {
address validator;
address slashIndicator;
address systemReward;
address lightClient;
address relayerHub;
address candidateHub;
address govHub;
address pledgeAgent;
address burn;
address foundation;
}

error ZeroAddressNotAllowed();

event paramChange(string key, bytes value);

PlatformAddresses public s_addresses;

// @dev using this approach we need to somehow pass the current addresses to the Gateway,
// preferrably in the constructor. An alternative would be to issue a community
// proposal for the Gateway initialize and use the updateParam() route
constructor(PlatformAddresses memory addresses) {
s_addresses = addresses;
}

function updateParam(string calldata key, bytes calldata value) external override onlyInit onlyGov {
address addr = _bytesToAddress(key, value, true); // new version of a platform contract

if (_eq(key, "validator")) {
s_addresses.validator = addr;
} else if (_eq(key, "slashIndicator")) {
s_addresses.slashIndicator = addr;
} else if (_eq(key, "systemReward")) {
s_addresses.systemReward = addr;
} else if (_eq(key, "lightClient")) {
s_addresses.lightClient = addr;
} else if (_eq(key, "relayerHub")) {
s_addresses.relayerHub = addr;
} else if (_eq(key, "candidateHub")) {
s_addresses.candidateHub = addr;
} else if (_eq(key, "govHub")) {
s_addresses.govHub = addr;
} else if (_eq(key, "pledgeAgent")) {
s_addresses.pledgeAgent = addr;
} else if (_eq(key, "burn")) {
s_addresses.burn = addr;
} else if (_eq(key, "foundation")) {
s_addresses.foundation = addr;
} else {
revert("unknown param");
}
emit paramChange(key, value);
}

function init() external onlyNotInit {
alreadyInit = true; // avoid onlyInit() modifier reverting
}

function _bytesToAddress(string calldata key, bytes calldata value, bool notZero) private pure returns (address addr) {
if (value.length != VALUE_LENGTH) {
revert MismatchParamLength(key);
}
addr = BytesToTypes.bytesToAddress(VALUE_LENGTH, value);
if (notZero && addr == address(0)) {
revert ZeroAddressNotAllowed();
}
}

function _eq(string memory s1, string memory s2) private pure returns (bool){
return Memory.compareStrings(s1, s2);
}
}
5 changes: 5 additions & 0 deletions contracts/GovHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ contract GovHub is System, IParamSubscriber {
uint256 public constant VOTING_PERIOD = 201600;
uint256 public constant EXECUTING_PERIOD = 201600;
bytes public constant INIT_MEMBERS = hex"f86994548e6acce441866674e04ab84587af2d394034c094bb06d463bc143eecc4a0cfa35e0346d5690fa9f694e2fe60f349c6e1a85caad1d22200c289da40dc1294b198db68258f06e79d415a0998be7f9b38ea722694dd173b85f306128f1b10d7d7219059c28c6d6c09";

bytes32 public constant _EXTDATA = keccak256("GovHub.extended.data.position");

uint256 public proposalMaxOperations;
uint256 public votingPeriod;
Expand Down Expand Up @@ -117,6 +119,9 @@ contract GovHub is System, IParamSubscriber {
bytes[] memory calldatas,
string memory description
) public onlyInit onlyMember returns (uint256) {

_workWithExtData(_EXTDATA, 12 ether); // @dev sample usage

require(
targets.length == values.length && targets.length == signatures.length && targets.length == calldatas.length,
"proposal function information arity mismatch"
Expand Down
32 changes: 32 additions & 0 deletions contracts/System.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,40 @@ import "./interface/IRelayerHub.sol";

contract System {

uint constant private GAP_SIZE = 200;

bool public alreadyInit;

//@dev approach1: use gap to maintain layout's backward compatibility
uint[GAP_SIZE-0] private __gap;

//@dev approach2: use positional extended-data for base class
struct ExtendedSystemData {
address newAddr;
uint newInt;
uint someValue;
mapping(address => uint) someMap;
// @dev no need to maintain gap here, simply append new data when needed
}

// @dev note that this approach uses assembly code rather than storage slot,
// hence zero impact on the contract's sequential layout
function _extSystemData(bytes32 position) internal pure returns (ExtendedSystemData storage _data) {
assembly {
_data.slot := position
}
}

function _workWithExtData(bytes32 position, uint param) internal {
//@dev note the base-class semantics of this flow: same logic with different storage locations
ExtendedSystemData storage sref_extData = _extSystemData(position);

// @dev sref_ is a storage ptr so set commands will work
address address_value = sref_extData.newAddr; address_value;
sref_extData.someValue = 22 ether;
sref_extData.someMap[msg.sender] = param;
}


address public constant VALIDATOR_CONTRACT_ADDR = 0x0000000000000000000000000000000000001000;
address public constant SLASH_CONTRACT_ADDR = 0x0000000000000000000000000000000000001001;
Expand Down