Skip to content

Commit c6108f3

Browse files
refactor(governance): derive SelfDescribing for ManageNeuron nested types (#9045)
## Why The `From<T> for SelfDescribingValue` impls for `ManageNeuron` nested types were hand-written boilerplate that simply listed each field. The `SelfDescribing` derive macro can generate these automatically. ## What - Added `SelfDescribing` derive to 12 `ManageNeuron` nested types (e.g., `IncreaseDissolveDelay`, `Split`, `Spawn`, `Merge`, etc.) and the `NeuronIdOrSubaccount` oneof in the protobuf generator config. - Removed the corresponding hand-written `From<T> for SelfDescribingValue` impls (~130 lines of boilerplate). - Updated test imports accordingly. ## Testing Existing unit tests cover the removed impls and verify the derive-generated versions produce equivalent output.
1 parent 3ebbce4 commit c6108f3

File tree

4 files changed

+34
-143
lines changed

4 files changed

+34
-143
lines changed

rs/nns/governance/protobuf_generator/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ pub fn generate_prost_files(proto: ProtoPaths<'_>, out: &Path) {
158158
"NodeProvider",
159159
"RewardNodeProvider",
160160
"RewardNodeProviders",
161+
// ManageNeuron nested types
162+
"ManageNeuron.IncreaseDissolveDelay",
163+
"ManageNeuron.AddHotKey",
164+
"ManageNeuron.RemoveHotKey",
165+
"ManageNeuron.SetDissolveTimestamp",
166+
"ManageNeuron.ChangeAutoStakeMaturity",
167+
"ManageNeuron.Split",
168+
"ManageNeuron.Spawn",
169+
"ManageNeuron.StakeMaturity",
170+
"ManageNeuron.DisburseToNeuron",
171+
"ManageNeuron.Merge",
172+
"ManageNeuron.DisburseMaturity",
173+
// ManageNeuron oneof
174+
"ManageNeuron.neuron_id_or_subaccount",
161175
];
162176
for type_name in self_describing_types {
163177
config.type_attribute(

rs/nns/governance/src/gen/ic_nns_governance.pb.v1.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ pub mod manage_neuron {
626626
candid::Deserialize,
627627
serde::Serialize,
628628
comparable::Comparable,
629+
ic_nns_governance_derive_self_describing::SelfDescribing,
629630
Clone,
630631
Copy,
631632
PartialEq,
@@ -668,6 +669,7 @@ pub mod manage_neuron {
668669
candid::Deserialize,
669670
serde::Serialize,
670671
comparable::Comparable,
672+
ic_nns_governance_derive_self_describing::SelfDescribing,
671673
Clone,
672674
PartialEq,
673675
::prost::Message,
@@ -682,6 +684,7 @@ pub mod manage_neuron {
682684
candid::Deserialize,
683685
serde::Serialize,
684686
comparable::Comparable,
687+
ic_nns_governance_derive_self_describing::SelfDescribing,
685688
Clone,
686689
PartialEq,
687690
::prost::Message,
@@ -697,6 +700,7 @@ pub mod manage_neuron {
697700
candid::Deserialize,
698701
serde::Serialize,
699702
comparable::Comparable,
703+
ic_nns_governance_derive_self_describing::SelfDescribing,
700704
Clone,
701705
Copy,
702706
PartialEq,
@@ -739,6 +743,7 @@ pub mod manage_neuron {
739743
candid::Deserialize,
740744
serde::Serialize,
741745
comparable::Comparable,
746+
ic_nns_governance_derive_self_describing::SelfDescribing,
742747
Clone,
743748
Copy,
744749
PartialEq,
@@ -857,6 +862,7 @@ pub mod manage_neuron {
857862
candid::Deserialize,
858863
serde::Serialize,
859864
comparable::Comparable,
865+
ic_nns_governance_derive_self_describing::SelfDescribing,
860866
Clone,
861867
Copy,
862868
PartialEq,
@@ -876,6 +882,7 @@ pub mod manage_neuron {
876882
candid::Deserialize,
877883
serde::Serialize,
878884
comparable::Comparable,
885+
ic_nns_governance_derive_self_describing::SelfDescribing,
879886
Clone,
880887
Copy,
881888
PartialEq,
@@ -896,6 +903,7 @@ pub mod manage_neuron {
896903
candid::Deserialize,
897904
serde::Serialize,
898905
comparable::Comparable,
906+
ic_nns_governance_derive_self_describing::SelfDescribing,
899907
Clone,
900908
PartialEq,
901909
::prost::Message,
@@ -940,6 +948,7 @@ pub mod manage_neuron {
940948
candid::Deserialize,
941949
serde::Serialize,
942950
comparable::Comparable,
951+
ic_nns_governance_derive_self_describing::SelfDescribing,
943952
Clone,
944953
Copy,
945954
PartialEq,
@@ -958,6 +967,7 @@ pub mod manage_neuron {
958967
candid::Deserialize,
959968
serde::Serialize,
960969
comparable::Comparable,
970+
ic_nns_governance_derive_self_describing::SelfDescribing,
961971
Clone,
962972
PartialEq,
963973
::prost::Message,
@@ -1117,6 +1127,7 @@ pub mod manage_neuron {
11171127
candid::Deserialize,
11181128
serde::Serialize,
11191129
comparable::Comparable,
1130+
ic_nns_governance_derive_self_describing::SelfDescribing,
11201131
Clone,
11211132
PartialEq,
11221133
::prost::Message,
@@ -1172,6 +1183,7 @@ pub mod manage_neuron {
11721183
candid::Deserialize,
11731184
serde::Serialize,
11741185
comparable::Comparable,
1186+
ic_nns_governance_derive_self_describing::SelfDescribing,
11751187
Clone,
11761188
PartialEq,
11771189
::prost::Oneof,

rs/nns/governance/src/proposals/manage_neuron.rs

Lines changed: 3 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ use crate::{
22
pb::v1::{
33
Empty, ManageNeuron, SelfDescribingValue, Topic, Visibility, Vote,
44
manage_neuron::{
5-
AddHotKey, ChangeAutoStakeMaturity, ClaimOrRefresh, Command, Configure, Disburse,
6-
DisburseMaturity, DisburseToNeuron, Follow, IncreaseDissolveDelay, JoinCommunityFund,
7-
LeaveCommunityFund, Merge, NeuronIdOrSubaccount, RefreshVotingPower, RegisterVote,
8-
RemoveHotKey, SetDissolveTimestamp, SetFollowing, SetVisibility, Spawn, Split,
9-
StakeMaturity, StartDissolving, StopDissolving,
5+
ClaimOrRefresh, Command, Configure, Disburse, Follow, JoinCommunityFund,
6+
LeaveCommunityFund, NeuronIdOrSubaccount, RefreshVotingPower, RegisterVote,
7+
SetFollowing, SetVisibility, StartDissolving, StopDissolving,
108
claim_or_refresh::{By as ClaimOrRefreshBy, MemoAndController},
119
configure::Operation,
1210
set_following::FolloweesForTopic,
@@ -80,19 +78,6 @@ impl From<ManageNeuron> for SelfDescribingValue {
8078
}
8179
}
8280

83-
impl From<NeuronIdOrSubaccount> for SelfDescribingValue {
84-
fn from(id: NeuronIdOrSubaccount) -> Self {
85-
match id {
86-
NeuronIdOrSubaccount::NeuronId(neuron_id) => {
87-
ValueBuilder::new().add_field("NeuronId", neuron_id).build()
88-
}
89-
NeuronIdOrSubaccount::Subaccount(subaccount) => ValueBuilder::new()
90-
.add_field("Subaccount", subaccount)
91-
.build(),
92-
}
93-
}
94-
}
95-
9681
impl From<Command> for SelfDescribingValue {
9782
fn from(command: Command) -> Self {
9883
use Command as C;
@@ -163,20 +148,6 @@ impl From<Configure> for SelfDescribingValue {
163148
}
164149
}
165150

166-
impl From<IncreaseDissolveDelay> for SelfDescribingValue {
167-
fn from(value: IncreaseDissolveDelay) -> Self {
168-
let IncreaseDissolveDelay {
169-
additional_dissolve_delay_seconds,
170-
} = value;
171-
ValueBuilder::new()
172-
.add_field(
173-
"additional_dissolve_delay_seconds",
174-
additional_dissolve_delay_seconds,
175-
)
176-
.build()
177-
}
178-
}
179-
180151
impl From<StartDissolving> for SelfDescribingValue {
181152
fn from(value: StartDissolving) -> Self {
182153
let StartDissolving {} = value;
@@ -191,29 +162,6 @@ impl From<StopDissolving> for SelfDescribingValue {
191162
}
192163
}
193164

194-
impl From<AddHotKey> for SelfDescribingValue {
195-
fn from(value: AddHotKey) -> Self {
196-
let AddHotKey { new_hot_key } = value;
197-
Self::singleton_map("new_hot_key", new_hot_key)
198-
}
199-
}
200-
201-
impl From<RemoveHotKey> for SelfDescribingValue {
202-
fn from(value: RemoveHotKey) -> Self {
203-
let RemoveHotKey { hot_key_to_remove } = value;
204-
Self::singleton_map("hot_key_to_remove", hot_key_to_remove)
205-
}
206-
}
207-
208-
impl From<SetDissolveTimestamp> for SelfDescribingValue {
209-
fn from(value: SetDissolveTimestamp) -> Self {
210-
let SetDissolveTimestamp {
211-
dissolve_timestamp_seconds,
212-
} = value;
213-
Self::singleton_map("dissolve_timestamp_seconds", dissolve_timestamp_seconds)
214-
}
215-
}
216-
217165
impl From<JoinCommunityFund> for SelfDescribingValue {
218166
fn from(value: JoinCommunityFund) -> Self {
219167
let JoinCommunityFund {} = value;
@@ -228,18 +176,6 @@ impl From<LeaveCommunityFund> for SelfDescribingValue {
228176
}
229177
}
230178

231-
impl From<ChangeAutoStakeMaturity> for SelfDescribingValue {
232-
fn from(value: ChangeAutoStakeMaturity) -> Self {
233-
let ChangeAutoStakeMaturity {
234-
requested_setting_for_auto_stake_maturity,
235-
} = value;
236-
Self::singleton_map(
237-
"requested_setting_for_auto_stake_maturity",
238-
requested_setting_for_auto_stake_maturity,
239-
)
240-
}
241-
}
242-
243179
impl From<SetVisibility> for SelfDescribingValue {
244180
fn from(value: SetVisibility) -> Self {
245181
let SetVisibility { visibility } = value;
@@ -259,59 +195,6 @@ impl From<Disburse> for SelfDescribingValue {
259195
}
260196
}
261197

262-
impl From<Split> for SelfDescribingValue {
263-
fn from(value: Split) -> Self {
264-
let Split { amount_e8s, memo } = value;
265-
ValueBuilder::new()
266-
.add_field("amount_e8s", amount_e8s)
267-
.add_field("memo", memo)
268-
.build()
269-
}
270-
}
271-
272-
impl From<Spawn> for SelfDescribingValue {
273-
fn from(value: Spawn) -> Self {
274-
let Spawn {
275-
new_controller,
276-
nonce,
277-
percentage_to_spawn,
278-
} = value;
279-
ValueBuilder::new()
280-
.add_field("new_controller", new_controller)
281-
.add_field("nonce", nonce)
282-
.add_field("percentage_to_spawn", percentage_to_spawn)
283-
.build()
284-
}
285-
}
286-
287-
impl From<StakeMaturity> for SelfDescribingValue {
288-
fn from(value: StakeMaturity) -> Self {
289-
let StakeMaturity {
290-
percentage_to_stake,
291-
} = value;
292-
Self::singleton_map("percentage_to_stake", percentage_to_stake)
293-
}
294-
}
295-
296-
impl From<DisburseToNeuron> for SelfDescribingValue {
297-
fn from(value: DisburseToNeuron) -> Self {
298-
let DisburseToNeuron {
299-
new_controller,
300-
amount_e8s,
301-
dissolve_delay_seconds,
302-
kyc_verified,
303-
nonce,
304-
} = value;
305-
ValueBuilder::new()
306-
.add_field("new_controller", new_controller)
307-
.add_field("amount_e8s", amount_e8s)
308-
.add_field("dissolve_delay_seconds", dissolve_delay_seconds)
309-
.add_field("kyc_verified", kyc_verified)
310-
.add_field("nonce", nonce)
311-
.build()
312-
}
313-
}
314-
315198
impl From<Follow> for SelfDescribingValue {
316199
fn from(follow: Follow) -> Self {
317200
let Follow { topic, followees } = follow;
@@ -368,35 +251,13 @@ impl From<ClaimOrRefresh> for SelfDescribingValue {
368251
}
369252
}
370253

371-
impl From<Merge> for SelfDescribingValue {
372-
fn from(merge: Merge) -> Self {
373-
let Merge { source_neuron_id } = merge;
374-
Self::singleton_map("source_neuron_id", source_neuron_id)
375-
}
376-
}
377-
378254
impl From<RefreshVotingPower> for SelfDescribingValue {
379255
fn from(value: RefreshVotingPower) -> Self {
380256
let RefreshVotingPower {} = value;
381257
SelfDescribingValue::NULL
382258
}
383259
}
384260

385-
impl From<DisburseMaturity> for SelfDescribingValue {
386-
fn from(value: DisburseMaturity) -> Self {
387-
let DisburseMaturity {
388-
percentage_to_disburse,
389-
to_account,
390-
to_account_identifier,
391-
} = value;
392-
ValueBuilder::new()
393-
.add_field("percentage_to_disburse", percentage_to_disburse)
394-
.add_field("to_account", to_account)
395-
.add_field("to_account_identifier", to_account_identifier)
396-
.build()
397-
}
398-
}
399-
400261
impl From<SetFollowing> for SelfDescribingValue {
401262
fn from(value: SetFollowing) -> Self {
402263
let SetFollowing { topic_following } = value;

rs/nns/governance/src/proposals/manage_neuron_tests.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ use super::*;
22

33
use crate::pb::v1::{
44
Account, SelfDescribingValue as SelfDescribingValuePb, Vote,
5-
manage_neuron::{claim_or_refresh, configure::Operation, disburse::Amount, set_following},
5+
manage_neuron::{
6+
AddHotKey, ChangeAutoStakeMaturity, DisburseMaturity, DisburseToNeuron,
7+
IncreaseDissolveDelay, Merge, RemoveHotKey, SetDissolveTimestamp, Spawn, Split,
8+
StakeMaturity, claim_or_refresh, configure::Operation, disburse::Amount, set_following,
9+
},
610
};
711

812
use ic_base_types::PrincipalId;

0 commit comments

Comments
 (0)