Skip to content

Commit

Permalink
Hoist last_id
Browse files Browse the repository at this point in the history
First step in unifying Witness processing and Transaction processing
  • Loading branch information
garious committed May 22, 2018
1 parent abfd7d6 commit 207b668
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl Bank {
return Err(BankError::AccountNotFound);
}

if !self.reserve_signature_with_last_id(&tr.sig, &tr.data.last_id) {
if !self.reserve_signature_with_last_id(&tr.sig, &tr.last_id) {
return Err(BankError::InvalidTransferSignature);
}

Expand All @@ -179,7 +179,7 @@ impl Bank {
let current = bal.load(Ordering::Relaxed) as i64;

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

Expand Down
32 changes: 13 additions & 19 deletions src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub const PUB_KEY_OFFSET: usize = 80;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct TransactionData {
pub tokens: i64,
pub last_id: Hash,
pub plan: Plan,
}

Expand All @@ -23,6 +22,7 @@ pub struct Transaction {
pub sig: Signature,
pub from: PublicKey,
pub data: TransactionData,
pub last_id: Hash,
}

impl Transaction {
Expand All @@ -32,12 +32,9 @@ impl Transaction {
let plan = Plan::Pay(Payment { tokens, to });
let mut tr = Transaction {
sig: Signature::default(),
data: TransactionData {
plan,
tokens,
last_id,
},
from: from,
data: TransactionData { plan, tokens },
last_id,
from,
};
tr.sign(from_keypair);
tr
Expand All @@ -57,20 +54,20 @@ impl Transaction {
(Condition::Signature(from), Payment { tokens, to: from }),
);
let mut tr = Transaction {
data: TransactionData {
plan,
tokens,
last_id,
},
from: from,
data: TransactionData { plan, tokens },
from,
last_id,
sig: Signature::default(),
};
tr.sign(from_keypair);
tr
}

fn get_sign_data(&self) -> Vec<u8> {
serialize(&(&self.data)).expect("serialize TransactionData in fn get_sign_data")
let mut data = serialize(&(&self.data)).expect("serialize TransactionData");
let last_id_data = serialize(&(&self.last_id)).expect("serialize last_id");
data.extend_from_slice(&last_id_data);
data
}

/// Sign this transaction.
Expand Down Expand Up @@ -153,12 +150,9 @@ mod tests {
to: Default::default(),
});
let claim0 = Transaction {
data: TransactionData {
plan,
tokens: 0,
last_id: Default::default(),
},
data: TransactionData { plan, tokens: 0 },
from: Default::default(),
last_id: Default::default(),
sig: Default::default(),
};
let buf = serialize(&claim0).unwrap();
Expand Down

0 comments on commit 207b668

Please sign in to comment.