forked from eoscostarica/eos-rate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrateproducer.cpp
529 lines (460 loc) · 18.5 KB
/
rateproducer.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
#include "../include/rateproducer.hpp"
namespace eoscostarica {
void rateproducer::rate(
name user,
name bp,
int8_t transparency,
int8_t infrastructure,
int8_t trustiness,
int8_t community,
int8_t development,
std::string comment) {
require_auth(user);
name scope = is_eden(user) ? eden_scope : _self;
rate_aux(scope, user, bp, transparency, infrastructure, trustiness, community, development, comment);
}
void rateproducer::rate_aux(
name scope,
name user,
name bp,
int8_t transparency,
int8_t infrastructure,
int8_t trustiness,
int8_t community,
int8_t development,
std::string comment) {
check( (transparency + infrastructure + trustiness + community + development), "Error vote must have value for at least one category" );
check( (MINVAL <= transparency && transparency <= MAXVAL), "Error transparency value out of range" );
check( (MINVAL <= infrastructure && infrastructure <= MAXVAL), "Error infrastructure value out of range" );
check( (MINVAL <= trustiness && trustiness <= MAXVAL), "Error trustiness value out of range" );
check( (MINVAL <= development && development <= MAXVAL), "Error development value out of range" );
check( (MINVAL <= community && community <= MAXVAL), "Error community value out of range" );
bool is_eden = scope.value == eden_scope.value;
name stats_ram_payer = is_eden ? _self : user;
check( is_blockproducer(bp), "votes are allowed only for registered block producers" );
name proxy_name = get_proxy(user);
if(proxy_name.length()) {
check(is_active_proxy(proxy_name), "votes are allowed only for active proxies" );
if(!is_eden) check( MIN_VOTERS <= get_voters(proxy_name), "delegated proxy does not have enough voters" );
} else {
if(!is_eden) check( MIN_VOTERS <= get_voters(user), "account does not have enough voters" );
}
ratings_table_v2 _ratings(_self, scope.value);
auto uniq_rating = (static_cast<uint128_t>(user.value) << 64) | bp.value;
auto uniq_rating_index = _ratings.get_index<name("uniqrating")>();
auto existing_rating = uniq_rating_index.find(uniq_rating);
if( existing_rating == uniq_rating_index.end() ) {
uint64_t rating_id = _ratings.available_primary_key();
_ratings.emplace(user, [&]( auto& row ) {
row.id = rating_id;
row.user = user;
row.bp = bp;
row.transparency = transparency;
row.infrastructure = infrastructure;
row.trustiness = trustiness;
row.community = community;
row.development = development;
});
save_bp_stats(scope,
stats_ram_payer,
bp,
transparency,
infrastructure,
trustiness,
community,
development);
if(!comment.empty()) {
SEND_INLINE_ACTION(*this, logcomment, { {get_self(), name("active")} }, { rating_id, comment, is_eden });
}
} else {
uniq_rating_index.modify(existing_rating, user, [&]( auto& row ) {
row.transparency = transparency;
row.infrastructure = infrastructure;
row.trustiness = trustiness;
row.community = community;
row.development = development ;
});
float bp_transparency = 0;
float bp_infrastructure = 0;
float bp_trustiness = 0;
float bp_community = 0;
float bp_development = 0;
uint32_t bp_ratings_cntr = 0;
float bp_average = 0;
calculate_bp_stats (scope,
bp,
&bp_transparency,
&bp_infrastructure,
&bp_trustiness,
&bp_community,
&bp_development,
&bp_ratings_cntr,
&bp_average);
update_bp_stats (scope,
&stats_ram_payer,
&bp,
&bp_transparency,
&bp_infrastructure,
&bp_trustiness,
&bp_community,
&bp_development,
&bp_ratings_cntr,
&bp_average);
if(!comment.empty()) {
SEND_INLINE_ACTION(*this, logcomment, { {get_self(), name("active")} }, { existing_rating->id, comment, is_eden });
}
}
}
void rateproducer::save_bp_stats (
name scope,
name ram_payer,
name bp_name,
float transparency,
float infrastructure,
float trustiness,
float community,
float development) {
stats_table _stats(_self, scope.value);
auto itr = _stats.find(bp_name.value);
float counter =0;
float sum = 0;
if(itr == _stats.end()) {
//new entry
_stats.emplace(ram_payer, [&]( auto& row ) {
if (transparency) {
row.transparency = transparency;
counter++;
sum += transparency;
}
if (infrastructure) {
row.infrastructure = infrastructure;
counter++;
sum += infrastructure;
}
if (trustiness) {
row.trustiness = trustiness;
counter++;
sum += trustiness;
}
if (development) {
row.development = development;
counter++;
sum += development;
}
if (community) {
row.community = community;
counter++;
sum += community;
}
if(counter) {
row.bp = bp_name;
row.ratings_cntr = 1;
row.average =sum/counter;
}
});
} else {
//update the entry
_stats.modify(itr, ram_payer, [&]( auto& row ) {
if (transparency) {
sum += transparency;
if(row.transparency) {
transparency = (transparency + row.transparency)/2;
}
row.transparency = transparency;
counter++;
}
if (infrastructure) {
sum += infrastructure;
if(row.infrastructure) {
infrastructure = (infrastructure + row.infrastructure)/2;
}
row.infrastructure = infrastructure;
counter++;
}
if (trustiness) {
sum += trustiness;
if(row.trustiness) {
trustiness = (trustiness + row.trustiness) / 2;
}
row.trustiness = trustiness;
counter++;
}
if (development) {
sum += development;
if(row.development) {
development = (development + row.development) / 2;
}
row.development = development;
counter++;
}
if (community) {
sum += community;
if(row.community) {
community = (community + row.community) / 2;
}
row.community = community;
counter++;
}
if(counter) {
row.ratings_cntr++;
row.average =( (sum/counter) + row.average ) / 2;
}
});
}
}
void rateproducer::calculate_bp_stats (
name scope,
name bp_name,
float * transparency,
float * infrastructure,
float * trustiness,
float * community,
float * development,
uint32_t * ratings_cntr,
float * average) {
float category_counter = 0;
float transparency_total = 0;
float infrastructure_total = 0;
float trustiness_total = 0;
float community_total = 0;
float development_total = 0;
float transparency_cntr = 0;
float infrastructure_cntr = 0;
float trustiness_cntr = 0;
float community_cntr = 0;
float development_cntr = 0;
uint32_t voters_cntr = 0;
ratings_table_v2 _ratings(_self, scope.value);
auto bps_index = _ratings.get_index<name("bp")>();
auto bps_it = bps_index.find(bp_name.value);
while(bps_it != bps_index.end()) {
if(bp_name == bps_it->bp) {
if(bps_it->transparency) {
transparency_total+=bps_it->transparency;
transparency_cntr++;
}
if(bps_it->infrastructure) {
infrastructure_total+=bps_it->infrastructure;
infrastructure_cntr++;
}
if(bps_it->trustiness) {
trustiness_total+=bps_it->trustiness;
trustiness_cntr++;
}
if(bps_it->community) {
community_total+=bps_it->community;
community_cntr++;
}
if(bps_it->development) {
development_total+=bps_it->development;
development_cntr++;
}
voters_cntr++;
}
bps_it ++;
}
if(transparency_cntr) {
*transparency = transparency_total/transparency_cntr;
category_counter++;
}
if(infrastructure_cntr) {
*infrastructure =infrastructure_total/infrastructure_cntr;
category_counter++;
}
if(trustiness_cntr) {
*trustiness = trustiness_total/trustiness_cntr;
category_counter++;
}
if(community_cntr) {
*community = community_total/community_cntr;
category_counter++;
}
if(development_cntr) {
*development = development_total/development_cntr;
category_counter++;
}
*average = (*transparency + *infrastructure + *trustiness + *community +*development)/category_counter;
*ratings_cntr = voters_cntr;
}
void rateproducer::update_bp_stats (
name scope,
name * ram_payer,
name * bp_name,
float * transparency,
float * infrastructure,
float * trustiness,
float * community,
float * development,
uint32_t * ratings_cntr,
float * average) {
stats_table _stats(_self, scope.value);
auto itr = _stats.find(bp_name->value);
if(itr != _stats.end()) {
//if rate categories are more than zero
// we store, otherwise remove the entry
if( *transparency +
*infrastructure +
*trustiness +
*community +
*development) {
_stats.modify(itr,*ram_payer, [&]( auto& row ) {
row.transparency = *transparency;
row.infrastructure = *infrastructure;
row.trustiness = *trustiness;
row.development = *development;
row.community = *community;
row.ratings_cntr= *ratings_cntr;
row.average = *average;
});
} else {
_stats.erase(itr);
}
}
}
void rateproducer::erase(name bp_name) {
erase_aux(_self, bp_name);
erase_aux(eden_scope, bp_name);
}
void rateproducer::erase_aux(name scope, name bp_name) {
require_auth(_self);
ratings_table_v2 _ratings(_self, scope.value);
auto itr = _ratings.begin();
while (itr != _ratings.end()) {
if(itr->bp == bp_name) {
itr = _ratings.erase(itr);
} else {
itr++;
}
}
//clean the stats summary
stats_table _stats(_self, scope.value);
auto itr_stats = _stats.find(bp_name.value);
if (itr_stats != _stats.end()) _stats.erase(itr_stats);
}
void rateproducer::erase_bp_info(name scope, std::set<eosio::name> * bps_to_clean) {
ratings_table_v2 _ratings(_self, scope.value);
stats_table _stats(_self, scope.value);
std::set<eosio::name>::iterator it;
for (it = bps_to_clean->begin(); it != bps_to_clean->end(); ++it) {
//clean all ratings related to an bp
auto itr = _ratings.begin();
while (itr != _ratings.end()) {
if(itr->bp == *it) {
itr = _ratings.erase(itr);
} else {
itr++;
}
}
//clean the stats summary
auto itr_stats = _stats.find((*it).value);
if (itr_stats != _stats.end()) _stats.erase(itr_stats);
}
}
void rateproducer::rminactive() {
rminactive_aux(_self);
rminactive_aux(eden_scope);
}
void rateproducer::rminactive_aux(name scope) {
require_auth(_self);
std::set<eosio::name> noupdated_bps;
stats_table _stats(_self, scope.value);
auto itr_stats = _stats.begin();
while (itr_stats != _stats.end()) {
if (!is_blockproducer(itr_stats->bp)) {
noupdated_bps.insert(itr_stats->bp);
}
itr_stats++;
}
if(noupdated_bps.size()) erase_bp_info(scope, &noupdated_bps);
print("bps deleted:",noupdated_bps.size());
}
void rateproducer::rmrate(name user, name bp) {
require_auth(user);
name scope = is_eden(user) ? eden_scope : _self;
rmrate_aux(scope, user, bp);
}
void rateproducer::rmrate_aux(name scope, name user, name bp) {
ratings_table_v2 _ratings(_self, scope.value);
auto uniq_rating = (static_cast<uint128_t>(user.value) << 64) | bp.value;
auto uniq_rating_index = _ratings.get_index<name("uniqrating")>();
auto existing_rating = uniq_rating_index.find(uniq_rating);
check( existing_rating != uniq_rating_index.end(), "Rating does not exist" );
//delete rate info
auto itr = uniq_rating_index.erase(existing_rating);
//update bp stats
float bp_transparency = 0;
float bp_infrastructure = 0;
float bp_trustiness = 0;
float bp_community = 0;
float bp_development = 0;
uint32_t bp_ratings_cntr = 0;
float bp_average = 0;
//re-calculate stats for the bp
calculate_bp_stats (scope,
bp,
&bp_transparency,
&bp_infrastructure,
&bp_trustiness,
&bp_community,
&bp_development,
&bp_ratings_cntr,
&bp_average);
//save the re-calcualtes stats
update_bp_stats (scope,
&user,
&bp,
&bp_transparency,
&bp_infrastructure,
&bp_trustiness,
&bp_community,
&bp_development,
&bp_ratings_cntr,
&bp_average);
}
void rateproducer::migrate() {
config c = cfg.get_or_create(_self, config{.owner = _self, .version = 0});
require_auth(c.owner);
// assert we only run once
// the comparison value needs to be hard-coded with each new migration
eosio::check(c.version < 2, "Migration already ran");
ratings_table _ratings_self(_self, _self.value);
ratings_table_v2 _ratings_self_v2(_self, _self.value);
ratings_table_v2 _ratings_eden_v2(_self, eden_scope.value);
for(auto itr = _ratings_self.begin(); itr != _ratings_self.end(); itr++) {
auto emplace_rating = [&]( auto& row ) -> auto {
row.id = itr->id;
row.user = itr->user;
row.bp = itr->bp;
row.transparency = itr->transparency;
row.infrastructure = itr->infrastructure;
row.trustiness = itr->trustiness;
row.community = itr->community;
row.development = itr->development;
};
name temp_scope = is_eden(itr->user) ? eden_scope : _self;
if(temp_scope.value == eden_scope.value) _ratings_eden_v2.emplace(_self, emplace_rating);
else _ratings_self_v2.emplace(_self, emplace_rating);
}
c.version++;
cfg.set(c, c.owner);
}
void rateproducer::logcomment(uint64_t rating_id, std::string comment, bool is_eden) {
require_auth(_self);
check( comment.length() <= 500, "comment must be less or equal than 500 characters" );
}
void rateproducer::loglike(uint64_t rating_id, name user, bool like) {
require_auth(user);
check( rating_id >= 0, "Wrong rating id" );
}
} // namespace eoscostarica
EOSIO_ACTION_DISPATCHER(eoscostarica::actions)
EOSIO_ABIGEN(
actions(eoscostarica::actions),
table("rating"_n, eoscostarica::ratings_v2),
table("stats"_n, eoscostarica::stats),
ricardian_clause("datastorage", eoscostarica::datastorage_clause),
ricardian_clause("datausage", eoscostarica::datausage_clause),
ricardian_clause("dataownership", eoscostarica::dataownership_clause),
ricardian_clause("datadistribution", eoscostarica::datadistribution_clause),
ricardian_clause("datafuture", eoscostarica::datafuture_clause)
)