sphinx-key: store previous messages on flash, not on sd card

This commit is contained in:
irriden
2023-09-11 17:01:28 +00:00
parent dbeeb2a0a2
commit 9b0d9e4ce8
3 changed files with 29 additions and 6 deletions

View File

@@ -26,6 +26,27 @@ impl FlashPersister {
let store = EspDefaultNvs::new(nvs, "sphinx", true).expect("no storage"); let store = EspDefaultNvs::new(nvs, "sphinx", true).expect("no storage");
Self(store) Self(store)
} }
pub fn set_prevs(&mut self, prev_vls: &Vec<u8>, prev_lss: &Vec<u8>) -> Result<()> {
self.0.set_raw("prev_vls", prev_vls)?;
self.0.set_raw("prev_lss", prev_lss)?;
Ok(())
}
pub fn _remove_prevs() {
todo!();
}
pub fn read_prevs(&self) -> Result<(Vec<u8>, Vec<u8>)> {
let mut vls_buf = [0u8; 4_096];
let mut lss_buf = [0u8; 4_096];
let prev_vls = self
.0
.get_raw("prev_vls", &mut vls_buf)?
.ok_or(anyhow!("no existing prev_vls"))?;
let prev_lss = self
.0
.get_raw("prev_lss", &mut lss_buf)?
.ok_or(anyhow!("no existing prev_lss"))?;
Ok((prev_vls.to_vec(), prev_lss.to_vec()))
}
} }
impl ControlPersist for FlashPersister { impl ControlPersist for FlashPersister {

View File

@@ -1,5 +1,6 @@
use crate::conn::mqtt::QOS; use crate::conn::mqtt::QOS;
use crate::core::lss; use crate::core::lss;
use crate::core::FlashPersister;
use crate::ota::{update_sphinx_key, validate_ota_message}; use crate::ota::{update_sphinx_key, validate_ota_message};
use crate::status::Status; use crate::status::Status;
@@ -16,8 +17,7 @@ use sphinx_signer::persist::{BackupPersister, FsPersister, ThreadMemoPersister};
use sphinx_signer::root::VlsHandlerError; use sphinx_signer::root::VlsHandlerError;
use sphinx_signer::sphinx_glyph as glyph; use sphinx_signer::sphinx_glyph as glyph;
use sphinx_signer::{self, RootHandler}; use sphinx_signer::{self, RootHandler};
use std::sync::mpsc; use std::sync::{mpsc, Arc, Mutex};
use std::sync::Arc;
use std::thread; use std::thread;
use embedded_svc::httpd::Result; use embedded_svc::httpd::Result;
@@ -86,6 +86,7 @@ pub fn make_event_loop(
mut ctrlr: Controller, mut ctrlr: Controller,
client_id: &str, client_id: &str,
node_id: &PublicKey, node_id: &PublicKey,
msgs_persister: Arc<Mutex<FlashPersister>>,
) -> Result<()> { ) -> Result<()> {
while let Ok(event) = rx.recv() { while let Ok(event) = rx.recv() {
log::info!("BROKER IP AND PORT: {}", config.broker); log::info!("BROKER IP AND PORT: {}", config.broker);
@@ -144,8 +145,6 @@ pub fn make_event_loop(
// store the previous msgs processed, for LSS last step // store the previous msgs processed, for LSS last step
let mut msgs: Option<(Vec<u8>, Vec<u8>)> = None; let mut msgs: Option<(Vec<u8>, Vec<u8>)> = None;
// persist them to sd card in case of crash
let msgs_persister = FsPersister::new(&ROOT_STORE, Some(8));
// signing loop // signing loop
log::info!("=> starting the main signing loop..."); log::info!("=> starting the main signing loop...");
@@ -184,7 +183,7 @@ pub fn make_event_loop(
} else { } else {
// muts! send LSS first! // muts! send LSS first!
mqtt_pub(&mut mqtt, client_id, topics::LSS_RES, &lss_b); mqtt_pub(&mut mqtt, client_id, topics::LSS_RES, &lss_b);
msgs_persister.set_prevs(&vls_b, &lss_b); msgs_persister.lock().unwrap().set_prevs(&vls_b, &lss_b)?;
msgs = Some((vls_b, lss_b)); msgs = Some((vls_b, lss_b));
} }
expected_sequence = Some(sequence + 1); expected_sequence = Some(sequence + 1);
@@ -221,6 +220,8 @@ pub fn make_event_loop(
log::warn!("Restoring previous message from sd card"); log::warn!("Restoring previous message from sd card");
msgs = Some( msgs = Some(
msgs_persister msgs_persister
.lock()
.unwrap()
.read_prevs() .read_prevs()
.map_err(|e| anyhow::anyhow!("{:?}", e))?, .map_err(|e| anyhow::anyhow!("{:?}", e))?,
) )

View File

@@ -176,7 +176,7 @@ fn make_and_launch_client(
}; };
// make the controller to validate Control messages // make the controller to validate Control messages
let ctrlr = controller_from_seed(&network, &seed[..], flash); let ctrlr = controller_from_seed(&network, &seed[..], flash.clone());
let pubkey = ctrlr.pubkey(); let pubkey = ctrlr.pubkey();
let pubkey_str = hex::encode(&pubkey.serialize()); let pubkey_str = hex::encode(&pubkey.serialize());
@@ -206,6 +206,7 @@ fn make_and_launch_client(
ctrlr, ctrlr,
&client_id, &client_id,
&pubkey, &pubkey,
flash,
)?; )?;
Ok(()) Ok(())
} }