Skip to content

Commit

Permalink
validator: added a validator crate
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtau committed Jun 30, 2022
1 parent 5cce766 commit 2f958af
Show file tree
Hide file tree
Showing 10 changed files with 808 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/1134-validator-crate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Added validator crate as per ADR-011
([#1134](https://github.com/informalsystems/tendermint-rs/issues/1134))
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ members = [
"std-ext",
"tendermint",
"test",
"testgen"
"testgen",
"validator"
]

exclude = [
Expand Down
35 changes: 35 additions & 0 deletions validator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "tendermint-validator"
version = "0.24.0-pre.2"
authors = ["Informal Systems <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
readme = "README.md"
categories = ["cryptography::cryptocurrencies", "network-programming"]
keywords = ["privval", "validator", "blockchain", "bft", "consensus", "tendermint"]
repository = "https://github.com/informalsystems/tendermint-rs"
description = """
tendermint-validator provides a simple framework with which to build key management
for Tendermint validators.
"""

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["flex-error/std", "flex-error/eyre_tracer"]

[dependencies]
ed25519-consensus = { version = "1.2", default-features = false }
flex-error = { version = "0.4.4", default-features = false }
rand_core = { version = "0.6", default-features = false, features = ["std"] }
serde_json = "1"
tempfile = { version = "3.2.0" }
tendermint = { version = "0.24.0-pre.2", default-features = false, path = "../tendermint" }
tendermint-proto = { version = "0.24.0-pre.2", default-features = false, path = "../proto", features = ["grpc"] }
tokio = { version = "1", features = ["full"] }
tokio-stream = { version = "0.1", features = ["net"] }
tonic = { version = "0.7", features = ["tls", "transport"] }
tracing = { version = "0.1", default-features = false }

[dev-dependencies]
tracing-subscriber = "0.2"
36 changes: 36 additions & 0 deletions validator/examples/softsign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// use std::collections::HashMap;

// use ed25519_consensus::SigningKey;
// use tendermint::account::Id;
// use tendermint_validator::{
// BasicServerConfig, FileStateProvider, GrpcSocket, SoftwareSigner, SoftwareSignerServer,
// };

use std::collections::HashMap;

use tendermint::chain;
use tendermint_validator::{
BasicServerConfig, FileStateProvider, GrpcSocket, KMSServer, SoftwareSigner,
};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::INFO)
.finish();
let _ = tracing::subscriber::set_global_default(subscriber);
let mut providers = HashMap::new();
let signer = SoftwareSigner::generate_ed25519(rand_core::OsRng);
let state_provider = FileStateProvider::new("/tmp/validator.json".into())
.await
.unwrap();
providers.insert(
chain::Id::try_from("test-chain-4bmabt").unwrap(),
(signer, state_provider),
);
let config = BasicServerConfig::new(None, GrpcSocket::Unix("/tmp/validator.test".into()));
let server = KMSServer::new(providers, config).await.unwrap();
server.serve().await
}
30 changes: 30 additions & 0 deletions validator/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Validator configuration.
use std::{net::SocketAddr, path::PathBuf};

use tonic::transport::ServerTlsConfig;

/// The common options for listening for gRPC connections.
#[derive(Debug, Clone)]
pub enum GrpcSocket {
/// the syntax in Tendermint config is "grpc://HOST:PORT"
Tcp(SocketAddr),
/// the syntax in Tendermint config is "grpc://unix:PATH"
Unix(PathBuf),
}

/// The basic configuration for the gRPC server.
#[derive(Debug, Clone)]
pub struct BasicServerConfig {
/// The optional TLS configuration.
pub tls_config: Option<ServerTlsConfig>,
/// The choice of a socket type to listen on.
pub socket: GrpcSocket,
}

impl BasicServerConfig {
/// creates a basic config for the gRPC server
pub fn new(tls_config: Option<ServerTlsConfig>, socket: GrpcSocket) -> Self {
Self { tls_config, socket }
}
}
18 changes: 18 additions & 0 deletions validator/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Validator errors (returned in a status via gRPC)
use flex_error::{define_error, DetailOnly};

define_error! {
Error {
IoError{
path: String,
} [DetailOnly<std::io::Error>] |e| {
format_args!("Error persisting {}", e.path)
},
JsonError{
path_or_msg: String,
} [DetailOnly<serde_json::Error>] |e| {
format_args!("Error parsing or serializing validator state {}", e.path_or_msg)
},
}
}
13 changes: 13 additions & 0 deletions validator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! A framework for building key management solutions for [Tendermint] validators in Rust.
//!
//! [Tendermint]: https://tendermint.com
pub mod config;
pub mod error;
pub mod server;
pub mod signer;
pub mod state;

pub use config::{BasicServerConfig, GrpcSocket};
pub use server::KMSServer;
pub use signer::{SignerProvider, SoftwareSigner};
pub use state::{FileStateProvider, ValidatorStateProvider};
Loading

0 comments on commit 2f958af

Please sign in to comment.