Skip to content

Commit 171ee31

Browse files
committed
impl and test Remove for cPGF
1 parent 73edfd5 commit 171ee31

File tree

8 files changed

+580
-42
lines changed

8 files changed

+580
-42
lines changed

crates/governance/src/cli/onchain.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@ impl PgfAction {
289289
}
290290
}
291291

292+
impl Display for PgfAction {
293+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
294+
match self {
295+
PgfAction::Add => write!(f, "Add"),
296+
PgfAction::Remove => write!(f, "Remove"),
297+
}
298+
}
299+
}
300+
292301
/// PGF funding
293302
#[derive(
294303
Debug,
@@ -301,7 +310,7 @@ impl PgfAction {
301310
)]
302311
pub struct PgfFunding {
303312
/// PGF continuous funding
304-
pub continuous: Vec<ContPGFTarget>,
313+
pub continuous: Vec<PgfContinuous>,
305314
/// PGF retro fundings
306315
pub retro: Vec<PGFTarget>,
307316
}
@@ -341,6 +350,12 @@ pub struct PgfContinuous {
341350
pub action: PgfAction,
342351
}
343352

353+
impl Display for PgfContinuous {
354+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
355+
write!(f, "\n{}:\n{}", &self.action, &self.target)
356+
}
357+
}
358+
344359
/// PGF retro funding
345360
#[derive(
346361
Debug,

crates/governance/src/pgf/storage/keys.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ pub fn fundings_key_prefix() -> Key {
6161
}
6262
}
6363

