From 908c0b5186c090c93ca9f3141bb7fc1e33c228ac Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Wed, 15 Jun 2022 14:15:31 -0700 Subject: [PATCH 1/7] use esp idf 4.3.2 --- README.md | 6 +- signer/src/lib.rs | 9 +- sphinx-key/.cargo/config.toml | 4 +- sphinx-key/.gitignore | 4 + sphinx-key/Cargo.toml | 3 +- sphinx-key/config.html | 191 ---------------------------- sphinx-key/docs/README.md | 115 +++++++++++++++++ sphinx-key/hmq.html | 212 -------------------------------- sphinx-key/run.sh | 3 - sphinx-key/scripts/build.sh | 24 ++++ sphinx-key/scripts/flash.sh | 22 ++++ sphinx-key/scripts/run-wokwi.sh | 36 ++++++ sphinx-key/sdkconfig.defaults | 4 +- sphinx-key/src/core/events.rs | 3 +- sphinx-key/src/test.rs | 9 ++ 15 files changed, 226 insertions(+), 419 deletions(-) create mode 100644 sphinx-key/.gitignore delete mode 100644 sphinx-key/config.html create mode 100644 sphinx-key/docs/README.md delete mode 100644 sphinx-key/hmq.html delete mode 100755 sphinx-key/run.sh create mode 100755 sphinx-key/scripts/build.sh create mode 100755 sphinx-key/scripts/flash.sh create mode 100755 sphinx-key/scripts/run-wokwi.sh create mode 100644 sphinx-key/src/test.rs diff --git a/README.md b/README.md index 82e37ac..06f57fc 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Find the path to your `riscv32-esp-elf-gcc` binary within the `.embuild` dir: ### flash release -`espflash target/riscv32imc-esp-espidf/release/sphinx-key` +`espflash target/riscv32imc-esp-espidf/release/sphinx-key --monitor` ### monitor @@ -88,4 +88,6 @@ espflash save-image esp32-c3 target/riscv32imc-esp-espidf/debug/sphinx-key ./asd espflash save-image esp32-c3 target/riscv32imc-esp-espidf/release/sphinx-key ./test-flash -espflash board-info \ No newline at end of file +espflash board-info + +export CC=$PWD/.embuild/espressif/tools/riscv32-esp-elf/esp-2021r2-8.4.0/riscv32-esp-elf/bin/riscv32-esp-elf-gcc \ No newline at end of file diff --git a/signer/src/lib.rs b/signer/src/lib.rs index 40adba9..e02af9b 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -3,11 +3,10 @@ use lightning_signer::Arc; use sphinx_key_parser::MsgDriver; use vls_protocol::msgs::{self, read_serial_request_header, write_serial_response_header, Message}; use vls_protocol::serde_bolt::WireString; -use vls_protocol_signer::lightning_signer; -use vls_protocol_signer::vls_protocol; - -pub use vls_protocol::model::PubKey; -pub use vls_protocol_signer::handler::{Handler, RootHandler}; +pub use vls_protocol_signer::lightning_signer; +pub use vls_protocol_signer::vls_protocol; +use vls_protocol::model::PubKey; +use vls_protocol_signer::handler::{Handler, RootHandler}; pub struct InitResponse { pub root_handler: RootHandler, diff --git a/sphinx-key/.cargo/config.toml b/sphinx-key/.cargo/config.toml index ff90964..3103fef 100644 --- a/sphinx-key/.cargo/config.toml +++ b/sphinx-key/.cargo/config.toml @@ -24,11 +24,11 @@ rustflags = ["-C", "default-linker-libraries"] [unstable] build-std = ["std", "panic_abort"] -#build-std-features = ["panic_immediate_abort"] # Required for older ESP-IDF versions without a realpath implementation +build-std-features = ["panic_immediate_abort"] # Required for older ESP-IDF versions without a realpath implementation [env] # Note: these variables are not used when using pio builder # Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF stable (v4.4) -ESP_IDF_VERSION = { value = "branch:release/v4.4" } +#ESP_IDF_VERSION = { value = "branch:release/v4.4" } # Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF master (mainline) #ESP_IDF_VERSION = { value = "master" } diff --git a/sphinx-key/.gitignore b/sphinx-key/.gitignore new file mode 100644 index 0000000..73a638b --- /dev/null +++ b/sphinx-key/.gitignore @@ -0,0 +1,4 @@ +/.vscode +/.embuild +/target +/Cargo.lock diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index 181cdf6..c2f4b95 100644 --- a/sphinx-key/Cargo.toml +++ b/sphinx-key/Cargo.toml @@ -19,8 +19,8 @@ pio = ["esp-idf-sys/pio"] pingpong = [] [dependencies] -sphinx-key-signer = { path = "../signer", optional = true } esp-idf-sys = { version = "0.31.5", features = ["binstart"] } +sphinx-key-signer = { path = "../signer", optional = true } embedded-svc = { version = "0.21.2" } esp-idf-svc = "0.41" esp-idf-hal = "0.37" @@ -39,3 +39,4 @@ anyhow = "1" [[bin]] name = "clear" path = "src/clear.rs" + diff --git a/sphinx-key/config.html b/sphinx-key/config.html deleted file mode 100644 index 07b0daa..0000000 --- a/sphinx-key/config.html +++ /dev/null @@ -1,191 +0,0 @@ - - - - - - - - - Sphinx Key - - - - - - - - - -

