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
This commit is contained in:
irriden
2024-01-23 22:12:37 +00:00
parent 6f51641eb8
commit 9ee17afcea
5 changed files with 30 additions and 32 deletions

31
sphinx-key/Cargo.lock generated
View File

@@ -237,7 +237,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]] [[package]]
name = "bolt-derive" name = "bolt-derive"
version = "0.2.0" 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 = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@@ -275,9 +275,9 @@ checksum = "a3e2c3daef883ecc1b5d58c15adae93470a91d425f3532ba1695849656af3fc1"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.4.3" version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "camino" name = "camino"
@@ -1156,7 +1156,7 @@ checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4"
[[package]] [[package]]
name = "lss-connector" name = "lss-connector"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"log", "log",
@@ -1501,9 +1501,9 @@ dependencies = [
[[package]] [[package]]
name = "rmp-serde" name = "rmp-serde"
version = "1.1.1" version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5b13be192e0220b8afb7222aa5813cb62cc269ebb5cac346ca6487681d2913e" checksum = "bffea85eea980d8a74453e5d02a8d93028f3c34725de143085a844ebe953258a"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"rmp", "rmp",
@@ -1513,7 +1513,7 @@ dependencies = [
[[package]] [[package]]
name = "rmp-utils" name = "rmp-utils"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"log", "log",
@@ -1703,7 +1703,7 @@ checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
[[package]] [[package]]
name = "sphinx-auther" name = "sphinx-auther"
version = "0.1.12" 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 = [ dependencies = [
"anyhow", "anyhow",
"base64", "base64",
@@ -1715,7 +1715,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-crypter" name = "sphinx-crypter"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"chacha20poly1305", "chacha20poly1305",
@@ -1726,7 +1726,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-glyph" name = "sphinx-glyph"
version = "0.1.2" 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 = [ dependencies = [
"anyhow", "anyhow",
"hex", "hex",
@@ -1758,7 +1758,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-signer" name = "sphinx-signer"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"bip39", "bip39",
@@ -1767,6 +1767,7 @@ dependencies = [
"log", "log",
"lss-connector", "lss-connector",
"rand", "rand",
"rmp-serde",
"serde", "serde",
"sphinx-glyph", "sphinx-glyph",
"thiserror", "thiserror",
@@ -2009,7 +2010,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]] [[package]]
name = "vls-core" name = "vls-core"
version = "0.11.0-rc.1" 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 = [ dependencies = [
"anyhow", "anyhow",
"bitcoin", "bitcoin",
@@ -2033,7 +2034,7 @@ dependencies = [
[[package]] [[package]]
name = "vls-persist" name = "vls-persist"
version = "0.11.0-rc.1" 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 = [ dependencies = [
"hex", "hex",
"log", "log",
@@ -2046,7 +2047,7 @@ dependencies = [
[[package]] [[package]]
name = "vls-protocol" name = "vls-protocol"
version = "0.11.0-rc.1" 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 = [ dependencies = [
"as-any", "as-any",
"bitcoin-consensus-derive", "bitcoin-consensus-derive",
@@ -2060,7 +2061,7 @@ dependencies = [
[[package]] [[package]]
name = "vls-protocol-signer" name = "vls-protocol-signer"
version = "0.11.0-rc.1" 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 = [ dependencies = [
"bit-vec", "bit-vec",
"log", "log",

View File

@@ -25,9 +25,9 @@ serde_json = { version = "1.0.81", default-features = false }
serde_urlencoded = "0.7.1" serde_urlencoded = "0.7.1"
# sphinx-rs # sphinx-rs
lss-connector = { git = "https://github.com/stakwork/sphinx-rs.git", default-features = false, 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 = "b461de86bdd8945254f38410902908f4c798da57" } 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 = "b461de86bdd8945254f38410902908f4c798da57" } sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true, rev = "437728607bce0288908fc8979936a850e52560b7" }
# local # local
# lss-connector = { path = "../../sphinx-rs/lss-connector", default-features = false } # lss-connector = { path = "../../sphinx-rs/lss-connector", default-features = false }
# sphinx-crypter = { path = "../../sphinx-rs/crypter" } # sphinx-crypter = { path = "../../sphinx-rs/crypter" }

View File

@@ -1,5 +1,5 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) # 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_PTHREAD_TASK_STACK_SIZE_DEFAULT=2500
# CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y # CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y
CONFIG_FATFS_LFN_STACK=y CONFIG_FATFS_LFN_STACK=y
@@ -21,6 +21,10 @@ CONFIG_MBEDTLS_DYNAMIC_FREE_CA_CERT=y
CONFIG_MBEDTLS_HARDWARE_AES=n 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 # All these settings below sum to a 60KB reduction in bin size
CONFIG_MBEDTLS_HAVE_TIME=n CONFIG_MBEDTLS_HAVE_TIME=n
CONFIG_MBEDTLS_ECDSA_DETERMINISTIC=n CONFIG_MBEDTLS_ECDSA_DETERMINISTIC=n

View File

@@ -23,8 +23,7 @@ pub fn make_client(
let mut conf = MqttClientConfiguration { let mut conf = MqttClientConfiguration {
client_id: Some(&client_id), client_id: Some(&client_id),
//buffer_size: 1024, out_buffer_size: 2 * 1024,
//task_stack: 12288,
username: Some(username), username: Some(username),
password: Some(password), password: Some(password),
..Default::default() ..Default::default()

View File

@@ -4,15 +4,14 @@ use crate::ota::{update_sphinx_key, validate_ota_message};
use crate::status::Status; use crate::status::Status;
use crate::bitcoin::Network; use crate::bitcoin::Network;
use crate::conn::mqtt::MsgBytes;
use glyph::control::{Config, ControlMessage, ControlResponse, Controller, Policy, Velocity}; use glyph::control::{Config, ControlMessage, ControlResponse, Controller, Policy, Velocity};
use glyph::error::Error as GlyphError; use glyph::error::Error as GlyphError;
use glyph::ser::{serialize_controlresponse, ByteBuf}; use glyph::ser::{serialize_controlresponse, ByteBuf};
use glyph::topics; use glyph::topics;
use lss_connector::secp256k1::PublicKey; use lss_connector::secp256k1::PublicKey;
use sphinx_signer::approver::SphinxApprover; use sphinx_signer::approver::SphinxApprover;
//use sphinx_signer::lightning_signer::persist::DummyPersister; use sphinx_signer::kvv::{CloudKVVStore, FsKVVStore, KVVPersister, RmpFormat};
use crate::conn::mqtt::MsgBytes;
use sphinx_signer::kvv::{CloudKVVStore, FsKVVStore};
use sphinx_signer::lightning_signer::persist::Persist; use sphinx_signer::lightning_signer::persist::Persist;
use sphinx_signer::root::VlsHandlerError; use sphinx_signer::root::VlsHandlerError;
use sphinx_signer::sphinx_glyph as glyph; 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 kvv_store = FsKVVStore::new(ROOT_STORE, *signer_id, None);
let fs_persister = CloudKVVStore::new(kvv_store); let fs_persister = KVVPersister(CloudKVVStore::new(kvv_store), RmpFormat);
let _ = fs_persister.enter(); let _ = fs_persister.enter();
let initial_allowlist = match fs_persister.get_nodes() { let initial_allowlist = match fs_persister.get_nodes() {
@@ -143,7 +142,6 @@ pub fn make_event_loop(
let mut expected_sequence = None; let mut expected_sequence = None;
let mut current_status = Status::ConnectingToMqtt; let mut current_status = Status::ConnectingToMqtt;
while let Ok(event) = rx.recv() { while let Ok(event) = rx.recv() {
log::info!("new event loop!");
check_memory(); check_memory();
match event { match event {
Event::Connected => { Event::Connected => {
@@ -161,7 +159,6 @@ pub fn make_event_loop(
Event::VlsMessage(msg_bytes) => { Event::VlsMessage(msg_bytes) => {
current_status = update_led(current_status, Status::Signing, &led_tx); current_status = update_led(current_status, Status::Signing, &led_tx);
let state1 = approver.control().get_state(); let state1 = approver.control().get_state();
//log::info!("FULL MSG {:?}", &msg_bytes);
match sphinx_signer::root::handle_with_lss( match sphinx_signer::root::handle_with_lss(
&root_handler, &root_handler,
&lss_signer, &lss_signer,
@@ -182,7 +179,6 @@ pub fn make_event_loop(
log::error!("LOCAL COMMIT ERROR! {:?}", e); log::error!("LOCAL COMMIT ERROR! {:?}", e);
unsafe { esp_idf_svc::sys::esp_restart() }; unsafe { esp_idf_svc::sys::esp_restart() };
} }
restart_esp_if_memory_low();
} }
expected_sequence = Some(sequence + 1); expected_sequence = Some(sequence + 1);
} }
@@ -220,13 +216,11 @@ pub fn make_event_loop(
msgs = None; msgs = None;
mqtt_pub(&mut mqtt, &client_id, &ret_topic, &bytes); mqtt_pub(&mut mqtt, &client_id, &ret_topic, &bytes);
if ret_topic == topics::VLS_RES { if ret_topic == topics::VLS_RES {
log::info!("HMACs matched! commit now...");
// and commit // and commit
if let Err(e) = root_handler.node().get_persister().commit() { if let Err(e) = root_handler.node().get_persister().commit() {
log::error!("LOCAL COMMIT ERROR AFTER LSS! {:?}", e); log::error!("LOCAL COMMIT ERROR AFTER LSS! {:?}", e);
unsafe { esp_idf_svc::sys::esp_restart() }; unsafe { esp_idf_svc::sys::esp_restart() };
} }
restart_esp_if_memory_low();
} }
if ret_topic == topics::LSS_CONFLICT_RES { if ret_topic == topics::LSS_CONFLICT_RES {
log::error!("LSS PUT CONFLICT! RESTART..."); log::error!("LSS PUT CONFLICT! RESTART...");
@@ -271,7 +265,7 @@ fn update_led(current: Status, new: Status, led_tx: &mpsc::Sender<Status>) -> St
} }
} }
pub(crate) fn restart_esp_if_memory_low() { pub(crate) fn _restart_esp_if_memory_low() {
unsafe { unsafe {
let size = esp_idf_svc::sys::heap_caps_get_free_size(4); 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); let block = esp_idf_svc::sys::heap_caps_get_largest_free_block(4);