-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloFromGradeFour.sol
128 lines (107 loc) · 3.86 KB
/
HelloFromGradeFour.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
pragma solidity ^0.4.0;
// A darkmatteryyc.ca experiment created using the Regis tool by ConsenSys - https://regis.nu
// Messages from Grade 4 students to their future selves
// This is the base contract that your contract HelloFromGradeFour extends from.
contract BaseRegistry {
// The owner of this registry.
address public owner = msg.sender;
uint public creationTime = now;
// This struct keeps all data for a Record.
struct Record {
// Keeps the address of this record creator.
address owner;
// Keeps the time when this record was created.
uint time;
// Keeps the index of the keys array for fast lookup
uint keysIndex;
string message;
}
// This mapping keeps the records of this Registry.
mapping(uint32 => Record) records;
// Keeps the total numbers of records in this Registry.
uint public numRecords;
// Keeps a list of all keys to interate the records.
uint32[] public keys;
modifier onlyOwner {
if (msg.sender != owner) throw;
_;
}
// This is the function that actually insert a record.
function register(uint32 key, string message) onlyOwner {
if (records[key].time == 0) {
records[key].time = now;
records[key].owner = msg.sender;
records[key].keysIndex = keys.length;
keys.length++;
keys[keys.length - 1] = key;
records[key].message = message;
numRecords++;
} else {
throw;
}
}
// Updates the values of the given record.
function update(uint32 key, string message) {
// Only the owner can update his record.
if (records[key].owner == msg.sender) {
records[key].message = message;
}
}
// Unregister a given record
function unregister(uint32 key) {
if (records[key].owner == msg.sender) {
uint keysIndex = records[key].keysIndex;
delete records[key];
numRecords--;
keys[keysIndex] = keys[keys.length - 1];
records[keys[keysIndex]].keysIndex = keysIndex;
keys.length--;
}
}
// Transfer ownership of a given record.
function transfer(uint32 key, address newOwner) {
if (records[key].owner == msg.sender) {
records[key].owner = newOwner;
} else {
throw;
}
}
// Tells whether a given key is registered.
function isRegistered(uint32 key) returns(bool) {
return records[key].time != 0;
}
function getRecordAtIndex(uint rindex) returns(uint32 key, address owner, uint time, string message) {
Record record = records[keys[rindex]];
key = keys[rindex];
owner = record.owner;
time = record.time;
message = record.message;
}
function getRecord(uint32 key) returns(address owner, uint time, string message) {
Record record = records[key];
owner = record.owner;
time = record.time;
message = record.message;
}
// Returns the owner of the given record. The owner could also be get
// by using the function getRecord but in that case all record attributes
// are returned.
function getOwner(uint32 key) returns(address) {
return records[key].owner;
}
// Returns the registration time of the given record. The time could also
// be get by using the function getRecord but in that case all record attributes
// are returned.
function getTime(uint32 key) returns(uint) {
return records[key].time;
}
// Registry owner can use this function to withdraw any value owned by
// the registry.
function withdraw(address to, uint value) onlyOwner {
if (!to.send(value)) throw;
}
function kill() onlyOwner {
suicide(owner);
}
}
contract HelloFromGradeFour is BaseRegistry {}