2018-06-06 08:58:49 -07:00
|
|
|
//! The `banking_stage` processes Transaction messages. It is intended to be used
|
|
|
|
//! to contruct a software pipeline. The stage uses all available CPU cores and
|
|
|
|
//! can do its processing in parallel with signature verification on the GPU.
|
2018-05-14 16:31:13 -07:00
|
|
|
|
|
|
|
use bank::Bank;
|
|
|
|
use bincode::deserialize;
|
2018-05-30 21:24:21 -07:00
|
|
|
use counter::Counter;
|
2018-08-06 11:35:45 -07:00
|
|
|
use log::Level;
|
2018-09-18 08:02:57 -07:00
|
|
|
use packet::{Packets, SharedPackets};
|
2018-05-14 16:31:13 -07:00
|
|
|
use rayon::prelude::*;
|
2018-05-23 14:49:59 -07:00
|
|
|
use record_stage::Signal;
|
2018-07-05 14:41:53 -07:00
|
|
|
use result::{Error, Result};
|
2018-07-03 21:14:08 -07:00
|
|
|
use service::Service;
|
2018-05-14 16:31:13 -07:00
|
|
|
use std::net::SocketAddr;
|
2018-07-05 14:41:53 -07:00
|
|
|
use std::sync::atomic::AtomicUsize;
|
|
|
|
use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender};
|
2018-05-29 20:20:28 -07:00
|
|
|
use std::sync::Arc;
|
2018-07-03 21:14:08 -07:00
|
|
|
use std::thread::{self, Builder, JoinHandle};
|
2018-05-14 16:31:13 -07:00
|
|
|
use std::time::Duration;
|
|
|
|
use std::time::Instant;
|
|
|
|
use timing;
|
2018-05-23 23:29:01 -07:00
|
|
|
use transaction::Transaction;
|
2018-05-14 16:31:13 -07:00
|
|
|
|
2018-06-06 08:58:49 -07:00
|
|
|
/// Stores the stage's thread handle and output receiver.
|
2018-05-14 16:31:13 -07:00
|
|
|
pub struct BankingStage {
|
2018-06-06 08:58:49 -07:00
|
|
|
/// Handle to the stage's thread.
|
2018-07-03 21:14:08 -07:00
|
|
|
thread_hdl: JoinHandle<()>,
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BankingStage {
|
2018-07-05 14:41:53 -07:00
|
|
|
/// Create the stage using `bank`. Exit when `verified_receiver` is dropped.
|
2018-06-06 08:58:49 -07:00
|
|
|
/// Discard input packets using `packet_recycler` to minimize memory
|
|
|
|
/// allocations in a previous stage such as the `fetch_stage`.
|
2018-05-14 16:31:13 -07:00
|
|
|
pub fn new(
|
|
|
|
bank: Arc<Bank>,
|
|
|
|
verified_receiver: Receiver<Vec<(SharedPackets, Vec<u8>)>>,
|
2018-07-02 14:45:16 -07:00
|
|
|
) -> (Self, Receiver<Signal>) {
|
2018-05-14 16:31:13 -07:00
|
|
|
let (signal_sender, signal_receiver) = channel();
|
2018-05-30 13:38:15 -07:00
|
|
|
let thread_hdl = Builder::new()
|
|
|
|
.name("solana-banking-stage".to_string())
|
|
|
|
.spawn(move || loop {
|
2018-09-18 08:02:57 -07:00
|
|
|
if let Err(e) = Self::process_packets(&bank, &verified_receiver, &signal_sender) {
|
2018-07-05 14:41:53 -07:00
|
|
|
match e {
|
|
|
|
Error::RecvTimeoutError(RecvTimeoutError::Disconnected) => break,
|
|
|
|
Error::RecvTimeoutError(RecvTimeoutError::Timeout) => (),
|
2018-09-15 23:46:16 -07:00
|
|
|
Error::SendError => break,
|
|
|
|
_ => error!("{:?}", e),
|
2018-05-30 13:38:15 -07:00
|
|
|
}
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
2018-09-14 16:25:14 -07:00
|
|
|
}).unwrap();
|
2018-07-02 14:45:16 -07:00
|
|
|
(BankingStage { thread_hdl }, signal_receiver)
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:58:49 -07:00
|
|
|
/// Convert the transactions from a blob of binary data to a vector of transactions and
|
|
|
|
/// an unused `SocketAddr` that could be used to send a response.
|
2018-06-27 11:33:56 -07:00
|
|
|
fn deserialize_transactions(p: &Packets) -> Vec<Option<(Transaction, SocketAddr)>> {
|
2018-05-14 16:31:13 -07:00
|
|
|
p.packets
|
|
|
|
.par_iter()
|
|
|
|
.map(|x| {
|
|
|
|
deserialize(&x.data[0..x.meta.size])
|
|
|
|
.map(|req| (req, x.meta.addr()))
|
|
|
|
.ok()
|
2018-09-14 16:25:14 -07:00
|
|
|
}).collect()
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
|
|
|
|
2018-06-06 08:58:49 -07:00
|
|
|
/// Process the incoming packets and send output `Signal` messages to `signal_sender`.
|
|
|
|
/// Discard packets via `packet_recycler`.
|
2018-07-10 19:33:16 -07:00
|
|
|
pub fn process_packets(
|
2018-07-11 20:10:25 -07:00
|
|
|
bank: &Arc<Bank>,
|
2018-05-14 16:31:13 -07:00
|
|
|
verified_receiver: &Receiver<Vec<(SharedPackets, Vec<u8>)>>,
|
|
|
|
signal_sender: &Sender<Signal>,
|
|
|
|
) -> Result<()> {
|
2018-08-12 10:04:21 -07:00
|
|
|
let timer = Duration::new(1, 0);
|
2018-05-14 16:31:13 -07:00
|
|
|
let recv_start = Instant::now();
|
2018-08-12 10:04:21 -07:00
|
|
|
let mms = verified_receiver.recv_timeout(timer)?;
|
2018-05-14 16:31:13 -07:00
|
|
|
let mut reqs_len = 0;
|
|
|
|
let mms_len = mms.len();
|
|
|
|
info!(
|
|
|
|
"@{:?} process start stalled for: {:?}ms batches: {}",
|
|
|
|
timing::timestamp(),
|
|
|
|
timing::duration_as_ms(&recv_start.elapsed()),
|
|
|
|
mms.len(),
|
|
|
|
);
|
2018-07-19 15:38:35 -07:00
|
|
|
let bank_starting_tx_count = bank.transaction_count();
|
2018-05-30 21:24:21 -07:00
|
|
|
let count = mms.iter().map(|x| x.1.len()).sum();
|
2018-05-14 16:31:13 -07:00
|
|
|
let proc_start = Instant::now();
|
|
|
|
for (msgs, vers) in mms {
|
2018-09-18 08:02:57 -07:00
|
|
|
let transactions = Self::deserialize_transactions(&msgs.read());
|
2018-05-25 14:51:41 -07:00
|
|
|
reqs_len += transactions.len();
|
|
|
|
let transactions = transactions
|
2018-05-14 16:31:13 -07:00
|
|
|
.into_iter()
|
|
|
|
.zip(vers)
|
2018-05-25 14:51:41 -07:00
|
|
|
.filter_map(|(tx, ver)| match tx {
|
2018-05-14 16:31:13 -07:00
|
|
|
None => None,
|
2018-05-25 14:51:41 -07:00
|
|
|
Some((tx, _addr)) => if tx.verify_plan() && ver != 0 {
|
|
|
|
Some(tx)
|
2018-05-14 16:31:13 -07:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2018-09-14 16:25:14 -07:00
|
|
|
}).collect();
|
2018-05-14 16:31:13 -07:00
|
|
|
|
2018-05-25 14:51:41 -07:00
|
|
|
debug!("process_transactions");
|
2018-05-29 08:52:40 -07:00
|
|
|
let results = bank.process_transactions(transactions);
|
2018-08-12 10:04:21 -07:00
|
|
|
let transactions = results.into_iter().filter_map(|x| x.ok()).collect();
|
2018-09-15 23:46:16 -07:00
|
|
|
if let Err(_) = signal_sender.send(Signal::Transactions(transactions)) {
|
|
|
|
return Err(Error::SendError);
|
2018-09-14 14:34:32 -07:00
|
|
|
}
|
2018-05-25 14:51:41 -07:00
|
|
|
debug!("done process_transactions");
|
2018-05-14 16:31:13 -07:00
|
|
|
}
|
|
|
|
let total_time_s = timing::duration_as_s(&proc_start.elapsed());
|
|
|
|
let total_time_ms = timing::duration_as_ms(&proc_start.elapsed());
|
|
|
|
info!(
|
2018-05-25 14:51:41 -07:00
|
|
|
"@{:?} done processing transaction batches: {} time: {:?}ms reqs: {} reqs/s: {}",
|
2018-05-14 16:31:13 -07:00
|
|
|
timing::timestamp(),
|
|
|
|
mms_len,
|
|
|
|
total_time_ms,
|
|
|
|
reqs_len,
|
|
|
|
(reqs_len as f32) / (total_time_s)
|
|
|
|
);
|
2018-08-06 11:35:45 -07:00
|
|
|
inc_new_counter_info!("banking_stage-process_packets", count);
|
|
|
|
inc_new_counter_info!(
|
2018-07-19 15:38:35 -07:00
|
|
|
"banking_stage-process_transactions",
|
|
|
|
bank.transaction_count() - bank_starting_tx_count
|
|
|
|
);
|
2018-05-14 16:31:13 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 21:14:08 -07:00
|
|
|
impl Service for BankingStage {
|
2018-09-13 14:00:17 -07:00
|
|
|
type JoinReturnType = ();
|
2018-07-03 21:14:08 -07:00
|
|
|
|
|
|
|
fn join(self) -> thread::Result<()> {
|
|
|
|
self.thread_hdl.join()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 16:31:13 -07:00
|
|
|
// TODO: When banking is pulled out of RequestStage, add this test back in.
|
|
|
|
|
|
|
|
//use bank::Bank;
|
|
|
|
//use entry::Entry;
|
|
|
|
//use hash::Hash;
|
|
|
|
//use record_stage::RecordStage;
|
2018-05-23 14:49:59 -07:00
|
|
|
//use record_stage::Signal;
|
2018-05-14 16:31:13 -07:00
|
|
|
//use result::Result;
|
|
|
|
//use std::sync::mpsc::{channel, Sender};
|
|
|
|
//use std::sync::{Arc, Mutex};
|
|
|
|
//use std::time::Duration;
|
2018-05-23 23:29:01 -07:00
|
|
|
//use transaction::Transaction;
|
2018-05-14 16:31:13 -07:00
|
|
|
//
|
|
|
|
//#[cfg(test)]
|
|
|
|
//mod tests {
|
|
|
|
// use bank::Bank;
|
|
|
|
// use mint::Mint;
|
2018-08-12 10:04:21 -07:00
|
|
|
// use signature::{KeyPair, KeyPairUtil};
|
2018-05-14 16:31:13 -07:00
|
|
|
// use transaction::Transaction;
|
|
|
|
//
|
|
|
|
// #[test]
|
2018-05-25 14:51:41 -07:00
|
|
|
// // TODO: Move this test banking_stage. Calling process_transactions() directly
|
2018-05-14 16:31:13 -07:00
|
|
|
// // defeats the purpose of this test.
|
|
|
|
// fn test_banking_sequential_consistency() {
|
|
|
|
// // In this attack we'll demonstrate that a verifier can interpret the ledger
|
|
|
|
// // differently if either the server doesn't signal the ledger to add an
|
|
|
|
// // Entry OR if the verifier tries to parallelize across multiple Entries.
|
|
|
|
// let mint = Mint::new(2);
|
|
|
|
// let bank = Bank::new(&mint);
|
2018-05-25 14:51:41 -07:00
|
|
|
// let banking_stage = EventProcessor::new(bank, &mint.last_id(), None);
|
2018-05-14 16:31:13 -07:00
|
|
|
//
|
|
|
|
// // Process a batch that includes a transaction that receives two tokens.
|
2018-08-12 10:04:21 -07:00
|
|
|
// let alice = KeyPair::new();
|
2018-05-25 15:05:37 -07:00
|
|
|
// let tx = Transaction::new(&mint.keypair(), alice.pubkey(), 2, mint.last_id());
|
|
|
|
// let transactions = vec![tx];
|
2018-05-25 14:51:41 -07:00
|
|
|
// let entry0 = banking_stage.process_transactions(transactions).unwrap();
|
2018-05-14 16:31:13 -07:00
|
|
|
//
|
|
|
|
// // Process a second batch that spends one of those tokens.
|
2018-05-25 15:05:37 -07:00
|
|
|
// let tx = Transaction::new(&alice, mint.pubkey(), 1, mint.last_id());
|
|
|
|
// let transactions = vec![tx];
|
2018-05-25 14:51:41 -07:00
|
|
|
// let entry1 = banking_stage.process_transactions(transactions).unwrap();
|
2018-05-14 16:31:13 -07:00
|
|
|
//
|
|
|
|
// // Collect the ledger and feed it to a new bank.
|
|
|
|
// let entries = vec![entry0, entry1];
|
|
|
|
//
|
|
|
|
// // Assert the user holds one token, not two. If the server only output one
|
|
|
|
// // entry, then the second transaction will be rejected, because it drives
|
|
|
|
// // the account balance below zero before the credit is added.
|
|
|
|
// let bank = Bank::new(&mint);
|
|
|
|
// for entry in entries {
|
|
|
|
// assert!(
|
|
|
|
// bank
|
2018-05-29 08:52:40 -07:00
|
|
|
// .process_transactions(entry.transactions)
|
2018-05-14 16:31:13 -07:00
|
|
|
// .into_iter()
|
|
|
|
// .all(|x| x.is_ok())
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// assert_eq!(bank.get_balance(&alice.pubkey()), Some(1));
|
|
|
|
// }
|
|
|
|
//}
|