fix new_with_persister, return the original msg from controller.handle

This commit is contained in:
Evan Feenstra
2022-09-12 11:28:35 -07:00
parent 51659c74ac
commit 17fee73ccb
3 changed files with 40 additions and 18 deletions

View File

@@ -58,9 +58,11 @@ impl Controller {
pub fn new_with_persister(
sk: SecretKey,
pk: PublicKey,
nonce: u64,
per: Arc<Mutex<dyn ControlPersist>>,
) -> Self {
let store1 = per.clone();
let store = store1.lock().unwrap();
let nonce = store.read_nonce().unwrap_or(0);
Self(sk, pk, nonce, per)
}
pub fn build_msg(&mut self, msg: ControlMessage) -> anyhow::Result<Vec<u8>> {
@@ -86,7 +88,8 @@ impl Controller {
pub fn parse_response(&self, input: &[u8]) -> anyhow::Result<ControlResponse> {
Ok(rmp_serde::from_slice(input)?)
}
pub fn handle(&mut self, input: &[u8]) -> anyhow::Result<(Vec<u8>, Option<Policy>)> {
// return the OG message for further processing
pub fn handle(&mut self, input: &[u8]) -> anyhow::Result<(Vec<u8>, ControlMessage)> {
let msg = self.parse_msg_no_nonce(input)?;
// increment the nonce EXCEPT for Nonce requests
let mut store = self.3.lock().unwrap();
@@ -94,24 +97,20 @@ impl Controller {
ControlMessage::Nonce => (),
_ => {
self.2 = self.2 + 1;
store.set_nonce(self.2);
store.set_nonce(self.2)?;
}
}
let mut new_policy = None;
let res = match msg {
let res = match msg.clone() {
ControlMessage::Nonce => ControlResponse::Nonce(self.2),
ControlMessage::ResetWifi => {
store.reset();
ControlResponse::ResetWifi
}
ControlMessage::UpdatePolicy(np) => {
new_policy = Some(np.clone());
ControlResponse::PolicyUpdated(np)
}
ControlMessage::UpdatePolicy(np) => ControlResponse::PolicyUpdated(np),
_ => ControlResponse::Nonce(self.2),
};
let response = self.build_response(res)?;
Ok((response, new_policy))
Ok((response, msg))
}
}
@@ -133,7 +132,8 @@ impl FlashKey {
pub trait ControlPersist: Sync + Send {
fn reset(&mut self);
fn set_nonce(&mut self, nonce: u64);
fn read_nonce(&self) -> Result<u64>;
fn set_nonce(&mut self, nonce: u64) -> Result<()>;
fn read_config(&self) -> Result<Config>;
fn write_config(&mut self, c: Config) -> Result<()>;
fn read_seed(&self) -> Result<[u8; 32]>;
@@ -144,7 +144,12 @@ pub struct DummyPersister;
impl ControlPersist for DummyPersister {
fn reset(&mut self) {}
fn set_nonce(&mut self, _nonce: u64) {}
fn read_nonce(&self) -> Result<u64> {
Ok(0u64)
}
fn set_nonce(&mut self, _nonce: u64) -> Result<()> {
Ok(())
}
fn read_config(&self) -> Result<Config> {
Ok(Default::default())
}

View File

@@ -7,6 +7,7 @@ use sphinx_key_signer::control::{Config, ControlPersist, Controller, FlashKey};
use sphinx_key_signer::lightning_signer::bitcoin::Network;
use std::convert::TryInto;
use std::sync::{Arc, Mutex};
// the controller validates Control messages
pub fn controller_from_seed(
network: &Network,
@@ -14,7 +15,7 @@ pub fn controller_from_seed(
flash: Arc<Mutex<FlashPersister>>,
) -> Controller {
let (pk, sk) = sphinx_key_signer::derive_node_keys(network, seed);
Controller::new_with_persister(sk, pk, 0, flash)
Controller::new_with_persister(sk, pk, flash)
}
pub struct FlashPersister(pub EspNvsStorage);
@@ -32,9 +33,19 @@ impl ControlPersist for FlashPersister {
.remove(FlashKey::Config.as_str())
.expect("couldnt remove config 1");
}
fn set_nonce(&mut self, nonce: u64) {
// self.0.set
//
fn read_nonce(&self) -> Result<u64> {
let mut buf = [0u8; 8];
let existing = self.0.get_raw(FlashKey::Nonce.as_str(), &mut buf)?;
if let None = existing {
return Err(anyhow!("no existing nonce"));
}
let r: [u8; 8] = existing.unwrap().0.try_into()?;
Ok(u64::from_be_bytes(r))
}
fn set_nonce(&mut self, nonce: u64) -> Result<()> {
let n = nonce.to_be_bytes();
self.0.put_raw(FlashKey::Nonce.as_str(), &n[..])?;
Ok(())
}
fn read_config(&self) -> Result<Config> {
let mut buf = [0u8; 250];

View File

@@ -1,7 +1,7 @@
use crate::conn::mqtt::QOS;
use crate::core::control::{controller_from_seed, FlashPersister};
use sphinx_key_signer::control::Config;
use sphinx_key_signer::control::{Config, ControlMessage};
use sphinx_key_signer::lightning_signer::bitcoin::Network;
use sphinx_key_signer::topics;
use sphinx_key_signer::vls_protocol::model::PubKey;
@@ -112,8 +112,14 @@ pub fn make_event_loop(
Event::Control(ref msg_bytes) => {
log::info!("GOT A CONTROL MSG");
match ctrlr.handle(msg_bytes) {
Ok((response, _new_policy)) => {
Ok((response, parsed_msg)) => {
// log::info!("CONTROL MSG {:?}", response);
match parsed_msg {
ControlMessage::UpdatePolicy(new_policy) => {
// update here
}
_ => (),
};
mqtt.publish(topics::CONTROL_RETURN, QOS, false, &response)
.expect("could not publish control response");
}