-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathPoolVotingThresholds.ts
226 lines (200 loc) · 7.85 KB
/
PoolVotingThresholds.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import * as Cardano from '../../Cardano';
import { CborReader, CborWriter } from '../CBOR';
import { HexBlob, InvalidArgumentError } from '@cardano-sdk/util';
import { UnitInterval } from '../Common';
const POOL_VOTING_THRESHOLDS_SIZE = 4;
/**
* Governance actions are ratified through on-chain voting. Different
* kinds of governance actions have different ratification requirements. One of those
* requirements is the approval of the action by SPOs. These thresholds specify
* the percentage of the stake held by all stake pools that must be meet by the SPOs who
* vote Yes for the approval to be successful.
*/
export class PoolVotingThresholds {
#motionNoConfidence: UnitInterval;
#committeeNormal: UnitInterval;
#committeeNoConfidence: UnitInterval;
#hardForkInitiation: UnitInterval;
#originalBytes: HexBlob | undefined = undefined;
/**
* Initialize a new instance of the PoolVotingThresholds class.
*
* @param motionNoConfidence Quorum threshold (percentage of the total active stake) that
* needs to be meet for a Motion of no-confidence to be enacted.
* @param committeeNormal Quorum threshold (percentage of the total active stake) that needs
* to be meet for a new committee to be elected if the constitutional committee is in a state of confidence.
* @param committeeNoConfidence Quorum threshold (percentage of the total active stake) that needs
* to be meet for a new committee to be elected if the constitutional committee is in a state of no-confidence.
* @param hardForkInitiation Quorum threshold (percentage of the total active stake) that
* needs to be meet to trigger a non-backwards compatible upgrade of the network (requires a prior software upgrade).
*/
constructor(
motionNoConfidence: UnitInterval,
committeeNormal: UnitInterval,
committeeNoConfidence: UnitInterval,
hardForkInitiation: UnitInterval
) {
this.#motionNoConfidence = motionNoConfidence;
this.#committeeNormal = committeeNormal;
this.#committeeNoConfidence = committeeNoConfidence;
this.#hardForkInitiation = hardForkInitiation;
}
/**
* Serializes a DrepVotingThresholds into CBOR format.
*
* @returns The DrepVotingThresholds in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
// CDDL
// pool_voting_thresholds =
// [ unit_interval ; motion no confidence
// , unit_interval ; committee normal
// , unit_interval ; committee no confidence
// , unit_interval ; hard fork initiation
// ]
writer.writeStartArray(POOL_VOTING_THRESHOLDS_SIZE);
writer.writeEncodedValue(Buffer.from(this.#motionNoConfidence.toCbor(), 'hex'));
writer.writeEncodedValue(Buffer.from(this.#committeeNormal.toCbor(), 'hex'));
writer.writeEncodedValue(Buffer.from(this.#committeeNoConfidence.toCbor(), 'hex'));
writer.writeEncodedValue(Buffer.from(this.#hardForkInitiation.toCbor(), 'hex'));
return writer.encodeAsHex();
}
/**
* Deserializes the PoolVotingThresholds from a CBOR byte array.
*
* @param cbor The CBOR encoded PoolVotingThresholds object.
* @returns The new PoolVotingThresholds instance.
*/
static fromCbor(cbor: HexBlob): PoolVotingThresholds {
const reader = new CborReader(cbor);
const length = reader.readStartArray();
if (length !== POOL_VOTING_THRESHOLDS_SIZE)
throw new InvalidArgumentError(
'cbor',
`Expected an array of ${POOL_VOTING_THRESHOLDS_SIZE} elements, but got an array of ${length} elements`
);
const motionNoConfidence = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
const committeeNormal = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
const committeeNoConfidence = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
const hardForkInitiation = UnitInterval.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
reader.readEndArray();
const thresholds = new PoolVotingThresholds(
motionNoConfidence,
committeeNormal,
committeeNoConfidence,
hardForkInitiation
);
thresholds.#originalBytes = cbor;
return thresholds;
}
/**
* Creates a Core PoolVotingThresholdsSHOLDS_SIZE object from the current PoolVotingThresholds object.
*
* @returns The Core Prices object.
*/
toCore(): Cardano.PoolVotingThresholds {
return {
committeeNoConfidence: this.#committeeNoConfidence.toCore(),
committeeNormal: this.#committeeNormal.toCore(),
hardForkInitiation: this.#hardForkInitiation.toCore(),
motionNoConfidence: this.#motionNoConfidence.toCore()
};
}
/**
* Creates a PoolVotingThresholds object from the given Core PoolVotingThresholdsSHOLDS_SIZE object.
*
* @param core core PoolVotingThresholdsSHOLDS_SIZE object.
*/
static fromCore(core: Cardano.PoolVotingThresholds) {
return new PoolVotingThresholds(
UnitInterval.fromCore(core.motionNoConfidence),
UnitInterval.fromCore(core.committeeNormal),
UnitInterval.fromCore(core.committeeNoConfidence),
UnitInterval.fromCore(core.hardForkInitiation)
);
}
/**
* Sets the quorum threshold (percentage of the total active stake) that
* needs to be meet for a Motion of no-confidence to be enacted.
*
* @param threshold The quorum threshold.
*/
setMotionNoConfidence(threshold: UnitInterval) {
this.#motionNoConfidence = threshold;
this.#originalBytes = undefined;
}
/**
* Sets the quorum threshold (percentage of the total active stake) that needs
* to be meet for a new committee to be elected if the constitutional committee
* is in a state of confidence.
*
* @param threshold The quorum threshold.
*/
setCommitteeNormal(threshold: UnitInterval) {
this.#committeeNormal = threshold;
this.#originalBytes = undefined;
}
/**
* Sets the quorum threshold (percentage of the total active stake) that needs
* to be meet for a new committee to be elected if the constitutional committee is
* in a state of no-confidence.
*
* @param threshold The quorum threshold.
*/
setCommitteeNoConfidence(threshold: UnitInterval) {
this.#committeeNoConfidence = threshold;
this.#originalBytes = undefined;
}
/**
* Sets the quorum threshold (percentage of the total active stake) that
* needs to be meet to trigger a non-backwards compatible upgrade of the network
* (requires a prior software upgrade).
*
* @param threshold The quorum threshold.
*/
setHardForkInitiation(threshold: UnitInterval) {
this.#hardForkInitiation = threshold;
this.#originalBytes = undefined;
}
/**
* Gets the quorum threshold (percentage of the total active stake) that
* needs to be meet for a Motion of no-confidence to be enacted.
*
* @returns The quorum threshold.
*/
motionNoConfidence(): UnitInterval {
return this.#motionNoConfidence;
}
/**
* Gets the quorum threshold (percentage of the total active stake) that needs
* to be meet for a new committee to be elected if the constitutional committee
* is in a state of confidence.
*
* @returns The quorum threshold.
*/
committeeNormal(): UnitInterval {
return this.#committeeNormal;
}
/**
* Gets the quorum threshold (percentage of the total active stake) that needs
* to be meet for a new committee to be elected if the constitutional committee is
* in a state of no-confidence.
*
* @returns The quorum threshold.
*/
committeeNoConfidence(): UnitInterval {
return this.#committeeNoConfidence;
}
/**
* Gets the quorum threshold (percentage of the total active stake) that
* needs to be meet to trigger a non-backwards compatible upgrade of the network
* (requires a prior software upgrade).
*
* @returns The quorum threshold.
*/
hardForkInitiation(): UnitInterval {
return this.#hardForkInitiation;
}
}