Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit eb7b4cb

Browse files
committed
Deadline is no longer optional, also add expiration test
1 parent 90698e4 commit eb7b4cb

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

feature-proposal/program/src/instruction.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub enum FeatureProposalInstruction {
7676

7777
impl Sealed for FeatureProposalInstruction {}
7878
impl Pack for FeatureProposalInstruction {
79-
const LEN: usize = 26; // see `test_get_packed_len()` for justification of "18"
79+
const LEN: usize = 25; // see `test_get_packed_len()` for justification of "18"
8080

8181
fn pack_into_slice(&self, dst: &mut [u8]) {
8282
let data = self.pack_into_vec();
@@ -177,12 +177,15 @@ mod tests {
177177
tokens_to_mint: 42,
178178
acceptance_criteria: AcceptanceCriteria {
179179
tokens_required: 0xdeadbeefdeadbeef,
180-
deadline: None,
180+
deadline: -1,
181181
}
182182
}
183183
.try_to_vec()
184184
.unwrap(),
185-
vec![0, 42, 0, 0, 0, 0, 0, 0, 0, 239, 190, 173, 222, 239, 190, 173, 222, 0]
185+
vec![
186+
0, 42, 0, 0, 0, 0, 0, 0, 0, 239, 190, 173, 222, 239, 190, 173, 222, 255, 255, 255,
187+
255, 255, 255, 255, 255
188+
]
186189
);
187190
}
188191

feature-proposal/program/src/processor.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,13 @@ pub fn process_instruction(
324324
&[feature_id_bump_seed],
325325
];
326326

327-
if let Some(deadline) = acceptance_criteria.deadline {
328-
if clock.unix_timestamp >= deadline {
329-
info!("Feature proposal expired");
330-
FeatureProposal::Expired
331-
.pack_into_slice(&mut feature_proposal_info.data.borrow_mut());
332-
return Ok(());
333-
}
327+
if clock.unix_timestamp >= acceptance_criteria.deadline {
328+
info!("Feature proposal expired");
329+
FeatureProposal::Expired
330+
.pack_into_slice(&mut feature_proposal_info.data.borrow_mut());
331+
return Ok(());
334332
}
333+
335334
info!("Unpacking acceptance token account");
336335
let acceptance_token =
337336
spl_token::state::Account::unpack(&acceptance_token_info.data.borrow())?;

feature-proposal/program/src/state.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ pub struct AcceptanceCriteria {
1515
pub tokens_required: u64,
1616

1717
/// If the required tokens are not tallied by this deadline then the proposal will expire.
18-
/// `None` if the proposal should never expire.
19-
pub deadline: Option<UnixTimestamp>,
18+
pub deadline: UnixTimestamp,
2019
}
2120

2221
/// Contents of a Feature Proposal account
@@ -38,7 +37,7 @@ pub enum FeatureProposal {
3837
impl Sealed for FeatureProposal {}
3938

4039
impl Pack for FeatureProposal {
41-
const LEN: usize = 18; // see `test_get_packed_len()` for justification of "18"
40+
const LEN: usize = 17; // see `test_get_packed_len()` for justification of "18"
4241

4342
fn pack_into_slice(&self, dst: &mut [u8]) {
4443
let data = self.try_to_vec().unwrap();
@@ -77,21 +76,11 @@ mod tests {
7776
assert_eq!(
7877
FeatureProposal::Pending(AcceptanceCriteria {
7978
tokens_required: 0xdeadbeefdeadbeef,
80-
deadline: None,
79+
deadline: -1,
8180
})
8281
.try_to_vec()
8382
.unwrap(),
84-
vec![1, 239, 190, 173, 222, 239, 190, 173, 222, 0],
85-
);
86-
87-
assert_eq!(
88-
FeatureProposal::Pending(AcceptanceCriteria {
89-
tokens_required: 0,
90-
deadline: Some(-1),
91-
})
92-
.try_to_vec()
93-
.unwrap(),
94-
vec![1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, 255, 255, 255, 255, 255],
83+
vec![1, 239, 190, 173, 222, 239, 190, 173, 222, 255, 255, 255, 255, 255, 255, 255, 255],
9584
);
9685
}
9786

feature-proposal/program/tests/functional.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async fn test_basic() {
6868
42,
6969
AcceptanceCriteria {
7070
tokens_required: 42,
71-
deadline: None,
71+
deadline: i64::MAX,
7272
},
7373
)],
7474
Some(&payer.pubkey()),
@@ -183,4 +183,41 @@ async fn test_basic() {
183183
));
184184
}
185185

186-
// TODO: more tests....
186+
#[tokio::test]
187+
async fn test_expired() {
188+
let feature_proposal = Keypair::new();
189+
190+
let (mut banks_client, payer, recent_blockhash) = program_test().start().await;
191+
192+
// Create a new feature proposal
193+
let mut transaction = Transaction::new_with_payer(
194+
&[propose(
195+
&payer.pubkey(),
196+
&feature_proposal.pubkey(),
197+
42,
198+
AcceptanceCriteria {
199+
tokens_required: 42,
200+
deadline: 0, // <=== Already expired
201+
},
202+
)],
203+
Some(&payer.pubkey()),
204+
);
205+
transaction.sign(&[&payer, &feature_proposal], recent_blockhash);
206+
banks_client.process_transaction(transaction).await.unwrap();
207+
208+
assert!(matches!(
209+
get_account_data::<FeatureProposal>(&mut banks_client, feature_proposal.pubkey()).await,
210+
Ok(FeatureProposal::Pending(_))
211+
));
212+
213+
// Tally will cause the proposal to expire
214+
let mut transaction =
215+
Transaction::new_with_payer(&[tally(&feature_proposal.pubkey())], Some(&payer.pubkey()));
216+
transaction.sign(&[&payer], recent_blockhash);
217+
banks_client.process_transaction(transaction).await.unwrap();
218+
219+
assert!(matches!(
220+
get_account_data::<FeatureProposal>(&mut banks_client, feature_proposal.pubkey()).await,
221+
Ok(FeatureProposal::Expired)
222+
));
223+
}

0 commit comments

Comments
 (0)