forked from eosrio/dappmetadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdappmetadata.cpp
542 lines (470 loc) · 15 KB
/
dappmetadata.cpp
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include <eosiolib/eosio.hpp>
#include <eosiolib/asset.hpp>
#include <eosiolib/time.hpp>
#include <eosiolib/singleton.hpp>
#include <eosiolib/privileged.hpp>
#include <vector>
#include <algorithm>
using namespace eosio;
using namespace std;
class [[eosio::contract("dappmetadata")]] metadata : public contract {
private:
struct [[eosio::table, eosio::contract("eosio.system")]] producer_info {
name owner;
double total_votes = 0;
uint64_t primary_key() const { return owner.value; }
};
typedef eosio::multi_index<"producers"_n,producer_info> producers_table;
struct [[eosio::table, eosio::contract("eosio.system")]] eosio_global_state : eosio::blockchain_parameters {
uint64_t free_ram() const { return max_ram_size - total_ram_bytes_reserved; }
uint64_t max_ram_size;
uint64_t total_ram_bytes_reserved;
int64_t total_ram_stake;
block_timestamp last_producer_schedule_update;
time_point last_pervote_bucket_fill;
int64_t pervote_bucket;
int64_t perblock_bucket;
uint32_t total_unpaid_blocks;
int64_t total_activated_stake;
time_point thresh_activated_stake_time;
uint16_t last_producer_schedule_size;
double total_producer_vote_weight;
block_timestamp last_name_close;
};
typedef eosio::singleton<"global"_n, eosio_global_state> gs_singleton;
struct [[eosio::table]] resources {
string ram_payer;
uint32_t avg_ram_usage;
uint32_t avg_cpu_us;
uint32_t avg_net;
};
struct [[eosio::table]] act {
capi_name action_name;
string short_desc;
string long_desc;
resources resources;
};
struct [[eosio::table]] initact {
capi_name action_name;
string short_desc;
string long_desc;
string ram_payer;
};
struct [[eosio::table]] insertdapp {
name account;
string title;
string description;
string source_code;
string website;
string logo;
vector<string> tags;
vector<initact> actions;
EOSLIB_SERIALIZE(insertdapp,(account)(title)(description)(source_code)(website)(logo)(tags)(actions))
};
struct [[eosio::table]] dapp {
name account;
string title;
string description;
string source_code;
string website;
string logo;
vector<string> tags;
capi_checksum256 code_hash;
capi_name last_validator;
vector<act> actions;
uint64_t updated_on;
uint64_t primary_key() const { return account.value; }
EOSLIB_SERIALIZE(dapp,(account)(title)(description)(source_code)(website)(logo)(tags)(code_hash)(last_validator)(actions)(updated_on))
};
typedef multi_index<"dapps"_n,dapp> dapps_table;
struct [[eosio::table]] validator {
capi_name account;
string url;
double weight;
vector<capi_name> approved_by;
uint64_t primary_key() const { return account; }
EOSLIB_SERIALIZE(validator,(account)(url)(weight)(approved_by))
};
typedef multi_index<"validators"_n,validator> validators_table;
struct [[eosio::table]] request {
uint64_t id;
capi_name payer;
capi_name contract;
capi_name validator;
double min_rep;
asset bounty;
bool accepted;
uint64_t accepted_on;
uint64_t expiration;
uint64_t primary_key() const { return id; }
EOSLIB_SERIALIZE(request,(id)(payer)(contract)(validator)(min_rep)(bounty)(accepted)(accepted_on)(expiration))
};
typedef multi_index<"requests"_n,request> requests_table;
struct [[eosio::table]] validation {
capi_name validator;
uint64_t version;
capi_checksum256 code_hash;
uint64_t timestamp;
string tag;
string notes;
uint64_t approvals;
uint64_t primary_key() const { return validator; }
EOSLIB_SERIALIZE(validation,(validator)(version)(code_hash)(timestamp)(tag)(notes)(approvals))
};
typedef multi_index<"validations"_n,validation> validations_table;
struct [[eosio::table]] receipt {
capi_name owner;
asset amount;
uint64_t primary_key() const { return owner; }
EOSLIB_SERIALIZE(receipt,(owner)(amount))
};
typedef multi_index<"receipts"_n,receipt> receipts_table;
validators_table _validators;
dapps_table _dapps;
producers_table _producers;
public:
using contract::contract;
metadata(name s, name code, datastream<const char*> ds)
:contract(s,code,ds),
_validators(_self,_self.value),
_dapps(_self,_self.value),
_producers("eosio"_n,"eosio"_n.value)
{}
double sum_votes(vector<capi_name> vec) {
double sum = 0;
gs_singleton _global("eosio"_n,"eosio"_n.value);
auto gs = _global.get();
auto total_votes = gs.total_producer_vote_weight;
for (capi_name & p : vec) {
auto tp = _producers.find(p);
sum += tp->total_votes;
}
return sum / (total_votes);
}
[[eosio::action]]
void regvalidator(capi_name owner, string url) {
require_auth(owner);
auto itr = _validators.find(owner);
eosio_assert(itr == _validators.end(),"Account already added!");
_validators.emplace(name(owner), [&](validator& o) {
o.account = owner;
o.url = url;
});
}
[[eosio::action]]
void rmvalidator(capi_name owner) {
require_auth(owner);
auto itr = _validators.get(owner,"Account not found");
_validators.erase(itr);
}
[[eosio::action]]
void update(name contract, capi_name action, uint64_t net, uint64_t cpu, uint64_t ram) {
require_auth(_self);
auto itr = _dapps.get(contract.value,"contract not found");
_dapps.modify(itr, contract, [&](dapp& o) {
for (auto & a : o.actions) {
if(action == a.action_name) {
a.resources.avg_net = net;
a.resources.avg_cpu_us = cpu;
a.resources.avg_ram_usage = ram;
}
}
});
}
[[eosio::action]]
void validate(capi_name validator, name contract, capi_checksum256 code_hash, string tag, string notes, int64_t id) {
require_auth(validator);
eosio_assert(is_account(contract.value),"Invalid account");
auto index = _dapps.get(contract.value,"contract not found");
// use -1 to bypass
if(id >= 0) {
requests_table _requests(_self,contract.value);
auto req = _requests.get(uint64_t(id),"request not found");
auto ft = (req.expiration) + (req.accepted_on);
eosio_assert( ft > current_time(), "request has expired");
// pay bounty to the validator
string memo = "contract validation bounty";
action(
permission_level{ _self, "active"_n },
name("eosio.token"),
name("transfer"),
make_tuple(_self.value, req.validator, req.bounty, memo)
).send();
_requests.erase(req);
}
validations_table _validations(_self,contract.value);
auto itr = _validations.find(validator);
if(itr != _validations.end()) {
_validations.modify(itr, name(validator), [&](validation& o) {
o.version += 1;
o.code_hash = code_hash;
o.tag = tag;
o.notes = notes;
o.approvals = 0;
o.timestamp = current_time();
});
} else {
_validations.emplace(name(validator), [&](validation& o) {
o.validator = validator;
o.version = 1;
o.code_hash = code_hash;
o.tag = tag;
o.notes = notes;
o.approvals = 0;
o.timestamp = current_time();
});
}
}
[[eosio::action]]
void rmvalidation(capi_name validator, name contract) {
require_auth(validator);
eosio_assert(is_account(validator),"Invalid account");
eosio_assert(is_account(contract.value),"Invalid account");
validations_table _validations(_self,contract.value);
auto itr = _validations.get(validator,"validation not found");
_validations.erase(itr);
}
[[eosio::action]]
void approve(capi_name producer, capi_name val_account) {
// require signature from block producer
require_auth(producer);
// check producer reg
auto prod = _producers.get(producer,"Producer not registered");
auto itr = _validators.get(val_account,"validator not found");
auto vecit = find(itr.approved_by.begin(), itr.approved_by.end(), producer);
eosio_assert(vecit == itr.approved_by.end(),"already approved");
_validators.modify(itr, _self, [&](validator& o) {
// include approval
o.approved_by.push_back(producer);
// update votes
o.weight = sum_votes(o.approved_by);
});
}
[[eosio::action]]
void unapprove(capi_name producer, capi_name val_account) {
// require signature from block producer
require_auth(producer);
// check producer reg
auto prod = _producers.get(producer,"Producer not registered");
auto itr = _validators.get(val_account,"validator not found");
auto vecit = find(itr.approved_by.begin(), itr.approved_by.end(), producer);
eosio_assert(vecit != itr.approved_by.end(),"did not approve yet");
_validators.modify(itr, _self, [&](validator& o) {
// remove approval entry
o.approved_by.erase(vecit);
// update votes
o.weight = sum_votes(o.approved_by);
});
}
[[eosio::action]]
void addrequest(capi_name payer, capi_name contract, capi_name val_account, asset bounty, uint64_t expiration, double min_rep) {
require_auth(payer);
// check receipt balance
receipts_table _receipts(_self,_self.value);
auto p = _receipts.get(payer,"payer not found");
eosio_assert(p.amount >= bounty,"not enough credits");
_receipts.modify(p, _self, [&](receipt& o) {
o.amount -= bounty;
});
requests_table _requests(_self,contract);
_requests.emplace(name(payer), [&](request& o) {
o.id = _requests.available_primary_key();
o.payer = payer;
o.contract = contract;
o.validator = val_account;
o.accepted = false;
o.expiration = expiration;
o.min_rep = min_rep;
o.bounty = bounty;
});
}
[[eosio::action]]
void cancelreq(capi_name payer, capi_name contract, uint64_t id) {
require_auth(payer);
requests_table _requests(_self,contract);
receipts_table _receipts(_self,_self.value);
auto req = _requests.get(id,"request not found");
if (req.accepted == true) {
auto ft = (req.expiration) + (req.accepted_on);
eosio_assert( ft < current_time(), "request is still active");
}
// refund payer
auto p = _receipts.find(req.payer);
_receipts.modify(p, _self, [&](receipt& o) {
o.amount += req.bounty;
});
// delete request
_requests.erase(req);
}
[[eosio::action]]
void refund(capi_name payer, asset quantity) {
receipts_table _receipts(_self,_self.value);
auto p = _receipts.get(payer, "balance not found");
eosio_assert(p.amount >= quantity, "not enough balance");
if(p.amount == quantity) {
_receipts.erase(p);
} else {
_receipts.modify(p, _self, [&](receipt& o) {
o.amount -= quantity;
});
}
string memo = "dappmetadata refund";
action(
permission_level{ _self, "active"_n },
name("eosio.token"),
name("transfer"),
make_tuple(_self.value, payer, quantity, memo)
).send();
}
[[eosio::action]]
void acceptreq(capi_name val_account, capi_name contract, uint64_t id) {
require_auth(val_account);
auto itr = _validators.get(val_account,"validator not found");
requests_table _requests(_self,contract);
auto req = _requests.get(id,"request not found");
// check validator, if assigned
if(req.validator != ""_n.value) {
eosio_assert(req.validator == val_account, "validator do not match");
}
// update votes
_validators.modify(itr, _self, [&](validator& o) {
o.weight = sum_votes(o.approved_by);
});
// check minimum reputation
eosio_assert(itr.weight >= req.min_rep,"not enough reputation to accept");
_requests.modify(req, name(val_account), [&](request& o){
o.accepted = true;
o.accepted_on = current_time();
});
}
[[eosio::action]]
void insert( insertdapp data ) {
require_auth( data.account );
// Check string sizes
eosio_assert(data.title.length() <= 32, "Title too long");
eosio_assert(data.description.length() <= 1024, "Description too long");
eosio_assert(data.source_code.length() <= 128, "Source code url too long");
eosio_assert(data.website.length() <= 128, "Website url too long");
eosio_assert(data.logo.length() <= 128, "Logo url too long");
// Check tags
eosio_assert(data.tags.size() <= 10, "Cannot add more than 10 tags");
for (auto & tag : data.tags) {
eosio_assert(tag.length() <= 16, "Tag must have less than 16 chars");
}
// Check actions
vector<string> ram_payer_opts = {"user","contract","both"};
for (auto & action : data.actions) {
// Check ram payer types
eosio_assert(any_of(ram_payer_opts.begin(), ram_payer_opts.end(), [&](const string & str){
return str == action.ram_payer;
}),"Invalid ram payer");
// Check descriptions
eosio_assert(action.short_desc.length() <= 256, "Short description too long");
eosio_assert(action.long_desc.length() <= 512, "Long description too long");
}
auto itr = _dapps.find(data.account.value);
if(itr != _dapps.end()) {
// Update entry
_dapps.modify(itr, data.account, [&](dapp& o) {
o.title = data.title;
o.description = data.description;
o.source_code = data.source_code;
o.website = data.website;
o.logo = data.logo;
o.tags = data.tags;
o.updated_on = current_time();
auto temp = o.actions;
o.actions.clear();
for (auto & action : data.actions) {
resources rec = resources{
.ram_payer = action.ram_payer,
.avg_net = 0,
.avg_cpu_us = 0,
.avg_ram_usage = 0
};
for (auto & tempact : temp) {
if(tempact.action_name == action.action_name) {
rec.avg_net = tempact.resources.avg_net;
rec.avg_cpu_us = tempact.resources.avg_cpu_us;
rec.avg_ram_usage = tempact.resources.avg_ram_usage;
}
}
o.actions.push_back({
.action_name = action.action_name,
.short_desc = action.short_desc,
.long_desc = action.long_desc,
.resources = rec
});
}
});
} else {
// Add new entry
_dapps.emplace(data.account, [&](dapp& o) {
o.account = data.account;
o.title = data.title;
o.description = data.description;
o.source_code = data.source_code;
o.website = data.website;
o.logo = data.logo;
o.tags = data.tags;
o.last_validator = 0;
o.updated_on = current_time();
for (auto & action : data.actions) {
o.actions.push_back({
.action_name = action.action_name,
.short_desc = action.short_desc,
.long_desc = action.long_desc,
.resources = {
.ram_payer = action.ram_payer,
.avg_net = 0,
.avg_cpu_us = 0,
.avg_ram_usage = 0
}
});
}
});
}
}
void received_token(const name& from, const name& to, const asset& quantity, const string& memo) {
if(to == "dappmetadata"_n) {
receipts_table _receipts(_self,_self.value);
auto itr = _receipts.find(from.value);
if(itr != _receipts.end()) {
_receipts.modify(itr, _self, [&](receipt& o) {
o.amount += quantity;
});
} else {
_receipts.emplace(_self, [&](receipt& o){
o.amount = quantity;
o.owner = from.value;
});
}
}
}
};
extern "C" {
[[noreturn]] void apply(uint64_t receiver, uint64_t code, uint64_t action) {
if (action == "transfer"_n.value and code == "eosio.token"_n.value) {
execute_action<metadata>(name(receiver),name(code),&metadata::received_token);
}
if(code == receiver) {
switch(action) {
EOSIO_DISPATCH_HELPER(metadata,
(acceptreq)
(addrequest)
(approve)
(cancelreq)
(insert)
(refund)
(regvalidator)
(rmvalidator)
(rmvalidation)
(unapprove)
(update)
(validate)
)
};
}
eosio_exit(0);
}
}