forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Storage.sol
59 lines (51 loc) · 1.81 KB
/
Storage.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0;
// A simple storage example, where each user can set, update
// or clear their data (represented as an integer) in the
// storage. The owner can clear any data.
contract Storage {
struct Record {
int data;
bool set;
}
mapping(address=>Record) records;
address owner;
constructor() {
owner = msg.sender;
}
// Only owner can change the owner
/// @notice modifies owner if msg.sender == owner
function changeOwner(address newOwner) public {
require(msg.sender == owner);
owner = newOwner;
}
function isSet(Record storage record) internal view returns(bool) {
return record.set;
}
/// @notice modifies records[msg.sender] if !records[msg.sender].set
/// @notice postcondition records[msg.sender].set
/// @notice postcondition records[msg.sender].data == data
function set(int data) public {
require(!isSet(records[msg.sender]));
Record memory rec = Record(data, true);
records[msg.sender] = rec;
}
/// @notice modifies records[msg.sender].data if records[msg.sender].set
/// @notice postcondition records[msg.sender].data == data
function update(int data) public {
Record storage rec = records[msg.sender];
require(isSet(rec));
rec.data = data;
}
// Anyone can modify their record, but owner can modify any record
/// @notice modifies records[msg.sender]
/// @notice modifies records if msg.sender == owner
/// @notice postcondition !records[at].set
/// @notice postcondition records[at].data == 0
function clear(address at) public {
require(msg.sender == owner || msg.sender == at);
Record storage rec = records[at];
rec.set = false;
rec.data = 0;
}
}