persist velocity after handle

This commit is contained in:
Evan Feenstra
2023-06-09 13:03:55 -07:00
parent 193b4c772b
commit d629652225
5 changed files with 49 additions and 11 deletions

8
sphinx-key/Cargo.lock generated
View File

@@ -1313,7 +1313,7 @@ dependencies = [
[[package]]
name = "lss-connector"
version = "0.1.0"
source = "git+https://github.com/stakwork/sphinx-rs.git#aa4a462f3fce8ecb03201a64c9d5ae785634a3e5"
source = "git+https://github.com/stakwork/sphinx-rs.git#275f09e004169fdc068af22a5b35f2fd1a6930cf"
dependencies = [
"anyhow",
"log",
@@ -1965,7 +1965,7 @@ dependencies = [
[[package]]
name = "sphinx-auther"
version = "0.1.12"
source = "git+https://github.com/stakwork/sphinx-rs.git#aa4a462f3fce8ecb03201a64c9d5ae785634a3e5"
source = "git+https://github.com/stakwork/sphinx-rs.git#275f09e004169fdc068af22a5b35f2fd1a6930cf"
dependencies = [
"anyhow",
"base64",
@@ -1988,7 +1988,7 @@ dependencies = [
[[package]]
name = "sphinx-glyph"
version = "0.1.2"
source = "git+https://github.com/stakwork/sphinx-rs.git#aa4a462f3fce8ecb03201a64c9d5ae785634a3e5"
source = "git+https://github.com/stakwork/sphinx-rs.git#275f09e004169fdc068af22a5b35f2fd1a6930cf"
dependencies = [
"anyhow",
"hex",
@@ -2025,7 +2025,7 @@ dependencies = [
[[package]]
name = "sphinx-signer"
version = "0.1.0"
source = "git+https://github.com/stakwork/sphinx-rs.git#aa4a462f3fce8ecb03201a64c9d5ae785634a3e5"
source = "git+https://github.com/stakwork/sphinx-rs.git#275f09e004169fdc068af22a5b35f2fd1a6930cf"
dependencies = [
"anyhow",
"bip39",

View File

@@ -20,9 +20,11 @@ pingpong = []
no_persist = []
[dependencies]
# sphinx-signer = { path = "../../sphinx-rs/signer", optional = true }
sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true }
sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git" }
# lss-connector = { path = "../../sphinx-rs/lss-connector", default-features = false }
lss-connector = { git = "https://github.com/stakwork/sphinx-rs.git", default-features = false }
sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git" }
bitflags = "1.3.2"
esp-idf-sys = { version = "0.32.1", features = ["binstart"] }
embedded-svc = "0.24.0"

View File

@@ -2,7 +2,9 @@ use anyhow::{anyhow, Result};
use embedded_svc::storage::RawStorage;
use esp_idf_svc::nvs::{EspDefaultNvs, EspDefaultNvsPartition};
use sphinx_signer::lightning_signer::bitcoin::Network;
use sphinx_signer::sphinx_glyph::control::{Config, ControlPersist, Controller, FlashKey, Policy};
use sphinx_signer::sphinx_glyph::control::{
Config, ControlPersist, Controller, FlashKey, Policy, Velocity,
};
use std::convert::TryInto;
use std::sync::{Arc, Mutex};
@@ -79,7 +81,7 @@ impl ControlPersist for FlashPersister {
let mut buf = [0u8; 250];
let existing = self.0.get_raw(FlashKey::Policy.as_str(), &mut buf)?;
if let None = existing {
return Err(anyhow!("no existing config"));
return Err(anyhow!("no existing policy"));
}
Ok(rmp_serde::from_slice(existing.unwrap())?)
}
@@ -92,4 +94,20 @@ impl ControlPersist for FlashPersister {
self.0.remove(FlashKey::Policy.as_str())?;
Ok(())
}
fn read_velocity(&self) -> Result<Velocity> {
let mut buf = [0u8; 250];
let existing = self
.0
.get_raw(FlashKey::Velocity.as_str(), &mut buf)?
.ok_or(anyhow!("no existing velocity"))?;
// if let None = existing {
// return Err(anyhow!("no existing velocity"));
// }
Ok(rmp_serde::from_slice(existing)?)
}
fn write_velocity(&mut self, vel: Velocity) -> Result<()> {
let vel1 = rmp_serde::to_vec(&vel)?;
self.0.set_raw(FlashKey::Velocity.as_str(), &vel1[..])?;
Ok(())
}
}

View File

@@ -7,7 +7,7 @@ use sphinx_signer::lightning_signer::bitcoin::Network;
use sphinx_signer::lightning_signer::persist::Persist;
use sphinx_signer::persist::{BackupPersister, FsPersister, ThreadMemoPersister};
use sphinx_signer::sphinx_glyph::control::{
Config, ControlMessage, ControlResponse, Controller, Policy,
Config, ControlMessage, ControlResponse, Controller, Policy, Velocity,
};
use sphinx_signer::sphinx_glyph::error::Error as GlyphError;
use sphinx_signer::sphinx_glyph::topics;
@@ -88,6 +88,7 @@ pub fn make_event_loop(
config: Config,
seed: [u8; 32],
policy: &Policy,
velocity: &Velocity,
mut ctrlr: Controller,
client_id: &str,
node_id: &PublicKey,
@@ -114,8 +115,9 @@ pub fn make_event_loop(
let persister = Arc::new(BackupPersister::new(sd_persister, lss_persister));
// initialize the RootHandler
let (rhb, approver) = sphinx_signer::root::builder(seed, network, policy, persister, node_id)
.expect("failed to init signer");
let (rhb, approver) =
sphinx_signer::root::builder(seed, network, policy, velocity, persister, node_id)
.expect("failed to init signer");
// FIXME it right to restart here?
let (root_handler, lss_signer) = match lss::init_lss(client_id, &rx, rhb, &mut mqtt) {
@@ -131,6 +133,7 @@ pub fn make_event_loop(
// signing loop
log::info!("=> starting the main signing loop...");
let flash_db = ctrlr.persister();
while let Ok(event) = rx.recv() {
match event {
Event::Connected => {
@@ -143,6 +146,7 @@ pub fn make_event_loop(
}
Event::VlsMessage(ref msg_bytes) => {
led_tx.send(Status::Signing).unwrap();
let state1 = approver.control().get_state();
let _ret = match sphinx_signer::root::handle_with_lss(
&root_handler,
&lss_signer,
@@ -165,6 +169,15 @@ pub fn make_event_loop(
mqtt_pub(&mut mqtt, client_id, topics::ERROR, &err_msg.to_vec()[..]);
}
};
let state2 = approver.control().get_state();
if state1 != state2 {
// save the velocity state in case of crash or restart
let mut flash_db = flash_db.lock().unwrap();
if let Err(e) = flash_db.write_velocity(state2) {
log::error!("failed to set velocity state {:?}", e);
}
drop(flash_db);
}
}
Event::LssMessage(ref msg_bytes) => {
match lss::handle_lss_msg(msg_bytes, &msgs, &lss_signer) {
@@ -272,6 +285,7 @@ pub fn make_event_loop(
_config: Config,
_seed: [u8; 32],
_policy: &Policy,
_velocity: &Velocity,
mut _ctrlr: Controller,
client_id: &str,
_node_id: &PublicKey,

View File

@@ -22,7 +22,7 @@ use esp_idf_hal::peripherals::Peripherals;
use esp_idf_svc::nvs::*;
use sphinx_signer::lightning_signer::bitcoin::Network;
use sphinx_signer::sphinx_glyph::control::{Config, ControlPersist, Policy};
use sphinx_signer::sphinx_glyph::control::{Config, ControlPersist, Policy, Velocity};
#[cfg(not(feature = "pingpong"))]
const CLIENT_ID: &str = "sphinx-1";
@@ -66,6 +66,7 @@ fn main() -> Result<()> {
if let Ok(exist) = flash.read_config() {
let seed = flash.read_seed().expect("no seed...");
let policy = flash.read_policy().unwrap_or_default();
let velocity = flash.read_velocity().unwrap_or_default();
println!(
"=============> START CLIENT NOW <============== {:?}",
exist
@@ -100,6 +101,7 @@ fn main() -> Result<()> {
exist.clone(),
seed,
&policy,
&velocity,
led_tx.clone(),
flash_arc.clone(),
) {
@@ -131,6 +133,7 @@ fn make_and_launch_client(
config: Config,
seed: [u8; 32],
policy: &Policy,
velocity: &Velocity,
led_tx: mpsc::Sender<Status>,
flash: Arc<Mutex<FlashPersister>>,
) -> anyhow::Result<()> {
@@ -172,6 +175,7 @@ fn make_and_launch_client(
config,
seed,
policy,
velocity,
ctrlr,
&client_id,
&pubkey,