-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDirectoryContract.sol
181 lines (155 loc) · 6.09 KB
/
DirectoryContract.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
contract DirectoryContractV3{
address owner;
uint RelayUploadPeriod = 60; // 1 hour
uint16 relayIndex = 1;
uint16[] public unhealthyRelaySet;
uint8 CurrentCounter = 1;
mapping (uint8 => bool) CounterOP;
mapping (uint8 => uint16[]) CounterList;
struct Relay{
bool registered;
bool healthflag;
uint8 counter;
bytes Hdr;
bytes encryptedSRI;
bytes NSD;
}
mapping (uint16 => Relay) relay;
mapping (address => uint16) relayaddr;
constructor(){
owner = msg.sender;
}
fallback() external payable{}
receive() external payable {}
// ************************
// Relay Method
// ************************
// Relay registration check.
function relay_registration_check() public view returns(uint16) {
if(relay[relayaddr[msg.sender]].registered == true) {
return relayaddr[msg.sender];
} else {
return 0;
}
}
// Relay registration.
function relay_register() public {
require(relay[relayaddr[msg.sender]].registered == false);
relayaddr[msg.sender] = relayIndex;
relay[relayIndex].registered = true;
relay[relayIndex].healthflag = true;
CounterOP[CurrentCounter] = true;
CounterList[CurrentCounter] = [relayIndex];
relayIndex++;
CurrentCounter++;
}
// Relay gets SRI upload period (in minutes).
function relay_get_upload_period() view public returns(uint) {
require(relay[relayaddr[msg.sender]].registered == true);
return RelayUploadPeriod;
}
// Relay gets the subset S of relays used for broadcast encryption.
function relay_get_index_set() view public returns(uint16, uint16[] memory) {
require(relay[relayaddr[msg.sender]].registered == true);
return (relayIndex, unhealthyRelaySet);
}
// Relay gets the current counter.
function relay_get_current_counter() view public returns(uint8) {
require(relay[relayaddr[msg.sender]].registered == true);
return CurrentCounter;
}
// Relay incentive mechanism.
function contract_transfer_to_relay(address payable addr) public payable {
addr.transfer(0.0005 * 10**18); //Transaction Fee (Gwei)
}
// Relay uploads counter, Hdr, encrypted SRI, and the plaintext NSD.
function relay_upload_SRI_and_NSD(uint8 _counter, bytes memory _Hdr, bytes memory _encryptedSRI, bytes memory _NSD) public {
require(relay[relayaddr[msg.sender]].healthflag == true);
uint16 index = relayaddr[msg.sender];
relay[index].counter = _counter;
relay[index].Hdr = _Hdr;
relay[index].encryptedSRI = _encryptedSRI;
relay[index].NSD = _NSD;
// contract_transfer_to_relay(msg.sender);
}
// Relay downloads SRI (Loop download).
function relay_download_SRI(uint16 i) view public returns(uint8, bytes memory, bytes memory) {
require(relay[relayaddr[msg.sender]].registered == true);
require(i < relayIndex && relay[i].healthflag == true);
return(relay[i].counter, relay[i].Hdr, relay[i].encryptedSRI);
}
// Relay gets the historical list of counter.
function relay_get_counter_list(uint8 counter) view public returns(bool[] memory, uint16[][] memory) {
require(relay[relayaddr[msg.sender]].registered == true);
require(counter < CurrentCounter);
bool[] memory operation = new bool[](CurrentCounter-counter);
uint16[][] memory alterSet = new uint16[][](CurrentCounter-counter);
for(uint8 i=counter; i<CurrentCounter; i++) {
operation[CurrentCounter-i-1] = CounterOP[i];
alterSet[CurrentCounter-i-1] = CounterList[i];
}
return (operation, alterSet);
}
// Relay cancellation.
function relay_cancellation() public {
require(relay[relayaddr[msg.sender]].registered == true);
delete relay[relayaddr[msg.sender]];
delete relayaddr[msg.sender];
}
// ************************
// Client Method
// ************************
// Client downloads NSD.
function client_download_NSD() view public returns(bool[] memory, bytes memory) {
bool[] memory NSDIndex = new bool[](relayIndex-1);
bytes memory NSDbytes;
for(uint16 i=1; i<relayIndex; i++) {
if(relay[i].healthflag == true) {
NSDIndex[i-1] = true;
NSDbytes = abi.encodePacked(NSDbytes, relay[i].NSD);
}
else {
NSDIndex[i] = false;
}
}
return (NSDIndex, NSDbytes);
}
// ************************
// Manager Method
// ************************
// Set relay SRI upload period (in minutes).
function setSRIperiod(uint period) public {
require(msg.sender == owner);
RelayUploadPeriod = period;
}
// Get the current index number.
function getrelayIndex() view public returns(uint16) {
require(msg.sender == owner);
return relayIndex;
}
// Get the information about one relay.
function getinfo(uint16 i) view public returns(uint8, bytes memory, bytes memory, bytes memory) {
require(msg.sender == owner);
return (relay[i].counter, relay[i].Hdr, relay[i].encryptedSRI, relay[i].NSD);
}
// Get healthflag of a relay.
function getHealthFlag(uint16 i) view public returns(bool) {
require(msg.sender == owner);
return relay[i].healthflag;
}
// Actively modify healthflag of an unhealthy relay to *FALSE*.
function modifyHealthFlag(uint16 size, uint16[] memory list) public {
require(msg.sender == owner);
for(uint16 i=0; i<size; i++) {
require(relay[list[i]].healthflag == true);
relay[list[i]].healthflag = false;
unhealthyRelaySet.push(list[i]);
}
CounterOP[CurrentCounter] = false;
CounterList[CurrentCounter] = list;
CurrentCounter++;
}
}