solana/src/tvu.rs

305 lines
10 KiB
Rust
Raw Normal View History

//! The `tvu` module implements the Transaction Validation Unit, a
//! 5-stage transaction validation pipeline in software.
2018-05-23 07:11:11 -07:00
//! 1. streamer
//! - Incoming blobs are picked up from the replicate socket.
//! 2. verifier
//! - TODO Blobs are sent to the GPU, and while the memory is there the PoH stream is verified
2018-05-23 08:29:24 -07:00
//! along with the ecdsa signature for the blob and each signature in all the transactions. Blobs
//! with errors are dropped, or marked for slashing.
2018-05-23 07:11:11 -07:00
//! 3.a retransmit
2018-05-25 23:24:29 -07:00
//! - Blobs originating from the parent (leader, at the moment, is the only parent), are retransmit to all the
2018-05-23 07:11:11 -07:00
//! peers in the crdt. Peers is everyone who is not me or the leader that has a known replicate
//! address.
//! 3.b window
2018-05-23 11:06:18 -07:00
//! - Verified blobs are placed into a window, indexed by the counter set by the leader.sockets. This could
2018-05-25 23:24:29 -07:00
//! be the PoH counter if its monotonically increasing in each blob. Erasure coding is used to
2018-05-23 07:11:11 -07:00
//! recover any missing packets, and requests are made at random to peers and parents to retransmit
//! a missing packet.
//! 4. accountant
//! - Contigous blobs are sent to the accountant for processing transactions
//! 5. validator
//! - TODO Validation messages are sent back to the leader
2018-05-14 14:33:11 -07:00
use bank::Bank;
use crdt::{Crdt, ReplicatedData};
2018-06-07 15:06:32 -07:00
use ncp::Ncp;
use packet;
2018-05-22 15:17:59 -07:00
use replicate_stage::ReplicateStage;
use std::net::UdpSocket;
2018-05-22 15:17:59 -07:00
use std::sync::atomic::AtomicBool;
use std::sync::mpsc::channel;
use std::sync::{Arc, RwLock};
2018-05-22 15:17:59 -07:00
use std::thread::JoinHandle;
use streamer;
pub struct Tvu {
2018-05-23 07:11:11 -07:00
pub thread_hdls: Vec<JoinHandle<()>>,
}
impl Tvu {
/// This service receives messages from a leader in the network and processes the transactions
2018-05-14 14:33:11 -07:00
/// on the bank state.
/// # Arguments
2018-05-23 07:11:11 -07:00
/// * `bank` - The bank state.
/// * `me` - my configuration
2018-05-25 23:24:29 -07:00
/// * `gossip` - my gossisp socket
/// * `replicate` - my replicate socket
/// * `leader` - leader configuration
/// * `exit` - The exit signal.
2018-05-23 07:11:11 -07:00
pub fn new(
bank: Arc<Bank>,
me: ReplicatedData,
gossip_listen_socket: UdpSocket,
replicate: UdpSocket,
repair_socket: UdpSocket,
leader: ReplicatedData,
exit: Arc<AtomicBool>,
2018-05-23 07:11:11 -07:00
) -> Self {
//replicate pipeline
let crdt = Arc::new(RwLock::new(Crdt::new(me)));
crdt.write()
.expect("'crdt' write lock in pub fn replicate")
.set_leader(leader.id);
crdt.write()
.expect("'crdt' write lock before insert() in pub fn replicate")
2018-05-12 19:00:22 -07:00
.insert(&leader);
let window = streamer::default_window();
let gossip_send_socket = UdpSocket::bind("0.0.0.0:0").expect("bind 0");
2018-06-07 15:06:32 -07:00
let ncp = Ncp::new(
crdt.clone(),
window.clone(),
gossip_listen_socket,
gossip_send_socket,
exit.clone(),
2018-06-07 15:06:32 -07:00
).expect("Ncp::new");
2018-05-23 08:29:24 -07:00
// TODO pull this socket out through the public interface
// make sure we are on the same interface
2018-05-23 08:29:24 -07:00
let mut local = replicate.local_addr().expect("tvu: get local address");
local.set_port(0);
2018-05-23 08:29:24 -07:00
let write = UdpSocket::bind(local).expect("tvu: bind to local socket");
let blob_recycler = packet::BlobRecycler::default();
let (blob_sender, blob_receiver) = channel();
let t_blob_receiver = streamer::blob_receiver(
exit.clone(),
blob_recycler.clone(),
replicate,
blob_sender.clone(),
2018-05-23 08:29:24 -07:00
).expect("tvu: blob receiver creation");
let (window_sender, window_receiver) = channel();
let (retransmit_sender, retransmit_receiver) = channel();
let t_retransmit = streamer::retransmitter(
write,
exit.clone(),
crdt.clone(),
blob_recycler.clone(),
retransmit_receiver,
);
let t_repair_receiver = streamer::blob_receiver(
exit.clone(),
blob_recycler.clone(),
repair_socket,
blob_sender.clone(),
).expect("tvu: blob repair receiver fail");
//TODO
//the packets coming out of blob_receiver need to be sent to the GPU and verified
//then sent to the window, which does the erasure coding reconstruction
let t_window = streamer::window(
exit.clone(),
crdt.clone(),
2018-05-12 19:00:22 -07:00
window,
blob_recycler.clone(),
blob_receiver,
window_sender,
retransmit_sender,
);
2018-05-22 15:17:59 -07:00
let replicate_stage = ReplicateStage::new(
2018-05-23 08:29:24 -07:00
bank.clone(),
2018-05-22 15:17:59 -07:00
exit.clone(),
window_receiver,
blob_recycler.clone(),
);
let mut threads = vec![
//replicate threads
t_blob_receiver,
t_retransmit,
t_window,
t_repair_receiver,
2018-05-22 15:17:59 -07:00
replicate_stage.thread_hdl,
];
2018-06-07 15:06:32 -07:00
threads.extend(ncp.thread_hdls.into_iter());
2018-05-23 10:54:48 -07:00
Tvu {
thread_hdls: threads,
}
}
}
#[cfg(test)]
2018-05-23 10:49:48 -07:00
pub mod tests {
2018-05-14 14:33:11 -07:00
use bank::Bank;
use bincode::serialize;
use crdt::{Crdt, TestNode};
2018-05-16 16:49:58 -07:00
use entry::Entry;
use hash::{hash, Hash};
use logger;
use mint::Mint;
2018-06-07 15:06:32 -07:00
use ncp::Ncp;
use packet::BlobRecycler;
use result::Result;
use signature::{KeyPair, KeyPairUtil};
use std::collections::VecDeque;
use std::net::UdpSocket;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::channel;
use std::sync::{Arc, RwLock};
use std::time::Duration;
use streamer;
2018-05-23 23:29:01 -07:00
use transaction::Transaction;
use tvu::Tvu;
fn new_replicator(
crdt: Arc<RwLock<Crdt>>,
listen: UdpSocket,
exit: Arc<AtomicBool>,
2018-06-07 15:06:32 -07:00
) -> Result<Ncp> {
let window = streamer::default_window();
let send_sock = UdpSocket::bind("0.0.0.0:0").expect("bind 0");
2018-06-07 15:06:32 -07:00
Ncp::new(crdt, window, listen, send_sock, exit)
}
2018-05-25 23:24:29 -07:00
/// Test that message sent from leader to target1 and replicated to target2
#[test]
fn test_replicate() {
logger::setup();
2018-05-23 10:49:48 -07:00
let leader = TestNode::new();
let target1 = TestNode::new();
let target2 = TestNode::new();
let exit = Arc::new(AtomicBool::new(false));
//start crdt_leader
2018-05-23 10:49:48 -07:00
let mut crdt_l = Crdt::new(leader.data.clone());
crdt_l.set_leader(leader.data.id);
let cref_l = Arc::new(RwLock::new(crdt_l));
let dr_l = new_replicator(cref_l, leader.sockets.gossip, exit.clone()).unwrap();
//start crdt2
2018-05-23 10:49:48 -07:00
let mut crdt2 = Crdt::new(target2.data.clone());
crdt2.insert(&leader.data);
crdt2.set_leader(leader.data.id);
let leader_id = leader.data.id;
let cref2 = Arc::new(RwLock::new(crdt2));
let dr_2 = new_replicator(cref2, target2.sockets.gossip, exit.clone()).unwrap();
// setup some blob services to send blobs into the socket
// to simulate the source peer and get blobs out of the socket to
// simulate target peer
let recv_recycler = BlobRecycler::default();
let resp_recycler = BlobRecycler::default();
let (s_reader, r_reader) = channel();
let t_receiver = streamer::blob_receiver(
exit.clone(),
recv_recycler.clone(),
2018-05-23 11:06:18 -07:00
target2.sockets.replicate,
s_reader,
).unwrap();
// simulate leader sending messages
let (s_responder, r_responder) = channel();
let t_responder = streamer::responder(
2018-05-23 11:06:18 -07:00
leader.sockets.requests,
exit.clone(),
resp_recycler.clone(),
r_responder,
);
let starting_balance = 10_000;
2018-05-14 14:39:34 -07:00
let mint = Mint::new(starting_balance);
2018-05-23 10:49:48 -07:00
let replicate_addr = target1.data.replicate_addr;
2018-05-23 08:29:24 -07:00
let bank = Arc::new(Bank::new(&mint));
let tvu = Tvu::new(
2018-05-23 10:52:47 -07:00
bank.clone(),
2018-05-23 10:49:48 -07:00
target1.data,
2018-05-23 11:06:18 -07:00
target1.sockets.gossip,
target1.sockets.replicate,
target1.sockets.repair,
2018-05-23 10:49:48 -07:00
leader.data,
exit.clone(),
2018-05-23 08:29:24 -07:00
);
let mut alice_ref_balance = starting_balance;
let mut msgs = VecDeque::new();
let mut cur_hash = Hash::default();
let num_blobs = 10;
let transfer_amount = 501;
let bob_keypair = KeyPair::new();
for i in 0..num_blobs {
let b = resp_recycler.allocate();
let b_ = b.clone();
let mut w = b.write().unwrap();
w.set_index(i).unwrap();
w.set_id(leader_id).unwrap();
let entry0 = Entry::new(&cur_hash, i, vec![]);
2018-05-14 14:33:11 -07:00
bank.register_entry_id(&cur_hash);
cur_hash = hash(&cur_hash);
2018-05-29 09:12:27 -07:00
let tx0 = Transaction::new(
2018-05-14 14:39:34 -07:00
&mint.keypair(),
bob_keypair.pubkey(),
transfer_amount,
cur_hash,
);
2018-05-14 14:33:11 -07:00
bank.register_entry_id(&cur_hash);
cur_hash = hash(&cur_hash);
2018-05-29 09:12:27 -07:00
let entry1 = Entry::new(&cur_hash, i + num_blobs, vec![tx0]);
2018-05-14 14:33:11 -07:00
bank.register_entry_id(&cur_hash);
cur_hash = hash(&cur_hash);
alice_ref_balance -= transfer_amount;
let serialized_entry = serialize(&vec![entry0, entry1]).unwrap();
w.data_mut()[..serialized_entry.len()].copy_from_slice(&serialized_entry);
w.set_size(serialized_entry.len());
w.meta.set_addr(&replicate_addr);
drop(w);
msgs.push_back(b_);
}
// send the blobs into the socket
s_responder.send(msgs).expect("send");
// receive retransmitted messages
let timer = Duration::new(1, 0);
let mut msgs: Vec<_> = Vec::new();
while let Ok(msg) = r_reader.recv_timeout(timer) {
trace!("msg: {:?}", msg);
msgs.push(msg);
}
2018-05-14 14:39:34 -07:00
let alice_balance = bank.get_balance(&mint.keypair().pubkey()).unwrap();
assert_eq!(alice_balance, alice_ref_balance);
2018-05-14 14:33:11 -07:00
let bob_balance = bank.get_balance(&bob_keypair.pubkey()).unwrap();
assert_eq!(bob_balance, starting_balance - alice_ref_balance);
exit.store(true, Ordering::Relaxed);
2018-05-23 08:29:24 -07:00
for t in tvu.thread_hdls {
t.join().expect("join");
}
for t in dr_l.thread_hdls {
t.join().expect("join");
}
for t in dr_2.thread_hdls {
t.join().expect("join");
}
t_receiver.join().expect("join");
t_responder.join().expect("join");
}
}