-
Notifications
You must be signed in to change notification settings - Fork 31
Feature add state api #63
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
base: master
Are you sure you want to change the base?
Changes from all commits
ab846c6
7403dcd
7e69d1a
aa20bcc
26a48aa
fcaa324
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -791,6 +791,19 @@ | |||||
| pub(crate) network_channels: usize, | ||||||
| } | ||||||
|
|
||||||
| #[derive(Deserialize, Serialize)] | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| pub(crate) enum NodeState { | ||||||
| None, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
please rename also in openapi |
||||||
| Locked, | ||||||
| Running, | ||||||
| Changing, | ||||||
| } | ||||||
|
|
||||||
| #[derive(Deserialize, Serialize)] | ||||||
| pub(crate) struct NodeStateResponse { | ||||||
| pub(crate) state: NodeState, | ||||||
| } | ||||||
|
|
||||||
| #[derive(Deserialize, Serialize)] | ||||||
| pub(crate) struct OpenChannelRequest { | ||||||
| pub(crate) peer_pubkey_and_opt_addr: String, | ||||||
|
|
@@ -1137,7 +1150,7 @@ | |||||
| } | ||||||
|
|
||||||
| async fn check_locked( | ||||||
| &self, | ||||||
|
Check warning on line 1153 in src/routes.rs
|
||||||
| ) -> Result<TokioMutexGuard<Option<Arc<UnlockedAppState>>>, APIError> { | ||||||
| let unlocked_app_state = self.get_unlocked_app_state().await; | ||||||
| if unlocked_app_state.is_some() { | ||||||
|
|
@@ -1149,7 +1162,7 @@ | |||||
| } | ||||||
|
|
||||||
| async fn check_unlocked( | ||||||
| &self, | ||||||
|
Check warning on line 1165 in src/routes.rs
|
||||||
| ) -> Result<TokioMutexGuard<Option<Arc<UnlockedAppState>>>, APIError> { | ||||||
| let unlocked_app_state = self.get_unlocked_app_state().await; | ||||||
| if unlocked_app_state.is_none() { | ||||||
|
|
@@ -2821,6 +2834,33 @@ | |||||
| })) | ||||||
| } | ||||||
|
|
||||||
| pub(crate) async fn node_state( | ||||||
| State(state): State<Arc<AppState>>, | ||||||
| ) -> Result<Json<NodeStateResponse>, APIError> { | ||||||
| let mnemonic_path = get_mnemonic_path(&state.static_state.storage_dir_path); | ||||||
| if check_already_initialized(&mnemonic_path).is_ok() { | ||||||
| return Ok(Json(NodeStateResponse { | ||||||
| state: NodeState::None, | ||||||
| })); | ||||||
| } | ||||||
|
|
||||||
| if *state.get_changing_state() { | ||||||
| return Ok(Json(NodeStateResponse { | ||||||
| state: NodeState::Changing, | ||||||
| })); | ||||||
| } | ||||||
|
|
||||||
| if (state.check_locked().await).is_ok() { | ||||||
| return Ok(Json(NodeStateResponse { | ||||||
| state: NodeState::Locked, | ||||||
| })); | ||||||
| } | ||||||
|
|
||||||
| Ok(Json(NodeStateResponse { | ||||||
| state: NodeState::Running, | ||||||
| })) | ||||||
| } | ||||||
|
|
||||||
| pub(crate) async fn open_channel( | ||||||
| State(state): State<Arc<AppState>>, | ||||||
| WithRejection(Json(payload), _): WithRejection<Json<OpenChannelRequest>, APIError>, | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -32,11 +32,11 @@ use crate::routes::{ | |||||
| ListPaymentsResponse, ListPeersResponse, ListSwapsResponse, ListTransactionsRequest, | ||||||
| ListTransactionsResponse, ListTransfersRequest, ListTransfersResponse, ListUnspentsRequest, | ||||||
| ListUnspentsResponse, MakerExecuteRequest, MakerInitRequest, MakerInitResponse, | ||||||
| NetworkInfoResponse, NodeInfoResponse, OpenChannelRequest, OpenChannelResponse, Payment, Peer, | ||||||
| PostAssetMediaResponse, RefreshRequest, RestoreRequest, RgbInvoiceRequest, RgbInvoiceResponse, | ||||||
| SendAssetRequest, SendAssetResponse, SendBtcRequest, SendBtcResponse, SendPaymentRequest, | ||||||
| SendPaymentResponse, Swap, SwapStatus, TakerRequest, Transaction, Transfer, UnlockRequest, | ||||||
| Unspent, | ||||||
| NetworkInfoResponse, NodeInfoResponse, NodeStateResponse, OpenChannelRequest, | ||||||
| OpenChannelResponse, Payment, Peer, PostAssetMediaResponse, RefreshRequest, RestoreRequest, | ||||||
| RgbInvoiceRequest, RgbInvoiceResponse, SendAssetRequest, SendAssetResponse, SendBtcRequest, | ||||||
| SendBtcResponse, SendPaymentRequest, SendPaymentResponse, Swap, SwapStatus, TakerRequest, | ||||||
| Transaction, Transfer, UnlockRequest, Unspent, | ||||||
| }; | ||||||
| use crate::utils::{hex_str_to_vec, ELECTRUM_URL_REGTEST, PROXY_ENDPOINT_LOCAL}; | ||||||
|
|
||||||
|
|
@@ -1019,6 +1019,20 @@ async fn node_info(node_address: SocketAddr) -> NodeInfoResponse { | |||||
| .unwrap() | ||||||
| } | ||||||
|
|
||||||
| async fn node_state(node_address: SocketAddr) -> NodeStateResponse { | ||||||
| println!("getting node state for {node_address}"); | ||||||
| let res = reqwest::Client::new() | ||||||
| .get(format!("http://{}/nodestate", node_address)) | ||||||
| .send() | ||||||
| .await | ||||||
| .unwrap(); | ||||||
| _check_response_is_ok(res) | ||||||
| .await | ||||||
| .json::<NodeStateResponse>() | ||||||
| .await | ||||||
| .unwrap() | ||||||
| } | ||||||
|
|
||||||
| async fn open_channel( | ||||||
| node_address: SocketAddr, | ||||||
| dest_peer_pubkey: &str, | ||||||
|
|
@@ -1676,6 +1690,7 @@ mod issue; | |||||
| mod lock_unlock_changepassword; | ||||||
| mod multi_hop; | ||||||
| mod multi_open_close; | ||||||
| mod node_state_test; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| mod open_after_double_send; | ||||||
| mod openchannel_fail; | ||||||
| mod openchannel_optional_addr; | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,62 @@ | ||||||||||||||||||||||||||||||||||||||||||
| use crate::routes::NodeState; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const TEST_DIR_BASE: &str = "tmp/node_state_test/"; | ||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| #[serial_test::serial] | ||||||||||||||||||||||||||||||||||||||||||
| #[tokio::test(flavor = "multi_thread", worker_threads = 1)] | ||||||||||||||||||||||||||||||||||||||||||
| #[traced_test] | ||||||||||||||||||||||||||||||||||||||||||
| async fn success() { | ||||||||||||||||||||||||||||||||||||||||||
| initialize(); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let test_dir_node1 = format!("{TEST_DIR_BASE}node1"); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if std::path::Path::new(&test_dir_node1).exists() { | ||||||||||||||||||||||||||||||||||||||||||
| std::fs::remove_dir_all(&test_dir_node1).unwrap(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let node1_addr = start_daemon(&test_dir_node1, NODE1_PEER_PORT).await; | ||||||||||||||||||||||||||||||||||||||||||
| let state_response = node_state(node1_addr).await; | ||||||||||||||||||||||||||||||||||||||||||
| assert!(matches!(state_response.state, NodeState::None)); | ||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let password = format!("{test_dir_node1}.{NODE1_PEER_PORT}"); | ||||||||||||||||||||||||||||||||||||||||||
| let payload = InitRequest { | ||||||||||||||||||||||||||||||||||||||||||
| password: password.clone(), | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
| let res = reqwest::Client::new() | ||||||||||||||||||||||||||||||||||||||||||
| .post(format!("http://{}/init", node1_addr)) | ||||||||||||||||||||||||||||||||||||||||||
| .json(&payload) | ||||||||||||||||||||||||||||||||||||||||||
| .send() | ||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||
| _check_response_is_ok(res) | ||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||
| .json::<InitResponse>() | ||||||||||||||||||||||||||||||||||||||||||
| .await | ||||||||||||||||||||||||||||||||||||||||||
| .unwrap(); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let state_response = node_state(node1_addr).await; | ||||||||||||||||||||||||||||||||||||||||||
| assert!(matches!(state_response.state, NodeState::Locked)); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| unlock(node1_addr, &password).await; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let state_response = node_state(node1_addr).await; | ||||||||||||||||||||||||||||||||||||||||||
| assert!(matches!(state_response.state, NodeState::Running)); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| lock(node1_addr).await; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let state_response = node_state(node1_addr).await; | ||||||||||||||||||||||||||||||||||||||||||
| assert!(matches!(state_response.state, NodeState::Locked)); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| unlock(node1_addr, &password).await; | ||||||||||||||||||||||||||||||||||||||||||
| let state_response = node_state(node1_addr).await; | ||||||||||||||||||||||||||||||||||||||||||
| assert!(matches!(state_response.state, NodeState::Running)); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| let node_info_response = node_info(node1_addr).await; | ||||||||||||||||||||||||||||||||||||||||||
| assert!(!node_info_response.pubkey.is_empty()); | ||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(node_info_response.num_channels, 0); | ||||||||||||||||||||||||||||||||||||||||||
| assert_eq!(node_info_response.num_peers, 0); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| println!("Node state test completed successfully"); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+61
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
names seem sufficiently descriptive