From 28af4c9b0b457e54d4b9aed0dcbf98043293a8bd Mon Sep 17 00:00:00 2001 From: Age Manning Date: Fri, 23 Aug 2024 02:35:05 +1000 Subject: [PATCH] Templating executor, logging and CLI --- .gitignore | 19 + Cargo.lock | 1103 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 16 + build.rs | 2 + src/cli.rs | 180 ++++++++ src/client/mod.rs | 71 +++ src/config.rs | 225 +++++++++ src/environment.rs | 184 ++++++++ src/main.rs | 80 +++- src/version.rs | 68 +++ 10 files changed, 1947 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 build.rs create mode 100644 src/cli.rs create mode 100644 src/client/mod.rs create mode 100644 src/config.rs create mode 100644 src/environment.rs create mode 100644 src/version.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..b627be9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +target/ +vendor/ +**/*.rs.bk +*.pk +*.sk +*.raw_keypairs +flamegraph.svg +perf.data* +*.tar.gz +/bin +/clippy.toml +/.cargo + +# IntelliJ +/*.iml +.idea + +# VSCode +/.vscode diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 00000000..e5a91552 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1103 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "addr2line" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + +[[package]] +name = "async-channel" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b47800b0be77592da0afd425cc03468052844aff33b84e33cc696f64e77b6a" +dependencies = [ + "concurrent-queue", + "event-listener-strategy", + "futures-core", + "pin-project-lite", +] + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "backtrace" +version = "0.3.73" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cc" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9e8aabfac534be767c909e0690571677d49f41bd8465ae876fe043d52ba5292" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "216aec2b177652e3846684cbfe25c9964d18ec45234f0f5da5157b207ed1aab6" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "cpufeatures" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51e852e6dc9a5bed1fae92dd2375037bf2b768725bf3be87811edee3249d09ad" +dependencies = [ + "libc", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.48.0", +] + +[[package]] +name = "ethereum_hashing" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c853bd72c9e5787f8aafc3df2907c2ed03cff3150c3acd94e2e53a98ab70a8ab" +dependencies = [ + "cpufeatures", + "ring", + "sha2", +] + +[[package]] +name = "event-listener" +version = "5.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6032be9bd27023a771701cc49f9f053c751055f71efb2e0ae5c15809093675ba" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f214dc438f977e6d4e3500aaa277f5ad94ca83fbbd9b1a15713ce2344ccc5a1" +dependencies = [ + "event-listener", + "pin-project-lite", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-executor" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-io" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" + +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "futures-sink" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "gimli" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" + +[[package]] +name = "git-version" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad568aa3db0fcbc81f2f116137f263d7304f512a1209b35b85150d3ef88ad19" +dependencies = [ + "git-version-macro", +] + +[[package]] +name = "git-version-macro" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53010ccb100b96a67bc32c0175f0ed1426b31b655d562898e57325f81c023ac0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags", + "libc", +] + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "metrics" +version = "0.2.0" +dependencies = [ + "prometheus", +] + +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi", + "libc", + "wasi", + "windows-sys 0.52.0", +] + +[[package]] +name = "object" +version = "0.36.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27b64972346851a39438c60b341ebc01bba47464ae329e55cf343eb93964efd9" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "parking" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae" + +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.52.6", +] + +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "prometheus" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d33c28a30771f7f96db69893f78b857f7450d7e0237e9c8fc6427a81bae7ed1" +dependencies = [ + "cfg-if", + "fnv", + "lazy_static", + "memchr", + "parking_lot", + "protobuf", + "thiserror", +] + +[[package]] +name = "protobuf" +version = "2.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "106dd99e98437432fed6519dedecfade6a06a73bb7b2a1e019fdd2bee5778d94" + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_syscall" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "sensitive_url" +version = "0.1.0" +dependencies = [ + "serde", + "url", +] + +[[package]] +name = "serde" +version = "1.0.208" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.208" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" +dependencies = [ + "autocfg", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "ssv" +version = "0.1.0" +dependencies = [ + "async-channel", + "clap", + "dirs", + "ethereum_hashing", + "futures", + "git-version", + "regex", + "sensitive_url", + "serde", + "target_info", + "task_executor", + "tokio", + "tracing", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "target_info" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c63f48baada5c52e65a29eef93ab4f8982681b67f9e8d29c7b05abcfec2b9ffe" + +[[package]] +name = "task_executor" +version = "0.1.0" +dependencies = [ + "async-channel", + "futures", + "metrics", + "tokio", + "tracing", +] + +[[package]] +name = "thiserror" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "tokio" +version = "1.39.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daa4fb1bc778bd6f04cbfc4bb2d06a7396a8f299dc33ea1900cedaa316f467b1" +dependencies = [ + "backtrace", + "libc", + "mio", + "pin-project-lite", + "signal-hook-registry", + "tokio-macros", + "windows-sys 0.52.0", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-normalization" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index ad7bcdc2..c1dcb83f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,19 @@ edition = "2021" author = "Sigma Prime" [dependencies] +task_executor = { path = "../sigp-crates/task_executor" } +sensitive_url = { path = "../sigp-crates/sensitive_url" } +async-channel = "2.3.1" +clap = "4.5.15" +dirs = "5.0.1" +ethereum_hashing = "0.7.0" +futures = "0.3.30" +# dirs = "3" +serde = { version = "1.0.208", features = ["derive"] } +tokio = { version = "1.39.2", features = ["rt", "rt-multi-thread", "time", "signal"] } +tracing = "0.1.40" +git-version = "0.3.9" +target_info = "0.1.0" + +[dev-dependencies] +regex = "1.10.6" diff --git a/build.rs b/build.rs new file mode 100644 index 00000000..3d8a25ec --- /dev/null +++ b/build.rs @@ -0,0 +1,2 @@ +// This is a stub for determining the build profile, see `build_profile_name`. +fn main() {} diff --git a/src/cli.rs b/src/cli.rs new file mode 100644 index 00000000..0de4af4b --- /dev/null +++ b/src/cli.rs @@ -0,0 +1,180 @@ +use clap::builder::styling::*; +use clap::{builder::ArgPredicate, Arg, ArgAction, Command}; +// use clap_utils::{get_color_style, FLAG_HEADER}; +use crate::version::VERSION; +use ethereum_hashing::have_sha_extensions; +use std::sync::LazyLock; + +pub static SHORT_VERSION: LazyLock = LazyLock::new(|| VERSION.replace("SSV/", "")); +pub static LONG_VERSION: LazyLock = LazyLock::new(|| { + format!( + "{}\n\ + SHA256 hardware acceleration: {}\n\ + Allocator: {}\n\ + Profile: {}", + SHORT_VERSION.as_str(), + have_sha_extensions(), + allocator_name(), + build_profile_name(), + ) +}); + +pub const FLAG_HEADER: &str = "Flags"; + +fn allocator_name() -> &'static str { + if cfg!(target_os = "windows") { + "system" + } else { + "jemalloc" + } +} + +fn build_profile_name() -> String { + // Nice hack from https://stackoverflow.com/questions/73595435/how-to-get-profile-from-cargo-toml-in-build-rs-or-at-runtime + // The profile name is always the 3rd last part of the path (with 1 based indexing). + // e.g. /code/core/target/cli/build/my-build-info-9f91ba6f99d7a061/out + std::env!("OUT_DIR") + .split(std::path::MAIN_SEPARATOR) + .nth_back(3) + .unwrap_or_else(|| "unknown") + .to_string() +} + +pub fn cli_app() -> Command { + Command::new("ssv") + .version(SHORT_VERSION.as_str()) + .author("Sigma Prime ") + .styles(get_color_style()) + .next_line_help(true) + .term_width(80) + .disable_help_flag(true) + .about( + "SSV Validator client. Maintained by Sigma Prime", + ) + .long_version(LONG_VERSION.as_str()) + .display_order(0) + .arg( + Arg::new("debug-level") + .long("debug-level") + .value_name("LEVEL") + .help("Specifies the verbosity level used when emitting logs to the terminal.") + .action(ArgAction::Set) + .value_parser(["info", "debug", "trace", "warn", "error"]) + .global(true) + .default_value("info") + .display_order(0) + ) + .arg( + Arg::new("datadir") + .long("datadir") + .short('d') + .value_name("DIR") + .global(true) + .help( + "Used to specify a custom root data directory for lighthouse keys and databases. \ + Defaults to $HOME/.lighthouse/{network} where network is the value of the `network` flag \ + Note: Users should specify separate custom datadirs for different networks.") + .action(ArgAction::Set) + .display_order(0) + ) + /* REST API related arguments */ + .arg( + Arg::new("http") + .long("http") + .help("Enable the RESTful HTTP API server. Disabled by default.") + .action(ArgAction::SetTrue) + .help_heading(FLAG_HEADER) + .display_order(0) + ) + /* + * Note: The HTTP server is **not** encrypted (i.e., not HTTPS) and therefore it is + * unsafe to publish on a public network. + * + * If the `--http-address` flag is used, the `--unencrypted-http-transport` flag + * must also be used in order to make it clear to the user that this is unsafe. + */ + .arg( + Arg::new("http-address") + .long("http-address") + .requires("http") + .value_name("ADDRESS") + .help("Set the address for the HTTP address. The HTTP server is not encrypted \ + and therefore it is unsafe to publish on a public network. When this \ + flag is used, it additionally requires the explicit use of the \ + `--unencrypted-http-transport` flag to ensure the user is aware of the \ + risks involved. For access via the Internet, users should apply \ + transport-layer security like a HTTPS reverse-proxy or SSH tunnelling.") + .requires("unencrypted-http-transport") + .display_order(0) + ) + .arg( + Arg::new("unencrypted-http-transport") + .long("unencrypted-http-transport") + .help("This is a safety flag to ensure that the user is aware that the http \ + transport is unencrypted and using a custom HTTP address is unsafe.") + .action(ArgAction::SetTrue) + .help_heading(FLAG_HEADER) + .requires("http-address") + .hide(true) + .display_order(0) + ) + .arg( + Arg::new("http-port") + .long("http-port") + .requires("http") + .value_name("PORT") + .help("Set the listen TCP port for the RESTful HTTP API server.") + .default_value_if("http", ArgPredicate::IsPresent, "5062") + .action(ArgAction::Set) + .display_order(0) + ) + .arg( + Arg::new("http-allow-origin") + .long("http-allow-origin") + .requires("http") + .value_name("ORIGIN") + .help("Set the value of the Access-Control-Allow-Origin response HTTP header. \ + Use * to allow any origin (not recommended in production). \ + If no value is supplied, the CORS allowed origin is set to the listen \ + address of this server (e.g., http://localhost:5062).") + .action(ArgAction::Set) + .display_order(0) + ) + /* Prometheus metrics HTTP server related arguments */ + .arg( + Arg::new("metrics") + .long("metrics") + .help("Enable the Prometheus metrics HTTP server. Disabled by default.") + .action(ArgAction::SetTrue) + .help_heading(FLAG_HEADER) + .display_order(0) + ) + .arg( + Arg::new("metrics-address") + .long("metrics-address") + .requires("metrics") + .value_name("ADDRESS") + .help("Set the listen address for the Prometheus metrics HTTP server.") + .default_value_if("metrics", ArgPredicate::IsPresent, "127.0.0.1") + .action(ArgAction::Set) + .display_order(0) + ) + .arg( + Arg::new("metrics-port") + .long("metrics-port") + .requires("metrics") + .value_name("PORT") + .help("Set the listen TCP port for the Prometheus metrics HTTP server.") + .default_value_if("metrics", ArgPredicate::IsPresent, "5064") + .action(ArgAction::Set) + .display_order(0) + ) +} + +pub fn get_color_style() -> Styles { + Styles::styled() + .header(AnsiColor::Yellow.on_default()) + .usage(AnsiColor::Green.on_default()) + .literal(AnsiColor::Green.on_default()) + .placeholder(AnsiColor::Green.on_default()) +} diff --git a/src/client/mod.rs b/src/client/mod.rs new file mode 100644 index 00000000..d1a2a55e --- /dev/null +++ b/src/client/mod.rs @@ -0,0 +1,71 @@ +// use tracing::{debug, info}; +use crate::config::Config; +use task_executor::TaskExecutor; + +pub struct SSVClient {} + +impl SSVClient { + /// Instantiates the SSZ client + pub async fn new(_executor: TaskExecutor, _config: Config) -> Result { + /* + // Attempt to raise soft fd limit. The behavior is OS specific: + // `linux` - raise soft fd limit to hard + // `macos` - raise soft fd limit to `min(kernel limit, hard fd limit)` + // `windows` & rest - noop + match fdlimit::raise_fd_limit().map_err(|e| format!("Unable to raise fd limit: {}", e))? { + fdlimit::Outcome::LimitRaised { from, to } => { + debug!( + "old_limit" = from, + "new_limit" = to + "Raised soft open file descriptor resource limit" + ); + } + fdlimit::Outcome::Unsupported => { + debug!("Raising soft open file descriptor resource limit is not supported"); + } + }; + + info!( + "beacon_nodes" = format!("{:?}", &config.beacon_nodes), + "validator_dir" = format!("{:?}", config.validator_dir), + "Starting validator client" + ); + + // Optionally start the metrics server. + let http_metrics_ctx = if config.http_metrics.enabled { + let shared = http_metrics::Shared { + validator_store: None, + genesis_time: None, + duties_service: None, + }; + + let ctx: Arc> = Arc::new(http_metrics::Context { + config: config.http_metrics.clone(), + shared: RwLock::new(shared), + log: log.clone(), + }); + + let exit = context.executor.exit(); + + let (_listen_addr, server) = http_metrics::serve(ctx.clone(), exit) + .map_err(|e| format!("Unable to start metrics API server: {:?}", e))?; + + context + .clone() + .executor + .spawn_without_exit(server, "metrics-api"); + + Some(ctx) + } else { + info!(log, "HTTP metrics server is disabled"); + None + }; + */ + Ok(SSVClient {}) + } + + /// Executes the main client logic + pub async fn run(&mut self) -> Result<(), String> { + Ok(()) + } +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 00000000..bd3442ee --- /dev/null +++ b/src/config.rs @@ -0,0 +1,225 @@ +// use crate::{http_api, http_metrics}; +use clap::ArgMatches; +// use clap_utils::{flags::DISABLE_MALLOC_TUNING_FLAG, parse_optional, parse_required}; + +use sensitive_url::SensitiveUrl; +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::PathBuf; +use std::str::FromStr; + +pub const DEFAULT_BEACON_NODE: &str = "http://localhost:5052/"; +/// The default Data directory, relative to the users home directory +pub const DEFAULT_ROOT_DIR: &str = ".ssv"; +/// Default network, used to partition the data storage +pub const DEFAULT_HARDCODED_NETWORK: &str = "mainnet"; +/// Directory within the network directory where secrets are stored. +pub const DEFAULT_SECRETS_DIR: &str = "secrets"; + +/// Stores the core configuration for this SSV instance. +#[derive(Clone, Serialize, Deserialize)] +pub struct Config { + /// The data directory, which stores all validator databases + pub data_dir: PathBuf, + /// The directory containing the passwords to unlock validator keystores. + pub secrets_dir: PathBuf, + /// The http endpoints of the beacon node APIs. + /// + /// Should be similar to `["http://localhost:8080"]` + pub beacon_nodes: Vec, + /// beacon node is not synced at startup. + pub allow_unsynced_beacon_node: bool, + /// Configuration for the HTTP REST API. + // TODO: + // pub http_api: http_api::Config, + /// Configuration for the HTTP REST API. + // TODO: + // pub http_metrics: http_metrics::Config, + /// A list of custom certificates that the validator client will additionally use when + /// connecting to a beacon node over SSL/TLS. + pub beacon_nodes_tls_certs: Option>, +} + +impl Default for Config { + /// Build a new configuration from defaults. + fn default() -> Self { + // WARNING: these directory defaults should be always overwritten with parameters from cli + // for specific networks. + let data_dir = dirs::home_dir() + .unwrap_or_else(|| PathBuf::from(".")) + .join(DEFAULT_ROOT_DIR) + .join(DEFAULT_HARDCODED_NETWORK); + let secrets_dir = data_dir.join(DEFAULT_SECRETS_DIR); + + let beacon_nodes = vec![SensitiveUrl::parse(DEFAULT_BEACON_NODE) + .expect("beacon_nodes must always be a valid url.")]; + Self { + data_dir, + secrets_dir, + beacon_nodes, + allow_unsynced_beacon_node: false, + // http_api: <_>::default(), + // http_metrics: <_>::default(), + beacon_nodes_tls_certs: None, + } + } +} + +/// Returns a `Default` implementation of `Self` with some parameters modified by the supplied +/// `cli_args`. +pub fn from_cli(cli_args: &ArgMatches) -> Result { + let mut config = Config::default(); + + let default_root_dir = dirs::home_dir() + .map(|home| home.join(DEFAULT_ROOT_DIR)) + .unwrap_or_else(|| PathBuf::from(".")); + + let (mut data_dir, mut secrets_dir) = (None, None); + if cli_args.get_one::("datadir").is_some() { + let temp_data_dir: PathBuf = parse_required(cli_args, "datadir")?; + secrets_dir = Some(temp_data_dir.join(DEFAULT_SECRETS_DIR)); + data_dir = Some(temp_data_dir); + }; + + if cli_args.get_one::("secrets-dir").is_some() { + secrets_dir = Some(parse_required(cli_args, "secrets-dir")?); + } + + config.data_dir = data_dir.unwrap_or_else(|| default_root_dir.join(DEFAULT_ROOT_DIR)); + + config.secrets_dir = secrets_dir.unwrap_or_else(|| default_root_dir.join(DEFAULT_SECRETS_DIR)); + + if !config.data_dir.exists() { + fs::create_dir_all(&config.data_dir) + .map_err(|e| format!("Failed to create {:?}: {:?}", config.data_dir, e))?; + } + + if let Some(beacon_nodes) = parse_optional::(cli_args, "beacon-nodes")? { + config.beacon_nodes = beacon_nodes + .split(',') + .map(SensitiveUrl::parse) + .collect::>() + .map_err(|e| format!("Unable to parse beacon node URL: {:?}", e))?; + } + + if let Some(tls_certs) = parse_optional::(cli_args, "beacon-nodes-tls-certs")? { + config.beacon_nodes_tls_certs = Some(tls_certs.split(',').map(PathBuf::from).collect()); + } + + /* + * Http API server + */ + // TODO: + + /* + if cli_args.get_flag("http") { + config.http_api.enabled = true; + } + + if let Some(address) = cli_args.get_one::("http-address") { + if cli_args.get_flag("unencrypted-http-transport") { + config.http_api.listen_addr = address + .parse::() + .map_err(|_| "http-address is not a valid IP address.")?; + } else { + return Err( + "While using `--http-address`, you must also use `--unencrypted-http-transport`." + .to_string(), + ); + } + } + + if let Some(port) = cli_args.get_one::("http-port") { + config.http_api.listen_port = port + .parse::() + .map_err(|_| "http-port is not a valid u16.")?; + } + + if let Some(allow_origin) = cli_args.get_one::("http-allow-origin") { + // Pre-validate the config value to give feedback to the user on node startup, instead of + // as late as when the first API response is produced. + hyper::header::HeaderValue::from_str(allow_origin) + .map_err(|_| "Invalid allow-origin value")?; + + config.http_api.allow_origin = Some(allow_origin.to_string()); + } + */ + + /* + * Prometheus metrics HTTP server + */ + + // TODO: + /* + if cli_args.get_flag("metrics") { + config.http_metrics.enabled = true; + } + + if let Some(address) = cli_args.get_one::("metrics-address") { + config.http_metrics.listen_addr = address + .parse::() + .map_err(|_| "metrics-address is not a valid IP address.")?; + } + + if let Some(port) = cli_args.get_one::("metrics-port") { + config.http_metrics.listen_port = port + .parse::() + .map_err(|_| "metrics-port is not a valid u16.")?; + } + + if let Some(allow_origin) = cli_args.get_one::("metrics-allow-origin") { + // Pre-validate the config value to give feedback to the user on node startup, instead of + // as late as when the first API response is produced. + hyper::header::HeaderValue::from_str(allow_origin) + .map_err(|_| "Invalid allow-origin value")?; + + config.http_metrics.allow_origin = Some(allow_origin.to_string()); + } + + if cli_args.get_flag(DISABLE_MALLOC_TUNING_FLAG) { + config.http_metrics.allocator_metrics_enabled = false; + } + */ + + Ok(config) +} + +// Helper functions for handling CLAP arguments + +/// Returns the value of `name` or an error if it is not in `matches` or does not parse +/// successfully using `std::string::FromStr`. +pub fn parse_required(matches: &ArgMatches, name: &str) -> Result +where + T: FromStr, + ::Err: std::fmt::Display, +{ + parse_optional(matches, name)?.ok_or_else(|| format!("{} not specified", name)) +} + +/// Returns the value of `name` (if present) or an error if it does not parse successfully using +/// `std::string::FromStr`. +pub fn parse_optional(matches: &ArgMatches, name: &str) -> Result, String> +where + T: FromStr, + ::Err: std::fmt::Display, +{ + matches + .try_get_one::(name) + .map_err(|e| format!("Unable to parse {}: {}", name, e))? + .map(|val| { + val.parse() + .map_err(|e| format!("Unable to parse {}: {}", name, e)) + }) + .transpose() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + // Ensures the default config does not panic. + fn default_config() { + Config::default(); + } +} diff --git a/src/environment.rs b/src/environment.rs new file mode 100644 index 00000000..3c722ae4 --- /dev/null +++ b/src/environment.rs @@ -0,0 +1,184 @@ +//! This struct is used to initialize the tokio runtime, task manager and provides basic +//! functionality for the application to start and shutdown gracefully. + +use futures::channel::mpsc::{channel, Receiver, Sender}; +use futures::{future, StreamExt}; +use std::sync::Arc; +use task_executor::{ShutdownReason, TaskExecutor}; +use tokio::runtime::{Builder as RuntimeBuilder, Runtime}; +use tracing::{error, info, warn}; +use { + futures::Future, + std::{pin::Pin, task::Context, task::Poll}, + tokio::signal::unix::{signal, Signal, SignalKind}, +}; + +/// The maximum time in seconds the client will wait for all internal tasks to shutdown. +const MAXIMUM_SHUTDOWN_TIME: u64 = 15; + +pub struct Environment { + /// The tokio runtime for the client, used to spawn tasks. + runtime: Arc, + /// Receiver side of an internal shutdown signal. + signal_rx: Option>, + /// Sender to request shutting down. + signal_tx: Sender, + signal: Option>, + exit: async_channel::Receiver<()>, +} + +impl Default for Environment { + /// Generates a default multi-threaded tokio task executor with the required channels in order + /// to gracefully shutdown the application. + /// + /// If a more fine-grained executor is required, a more general function should be built. + fn default() -> Self { + // Create a multi-threaded task executor + let runtime = match RuntimeBuilder::new_multi_thread().enable_all().build() { + Err(e) => { + error!(error = %e, "Failed to initialize runtime"); + panic!("Failed to start the application"); + } + Ok(runtime) => Arc::new(runtime), + }; + // Channels to exit tasks and gracefully terminate the application + let (signal, exit) = async_channel::bounded(1); + let (signal_tx, signal_rx) = channel(1); + + Environment { + runtime, + signal_rx: Some(signal_rx), + signal_tx, + signal: Some(signal), + exit, + } + } +} + +impl Environment { + /// Generates a task executor that can be used by the application to spawn tasks. + pub fn executor(&mut self) -> TaskExecutor { + TaskExecutor::new( + Arc::downgrade(self.runtime()), + self.exit.clone(), + self.signal_tx.clone(), + ) + } + + /// Returns a mutable reference to the `tokio` runtime. + /// + /// Useful in the rare scenarios where it's necessary to block the current thread until a task + /// is finished (e.g., during testing). + pub fn runtime(&self) -> &Arc { + &self.runtime + } + + /// Block the current thread until a shutdown signal is received. + /// + /// This can be either the user Ctrl-C'ing or a task requesting to shutdown. + #[cfg(target_family = "unix")] + pub fn block_until_shutdown_requested(&mut self) -> Result { + // future of a task requesting to shutdown + let mut signal_rx = self + .signal_rx + .take() + .ok_or("Inner shutdown already received")?; + let inner_shutdown = async move { + signal_rx + .next() + .await + .ok_or("Internal shutdown channel exhausted") + }; + futures::pin_mut!(inner_shutdown); + + let register_handlers = async { + let mut handles = vec![]; + + // setup for handling SIGTERM + match signal(SignalKind::terminate()) { + Ok(terminate_stream) => { + let terminate = SignalFuture::new(terminate_stream, "Received SIGTERM"); + handles.push(terminate); + } + Err(e) => error!(error = ?e, "Could not register SIGTERM handler"), + }; + + // setup for handling SIGINT + match signal(SignalKind::interrupt()) { + Ok(interrupt_stream) => { + let interrupt = SignalFuture::new(interrupt_stream, "Received SIGINT"); + handles.push(interrupt); + } + Err(e) => error!(error = ?e, "Could not register SIGINT handler"), + } + + // setup for handling a SIGHUP + match signal(SignalKind::hangup()) { + Ok(hup_stream) => { + let hup = SignalFuture::new(hup_stream, "Received SIGHUP"); + handles.push(hup); + } + Err(e) => error!(error = ?e, "Could not register SIGHUP handler"), + } + + future::select(inner_shutdown, future::select_all(handles.into_iter())).await + }; + + match self.runtime().block_on(register_handlers) { + future::Either::Left((Ok(reason), _)) => { + info!(reason = reason.message(), "Internal shutdown received"); + Ok(reason) + } + future::Either::Left((Err(e), _)) => Err(e.into()), + future::Either::Right(((res, _, _), _)) => { + res.ok_or_else(|| "Handler channel closed".to_string()) + } + } + } + + /// Shutdown the `tokio` runtime when all tasks are idle. + pub fn shutdown_on_idle(self) { + match Arc::try_unwrap(self.runtime) { + Ok(runtime) => { + runtime.shutdown_timeout(std::time::Duration::from_secs(MAXIMUM_SHUTDOWN_TIME)) + } + Err(e) => warn!( + error = ?e, + "Failed to obtain runtime access to shutdown gracefully" + ), + } + } + + /// Fire exit signal which shuts down all spawned services + pub fn fire_signal(&mut self) { + if let Some(signal) = self.signal.take() { + drop(signal); + } + } +} + +#[cfg(target_family = "unix")] +struct SignalFuture { + signal: Signal, + message: &'static str, +} + +#[cfg(target_family = "unix")] +impl SignalFuture { + pub fn new(signal: Signal, message: &'static str) -> SignalFuture { + SignalFuture { signal, message } + } +} + +#[cfg(target_family = "unix")] +impl Future for SignalFuture { + type Output = Option; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + match self.signal.poll_recv(cx) { + Poll::Pending => Poll::Pending, + Poll::Ready(Some(_)) => Poll::Ready(Some(ShutdownReason::Success(self.message))), + Poll::Ready(None) => Poll::Ready(None), + } + } +} diff --git a/src/main.rs b/src/main.rs index e7a11a96..4ccff8c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,81 @@ +use tracing::{error, info}; + +mod cli; +mod client; +mod config; +mod environment; +mod version; + +use client::SSVClient; +use environment::Environment; +use futures::TryFutureExt; +use task_executor::ShutdownReason; + fn main() { - println!("Hello, world!"); + // Enable backtraces unless a RUST_BACKTRACE value has already been explicitly provided. + if std::env::var("RUST_BACKTRACE").is_err() { + std::env::set_var("RUST_BACKTRACE", "1"); + } + + // Obtain the CLI and build the config + let cli = cli::cli_app(); + let matches = cli.get_matches(); + // Build the config + let config = match config::from_cli(&matches) { + Ok(config) => config, + Err(e) => { + error!(e, "Unable to initialize configuration"); + return; + } + }; + + // Construct the task executor and exit signals + let mut environment = Environment::default(); + // Build the core task executor + let core_executor = environment.executor(); + + // The clone's here simply copy the Arc of the runtime. We pass these through the main + // execution task + let ssv_executor = core_executor.clone(); + let shutdown_executor = core_executor.clone(); + + // Run the main task + core_executor.spawn( + async move { + if let Err(e) = SSVClient::new(ssv_executor, config) + .and_then(|mut client| async move { client.run().await }) + .await + { + error!(reason = e, "Failed to start SSZ client"); + // Ignore the error since it always occurs during normal operation when + // shutting down. + let _ = shutdown_executor + .shutdown_sender() + .try_send(ShutdownReason::Failure("Failed to start SSZ client")); + } + }, + "ssz_client", + ); + + // Block this thread until we get a ctrl-c or a task sends a shutdown signal. + let shutdown_reason = match environment.block_until_shutdown_requested() { + Ok(reason) => reason, + Err(e) => { + error!(error = ?e, "Failed to shutdown"); + return; + } + }; + info!(reason = ?shutdown_reason, "Shutting down..."); + + environment.fire_signal(); + + // Shutdown the environment once all tasks have completed. + environment.shutdown_on_idle(); + + match shutdown_reason { + ShutdownReason::Success(_) => {} + ShutdownReason::Failure(msg) => { + error!(reason = msg.to_string(), "Failed to shutdown gracefully"); + } + }; } diff --git a/src/version.rs b/src/version.rs new file mode 100644 index 00000000..9177c747 --- /dev/null +++ b/src/version.rs @@ -0,0 +1,68 @@ +use git_version::git_version; +use target_info::Target; + +/// Returns the current version of this build of SSV. +/// +/// A plus-sign (`+`) is appended to the git commit if the tree is dirty. +/// Commit hash is omitted if the sources don't include git information. +/// +/// ## Example +/// +/// `SSVClient/v1.5.1-67da032+` +pub const VERSION: &str = git_version!( + args = [ + "--always", + "--dirty=+", + "--abbrev=7", + // NOTE: using --match instead of --exclude for compatibility with old Git + "--match=thiswillnevermatchlol" + ], + prefix = "SSV/v0.1.0-", + fallback = "SSV/v0.1.0" +); + +/// Returns the first eight characters of the latest commit hash for this build. +/// +/// No indication is given if the tree is dirty. This is part of the standard +/// for reporting the client version to the execution engine. +#[allow(dead_code)] +pub const COMMIT_PREFIX: &str = git_version!( + args = [ + "--always", + "--abbrev=8", + // NOTE: using --match instead of --exclude for compatibility with old Git + "--match=thiswillnevermatchlol" + ], + prefix = "", + suffix = "", + cargo_prefix = "", + cargo_suffix = "", + fallback = "00000000" +); + +/// Returns `VERSION`, but with platform information appended to the end. +/// +/// ## Example +/// +/// `SSV/v0.1.0-67da032+/x86_64-linux` +#[allow(dead_code)] +pub fn version_with_platform() -> String { + format!("{}/{}-{}", VERSION, Target::arch(), Target::os()) +} + +#[cfg(test)] +mod test { + use super::*; + use regex::Regex; + + #[test] + fn version_formatting() { + let re = + Regex::new(r"^SSV/v[0-9]+\.[0-9]+\.[0-9]+(-rc.[0-9])?(-[[:xdigit:]]{7})?\+?$").unwrap(); + assert!( + re.is_match(VERSION), + "version doesn't match regex: {}", + VERSION + ); + } +}