64-
/// LazyMap handler for the pgf fundings substorage
64+
/// Nested LazyMap handler for the continuous PGF substorage. Structure: Address
65+
/// -> Proposal ID -> Target data
6566
pub fn fundings_handle()
6667
-> lazy_map::NestedMap<String, LazyMap<u64, ContPGFTarget>> {
6768
LazyMap::open(fundings_key_prefix())

crates/governance/src/storage/mod.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use namada_core::hash::Hash;
1717
use namada_core::token;
1818
use namada_state::{iter_prefix, Error, Result, StorageRead, StorageWrite};
1919
use namada_systems::trans_token;
20+
use proposal::{ContPGFTarget, PGFAction};
2021

2122
use crate::parameters::GovernanceParameters;
2223
use crate::storage::keys as governance_keys;
@@ -52,7 +53,7 @@ where
5253
storage.write(&author_key, data.author.clone())?;
5354

5455
let proposal_type_key = governance_keys::get_proposal_type_key(proposal_id);
55-
match data.r#type {
56+
match data.r#type.clone() {
5657
ProposalType::DefaultWithWasm(_) => {
5758
storage.write(&proposal_type_key, data.r#type.clone())?;
5859
let proposal_code_key =
@@ -62,6 +63,35 @@ where
6263
code.ok_or(Error::new_const("Missing proposal code"))?;
6364
storage.write(&proposal_code_key, proposal_code)?;
6465
}
66+
ProposalType::PGFPayment(a) => {
67+
let transformed_data = a
68+
.into_iter()
69+
.map(|action| match action {
70+
proposal::PGFAction::Continuous(add_remove) => {
71+
let pgf_target = match add_remove {
72+
proposal::AddRemove::Add(c_tgt) => {
73+
proposal::AddRemove::Add(ContPGFTarget {
74+
target: c_tgt.target,
75+
end_epoch: c_tgt.end_epoch,
76+
proposal_id,
77+
})
78+
}
79+
proposal::AddRemove::Remove(c) => {
80+
proposal::AddRemove::Remove(c)
81+
}
82+
};
83+
proposal::PGFAction::Continuous(pgf_target)
84+
}
85+
proposal::PGFAction::Retro(pgftarget) => {
86+
proposal::PGFAction::Retro(pgftarget)
87+
}
88+
})
89+
.collect::<BTreeSet<PGFAction>>();
90+
storage.write(
91+
&proposal_type_key,
92+
ProposalType::PGFPayment(transformed_data),
93+
)?;
94+
}
6595
_ => storage.write(&proposal_type_key, data.r#type.clone())?,
6696
}
6797

crates/governance/src/storage/proposal.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ impl TryFrom<PgfFundingProposal> for InitProposalData {
139139
.continuous
140140
.iter()
141141
.cloned()
142-
.map(|c_target| {
143-
if c_target.target.amount().is_zero() {
144-
PGFAction::Continuous(AddRemove::Remove(c_target))
145-
} else {
146-
PGFAction::Continuous(AddRemove::Add(c_target))
142+
.map(|c_target| match c_target.action {
143+
PgfAction::Add => {
144+
PGFAction::Continuous(AddRemove::Add(c_target.target))
145+
}
146+
PgfAction::Remove => {
147+
PGFAction::Continuous(AddRemove::Remove(c_target.target))
147148
}
148149
})
149150
.collect::<BTreeSet<PGFAction>>();
@@ -284,6 +285,8 @@ pub struct ContPGFTarget {
284285
pub target: PGFTarget,
285286
/// The epoch at which the funding ends, if any
286287
pub end_epoch: Option<Epoch>,
288+
/// The proposal ID that added this PGF payment
289+
pub proposal_id: u64,
287290
}
288291

289292
impl Display for ContPGFTarget {
@@ -746,10 +749,11 @@ pub mod testing {
746749
pub fn arb_cpgf_target()(
747750
target in arb_pgf_target(),
748751
end_epoch in arb_epoch_opt(),
752+
proposal_id in 0..u64::MAX,
749753
) -> ContPGFTarget {
750754
ContPGFTarget {
751755
target,
752-
end_epoch,
756+
end_epoch,proposal_id
753757
}
754758
}
755759
}

crates/governance/src/vp/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -527,18 +527,9 @@ where
527527
));
528528
}
529529

530-
// can't remove and add the same target in the same proposal
531-
let are_targets_unique = are_continuous_add_targets_unique
532-
.intersection(&are_continuous_remove_targets_unique)
533-
.count() as u64
534-
== 0;
535-
536-
are_targets_unique.ok_or_else(|| {
537-
Error::new_const(
538-
"One or more payment targets were added and removed \
539-
in the same proposal",
540-
)
541-
})
530+
// Should we add a check to the VP for the new data format?
531+
532+
Ok(())
542533
}
543534
// Default proposal condition are checked already for all other
544535
// proposals.

crates/sdk/src/signing.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,10 +998,12 @@ fn proposal_type_to_ledger_vector(
998998
PGFAction::Continuous(AddRemove::Add(ContPGFTarget {
999999
target: PGFTarget::Internal(target),
10001000
end_epoch,
1001+
proposal_id,
10011002
})) => {
10021003
output.push(
10031004
"PGF Action : Add Continuous Payment".to_string(),
10041005
);
1006+
output.push(format!("Proposal ID: {}", proposal_id));
10051007
output.push(format!("Target: {}", target.target));
10061008
output.push(format!(
10071009
"Amount: NAM {}",
@@ -1021,10 +1023,12 @@ fn proposal_type_to_ledger_vector(
10211023
PGFAction::Continuous(AddRemove::Add(ContPGFTarget {
10221024
target: PGFTarget::Ibc(target),
10231025
end_epoch,
1026+
proposal_id,
10241027
})) => {
10251028
output.push(
10261029
"PGF Action : Add Continuous Payment".to_string(),
10271030
);
1031+
output.push(format!("Proposal ID: {}", proposal_id));
10281032
output.push(format!("Target: {}", target.target));
10291033
output.push(format!(
10301034
"Amount: NAM {}",
@@ -1048,12 +1052,14 @@ fn proposal_type_to_ledger_vector(
10481052
ContPGFTarget {
10491053
target: PGFTarget::Internal(target),
10501054
end_epoch,
1055+
proposal_id,
10511056
},
10521057
)) => {
10531058
output.push(
10541059
"PGF Action : Remove Continuous Payment"
10551060
.to_string(),
10561061
);
1062+
output.push(format!("Proposal ID: {}", proposal_id));
10571063
output.push(format!("Target: {}", target.target));
10581064
output.push(format!(
10591065
"Amount: NAM {}",
@@ -1074,12 +1080,14 @@ fn proposal_type_to_ledger_vector(
10741080
ContPGFTarget {
10751081
target: PGFTarget::Ibc(target),
10761082
end_epoch,
1083+
proposal_id,
10771084
},
10781085
)) => {
10791086
output.push(
10801087
"PGF Action : Remove Continuous Payment"
10811088
.to_string(),
10821089
);
1090+
output.push(format!("Proposal ID: {}", proposal_id));
10831091
output.push(format!("Target: {}", target.target));
10841092
output.push(format!(
10851093
"Amount: NAM {}",
@@ -3086,6 +3094,7 @@ mod test_signing {
30863094
amount: Amount::zero(),
30873095
}),
30883096
end_epoch: Some(Epoch::from(1)),
3097+
proposal_id: 0,
30893098
}), // TODO: ask Murisi if this is ok
30903099
)])),
30913100
&tx,
@@ -3111,6 +3120,7 @@ mod test_signing {
31113120
amount: Amount::zero(),
31123121
}),
31133122
end_epoch: Some(Epoch::from(1)),
3123+
proposal_id: 0,
31143124
}), // TODO: ask Murisi if this is ok
31153125
)])),
31163126
&tx,
@@ -3161,6 +3171,7 @@ mod test_signing {
31613171
channel_id: ChannelId::new(16),
31623172
}),
31633173
end_epoch: None,
3174+
proposal_id: 0,
31643175
}), // TODO: ask Murisi if this is ok
31653176
)])),
31663177
&tx,
@@ -3191,6 +3202,7 @@ mod test_signing {
31913202
channel_id: ChannelId::new(16),
31923203
}),
31933204
end_epoch: None,
3205+
proposal_id: 0,
31943206
}), // TODO: ask Murisi if this is ok
31953207
)])),
31963208
&tx,

