Skip to content

Commit

Permalink
test(sdk): generate test vectors using testnet (#2381)
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek authored Dec 16, 2024
1 parent 0ff6b27 commit 4c203e4
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 5 deletions.
9 changes: 8 additions & 1 deletion packages/rs-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ cargo test -p dash-sdk --no-default-features --features network-testing
## Offline Testing

Offline testing uses the vectors generated using `packages/rs-sdk/scripts/generate_test_vectors.sh` script.
These vectors must be saved in `packages/rs-sdk/tests/vectors`.
This script will connect to node defined in `packages/rs-sdk/tests/.env`, execute all tests against it and
update test vectors in `packages/rs-sdk/tests/vectors`.

To generate test vectors against a testnet node (or other remote node), you can use helper script
`packages/rs-sdk/scripts/connect_to_remote.sh` which will generate `.env` file for you and tunnel connection to Dash
Core RPC on the remote host.

Refer to rich comments / help in the forementioned scripts for more details.

### Generating test vectors

Expand Down
91 changes: 91 additions & 0 deletions packages/rs-sdk/scripts/connect_to_remote.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#! /bin/bash

set -e -o pipefail

CORE_USER=dashmate

function usage() {

echo This script connects to a remote node and runs the SDK tests on it.
echo It establishes SSH tunnel to the remote node and uses DAPI and Core RPC
echo services running on the remote node to run the tests.
echo
echo Note: you can use
echo
echo "Usage: $0 <remote-user@remote-node> [ssh arguments]"
echo " remote-user: The username to use for SSH connection."
echo " remote-node: The IP address or hostname of the remote node."
echo " ssh arguments: Additional arguments to pass to the SSH command."
echo
echo "Example: $0 [email protected] sudo -u dashmate"
exit 1
}

if [ "$#" -lt 1 ]; then
usage
fi

REMOTE="$1"

# Validate remote format
if ! echo "$REMOTE" | grep -q "@"; then
echo "Error: Remote must be in format user@host"
usage
fi

# REMOTE_USER=$(echo "$1" | cut -d@ -f1)
REMOTE_NODE=$(echo "$1" | cut -d@ -f2)

SSH_ARGS=${*:2}

echo -n Reading configuration from remote "$REMOTE" using \`dashmate config get\` command...
# read core username and password from the remote node
CORE_PORT=$(ssh "$REMOTE" ${SSH_ARGS} dashmate config get core.rpc.port)
CORE_PASSWORD=$(ssh "$REMOTE" ${SSH_ARGS} dashmate config get "core.rpc.users.${CORE_USER}.password")

DAPI_PORT=$(ssh "$REMOTE" ${SSH_ARGS} dashmate config get platform.gateway.listeners.dapiAndDrive.port)
DAPI_SSL_ENABLED=$(ssh "$REMOTE" ${SSH_ARGS} dashmate config get platform.gateway.ssl.enabled)

echo " done."

# Double check that the variables are set
if [ -z "$CORE_PORT" ] || [ -z "$CORE_PASSWORD" ] || [ -z "$DAPI_PORT" ] || [ -z "$DAPI_SSL_ENABLED" ]; then
echo "Failed to read configuration from the remote node."
exit 1
fi

ENV_FILE="$(realpath "$0" | xargs dirname)/../tests/.env"
echo -n Generating "${ENV_FILE}" file for the SDK tests...

cat >"${ENV_FILE}" <<EOF
# Configuration of tests and examples, generated by $(realpath "$0")
DASH_SDK_PLATFORM_HOST="${REMOTE_NODE}"
DASH_SDK_PLATFORM_PORT=${DAPI_PORT}
DASH_SDK_PLATFORM_SSL=${DAPI_SSL_ENABLED}
DASH_SDK_CORE_HOST=127.0.0.1
DASH_SDK_CORE_PORT=12367
DASH_SDK_CORE_USER="${CORE_USER}"
DASH_SDK_CORE_PASSWORD="${CORE_PASSWORD}"
EOF

trap 'rm -f "${ENV_FILE}"' INT TERM EXIT # cleanup

chmod 600 "${ENV_FILE}"

echo " done."
echo
echo "Configured DAPI endpoint: ${REMOTE_NODE}:${DAPI_PORT}, SSL: ${DAPI_SSL_ENABLED}"
echo
echo "Establishing SSH tunnel to the remote node ${REMOTE_NODE} to forward Dash Core RPC:"
echo " 127.0.0.1:12367-> ${REMOTE_NODE}:${CORE_PORT}"
echo
echo "To stop the tunnel, press Ctrl+C."

ssh -L "12367:127.0.0.1:${CORE_PORT}" "$REMOTE" \
-o ServerAliveInterval=60 \
-o ServerAliveCountMax=3 \
-o ExitOnForwardFailure=yes \
-N ${SSH_ARGS}
3 changes: 2 additions & 1 deletion packages/rs-sdk/scripts/generate_test_vectors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
# its test vector is generated.
#
# Otherwise, all existing test vectors are removed and regenerated.

#
# HINT: You can use `connect_to_remote.sh` script to use some remote node (like testnet) to generate test vectors.
CARGO_DIR="$(realpath "$(dirname "$0")/..")"

pushd "$CARGO_DIR"
Expand Down
2 changes: 2 additions & 0 deletions packages/rs-sdk/tests/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ DASH_SDK_PLATFORM_SSL=false
# ProTxHash of masternode that has at least 1 vote casted for DPNS name `testname`
DASH_SDK_MASTERNODE_OWNER_PRO_REG_TX_HASH="6ac88f64622d9bc0cb79ad0f69657aa9488b213157d20ae0ca371fa5f04fb222"

# Dash Core host to use for RPC. Defaults to DASH_SDK_PLATFORM_HOST.
DASH_SDK_CORE_HOST=127.0.0.1
DASH_SDK_CORE_PORT=20002
DASH_SDK_CORE_USER="someuser"
DASH_SDK_CORE_PASSWORD="verysecretpassword"
10 changes: 8 additions & 2 deletions packages/rs-sdk/tests/fetch/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use dpp::{
};
use rs_dapi_client::{Address, AddressList};
use serde::Deserialize;
use std::{path::PathBuf, str::FromStr};
use std::path::PathBuf;
use zeroize::Zeroizing;

/// Existing document ID
Expand All @@ -36,6 +36,11 @@ pub struct Config {
/// Port of the Dash Platform node grpc interface
#[serde(default)]
pub platform_port: u16,
/// Host of the Dash Core RPC interface running on the Dash Platform node.
/// Defaults to the same as [platform_host](Config::platform_host).
#[serde(default)]
#[cfg_attr(not(feature = "network-testing"), allow(unused))]
pub core_host: Option<String>,
/// Port of the Dash Core RPC interface running on the Dash Platform node
#[serde(default)]
pub core_port: u16,
Expand Down Expand Up @@ -180,9 +185,10 @@ impl Config {
// offline testing takes precedence over network testing
#[cfg(all(feature = "network-testing", not(feature = "offline-testing")))]
let sdk = {
let core_host = self.core_host.as_ref().unwrap_or(&self.platform_host);
// Dump all traffic to disk
let builder = dash_sdk::SdkBuilder::new(self.address_list()).with_core(
&self.platform_host,
core_host,
self.core_port,
&self.core_user,
&self.core_password,
Expand Down
1 change: 0 additions & 1 deletion packages/rs-sdk/tests/fetch/evonode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use super::{common::setup_logs, config::Config};
use dash_sdk::platform::{types::evonode::EvoNode, FetchUnproved};
use dpp::dashcore::{hashes::Hash, ProTxHash};
use drive_proof_verifier::types::EvoNodeStatus;
use http::Uri;
use rs_dapi_client::Address;
use std::time::Duration;
/// Given some existing evonode URIs, WHEN we connect to them, THEN we get status.
Expand Down

0 comments on commit 4c203e4

Please sign in to comment.