Skip to content

Commit

Permalink
Rename TransactionData to Contract
Browse files Browse the repository at this point in the history
No longer a single place to get all the data that was signed.
  • Loading branch information
garious committed May 22, 2018
1 parent 22da233 commit 8041dbc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl Bank {
/// Deduct tokens from the 'from' address the account has sufficient
/// funds and isn't a duplicate.
pub fn process_verified_transaction_debits(&self, tr: &Transaction) -> Result<()> {
info!("Transaction {}", tr.data.tokens);
info!("Transaction {}", tr.contract.tokens);
let bals = self.balances
.read()
.expect("'balances' read lock in process_verified_transaction_debits");
Expand All @@ -178,14 +178,14 @@ impl Bank {
let bal = option.expect("assignment of option to bal");
let current = bal.load(Ordering::Relaxed) as i64;

if current < tr.data.tokens {
if current < tr.contract.tokens {
self.forget_signature_with_last_id(&tr.sig, &tr.last_id);
return Err(BankError::InsufficientFunds);
}

let result = bal.compare_exchange(
current as isize,
(current - tr.data.tokens) as isize,
(current - tr.contract.tokens) as isize,
Ordering::Relaxed,
Ordering::Relaxed,
);
Expand All @@ -201,7 +201,7 @@ impl Bank {
}

pub fn process_verified_transaction_credits(&self, tr: &Transaction) {
let mut plan = tr.data.plan.clone();
let mut plan = tr.contract.plan.clone();
plan.apply_witness(&Witness::Timestamp(*self.last_time
.read()
.expect("timestamp creation in process_verified_transaction_credits")));
Expand Down
2 changes: 1 addition & 1 deletion src/bin/testnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ fn main() {
// transfer to oneself.
let entry1: Entry = entries.next().unwrap();
let deposit = if let Event::Transaction(ref tr) = entry1.events[0] {
tr.data.plan.final_payment()
tr.contract.plan.final_payment()
} else {
None
};
Expand Down
2 changes: 1 addition & 1 deletion src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ mod tests {
fn test_create_events() {
let mut events = Mint::new(100).create_events().into_iter();
if let Event::Transaction(tr) = events.next().unwrap() {
if let Plan::Pay(payment) = tr.data.plan {
if let Plan::Pay(payment) = tr.contract.plan {
assert_eq!(tr.from, payment.to);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/thin_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ mod tests {
let last_id = client.get_last_id().wait().unwrap();

let mut tr2 = Transaction::new(&alice.keypair(), bob_pubkey, 501, last_id);
tr2.data.tokens = 502;
tr2.data.plan = Plan::new_payment(502, bob_pubkey);
tr2.contract.tokens = 502;
tr2.contract.plan = Plan::new_payment(502, bob_pubkey);
let _sig = client.transfer_signed(tr2).unwrap();

assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 500);
Expand Down
26 changes: 13 additions & 13 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const SIG_OFFSET: usize = 8;
pub const PUB_KEY_OFFSET: usize = 80;

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct TransactionData {
pub struct Contract {
pub tokens: i64,
pub plan: Plan,
}
Expand All @@ -21,7 +21,7 @@ pub struct TransactionData {
pub struct Transaction {
pub sig: Signature,
pub from: PublicKey,
pub data: TransactionData,
pub contract: Contract,
pub last_id: Hash,
}

Expand All @@ -32,7 +32,7 @@ impl Transaction {
let plan = Plan::Pay(Payment { tokens, to });
let mut tr = Transaction {
sig: Signature::default(),
data: TransactionData { plan, tokens },
contract: Contract { plan, tokens },
last_id,
from,
};
Expand All @@ -54,7 +54,7 @@ impl Transaction {
(Condition::Signature(from), Payment { tokens, to: from }),
);
let mut tr = Transaction {
data: TransactionData { plan, tokens },
contract: Contract { plan, tokens },
from,
last_id,
sig: Signature::default(),
Expand All @@ -64,7 +64,7 @@ impl Transaction {
}

fn get_sign_data(&self) -> Vec<u8> {
let mut data = serialize(&(&self.data)).expect("serialize TransactionData");
let mut data = serialize(&(&self.contract)).expect("serialize Contract");
let last_id_data = serialize(&(&self.last_id)).expect("serialize last_id");
data.extend_from_slice(&last_id_data);
data
Expand All @@ -81,7 +81,7 @@ impl Transaction {
}

pub fn verify_plan(&self) -> bool {
self.data.plan.verify(self.data.tokens)
self.contract.plan.verify(self.contract.tokens)
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ mod tests {
to: Default::default(),
});
let claim0 = Transaction {
data: TransactionData { plan, tokens: 0 },
contract: Contract { plan, tokens: 0 },
from: Default::default(),
last_id: Default::default(),
sig: Default::default(),
Expand All @@ -166,9 +166,9 @@ mod tests {
let keypair = KeyPair::new();
let pubkey = keypair.pubkey();
let mut tr = Transaction::new(&keypair, pubkey, 42, zero);
tr.data.tokens = 1_000_000; // <-- attack, part 1!
if let Plan::Pay(ref mut payment) = tr.data.plan {
payment.tokens = tr.data.tokens; // <-- attack, part 2!
tr.contract.tokens = 1_000_000; // <-- attack, part 1!
if let Plan::Pay(ref mut payment) = tr.contract.plan {
payment.tokens = tr.contract.tokens; // <-- attack, part 2!
};
assert!(tr.verify_plan());
assert!(!tr.verify_sig());
Expand All @@ -182,7 +182,7 @@ mod tests {
let pubkey1 = keypair1.pubkey();
let zero = Hash::default();
let mut tr = Transaction::new(&keypair0, pubkey1, 42, zero);
if let Plan::Pay(ref mut payment) = tr.data.plan {
if let Plan::Pay(ref mut payment) = tr.contract.plan {
payment.to = thief_keypair.pubkey(); // <-- attack!
};
assert!(tr.verify_plan());
Expand All @@ -204,13 +204,13 @@ mod tests {
let keypair1 = KeyPair::new();
let zero = Hash::default();
let mut tr = Transaction::new(&keypair0, keypair1.pubkey(), 1, zero);
if let Plan::Pay(ref mut payment) = tr.data.plan {
if let Plan::Pay(ref mut payment) = tr.contract.plan {
payment.tokens = 2; // <-- attack!
}
assert!(!tr.verify_plan());

// Also, ensure all branchs of the plan spend all tokens
if let Plan::Pay(ref mut payment) = tr.data.plan {
if let Plan::Pay(ref mut payment) = tr.contract.plan {
payment.tokens = 0; // <-- whoops!
}
assert!(!tr.verify_plan());
Expand Down

0 comments on commit 8041dbc

Please sign in to comment.