From 384a118db61187a04f1203b75493e48689766341 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 13 Jul 2024 21:16:56 +0200 Subject: [PATCH] fix: making start height configurable (#54) * fix: making start height configurable * fix: clearer errors for scalar parsing --- Cargo.lock | 176 ++++++++++++++++++++++---------------------- src/cfg.rs | 19 +++-- src/main.rs | 7 +- src/node_types.rs | 44 ++++++----- src/storage.rs | 16 ++-- src/utils.rs | 7 +- src/zk_snark/mod.rs | 28 +++++-- 7 files changed, 164 insertions(+), 133 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a1890f6..91db45d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12,7 +12,7 @@ dependencies = [ "arc-swap", "backtrace", "canonical-path", - "clap 4.5.8", + "clap 4.5.9", "color-eyre", "fs-err", "once_cell", @@ -119,7 +119,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e01ed3140b2f8d422c68afa1ed2e85d996ea619c988ac834d255db32138655cb" dependencies = [ "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -235,7 +235,7 @@ dependencies = [ "actix-router", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -753,13 +753,13 @@ checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de" [[package]] name = "async-trait" -version = "0.1.80" +version = "0.1.81" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" +checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -822,7 +822,7 @@ checksum = "3c87f3f15e7794432337fc718554eaa4dc8f04c9677a950ffe366f20a162ae42" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -1097,9 +1097,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" +checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" dependencies = [ "serde", ] @@ -1136,7 +1136,7 @@ dependencies = [ "auditable-serde", "binfarce", "cargo-lock", - "clap 4.5.8", + "clap 4.5.9", "display-error-chain", "home", "is-terminal", @@ -1163,9 +1163,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.104" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74b6a57f98764a267ff415d50a25e6e166f3831a5071af4995296ea97d210490" +checksum = "47de7e88bbbd467951ae7f5a6f34f70d1b4d9cfce53d5fd70f74ebe118b3db56" dependencies = [ "jobserver", "libc", @@ -1348,9 +1348,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.8" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" +checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462" dependencies = [ "clap_builder", "clap_derive 4.5.8", @@ -1358,9 +1358,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.8" +version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" +checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942" dependencies = [ "anstream", "anstyle", @@ -1390,7 +1390,7 @@ dependencies = [ "heck 0.5.0", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -1788,7 +1788,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -1875,7 +1875,7 @@ dependencies = [ "bls12_381", "celestia-rpc", "celestia-types", - "clap 4.5.8", + "clap 4.5.9", "colored", "config", "crypto-hash", @@ -1952,7 +1952,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version 0.4.0", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -2097,7 +2097,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -2422,7 +2422,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -2877,7 +2877,7 @@ checksum = "999ce923619f88194171a67fb3e6d613653b8d4d6078b529b15a765da0edcc17" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -3472,9 +3472,9 @@ dependencies = [ [[package]] name = "http-body" -version = "1.0.0" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" +checksum = "1efedce1fb8e6913f23e0c92de8e62cd5b772a67e7b3946df930a62566c93184" dependencies = [ "bytes", "http 1.1.0", @@ -3489,7 +3489,7 @@ dependencies = [ "bytes", "futures-util", "http 1.1.0", - "http-body 1.0.0", + "http-body 1.0.1", "pin-project-lite", ] @@ -3513,9 +3513,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.29" +version = "0.14.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f361cde2f109281a220d4307746cdfd5ee3f410da58a70377762396775634b33" +checksum = "a152ddd61dfaec7273fe8419ab357f33aee0d914c5f4efbf0d96fa749eea5ec9" dependencies = [ "bytes", "futures-channel", @@ -3537,16 +3537,16 @@ dependencies = [ [[package]] name = "hyper" -version = "1.4.0" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4fe55fb7a772d59a5ff1dfbff4fe0258d19b89fec4b233e75d35d5d2316badc" +checksum = "50dfd22e0e76d0f662d429a5f80fcaf3855009297eab6a0a9f8543834744ba05" dependencies = [ "bytes", "futures-channel", "futures-util", "h2 0.4.5", "http 1.1.0", - "http-body 1.0.0", + "http-body 1.0.1", "httparse", "itoa 1.0.11", "pin-project-lite", @@ -3563,7 +3563,7 @@ checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" dependencies = [ "futures-util", "http 0.2.12", - "hyper 0.14.29", + "hyper 0.14.30", "log", "rustls 0.21.12", "rustls-native-certs 0.6.3", @@ -3579,9 +3579,9 @@ checksum = "5ee4be2c948921a1a5320b629c4193916ed787a7f7f293fd3f7f5a6c9de74155" dependencies = [ "futures-util", "http 1.1.0", - "hyper 1.4.0", + "hyper 1.4.1", "hyper-util", - "rustls 0.23.10", + "rustls 0.23.11", "rustls-native-certs 0.7.1", "rustls-pki-types", "tokio", @@ -3596,7 +3596,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" dependencies = [ "bytes", - "hyper 0.14.29", + "hyper 0.14.30", "native-tls", "tokio", "tokio-native-tls", @@ -3612,8 +3612,8 @@ dependencies = [ "futures-channel", "futures-util", "http 1.1.0", - "http-body 1.0.0", - "hyper 1.4.0", + "http-body 1.0.1", + "hyper 1.4.1", "pin-project-lite", "socket2 0.5.7", "tokio", @@ -3925,7 +3925,7 @@ dependencies = [ "beef", "futures-timer", "futures-util", - "hyper 0.14.29", + "hyper 0.14.30", "jsonrpsee-types 0.20.3", "rustc-hash", "serde", @@ -3964,7 +3964,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f80c17f62c7653ce767e3d7288b793dfec920f97067ceb189ebdd3570f2bc20" dependencies = [ "async-trait", - "hyper 0.14.29", + "hyper 0.14.30", "hyper-rustls 0.24.2", "jsonrpsee-core 0.20.3", "jsonrpsee-types 0.20.3", @@ -4236,7 +4236,7 @@ checksum = "5cf92c10c7e361d6b99666ec1c6f9805b0bea2c3bd8c78dc6fe98ac5bd78db11" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -4320,7 +4320,7 @@ dependencies = [ "cfg-if 1.0.0", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -4603,7 +4603,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -4771,7 +4771,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -4812,7 +4812,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -4981,7 +4981,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f12335488a2f3b0a83b14edad48dca9879ce89b2edd10e80237e4e852dd645e" dependencies = [ "proc-macro2", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -5106,7 +5106,7 @@ dependencies = [ "prost 0.12.6", "prost-types", "regex", - "syn 2.0.68", + "syn 2.0.71", "tempfile", ] @@ -5133,7 +5133,7 @@ dependencies = [ "itertools 0.12.1", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -5195,7 +5195,7 @@ dependencies = [ "quinn-proto", "quinn-udp", "rustc-hash", - "rustls 0.23.10", + "rustls 0.23.11", "thiserror", "tokio", "tracing", @@ -5211,7 +5211,7 @@ dependencies = [ "rand 0.8.5", "ring", "rustc-hash", - "rustls 0.23.10", + "rustls 0.23.11", "slab", "thiserror", "tinyvec", @@ -5486,7 +5486,7 @@ dependencies = [ "h2 0.3.26", "http 0.2.12", "http-body 0.4.6", - "hyper 0.14.29", + "hyper 0.14.30", "hyper-rustls 0.24.2", "hyper-tls", "ipnet", @@ -5531,9 +5531,9 @@ dependencies = [ "futures-util", "h2 0.4.5", "http 1.1.0", - "http-body 1.0.0", + "http-body 1.0.1", "http-body-util", - "hyper 1.4.0", + "hyper 1.4.1", "hyper-rustls 0.27.2", "hyper-util", "ipnet", @@ -5544,7 +5544,7 @@ dependencies = [ "percent-encoding", "pin-project-lite", "quinn", - "rustls 0.23.10", + "rustls 0.23.11", "rustls-native-certs 0.7.1", "rustls-pemfile 2.1.2", "rustls-pki-types", @@ -5738,9 +5738,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.10" +version = "0.23.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05cff451f60db80f490f3c182b77c35260baace73209e9cdbbe526bfe3a4d402" +checksum = "4828ea528154ae444e5a642dbb7d5623354030dc9822b83fd9bb79683c7399d0" dependencies = [ "once_cell", "ring", @@ -5944,9 +5944,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] @@ -5962,13 +5962,13 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.203" +version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -5990,7 +5990,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -6266,9 +6266,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.68" +version = "2.0.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" +checksum = "b146dcf730474b4bcd16c311627b31ede9ab149045db4d6088b3becaea046462" dependencies = [ "proc-macro2", "quote", @@ -6386,22 +6386,22 @@ checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" [[package]] name = "thiserror" -version = "1.0.61" +version = "1.0.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" +checksum = "f2675633b1499176c2dff06b0856a27976a8f9d436737b4cf4f312d4d91d8bbb" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.61" +version = "1.0.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" +checksum = "d20468752b09f49e909e55a5d338caa8bedf615594e9d80bc4c565d30faf798c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -6458,9 +6458,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.6.1" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c55115c6fbe2d2bef26eb09ad74bde02d8255476fc0c7b515ef09fbb35742d82" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" dependencies = [ "tinyvec_macros", ] @@ -6498,7 +6498,7 @@ checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -6538,7 +6538,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" dependencies = [ - "rustls 0.23.10", + "rustls 0.23.11", "rustls-pki-types", "tokio", ] @@ -6612,7 +6612,7 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.14", + "toml_edit 0.22.15", ] [[package]] @@ -6659,9 +6659,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.14" +version = "0.22.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" +checksum = "d59a3a72298453f564e2b111fa896f8d07fabb36f51f06d7e875fc5e0b5a3ef1" dependencies = [ "indexmap 2.2.6", "serde", @@ -6724,7 +6724,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -6919,9 +6919,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "uuid" -version = "1.9.1" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5de17fd2f7da591098415cff336e12965a28061ddace43b59cb3c430179c9439" +checksum = "81dfa00651efa65069b0b6b651f4aaa31ba9e3c3ce0137aaad053604ee7e0314" [[package]] name = "valuable" @@ -7014,7 +7014,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", "wasm-bindgen-shared", ] @@ -7048,7 +7048,7 @@ checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -7336,7 +7336,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] @@ -7356,32 +7356,32 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.68", + "syn 2.0.71", ] [[package]] name = "zstd" -version = "0.13.1" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d789b1514203a1120ad2429eae43a7bd32b90976a7bb8a05f7ec02fa88cc23a" +checksum = "fcf2b778a664581e31e389454a7072dab1647606d44f7feea22cd5abb9c9f3f9" dependencies = [ "zstd-safe", ] [[package]] name = "zstd-safe" -version = "7.1.0" +version = "7.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cd99b45c6bc03a018c8b8a86025678c87e55526064e38f9df301989dce7ec0a" +checksum = "fa556e971e7b568dc775c136fc9de8c779b1c2fc3a63defaafadffdbd3181afa" dependencies = [ "zstd-sys", ] [[package]] name = "zstd-sys" -version = "2.0.11+zstd.1.5.6" +version = "2.0.12+zstd.1.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75652c55c0b6f3e6f12eb786fe1bc960396bf05a1eb3bf1f3691c3610ac2e6d4" +checksum = "0a4e40c320c3cb459d9a9ff6de98cff88f4751ee9275d140e2be94a2b74e4c13" dependencies = [ "cc", "pkg-config", diff --git a/src/cfg.rs b/src/cfg.rs index e1070fb..00d2ad4 100644 --- a/src/cfg.rs +++ b/src/cfg.rs @@ -30,6 +30,10 @@ pub struct CommandLineArgs { #[arg(short = 'n', long)] celestia_namespace_id: Option, + // Height to start searching the DA layer for SNARKs on + #[arg(short = 's', long)] + celestia_start_height: Option, + /// Duration between epochs in seconds #[arg(short, long)] epoch_time: Option, @@ -43,7 +47,7 @@ pub struct CommandLineArgs { port: Option, #[arg(long)] - public_key: Option, + verifying_key: Option, #[arg(long)] config_path: Option, @@ -63,7 +67,7 @@ pub struct Config { pub da_layer: Option, pub redis_config: Option, pub epoch_time: Option, - pub public_key: Option, + pub verifying_key: Option, } #[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)] @@ -106,6 +110,7 @@ impl Default for RedisConfig { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct CelestiaConfig { pub connection_string: String, + pub start_height: u64, pub namespace_id: String, } @@ -113,6 +118,7 @@ impl Default for CelestiaConfig { fn default() -> Self { CelestiaConfig { connection_string: "ws://localhost:26658".to_string(), + start_height: 0, namespace_id: "00000000000000de1008".to_string(), } } @@ -127,7 +133,7 @@ impl Default for Config { celestia_config: Some(CelestiaConfig::default()), redis_config: Some(RedisConfig::default()), epoch_time: Some(60), - public_key: None, + verifying_key: None, } } } @@ -169,7 +175,7 @@ pub fn load_config(args: CommandLineArgs) -> Result .or(default_config.celestia_config), da_layer: file_config.da_layer.or(default_config.da_layer), epoch_time: file_config.epoch_time.or(default_config.epoch_time), - public_key: file_config.public_key.or(default_config.public_key), + verifying_key: file_config.verifying_key.or(default_config.verifying_key), }; Ok(Config { @@ -193,13 +199,16 @@ pub fn load_config(args: CommandLineArgs) -> Result .unwrap() .connection_string, ), + start_height: args + .celestia_start_height + .unwrap_or(merged_config.celestia_config.clone().unwrap().start_height), namespace_id: args .celestia_namespace_id .unwrap_or(merged_config.celestia_config.unwrap().namespace_id), }), da_layer: merged_config.da_layer, epoch_time: Some(args.epoch_time.unwrap_or(merged_config.epoch_time.unwrap())), - public_key: args.public_key.or(merged_config.public_key), + verifying_key: args.verifying_key.or(merged_config.verifying_key), }) } diff --git a/src/main.rs b/src/main.rs index 1770946..ebbeece 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,9 +34,12 @@ async fn main() -> std::io::Result<()> { dotenv().ok(); let da = initialize_da_layer(&config).await; - let node: Arc = match args.command { - Commands::LightClient {} => Arc::new(LightClient::new(da, config.public_key)), + Commands::LightClient {} => Arc::new(LightClient::new( + da, + config.celestia_config.unwrap(), + config.verifying_key, + )), Commands::Sequencer {} => Arc::new(Sequencer::new( Arc::new( RedisConnections::new(&config.clone().redis_config.unwrap()) diff --git a/src/node_types.rs b/src/node_types.rs index 41ada7e..78d83f0 100644 --- a/src/node_types.rs +++ b/src/node_types.rs @@ -1,4 +1,5 @@ use crate::{ + cfg::CelestiaConfig, consts::{CHANNEL_BUFFER_SIZE, DA_RETRY_COUNT, DA_RETRY_INTERVAL}, error::{DataAvailabilityError, DeimosResult}, }; @@ -48,7 +49,9 @@ pub struct Sequencer { pub struct LightClient { pub da: Arc, - pub sequencer_public_key: Option, + // verifying_key is the [`VerifyingKey`] used to verify epochs from the prover/sequencer + pub verifying_key: Option, + start_height: u64, } #[async_trait] @@ -89,11 +92,11 @@ impl NodeType for LightClient { self.da.start().await.unwrap(); info!("starting main light client loop"); - // todo: persist current_position in datastore - // also: have initial starting position be configurable - let handle = spawn(async move { - let mut current_position = 0; + // todo: persist current_position in datastore + let start_height = self.start_height; + spawn(async move { + let mut current_position = start_height; let mut ticker = interval(Duration::from_secs(1)); loop { // target is updated when a new header is received @@ -112,10 +115,10 @@ impl NodeType for LightClient { let verifying_key = deserialize_custom_to_verifying_key(&epoch_json.verifying_key) .unwrap(); - if self.sequencer_public_key.is_some() { + if self.verifying_key.is_some() { if verify_signature( &epoch_json.clone(), - self.sequencer_public_key.clone(), + self.verifying_key.clone(), ) .is_ok() { @@ -137,34 +140,38 @@ impl NodeType for LightClient { verifying_key, ) { Ok(_) => (), - Err(err) => panic!("Failed to validate epoch: {:?}", err), + Err(err) => panic!("failed to validate epoch: {:?}", err), } } info!("light client: got epochs at height {}", i + 1); } - Err(e) => debug!("light client: getting epoch: {}", e), + Err(e) => { + debug!("light client: getting epoch: {}", e) + } }; } ticker.tick().await; // only for testing purposes current_position = target; // Update the current position to the latest target } - }); - - handle - .await - .map_err(|_| GeneralError::WebserverError.into()) + }) + .await + .map_err(|_| { + DataAvailabilityError::InitializationError("failed to initialize".to_string()).into() + }) } } impl LightClient { pub fn new( da: Arc, + cfg: CelestiaConfig, sequencer_pub_key: Option, ) -> LightClient { LightClient { da, - sequencer_public_key: sequencer_pub_key, + verifying_key: sequencer_pub_key, + start_height: cfg.start_height, } } } @@ -291,13 +298,10 @@ impl Sequencer { BatchMerkleProofCircuit::new(&prev_commitment, ¤t_commitment, proofs)?; let (proof, verifying_key) = batch_circuit.create_and_verify_snark()?; - let signed_prev_commitment = self.key.sign(&prev_commitment.as_bytes()).to_string(); - let signed_current_commitment = self.key.sign(¤t_commitment.as_bytes()).to_string(); - let epoch_json = EpochJson { height: epoch, - prev_commitment: signed_prev_commitment, - current_commitment: signed_current_commitment, + prev_commitment: prev_commitment, + current_commitment: current_commitment, proof: serialize_proof(&proof), verifying_key: serialize_verifying_key_to_custom(&verifying_key), signature: None, diff --git a/src/storage.rs b/src/storage.rs index 99180fb..7473bd0 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -172,7 +172,8 @@ pub trait Database: Send + Sync { impl RedisConnections { pub fn new(cfg: &RedisConfig) -> Result> { - let try_client = Client::open(cfg.clone().connection_string)?; + let connection_string = cfg.connection_string.clone(); + let try_client = Client::open(connection_string.clone())?; let try_connection = try_client.get_connection(); if try_connection.is_err() { @@ -180,16 +181,17 @@ impl RedisConnections { let _child = Command::new("redis-server").spawn()?; + // TODO: fix this hack sleep(Duration::from_secs(5)); debug!("redis-server started"); } - let client = Client::open("redis://127.0.0.1/")?; - let derived_client = Client::open("redis://127.0.0.1/1")?; - let input_order = Client::open("redis://127.0.0.1/2")?; - let app_state = Client::open("redis://127.0.0.1/3")?; - let merkle_proofs = Client::open("redis://127.0.0.1/4")?; - let commitments = Client::open("redis://127.0.0.1/5")?; + let client = Client::open(connection_string.clone())?; + let derived_client = Client::open(connection_string.clone() + "1")?; + let input_order = Client::open(connection_string.clone() + "2")?; + let app_state = Client::open(connection_string.clone() + "3")?; + let merkle_proofs = Client::open(connection_string.clone() + "4")?; + let commitments = Client::open(connection_string.clone() + "5")?; Ok(RedisConnections { main_dict: Mutex::new(client.get_connection()?), diff --git a/src/utils.rs b/src/utils.rs index f2a0e66..6d567bc 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -133,9 +133,10 @@ pub fn validate_epoch( .into_iter() .collect(); - let scalars = scalars.map_err(|_| { + let scalars = scalars.map_err(|e| { DeimosError::General(GeneralError::ParsingError(format!( - "unable to parse public input parameters" + "unable to parse public input parameters: {}", + e.to_string() ))) })?; @@ -144,7 +145,7 @@ pub fn validate_epoch( .map_err(|e| DeimosError::Proof(ProofError::VerificationError(e.to_string())))?; debug!( - "validate_epoch: zkSNARK with groth16 random parameters for epoch between commitment {} and {} was successfully verified!", + "validate_epoch: zkSNARK with groth16 random parameters for epoch between commitment {} and {} was successfully verified", previous_commitment, current_commitment ); diff --git a/src/zk_snark/mod.rs b/src/zk_snark/mod.rs index b39ed57..120ce30 100644 --- a/src/zk_snark/mod.rs +++ b/src/zk_snark/mod.rs @@ -1,3 +1,5 @@ +use std::array::TryFromSliceError; + use crate::{ error::{DeimosError, GeneralError, ProofError}, storage::ChainEntry, @@ -304,14 +306,24 @@ pub struct BatchMerkleProofCircuit { } pub fn hex_to_scalar(hex_string: &str) -> Result { - let byte_array: [u8; 32] = hex::decode(hex_string) - .map_err(|_| { - GeneralError::DecodingError(format!("Failed to decode hex string: {}", hex_string)) - })? - .try_into() - .map_err(|_| { - GeneralError::ParsingError("Failed to parse hex string to byte array".to_string()) - })?; + let bytes = hex::decode(hex_string).map_err(|e| { + GeneralError::DecodingError(format!( + "failed to decode hex string {}: {}", + hex_string, + e.to_string() + )) + })?; + + if bytes.len() != 32 { + return Err(GeneralError::ParsingError(format!( + "failed to parse hex string to byte array: expected 32 bytes, got {} bytes", + bytes.len() + ))); + } + + let byte_array: [u8; 32] = bytes.try_into().map_err(|_| { + GeneralError::ParsingError(format!("failed to parse hex string to byte array")) + })?; let mut wide = [0u8; 64]; wide[..32].copy_from_slice(&byte_array); // Fill 0s in front of it, then the value remains the same