- Enter your WiFi credentials and MQTT Broker to connect you Sphinx Key -

- - - - - - - - - -

- - - - \ No newline at end of file diff --git a/sphinx-key/docs/README.md b/sphinx-key/docs/README.md new file mode 100644 index 0000000..c97a7de --- /dev/null +++ b/sphinx-key/docs/README.md @@ -0,0 +1,115 @@ +# sphinx_key + +## Dev Containers +This repository offers Dev Containers supports for: +- [Gitpod](https://gitpod.io/) + - ["Open in Gitpod" button](https://www.gitpod.io/docs/getting-started#open-in-gitpod-button) +- [VS Code Dev Containers](https://code.visualstudio.com/docs/remote/containers#_quick-start-open-an-existing-folder-in-a-container) +- [GitHub Codespaces](https://docs.github.com/en/codespaces/developing-in-codespaces/creating-a-codespace) +> **Note** +> +> In order to use Gitpod the project needs to be published in a GitLab, GitHub, +> or Bitbucket repository. +> +> In [order to use GitHub Codespaces](https://github.com/features/codespaces#faq) +> the project needs to be published in a GitHub repository and the user needs +> to be part of the Codespaces beta or have the project under an organization. + +If using VS Code or GitHub Codespaces, you can pull the image instead of building it +from the Dockerfile by selecting the `image` property instead of `build` in +`.devcontainer/devcontainer.json`. + +When using Dev Containers, some tooling to facilitate building, flashing and +simulating in Wokwi is also added. +### Build +- Terminal approach: + + ``` + scripts/build.sh [debug | release] + ``` + > If no argument is passed, `release` will be used as default + + +- UI approach: + + The default build task is already set to build the project, and it can be used + in VS Code and Gitpod: + - From the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) (`Ctrl-Shift-P` or `Cmd-Shift-P`) run the `Tasks: Run Build Task` command. + - `Terminal`-> `Run Build Task` in the menu. + - With `Ctrl-Shift-B` or `Cmd-Shift-B`. + - From the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) (`Ctrl-Shift-P` or `Cmd-Shift-P`) run the `Tasks: Run Task` command and + select `Build`. + - From UI: Press `Build` on the left side of the Status Bar. + +### Flash + +> **Note** +> +> When using GitHub Codespaces, we need to make the ports +> public, [see instructions](https://docs.github.com/en/codespaces/developing-in-codespaces/forwarding-ports-in-your-codespace#sharing-a-port). + +- Terminal approach: + - Using `flash.sh` script: + + ``` + scripts/flash.sh [debug | release] + ``` + > If no argument is passed, `release` will be used as default + +- UI approach: + - From the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) (`Ctrl-Shift-P` or `Cmd-Shift-P`) run the `Tasks: Run Task` command and + select `Build & Flash`. + - From UI: Press `Build & Flash` on the left side of the Status Bar. +- Any alternative flashing method from host machine. + + +### Wokwi Simulation +When using a custom Wokwi project, please change the `WOKWI_PROJECT_ID` in +`run-wokwi.sh`. If no project id is specified, a DevKit for esp32c3 will be +used. +> **Warning** +> +> ESP32-S3 is not available in Wokwi + +- Terminal approach: + + ``` + scripts/run-wokwi.sh [debug | release] + ``` + > If no argument is passed, `release` will be used as default + +- UI approach: + + The default test task is already set to build the project, and it can be used + in VS Code and Gitpod: + - From the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) (`Ctrl-Shift-P` or `Cmd-Shift-P`) run the `Tasks: Run Test Task` command + - With `Ctrl-Shift-,` or `Cmd-Shift-,` + > **Note** + > + > This Shortcut is not available in Gitpod by default. + - From the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) (`Ctrl-Shift-P` or `Cmd-Shift-P`) run the `Tasks: Run Task` command and + select `Build & Run Wokwi`. + - From UI: Press `Build & Run Wokwi` on the left side of the Status Bar. + +> **Warning** +> +> The simulation will pause if the browser tab is in the background.This may +> affect the execution, specially when debuging. + +#### Debuging with Wokwi + +Wokwi offers debugging with GDB. + +- Terminal approach: + ``` + $HOME/.espressif/tools/riscv32-esp-elf/esp-2021r2-patch3-8.4.0/riscv32-esp-elf/bin/riscv32-esp-elf-gdb target/riscv32imc-esp-espidf/debug/sphinx_key -ex "target remote localhost:9333" + ``` + + > [Wokwi Blog: List of common GDB commands for debugging.](https://blog.wokwi.com/gdb-avr-arduino-cheatsheet/?utm_source=urish&utm_medium=blog) +- UI approach: + 1. Run the Wokwi Simulation in `debug` profile + 2. Go to `Run and Debug` section of the IDE (`Ctrl-Shift-D or Cmd-Shift-D`) + 3. Start Debugging by pressing the Play Button or pressing `F5` + 4. Choose the proper user: + - `esp` when using VS Code or GitHub Codespaces + - `gitpod` when using Gitpod diff --git a/sphinx-key/hmq.html b/sphinx-key/hmq.html deleted file mode 100644 index fa5cc8b..0000000 --- a/sphinx-key/hmq.html +++ /dev/null @@ -1,212 +0,0 @@ - - - - - - - HMQ CLIENT - - - - - -
-
-
-

