Skip to content

Commit

Permalink
Merge pull request #1665 from get10101/fix/chain-monitor-backwards-in…
Browse files Browse the repository at this point in the history
…compatibility

Read and write `ChainMonitor` using the same key
  • Loading branch information
luckysori authored Dec 1, 2023
2 parents 6f383df + e7f3a4f commit 2fedb1d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Fix backwards-compatibility of `ChainMonitor`s created before version 1.5.0.

## [1.6.5] - 2023-11-29

- Add support for Android 9
Expand Down
26 changes: 23 additions & 3 deletions crates/ln-dlc-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ use std::string::ToString;

pub mod sled;

// Kinds.

const CONTRACT: u8 = 1;
const CHANNEL: u8 = 2;
const CHAIN_MONITOR: u8 = 3;
Expand All @@ -48,6 +50,8 @@ const SUB_CHANNEL: u8 = 7;
const ADDRESS: u8 = 8;
const ACTION: u8 = 9;

const CHAIN_MONITOR_KEY: &str = "chain_monitor";

pub trait WalletStorage {
fn upsert_address(&self, address: &Address, privkey: &SecretKey) -> Result<()>;
fn delete_address(&self, address: &Address) -> Result<()>;
Expand Down Expand Up @@ -439,7 +443,7 @@ impl<K: DlcStoreProvider> dlc_manager::Storage for DlcStorageProvider<K> {
self.store
.write(
CHAIN_MONITOR,
"chain_monitor".to_string().into_bytes(),
CHAIN_MONITOR_KEY.to_string().into_bytes(),
monitor.serialize()?,
)
.map_err(|e| Error::StorageError(format!("Error writing chain monitor: {e}")))
Expand All @@ -448,7 +452,10 @@ impl<K: DlcStoreProvider> dlc_manager::Storage for DlcStorageProvider<K> {
fn get_chain_monitor(&self) -> Result<Option<ChainMonitor>, Error> {
let chain_monitors = self
.store
.read(CHAIN_MONITOR, None)
.read(
CHAIN_MONITOR,
Some(CHAIN_MONITOR_KEY.to_string().into_bytes()),
)
.map_err(|e| Error::StorageError(format!("Error reading chain monitor: {e}")))?;

let serialized = chain_monitors.first();
Expand Down Expand Up @@ -1127,14 +1134,27 @@ mod tests {

storage
.persist_chain_monitor(&chain_monitor)
.expect("to be able to persist the chain monistor.");
.expect("to be able to persist the chain monitor.");

let retrieved = storage
.get_chain_monitor()
.expect("to be able to retrieve the chain monitor.")
.expect("to have a persisted chain monitor.");

assert_eq!(chain_monitor, retrieved);

let chain_monitor2 = ChainMonitor::new(456);

storage
.persist_chain_monitor(&chain_monitor2)
.expect("to be able to persist the chain monitor.");

let retrieved2 = storage
.get_chain_monitor()
.expect("to be able to retrieve the chain monitor.")
.expect("to have a persisted chain monitor.");

assert_eq!(chain_monitor2, retrieved2);
}

#[test]
Expand Down
20 changes: 17 additions & 3 deletions crates/tests-e2e/tests/restore_from_backup.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use anyhow::Result;
use native::api;
use native::trade::position::PositionState;
use tests_e2e::app::run_app;
use tests_e2e::logger::init_tracing;
use tests_e2e::setup;
use tests_e2e::setup::dummy_order;
use tests_e2e::wait_until;
use tokio::task::spawn_blocking;

#[tokio::test]
#[ignore = "need to be run with 'just e2e' command"]
async fn app_can_be_restored_from_a_backup() -> Result<()> {
async fn app_can_be_restored_from_a_backup() {
init_tracing();

let test = setup::TestSetup::new_with_open_position().await;
Expand All @@ -34,5 +36,17 @@ async fn app_can_be_restored_from_a_backup() -> Result<()> {
.await
.unwrap();

Ok(())
let closing_order = {
let mut order = dummy_order();
order.direction = api::Direction::Short;
order
};

tracing::info!("Closing a position");
spawn_blocking(move || api::submit_order(closing_order).unwrap())
.await
.unwrap();

wait_until!(test.app.rx.position().unwrap().position_state == PositionState::Closing);
wait_until!(test.app.rx.position_close().is_some());
}
2 changes: 1 addition & 1 deletion mobile/linux/rust.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ FetchContent_Declare(

FetchContent_MakeAvailable(Corrosion)

corrosion_import_crate(MANIFEST_PATH ../native/Cargo.toml)
corrosion_import_crate(MANIFEST_PATH ../native/Cargo.toml CRATES native)

# Flutter-specific

Expand Down

0 comments on commit 2fedb1d

Please sign in to comment.