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

Commit 3ab6d60

Browse files
committed
Fixed broken tests after rebase
1 parent f969ff7 commit 3ab6d60

File tree

6 files changed

+70
-47
lines changed

6 files changed

+70
-47
lines changed

src/bank.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ impl Bank {
240240
// Negative fee shouldn't be possible here, we checked for valid fees when
241241
// deserializing the transactions
242242
let total_cost_result = tx.instructions.iter().try_fold(tx.fee, |total, i| {
243-
if let Instruction::NewContract(contract) = i {
244-
if contract.tokens < 0 {
243+
if let Instruction::NewContract(box_contract) = i {
244+
if box_contract.tokens < 0 {
245245
return Err(BankError::NegativeTokens);
246246
}
247247

248-
Ok(total + contract.tokens)
248+
Ok(total + box_contract.tokens)
249249
} else {
250250
Ok(total)
251251
}
@@ -275,16 +275,16 @@ impl Bank {
275275
fn apply_credits(&self, tx: &Transaction, balances: &mut HashMap<PublicKey, i64>) {
276276
for i in &tx.instructions {
277277
match i {
278-
Instruction::NewContract(contract) => {
279-
let plan = contract.plan.clone();
278+
Instruction::NewContract(box_contract) => {
279+
let plan = box_contract.plan.clone();
280280
if let Some(payment) = plan.final_payment() {
281281
self.apply_payment(&payment, balances);
282282
} else {
283283
let mut pending = self
284284
.pending
285285
.write()
286286
.expect("'pending' write lock in apply_credits");
287-
pending.insert(contract.id, plan);
287+
pending.insert(box_contract.id, plan);
288288
}
289289
}
290290
Instruction::ApplyTimestamp(dt) => {
@@ -457,8 +457,8 @@ impl Bank {
457457
panic!("invalid ledger, first transaction is empty");
458458
}
459459

460-
let deposit = if let Instruction::NewContract(contract) = &tx.instructions[0] {
461-
contract.plan.final_payment()
460+
let deposit = if let Instruction::NewContract(box_contract) = &tx.instructions[0] {
461+
box_contract.plan.final_payment()
462462
} else {
463463
None
464464
}.expect("invalid ledger, needs to start with a contract");
@@ -721,8 +721,8 @@ mod tests {
721721
.unwrap();
722722

723723
let contract_id;
724-
if let Instruction::NewContract(contract) = &tx.instructions[0] {
725-
contract_id = contract.id;
724+
if let Instruction::NewContract(box_contract) = &tx.instructions[0] {
725+
contract_id = box_contract.id;
726726
} else {
727727
panic!("expecting contract instruction");
728728
}

src/entry_writer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ mod tests {
106106
use packet::BLOB_DATA_SIZE;
107107
use signature::{KeyPair, KeyPairUtil};
108108
use std::io::Cursor;
109-
use std::str;
110109
use transaction::{Transaction, BASE_TRANSACTION_SIZE, MAX_INSTRUCTION_SIZE};
111110

112111
#[test]
@@ -153,7 +152,7 @@ mod tests {
153152

154153
#[test]
155154
fn test_read_entries_from_buf() {
156-
let mint = Mint::new(1);
155+
let mut mint = Mint::new(1);
157156
let mut buf = vec![];
158157
let created_entries = mint.create_entries();
159158
EntryWriter::write_entries(&mut buf, created_entries.clone()).unwrap();

src/ledger.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,19 @@ pub fn next_entries(
312312
mod tests {
313313
use super::*;
314314
use bincode::serialized_size;
315+
use budget::{Budget, Condition};
315316
use chrono::prelude::*;
316317
use entry::{next_entry, Entry};
317318
use hash::hash;
318-
use packet::{BlobRecycler, BLOB_DATA_SIZE, PACKET_DATA_SIZE};
319+
use packet::{BlobRecycler, BLOB_DATA_SIZE};
320+
use payment_plan::Payment;
319321
use signature::{KeyPair, KeyPairUtil};
320322
use std;
321323
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
322-
use transaction::{Transaction, Vote, BASE_TRANSACTION_SIZE, MAX_INSTRUCTION_SIZE};
324+
use transaction::{
325+
Contract, Instruction, Plan, Transaction, Vote, BASE_TRANSACTION_SIZE, FEE_PER_INSTRUCTION,
326+
MAX_INSTRUCTION_SIZE,
327+
};
323328

324329
#[test]
325330
fn test_verify_slice() {
@@ -399,16 +404,34 @@ mod tests {
399404
next_id,
400405
2,
401406
);
402-
let tx_large = Transaction::new(&keypair, keypair.pubkey(), 1, next_id);
403407

404-
let tx_small_size = serialized_size(&tx_small).unwrap();
405-
let tx_large_size = serialized_size(&tx_large).unwrap();
408+
// Create large transaction
409+
let transfer_value = 1000;
410+
let date_condition = (
411+
Condition::Timestamp(Utc::now(), keypair.pubkey()),
412+
Payment {
413+
tokens: transfer_value,
414+
to: keypair.pubkey(),
415+
},
416+
);
417+
418+
let budget = Budget::Or(date_condition.clone(), date_condition);
419+
let plan = Plan::Budget(budget);
420+
let contract = Contract::new(transfer_value, plan);
421+
let tx_large = Transaction::new_from_instructions(
422+
&keypair,
423+
vec![Instruction::NewContract(Box::new(contract))],
424+
next_id,
425+
FEE_PER_INSTRUCTION as i64,
426+
);
427+
428+
let tx_small_size = serialized_size(&tx_small).unwrap() as usize;
429+
let tx_large_size = serialized_size(&tx_large).unwrap() as usize;
406430
assert!(tx_small_size < tx_large_size);
407-
assert!(tx_large_size < PACKET_DATA_SIZE as u64);
431+
assert!(tx_large_size <= BASE_TRANSACTION_SIZE + MAX_INSTRUCTION_SIZE);
408432

409-
let transaction_size = BASE_TRANSACTION_SIZE + MAX_INSTRUCTION_SIZE;
410433
// NOTE: if Entry grows to larger than a transaction, the code below falls over
411-
let threshold = (BLOB_DATA_SIZE / transaction_size) - 1; // 256 is transaction size
434+
let threshold = (BLOB_DATA_SIZE / (tx_small_size as usize)) - 1;
412435

413436
// verify no split
414437
let transactions = vec![tx_small.clone(); threshold];
@@ -432,7 +455,7 @@ mod tests {
432455
transactions.extend(large_transactions);
433456

434457
let entries0 = next_entries(&id, 0, transactions.clone());
435-
assert!(entries0.len() > 2);
458+
assert!(entries0.len() >= 2);
436459
assert!(entries0[0].has_more);
437460
assert!(!entries0[entries0.len() - 1].has_more);
438461
assert!(entries0.verify(&id));

src/mint.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ mod tests {
8181
fn test_create_transactions() {
8282
let mut transactions = Mint::new(100).create_transactions().into_iter();
8383
let tx = transactions.next().unwrap();
84-
if let Instruction::NewContract(contract) = &tx.instructions[0] {
85-
if let Plan::Budget(Budget::Pay(payment)) = &contract.plan {
84+
if let Instruction::NewContract(box_contract) = &tx.instructions[0] {
85+
if let Plan::Budget(Budget::Pay(payment)) = &box_contract.plan {
8686
assert_eq!(tx.from, payment.to);
8787
}
8888
}

src/thin_client.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ mod tests {
409409
let last_id = client.get_last_id();
410410

411411
let mut tr2 = Transaction::new(&alice.keypair(), bob_pubkey, 501, last_id);
412-
if let Instruction::NewContract(contract) = &mut tr2.instructions[0] {
413-
contract.tokens = 502;
414-
contract.plan = Plan::Budget(Budget::new_payment(502, bob_pubkey));
412+
if let Instruction::NewContract(box_contract) = &mut tr2.instructions[0] {
413+
box_contract.tokens = 502;
414+
box_contract.plan = Plan::Budget(Budget::new_payment(502, bob_pubkey));
415415
}
416416
let sig = client.transfer_signed(&tr2).unwrap();
417417
client.poll_for_signature(&sig).unwrap();
@@ -524,7 +524,7 @@ mod tests {
524524
let budget = Budget::Pay(payment);
525525
let plan = Plan::Budget(budget);
526526
let contract = Contract::new(transfer_value, plan);
527-
let instruction = Instruction::NewContract(contract);
527+
let instruction = Instruction::NewContract(Box::new(contract));
528528
multi_instructions.push(instruction);
529529
}
530530

@@ -600,7 +600,7 @@ mod tests {
600600
expected_balance += transfer_value;
601601

602602
let date_condition = (
603-
Condition::Timestamp(Utc::now(), alice.pubkey()),
603+
Timestamp(Utc::now(), alice.pubkey()),
604604
Payment {
605605
tokens: transfer_value,
606606
to: bob_pubkey,
@@ -610,7 +610,7 @@ mod tests {
610610
let budget = Budget::Or(date_condition.clone(), date_condition);
611611
let plan = Plan::Budget(budget);
612612
let contract = Contract::new(transfer_value, plan);
613-
contract_instructions.push(Instruction::NewContract(contract));
613+
contract_instructions.push(Instruction::NewContract(Box::new(contract)));
614614
}
615615

616616
// Make contracts that need a signature
@@ -623,7 +623,7 @@ mod tests {
623623
let plan = Plan::Budget(budget);
624624
let contract = Contract::new(transfer_value, plan);
625625
contract_ids.push(contract.id);
626-
contract_instructions.push(Instruction::NewContract(contract));
626+
contract_instructions.push(Instruction::NewContract(Box::new(contract)));
627627
}
628628

629629
let contract_transaction = Transaction::new_from_instructions(

src/transaction.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub const PUB_KEY_OFFSET: usize = 80;
1313
pub const MAX_ALLOWED_INSTRUCTIONS: usize = 20;
1414
// The biggest current instruction is a Budget with a payment plan of 'Or' with two
1515
// datetime 'Condition' branches. This is the serialized size of that instruction
16-
pub const MAX_INSTRUCTION_SIZE: usize = 314;
16+
pub const MAX_INSTRUCTION_SIZE: usize = 352;
1717
// Serialized size of everything in the transaction excluding the instructions
1818
pub const BASE_TRANSACTION_SIZE: usize = 168;
1919
pub const FEE_PER_INSTRUCTION: usize = 0;
@@ -86,7 +86,7 @@ pub struct Vote {
8686
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
8787
pub enum Instruction {
8888
/// Declare and instanstansiate `Contract`.
89-
NewContract(Contract),
89+
NewContract(Box<Contract>),
9090

9191
/// Tell a payment plan acknowledge the given `DateTime` has past.
9292
ApplyTimestamp(DateTime<Utc>),
@@ -150,7 +150,7 @@ impl Transaction {
150150
let budget = Budget::Pay(payment);
151151
let plan = Plan::Budget(budget);
152152
let contract = Contract::new(tokens, plan);
153-
let instruction = Instruction::NewContract(contract);
153+
let instruction = Instruction::NewContract(Box::new(contract));
154154
Self::new_from_instructions(from_keypair, vec![instruction], last_id, fee)
155155
}
156156

@@ -195,7 +195,7 @@ impl Transaction {
195195
);
196196
let plan = Plan::Budget(budget);
197197
let contract = Contract::new(tokens, plan);
198-
let instructions = vec![Instruction::NewContract(contract)];
198+
let instructions = vec![Instruction::NewContract(Box::new(contract))];
199199
Self::new_from_instructions(from_keypair, instructions, last_id, 0)
200200
}
201201

@@ -230,8 +230,8 @@ impl Transaction {
230230
}
231231

232232
for i in &self.instructions {
233-
if let Instruction::NewContract(contract) = i {
234-
if !contract.plan.verify(contract.tokens) {
233+
if let Instruction::NewContract(contract_box) = i {
234+
if !contract_box.plan.verify(contract_box.tokens) {
235235
return false;
236236
}
237237
}
@@ -310,7 +310,7 @@ mod tests {
310310
});
311311
let plan = Plan::Budget(budget);
312312
let contract = Contract::new(0, plan);
313-
let instruction = Instruction::NewContract(contract);
313+
let instruction = Instruction::NewContract(Box::new(contract));
314314
let claim0 = Transaction {
315315
instructions: vec![instruction],
316316
from: Default::default(),
@@ -329,10 +329,11 @@ mod tests {
329329
let keypair = KeyPair::new();
330330
let pubkey = keypair.pubkey();
331331
let mut tx = Transaction::new(&keypair, pubkey, 42, zero);
332-
if let Instruction::NewContract(contract) = &mut tx.instructions[0] {
333-
contract.tokens = 1_000_000; // <-- attack, part 1!
334-
if let Plan::Budget(Budget::Pay(ref mut payment)) = contract.plan {
335-
payment.tokens = contract.tokens; // <-- attack, part 2!
332+
if let Instruction::NewContract(contract_box) = &mut tx.instructions[0] {
333+
let tokens = 1_000_000; // <-- attack, part 1!
334+
contract_box.tokens = tokens;
335+
if let Plan::Budget(Budget::Pay(ref mut payment)) = contract_box.plan {
336+
payment.tokens = tokens; // <-- attack, part 2!
336337
}
337338
}
338339
assert!(tx.verify_plan());
@@ -347,8 +348,8 @@ mod tests {
347348
let pubkey1 = keypair1.pubkey();
348349
let zero = Hash::default();
349350
let mut tx = Transaction::new(&keypair0, pubkey1, 42, zero);
350-
if let Instruction::NewContract(contract) = &mut tx.instructions[0] {
351-
if let Plan::Budget(Budget::Pay(ref mut payment)) = contract.plan {
351+
if let Instruction::NewContract(contract_box) = &mut tx.instructions[0] {
352+
if let Plan::Budget(Budget::Pay(ref mut payment)) = contract_box.plan {
352353
payment.to = thief_keypair.pubkey(); // <-- attack!
353354
}
354355
}
@@ -371,16 +372,16 @@ mod tests {
371372
let keypair1 = KeyPair::new();
372373
let zero = Hash::default();
373374
let mut tx = Transaction::new(&keypair0, keypair1.pubkey(), 1, zero);
374-
if let Instruction::NewContract(contract) = &mut tx.instructions[0] {
375-
if let Plan::Budget(Budget::Pay(ref mut payment)) = contract.plan {
375+
if let Instruction::NewContract(contract_box) = &mut tx.instructions[0] {
376+
if let Plan::Budget(Budget::Pay(ref mut payment)) = contract_box.plan {
376377
payment.tokens = 2; // <-- attack!
377378
}
378379
}
379380
assert!(!tx.verify_plan());
380381

381382
// Also, ensure all branchs of the plan spend all tokens
382-
if let Instruction::NewContract(contract) = &mut tx.instructions[0] {
383-
if let Plan::Budget(Budget::Pay(ref mut payment)) = contract.plan {
383+
if let Instruction::NewContract(contract_box) = &mut tx.instructions[0] {
384+
if let Plan::Budget(Budget::Pay(ref mut payment)) = contract_box.plan {
384385
payment.tokens = 0; // <-- whoops!
385386
}
386387
}

0 commit comments

Comments
 (0)