-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddresses.sol
415 lines (346 loc) · 16.2 KB
/
addresses.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Belgian addresses
* @dev Manage belgian addresses and their location
*/
contract BelgianAddresses {
/***************************************************************************************************************/
/* STRUCTURES */
/***************************************************************************************************************/
/**
* @dev Data of an address
*/
struct StreetAddress {
bytes32 addressId;
string streetName;
string postcode;
string houseNumber;
string boxNumber;
string latitude;
string longitude;
}
/***************************************************************************************************************/
/* STATE */
/***************************************************************************************************************/
/**
* Map used to contain all addresses
*/
mapping(bytes32 => StreetAddress) public belgianAddresses;
/**
* Map used to list all address ids by postcode
*/
mapping(string => bytes32[]) addressIdsByPostcode;
/**
* List of all available postcodes
*/
string [] postcodes;
/***************************************************************************************************************/
/* PERMISSIONS STATE */
/***************************************************************************************************************/
address private owner;
/***************************************************************************************************************/
/* EVENTS */
/***************************************************************************************************************/
/**
* Event fired when an address is created.
*/
event AddressCreated (bytes32 indexed newAddressId);
/**
* Event fired when an address is removed.
*/
event AddressRemoved (bytes32 indexed oldAddressId, StreetAddress oldAddress);
/**
* Event fired when an address is updated
*/
event AddressUpdated (bytes32 indexed oldAddressId, StreetAddress oldAddress, bytes32 indexed newAddressId);
/**
* Event fired when maintainer is changed
*/
event OwnerSet(address indexed oldOwner, address indexed newOwner);
/***************************************************************************************************************/
/* MODIFIERS */
/***************************************************************************************************************/
// modifier to check if caller is owner
modifier isOwner() {
// If the first argument of 'require' evaluates to 'false', execution terminates and all
// changes to the state and to Ether balances are reverted.
// This used to consume all gas in old EVM versions, but not anymore.
// It is often a good idea to use 'require' to check if functions are called correctly.
// As a second argument, you can also provide an explanation about what went wrong.
require(msg.sender == owner, "Caller is not owner");
_;
}
/***************************************************************************************************************/
/* CONSTRUCTOR */
/***************************************************************************************************************/
/**
* @dev Set contract deployer as owner
*/
constructor() {
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
emit OwnerSet(address(0), owner);
}
/***************************************************************************************************************/
/* PUBLIC STATE METHODS */
/***************************************************************************************************************/
/**
* Add an address to the registry. The address ID is computed by the function.
* Fires an AddressCrated event.
* Returns the id of the new address.
*/
function createAddress (
string memory streetName,
string memory postcode,
string memory houseNumber,
string memory boxNumber,
string memory latitude,
string memory longitude) public isOwner returns (bytes32) {
// Create address
StreetAddress memory newAddress;
newAddress.streetName = streetName;
newAddress.postcode = postcode;
newAddress.houseNumber = houseNumber;
newAddress.boxNumber = boxNumber;
newAddress.latitude = latitude;
newAddress.longitude = longitude;
bytes32 id = _createAddress(newAddress);
// Emit event - AddressCreated
emit AddressCreated(id);
// Return address id
return id;
}
/**
* Remove an address from the registry.
* Fires an AddressRemoved event.
*/
function removeAddress (bytes32 addressId) public isOwner {
// Check requirements - Address exist
require(belgianAddresses[addressId].addressId == addressId, "Requested address does not exist.");
// Remove the address
StreetAddress memory oldAddress = _removeAddress(addressId);
// Emit event - AddressRemoved
// The address data are put in the log so they remain accessible for ever
emit AddressRemoved(addressId, oldAddress);
}
/**
* Update an existing address. The updated address will have a new ID computed by the function.
* Emits an AddressUpdated event.
* Returns the ID of the new version of the address.
*/
function updateAddress (
bytes32 addressId,
string memory streetName,
string memory postcode,
string memory houseNumber,
string memory boxNumber,
string memory latitude,
string memory longitude) public isOwner returns (bytes32) {
// Check requirements - Deleted address exist
require(belgianAddresses[addressId].addressId == addressId, "Requested address does not exist.");
// Create address
StreetAddress memory newAddress;
newAddress.streetName = streetName;
newAddress.postcode = postcode;
newAddress.houseNumber = houseNumber;
newAddress.boxNumber = boxNumber;
newAddress.latitude = latitude;
newAddress.longitude = longitude;
bytes32 id = _createAddress(newAddress);
// Remove the address
StreetAddress memory oldAddress = _removeAddress(addressId);
// Emit event AddressUpdated
emit AddressUpdated (addressId, oldAddress, id);
// Return ID of the new version of the address
return id;
}
/***************************************************************************************************************/
/* VIEW FUNCTIONS */
/***************************************************************************************************************/
/**
* Get an address by ID
*/
function getAddress (bytes32 newAddressId) public view returns (StreetAddress memory) {
// Require address exist
require(belgianAddresses[newAddressId].addressId == newAddressId, "Requested address does not exist.");
return belgianAddresses[newAddressId];
}
/**
* Get the list of address ids related to a postcode.
*/
function listAddressIdsByPostcode (string memory postcode) public view returns (bytes32 [] memory) {
// Require postcode exist
require (addressIdsByPostcode[postcode].length > 0, "The provided postcode does not exist");
return addressIdsByPostcode[postcode];
}
/**
* List all available postcodes
*/
function listPostcodes () public view returns (string [] memory) {
return postcodes;
}
/***************************************************************************************************************/
/* PRIVATE STATE FUNCTIONS */
/***************************************************************************************************************/
/**
* Add an address to the registry.
* The ID of the address is computed by the function and returned.
*/
function _createAddress (StreetAddress memory newAddress) private returns (bytes32) {
// Compute ID
newAddress.addressId = keccak256(abi.encodePacked(newAddress.postcode, newAddress.streetName, newAddress.houseNumber, newAddress.boxNumber, newAddress.latitude, newAddress.longitude));
// Check the address does not exist yet
require(belgianAddresses[newAddress.addressId].addressId != newAddress.addressId, "Address already exist.");
// Add address to the storage & the list of addresses by postcode
belgianAddresses[newAddress.addressId] = newAddress;
if(addressIdsByPostcode[newAddress.postcode].length <= 0) {
// Postcode is not know -> Add to postcode list
postcodes.push(newAddress.postcode);
}
// Update the list of address ids by postcode
addressIdsByPostcode[newAddress.postcode].push(newAddress.addressId);
// Return the new ID
return newAddress.addressId;
}
/**
* Remove an address from the registry.
* A copy of the removed address is returned by the function
*/
function _removeAddress (bytes32 addressID) private returns (StreetAddress memory) {
// Require address exist
require (belgianAddresses[addressID].addressId != 0x0, "Requested address does not exist");
// Remove address from the list of addresses for the postcode
string memory targetPostcode = belgianAddresses[addressID].postcode;
for (uint i = 0 ; i < addressIdsByPostcode[targetPostcode].length ; i++) {
if (addressIdsByPostcode[belgianAddresses[addressID].postcode][i] == addressID) {
// Replace by the last element & pop the alst element
addressIdsByPostcode[belgianAddresses[addressID].postcode][i] =
addressIdsByPostcode[belgianAddresses[addressID].postcode][addressIdsByPostcode[targetPostcode].length-1];
addressIdsByPostcode[belgianAddresses[addressID].postcode].pop();
// Break
i = addressIdsByPostcode[targetPostcode].length + 1;
}
}
// Remove the postcode from the list if there are no addresses left
if (addressIdsByPostcode[targetPostcode].length <= 0) {
bytes32 postcodeHash = keccak256(abi.encodePacked(targetPostcode));
for (uint i = 0 ; i < postcodes.length ; i++) {
if (keccak256(abi.encodePacked(postcodes[i])) == postcodeHash) {
// Replace by last element and pop
postcodes[i] = postcodes[postcodes.length-1];
postcodes.pop();
// Break
i = postcodes.length + 1;
}
}
}
// Get a copy of the address that will be removed
StreetAddress memory oldAddress = belgianAddresses[addressID];
// Remove the address from storage
delete belgianAddresses[addressID];
// Return a copy of the removed address
return oldAddress;
}
/***************************************************************************************************************/
/* PRIVATE UTILITY FUNCTIONS */
/***************************************************************************************************************/
/**
* Explode UTF8 string into an array of codepoints that identify each readable character of the input string.
* Returns the array of codepoints and the index of the last element of the array
*/
function _explodeUtf8StringToCodepoints (string memory input) private pure returns (bytes32 [] memory, uint8) {
uint8 count = 0;
bytes memory input_rep = bytes(input);
bytes32 [] memory results = new bytes32 [] (input_rep.length);
for (uint i = 0 ; i < input_rep.length;)
{
if (uint8(input_rep[i]>>7)==0) {
results[count] = keccak256(abi.encodePacked(input_rep[i]));
i+=1;
}
else if (uint8(input_rep[i]>>5)==0x6) {
results[count] = keccak256(abi.encodePacked(input_rep[i], input_rep[i+1]));
i+=2;
}
else if (uint8(input_rep[i]>>4)==0xE) {
results[count] = keccak256(abi.encodePacked(input_rep[i], input_rep[i+1], input_rep[i+2]));
i+=3;
}
else if (uint8(input_rep[i]>>3)==0x1E) {
results[count] = keccak256(abi.encodePacked(input_rep[i], input_rep[i+1], input_rep[i+2], input_rep[i+3]));
i+=4;
}
else {
//For safety
results[count] = keccak256(abi.encodePacked(input_rep[i]));
i+=1;
}
count++;
}
return (results, count);
}
/**
* Find the minimum between three integers
*/
function _min3(uint8 x, uint8 y, uint8 z) private pure returns (uint8) {
if(x <= y && x <= z)
return x;
if(y <= x && y <= z)
return y;
else
return z;
}
/**
* Compute the levenshtein distance between two strings
*/
function _levenshtein (string memory origin, string memory target) public pure returns (uint8) {
// Explode both strings
(bytes32[] memory origin_exploded, uint256 origin_exploded_length) = _explodeUtf8StringToCodepoints(origin);
(bytes32[] memory target_exploded, uint256 target_exploded_length) = _explodeUtf8StringToCodepoints(target);
// Create matrix for subproblems solutions
uint8[8][8] memory matrix;
// Initialising first column:
for(uint8 i = 0; i <= origin_exploded_length; i++)
matrix[i][0] = i;
// Initialising first row:
for(uint8 j = 0; j <= target_exploded_length; j++)
matrix[0][j] = j;
// Applying the algorithm:
uint8 insertion;
uint8 deletion;
uint8 replacement;
for(uint8 i = 1; i <= origin_exploded_length; i++) {
for(uint8 j = 1; j <= target_exploded_length; j++) {
if(origin_exploded[i - 1] == target_exploded[j - 1])
matrix[i][j] = matrix[i - 1][j - 1];
else {
insertion = matrix[i][j - 1];
deletion = matrix[i - 1][j];
replacement = matrix[i - 1][j - 1];
// Using the sub-problems
matrix[i][j] = 1 + _min3(insertion, deletion, replacement);
}
}
}
uint8 r = matrix[origin_exploded_length][target_exploded_length];
return r;
}
/***************************************************************************************************************/
/* PERMISSIONS MANAGEMENT */
/***************************************************************************************************************/
/**
* @dev Change owner
* @param newOwner address of new owner
*/
function changeOwner(address newOwner) public isOwner {
emit OwnerSet(owner, newOwner);
owner = newOwner;
}
/**
* @dev Return owner address
* @return address of owner
*/
function getOwner() external view returns (address) {
return owner;
}
}