crates/tests/src/e2e/ibc_tests.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use namada_apps_lib::tendermint_rpc::{Client, HttpClient, Url};
2626
use namada_core::masp::PaymentAddress;
2727
use namada_sdk::address::MASP;
2828
use namada_sdk::chain::Epoch;
29-
use namada_sdk::governance::cli::onchain::PgfFunding;
29+
use namada_sdk::governance::cli::onchain::{
30+
PgfAction, PgfContinuous, PgfFunding,
31+
};
3032
use namada_sdk::governance::pgf::ADDRESS as PGF_ADDRESS;
3133
use namada_sdk::governance::storage::proposal::{PGFIbcTarget, PGFTarget};
3234
use namada_sdk::ibc::apps::nft_transfer::types::{
@@ -2661,14 +2663,18 @@ fn propose_funding(
26612663
src_channel_id: &ChannelId,
26622664
) -> Result<Epoch> {
26632665
let pgf_funding = PgfFunding {
2664-
continuous: vec![ContPGFTarget {
2665-
target: PGFTarget::Ibc(PGFIbcTarget {
2666-
amount: Amount::native_whole(10),
2667-
target: continuous_receiver.as_ref().to_string(),
2668-
port_id: src_port_id.clone(),
2669-
channel_id: src_channel_id.clone(),
2670-
}),
2671-
end_epoch: None,
2666+
continuous: vec![PgfContinuous {
2667+
target: ContPGFTarget {
2668+
target: PGFTarget::Ibc(PGFIbcTarget {
2669+
amount: Amount::native_whole(10),
2670+
target: continuous_receiver.as_ref().to_string(),
2671+
port_id: src_port_id.clone(),
2672+
channel_id: src_channel_id.clone(),
2673+
}),
2674+
end_epoch: None,
2675+
proposal_id: 0,
2676+
},
2677+
action: PgfAction::Add,
26722678
}],
26732679
retro: vec![PGFTarget::Ibc(PGFIbcTarget {
26742680
amount: Amount::native_whole(5),

0 commit comments

Comments
 (0)