forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEvents.sol
54 lines (47 loc) · 1.82 KB
/
Events.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0;
// Example on specifying events. Users of the contract can add register a number
// corresponding to their address and then update it but only with a greater number.
// Events keep track of such changes in the data and the verifier checks whether
// they are properly emitted.
contract Registry{
struct Entry {
bool set;
int data;
}
mapping(address=>Entry) entries;
/// @notice tracks-changes-in entries
/// @notice precondition !entries[at].set
/// @notice postcondition entries[at].set && entries[at].data == value
event new_entry(address at, int value);
/// @notice tracks-changes-in entries
/// @notice precondition entries[at].set && entries[at].data < value
/// @notice postcondition entries[at].set && entries[at].data == value
event updated_entry(address at, int value);
/// @notice emits new_entry
function add(int value) public {
require(!entries[msg.sender].set);
entries[msg.sender].set = true;
entries[msg.sender].data = value;
emit new_entry(msg.sender, value);
}
/// @notice emits updated_entry
function update(int value) public {
require(entries[msg.sender].set);
require(entries[msg.sender].data < value);
entries[msg.sender].data = value;
emit updated_entry(msg.sender, value);
}
/// @notice emits new_entry
/// @notice emits updated_entry
function add_or_update(int value) public {
require(!entries[msg.sender].set || entries[msg.sender].data < value);
entries[msg.sender].data = value;
if (entries[msg.sender].set) {
emit updated_entry(msg.sender, value);
} else {
entries[msg.sender].set = true;
emit new_entry(msg.sender, value);
}
}
}