Skip to content

Commit

Permalink
Merge pull request #422 from Lederstrumpf/state_recovery
Browse files Browse the repository at this point in the history
State recovery
  • Loading branch information
zkao authored Jun 21, 2022
2 parents 1126b4c + 867aed7 commit 4d6a27b
Show file tree
Hide file tree
Showing 21 changed files with 824 additions and 22 deletions.
25 changes: 24 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ required-features = ["server"]
# farcaster_core = { version = "0.4.4", features = ["serde"] }
farcaster_core = { git = "https://github.com/farcaster-project/farcaster-core", branch = "main", features = ["serde"] }


anyhow = "1"
hex = "^0.4.3"

Expand All @@ -74,6 +73,9 @@ lazy_static = "1.4"
lightning_encoding = "0.6.1"
lnp-core = { version = "0.6.0", git = "https://github.com/LNP-BP/lnp-core" }
lnpbp = { version = "0.6.0", features = ["all"] }
lmdb = "0.8.0"
# lnp-core = { path = "../ext/lnp-core" }
# internet2 = { path = "../ext/rust-internet2" }
# Congig & logging
log = { version = "0.4", features = ["max_level_trace", "release_max_level_debug"] }
monero = "0.16"
Expand Down
1 change: 0 additions & 1 deletion doc/sequence_diagram.svg

This file was deleted.

1 change: 1 addition & 0 deletions doc/sequencediagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 21 additions & 3 deletions doc/sequencediagram.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ title Farcaster node
participant t_syncer
participant t_wallet
participant t_swap
participant t_checkpoint
participant t_farcasterd
participant t_cli
participant peerd
participant m_cli
participant m_farcasterd
participant m_checkpoint
participant m_swap
participant m_wallet
participant m_syncer
Expand All @@ -35,6 +37,7 @@ t_wallet <- t_farcasterd : Ctl TakeOffer
t_wallet -> t_wallet : create taker wallet
t_wallet -> t_farcasterd : Ctl LaunchSwap
t_swap -> t_farcasterd : Ctl Hello
t_swap -> t_wallet : Ctl Hello
t_farcasterd -> t_farcasterd:launch syncer
t_swap <- t_farcasterd : Ctl TakeSwap
t_swap -> peerd : Msg TakerCommit
Expand All @@ -44,11 +47,13 @@ m_farcasterd -> m_wallet : Msg TakerCommit
m_wallet -> m_wallet : create maker wallet
m_wallet -> m_farcasterd : Ctl LaunchSwap
m_swap -> m_farcasterd : Ctl Hello
m_swap -> m_wallet : Ctl Hello
m_farcasterd -> m_farcasterd:launch syncer
m_farcasterd -> m_swap : Ctl MakeSwap

m_swap -> peerd : Msg MakerCommit
t_swap <- peerd : Msg MakerCommit
// TODO: verify that swapd launches no matter what
t_syncer <- t_swap : Ctl WatchHeight
t_syncer <- t_swap : if Bob, Watch Arbitrating Funding Address
t_swap -> t_wallet : Msg MakerCommit
Expand Down Expand Up @@ -88,36 +93,44 @@ t_swap -> t_wallet : if Bob, SEND PENDING Msg Reveal (maker is sender)
==Commit-Reveal Complete==
==Changing semantics: On Commit-Reveal, Maker and Taker were the key roles. From now on Bob or Alice are the key roles. Now t_ is bob_ on the left and m_ is alice_ on the right.==
==Swap setup: Bob is left, Alice right==
t_wallet -> t_checkpoint: Ctl CheckpointWalletBobPrelockBob
t_wallet -> t_swap : Ctl CoreArbitratingSetup
t_swap -> t_checkpoint: Ctl CheckpointSwapBobPrelockBob
// TODO: During replay of CheckpointSwapBobPrelockBob, Bob has to rewatch these 3 txs
t_syncer <- t_swap : Watch Arbitrating Lock
t_syncer <- t_swap : Watch Cancel
t_syncer <- t_swap : Watch Refund
peerd <- t_swap : Msg CoreArbitratingSetup
m_swap <- peerd : Msg CoreArbitratingSetup
m_swap -> m_syncer : Watch Arbitrating Lock
// TODO: During replay of CheckpointWalletAlicePrelockBob, Alice has to rewatch these 2 txs (arbitrating already final then)
m_swap -> m_syncer : Watch Cancel
m_swap -> m_syncer : Watch Refund

