Revert 1dd8c5ed362693fa46e4a281e1caaae427a88edc
Per @sakridge, this might cause a performance degradation. Need to benchmark it.
This commit is contained in:
parent
16337d7c1e
commit
f6fe998ed4
28
src/bank.rs
28
src/bank.rs
|
@ -114,7 +114,7 @@ impl Bank {
|
||||||
/// Create an Bank using a deposit.
|
/// Create an Bank using a deposit.
|
||||||
pub fn new_from_deposit(deposit: &Payment) -> Self {
|
pub fn new_from_deposit(deposit: &Payment) -> Self {
|
||||||
let bank = Self::default();
|
let bank = Self::default();
|
||||||
bank.apply_payment(deposit);
|
bank.apply_payment(deposit, &mut bank.balances.write().unwrap());
|
||||||
bank
|
bank
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,8 +130,7 @@ impl Bank {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Commit funds to the `payment.to` party.
|
/// Commit funds to the `payment.to` party.
|
||||||
fn apply_payment(&self, payment: &Payment) {
|
fn apply_payment(&self, payment: &Payment, balances: &mut HashMap<PublicKey, i64>) {
|
||||||
let mut balances = self.balances.write().unwrap();
|
|
||||||
if balances.contains_key(&payment.to) {
|
if balances.contains_key(&payment.to) {
|
||||||
*balances.get_mut(&payment.to).unwrap() += payment.tokens;
|
*balances.get_mut(&payment.to).unwrap() += payment.tokens;
|
||||||
} else {
|
} else {
|
||||||
|
@ -206,8 +205,7 @@ impl Bank {
|
||||||
|
|
||||||
/// Deduct tokens from the 'from' address the account has sufficient
|
/// Deduct tokens from the 'from' address the account has sufficient
|
||||||
/// funds and isn't a duplicate.
|
/// funds and isn't a duplicate.
|
||||||
fn apply_debits(&self, tx: &Transaction) -> Result<()> {
|
fn apply_debits(&self, tx: &Transaction, bals: &mut HashMap<PublicKey, i64>) -> Result<()> {
|
||||||
let mut bals = self.balances.write().unwrap();
|
|
||||||
let mut purge = false;
|
let mut purge = false;
|
||||||
{
|
{
|
||||||
let option = bals.get_mut(&tx.from);
|
let option = bals.get_mut(&tx.from);
|
||||||
|
@ -243,7 +241,7 @@ impl Bank {
|
||||||
|
|
||||||
/// Apply only a transaction's credits. Credits from multiple transactions
|
/// Apply only a transaction's credits. Credits from multiple transactions
|
||||||
/// may safely be applied in parallel.
|
/// may safely be applied in parallel.
|
||||||
fn apply_credits(&self, tx: &Transaction) {
|
fn apply_credits(&self, tx: &Transaction, balances: &mut HashMap<PublicKey, i64>) {
|
||||||
match &tx.instruction {
|
match &tx.instruction {
|
||||||
Instruction::NewContract(contract) => {
|
Instruction::NewContract(contract) => {
|
||||||
let mut plan = contract.plan.clone();
|
let mut plan = contract.plan.clone();
|
||||||
|
@ -252,7 +250,7 @@ impl Bank {
|
||||||
.expect("timestamp creation in apply_credits")));
|
.expect("timestamp creation in apply_credits")));
|
||||||
|
|
||||||
if let Some(payment) = plan.final_payment() {
|
if let Some(payment) = plan.final_payment() {
|
||||||
self.apply_payment(&payment);
|
self.apply_payment(&payment, balances);
|
||||||
} else {
|
} else {
|
||||||
let mut pending = self.pending
|
let mut pending = self.pending
|
||||||
.write()
|
.write()
|
||||||
|
@ -272,8 +270,9 @@ impl Bank {
|
||||||
/// Process a Transaction. If it contains a payment plan that requires a witness
|
/// Process a Transaction. If it contains a payment plan that requires a witness
|
||||||
/// to progress, the payment plan will be stored in the bank.
|
/// to progress, the payment plan will be stored in the bank.
|
||||||
fn process_transaction(&self, tx: &Transaction) -> Result<()> {
|
fn process_transaction(&self, tx: &Transaction) -> Result<()> {
|
||||||
self.apply_debits(tx)?;
|
let bals = &mut self.balances.write().unwrap();
|
||||||
self.apply_credits(tx);
|
self.apply_debits(tx, bals)?;
|
||||||
|
self.apply_credits(tx, bals);
|
||||||
self.transaction_count.fetch_add(1, Ordering::Relaxed);
|
self.transaction_count.fetch_add(1, Ordering::Relaxed);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -281,11 +280,12 @@ impl Bank {
|
||||||
/// Process a batch of transactions.
|
/// Process a batch of transactions.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn process_transactions(&self, txs: Vec<Transaction>) -> Vec<Result<Transaction>> {
|
pub fn process_transactions(&self, txs: Vec<Transaction>) -> Vec<Result<Transaction>> {
|
||||||
|
let bals = &mut self.balances.write().unwrap();
|
||||||
debug!("processing Transactions {}", txs.len());
|
debug!("processing Transactions {}", txs.len());
|
||||||
let txs_len = txs.len();
|
let txs_len = txs.len();
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let results: Vec<_> = txs.into_iter()
|
let results: Vec<_> = txs.into_iter()
|
||||||
.map(|tx| self.apply_debits(&tx).map(|_| tx))
|
.map(|tx| self.apply_debits(&tx, bals).map(|_| tx))
|
||||||
.collect(); // Calling collect() here forces all debits to complete before moving on.
|
.collect(); // Calling collect() here forces all debits to complete before moving on.
|
||||||
|
|
||||||
let debits = now.elapsed();
|
let debits = now.elapsed();
|
||||||
|
@ -295,7 +295,7 @@ impl Bank {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|result| {
|
.map(|result| {
|
||||||
result.map(|tx| {
|
result.map(|tx| {
|
||||||
self.apply_credits(&tx);
|
self.apply_credits(&tx, bals);
|
||||||
tx
|
tx
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -387,7 +387,7 @@ impl Bank {
|
||||||
None
|
None
|
||||||
}.expect("invalid ledger, needs to start with a contract");
|
}.expect("invalid ledger, needs to start with a contract");
|
||||||
|
|
||||||
self.apply_payment(&deposit);
|
self.apply_payment(&deposit, &mut self.balances.write().unwrap());
|
||||||
self.register_entry_id(&entry0.id);
|
self.register_entry_id(&entry0.id);
|
||||||
self.register_entry_id(&entry1.id);
|
self.register_entry_id(&entry1.id);
|
||||||
|
|
||||||
|
@ -406,7 +406,7 @@ impl Bank {
|
||||||
{
|
{
|
||||||
e.get_mut().apply_witness(&Witness::Signature(from));
|
e.get_mut().apply_witness(&Witness::Signature(from));
|
||||||
if let Some(payment) = e.get().final_payment() {
|
if let Some(payment) = e.get().final_payment() {
|
||||||
self.apply_payment(&payment);
|
self.apply_payment(&payment, &mut self.balances.write().unwrap());
|
||||||
e.remove_entry();
|
e.remove_entry();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -455,7 +455,7 @@ impl Bank {
|
||||||
.read()
|
.read()
|
||||||
.expect("'last_time' read lock when creating timestamp")));
|
.expect("'last_time' read lock when creating timestamp")));
|
||||||
if let Some(payment) = plan.final_payment() {
|
if let Some(payment) = plan.final_payment() {
|
||||||
self.apply_payment(&payment);
|
self.apply_payment(&payment, &mut self.balances.write().unwrap());
|
||||||
completed.push(key.clone());
|
completed.push(key.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue