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

401 fix issues of connecting to stellar overlay network on testnet #409

2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ members = [
"clients/wallet",
"clients/service",
"clients/stellar-relay-lib",
"pallets/clients-info",
"pallets/currency",
"pallets/fee",
"pallets/nomination",
Expand Down Expand Up @@ -57,7 +56,6 @@ pallet-aura = { git = "https://github.com/paritytech//substrate", branch = "polk
pallet-authorship = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.40" }
pallet-balances = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.40" }
pallet-grandpa = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.40" }
pallet-session = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.40" }
pallet-sudo = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.40" }
pallet-timestamp = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.40" }
pallet-transaction-payment = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.40" }
Expand Down
2 changes: 1 addition & 1 deletion clients/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ testchain = { package = "spacewalk-standalone", path = "../../testchain/node", o
testchain-runtime = { package = "spacewalk-runtime-standalone", path = "../../testchain/runtime", optional = true }

# Substrate Stellar Dependencies
substrate-stellar-sdk = { git = "https://github.com/pendulum-chain/substrate-stellar-sdk", branch = "polkadot-v0.9.40" }
substrate-stellar-sdk = { git = "https://github.com/pendulum-chain/substrate-stellar-sdk", branch = "401-spacewalk-fix-issues-of-connecting-to-stellar-overlay-network-on-testnet" }
wallet = { path = "../wallet" }


Expand Down
2 changes: 1 addition & 1 deletion clients/runtime/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ thiserror = "1.0.23"

subxt = "0.25.0"

sc-client-db = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sc-client-db = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sc-network = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sc-service = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
Binary file modified clients/runtime/metadata-parachain-amplitude.scale
Binary file not shown.
Binary file modified clients/runtime/metadata-standalone.scale
Binary file not shown.
10 changes: 7 additions & 3 deletions clients/stellar-relay-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ serde = { version = "1.0.152", features = ["alloc"] }
serde_json = "1.0.93"
serde_with = "2.2.0"


substrate-stellar-sdk = {git = "https://github.com/pendulum-chain/substrate-stellar-sdk", branch = "polkadot-v0.9.40", default-features = false, features = ['all-types']}
substrate-stellar-sdk = {git = "https://github.com/pendulum-chain/substrate-stellar-sdk", branch = "401-spacewalk-fix-issues-of-connecting-to-stellar-overlay-network-on-testnet", default-features = false, features = ['all-types']}

err-derive = "0.3.1"

Expand All @@ -43,4 +42,9 @@ tokio = { version = "1.0", features = [
] }

[features]
default = []
std = [
"substrate-stellar-sdk/std"
]
default = [
"std"
]
4 changes: 2 additions & 2 deletions clients/stellar-relay-lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ The `StellarOverlayConfig` is a configuration to connect to the Stellar Node. It
* `ConnectionInfoCfg`
* `NodeInfoCfg`.

The `NodeInfoCfg` contains the information of the Stellar Node to connect to. Except the address and the port.
The `NodeInfoCfg` contains the Stellar Node information to connect to, except the address and the port.
```rust
pub struct NodeInfoCfg {
pub ledger_version: u32,
Expand Down Expand Up @@ -74,7 +74,7 @@ The `StellarOverlayConnection` has 2 async methods to interact with the Stellar
The `StellarRelayMessage` is an enum with the following variants:
* _`Connect`_ -> interprets a successful connection to Stellar Node. It contains the `PublicKey` and the `NodeInfo`
* _`Data`_ -> a wrapper of a `StellarMessage` and additional fields: the _message type_ and the unique `p_id`(process id)
* _`Timeout`_ -> Depends on the `timeout_in_secs` and `retries` defined in the `ConnectionInfo` (**10** and **3** by default). This message is returned after multiple retries have been done.
* _`Timeout`_ -> Depends on the `timeout_in_secs` and `retries` defined in the `ConnectionInfoCfg` (**10** and **3** by default). This message is returned after multiple retries have been done.
For example, Stellar Relay will wait for 10 seconds to read from the existing tcp stream before retrying again. After the 3rd retry, StellarRelay will create a new stream in 3 attempts, with an interval of 3 seconds.
* _`Error`_ -> a todo

Expand Down
48 changes: 44 additions & 4 deletions clients/stellar-relay-lib/examples/connect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
use stellar_relay_lib::{
connect_to_stellar_overlay_network,
sdk::types::{ScpStatementPledges, StellarMessage},
StellarOverlayConfig, StellarRelayMessage,
ConnectorActions, StellarOverlayConfig, StellarRelayMessage,
};
use substrate_stellar_sdk::{
types::{MessageType, ScpStatementExternalize},

Check warning on line 7 in clients/stellar-relay-lib/examples/connect.rs

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

unused imports: `IntoHash`, `MessageType`
Hash, IntoHash, XdrCodec,
};

//arrange
fn get_tx_set_hash(x: &ScpStatementExternalize) -> Hash {
let scp_value = x.commit.value.get_vec();
scp_value[0..32].try_into().unwrap()
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -13,7 +23,7 @@

let (cfg_file_path, sk_file_path) = if arg_network == "mainnet" {
(
"./clients/stellar-relay-lib/resources/config/mainnet/stellar_relay_config_iowa.json",
"./clients/stellar-relay-lib/resources/config/mainnet/stellar_relay_config_mainnet_iowa.json",
"./clients/stellar-relay-lib/resources/secretkey/stellar_secretkey_mainnet",
)
} else {
Expand All @@ -27,6 +37,8 @@

let mut overlay_connection = connect_to_stellar_overlay_network(cfg, &secret_key).await?;

let mut is_sent = false;
let actions_sender = overlay_connection.get_actions_sender();
while let Some(relay_message) = overlay_connection.listen().await {
match relay_message {
StellarRelayMessage::Connect { pub_key, node_info } => {
Expand All @@ -44,7 +56,25 @@
let stmt_type = match msg.statement.pledges {
ScpStatementPledges::ScpStPrepare(_) => "ScpStPrepare",
ScpStatementPledges::ScpStConfirm(_) => "ScpStConfirm",
ScpStatementPledges::ScpStExternalize(_) => "ScpStExternalize",
ScpStatementPledges::ScpStExternalize(stmt) => {
if !is_sent {
let txset_hash = get_tx_set_hash(&stmt);
println!(
"Found ScpStExternalize! sending txsethash: {}",
hex::encode(txset_hash)
);
actions_sender
.send(ConnectorActions::SendMessage(Box::new(
StellarMessage::GetTxSet(txset_hash),
)))
.await
.expect("should be able to send message");

is_sent = true;
}

"ScpStExternalize"
},
ScpStatementPledges::ScpStNominate(_) => "ScpStNominate ",
};
log::info!(
Expand All @@ -54,8 +84,18 @@
slot
);
},
StellarMessage::GeneralizedTxSet(set) => {
println!("CARLA CARLA CARLA set: {:?}", set.to_base64_encoded_xdr_string());
},
StellarMessage::TxSet(set) => {
let x = set.to_base64_xdr();
println!(
"CARLA CARLA CARLA CARLA!!! set txset!!!!!: {:?}",
std::str::from_utf8(&x)
);
},
_ => {
log::info!("rcv StellarMessage of type: {:?}", msg_type);
println!("the type: {:?}", msg_type)
},
},
StellarRelayMessage::Error(e) => {
Expand Down
25 changes: 5 additions & 20 deletions clients/stellar-relay-lib/src/connection/helper.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use rand::Rng;
use sha2::{Digest, Sha256};
use std::time::{SystemTime, UNIX_EPOCH};
use substrate_stellar_sdk::{
types::{TransactionSet, Uint256},
SecretKey, XdrCodec,
};
use substrate_stellar_sdk::{types::Uint256, SecretKey};

/// a helpful macro to unwrap an `Ok` or return immediately.
/// a helpful macro to log an error (if it occurs) and return immediately.
macro_rules! log_error {
// expression, return value, extra log
($res:expr, $log:expr) => {
$res.map_err(|e| {
if let Err(e) = $res {
log::error!("{:?}: {e:?}", $log);
e
})?;
return
}
};
}

Expand Down Expand Up @@ -41,15 +38,3 @@ pub fn time_now() -> u64 {
u64::MAX
})
}

//todo: this has to be moved somewhere.
pub fn compute_non_generic_tx_set_content_hash(tx_set: &TransactionSet) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(tx_set.previous_ledger_hash);

tx_set.txes.get_vec().iter().for_each(|envlp| {
hasher.update(envlp.to_xdr());
});

hasher.finalize().as_slice().try_into().unwrap()
}
3 changes: 2 additions & 1 deletion clients/stellar-relay-lib/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ mod overlay_connection;
mod services;
pub mod xdr_converter;

pub(crate) use connector::*;
pub(crate) use connector::Connector;
pub use connector::ConnectorActions;
pub use error::Error;
pub use helper::*;
pub use overlay_connection::*;
Expand Down
56 changes: 27 additions & 29 deletions clients/stellar-relay-lib/src/connection/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub(crate) async fn receiving_service(
actions_sender: mpsc::Sender<ConnectorActions>,
timeout_in_secs: u64,
retries: u8,
) -> Result<(), Error> {
) {
let mut retry = 0;
let mut retry_read = 0;
let mut proc_id = 0;
Expand All @@ -171,9 +171,8 @@ pub(crate) async fn receiving_service(
{
Ok(Ok(0)) => {
if retry_read >= retries {
return Err(Error::ReadFailed(format!(
"Failed to read messages from the stream. Received 0 size more than {retries} times"
)))
log::error!("proc_id: {proc_id}. Failed to read messages from the stream. Received 0 size more than {retries} times");
return
}
retry_read += 1;
},
Expand Down Expand Up @@ -213,18 +212,20 @@ pub(crate) async fn receiving_service(
retry = 0;
retry_read = 0;
// let's read the continuation number of bytes from the previous message.
read_unfinished_message(
&mut r_stream,
&actions_sender,
&mut lack_bytes_from_prev,
&mut proc_id,
&mut readbuf,
)
.await?;
log_error!(
read_unfinished_message(
&mut r_stream,
&actions_sender,
&mut lack_bytes_from_prev,
&mut proc_id,
&mut readbuf,
).await,
format!("proc_id:{proc_id}. Error occurred while reading unfinished stellar message")
);
},
Ok(Err(e)) => {
log::error!("proc_id: {proc_id}. Error occurred while reading the stream: {e:?}");
return Err(Error::ConnectionFailed(e.to_string()))
return
},
Err(elapsed) => {
log::error!(
Expand All @@ -233,9 +234,8 @@ pub(crate) async fn receiving_service(
);

if retry >= retries {
return Err(Error::ConnectionFailed(
"TIMED OUT reading for messages from the stream".to_string(),
))
log::error!("proc_id: {proc_id}. Exhausted maximum retries for reading messages from Stellar Node.");
return
}
retry += 1;
},
Expand Down Expand Up @@ -285,21 +285,19 @@ pub(crate) async fn connection_handler(
mut connector: Connector,
mut actions_receiver: mpsc::Receiver<ConnectorActions>,
mut w_stream: tcp::OwnedWriteHalf,
) -> Result<(), Error> {
) {
let mut timeout_counter = 0;
loop {
match timeout(Duration::from_secs(connector.timeout_in_secs), actions_receiver.recv()).await
{
Ok(Some(ConnectorActions::Disconnect)) => {
w_stream.shutdown().await.map_err(|e| {
log::error!("Failed to shutdown write half of stream: {:?}", e);

Error::ConnectionFailed("Failed to disconnect tcp stream".to_string())
})?;

log_error!(
w_stream.shutdown().await,
format!("Failed to shutdown write half of stream:")
);
drop(connector);
drop(actions_receiver);
return Ok(())
return
},

Ok(Some(action)) => {
Expand All @@ -317,11 +315,11 @@ pub(crate) async fn connection_handler(
Err(elapsed) => {
log::error!("Connection timed out after {} seconds", elapsed.to_string());
if timeout_counter >= connector.retries {
connector.send_to_user(StellarRelayMessage::Timeout).await?;
return Err(Error::ConnectionFailed(format!(
"Timed out! elapsed time: {:?}",
elapsed.to_string()
)))
log_error!(
connector.send_to_user(StellarRelayMessage::Timeout).await,
format!("Connection Timed out:")
);
return
}
timeout_counter += 1;
},
Expand Down
1 change: 1 addition & 0 deletions clients/stellar-relay-lib/src/connection/xdr_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub(crate) fn from_authenticated_message(message: &AuthenticatedMessage) -> Resu
message_to_bytes(message)
}

// todo: move to substrate-stellar-sdk
/// To easily convert any bytes to a Stellar type.
///
/// # Examples
Expand Down
6 changes: 4 additions & 2 deletions clients/stellar-relay-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ mod tests;

pub(crate) use connection::{
handshake::{self, HandshakeState},
ConnectionInfo, Connector, ConnectorActions,
ConnectionInfo, Connector,
};
pub use connection::{
helper, xdr_converter, ConnectorActions, Error, StellarOverlayConnection, StellarRelayMessage,
};
pub use connection::{helper, xdr_converter, Error, StellarOverlayConnection, StellarRelayMessage};

pub use substrate_stellar_sdk as sdk;

Expand Down
Loading
Loading