Connection - {{ connection.state }} - ({{ connection.error }}) - -

-
-

- -

-

- -

-

- -

-

- -

-
-
-
- -
-
-

Subscriptions

-
-

- -

-

- -

-

- -

-
-
    -
  • -
    -
    - QoS - {{ info.qos }} -
    -
    -
    -
    - Topic - {{ filter }} -
    -
    - -
  • -
-
-
- -
-
-

Messages

-
-

- -

-

- -

-

- -

-
-
    -
  • - {{ message.topic }}: - {{ message.payload }} -
  • -
-
-
-
- - - - - - - \ No newline at end of file diff --git a/sphinx-key/run.sh b/sphinx-key/run.sh deleted file mode 100755 index a2c7530..0000000 --- a/sphinx-key/run.sh +++ /dev/null @@ -1,3 +0,0 @@ -cargo build --features pingpong -espflash target/riscv32imc-esp-espidf/debug/sphinx-key -espmonitor /dev/tty.usbserial-1420 \ No newline at end of file diff --git a/sphinx-key/scripts/build.sh b/sphinx-key/scripts/build.sh new file mode 100755 index 0000000..a22e3c6 --- /dev/null +++ b/sphinx-key/scripts/build.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +# Gitpod and VsCode Codespaces tasks do not source the user environment +if [ "${USER}" == "gitpod" ]; then + which idf.py >/dev/null || { + source ~/export-esp.sh > /dev/null 2>&1 + } +elif [ "${CODESPACE_NAME}" != "" ]; then + which idf.py >/dev/null || { + source ~/export-esp.sh > /dev/null 2>&1 + } +fi + +case "$1" in + ""|"release") + cargo build --release + ;; + "debug") + cargo build + ;; + *) + echo "Wrong argument. Only \"debug\"/\"release\" arguments are supported" + exit 1;; +esac \ No newline at end of file diff --git a/sphinx-key/scripts/flash.sh b/sphinx-key/scripts/flash.sh new file mode 100755 index 0000000..9f74348 --- /dev/null +++ b/sphinx-key/scripts/flash.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -e + +BUILD_MODE="" +case "$1" in + ""|"release") + bash scripts/build.sh + BUILD_MODE="release" + ;; + "debug") + bash scripts/build.sh debug + BUILD_MODE="debug" + ;; + *) + echo "Wrong argument. Only \"debug\"/\"release\" arguments are supported" + exit 1;; +esac + +export ESP_ARCH=riscv32imc-esp-espidf + +web-flash --chip esp32c3 target/${ESP_ARCH}/${BUILD_MODE}/sphinx-key diff --git a/sphinx-key/scripts/run-wokwi.sh b/sphinx-key/scripts/run-wokwi.sh new file mode 100755 index 0000000..03f62b1 --- /dev/null +++ b/sphinx-key/scripts/run-wokwi.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +set -e + +BUILD_MODE="" +case "$1" in + ""|"release") + bash scripts/build.sh + BUILD_MODE="release" + ;; + "debug") + bash scripts/build.sh debug + BUILD_MODE="debug" + ;; + *) + echo "Wrong argument. Only \"debug\"/\"release\" arguments are supported" + exit 1;; +esac + +if [ "${USER}" == "gitpod" ];then + gp_url=$(gp url 9012) + echo "gp_url=${gp_url}" + export WOKWI_HOST=${gp_url:8} +elif [ "${CODESPACE_NAME}" != "" ];then + export WOKWI_HOST=${CODESPACE_NAME}-9012.githubpreview.dev +fi + +export ESP_ARCH=riscv32imc-esp-espidf + +# TODO: Update with your Wokwi Project +export WOKWI_PROJECT_ID="" +if [ "${WOKWI_PROJECT_ID}" == "" ]; then + wokwi-server --chip esp32c3 target/${ESP_ARCH}/${BUILD_MODE}/sphinx-key +else + wokwi-server --chip esp32c3 --id ${WOKWI_PROJECT_ID} target/${ESP_ARCH}/${BUILD_MODE}/sphinx-key +fi \ No newline at end of file diff --git a/sphinx-key/sdkconfig.defaults b/sphinx-key/sdkconfig.defaults index 0ac9af4..3ca3b5d 100644 --- a/sphinx-key/sdkconfig.defaults +++ b/sphinx-key/sdkconfig.defaults @@ -6,5 +6,5 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 #CONFIG_FREERTOS_HZ=1000 # Workaround for https://github.com/espressif/esp-idf/issues/7631 -CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n -CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n +#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n +#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index 607e997..30f5803 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -1,5 +1,6 @@ use crate::conn::mqtt::{RETURN_TOPIC, TOPIC, QOS}; -use sphinx_key_signer::{self, InitResponse, PubKey}; +use sphinx_key_signer::{self, InitResponse}; +use sphinx_key_signer::vls_protocol::model::PubKey; use std::sync::mpsc; use esp_idf_sys; diff --git a/sphinx-key/src/test.rs b/sphinx-key/src/test.rs new file mode 100644 index 0000000..2d3bfaf --- /dev/null +++ b/sphinx-key/src/test.rs @@ -0,0 +1,9 @@ +use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported + +fn main() { + // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, + // or else some patches to the runtime implemented by esp-idf-sys might not link properly. + esp_idf_sys::link_patches(); + + println!("Hello, world!"); +} From 209dda952a268008655b38fc9d35f16d48ebb061 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 16 Jun 2022 09:24:11 -0700 Subject: [PATCH 2/7] fix broker init on release --- README.md | 2 +- broker/src/main.rs | 10 ++++- signer/Cargo.toml | 4 +- signer/src/lib.rs | 3 ++ sphinx-key/src/conn/mqtt.rs | 83 ++++++++++++++++++----------------- sphinx-key/src/core/events.rs | 74 +++++++++++++++++++++---------- 6 files changed, 109 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 06f57fc..e651b3d 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Find the path to your `riscv32-esp-elf-gcc` binary within the `.embuild` dir: ### flash test -`espflash target/riscv32imc-esp-espidf/debug/sphinx-key` +`espflash target/riscv32imc-esp-espidf/debug/sphinx-key --monitor` ### build release diff --git a/broker/src/main.rs b/broker/src/main.rs index c6a2ac2..88a2df1 100644 --- a/broker/src/main.rs +++ b/broker/src/main.rs @@ -60,10 +60,18 @@ fn main() -> anyhow::Result<()> { run_test::run_test(); } else { let (tx, rx) = mpsc::channel(1000); - let (status_tx, _status_rx) = mpsc::channel(1000); + let (status_tx, mut status_rx) = mpsc::channel(1000); + log::info!("=> start broker"); let _runtime = start_broker(rx, status_tx, "sphinx-1"); + log::info!("=> wait for connected status"); + // wait for connection = true + let status = status_rx.blocking_recv().expect("couldnt receive"); + log::info!("=> connection status: {}", status); + assert_eq!(status, true, "expected connected = true"); // runtime.block_on(async { init::blocking_connect(tx.clone()); + log::info!("=====> sent seed!"); + // listen to reqs from CLN let conn = UnixConnection::new(parent_fd); let client = UnixClient::new(conn); diff --git a/signer/Cargo.toml b/signer/Cargo.toml index 7a74189..357ba93 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -6,11 +6,9 @@ edition = "2018" [dependencies] sphinx-key-parser = { path = "../parser" } +# vls-protocol-signer = { path = "../../../evanf/validating-lightning-signer/vls-protocol-signer", default-features = false, features = ["secp-lowmemory"] } vls-protocol-signer = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer", default-features = false, features = ["secp-lowmemory"] } anyhow = {version = "1", features = ["backtrace"]} log = "0.4" -[patch.crates-io] -# Low-memory version of secp256k1 with static precomputation -secp256k1 = { git = "https://github.com/devrandom/rust-secp256k1.git", rev = "4e745ebe7e4c9cd0a7e9c8d5c42e989522e52f71" } diff --git a/signer/src/lib.rs b/signer/src/lib.rs index e02af9b..2d3181a 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -26,8 +26,11 @@ pub fn init(bytes: Vec) -> anyhow::Result { .iter() .map(|s| from_wire_string(s)) .collect::>(); + log::info!("allowlist {:?}", allowlist); let seed = init.dev_seed.as_ref().map(|s| s.0).expect("no seed"); + log::info!("seed {:?}", seed); let root_handler = RootHandler::new(0, Some(seed), persister, allowlist); + log::info!("root_handler created"); let init_reply = root_handler .handle(Message::HsmdInit2(init)) .expect("handle init"); diff --git a/sphinx-key/src/conn/mqtt.rs b/sphinx-key/src/conn/mqtt.rs index 8c7a8bb..0ff559c 100644 --- a/sphinx-key/src/conn/mqtt.rs +++ b/sphinx-key/src/conn/mqtt.rs @@ -1,16 +1,16 @@ use crate::core::events::Event as CoreEvent; -use embedded_svc::mqtt::client::utils::ConnState; -use embedded_svc::mqtt::client::{Connection, MessageImpl, QoS, Event, Message as MqttMessage}; -use embedded_svc::mqtt::client::utils::Connection as MqttConnection; -use esp_idf_svc::mqtt::client::*; use anyhow::Result; -use log::*; -use std::thread; -use esp_idf_sys::{self}; -use esp_idf_sys::EspError; +use embedded_svc::mqtt::client::utils::ConnState; +use embedded_svc::mqtt::client::utils::Connection as MqttConnection; +use embedded_svc::mqtt::client::{Connection, Event, Message as MqttMessage, MessageImpl, QoS}; use esp_idf_hal::mutex::Condvar; -use std::sync::{mpsc}; +use esp_idf_svc::mqtt::client::*; +use esp_idf_sys::EspError; +use esp_idf_sys::{self}; +use log::*; +use std::sync::mpsc; +use std::thread; pub const TOPIC: &str = "sphinx"; pub const RETURN_TOPIC: &str = "sphinx-return"; @@ -18,10 +18,14 @@ pub const USERNAME: &str = "sphinx-key"; pub const PASSWORD: &str = "sphinx-key-pass"; pub const QOS: QoS = QoS::AtMostOnce; -pub fn make_client(broker: &str, client_id: &str) -> Result<( - EspMqttClient>, +pub fn make_client( + broker: &str, + client_id: &str, +) -> Result<( + EspMqttClient>, MqttConnection, )> { + log::info!("make_client with id {}", client_id); let conf = MqttClientConfiguration { client_id: Some(client_id), buffer_size: 2048, @@ -44,42 +48,41 @@ pub fn make_client(broker: &str, client_id: &str) -> Result<( pub fn start_listening( client: EspMqttClient>, - mut connection: MqttConnection, + mut connection: MqttConnection, tx: mpsc::Sender, ) -> Result>> { - // must start pumping before subscribe or publish will not work thread::spawn(move || { info!("MQTT Listening for messages"); loop { match connection.next() { - Some(msg) => { - match msg { - Err(e) => match e.to_string().as_ref() { - "ESP_FAIL" => { - error!("ESP_FAIL msg!"); - }, - _ => error!("Unknown error: {}", e), - }, - Ok(msg) => { - match msg { - Event::BeforeConnect => info!("RECEIVED BeforeConnect MESSAGE"), - Event::Connected(_flag) => { - info!("RECEIVED Connected MESSAGE"); - tx.send(CoreEvent::Connected).expect("couldnt send Event::Connected"); - }, - Event::Disconnected => { - warn!("RECEIVED Disconnected MESSAGE"); - tx.send(CoreEvent::Disconnected).expect("couldnt send Event::Disconnected"); - }, - Event::Subscribed(_mes_id) => info!("RECEIVED Subscribed MESSAGE"), - Event::Unsubscribed(_mes_id) => info!("RECEIVED Unsubscribed MESSAGE"), - Event::Published(_mes_id) => info!("RECEIVED Published MESSAGE"), - Event::Received(msg) => tx.send(CoreEvent::Message(msg.data().to_vec())).expect("couldnt send Event::Message"), - Event::Deleted(_mes_id) => info!("RECEIVED Deleted MESSAGE"), - } - }, - } + Some(msg) => match msg { + Err(e) => match e.to_string().as_ref() { + "ESP_FAIL" => { + error!("ESP_FAIL msg!"); + } + _ => error!("Unknown error: {}", e), + }, + Ok(msg) => match msg { + Event::BeforeConnect => info!("RECEIVED BeforeConnect MESSAGE"), + Event::Connected(_flag) => { + info!("RECEIVED Connected MESSAGE"); + tx.send(CoreEvent::Connected) + .expect("couldnt send Event::Connected"); + } + Event::Disconnected => { + warn!("RECEIVED Disconnected MESSAGE"); + tx.send(CoreEvent::Disconnected) + .expect("couldnt send Event::Disconnected"); + } + Event::Subscribed(_mes_id) => info!("RECEIVED Subscribed MESSAGE"), + Event::Unsubscribed(_mes_id) => info!("RECEIVED Unsubscribed MESSAGE"), + Event::Published(_mes_id) => info!("RECEIVED Published MESSAGE"), + Event::Received(msg) => tx + .send(CoreEvent::Message(msg.data().to_vec())) + .expect("couldnt send Event::Message"), + Event::Deleted(_mes_id) => info!("RECEIVED Deleted MESSAGE"), + }, }, None => break, } diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index 30f5803..252e1de 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -1,32 +1,49 @@ -use crate::conn::mqtt::{RETURN_TOPIC, TOPIC, QOS}; -use sphinx_key_signer::{self, InitResponse}; +use crate::conn::mqtt::{QOS, RETURN_TOPIC, TOPIC}; use sphinx_key_signer::vls_protocol::model::PubKey; +use sphinx_key_signer::{self, InitResponse}; use std::sync::mpsc; -use esp_idf_sys; -use embedded_svc::mqtt::client::Client; use embedded_svc::httpd::Result; use embedded_svc::mqtt::client::utils::ConnState; +use embedded_svc::mqtt::client::Client; use embedded_svc::mqtt::client::{MessageImpl, Publish}; use esp_idf_svc::mqtt::client::*; +use esp_idf_sys; use esp_idf_sys::EspError; pub enum Event { Connected, Disconnected, - Message(Vec) + Message(Vec), } #[cfg(not(feature = "pingpong"))] -pub fn make_event_loop(mut mqtt: EspMqttClient>, rx: mpsc::Receiver, do_log: bool) -> Result<()> { - +pub fn make_event_loop( + mut mqtt: EspMqttClient>, + rx: mpsc::Receiver, + do_log: bool, +) -> Result<()> { // initialize the RootHandler let root_handler = loop { if let Ok(event) = rx.recv() { - if let Event::Message(msg_bytes) = event { - let InitResponse { root_handler, init_reply } = sphinx_key_signer::init(msg_bytes).expect("failed to init signer"); - mqtt.publish(RETURN_TOPIC, QOS, false, init_reply).expect("could not publish init response"); - break root_handler + match event { + Event::Connected => { + log::info!("SUBSCRIBE to {}", TOPIC); + mqtt.subscribe(TOPIC, QOS) + .expect("could not MQTT subscribe"); + } + Event::Message(ref msg_bytes) => { + let InitResponse { + root_handler, + init_reply, + } = sphinx_key_signer::init(msg_bytes.clone()).expect("failed to init signer"); + mqtt.publish(RETURN_TOPIC, QOS, false, init_reply) + .expect("could not publish init response"); + break root_handler; + } + Event::Disconnected => { + log::info!("GOT an early Event::Disconnected msg!"); + } } } }; @@ -37,14 +54,22 @@ pub fn make_event_loop(mut mqtt: EspMqttClient> match event { Event::Connected => { log::info!("SUBSCRIBE TO {}", TOPIC); - mqtt.subscribe(TOPIC, QOS).expect("could not MQTT subscribe"); - }, + mqtt.subscribe(TOPIC, QOS) + .expect("could not MQTT subscribe"); + } Event::Message(ref msg_bytes) => { - let _ret = match sphinx_key_signer::handle(&root_handler, msg_bytes.clone(), dummy_peer.clone(), do_log) { - Ok(b) => mqtt.publish(RETURN_TOPIC, QOS, false, b).expect("could not publish init response"), + let _ret = match sphinx_key_signer::handle( + &root_handler, + msg_bytes.clone(), + dummy_peer.clone(), + do_log, + ) { + Ok(b) => mqtt + .publish(RETURN_TOPIC, QOS, false, b) + .expect("could not publish init response"), Err(e) => panic!("HANDLE FAILED {:?}", e), }; - }, + } Event::Disconnected => { log::info!("GOT A Event::Disconnected msg!"); } @@ -55,22 +80,27 @@ pub fn make_event_loop(mut mqtt: EspMqttClient> } #[cfg(feature = "pingpong")] -pub fn make_event_loop(mut mqtt: EspMqttClient>, rx: mpsc::Receiver, do_log: bool) -> Result<()> { - +pub fn make_event_loop( + mut mqtt: EspMqttClient>, + rx: mpsc::Receiver, + do_log: bool, +) -> Result<()> { log::info!("About to subscribe to the mpsc channel"); while let Ok(event) = rx.recv() { match event { Event::Connected => { log::info!("SUBSCRIBE TO {}", TOPIC); - mqtt.subscribe(TOPIC, QOS).expect("could not MQTT subscribe"); - }, + mqtt.subscribe(TOPIC, QOS) + .expect("could not MQTT subscribe"); + } Event::Message(msg_bytes) => { let b = sphinx_key_signer::parse_ping_and_form_response(msg_bytes); if do_log { log::info!("GOT A PING MESSAGE! returning pong now..."); } - mqtt.publish(RETURN_TOPIC, QOS, false, b).expect("could not publish ping response"); - }, + mqtt.publish(RETURN_TOPIC, QOS, false, b) + .expect("could not publish ping response"); + } Event::Disconnected => { log::info!("GOT A Event::Disconnected msg!"); } From 5da6633182e83fbaf5baf9becc47312a685a7865 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 16 Jun 2022 09:57:28 -0700 Subject: [PATCH 3/7] point to gitlab std-signer branch --- README.md | 5 ++--- signer/Cargo.toml | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e651b3d..952b262 100644 --- a/README.md +++ b/README.md @@ -84,10 +84,9 @@ after pressing the ok button, restart the sphinx key, and wait for a MQTT connec ### espflash notes -espflash save-image esp32-c3 target/riscv32imc-esp-espidf/debug/sphinx-key ./asdf-blah - espflash save-image esp32-c3 target/riscv32imc-esp-espidf/release/sphinx-key ./test-flash espflash board-info -export CC=$PWD/.embuild/espressif/tools/riscv32-esp-elf/esp-2021r2-8.4.0/riscv32-esp-elf/bin/riscv32-esp-elf-gcc \ No newline at end of file +export CC=$PWD/.embuild/espressif/tools/riscv32-esp-elf/esp-2021r2-8.4.0/riscv32-esp-elf/bin/riscv32-esp-elf-gcc + diff --git a/signer/Cargo.toml b/signer/Cargo.toml index 357ba93..3353ec3 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -7,7 +7,8 @@ edition = "2018" [dependencies] sphinx-key-parser = { path = "../parser" } # vls-protocol-signer = { path = "../../../evanf/validating-lightning-signer/vls-protocol-signer", default-features = false, features = ["secp-lowmemory"] } -vls-protocol-signer = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer", default-features = false, features = ["secp-lowmemory"] } +# vls-protocol-signer = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer", default-features = false, features = ["secp-lowmemory"] } +vls-protocol-signer = { git = "https://gitlab.com/Evanfeenstra/validating-lightning-signer", branch = "std-signer", default-features = false, features = ["secp-lowmemory"] } anyhow = {version = "1", features = ["backtrace"]} log = "0.4" From 9d771c136331fd87180eaaa564273ada0a4b9dfa Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 16 Jun 2022 11:58:57 -0700 Subject: [PATCH 4/7] increase main task stack size in esp-idf --- README.md | 8 ++++++++ signer/Cargo.toml | 4 ++-- signer/src/lib.rs | 9 +++++---- sphinx-key/sdkconfig.defaults | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 952b262..2eb45ce 100644 --- a/README.md +++ b/README.md @@ -90,3 +90,11 @@ espflash board-info export CC=$PWD/.embuild/espressif/tools/riscv32-esp-elf/esp-2021r2-8.4.0/riscv32-esp-elf/bin/riscv32-esp-elf-gcc + +cargo +nightly build --release + +esptool.py --chip esp32c3 elf2image target/riscv32imc-esp-espidf/release/sphinx-key + +esptool.py --chip esp32c3 -p /dev/tty.usbserial-1420 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x10000 target/riscv32imc-esp-espidf/release/sphinx-key.bin + +espmonitor /dev/tty.usbserial-1420 diff --git a/signer/Cargo.toml b/signer/Cargo.toml index 3353ec3..ffbb385 100644 --- a/signer/Cargo.toml +++ b/signer/Cargo.toml @@ -6,9 +6,9 @@ edition = "2018" [dependencies] sphinx-key-parser = { path = "../parser" } -# vls-protocol-signer = { path = "../../../evanf/validating-lightning-signer/vls-protocol-signer", default-features = false, features = ["secp-lowmemory"] } +# vls-protocol-signer = { path = "../../../evanf/validating-lightning-signer/vls-protocol-signer", default-features = false, features = ["std", "secp-lowmemory"] } # vls-protocol-signer = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer", default-features = false, features = ["secp-lowmemory"] } -vls-protocol-signer = { git = "https://gitlab.com/Evanfeenstra/validating-lightning-signer", branch = "std-signer", default-features = false, features = ["secp-lowmemory"] } +vls-protocol-signer = { git = "https://gitlab.com/Evanfeenstra/validating-lightning-signer", branch = "std-signer", default-features = false, features = ["std", "secp-lowmemory"] } anyhow = {version = "1", features = ["backtrace"]} log = "0.4" diff --git a/signer/src/lib.rs b/signer/src/lib.rs index 2d3181a..1a06662 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -1,12 +1,13 @@ use lightning_signer::persist::{DummyPersister, Persist}; -use lightning_signer::Arc; +// use lightning_signer::Arc; use sphinx_key_parser::MsgDriver; +use std::sync::Arc; +use vls_protocol::model::PubKey; use vls_protocol::msgs::{self, read_serial_request_header, write_serial_response_header, Message}; use vls_protocol::serde_bolt::WireString; +use vls_protocol_signer::handler::{Handler, RootHandler}; pub use vls_protocol_signer::lightning_signer; pub use vls_protocol_signer::vls_protocol; -use vls_protocol::model::PubKey; -use vls_protocol_signer::handler::{Handler, RootHandler}; pub struct InitResponse { pub root_handler: RootHandler, @@ -28,7 +29,7 @@ pub fn init(bytes: Vec) -> anyhow::Result { .collect::>(); log::info!("allowlist {:?}", allowlist); let seed = init.dev_seed.as_ref().map(|s| s.0).expect("no seed"); - log::info!("seed {:?}", seed); + log::info!("create root handler now"); let root_handler = RootHandler::new(0, Some(seed), persister, allowlist); log::info!("root_handler created"); let init_reply = root_handler diff --git a/sphinx-key/sdkconfig.defaults b/sphinx-key/sdkconfig.defaults index 3ca3b5d..5259aff 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=7000 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=13000 # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). # This allows to use 1 ms granuality for thread sleeps (10 ms by default). From d67f574c9d286fd6378d78171dfc709f9041240d Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 16 Jun 2022 12:04:31 -0700 Subject: [PATCH 5/7] fix tester import --- tester/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tester/src/main.rs b/tester/src/main.rs index 059a5cd..ed3711e 100644 --- a/tester/src/main.rs +++ b/tester/src/main.rs @@ -2,7 +2,8 @@ use sphinx_key_parser as parser; use clap::{App, AppSettings, Arg}; use rumqttc::{self, AsyncClient, Event, MqttOptions, Packet, QoS}; -use sphinx_key_signer::{self, InitResponse, PubKey}; +use sphinx_key_signer::{self, InitResponse}; +use sphinx_key_signer::vls_protocol::model::PubKey; use std::error::Error; use std::time::Duration; use vls_protocol::msgs; From 60f55d7fa5a694aaeade40e0a12ec35a1948958b Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 16 Jun 2022 13:36:25 -0700 Subject: [PATCH 6/7] increase main task stack size and mqtt buffer size --- sphinx-key/sdkconfig.defaults | 2 +- sphinx-key/src/conn/mqtt.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sphinx-key/sdkconfig.defaults b/sphinx-key/sdkconfig.defaults index 5259aff..ce0978e 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=13000 +CONFIG_ESP_MAIN_TASK_STACK_SIZE=24000 # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). # This allows to use 1 ms granuality for thread sleeps (10 ms by default). diff --git a/sphinx-key/src/conn/mqtt.rs b/sphinx-key/src/conn/mqtt.rs index 0ff559c..c8834cf 100644 --- a/sphinx-key/src/conn/mqtt.rs +++ b/sphinx-key/src/conn/mqtt.rs @@ -28,7 +28,7 @@ pub fn make_client( log::info!("make_client with id {}", client_id); let conf = MqttClientConfiguration { client_id: Some(client_id), - buffer_size: 2048, + buffer_size: 4096, task_stack: 12288, username: Some(USERNAME), password: Some(PASSWORD), From 10f8dd4893601cc0bcfe6af7be2ffb89f57efde9 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 16 Jun 2022 14:26:19 -0700 Subject: [PATCH 7/7] allow broker to wait 10 seconds for esp32 --- broker/src/mqtt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/broker/src/mqtt.rs b/broker/src/mqtt.rs index 642cdd2..c99ea97 100644 --- a/broker/src/mqtt.rs +++ b/broker/src/mqtt.rs @@ -17,7 +17,7 @@ const PUB_TOPIC: &str = "sphinx"; const USERNAME: &str = "sphinx-key"; const PASSWORD: &str = "sphinx-key-pass"; // must get a reply within this time, or disconnects -const REPLY_TIMEOUT_MS: u64 = 1000; +const REPLY_TIMEOUT_MS: u64 = 10000; static CONNECTED: SyncLazy> = SyncLazy::new(|| Mutex::new(false)); fn set_connected(b: bool) {