m_wallet <- m_swap : Msg CoreArbitratingSetup
m_wallet -> m_checkpoint : Ctl CheckpointWalletAlicePrelockBob
m_wallet -> m_swap : Ctl RefundProcedureSignatures
m_swap -> m_checkpoint : Ctl CheckpointSwapAlicePrelockBob
m_swap -> peerd : Msg RefundProcedureSignatures
peerd -> t_swap : Msg RefundProcedureSignatures
t_wallet <- t_swap : Msg RefundProcedureSignatures
t_wallet -> t_swap:Ctl Datum::SignedArbitratingLock
// DONE: do we know that same inputs are being used in case of replay?
// -> yes, but create different sig
t_wallet -> t_checkpoint : Ctl CheckpointWalletBobPreBuySig
t_wallet -> t_swap : Ctl BuyProcedureSignature
t_swap -> t_checkpoint : Ctl CheckpointSwapBobPreBuySig
t_syncer <- t_swap : Broadcast Arbitrating Lock
t_swap -> t_syncer : Watch Accordant Lock
t_swap -> t_syncer : Watch Buy
t_swap --> t_swap : Checkpoint Bob 0

parallel
t_syncer -> t_swap : Arbitrating Lock final
// TODO: maybe instead of checkpointing earlier, reach this stage via a message from walletd in lieu of the syncer
m_swap <- m_syncer : Arbitrating Lock final
parallel off

m_swap --> m_swap : Checkpoint Alice 0
m_swap -> m_syncer : Watch Accordant Lock
m_swap -> m_syncer : Broadcast Accordant Lock

parallel
m_swap <- m_syncer : Accordant Lock final
Expand All @@ -126,8 +139,10 @@ parallel off

peerd <- t_swap : Msg BuyProcedureSignature
m_swap <- peerd : Msg BuyProcedureSignature
m_swap -> m_checkpoint : Ctl CheckpointSwapAlicePreBuy
m_swap -> m_syncer:Watch Buy
m_swap -> m_wallet : Msg BuyProcedureSignature
m_wallet -> m_checkpoint : Ctl CheckpointWalletAlicePreBuy
==Swap Setup Complete==
==Buy Procedure: Bob is left, Alice right==

Expand All @@ -141,6 +156,7 @@ t_wallet <- t_swap : Ctl Buy signature
t_wallet -> t_wallet : recover accordant keys

==Cancel Init t > t0: Bob is left, Alice right, either have a fully signed and valid cancel tx, and can publish==
// TODO: insert Ctl Tx::Cancel from wallet to swap (or do it after CoreArbitratingSetup, as in the code atm)
parallel
t_swap <- t_syncer : Ctl Cancel valid
m_swap <- m_syncer : Ctl Cancel valid
Expand All @@ -160,8 +176,10 @@ m_swap -> m_wallet : Ctl Tx::Refund tx
m_wallet -> m_wallet : recover accordant keys

== Punish process t > t1 > t0 ==
// TODO: none of this is true except last step
m_swap<-m_syncer:Ctl Event: punish valid
m_swap->m_wallet:Ctl Event: punish valid
m_wallet->m_wallet:fully sign punish
// TODO: in the code, this actually already happens after CoreArbitratingSetup - think about this and move either this or that
m_swap<-m_wallet:Ctl Tx::Punish
m_swap->m_syncer:Ctl Broadcast punish tx
1 change: 1 addition & 0 deletions doc/staterecovery_sequencediagram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions doc/staterecovery_sequencediagram.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
title State Recovery Procedure

participant syncerd
// TODO: remove walletd once it's stateless
participant walletd
participant swapd
participant checkpointd
participant farcasterd
participant cli
participant peerd

