2018-05-11 23:31:32 -07:00
|
|
|
//! 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-11 23:31:32 -07:00
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
use bank::Bank;
|
2018-06-13 21:52:23 -07:00
|
|
|
use blob_fetch_stage::BlobFetchStage;
|
|
|
|
use crdt::Crdt;
|
2018-05-11 23:31:32 -07:00
|
|
|
use packet;
|
2018-05-22 15:17:59 -07:00
|
|
|
use replicate_stage::ReplicateStage;
|
2018-05-11 23:31:32 -07:00
|
|
|
use std::net::UdpSocket;
|
2018-05-22 15:17:59 -07:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2018-05-11 23:31:32 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
2018-05-22 15:17:59 -07:00
|
|
|
use std::thread::JoinHandle;
|
2018-05-11 23:31:32 -07:00
|
|
|
use streamer;
|
2018-06-13 21:52:23 -07:00
|
|
|
use window_stage::WindowStage;
|
2018-05-11 23:31:32 -07:00
|
|
|
|
|
|
|
pub struct Tvu {
|
2018-05-23 07:11:11 -07:00
|
|
|
pub thread_hdls: Vec<JoinHandle<()>>,
|
2018-05-11 23:31:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2018-05-11 23:31:32 -07:00
|
|
|
/// # Arguments
|
2018-05-23 07:11:11 -07:00
|
|
|
/// * `bank` - The bank state.
|
2018-06-13 21:52:23 -07:00
|
|
|
/// * `crdt` - The crdt state.
|
|
|
|
/// * `window` - The window state.
|
|
|
|
/// * `replicate_socket` - my replicate socket
|
|
|
|
/// * `repair_socket` - my repair socket
|
|
|
|
/// * `retransmit_socket` - my retransmit socket
|
2018-05-11 23:31:32 -07:00
|
|
|
/// * `exit` - The exit signal.
|
2018-05-23 07:11:11 -07:00
|
|
|
pub fn new(
|
|
|
|
bank: Arc<Bank>,
|
2018-06-13 21:52:23 -07:00
|
|
|
crdt: Arc<RwLock<Crdt>>,
|
|
|
|
window: streamer::Window,
|
|
|
|
replicate_socket: UdpSocket,
|
2018-06-02 08:32:51 -07:00
|
|
|
repair_socket: UdpSocket,
|
2018-06-13 21:52:23 -07:00
|
|
|
retransmit_socket: UdpSocket,
|
2018-05-11 23:31:32 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2018-05-23 07:11:11 -07:00
|
|
|
) -> Self {
|
2018-05-11 23:31:32 -07:00
|
|
|
let blob_recycler = packet::BlobRecycler::default();
|
2018-06-13 21:52:23 -07:00
|
|
|
let fetch_stage = BlobFetchStage::new_multi_socket(
|
|
|
|
vec![replicate_socket, repair_socket],
|
2018-05-11 23:31:32 -07:00
|
|
|
exit.clone(),
|
|
|
|
blob_recycler.clone(),
|
|
|
|
);
|
|
|
|
//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
|
2018-06-13 21:52:23 -07:00
|
|
|
let window_stage = WindowStage::new(
|
|
|
|
crdt,
|
2018-05-12 19:00:22 -07:00
|
|
|
window,
|
2018-06-13 21:52:23 -07:00
|
|
|
retransmit_socket,
|
2018-05-22 15:17:59 -07:00
|
|
|
exit.clone(),
|
|
|
|
blob_recycler.clone(),
|
2018-06-13 21:52:23 -07:00
|
|
|
fetch_stage.blob_receiver,
|
2018-05-22 15:17:59 -07:00
|
|
|
);
|
2018-05-11 23:31:32 -07:00
|
|
|
|
2018-06-13 21:52:23 -07:00
|
|
|
let replicate_stage =
|
|
|
|
ReplicateStage::new(bank, exit, window_stage.blob_receiver, blob_recycler);
|
|
|
|
|
|
|
|
let mut threads = vec![replicate_stage.thread_hdl];
|
|
|
|
threads.extend(fetch_stage.thread_hdls.into_iter());
|
|
|
|
threads.extend(window_stage.thread_hdls.into_iter());
|
2018-05-23 10:54:48 -07:00
|
|
|
Tvu {
|
|
|
|
thread_hdls: threads,
|
|
|
|
}
|
2018-05-11 23:31:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2018-05-23 10:49:48 -07:00
|
|
|
pub mod tests {
|
2018-05-14 14:33:11 -07:00
|
|
|
use bank::Bank;
|
2018-05-11 23:31:32 -07:00
|
|
|
use bincode::serialize;
|
2018-05-27 18:21:39 -07:00
|
|
|
use crdt::{Crdt, TestNode};
|
2018-05-16 16:49:58 -07:00
|
|
|
use entry::Entry;
|
2018-05-11 23:31:32 -07:00
|
|
|
use hash::{hash, Hash};
|
|
|
|
use logger;
|
|
|
|
use mint::Mint;
|
2018-06-07 15:06:32 -07:00
|
|
|
use ncp::Ncp;
|
2018-05-11 23:31:32 -07:00
|
|
|
use packet::BlobRecycler;
|
2018-05-27 18:21:39 -07:00
|
|
|
use result::Result;
|
2018-05-11 23:31:32 -07:00
|
|
|
use signature::{KeyPair, KeyPairUtil};
|
|
|
|
use std::collections::VecDeque;
|
2018-05-27 18:21:39 -07:00
|
|
|
use std::net::UdpSocket;
|
2018-05-11 23:31:32 -07:00
|
|
|
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;
|
2018-05-27 18:21:39 -07:00
|
|
|
use tvu::Tvu;
|
2018-05-11 23:31:32 -07:00
|
|
|
|
2018-06-13 21:52:23 -07:00
|
|
|
fn new_ncp(
|
2018-05-27 18:21:39 -07:00
|
|
|
crdt: Arc<RwLock<Crdt>>,
|
|
|
|
listen: UdpSocket,
|
|
|
|
exit: Arc<AtomicBool>,
|
2018-06-13 21:52:23 -07:00
|
|
|
) -> Result<(Ncp, streamer::Window)> {
|
2018-05-27 18:21:39 -07:00
|
|
|
let window = streamer::default_window();
|
|
|
|
let send_sock = UdpSocket::bind("0.0.0.0:0").expect("bind 0");
|
2018-06-13 21:52:23 -07:00
|
|
|
let ncp = Ncp::new(crdt, window.clone(), listen, send_sock, exit)?;
|
|
|
|
Ok((ncp, window))
|
2018-05-27 18:21:39 -07:00
|
|
|
}
|
2018-05-25 23:24:29 -07:00
|
|
|
/// Test that message sent from leader to target1 and replicated to target2
|
2018-05-11 23:31:32 -07:00
|
|
|
#[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();
|
2018-05-11 23:31:32 -07:00
|
|
|
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);
|
2018-05-11 23:31:32 -07:00
|
|
|
|
|
|
|
let cref_l = Arc::new(RwLock::new(crdt_l));
|
2018-06-13 21:52:23 -07:00
|
|
|
let dr_l = new_ncp(cref_l, leader.sockets.gossip, exit.clone()).unwrap();
|
2018-05-11 23:31:32 -07:00
|
|
|
|
|
|
|
//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;
|
2018-05-11 23:31:32 -07:00
|
|
|
let cref2 = Arc::new(RwLock::new(crdt2));
|
2018-06-13 21:52:23 -07:00
|
|
|
let dr_2 = new_ncp(cref2, target2.sockets.gossip, exit.clone()).unwrap();
|
2018-05-11 23:31:32 -07:00
|
|
|
|
|
|
|
// 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,
|
2018-05-11 23:31:32 -07:00
|
|
|
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,
|
2018-05-11 23:31:32 -07:00
|
|
|
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));
|
2018-06-13 21:52:23 -07:00
|
|
|
|
|
|
|
//start crdt1
|
|
|
|
let mut crdt1 = Crdt::new(target1.data.clone());
|
|
|
|
crdt1.insert(&leader.data);
|
|
|
|
crdt1.set_leader(leader.data.id);
|
|
|
|
let cref1 = Arc::new(RwLock::new(crdt1));
|
|
|
|
let dr_1 = new_ncp(cref1.clone(), target1.sockets.gossip, exit.clone()).unwrap();
|
|
|
|
|
2018-05-23 08:29:24 -07:00
|
|
|
let tvu = Tvu::new(
|
2018-05-23 10:52:47 -07:00
|
|
|
bank.clone(),
|
2018-06-13 21:52:23 -07:00
|
|
|
cref1,
|
|
|
|
dr_1.1,
|
2018-05-23 11:06:18 -07:00
|
|
|
target1.sockets.replicate,
|
2018-06-02 08:32:51 -07:00
|
|
|
target1.sockets.repair,
|
2018-06-13 21:52:23 -07:00
|
|
|
target1.sockets.retransmit,
|
2018-05-11 23:31:32 -07:00
|
|
|
exit.clone(),
|
2018-05-23 08:29:24 -07:00
|
|
|
);
|
2018-05-11 23:31:32 -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();
|
|
|
|
|
2018-05-23 22:26:55 -07:00
|
|
|
let entry0 = Entry::new(&cur_hash, i, vec![]);
|
2018-05-14 14:33:11 -07:00
|
|
|
bank.register_entry_id(&cur_hash);
|
2018-05-11 23:31:32 -07:00
|
|
|
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(),
|
2018-05-11 23:31:32 -07:00
|
|
|
bob_keypair.pubkey(),
|
|
|
|
transfer_amount,
|
|
|
|
cur_hash,
|
|
|
|
);
|
2018-05-14 14:33:11 -07:00
|
|
|
bank.register_entry_id(&cur_hash);
|
2018-05-11 23:31:32 -07:00
|
|
|
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);
|
2018-05-11 23:31:32 -07:00
|
|
|
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();
|
2018-05-11 23:31:32 -07:00
|
|
|
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();
|
2018-05-11 23:31:32 -07:00
|
|
|
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 {
|
2018-05-11 23:31:32 -07:00
|
|
|
t.join().expect("join");
|
|
|
|
}
|
2018-06-13 21:52:23 -07:00
|
|
|
for t in dr_l.0.thread_hdls {
|
|
|
|
t.join().expect("join");
|
|
|
|
}
|
|
|
|
for t in dr_2.0.thread_hdls {
|
2018-05-27 18:21:39 -07:00
|
|
|
t.join().expect("join");
|
|
|
|
}
|
2018-06-13 21:52:23 -07:00
|
|
|
for t in dr_1.0.thread_hdls {
|
2018-05-27 18:21:39 -07:00
|
|
|
t.join().expect("join");
|
|
|
|
}
|
2018-05-11 23:31:32 -07:00
|
|
|
t_receiver.join().expect("join");
|
|
|
|
t_responder.join().expect("join");
|
|
|
|
}
|
|
|
|
}
|