-
Notifications
You must be signed in to change notification settings - Fork 55
/
ProposedProtocolParameterUpdates.ts
139 lines (118 loc) · 4.71 KB
/
ProposedProtocolParameterUpdates.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
import * as Cardano from '../../Cardano';
import * as Crypto from '@cardano-sdk/crypto';
import { CborReader, CborReaderState, CborWriter } from '../CBOR';
import { HexBlob } from '@cardano-sdk/util';
import { ProtocolParamUpdate } from './ProtocolParamUpdate';
/**
* In the Cardano network, stakeholders can propose changes to the protocol parameters. These proposals are then
* collected into a set which represents the ProposedProtocolParameterUpdates.
*
* This proposed protocol parameter updates are represented as a map of genesis delegate key hash to parameters updates. So in principles,
* each genesis delegate can propose a different update.
*/
export class ProposedProtocolParameterUpdates {
#proposedUpdates = new Map<Cardano.GenesisDelegateKeyHash, ProtocolParamUpdate>();
#originalBytes: HexBlob | undefined = undefined;
/**
* Initializes a new instance of the ProposedProtocolParameterUpdates class.
*
* @param proposedUpdates The proposed updates.
*/
constructor(proposedUpdates: Map<Cardano.GenesisDelegateKeyHash, ProtocolParamUpdate>) {
this.#proposedUpdates = proposedUpdates;
}
/**
* Serializes a ProposedProtocolParameterUpdates into CBOR format.
*
* @returns The ProposedProtocolParameterUpdates in CBOR format.
*/
toCbor(): HexBlob {
const writer = new CborWriter();
if (this.#originalBytes) return this.#originalBytes;
const sortedCanonically = new Map([...this.#proposedUpdates].sort((a, b) => (a > b ? 1 : -1)));
// CDDL
// proposed_protocol_parameter_updates =
// { * genesisDelegateKeyHash => protocol_param_update }
writer.writeStartMap(sortedCanonically.size);
for (const [key, value] of sortedCanonically) {
writer.writeByteString(Buffer.from(key, 'hex'));
writer.writeEncodedValue(Buffer.from(value.toCbor(), 'hex'));
}
return writer.encodeAsHex();
}
/**
* Deserializes the ProposedProtocolParameterUpdates from a CBOR byte array.
*
* @param cbor The CBOR encoded ProposedProtocolParameterUpdates object.
* @returns The new ProposedProtocolParameterUpdates instance.
*/
static fromCbor(cbor: HexBlob): ProposedProtocolParameterUpdates {
const reader = new CborReader(cbor);
const proposedUpdates = new Map<Cardano.GenesisDelegateKeyHash, ProtocolParamUpdate>();
reader.readStartMap();
while (reader.peekState() !== CborReaderState.EndMap) {
const genesisHash = Crypto.Hash28ByteBase16(HexBlob.fromBytes(reader.readByteString()));
const params = ProtocolParamUpdate.fromCbor(HexBlob.fromBytes(reader.readEncodedValue()));
proposedUpdates.set(genesisHash, params);
}
reader.readEndMap();
const updates = new ProposedProtocolParameterUpdates(proposedUpdates);
updates.#originalBytes = cbor;
return updates;
}
/**
* Creates a Core ProposedProtocolParameterUpdates object from the current ProposedProtocolParameterUpdates object.
*
* @returns The Core ProposedProtocolParameterUpdates object.
*/
toCore(): Cardano.ProposedProtocolParameterUpdates {
return new Map<Cardano.GenesisDelegateKeyHash, Cardano.ProtocolParametersUpdate>(
[...this.#proposedUpdates].map(([key, value]) => [key, value.toCore()])
);
}
/**
* Creates a ProposedProtocolParameterUpdates object from the given Core ProposedProtocolParameterUpdates object.
*
* @param updates core ProposedProtocolParameterUpdates object.
*/
static fromCore(updates: Cardano.ProposedProtocolParameterUpdates) {
return new ProposedProtocolParameterUpdates(
new Map<Cardano.GenesisDelegateKeyHash, ProtocolParamUpdate>(
[...updates].map(([key, value]) => [key, ProtocolParamUpdate.fromCore(value)])
)
);
}
/**
* Gets the number of elements in the proposed updates map.
*
* @returns The number of entries in the map.
*/
size(): number {
return this.#proposedUpdates.size;
}
/**
* Inserts a new ProtocolParametersUpdate in the map.
*
* @param key The key hash of the genesis delegate proposing the update.
* @param value The parameters that are being proposed for the update.
*/
insert(key: Cardano.GenesisDelegateKeyHash, value: ProtocolParamUpdate): void {
this.#proposedUpdates.set(key, value);
}
/**
* Gets a ProtocolParametersUpdate given a genesis delegate key hash.
*
* @param key The key hash of the genesis delegate proposing the update.
*/
get(key: Cardano.GenesisDelegateKeyHash): ProtocolParamUpdate | undefined {
return this.#proposedUpdates.get(key);
}
/**
* Gets the genesis delegate key hashes present in the map.
*
* @returns The genesis delegate key hashes.
*/
keys(): Array<Cardano.GenesisDelegateKeyHash> {
return [...this.#proposedUpdates.keys()];
}
}