/
index.html
337 lines (310 loc) · 11.7 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="zxcat">
<title>Delegation modelling</title>
<!-- <script src="https://unpkg.com/golos-js@0.6.3/dist/golos.min.js"></script> -->
<style>
* {font-family: Arial, Helvetica, sans-serif}
small {font-size:70%; color:#777}
</style>
</head><body>
<h1>Delegation cheating model</h1>
<label for="mgests" title="Account MGESTS"> MGests:
<input type="text" value="1000000" id="mgests"/></label><br>
<label for="days_exp" title="Time in days to return delegation"> Delegation expiring days:
<input type="text" value="7" id="exp_days"/></label><br><br>
<label for="delegate_at" title="At which VP % cheater will delegate"> Delegation point, VP %:
<input type="text" value="50" id="delegate_at"/></label><br><br>
<button onclick="test()">Test Simple</button>
<button onclick="test2()">Test Fixes</button><br>
<small>Check console for results</small>
<script>
function $(id) { return document.getElementById(id) }
function input(id) { return $(id).value }
function secToDays(sec) {
let s = sec % 60; sec = sec / 60 | 0;
let m = sec % 60; sec = sec / 60 | 0;
let h = sec % 24; sec = sec / 24 | 0;
if (s < 10) s = '0'+s;
if (m < 10) m = '0'+m;
return `${sec} days ${h}:${m}:${s}`
}
</script><script>
'use strict';
// Note: using `value | 0` trick to get integer part of value (caution: works only with 32-bit integers)
const
GLS_100_PERCENT = 10000,
GLS = {
VOTE_REGENERATION_SECONDS: 5*60*60*24, // 5 day
MIN_VOTE_INTERVAL_SECONDS: 3,
CASHOUT_WINDOW_SECONDS: 60*60*24*7, // 1 week
DELEGATION_EXPIRING_SECONDS: 60*60*24*7,
VOTE_REGEN_PER_DAY: 40, // hardcoded in dynamic_global_property_object struct
};
class Account {
constructor(name, vests) {
let me = this;
me.name = name !== undefined ? name : 'account';
me.vests = (vests || 0) / 1e6 | 0; // use MGESTS to avoid 32-bit overflow
me.delegatedVests = 0;
me.receivedVests = 0;
me.vp = GLS_100_PERCENT;
}
// rshares for 100% vote weight
get rshares() {
return this.rsharesPrecise();
}
get effectiveVests() {
return this.vests - this.delegatedVests + this.receivedVests;
}
delegate(amount) {
this.delegatedVests += amount;
}
receive(amount) {
this.receivedVests += amount;
}
secToRegenerate(percents) {
if (percents === undefined) percents = GLS_100_PERCENT;
let toRegenerate = percents - this.vp;
return ((GLS.VOTE_REGENERATION_SECONDS / GLS_100_PERCENT * toRegenerate
+ 2.8 | 0) / 3 | 0) * 3; // rounding up and make it multiple of 3 seconds
}
rsharesPrecise(weight, secSincePrevVote, extendedReturn) {
if (weight === undefined) weight = GLS_100_PERCENT;
if (secSincePrevVote === undefined) secSincePrevVote = GLS.MIN_VOTE_INTERVAL_SECONDS;
let me = this;
let regenerated = (GLS_100_PERCENT * secSincePrevVote) / GLS.VOTE_REGENERATION_SECONDS | 0;
let currentPwr = Math.min(me.vp + regenerated, GLS_100_PERCENT);
let usedPwr = (currentPwr * weight) / GLS_100_PERCENT | 0;
let maxDenom = GLS.VOTE_REGEN_PER_DAY * GLS.VOTE_REGENERATION_SECONDS / (60 * 60 * 24) | 0;
usedPwr = (usedPwr + maxDenom - 1) / maxDenom | 0;
// let rshares = ((uint128_t(me.effectiveVests) * usedPwr) / (GLS_100_PERCENT)).to_uint64();
let rshares = me.effectiveVests * usedPwr / GLS_100_PERCENT | 0;
if (rshares <= 3e7 / 1e6) {
rshares = 0;
console.log(`${me.name}'s rshares too small to vote`);
}
return extendedReturn ? [rshares, usedPwr, regenerated] : rshares;
}
vote(weight, secSincePrevVote) {
if (secSincePrevVote === undefined) secSincePrevVote = 3;
let r = this.rsharesPrecise(weight, secSincePrevVote, true),
rs = r[0],
usedPwr = r[1],
regenerated = r[2]
this.vp += regenerated - usedPwr;
if (rs > 0)
console.log(`${this.name} vote ${rs} rshares (after ${secSincePrevVote} seconds), new VP: ${this.vp}`);
return rs;
}
}
class Strategy {
init(acc) {
this.acc = acc;
this.votes = 0;
}
iterate() {
throw("Not implemented");
}
}
class MostEffectiveVoting extends Strategy {
init(acc) {
super.init(acc);
this.i = 0;
}
iterate() {
let wait = this.acc.secToRegenerate();
return {
rshares: this.acc.vote(GLS_100_PERCENT, wait),
seconds: wait,
votes: ++this.votes,
stop: this.i >= GLS.VOTE_REGEN_PER_DAY
};
}
}
class DelegationCheating extends Strategy {
init(acc) {
super.init(acc);
this.acc2 = new Account('accomplice', 0);
this.stage = 1;
}
iterate() {
let r = {}, acc = this.acc, acc2 = this.acc2;
// 1. fast voting
if (this.stage === 1) {
let wait = acc.vp === 10000 ? 0 : 3;
let rs = acc.vote(GLS_100_PERCENT, wait);
r.voted = rs > 0;
if (r.voted) {
r.rshares = rs;
r.seconds = wait;
r.votes = ++this.votes;
} else {
console.log(`${acc.name} out of VP`);
this.delegate(1);
}
}
// 2. fast voting again from delegatee
if (this.stage === 2) {
let rs = acc2.vote();
r.voted = rs > 0;
if (r.voted) {
r.rshares = rs;
r.seconds = 3;
r.votes = ++this.votes;
} else {
// Can't vote, return delegated vests and wait delegation expired
console.log(`${acc2.name} out of VP`);
this.delegate(-1);
r.rshares = 0;
r.seconds = GLS.DELEGATION_EXPIRING_SECONDS;
r.votes = this.votes;
r.stop = true; // mark cycle end
}
}
return r;
}
// direction should be either 1 or -1
delegate(direction) {
let acc = this.acc, acc2 = this.acc2;
this.stage += direction;
let amount = direction > 0 ? acc.vests : acc2.receivedVests;
acc.delegate(direction * amount);
acc2.receive(direction * amount);
if (direction > 0) {
console.log(`Delegating ${amount} GESTS from ${acc.name} to ${acc2.name}`);
} else {
console.log(`Return delegation, ${amount} GESTS`);
}
}
}
class DelegationCheating2 extends DelegationCheating {
constructor(fixType, delegationPoint) {
super();
this.fixType = fixType;
this.delegationPoint = delegationPoint * 100 | 0;
}
iterate() {
let r = super.iterate();
let me = this, acc = me.acc;
if (!r.stop && me.stage === 1 && acc.vp <= me.delegationPoint) {
console.log(`${acc.name} passed delegation point ${me.delegationPoint} vp (actual: ${acc.vp})`);
me.delegate(1);
r.mark = true;
}
return r;
}
delegate(direction) {
let me = this, fixType = me.fixType;
if (direction < 0 || fixType == 0) {
super.delegate(direction);
} else {
let delegator = me.acc, delegatee = me.acc2;
switch (fixType) {
case 1:
// Limit allowed delegation depending on VP
this.stage++;
let amount = delegator.vests * delegator.vp / GLS_100_PERCENT | 0; // max allowed to delegate
delegator.delegate(amount);
delegatee.receive(amount);
console.log(`Delegating max allowed ${amount} GESTS from ${delegator.name} to ${delegatee.name}`);
break;
case 2:
// Patch VP of delegatee
super.delegate(direction);
let oldVp = delegatee.vp;
delegatee.vp = (delegator.vests * delegator.vp + delegatee.vests * delegatee.vp) /
(delegator.vests + delegatee.vests) | 0;
console.log(`Fix deelgatee VP from ${oldVp} to ${delegatee.vp}`);
break;
}
}
}
}
function testStrategy(strategy, acc, maxTime) {
let iterations = 0;
let rshares = 0, seconds = 0, votes = 0, mark = false, more = true;
strategy.init(acc);
do {
let r = strategy.iterate();
seconds += r.seconds;
if (maxTime && seconds >= maxTime) {
if (seconds == maxTime) rshares += r.rshares;
else seconds -= r.seconds;
break; // time was limited
}
rshares += r.rshares;
votes = r.votes;
more = !r.stop;
if (r.mark) {
// mark delegation point for extended case
mark = {
delegatedAtSec: seconds,
remainigVests: acc.effectiveVests,
remainigVP: acc.vp
}
r.mark = false;
}
if (++iterations > 1e4) return console.log('infinite loop? stop.');
} while (more);
return {
rshares: rshares,
seconds: seconds,
votes: votes,
mark: mark
}
}
function initTest(type) {
if (type === undefined) type = '';
const gests = input('mgests') * 1e6;
const expDays = input('exp_days');
if (expDays*1) {
GLS.DELEGATION_EXPIRING_SECONDS = 60*60*24 * expDays;
}
let cheater = new Account('cheater'+type, gests);
let voter = new Account('voter'+type, gests);
return {cheater:cheater, voter:voter};
}
function test() {
// simple
let ini = initTest();
let r1 = testStrategy(new DelegationCheating(), ini.cheater);
let r2 = testStrategy(new MostEffectiveVoting(), ini.voter, r1.seconds);
console.log(ini.cheater, r1, secToDays(r1.seconds));
console.log(ini.voter, r2, secToDays(r2.seconds));
console.log(`voter: 100%, cheater: ${(r1.rshares/r2.rshares*10000|0)/100}%`);
}
function test2() {
// with delegation point
let r = [], delegateAt = input('delegate_at');
for (var type = 0; type < 3; type++) {
let ini = initTest(type);
let r1 = testStrategy(new DelegationCheating2(type, delegateAt), ini.cheater);
let r2 = testStrategy(new MostEffectiveVoting(), ini.voter, r1.seconds);
r.push([ini.cheater, ini.voter, r1, r2]);
console.log('--------------------------------------------------------------------------------');
}
// Extended case: let cheater1 continue to vote (in most effective way) after delegation
let r1 = r[1][2], mark = r1.mark;
let cheater1 = new Account('cheater1+', mark.remainigVests*1e6);
cheater1.vp = mark.remainigVP;
let rc = testStrategy(new MostEffectiveVoting(), cheater1, r1.seconds - mark.delegatedAtSec);
console.log('================================================================================');
for (type = 0; type < 3; type++) {
let a = r[type];
let cheater = a[0], voter = a[1], r1 = a[2], r2 = a[3];
console.log(`Testing fix ${type} (${['None','Delegaion Limit','VP patch'][type]}) with delegation at ${delegateAt}% vp`);
console.log(cheater, r1, secToDays(r1.seconds));
console.log(voter, r2, secToDays(r2.seconds));
console.log(`voter: 100%, ${cheater.name}: ${(r1.rshares/r2.rshares*10000|0)/100}%`);
if (type == 1) {
let ext = ((rc.rshares+r1.rshares)/r2.rshares*10000|0)/100;
console.log(`extended cheater1: ${ext}%`);
}
}
}
</script>
</body></html>