== State Recovery Procedure
entryspacing 0.8
farcasterd -> checkpointd : launch
checkpointd -> farcasterd : RecoverableStates
farcasterd -> cli : RecoverableStates
cli -> farcasterd : RecoverSelectedState
farcasterd -> walletd : launch
farcasterd -> swapd : launch
farcasterd -> syncerd : launch
farcasterd -> peerd : launch
walletd -> farcasterd : Ctl Hello
swapd -> farcasterd : Ctl Hello
syncerd -> farcasterd : Ctl Hello
peerd -> farcasterd : Ctl Hello
farcasterd -> checkpointd : RecoverSelectedState
checkpointd -> walletd : RecoverWallet
checkpointd -> peerd : RecoverPeerd
checkpointd -> swapd : RecoverSwap
// these tasks could be checkpointed, but can probably be recreated from scratch based on recovered state in swapd (and currently also walletd)
swapd -> syncerd : WatchTasks
swapd -> swapd : ResumeSwap
52 changes: 52 additions & 0 deletions src/bin/databased.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// LNP Node: node running lightning network protocol and generalized lightning
// channels.
// Written in 2020 by
// Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

#![recursion_limit = "256"]
// Coding conventions
#![deny(
non_upper_case_globals,
non_camel_case_types,
non_snake_case,
unused_mut,
unused_imports,
dead_code,
missing_docs
)]

//! Main executable for databased: farcaster node databaseing microservice.
#[macro_use]
extern crate log;

use clap::Parser;

use farcaster_node::databased::{self, Opts};
use farcaster_node::ServiceConfig;

fn main() {
let mut opts = Opts::parse();
trace!("Command-line arguments: {:?}", &opts);
opts.process();
trace!("Processed arguments: {:?}", &opts);

let service_config: ServiceConfig = opts.shared.clone().into();
trace!("Daemon configuration: {:#?}", &service_config);
debug!("MSG RPC socket {}", &service_config.msg_endpoint);
debug!("CTL RPC socket {}", &service_config.ctl_endpoint);

debug!("Starting runtime ...");
databased::run(service_config, opts.shared.data_dir).expect("Error running databased runtime");

unreachable!()
}
3 changes: 1 addition & 2 deletions src/bin/walletd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ fn main() {
let wallet_token = Token(opts.wallet_token.token);

let node_secrets = NodeSecrets::new(opts.key_opts.key_file.clone());
let node_id = node_secrets.node_id();

debug!("Starting runtime ...");
walletd::run(service_config, wallet_token, node_secrets, node_id)
walletd::run(service_config, wallet_token, node_secrets)
.expect("Error running walletd runtime");

unreachable!()
Expand Down
21 changes: 21 additions & 0 deletions src/databased/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// LNP Node: node running lightning network protocol and generalized lightning
// channels.
// Written in 2020 by
// Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

#[cfg(feature = "shell")]
mod opts;
mod runtime;

#[cfg(feature = "shell")]
pub use opts::Opts;
pub use runtime::run;
41 changes: 41 additions & 0 deletions src/databased/opts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// LNP Node: node running lightning network protocol and generalized lightning
// channels.
// Written in 2020 by
// Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use crate::opts::FARCASTER_KEY_FILE;
use clap::{AppSettings, Parser, ValueHint};
use std::path::PathBuf;
use std::{fs, io::Read};

use crate::opts::TokenString;
use bitcoin::secp256k1::{
rand::{rngs::ThreadRng, thread_rng},
PublicKey, Secp256k1, SecretKey,
};
use strict_encoding::{StrictDecode, StrictEncode};

/// database daemon; part of Farcaster Node
#[derive(Parser, Clone, PartialEq, Eq, Debug)]
#[clap(name = "databased", bin_name = "databased", author, version)]
pub struct Opts {
/// These params can be read also from the configuration file, not just
/// command-line args or environment variables
#[clap(flatten)]
pub shared: crate::opts::Opts,
}

impl Opts {
pub fn process(&mut self) {
self.shared.process();
}
}
Loading

0 comments on commit 4d6a27b

Please sign in to comment.