Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lightning Network Channels #1133

Merged
merged 42 commits into from
Dec 14, 2021
Merged

Lightning Network Channels #1133

merged 42 commits into from
Dec 14, 2021

Conversation

shamardy
Copy link
Collaborator

Part of #1045

Features:

  • Withdraw to p2wsh addresses
  • Ability to restart a lightning node and continue operation
  • Ability to run multiple lightning nodes for different coins (only BTC and tBTC are supported for now)
  • connect_to_lightning_node RPC
  • open_channel RPC

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first review iteration. I may have more remarks/questions in the future.

mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning/ln_rpc.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
@shamardy
Copy link
Collaborator Author

@artemii235 this is not ready for the next review yet. I need to use the correct size and fee here https://github.com/KomodoPlatform/atomicDEX-API/blob/7ff4b28217a2e12a1e9369ae883b7f2e145e9071/mm2src/coins/lightning.rs#L515

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few minor questions and refactoring suggestions.

mm2src/coins/lightning/ln_rpc.rs Show resolved Hide resolved
mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
network: conf.network,
best_block: BestBlock::new(BlockHash::from_hash(best_block_hash), best_block.height as u32),
let mut restarting_node = true;
let rpc_client = match &filter.clone().unwrap().platform_coin.as_ref().rpc_client {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is unwrap safe here? Please add a comment if yes or return the error otherwise.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if rpc_client is initialized before platform_coin is moved?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is unwrap safe here? Please add a comment if yes or return the error otherwise.

filter which implements the Filter trait https://github.com/KomodoPlatform/atomicDEX-API/blob/7ff4b28217a2e12a1e9369ae883b7f2e145e9071/mm2src/coins/lightning/ln_rpc.rs#L138
is only needed for electrum clients that's why I chose to make it Some here https://github.com/KomodoPlatform/atomicDEX-API/blob/7ff4b28217a2e12a1e9369ae883b7f2e145e9071/mm2src/coins/lightning/ln_utils.rs#L247 for now until Native client is implemented then this code will be changed depending on which client is used.

What if rpc_client is initialized before platform_coin is moved?

I chose to use filter in places where the code will be used for electrum clients only in the future as a reminder. Maybe I should add a comment in the code as a reminder also.

mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
activation_params: Self::ActivationParams,
protocol_conf: Self::ProtocolInfo,
) -> Result<(Self, Self::ActivationResult), MmError<Self::ActivationError>> {
let network = network_from_string(protocol_conf.network)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be moved to the request deserialization stage by adding BitcoinNetwork enum that implements Into<bitcoin::network::constants::Network>.

protocol_conf: Self::ProtocolInfo,
) -> Result<(Self, Self::ActivationResult), MmError<Self::ActivationError>> {
let network = network_from_string(protocol_conf.network)?;
let params = LightningActivationParams::from_activation_req(platform_coin.clone(), activation_params)?;
Copy link
Member

@artemii235 artemii235 Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from_activation_req performs additional validation of the platform config and activation params. I think it will be useful to have validate_platform_configuration and validate_activation_params methods in L2ActivationOps instead.

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The set of comments from me 😅

mm2src/coins/lightning/ln_rpc.rs Show resolved Hide resolved
mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
if item.height > 0 && item.height <= current_height as i64 {
let header = match client
.blockchain_block_header(
item.height.try_into().expect("Convertion to u64 should not fail"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid that electrum can return a negative height in some implementation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand the negative height gets returned if the transaction is unconfirmed which is what's needed here. I also used blockchain.scripthash.get_history because this is the only electrum method that returns the height. Please correct me If I am wrong.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never know what kind of trash some external service may return :D

mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
@shamardy
Copy link
Collaborator Author

shamardy commented Dec 2, 2021

@artemii235 @sergeyboyko0791 this is ready for the next review iteration

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the last question for now 🙂

mm2src/coins/lightning/ln_utils.rs Show resolved Hide resolved
Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question in regards to the latest change.

mm2src/coins/lightning/ln_utils.rs Outdated Show resolved Hide resolved
artemii235
artemii235 previously approved these changes Dec 8, 2021
Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! @sergeyboyko0791 Could you please check that all your remarks are resolved too?

@shamardy
Copy link
Collaborator Author

shamardy commented Dec 9, 2021

@artemii235 @sergeyboyko0791 this is not ready for merge yet if the review fixes are accepted as I found a mistake here after I reviewed my code one more time https://github.com/KomodoPlatform/atomicDEX-API/blob/6ab6a976cf1841a2b88ea66a6025993458dc65e2/mm2src/coins/lightning/ln_utils.rs#L655
This is supposed to be the transaction index in the block, not the vout index. I will add a new electrum method to get the transaction index using the blockchain.transaction.get_merkle RPC method then use it to get the correct index.

Copy link
Member

@artemii235 artemii235 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

process_tx_for_unconfirmation(txid, filter.clone(), channel_manager.clone()).await;
}

for txid in chain_monitor_relevant_txids {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking comment: I guess you can use channel_manager_relevant_txids.chain(chain_monitor_relevant_txids) or something like this. Just for the next iteration :)

}

#[cfg(not(target_arch = "wasm32"))]
async fn process_txs_confirmations(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function looks complicated. Could you please refactor it at the next iteration?

}

#[cfg(not(target_arch = "wasm32"))]
async fn get_best_header(best_header_listener: &ElectrumClient) -> EnableLightningResult<ElectrumBlockHeader> {
Copy link

@sergeyboyko0791 sergeyboyko0791 Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a part of the ElectrumClient or a trait that is implemented by the client?

Err(e) => log::error!("{}", e.to_string()),
}

Timer::sleep(TRY_RECONNECTING_TO_NODE_INTERVAL as f64).await;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If MmArc is stopping, the context may live until this sleep ends. In the worst case, it's 60 seconds.
Please take MmWeak and drop MmArc before the sleep starts.

if item.height > 0 && item.height <= current_height as i64 {
let header = match client
.blockchain_block_header(
item.height.try_into().expect("Convertion to u64 should not fail"),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never know what kind of trash some external service may return :D

Copy link

@sergeyboyko0791 sergeyboyko0791 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a ton of work 🤪
I leave some non-blocking comments, could you please take them into account?
Only one thing I thing should be fixed: please avoid keeping MmArc during a Timer future is used :) #1162

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants