From 9ee17afcea74be973e231ec2c7e860f0ed148da3 Mon Sep 17 00:00:00 2001 From: irriden Date: Tue, 23 Jan 2024 22:12:37 +0000 Subject: [PATCH] signer: latency optimizations serialize mutation values using rmp-serde remove unneeded data stored in channel state remove unnecessary lss roundtrip after SignPenaltyToUs message tweak mqtt settings --- sphinx-key/Cargo.lock | 31 ++++++++++++++++--------------- sphinx-key/Cargo.toml | 6 +++--- sphinx-key/sdkconfig.defaults | 6 +++++- sphinx-key/src/conn/mqtt.rs | 3 +-- sphinx-key/src/core/events.rs | 16 +++++----------- 5 files changed, 30 insertions(+), 32 deletions(-) diff --git a/sphinx-key/Cargo.lock b/sphinx-key/Cargo.lock index 661fea7..f1a8f61 100644 --- a/sphinx-key/Cargo.lock +++ b/sphinx-key/Cargo.lock @@ -237,7 +237,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bolt-derive" version = "0.2.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=a28153e0f1b6598e6538d32b3c5bfb9c7657d565#a28153e0f1b6598e6538d32b3c5bfb9c7657d565" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=753024b51bf681695490eff76888a4d63241fd7c#753024b51bf681695490eff76888a4d63241fd7c" dependencies = [ "proc-macro2", "quote", @@ -275,9 +275,9 @@ checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1" [[package]] name = "byteorder" -version = "1.4.3" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "camino" @@ -1156,7 +1156,7 @@ checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "lss-connector" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=b461de86bdd8945254f38410902908f4c798da57#b461de86bdd8945254f38410902908f4c798da57" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=437728607bce0288908fc8979936a850e52560b7#437728607bce0288908fc8979936a850e52560b7" dependencies = [ "anyhow", "log", @@ -1501,9 +1501,9 @@ dependencies = [ [[package]] name = "rmp-serde" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5b13be192e0220b8afb7222aa5813cb62cc269ebb5cac346ca6487681d2913e" +checksum = "bffea85eea980d8a74453e5d02a8d93028f3c34725de143085a844ebe953258a" dependencies = [ "byteorder", "rmp", @@ -1513,7 +1513,7 @@ dependencies = [ [[package]] name = "rmp-utils" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=b461de86bdd8945254f38410902908f4c798da57#b461de86bdd8945254f38410902908f4c798da57" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=437728607bce0288908fc8979936a850e52560b7#437728607bce0288908fc8979936a850e52560b7" dependencies = [ "anyhow", "log", @@ -1703,7 +1703,7 @@ checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" [[package]] name = "sphinx-auther" version = "0.1.12" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=b461de86bdd8945254f38410902908f4c798da57#b461de86bdd8945254f38410902908f4c798da57" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=437728607bce0288908fc8979936a850e52560b7#437728607bce0288908fc8979936a850e52560b7" dependencies = [ "anyhow", "base64", @@ -1715,7 +1715,7 @@ dependencies = [ [[package]] name = "sphinx-crypter" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=b461de86bdd8945254f38410902908f4c798da57#b461de86bdd8945254f38410902908f4c798da57" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=437728607bce0288908fc8979936a850e52560b7#437728607bce0288908fc8979936a850e52560b7" dependencies = [ "anyhow", "chacha20poly1305", @@ -1726,7 +1726,7 @@ dependencies = [ [[package]] name = "sphinx-glyph" version = "0.1.2" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=b461de86bdd8945254f38410902908f4c798da57#b461de86bdd8945254f38410902908f4c798da57" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=437728607bce0288908fc8979936a850e52560b7#437728607bce0288908fc8979936a850e52560b7" dependencies = [ "anyhow", "hex", @@ -1758,7 +1758,7 @@ dependencies = [ [[package]] name = "sphinx-signer" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=b461de86bdd8945254f38410902908f4c798da57#b461de86bdd8945254f38410902908f4c798da57" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=437728607bce0288908fc8979936a850e52560b7#437728607bce0288908fc8979936a850e52560b7" dependencies = [ "anyhow", "bip39", @@ -1767,6 +1767,7 @@ dependencies = [ "log", "lss-connector", "rand", + "rmp-serde", "serde", "sphinx-glyph", "thiserror", @@ -2009,7 +2010,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "vls-core" version = "0.11.0-rc.1" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=a28153e0f1b6598e6538d32b3c5bfb9c7657d565#a28153e0f1b6598e6538d32b3c5bfb9c7657d565" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=753024b51bf681695490eff76888a4d63241fd7c#753024b51bf681695490eff76888a4d63241fd7c" dependencies = [ "anyhow", "bitcoin", @@ -2033,7 +2034,7 @@ dependencies = [ [[package]] name = "vls-persist" version = "0.11.0-rc.1" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=a28153e0f1b6598e6538d32b3c5bfb9c7657d565#a28153e0f1b6598e6538d32b3c5bfb9c7657d565" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=753024b51bf681695490eff76888a4d63241fd7c#753024b51bf681695490eff76888a4d63241fd7c" dependencies = [ "hex", "log", @@ -2046,7 +2047,7 @@ dependencies = [ [[package]] name = "vls-protocol" version = "0.11.0-rc.1" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=a28153e0f1b6598e6538d32b3c5bfb9c7657d565#a28153e0f1b6598e6538d32b3c5bfb9c7657d565" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=753024b51bf681695490eff76888a4d63241fd7c#753024b51bf681695490eff76888a4d63241fd7c" dependencies = [ "as-any", "bitcoin-consensus-derive", @@ -2060,7 +2061,7 @@ dependencies = [ [[package]] name = "vls-protocol-signer" version = "0.11.0-rc.1" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=a28153e0f1b6598e6538d32b3c5bfb9c7657d565#a28153e0f1b6598e6538d32b3c5bfb9c7657d565" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=753024b51bf681695490eff76888a4d63241fd7c#753024b51bf681695490eff76888a4d63241fd7c" dependencies = [ "bit-vec", "log", diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index 24744c2..871e9d4 100644 --- a/sphinx-key/Cargo.toml +++ b/sphinx-key/Cargo.toml @@ -25,9 +25,9 @@ serde_json = { version = "1.0.81", default-features = false } serde_urlencoded = "0.7.1" # sphinx-rs -lss-connector = { git = "https://github.com/stakwork/sphinx-rs.git", default-features = false, rev = "b461de86bdd8945254f38410902908f4c798da57" } -sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git", rev = "b461de86bdd8945254f38410902908f4c798da57" } -sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true, rev = "b461de86bdd8945254f38410902908f4c798da57" } +lss-connector = { git = "https://github.com/stakwork/sphinx-rs.git", default-features = false, rev = "437728607bce0288908fc8979936a850e52560b7" } +sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git", rev = "437728607bce0288908fc8979936a850e52560b7" } +sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true, rev = "437728607bce0288908fc8979936a850e52560b7" } # local # lss-connector = { path = "../../sphinx-rs/lss-connector", default-features = false } # sphinx-crypter = { path = "../../sphinx-rs/crypter" } diff --git a/sphinx-key/sdkconfig.defaults b/sphinx-key/sdkconfig.defaults index f047135..fc9f4b3 100644 --- a/sphinx-key/sdkconfig.defaults +++ b/sphinx-key/sdkconfig.defaults @@ -1,5 +1,5 @@ # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) -CONFIG_ESP_MAIN_TASK_STACK_SIZE=32000 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=40000 CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=2500 # CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y CONFIG_FATFS_LFN_STACK=y @@ -21,6 +21,10 @@ CONFIG_MBEDTLS_DYNAMIC_FREE_CA_CERT=y CONFIG_MBEDTLS_HARDWARE_AES=n +CONFIG_LWIP_TCP_SND_BUF_DEFAULT=65535 +CONFIG_LWIP_TCP_WND_DEFAULT=65535 +CONFIG_LWIP_TCP_MSS=1460 + # All these settings below sum to a 60KB reduction in bin size CONFIG_MBEDTLS_HAVE_TIME=n CONFIG_MBEDTLS_ECDSA_DETERMINISTIC=n diff --git a/sphinx-key/src/conn/mqtt.rs b/sphinx-key/src/conn/mqtt.rs index e9d3dc9..b450684 100644 --- a/sphinx-key/src/conn/mqtt.rs +++ b/sphinx-key/src/conn/mqtt.rs @@ -23,8 +23,7 @@ pub fn make_client( let mut conf = MqttClientConfiguration { client_id: Some(&client_id), - //buffer_size: 1024, - //task_stack: 12288, + out_buffer_size: 2 * 1024, username: Some(username), password: Some(password), ..Default::default() diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index aa72537..da01d7e 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -4,15 +4,14 @@ use crate::ota::{update_sphinx_key, validate_ota_message}; use crate::status::Status; use crate::bitcoin::Network; +use crate::conn::mqtt::MsgBytes; use glyph::control::{Config, ControlMessage, ControlResponse, Controller, Policy, Velocity}; use glyph::error::Error as GlyphError; use glyph::ser::{serialize_controlresponse, ByteBuf}; use glyph::topics; use lss_connector::secp256k1::PublicKey; use sphinx_signer::approver::SphinxApprover; -//use sphinx_signer::lightning_signer::persist::DummyPersister; -use crate::conn::mqtt::MsgBytes; -use sphinx_signer::kvv::{CloudKVVStore, FsKVVStore}; +use sphinx_signer::kvv::{CloudKVVStore, FsKVVStore, KVVPersister, RmpFormat}; use sphinx_signer::lightning_signer::persist::Persist; use sphinx_signer::root::VlsHandlerError; use sphinx_signer::sphinx_glyph as glyph; @@ -86,8 +85,8 @@ pub fn make_event_loop( } } - let kvv_store = FsKVVStore::new(ROOT_STORE, *signer_id, None).0; - let fs_persister = CloudKVVStore::new(kvv_store); + let kvv_store = FsKVVStore::new(ROOT_STORE, *signer_id, None); + let fs_persister = KVVPersister(CloudKVVStore::new(kvv_store), RmpFormat); let _ = fs_persister.enter(); let initial_allowlist = match fs_persister.get_nodes() { @@ -143,7 +142,6 @@ pub fn make_event_loop( let mut expected_sequence = None; let mut current_status = Status::ConnectingToMqtt; while let Ok(event) = rx.recv() { - log::info!("new event loop!"); check_memory(); match event { Event::Connected => { @@ -161,7 +159,6 @@ pub fn make_event_loop( Event::VlsMessage(msg_bytes) => { current_status = update_led(current_status, Status::Signing, &led_tx); let state1 = approver.control().get_state(); - //log::info!("FULL MSG {:?}", &msg_bytes); match sphinx_signer::root::handle_with_lss( &root_handler, &lss_signer, @@ -182,7 +179,6 @@ pub fn make_event_loop( log::error!("LOCAL COMMIT ERROR! {:?}", e); unsafe { esp_idf_svc::sys::esp_restart() }; } - restart_esp_if_memory_low(); } expected_sequence = Some(sequence + 1); } @@ -220,13 +216,11 @@ pub fn make_event_loop( msgs = None; mqtt_pub(&mut mqtt, &client_id, &ret_topic, &bytes); if ret_topic == topics::VLS_RES { - log::info!("HMACs matched! commit now..."); // and commit if let Err(e) = root_handler.node().get_persister().commit() { log::error!("LOCAL COMMIT ERROR AFTER LSS! {:?}", e); unsafe { esp_idf_svc::sys::esp_restart() }; } - restart_esp_if_memory_low(); } if ret_topic == topics::LSS_CONFLICT_RES { log::error!("LSS PUT CONFLICT! RESTART..."); @@ -271,7 +265,7 @@ fn update_led(current: Status, new: Status, led_tx: &mpsc::Sender) -> St } } -pub(crate) fn restart_esp_if_memory_low() { +pub(crate) fn _restart_esp_if_memory_low() { unsafe { let size = esp_idf_svc::sys::heap_caps_get_free_size(4); let block = esp_idf_svc::sys::heap_caps_get_largest_free_block(4);