2018-07-02 15:24:40 -07:00
|
|
|
//! The `fullnode` module hosts all the fullnode microservices.
|
|
|
|
|
2018-07-02 11:20:35 -07:00
|
|
|
use bank::Bank;
|
2018-08-09 15:17:50 -06:00
|
|
|
use broadcast_stage::BroadcastStage;
|
2018-07-11 00:18:48 -07:00
|
|
|
use crdt::{Crdt, NodeInfo, TestNode};
|
2018-08-22 11:54:37 -06:00
|
|
|
use drone::DRONE_PORT;
|
2018-07-02 10:07:32 -07:00
|
|
|
use entry::Entry;
|
2018-08-09 13:40:47 -06:00
|
|
|
use ledger::read_ledger;
|
2018-07-02 15:24:40 -07:00
|
|
|
use ncp::Ncp;
|
|
|
|
use packet::BlobRecycler;
|
2018-08-14 18:03:48 -06:00
|
|
|
use rpc::{JsonRpcService, RPC_PORT};
|
2018-07-02 15:24:40 -07:00
|
|
|
use rpu::Rpu;
|
2018-07-03 22:14:08 -06:00
|
|
|
use service::Service;
|
2018-08-09 08:56:04 -06:00
|
|
|
use signature::{Keypair, KeypairUtil};
|
2018-08-20 12:12:54 -07:00
|
|
|
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
2018-07-09 14:53:18 -06:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2018-07-02 15:24:40 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
2018-07-03 22:14:08 -06:00
|
|
|
use std::thread::{JoinHandle, Result};
|
2018-07-02 15:24:40 -07:00
|
|
|
use tpu::Tpu;
|
|
|
|
use tvu::Tvu;
|
2018-07-05 12:01:40 -07:00
|
|
|
use untrusted::Input;
|
2018-08-09 13:40:47 -06:00
|
|
|
use window;
|
2018-07-02 11:20:35 -07:00
|
|
|
|
2018-08-09 15:29:07 -06:00
|
|
|
pub struct Fullnode {
|
2018-07-09 14:53:18 -06:00
|
|
|
exit: Arc<AtomicBool>,
|
2018-07-03 22:14:08 -06:00
|
|
|
thread_hdls: Vec<JoinHandle<()>>,
|
2018-07-02 15:24:40 -07:00
|
|
|
}
|
2018-07-02 11:20:35 -07:00
|
|
|
|
2018-07-05 12:01:40 -07:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
|
|
|
/// Fullnode configuration to be stored in file
|
|
|
|
pub struct Config {
|
2018-07-11 00:18:48 -07:00
|
|
|
pub node_info: NodeInfo,
|
2018-07-05 12:01:40 -07:00
|
|
|
pkcs8: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Structure to be replicated by the network
|
|
|
|
impl Config {
|
2018-07-12 17:26:56 -06:00
|
|
|
pub fn new(bind_addr: &SocketAddr, pkcs8: Vec<u8>) -> Self {
|
2018-07-05 12:01:40 -07:00
|
|
|
let keypair =
|
2018-08-09 08:56:04 -06:00
|
|
|
Keypair::from_pkcs8(Input::from(&pkcs8)).expect("from_pkcs8 in fullnode::Config new");
|
2018-07-05 12:01:40 -07:00
|
|
|
let pubkey = keypair.pubkey();
|
2018-07-11 00:18:48 -07:00
|
|
|
let node_info = NodeInfo::new_leader_with_pubkey(pubkey, bind_addr);
|
2018-07-10 12:02:51 -07:00
|
|
|
Config { node_info, pkcs8 }
|
2018-07-05 12:01:40 -07:00
|
|
|
}
|
2018-08-09 08:56:04 -06:00
|
|
|
pub fn keypair(&self) -> Keypair {
|
|
|
|
Keypair::from_pkcs8(Input::from(&self.pkcs8))
|
2018-07-05 12:01:40 -07:00
|
|
|
.expect("from_pkcs8 in fullnode::Config keypair")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 15:29:07 -06:00
|
|
|
impl Fullnode {
|
2018-08-22 17:39:34 -06:00
|
|
|
pub fn new(
|
2018-08-22 16:45:49 -06:00
|
|
|
node: TestNode,
|
2018-08-03 11:06:06 -07:00
|
|
|
ledger_path: &str,
|
2018-08-09 08:56:04 -06:00
|
|
|
keypair: Keypair,
|
2018-08-22 16:51:19 -06:00
|
|
|
leader_addr: Option<SocketAddr>,
|
2018-07-31 16:54:24 -07:00
|
|
|
sigverify_disabled: bool,
|
2018-08-09 15:29:07 -06:00
|
|
|
) -> Self {
|
2018-07-02 15:24:40 -07:00
|
|
|
info!("creating bank...");
|
2018-08-22 16:51:19 -06:00
|
|
|
let bank = Bank::new_default(leader_addr.is_some());
|
2018-08-03 11:06:06 -07:00
|
|
|
|
2018-08-10 17:56:08 -07:00
|
|
|
let entries = read_ledger(ledger_path, true).expect("opening ledger");
|
2018-08-03 11:06:06 -07:00
|
|
|
|
|
|
|
let entries = entries.map(|e| e.expect("failed to parse entry"));
|
2018-07-02 11:20:35 -07:00
|
|
|
|
2018-07-02 10:07:32 -07:00
|
|
|
info!("processing ledger...");
|
|
|
|
let (entry_height, ledger_tail) = bank.process_ledger(entries).expect("process_ledger");
|
2018-07-02 15:24:40 -07:00
|
|
|
// entry_height is the network-wide agreed height of the ledger.
|
|
|
|
// initialize it from the input ledger
|
|
|
|
info!("processed {} ledger...", entry_height);
|
2018-07-02 11:20:35 -07:00
|
|
|
|
2018-07-02 15:24:40 -07:00
|
|
|
info!("creating networking stack...");
|
2018-07-02 11:20:35 -07:00
|
|
|
|
2018-07-02 15:24:40 -07:00
|
|
|
let local_gossip_addr = node.sockets.gossip.local_addr().unwrap();
|
2018-07-02 11:20:35 -07:00
|
|
|
info!(
|
2018-07-02 15:24:40 -07:00
|
|
|
"starting... local gossip address: {} (advertising {})",
|
2018-07-09 17:55:11 -07:00
|
|
|
local_gossip_addr, node.data.contact_info.ncp
|
2018-07-02 11:20:35 -07:00
|
|
|
);
|
2018-07-09 14:53:18 -06:00
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2018-08-22 17:37:57 -06:00
|
|
|
let local_requests_addr = node.sockets.requests.local_addr().unwrap();
|
|
|
|
let requests_addr = node.data.contact_info.rpu;
|
2018-08-22 20:28:05 -06:00
|
|
|
let leader_info = leader_addr.map(NodeInfo::new_entry_point);
|
2018-08-22 17:37:57 -06:00
|
|
|
let server = Self::new_with_bank(
|
2018-08-22 16:45:49 -06:00
|
|
|
keypair,
|
|
|
|
bank,
|
|
|
|
entry_height,
|
|
|
|
&ledger_tail,
|
|
|
|
node,
|
2018-08-22 20:28:05 -06:00
|
|
|
leader_info.as_ref(),
|
2018-08-22 16:45:49 -06:00
|
|
|
exit,
|
2018-08-22 17:37:57 -06:00
|
|
|
Some(ledger_path),
|
2018-08-22 16:45:49 -06:00
|
|
|
sigverify_disabled,
|
2018-08-22 17:37:57 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
match leader_addr {
|
|
|
|
Some(leader_addr) => {
|
|
|
|
info!(
|
|
|
|
"validator ready... local request address: {} (advertising {}) connected to: {}",
|
|
|
|
local_requests_addr, requests_addr, leader_addr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
info!(
|
|
|
|
"leader ready... local request address: {} (advertising {})",
|
|
|
|
local_requests_addr, requests_addr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
server
|
2018-08-22 16:45:49 -06:00
|
|
|
}
|
|
|
|
|
2018-08-22 18:50:19 -06:00
|
|
|
/// Create a fullnode instance acting as a leader or validator.
|
2018-07-02 15:24:40 -07:00
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// .---------------------.
|
|
|
|
/// | Leader |
|
|
|
|
/// | |
|
|
|
|
/// .--------. | .-----. |
|
|
|
|
/// | |---->| | |
|
|
|
|
/// | Client | | | RPU | |
|
|
|
|
/// | |<----| | |
|
|
|
|
/// `----+---` | `-----` |
|
|
|
|
/// | | ^ |
|
|
|
|
/// | | | |
|
|
|
|
/// | | .--+---. |
|
|
|
|
/// | | | Bank | |
|
|
|
|
/// | | `------` |
|
|
|
|
/// | | ^ |
|
|
|
|
/// | | | | .------------.
|
|
|
|
/// | | .--+--. .-----. | | |
|
|
|
|
/// `-------->| TPU +-->| NCP +------>| Validators |
|
|
|
|
/// | `-----` `-----` | | |
|
|
|
|
/// | | `------------`
|
|
|
|
/// `---------------------`
|
|
|
|
///
|
|
|
|
/// .-------------------------------.
|
|
|
|
/// | Validator |
|
|
|
|
/// | |
|
|
|
|
/// .--------. | .-----. |
|
|
|
|
/// | |-------------->| | |
|
|
|
|
/// | Client | | | RPU | |
|
|
|
|
/// | |<--------------| | |
|
|
|
|
/// `--------` | `-----` |
|
|
|
|
/// | ^ |
|
|
|
|
/// | | |
|
|
|
|
/// | .--+---. |
|
|
|
|
/// | | Bank | |
|
|
|
|
/// | `------` |
|
|
|
|
/// | ^ |
|
|
|
|
/// .--------. | | | .------------.
|
|
|
|
/// | | | .--+--. | | |
|
|
|
|
/// | Leader |<------------->| TVU +<--------------->| |
|
|
|
|
/// | | | `-----` | | Validators |
|
|
|
|
/// | | | ^ | | |
|
|
|
|
/// | | | | | | |
|
|
|
|
/// | | | .--+--. | | |
|
|
|
|
/// | |<------------->| NCP +<--------------->| |
|
|
|
|
/// | | | `-----` | | |
|
|
|
|
/// `--------` | | `------------`
|
|
|
|
/// `-------------------------------`
|
|
|
|
/// ```
|
2018-08-22 18:50:19 -06:00
|
|
|
pub fn new_with_bank(
|
2018-08-09 08:56:04 -06:00
|
|
|
keypair: Keypair,
|
2018-08-22 18:50:19 -06:00
|
|
|
bank: Bank,
|
2018-07-02 15:24:40 -07:00
|
|
|
entry_height: u64,
|
2018-08-09 12:35:52 -06:00
|
|
|
ledger_tail: &[Entry],
|
2018-08-22 18:50:19 -06:00
|
|
|
mut node: TestNode,
|
2018-08-22 20:28:05 -06:00
|
|
|
leader_info: Option<&NodeInfo>,
|
2018-07-02 15:24:40 -07:00
|
|
|
exit: Arc<AtomicBool>,
|
2018-08-05 22:04:27 -07:00
|
|
|
ledger_path: Option<&str>,
|
2018-08-22 18:50:19 -06:00
|
|
|
sigverify_disabled: bool,
|
|
|
|
) -> Self {
|
|
|
|
if leader_info.is_none() {
|
|
|
|
node.data.leader_id = node.data.id;
|
|
|
|
}
|
2018-07-02 15:24:40 -07:00
|
|
|
|
2018-08-22 18:50:19 -06:00
|
|
|
let bank = Arc::new(bank);
|
2018-08-22 18:51:53 -06:00
|
|
|
let mut thread_hdls = vec![];
|
2018-08-14 18:03:48 -06:00
|
|
|
|
2018-08-22 19:00:56 -06:00
|
|
|
let rpu = Rpu::new(
|
|
|
|
&bank,
|
|
|
|
node.sockets.requests,
|
|
|
|
node.sockets.respond,
|
|
|
|
exit.clone(),
|
|
|
|
);
|
|
|
|
thread_hdls.extend(rpu.thread_hdls());
|
2018-07-23 18:55:58 -07:00
|
|
|
|
2018-08-22 19:00:56 -06:00
|
|
|
let rpc_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), RPC_PORT);
|
|
|
|
let rpc_service = JsonRpcService::new(bank.clone(), rpc_addr, exit.clone());
|
|
|
|
thread_hdls.extend(rpc_service.thread_hdls());
|
2018-07-02 10:07:32 -07:00
|
|
|
|
2018-08-22 19:00:56 -06:00
|
|
|
let blob_recycler = BlobRecycler::default();
|
|
|
|
let window =
|
|
|
|
window::new_window_from_entries(ledger_tail, entry_height, &node.data, &blob_recycler);
|
2018-07-02 15:24:40 -07:00
|
|
|
|
2018-08-22 19:00:56 -06:00
|
|
|
let crdt = Arc::new(RwLock::new(Crdt::new(node.data).expect("Crdt::new")));
|
2018-08-22 18:50:19 -06:00
|
|
|
|
2018-08-22 19:00:56 -06:00
|
|
|
let ncp = Ncp::new(
|
|
|
|
&crdt,
|
|
|
|
window.clone(),
|
|
|
|
ledger_path,
|
|
|
|
node.sockets.gossip,
|
|
|
|
node.sockets.gossip_send,
|
|
|
|
exit.clone(),
|
|
|
|
).expect("Ncp::new");
|
|
|
|
thread_hdls.extend(ncp.thread_hdls());
|
|
|
|
|
|
|
|
match leader_info {
|
2018-08-22 20:28:05 -06:00
|
|
|
Some(leader_info) => {
|
2018-08-22 19:00:56 -06:00
|
|
|
// Start in validator mode.
|
|
|
|
crdt.write().unwrap().insert(leader_info);
|
2018-08-22 18:50:19 -06:00
|
|
|
let tvu = Tvu::new(
|
|
|
|
keypair,
|
|
|
|
&bank,
|
|
|
|
entry_height,
|
2018-08-22 19:00:56 -06:00
|
|
|
crdt,
|
|
|
|
window,
|
2018-08-22 18:50:19 -06:00
|
|
|
node.sockets.replicate,
|
|
|
|
node.sockets.repair,
|
|
|
|
node.sockets.retransmit,
|
|
|
|
ledger_path,
|
|
|
|
exit.clone(),
|
|
|
|
);
|
|
|
|
thread_hdls.extend(tvu.thread_hdls());
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Start in leader mode.
|
|
|
|
let ledger_path = ledger_path.expect("ledger path");
|
|
|
|
let tick_duration = None;
|
|
|
|
// TODO: To light up PoH, uncomment the following line:
|
|
|
|
//let tick_duration = Some(Duration::from_millis(1000));
|
|
|
|
|
|
|
|
let (tpu, blob_receiver) = Tpu::new(
|
|
|
|
keypair,
|
|
|
|
&bank,
|
|
|
|
&crdt,
|
|
|
|
tick_duration,
|
|
|
|
node.sockets.transaction,
|
|
|
|
&blob_recycler,
|
|
|
|
exit.clone(),
|
|
|
|
ledger_path,
|
|
|
|
sigverify_disabled,
|
|
|
|
);
|
|
|
|
thread_hdls.extend(tpu.thread_hdls());
|
|
|
|
|
|
|
|
let broadcast_stage = BroadcastStage::new(
|
|
|
|
node.sockets.broadcast,
|
|
|
|
crdt,
|
|
|
|
window,
|
|
|
|
entry_height,
|
|
|
|
blob_recycler.clone(),
|
|
|
|
blob_receiver,
|
|
|
|
);
|
|
|
|
thread_hdls.extend(broadcast_stage.thread_hdls());
|
|
|
|
}
|
2018-08-22 18:51:53 -06:00
|
|
|
}
|
2018-08-22 18:50:19 -06:00
|
|
|
|
|
|
|
Fullnode { exit, thread_hdls }
|
2018-07-09 14:53:18 -06:00
|
|
|
}
|
|
|
|
|
2018-07-16 22:22:29 -07:00
|
|
|
//used for notifying many nodes in parallel to exit
|
2018-07-17 08:18:42 -07:00
|
|
|
pub fn exit(&self) {
|
2018-07-16 22:22:29 -07:00
|
|
|
self.exit.store(true, Ordering::Relaxed);
|
|
|
|
}
|
2018-07-09 14:53:18 -06:00
|
|
|
pub fn close(self) -> Result<()> {
|
2018-07-17 08:18:42 -07:00
|
|
|
self.exit();
|
2018-07-09 14:53:18 -06:00
|
|
|
self.join()
|
2018-07-02 15:24:40 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 22:14:08 -06:00
|
|
|
|
2018-08-09 15:29:07 -06:00
|
|
|
impl Service for Fullnode {
|
2018-07-03 22:14:08 -06:00
|
|
|
fn thread_hdls(self) -> Vec<JoinHandle<()>> {
|
|
|
|
self.thread_hdls
|
|
|
|
}
|
|
|
|
|
|
|
|
fn join(self) -> Result<()> {
|
|
|
|
for thread_hdl in self.thread_hdls() {
|
|
|
|
thread_hdl.join()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-02 15:24:40 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use bank::Bank;
|
|
|
|
use crdt::TestNode;
|
2018-08-09 15:29:07 -06:00
|
|
|
use fullnode::Fullnode;
|
2018-07-02 15:24:40 -07:00
|
|
|
use mint::Mint;
|
2018-07-17 11:45:52 -07:00
|
|
|
use service::Service;
|
2018-08-09 08:56:04 -06:00
|
|
|
use signature::{Keypair, KeypairUtil};
|
2018-07-09 14:53:18 -06:00
|
|
|
use std::sync::atomic::AtomicBool;
|
2018-07-02 15:24:40 -07:00
|
|
|
use std::sync::Arc;
|
2018-08-03 11:06:06 -07:00
|
|
|
|
2018-07-02 15:24:40 -07:00
|
|
|
#[test]
|
|
|
|
fn validator_exit() {
|
2018-08-09 08:57:24 -06:00
|
|
|
let keypair = Keypair::new();
|
|
|
|
let tn = TestNode::new_localhost_with_pubkey(keypair.pubkey());
|
2018-07-02 15:24:40 -07:00
|
|
|
let alice = Mint::new(10_000);
|
|
|
|
let bank = Bank::new(&alice);
|
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
2018-06-28 14:51:53 -07:00
|
|
|
let entry = tn.data.clone();
|
2018-08-22 20:28:05 -06:00
|
|
|
let v = Fullnode::new_with_bank(keypair, bank, 0, &[], tn, Some(&entry), exit, None, false);
|
2018-07-17 08:18:42 -07:00
|
|
|
v.exit();
|
2018-07-17 11:45:52 -07:00
|
|
|
v.join().unwrap();
|
2018-07-02 11:20:35 -07:00
|
|
|
}
|
2018-07-17 08:18:42 -07:00
|
|
|
#[test]
|
|
|
|
fn validator_parallel_exit() {
|
2018-08-09 15:29:07 -06:00
|
|
|
let vals: Vec<Fullnode> = (0..2)
|
2018-07-17 08:18:42 -07:00
|
|
|
.map(|_| {
|
2018-08-09 08:57:24 -06:00
|
|
|
let keypair = Keypair::new();
|
|
|
|
let tn = TestNode::new_localhost_with_pubkey(keypair.pubkey());
|
2018-07-17 08:18:42 -07:00
|
|
|
let alice = Mint::new(10_000);
|
|
|
|
let bank = Bank::new(&alice);
|
|
|
|
let exit = Arc::new(AtomicBool::new(false));
|
|
|
|
let entry = tn.data.clone();
|
2018-08-22 20:28:05 -06:00
|
|
|
Fullnode::new_with_bank(keypair, bank, 0, &[], tn, Some(&entry), exit, None, false)
|
2018-07-17 08:18:42 -07:00
|
|
|
})
|
|
|
|
.collect();
|
2018-07-17 11:45:52 -07:00
|
|
|
//each validator can exit in parallel to speed many sequential calls to `join`
|
2018-08-05 22:04:27 -07:00
|
|
|
vals.iter().for_each(|v| v.exit());
|
2018-07-17 11:45:52 -07:00
|
|
|
//while join is called sequentially, the above exit call notified all the
|
2018-07-17 08:18:42 -07:00
|
|
|
//validators to exit from all their threads
|
2018-08-03 11:06:06 -07:00
|
|
|
vals.into_iter().for_each(|v| {
|
2018-08-05 22:04:27 -07:00
|
|
|
v.join().unwrap();
|
2018-08-03 11:06:06 -07:00
|
|
|
});
|
2018-07-17 08:18:42 -07:00
|
|
|
}
|
2018-07-02 11:20:35 -07:00
|
|
|
}
|