Skip to content

Commit 93ddbce

Browse files
authored
Allow Tokio Console support (#517)
* first iteration: add `tracing` feature of tokio, and the console-subscriber * update on tokio version, based on: tokio-rs/console#345 (comment) * use `tracing` library, instead of log. * remove unnecessary logs; add necessary logs; add timeout in `read_message_from_stellar` because it gets stuck * cleanup zombie task * Update README.md add documentation of the `tokio-console`. * remove duplicate `Parachain Block Listener` * https://github.com/pendulum-chain/spacewalk/pull/517/files#r1600267305 * #517 (comment), #517 (comment), * https://github.com/pendulum-chain/spacewalk/actions/runs/9094123182/job/24994504193?pr=517 * remocehttps://github.com/pendulum-chain/spacewalk/actions/runs/9095519314/job/24998905803?pr=517 * https://github.com/pendulum-chain/spacewalk/actions/runs/9096987912/job/25003758078?pr=517 * https://github.com/pendulum-chain/spacewalk/actions/runs/9098087121/job/25007476694?pr=517 * https://github.com/pendulum-chain/spacewalk/actions/runs/9108826563/job/25040418069?pr=517
1 parent 8331813 commit 93ddbce

29 files changed

+474
-141
lines changed

Cargo.lock

+268-20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/README.md

+38-1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,43 @@ If the transaction submission fails giving a `tx_failed` in the `result_codes` o
218218
due to the converted destination account not having trustlines set up for the redeemed asset.
219219
The destination account is derived automatically from the account that called the extrinsic on-chain.
220220

221+
### Debugging with `tokio-console`
222+
The vault is `tokio-console` ready, with the feature **_`allow-debugger`_**. _Remember to [set the rustflags](https://github.com/tokio-rs/console?tab=readme-ov-file#instrumenting-your-program)!_
223+
```
224+
RUSTFLAGS="--cfg tokio_unstable" cargo run --bin vault --features allow-debugger
225+
```
226+
[Install tokio-console](https://github.com/tokio-rs/console?tab=readme-ov-file#running-the-console)
227+
and connect to the vault.
228+
If using the [testchain](../testchain) and vault's `standalone-metadata` feature, you can use the command:
229+
```
230+
tokio-console http://127.0.0.1:6669
231+
```
232+
This will display:
233+
<img width="1138" alt="Screenshot 2024-05-13 at 6 33 33 PM" src="https://github.com/pendulum-chain/spacewalk/assets/2826165/6681a16c-84c0-47f5-abc6-e4ba6a7dc032">
234+
The multiple ` tokio::task clients/vault/src/system.rs ` tasks follows the tasks spawned consecutively, in [system.rs](https://github.com/pendulum-chain/spacewalk/blob/main/clients/vault/src/system.rs):
235+
236+
* The first 4 tasks are from `fn create_initial_tasks(...)` :
237+
* VaultId Registration Listener
238+
* Restart Timer
239+
* Stellar Transaction Listener
240+
* Parachain Block Listener
241+
* Next 5 tasks from `fn create_issue_tasks(...)` :
242+
* Issue Request Listener
243+
* Issue Cancel Listener
244+
* Issue Execute Listener
245+
* Issue Executor
246+
* Issue Cancel Scheduler
247+
* Next 4 tasks from `fn create_replace_tasks(...)` :
248+
* Request Replace Listener
249+
* Accept Replace Listener
250+
* Execute Replace Listener
251+
* Replace Cancellation Scheduler
252+
* Redeem Request Listener
253+
* The last 2 tasks from `create_bridge_metrics_tasks(...)` :
254+
* Bridge Metrics Listener
255+
* Bridge Metrics Poller
256+
257+
221258
## Notes on the implementation of subxt
222259

223260
This section is supposed to help when encountering issues with communication of vault client and parachain.
@@ -257,4 +294,4 @@ found [here](https://docs.rs/subxt-macro/latest/subxt_macro/#adding-derives-for-
257294
When the compiler complains about mismatched types although the types seem to be the same, you might have to use type
258295
substitutions.
259296
This is done by adding the `#[subxt(substitute_type = "some type")]` attribute to the metadata module.
260-
More documentation can be found [here](https://docs.rs/subxt-macro/latest/subxt_macro/#substituting-types).
297+
More documentation can be found [here](https://docs.rs/subxt-macro/latest/subxt_macro/#substituting-types).

clients/runner/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
[dependencies]
88
clap = { version = "4.0.17", features = ["derive"]}
99
hex = "0.4.3"
10-
tokio = { version = "1.8", features = ["rt-multi-thread", "macros", "time"] }
10+
tokio = { version = "1.37", features = ["rt-multi-thread", "macros", "time"] }
1111
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", "full", "bit-vec"] }
1212
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
1313
thiserror = "1.0.0"

clients/runtime/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ log = "0.4.0"
3131
serde = { version = "1.0.136", features = ["derive"] }
3232
serde_json = "1.0.71"
3333
thiserror = "1.0"
34-
tokio = { version = "1.0", features = ["full"] }
34+
tokio = { version = "1.37", features = ["full"] }
3535
prometheus = { version = "0.12.0", features = ["process"] }
3636
url = "2"
3737

clients/runtime/client/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ description = "Embed a substrate node into your subxt application."
1212
keywords = ["parity", "substrate", "blockchain"]
1313

1414
[dependencies]
15-
tokio = { version = "1.10", features = ["time", "rt-multi-thread"] }
15+
tokio = { version = "1.37", features = ["time", "rt-multi-thread"] }
1616
futures = { version = "0.3.9", features = ["compat"], package = "futures" }
1717
futures01 = { package = "futures", version = "0.1.29" }
1818
jsonrpsee = "0.16.0"

clients/service/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ thiserror = "1.0"
1212

1313
hyper = { version = "0.14.11" }
1414
hyper-tls = "0.5.0"
15-
tokio = { version = "1.0", features = ["full"] }
15+
tokio = { version = "1.37", features = ["full"] }
1616
warp = "0.3.2"
1717

1818
serde = { version = "1.0.136", features = ["derive"] }

clients/stellar-relay-lib/Cargo.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ wallet = { path = "../wallet", features = ["testing-utils"] }
1616

1717
[dependencies]
1818
hex = "0.4.3"
19-
log = {version = "0.4.14"}
19+
tracing = { version = "0.1", features = ["log"] }
2020

2121
base64 = "0.13.0"
2222
rand = "0.8.5"
@@ -34,11 +34,12 @@ substrate-stellar-sdk = {git = "https://github.com/pendulum-chain/substrate-stel
3434

3535
err-derive = "0.3.1"
3636

37-
tokio = { version = "1.0", features = [
37+
tokio = { version = "1.37", features = [
3838
"macros", # allows main function to be async
3939
"rt-multi-thread", # for multi-thread runtime
4040
"sync", # to make channels available
41-
"time" # for timeouts and sleep, when reconnecting
41+
"time", # for timeouts and sleep, when reconnecting
42+
"tracing" # for tokio console
4243
] }
4344
async-std = { version = "1.12.0", features = ["attributes"] }
4445

clients/stellar-relay-lib/examples/connect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
3737
ScpStatementPledges::ScpStExternalize(_) => "ScpStExternalize",
3838
ScpStatementPledges::ScpStNominate(_) => "ScpStNominate ",
3939
};
40-
log::info!(
40+
tracing::info!(
4141
"{} sent StellarMessage of type {} for ledger {}",
4242
node_id,
4343
stmt_type,

clients/stellar-relay-lib/src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl StellarOverlayConfig {
4545

4646
let public_key = secret_key.get_public().to_encoding();
4747
let public_key = std::str::from_utf8(&public_key).unwrap();
48-
log::info!(
48+
tracing::info!(
4949
"connection_info(): Connecting to Stellar overlay network using public key: {public_key}"
5050
);
5151

clients/stellar-relay-lib/src/connection/authentication/certificate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn create_auth_cert(
5353

5454
let signature: Signature = Signature::new(keypair.create_signature(raw_sig_data).to_vec())
5555
.map_err(|e| {
56-
log::error!("create_auth_cert(): {e:?}");
56+
tracing::error!("create_auth_cert(): {e:?}");
5757
Error::AuthSignatureFailed
5858
})?;
5959

@@ -87,7 +87,7 @@ pub fn verify_remote_auth_cert(
8787
match auth_cert_sig.try_into() {
8888
Ok(raw_sig) => remote_pub_key.verify_signature(raw_data, &raw_sig),
8989
Err(_) => {
90-
log::warn!(
90+
tracing::warn!(
9191
"failed to convert auth cert signature of size {} to fixed array of 64.",
9292
sig_len
9393
);

clients/stellar-relay-lib/src/connection/connector/connector.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use substrate_stellar_sdk::{
77
types::{AuthenticatedMessageV0, Curve25519Public, HmacSha256Mac, MessageType},
88
XdrCodec,
99
};
10+
use tracing::{error, trace};
1011

1112
use crate::{
1213
connection::{
@@ -86,18 +87,12 @@ impl Connector {
8687
body: &[u8],
8788
) -> Result<(), Error> {
8889
let remote_info = self.remote_info.as_ref().ok_or(Error::NoRemoteInfo)?;
89-
log::trace!(
90+
trace!(
9091
"verify_auth(): remote sequence: {}, auth message sequence: {}",
9192
remote_info.sequence(),
9293
auth_msg.sequence
9394
);
9495

95-
let auth_msg_xdr = auth_msg.to_base64_xdr();
96-
let auth_msg_xdr =
97-
String::from_utf8(auth_msg_xdr.clone()).unwrap_or(format!("{:?}", auth_msg_xdr));
98-
99-
log::debug!("verify_auth(): received auth message from Stellar Node: {auth_msg_xdr}");
100-
10196
if remote_info.sequence() != auth_msg.sequence {
10297
// must be handled on main thread because workers could mix up order of messages.
10398
return Err(Error::InvalidSequenceNumber)
@@ -169,7 +164,7 @@ impl Connector {
169164

170165
pub fn stop(&mut self) {
171166
if let Err(e) = self.tcp_stream.shutdown(Shutdown::Both) {
172-
log::error!("stop(): failed to shutdown tcp stream: {}", e);
167+
error!("stop(): failed to shutdown tcp stream: {}", e);
173168
}
174169
}
175170
}

clients/stellar-relay-lib/src/connection/connector/message_handler.rs

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use substrate_stellar_sdk::{
2-
types::{ErrorCode, Hello, MessageType, StellarMessage},
3-
XdrCodec,
4-
};
5-
61
use crate::connection::{
72
authentication::verify_remote_auth_cert,
83
helper::{error_to_string, time_now},
94
hmac::HMacKeys,
105
xdr_converter::parse_authenticated_message,
116
Connector, Error, Xdr,
127
};
8+
use substrate_stellar_sdk::{
9+
types::{ErrorCode, Hello, MessageType, StellarMessage},
10+
XdrCodec,
11+
};
12+
use tracing::{error, info, trace, warn};
1313

1414
use crate::node::RemoteInfo;
1515

@@ -33,13 +33,13 @@ impl Connector {
3333

3434
MessageType::ErrorMsg => match auth_msg.message {
3535
StellarMessage::ErrorMsg(e) => {
36-
log::error!(
36+
error!(
3737
"process_raw_message(): Received ErrorMsg during authentication: {}",
3838
error_to_string(e.clone())
3939
);
4040
return Err(Error::from(e))
4141
},
42-
other => log::error!(
42+
other => error!(
4343
"process_raw_message(): Received ErrorMsg during authentication: {:?}",
4444
other
4545
),
@@ -50,9 +50,7 @@ impl Connector {
5050
if self.is_handshake_created() {
5151
self.verify_auth(&auth_msg, &data[4..(data.len() - 32)])?;
5252
self.increment_remote_sequence()?;
53-
log::trace!(
54-
"process_raw_message(): Processing {msg_type:?} message: auth verified"
55-
);
53+
trace!("process_raw_message(): Processing {msg_type:?} message: auth verified");
5654
}
5755

5856
return self.process_stellar_message(auth_msg.message, msg_type).await
@@ -80,29 +78,23 @@ impl Connector {
8078
} else {
8179
self.send_auth_message().await?;
8280
}
83-
log::info!("process_stellar_message(): Hello message processed successfully");
81+
info!("process_stellar_message(): Hello message processed successfully");
8482
},
8583

8684
StellarMessage::Auth(_) => {
8785
self.process_auth_message().await?;
8886
},
8987

9088
StellarMessage::ErrorMsg(e) => {
91-
log::error!(
92-
"process_stellar_message(): Received ErrorMsg during authentication: {e:?}"
93-
);
89+
error!("process_stellar_message(): Received ErrorMsg during authentication: {e:?}");
9490
if e.code == ErrorCode::ErrConf || e.code == ErrorCode::ErrAuth {
9591
return Err(Error::from(e))
9692
}
9793
return Ok(Some(StellarMessage::ErrorMsg(e)))
9894
},
9995

96+
// we do not handle other messages. Return to caller
10097
other => {
101-
log::trace!(
102-
"process_stellar_message(): Processing {} message: received from overlay",
103-
String::from_utf8(other.to_base64_xdr())
104-
.unwrap_or(format!("{:?}", other.to_base64_xdr()))
105-
);
10698
self.check_to_send_more(msg_type).await?;
10799
return Ok(Some(other))
108100
},
@@ -124,7 +116,7 @@ impl Connector {
124116
remote.node().overlay_version,
125117
);
126118
} else {
127-
log::warn!("process_auth_message(): No remote overlay version after handshake.");
119+
warn!("process_auth_message(): No remote overlay version after handshake.");
128120
}
129121

130122
self.check_to_send_more(MessageType::Auth).await

0 commit comments

Comments
 (0)