-
Notifications
You must be signed in to change notification settings - Fork 1
/
Validator.ts
209 lines (156 loc) · 6.39 KB
/
Validator.ts
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
import { NominationData } from "./NominationData";
import { Nominator } from "./Nominator";
import { Settings } from "./Settings";
import { ValidatorIdentity } from "./Types";
export class Validator {
private nominators: Nominator[]; //Stores nominator objects
private val_address: string; //The chain prefix applied address for the validator
private bonded_amount: number;//self stake
private val_identity: ValidatorIdentity; //Identity object
private era_points: number[];//An era point entry for each era validated
private commission: number;//Commission 1-100
private isBlocked: boolean;//Has the validator blocked nominations
private isValidator: boolean;//Does the stash have the validator intent
public constructor(val_address: string) {
this.val_address = val_address;
this.nominators = [];
this.val_identity = <ValidatorIdentity>{};
this.bonded_amount = -1;
this.isBlocked = true;
this.isValidator = false;
this.era_points = [];
this.commission = 100;
}
public addNominator(nominator: Nominator) {
this.nominators.push(nominator);
}
public addEraPoints(era_points: number) {
this.era_points.push(era_points);
}
public getAddress() {
return this.val_address;
}
public getBondedAmount(): number {
return this.bonded_amount;
}
public getParentIdentity() {
return this.val_identity.name;
}
public getIntent() {
return this.isValidator;
}
public getBlockedNominations() {
return this.isBlocked;
}
/* Retrieves the name of the validator in Primary\Sub format
If there isn't a sub just display the primary
*/
public getIdentityName() {
if (this.val_identity.sub_identity == undefined) {
return this.val_identity.name.trim();
} else {
return `${this.val_identity.name.trim()}\\${this.val_identity.sub_identity.trim()}`;
}
}
public getEraPoints() {
return this.era_points;
}
//Sum era points, then divide by the total number of entries
public getAverageEraPoints() {
if (this.era_points.length == 0)
return 0;
else
return this.era_points.reduce((x, y) => x + y, 0) / this.era_points.length;
}
//If you generated era points then you validated
public getNumberErasValidated() {
return this.era_points.length;
}
public getCommission() {
return this.commission;
}
//Gets the sum of all nominator bonds
public getNominations(): number {
var result = 0.0;
this.nominators.forEach(nominator => {
result += nominator.getBondedAmount();
});
return result;
}
/* SCORING SECTION
The following functions aid with establishing a score for each validator
*/
private getSelfStakeScore(): number {
const max_self_stake = NominationData.getInstance().getMaxSelfStake();
var result = (this.getBondedAmount() / max_self_stake) * Settings.w_self_stake;
result = result > Settings.w_self_stake ? Settings.w_self_stake : result;
if (Settings.debug) {
console.log(`${this.getIdentityName()} - Self Stake - (${this.getBondedAmount()}) Max - (${max_self_stake}) Result - [${result}]`);
}
return result;
}
private getEraPointsScore(): number {
const max_era_points = NominationData.getInstance().getMaxEraPoints();
const result = (this.getAverageEraPoints() / max_era_points) * Settings.w_era_points;
if (Settings.debug) {
console.log(`${this.getIdentityName()} - Average Era Points - (${this.getAverageEraPoints()}) Max - (${max_era_points}) Result - [${result}]`);
}
return result;
}
private getNominationScore(): number {
const nominator_data = NominationData.getInstance();
const mean = nominator_data.getAvergaeNominatorBond();
const sd = nominator_data.getStandardDeviation();
const val_sd = Math.abs((this.getNominations() - mean) / sd);
var result = ((Settings.std_dev_reward - val_sd) / Settings.std_dev_reward) * Settings.w_nominations;
result = result < 0 ? 0 : result;
if (Settings.debug) {
console.log(`${this.getIdentityName()} - Nominations - (${this.getNominations()}) Std Dev - (${val_sd}) Result - [${result}]`);
}
return result;
}
private getCommissionScore(): number {
var commission = this.getCommission() <= Settings.min_commission ? Settings.min_commission : this.getCommission();
const result = (1 - (commission / (Settings.max_commission - Settings.min_commission))) * Settings.w_commission;
if (Settings.debug) {
console.log(`${this.getIdentityName()} - Actual Commssion - (${this.getCommission()}) Adjusted Commission - (${commission}) Max - (${Settings.max_commission}) Result - [${result}]`);
}
return result;
}
private getNumErasValidationScore(): number {
var number_eras_active = this.getNumberErasValidated() <= Settings.validation_eras ? Settings.validation_eras : this.getNumberErasValidated();
var result = 0;
if (number_eras_active >= Settings.validation_eras) {
result = Settings.w_validation_eras;
} else {
result = (number_eras_active / Settings.validation_eras) * Settings.w_validation_eras;
}
if (Settings.debug) {
console.log(`${this.getIdentityName()} - Number of Eras active - (${number_eras_active}) Target - (${Settings.w_validation_eras}) Result - [${result}]`);
}
return result;
}
public getValidatorScore() {
return this.getSelfStakeScore() +
this.getEraPointsScore() +
this.getCommissionScore() +
this.getNominationScore() +
this.getNumErasValidationScore();
}
// Basic Mutators
public setIntention(intent: boolean) {
this.isValidator = intent;
}
public setBlocked(blocked: boolean) {
this.isBlocked = blocked;
}
public setBondedAmount(bonded_ammount: number) {
this.bonded_amount = bonded_ammount;
}
public setIdentity(val_identity: ValidatorIdentity) {
this.val_identity = val_identity;
}
public setCommission(commission: number) {
this.commission = commission;
}
}