-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCasper.js
More file actions
131 lines (111 loc) · 5.86 KB
/
Casper.js
File metadata and controls
131 lines (111 loc) · 5.86 KB
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
class Casper {
constructor(totalD, h, baseInterestFactor, basePenaltyFactor, baseDepositDependence, attackDuration, exponentialTerm = 0) {
this.validatorInitDeposits = [];
this.validatorPrevDeposits = [];
this.validatorDeposits = [];
// We consider two validators, one is attacked, other is not.
this.numValidators = 2;
this.baseInterestFactor = baseInterestFactor;
this.basePenaltyFactor = basePenaltyFactor;
this.baseDepositDependence = baseDepositDependence;
this.rewardFactor = this.baseInterestFactor / Math.pow(totalD, baseDepositDependence);
this.depositScaleFactor = Casper.INITIAL_SCALE_FACTOR; // 이게 무슨 문법이다냐
this.prevDepositScaleFactor = this.depositScaleFactor;
this.totalVoteUnscaled = 0;
this.epoch = 0;
// we assume that when we begin, the previous two epochs were justified
this.lastJustifiedEpoch = 0;
this.lastFinalizedEpoch = -1;
this.finalizationEpoch = 0;
this.D = totalD;
// h is the deposit fraction of the validator who is being 'censored'. During a censorship attack this validator represents the victims, during a minority fork those who are voting on the other chain
this.h = h;
this.attackDuration = attackDuration;
this.exponentialTerm = Number.POSITIVE_INFINITY;
if (exponentialTerm > 0) this.exponentialTerm = exponentialTerm;
this.initValidators(totalD, h);
}
initValidators(D, h) {
this.validatorDeposits[0] = D * (1 - h) / Casper.INITIAL_SCALE_FACTOR;
this.validatorDeposits[1] = D * h / Casper.INITIAL_SCALE_FACTOR;
for (var i = 0; i < this.numValidators; i++) {
this.validatorPrevDeposits[i] = this.validatorDeposits[i];
this.validatorInitDeposits[i] = this.validatorDeposits[i];
}
}
getTotalDepositsUnscaled() {
var result = 0;
for (var i = 0; i < this.numValidators; i++) {
result += this.validatorDeposits[i];
}
return result;
}
getESF() {
return this.epoch - this.lastFinalizedEpoch;
}
getCollectiveReward() {
// relevant for the first epoch, as we are assuming that we start in something close to steady-state
if (this.epoch == 1) return this.rewardFactor / 2;
var votePercentage = this.totalVoteUnscaled / this.getTotalDepositsUnscaled();
if (this.getESF() > 2) votePercentage = 0;
return votePercentage * this.rewardFactor / 2; // m * ro/2
}
getScaledDeposit(i) {
// We consider the deposits _after_ the rescale. Since we process the whole epoch in a single call to processEpoch,
// this is equivalent to multiplying the base deposits at the start of the previous epoch with the current scale factor.
return this.validatorPrevDeposits[i] * this.depositScaleFactor;
}
getNextScaledDeposit(i) {
return this.validatorDeposits[i] * this.depositScaleFactor;
}
getFinalizationEpoch() {
if (this.getESF() == 1) return this.finalizationEpoch;
else return "not finalized";
}
processEpoch() {
this.epoch++;
for (var i = 0; i < this.numValidators; i++) {
this.validatorPrevDeposits[i] = this.validatorDeposits[i];
}
// update scale factor
this.prevDepositScaleFactor = this.depositScaleFactor;
if (this.epoch > 1) this.depositScaleFactor = this.depositScaleFactor * (1 + this.getCollectiveReward()) / (1 + this.rewardFactor);
// update reward factor
this.rewardFactor = this.baseInterestFactor / Math.pow(this.getTotalDepositsUnscaled() * this.prevDepositScaleFactor, this.baseDepositDependence) + this.basePenaltyFactor * (this.getESF() - 2);
if (this.getESF() >= this.exponentialTerm) this.rewardFactor += (Math.exp(this.getESF() - this.exponentialTerm) - 1) * this.basePenaltyFactor;
// update base deposits
this.totalVoteUnscaled = 0;
for (var i = 0; i < this.numValidators; i++) {
if ((i == 0) || (this.epoch > this.attackDuration)) {
this.validatorDeposits[i] += this.validatorDeposits[i] * this.rewardFactor;
this.totalVoteUnscaled += this.validatorDeposits[i];
}
// check for justification/finalization
if (this.totalVoteUnscaled / this.getTotalDepositsUnscaled() > 2. / 3 && this.lastJustifiedEpoch < this.epoch) {
if (this.lastJustifiedEpoch == this.epoch - 1) {
if (this.epoch - this.lastFinalizedEpoch >= 3) this.finalizationEpoch = this.epoch; // just for tracing purposes
this.lastFinalizedEpoch = this.epoch - 1;
}
this.lastJustifiedEpoch = this.epoch;
}
}
}
getDepositChange(idx) {
return (this.getScaledDeposit(idx) / Casper.INITIAL_SCALE_FACTOR) / this.validatorInitDeposits[idx] - 1;
}
processEpochs(n) { // 이건 뭐하는 함수인지 모르겠네
var deps = [];
var z = this.numValidators - 1;
for (var i = 0; i < n; i++) {
this.processEpoch();
if (i % (Math.ceil(n / 1000)) == 0) {
var depsDepn = Math.pow(this.numValidators * this.validatorInitDeposits[z] * Casper.INITIAL_SCALE_FACTOR, this.baseDepositDependence);
var interest = Math.pow(1 + this.f * this.baseInterestFactor / (2 * depsDepn), i);
var naiveDep = this.validatorInitDeposits[z] * Casper.INITIAL_SCALE_FACTOR * interest;
deps.push(i + ", " + this.getScaledDeposit(z) + " " + naiveDep + " " + (this.getScaledDeposit(z) / naiveDep));
}
}
return deps;
}
}
Casper.INITIAL_SCALE_FACTOR = 10000000000;