-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathDatabase.sol
150 lines (124 loc) · 4.69 KB
/
Database.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
pragma solidity ^0.4.24;
// @title A shared storage contract for platform contracts to store and retrieve data
// @notice This contract holds all long-term data for smart-contract systems
// @dev The bytes32 hashes are derived from keccak256(variableName, uniqueID) => value
// @dec Can enable upgradeable contracts by setting a contract manager
contract Database{
// Storage Variables
mapping(bytes32 => uint) public uintStorage;
mapping(bytes32 => string) public stringStorage;
mapping(bytes32 => address) public addressStorage;
mapping(bytes32 => bytes) public bytesStorage;
mapping(bytes32 => bytes32) public bytes32Storage;
mapping(bytes32 => bool) public boolStorage;
mapping(bytes32 => int) public intStorage;
// @notice Constructor: Sets the owners of the platform
// @dev Owners must set the contract manager to add more contracts
constructor(address[] _owners, bool _upgradeable)
public {
for(uint i=0; i<_owners.length; i++){
require(_owners[i] != address(0), "Empty address");
boolStorage[keccak256(abi.encodePacked("owner", _owners[i]))] = true;
emit LogInitialized(_owners[i], _upgradeable);
}
if (_upgradeable){
boolStorage[keccak256("upgradeable")] = true;
}
}
// @notice ContractManager will be the only contract that can add/remove contracts on the platform.
// @param (address) _contractManager is the contract which can upgrade/remove contracts to platform
function enableContractManagement(address _contractManager)
external
returns (bool){
require(_contractManager != address(0), "Empty address");
require(boolStorage[keccak256(abi.encodePacked("owner", msg.sender))], "Not owner");
require(addressStorage[keccak256(abi.encodePacked("contract", "ContractManager"))] == address(0), "There is already a contract manager");
addressStorage[keccak256(abi.encodePacked("contract", "ContractManager"))] = _contractManager;
boolStorage[keccak256(abi.encodePacked("contract", _contractManager))] = true;
return true;
}
// @notice Storage functions
function setAddress(bytes32 _key, address _value)
onlyApprovedContract
external {
addressStorage[_key] = _value;
}
function setUint(bytes32 _key, uint _value)
onlyApprovedContract
external {
uintStorage[_key] = _value;
}
function setString(bytes32 _key, string _value)
onlyApprovedContract
external {
stringStorage[_key] = _value;
}
function setBytes(bytes32 _key, bytes _value)
onlyApprovedContract
external {
bytesStorage[_key] = _value;
}
function setBytes32(bytes32 _key, bytes32 _value)
onlyApprovedContract
external {
bytes32Storage[_key] = _value;
}
function setBool(bytes32 _key, bool _value)
onlyApprovedContract
external {
boolStorage[_key] = _value;
}
function setInt(bytes32 _key, int _value)
onlyApprovedContract
external {
intStorage[_key] = _value;
}
// Deletion functions: Can alternatively use setter functions and set to null value (ie. uint = 0)
function deleteAddress(bytes32 _key)
onlyApprovedContract
external {
delete addressStorage[_key];
}
function deleteUint(bytes32 _key)
onlyApprovedContract
external {
delete uintStorage[_key];
}
function deleteString(bytes32 _key)
onlyApprovedContract
external {
delete stringStorage[_key];
}
function deleteBytes(bytes32 _key)
onlyApprovedContract
external {
delete bytesStorage[_key];
}
function deleteBytes32(bytes32 _key)
onlyApprovedContract
external {
delete bytes32Storage[_key];
}
function deleteBool(bytes32 _key)
onlyApprovedContract
external {
delete boolStorage[_key];
}
function deleteInt(bytes32 _key)
onlyApprovedContract
external {
delete intStorage[_key];
}
// --------------------------------------------------------------------------------------
// Modifiers
// --------------------------------------------------------------------------------------
// Caller must be registered as a contract through ContractManager.sol
modifier onlyApprovedContract() {
require(boolStorage[keccak256(abi.encodePacked("contract", msg.sender))]);
_;
}
// --------------------------------------------------------------------------------------
// Events
// --------------------------------------------------------------------------------------
event LogInitialized(address _owner, bool _upgradeable);
}