solana/src/fullnode.rs

339 lines
12 KiB
Rust
Raw Normal View History

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;
use broadcast_stage::BroadcastStage;
use crdt::{Crdt, Node, NodeInfo};
2018-08-22 10:54:37 -07:00
use drone::DRONE_PORT;
use entry::Entry;
use ledger::read_ledger;
2018-07-02 15:24:40 -07:00
use ncp::Ncp;
use packet::BlobRecycler;
2018-08-14 17:03:48 -07:00
use rpc::{JsonRpcService, RPC_PORT};
2018-07-02 15:24:40 -07:00
use rpu::Rpu;
use service::Service;
2018-08-09 07:56:04 -07:00
use signature::{Keypair, KeypairUtil};
2018-08-20 12:12:54 -07:00
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2018-07-09 13:53:18 -07:00
use std::sync::atomic::{AtomicBool, Ordering};
2018-07-02 15:24:40 -07:00
use std::sync::{Arc, RwLock};
use std::thread::{JoinHandle, Result};
2018-07-02 15:24:40 -07:00
use tpu::Tpu;
use tvu::Tvu;
use untrusted::Input;
use window;
2018-07-02 11:20:35 -07:00
2018-08-09 14:29:07 -07:00
pub struct Fullnode {
2018-07-09 13:53:18 -07:00
exit: Arc<AtomicBool>,
thread_hdls: Vec<JoinHandle<()>>,
2018-07-02 15:24:40 -07:00
}
2018-07-02 11:20:35 -07:00
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
/// Fullnode configuration to be stored in file
pub struct Config {
pub node_info: NodeInfo,
pkcs8: Vec<u8>,
}
/// Structure to be replicated by the network
impl Config {
pub fn new(bind_addr: &SocketAddr, pkcs8: Vec<u8>) -> Self {
let keypair =
2018-08-09 07:56:04 -07:00
Keypair::from_pkcs8(Input::from(&pkcs8)).expect("from_pkcs8 in fullnode::Config new");
let pubkey = keypair.pubkey();
let node_info = NodeInfo::new_with_pubkey_socketaddr(pubkey, bind_addr);
2018-07-10 12:02:51 -07:00
Config { node_info, pkcs8 }
}
2018-08-09 07:56:04 -07:00
pub fn keypair(&self) -> Keypair {
Keypair::from_pkcs8(Input::from(&self.pkcs8))
.expect("from_pkcs8 in fullnode::Config keypair")
}
}
2018-08-09 14:29:07 -07:00
impl Fullnode {
2018-08-22 16:39:34 -07:00
pub fn new(
node: Node,
ledger_path: &str,
2018-08-09 07:56:04 -07:00
keypair: Keypair,
leader_addr: Option<SocketAddr>,
sigverify_disabled: bool,
2018-08-09 14:29:07 -07:00
) -> Self {
2018-07-02 15:24:40 -07:00
info!("creating bank...");
let bank = Bank::new_default(leader_addr.is_none());
let entries = read_ledger(ledger_path, true).expect("opening ledger");
let entries = entries.map(|e| e.expect("failed to parse entry"));
2018-07-02 11:20:35 -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 {})",
local_gossip_addr, node.info.contact_info.ncp
2018-07-02 11:20:35 -07:00
);
2018-07-09 13:53:18 -07:00
let exit = Arc::new(AtomicBool::new(false));
let local_requests_addr = node.sockets.requests.local_addr().unwrap();
let requests_addr = node.info.contact_info.rpu;
let leader_info = leader_addr.map(|i| NodeInfo::new_entry_point(&i));
let server = Self::new_with_bank(
keypair,
bank,
entry_height,
&ledger_tail,
node,
2018-08-22 19:28:05 -07:00
leader_info.as_ref(),
exit,
Some(ledger_path),
sigverify_disabled,
);
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
}
/// 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 +<--------------->| |
/// | | | `-----` | | |
/// `--------` | | `------------`
/// `-------------------------------`
/// ```
pub fn new_with_bank(
2018-08-09 07:56:04 -07:00
keypair: Keypair,
bank: Bank,
2018-07-02 15:24:40 -07:00
entry_height: u64,
ledger_tail: &[Entry],
mut node: Node,
2018-08-22 19:28:05 -07:00
leader_info: Option<&NodeInfo>,
2018-07-02 15:24:40 -07:00
exit: Arc<AtomicBool>,
ledger_path: Option<&str>,
sigverify_disabled: bool,
) -> Self {
if leader_info.is_none() {
node.info.leader_id = node.info.id;
}
2018-07-02 15:24:40 -07:00
let bank = Arc::new(bank);
2018-08-22 17:51:53 -07:00
let mut thread_hdls = vec![];
2018-08-14 17:03:48 -07:00
let rpu = Rpu::new(
&bank,
node.sockets.requests,
node.sockets.respond,
exit.clone(),
);
thread_hdls.extend(rpu.thread_hdls());
// TODO: this code assumes this node is the leader
let mut drone_addr = node.info.contact_info.tpu;
2018-08-23 09:47:02 -07:00
drone_addr.set_port(DRONE_PORT);
let rpc_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::from(0)), RPC_PORT);
2018-08-23 09:47:02 -07:00
let rpc_service = JsonRpcService::new(
&bank,
node.info.contact_info.tpu,
2018-08-23 09:47:02 -07:00
drone_addr,
rpc_addr,
exit.clone(),
);
thread_hdls.extend(rpc_service.thread_hdls());
let blob_recycler = BlobRecycler::default();
let window =
window::new_window_from_entries(ledger_tail, entry_height, &node.info, &blob_recycler);
2018-07-02 15:24:40 -07:00
let crdt = Arc::new(RwLock::new(Crdt::new(node.info).expect("Crdt::new")));
let ncp = Ncp::new(
&crdt,
window.clone(),
ledger_path,
node.sockets.gossip,
exit.clone(),
);
thread_hdls.extend(ncp.thread_hdls());
match leader_info {
2018-08-22 19:28:05 -07:00
Some(leader_info) => {
// Start in validator mode.
// TODO: let Crdt get that data from the network?
crdt.write().unwrap().insert(leader_info);
let tvu = Tvu::new(
keypair,
&bank,
entry_height,
crdt,
window,
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 17:51:53 -07:00
}
Fullnode { exit, thread_hdls }
2018-07-09 13:53:18 -07: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 13:53:18 -07:00
pub fn close(self) -> Result<()> {
2018-07-17 08:18:42 -07:00
self.exit();
2018-07-09 13:53:18 -07:00
self.join()
2018-07-02 15:24:40 -07:00
}
}
2018-08-09 14:29:07 -07:00
impl Service for Fullnode {
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::Node;
2018-08-09 14:29:07 -07: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 07:56:04 -07:00
use signature::{Keypair, KeypairUtil};
2018-07-09 13:53:18 -07:00
use std::sync::atomic::AtomicBool;
2018-07-02 15:24:40 -07:00
use std::sync::Arc;
2018-07-02 15:24:40 -07:00
#[test]
fn validator_exit() {
2018-08-09 07:57:24 -07:00
let keypair = Keypair::new();
let tn = Node::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));
let entry = tn.info.clone();
2018-08-22 19:28:05 -07: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 14:29:07 -07:00
let vals: Vec<Fullnode> = (0..2)
2018-07-17 08:18:42 -07:00
.map(|_| {
2018-08-09 07:57:24 -07:00
let keypair = Keypair::new();
let tn = Node::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.info.clone();
2018-08-22 19:28:05 -07: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`
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
vals.into_iter().for_each(|v| {
v.join().unwrap();
});
2018-07-17 08:18:42 -07:00
}
2018-07-02 11:20:35 -07:00
}