Skip to content

Commit 1e89ce3

Browse files
authored
feat: Use namespaced storage (#236)
2 parents 59f7eaa + b1c3c62 commit 1e89ce3

23 files changed

+458
-372
lines changed

contracts/Store.sol

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,92 @@ import "./registries/IRegistry.sol";
1717
/****************************************************************************
1818
* WARNING: Be carefull when editing this file. *
1919
* *
20-
* If you want add new variables for expanded features, add them at the *
21-
* end, or (better?) create a Store_v2 that inherits from this Store. *
20+
* If you want to add new variables, add them to the end of the *
21+
* struct `PocoStorage`. *
22+
* Read more about: *
23+
* - Diamond proxy storage https://eips.ethereum.org/EIPS/eip-2535 *
24+
* - Namespaced storage https://eips.ethereum.org/EIPS/eip-7201 *
2225
* *
23-
* If in doubt, read about Diamond proxy storage. *
2426
****************************************************************************/
2527

26-
// TODO replace with diamond AppStorage using namespaced storage.
27-
// TODO check storage padding.
2828
abstract contract Store {
29-
// Registries
30-
IRegistry internal m_appregistry;
31-
IRegistry internal m_datasetregistry;
32-
IRegistry internal m_workerpoolregistry;
29+
// Poco - Constants
30+
uint256 public constant CONTRIBUTION_DEADLINE_RATIO = 7;
31+
uint256 public constant REVEAL_DEADLINE_RATIO = 2;
32+
uint256 public constant FINAL_DEADLINE_RATIO = 10;
33+
uint256 public constant WORKERPOOL_STAKE_RATIO = 30;
34+
uint256 public constant KITTY_RATIO = 10;
35+
uint256 public constant KITTY_MIN = 1e9; // ADJUSTEMENT VARIABLE
3336

34-
// Escrow
35-
IERC20 internal m_baseToken;
36-
string internal m_name;
37-
string internal m_symbol;
38-
uint8 internal m_decimals;
39-
uint256 internal m_totalSupply;
40-
mapping(address => uint256) internal m_balances;
41-
mapping(address => uint256) internal m_frozens;
42-
mapping(address => mapping(address => uint256)) internal m_allowances;
37+
// Seized funds of workerpools that do not honor their deals are sent
38+
// out to this kitty address.
39+
// It is determined with address(uint256(keccak256(bytes('iExecKitty'))) - 1).
40+
address public constant KITTY_ADDRESS = 0x99c2268479b93fDe36232351229815DF80837e23;
4341

44-
// Poco - Constants
45-
uint256 internal constant CONTRIBUTION_DEADLINE_RATIO = 7;
46-
uint256 internal constant REVEAL_DEADLINE_RATIO = 2;
47-
uint256 internal constant FINAL_DEADLINE_RATIO = 10;
48-
uint256 internal constant WORKERPOOL_STAKE_RATIO = 30;
49-
uint256 internal constant KITTY_RATIO = 10;
50-
uint256 internal constant KITTY_MIN = 1000000000; // ADJUSTEMENT VARIABLE
51-
address internal constant KITTY_ADDRESS = 0x99c2268479b93fDe36232351229815DF80837e23; // address(uint256(keccak256(bytes('iExecKitty'))) - 1);
52-
uint256 internal constant GROUPMEMBER_PURPOSE = 4;
53-
bytes32 internal EIP712DOMAIN_SEPARATOR;
42+
// Used with ERC-734 Key Manager identity contract for authorization management.
43+
uint256 public constant GROUPMEMBER_PURPOSE = 4;
5444

55-
// Poco - Storage
56-
mapping(bytes32 => address) internal m_presigned; // per order
57-
mapping(bytes32 => uint256) internal m_consumed; // per order
58-
mapping(bytes32 => IexecLibCore_v5.Deal) internal m_deals; // per deal
59-
mapping(bytes32 => IexecLibCore_v5.Task) internal m_tasks; // per task
60-
mapping(bytes32 => IexecLibCore_v5.Consensus) internal m_consensus; // per task
61-
mapping(bytes32 => mapping(address => IexecLibCore_v5.Contribution)) internal m_contributions; // per task-worker
62-
mapping(address => uint256) internal m_workerScores; // per worker
45+
// keccak256(abi.encode(uint256(keccak256("iexec.poco.storage.PocoStorage")) - 1)) & ~bytes32(uint256(0xff));
46+
bytes32 private constant POCO_STORAGE_LOCATION =
47+
0x5862653c6982c162832160cf30593645e8487b257e44d77cdd6b51eee2651b00;
6348

64-
// Poco - Settings
65-
address internal m_teebroker;
66-
uint256 internal m_callbackgas;
49+
/// @custom:storage-location erc7201:iexec.poco.storage.PocoStorage
50+
struct PocoStorage {
51+
// Registries
52+
IRegistry m_appregistry;
53+
IRegistry m_datasetregistry;
54+
IRegistry m_workerpoolregistry;
55+
// Escrow
56+
IERC20 m_baseToken;
57+
string m_name;
58+
string m_symbol;
59+
uint8 m_decimals;
60+
uint256 m_totalSupply;
61+
// In order to use the protocol, users have to deposit RLC
62+
// and allow PoCo smart contracts to manage them. This state
63+
// variable keeps track of users balances.
64+
mapping(address => uint256) m_balances;
65+
// When a deal is created, the protocol temporarily locks an amount
66+
// of RLC tokens from the balances of both the requester and the workerpool owners.
67+
// This is to guarantee the payment of different actors later. Frozen funds
68+
// are released when the computation is completed and the result is pushed.
69+
mapping(address => uint256) m_frozens;
70+
mapping(address => mapping(address => uint256)) m_allowances;
71+
// EIP-712 domain hash.
72+
// Modified in IexecConfigurationFacet.updateDomainSeparator
73+
bytes32 EIP712DOMAIN_SEPARATOR; // TODO rename
74+
// Poco - Storage
6775

68-
// Categories
69-
IexecLibCore_v5.Category[] internal m_categories;
76+
// Mapping an order hash to its owner. Since a smart contract cannot sign orders
77+
// with a private key, it adds an entry to this mapping to provide presigned orders.
78+
mapping(bytes32 => address) m_presigned;
79+
// Each order has a volume (>=1). This tracks how much is consumed from
80+
// the volume of each order. Mapping an order hash to its consumed amount.
81+
mapping(bytes32 => uint256) m_consumed;
82+
// a mapping to store PoCo classic deals.
83+
mapping(bytes32 => IexecLibCore_v5.Deal) m_deals;
84+
mapping(bytes32 => IexecLibCore_v5.Task) m_tasks; // per task
85+
mapping(bytes32 => IexecLibCore_v5.Consensus) m_consensus; // per task
86+
mapping(bytes32 => mapping(address => IexecLibCore_v5.Contribution)) m_contributions; // per task-worker
87+
mapping(address => uint256) m_workerScores; // per worker
88+
// Poco - Settings
89+
// Address of a trusted TEE authority that manages enclave challenges.
90+
// Modified in IexecConfigurationFacet.setTeeBroker
91+
address m_teebroker;
92+
// Max amount of gas to be used with callbacks.
93+
// Modified in IexecConfigurationFacet.setCallbackGas
94+
uint256 m_callbackgas;
95+
// List of defined computation categories.
96+
IexecLibCore_v5.Category[] m_categories;
97+
// Backward compatibility
98+
// Modified in IexecConfigurationFacet.configure
99+
IexecHubInterface m_v3_iexecHub;
100+
mapping(address => bool) m_v3_scoreImported;
101+
}
70102

71-
// Backward compatibility
72-
IexecHubInterface internal m_v3_iexecHub;
73-
mapping(address => bool) internal m_v3_scoreImported;
103+
function getPocoStorage() internal pure returns (PocoStorage storage $) {
104+
assembly {
105+
$_slot := POCO_STORAGE_LOCATION
106+
}
107+
}
74108
}

contracts/Store.v8.sol

Lines changed: 90 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -11,144 +11,100 @@ import {IexecLibCore_v5} from "./libs/IexecLibCore_v5.sol";
1111
/****************************************************************************
1212
* WARNING: Be carefull when editing this file. *
1313
* *
14-
* If you want add new variables for expanded features, add them at the *
15-
* end, or (better?) create a Store_v2 that inherits from this Store. *
14+
* If you want to add new variables, add them to the end of the *
15+
* struct `PocoStorage`. *
16+
* Read more about: *
17+
* - Diamond proxy storage https://eips.ethereum.org/EIPS/eip-2535 *
18+
* - Namespaced storage https://eips.ethereum.org/EIPS/eip-7201 *
1619
* *
17-
* If in doubt, read about Diamond proxy storage. *
1820
****************************************************************************/
1921

20-
/// @dev registries
21-
interface IRegistry is IERC721Enumerable {
22-
function isRegistered(address _entry) external view returns (bool);
23-
}
24-
25-
/// @dev Poco store
26-
/**
27-
* @title Central storage of all modules contracts. It follows the Diamond
28-
* pattern aka ERC-2535.
29-
* @dev note the new added state variable "m_dealsBoost" that holds a new type
30-
* of deals for the PoCo Boost workflow.
31-
*/
32-
// TODO replace with diamond AppStorage using namespaced storage.
33-
// TODO check storage padding.
3422
abstract contract Store {
35-
// Registries
36-
//slither-disable-next-line constable-states
37-
IRegistry internal m_appregistry;
38-
//slither-disable-next-line constable-states
39-
IRegistry internal m_datasetregistry;
40-
//slither-disable-next-line constable-states
41-
IRegistry internal m_workerpoolregistry;
42-
43-
// Escrow
44-
//slither-disable-next-line constable-states
45-
IERC20 internal m_baseToken;
46-
//slither-disable-next-line constable-states
47-
string internal m_name;
48-
//slither-disable-next-line constable-states
49-
string internal m_symbol;
50-
//slither-disable-next-line constable-states
51-
uint8 internal m_decimals;
52-
//slither-disable-next-line constable-states
53-
uint256 internal m_totalSupply;
54-
55-
/**
56-
* @dev In order to use the protocol, users have to deposit RLC
57-
* and allow PoCo smart contracts to manage them. This state
58-
* variable keeps track of users balances.
59-
*/
60-
mapping(address => uint256) internal m_balances;
61-
62-
/**
63-
* @dev When a deal is created, the protocol temporarily locks an amount
64-
* of RLC tokens from the balances of both the requester and the workerpool owners.
65-
* This is to guarantee the payment of different actors later. Frozen funds
66-
* are released when the computation is completed and the result is pushed.
67-
*/
68-
mapping(address => uint256) internal m_frozens;
69-
70-
mapping(address => mapping(address => uint256)) internal m_allowances;
71-
7223
// Poco - Constants
73-
uint256 internal constant CONTRIBUTION_DEADLINE_RATIO = 7;
74-
uint256 internal constant REVEAL_DEADLINE_RATIO = 2;
75-
uint256 internal constant FINAL_DEADLINE_RATIO = 10;
76-
uint256 internal constant WORKERPOOL_STAKE_RATIO = 30;
77-
uint256 internal constant KITTY_RATIO = 10;
78-
uint256 internal constant KITTY_MIN = 1e9; // ADJUSTEMENT VARIABLE
79-
80-
/**
81-
* @dev Seized funds of workerpools that do not honor their deals are sent
82-
* out to this kitty address.
83-
* It is determined with address(uint256(keccak256(bytes('iExecKitty'))) - 1).
84-
*/
85-
address internal constant KITTY_ADDRESS = 0x99c2268479b93fDe36232351229815DF80837e23;
86-
87-
/**
88-
* @dev Used with ERC-734 Key Manager identity contract for authorization management.
89-
*/
90-
uint256 internal constant GROUPMEMBER_PURPOSE = 4;
91-
92-
/**
93-
* @dev EIP-712 domain hash.
94-
*/
95-
// Modified in IexecConfigurationFacet.updateDomainSeparator
96-
//slither-disable-next-line constable-states
97-
bytes32 internal EIP712DOMAIN_SEPARATOR;
98-
99-
// Poco - Storage
100-
101-
/**
102-
* @dev Mapping an order hash to its owner. Since a smart contract cannot sign orders
103-
* with a private key, it adds an entry to this mapping to provide presigned orders.
104-
*/
105-
mapping(bytes32 => address) internal m_presigned;
106-
107-
/**
108-
* @dev Each order has a volume (>=1). This tracks how much is consumed from
109-
* the volume of each order. Mapping an order hash to its consumed amount.
110-
*/
111-
mapping(bytes32 => uint256) internal m_consumed;
112-
113-
/**
114-
* @dev a mapping to store PoCo classic deals.
115-
*/
116-
mapping(bytes32 => IexecLibCore_v5.Deal) internal m_deals;
117-
118-
mapping(bytes32 => IexecLibCore_v5.Task) internal m_tasks; // per task
119-
mapping(bytes32 => IexecLibCore_v5.Consensus) internal m_consensus; // per task
120-
mapping(bytes32 => mapping(address => IexecLibCore_v5.Contribution)) internal m_contributions; // per task-worker
121-
mapping(address => uint256) internal m_workerScores; // per worker
122-
123-
// Poco - Settings
124-
125-
/**
126-
* @dev Address of a trusted TEE authority that manages enclave challenges.
127-
*/
128-
// Modified in IexecConfigurationFacet.setTeeBroker
129-
//slither-disable-next-line constable-states
130-
address internal m_teebroker;
131-
132-
/**
133-
* @dev Max amount of gas to be used with callbacks.
134-
*/
135-
// Modified in IexecConfigurationFacet.setCallbackGas
136-
//slither-disable-next-line constable-states
137-
uint256 internal m_callbackgas;
138-
139-
/**
140-
* @dev List of defined computation categories.
141-
*/
142-
IexecLibCore_v5.Category[] internal m_categories;
143-
144-
// Backward compatibility
145-
// Modified in IexecConfigurationFacet.configure
146-
//slither-disable-next-line constable-states
147-
address internal m_v3_iexecHub; // IexecHubInterface
148-
mapping(address => bool) internal m_v3_scoreImported;
24+
uint256 public constant CONTRIBUTION_DEADLINE_RATIO = 7;
25+
uint256 public constant REVEAL_DEADLINE_RATIO = 2;
26+
uint256 public constant FINAL_DEADLINE_RATIO = 10;
27+
uint256 public constant WORKERPOOL_STAKE_RATIO = 30;
28+
uint256 public constant KITTY_RATIO = 10;
29+
uint256 public constant KITTY_MIN = 1e9; // ADJUSTEMENT VARIABLE
30+
31+
// Seized funds of workerpools that do not honor their deals are sent
32+
// out to this kitty address.
33+
// It is determined with address(uint256(keccak256(bytes('iExecKitty'))) - 1).
34+
address public constant KITTY_ADDRESS = 0x99c2268479b93fDe36232351229815DF80837e23;
35+
36+
// Used with ERC-734 Key Manager identity contract for authorization management.
37+
uint256 public constant GROUPMEMBER_PURPOSE = 4;
38+
39+
// keccak256(abi.encode(uint256(keccak256("iexec.poco.storage.PocoStorage")) - 1)) & ~bytes32(uint256(0xff));
40+
bytes32 private constant POCO_STORAGE_LOCATION =
41+
0x5862653c6982c162832160cf30593645e8487b257e44d77cdd6b51eee2651b00;
42+
43+
/// @custom:storage-location erc7201:iexec.poco.storage.PocoStorage
44+
struct PocoStorage {
45+
// Registries
46+
IRegistry m_appregistry;
47+
IRegistry m_datasetregistry;
48+
IRegistry m_workerpoolregistry;
49+
// Escrow
50+
IERC20 m_baseToken;
51+
string m_name;
52+
string m_symbol;
53+
uint8 m_decimals;
54+
uint256 m_totalSupply;
55+
// In order to use the protocol, users have to deposit RLC
56+
// and allow PoCo smart contracts to manage them. This state
57+
// variable keeps track of users balances.
58+
mapping(address => uint256) m_balances;
59+
// When a deal is created, the protocol temporarily locks an amount
60+
// of RLC tokens from the balances of both the requester and the workerpool owners.
61+
// This is to guarantee the payment of different actors later. Frozen funds
62+
// are released when the computation is completed and the result is pushed.
63+
mapping(address => uint256) m_frozens;
64+
mapping(address => mapping(address => uint256)) m_allowances;
65+
// EIP-712 domain hash.
66+
// Modified in IexecConfigurationFacet.updateDomainSeparator
67+
bytes32 EIP712DOMAIN_SEPARATOR; // TODO rename
68+
// Poco - Storage
69+
70+
// Mapping an order hash to its owner. Since a smart contract cannot sign orders
71+
// with a private key, it adds an entry to this mapping to provide presigned orders.
72+
mapping(bytes32 => address) m_presigned;
73+
// Each order has a volume (>=1). This tracks how much is consumed from
74+
// the volume of each order. Mapping an order hash to its consumed amount.
75+
mapping(bytes32 => uint256) m_consumed;
76+
// a mapping to store PoCo classic deals.
77+
mapping(bytes32 => IexecLibCore_v5.Deal) m_deals;
78+
mapping(bytes32 => IexecLibCore_v5.Task) m_tasks; // per task
79+
mapping(bytes32 => IexecLibCore_v5.Consensus) m_consensus; // per task
80+
mapping(bytes32 => mapping(address => IexecLibCore_v5.Contribution)) m_contributions; // per task-worker
81+
mapping(address => uint256) m_workerScores; // per worker
82+
// Poco - Settings
83+
// Address of a trusted TEE authority that manages enclave challenges.
84+
// Modified in IexecConfigurationFacet.setTeeBroker
85+
address m_teebroker;
86+
// Max amount of gas to be used with callbacks.
87+
// Modified in IexecConfigurationFacet.setCallbackGas
88+
uint256 m_callbackgas;
89+
// List of defined computation categories.
90+
IexecLibCore_v5.Category[] m_categories;
91+
// Backward compatibility
92+
// Modified in IexecConfigurationFacet.configure
93+
address m_v3_iexecHub; // IexecHubInterface
94+
mapping(address => bool) m_v3_scoreImported;
95+
// /!\ New storage variables not present in v6 store.
96+
// A mapping to store PoCo Boost deals.
97+
mapping(bytes32 => IexecLibCore_v5.DealBoost) m_dealsBoost;
98+
}
99+
100+
function getPocoStorage() internal pure returns (PocoStorage storage $) {
101+
assembly ("memory-safe") {
102+
$.slot := POCO_STORAGE_LOCATION
103+
}
104+
}
105+
}
149106

150-
/**
151-
* @dev A mapping to store PoCo Boost deals.
152-
*/
153-
mapping(bytes32 => IexecLibCore_v5.DealBoost) internal m_dealsBoost;
107+
// Use in registries.
108+
interface IRegistry is IERC721Enumerable {
109+
function isRegistered(address _entry) external view returns (bool);
154110
}

0 commit comments

Comments
 (0)