Skip to content

Commit 9fce759

Browse files
committed
fix: fix nim interop env setup file
1 parent 2279c1f commit 9fce759

File tree

2 files changed

+139
-130
lines changed

2 files changed

+139
-130
lines changed
Lines changed: 108 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,108 @@
1-
#!/usr/bin/env bash
2-
set -euo pipefail
3-
4-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5-
PROJECT_DIR="${SCRIPT_DIR}/.."
6-
7-
# Colors
8-
GREEN='\033[0;32m'
9-
RED='\033[0;31m'
10-
NC='\033[0m'
11-
12-
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
13-
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
14-
15-
main() {
16-
log_info "Setting up nim echo server for interop testing..."
17-
18-
# Check if nim is available
19-
if ! command -v nim &> /dev/null || ! command -v nimble &> /dev/null; then
20-
log_error "Nim not found. Please install nim first."
21-
exit 1
22-
fi
23-
24-
cd "${PROJECT_DIR}"
25-
26-
# Create logs directory
27-
mkdir -p logs
28-
29-
# Check if binary already exists
30-
if [[ -f "nim_echo_server" ]]; then
31-
log_info "nim_echo_server already exists, skipping build"
32-
return 0
33-
fi
34-
35-
log_info "Installing nim-libp2p globally..."
36-
# Install libp2p globally so nim can find it
37-
nimble install -y libp2p
38-
39-
log_info "Building nim echo server..."
40-
# Compile the echo server
41-
nim c \
42-
-d:release \
43-
-d:chronicles_log_level=INFO \
44-
--opt:speed \
45-
-d:libp2p_quic_support \
46-
-d:ssl \
47-
--mm:orc \
48-
-o:nim_echo_server \
49-
nim_echo_server.nim
50-
51-
# Verify binary was created
52-
if [[ -f "nim_echo_server" ]]; then
53-
log_info "✅ nim_echo_server built successfully"
54-
else
55-
log_error "❌ Failed to build nim_echo_server"
56-
exit 1
57-
fi
58-
59-
log_info "🎉 Setup complete!"
60-
}
61-
62-
main "$@"
1+
{.used.}
2+
3+
import chronos
4+
import stew/byteutils
5+
import libp2p
6+
7+
##
8+
# Simple Echo Protocol Implementation for py-libp2p Interop Testing
9+
##
10+
const EchoCodec = "/echo/1.0.0"
11+
12+
type EchoProto = ref object of LPProtocol
13+
14+
proc new(T: typedesc[EchoProto]): T =
15+
proc handle(conn: Connection, proto: string) {.async: (raises: [CancelledError]).} =
16+
try:
17+
echo "Echo server: Received connection from ", conn.peerId
18+
19+
# Read and echo messages in a loop
20+
while not conn.atEof:
21+
try:
22+
# Read length-prefixed message using nim-libp2p's readLp
23+
let message = await conn.readLp(1024 * 1024) # Max 1MB
24+
if message.len == 0:
25+
echo "Echo server: Empty message, closing connection"
26+
break
27+
28+
let messageStr = string.fromBytes(message)
29+
echo "Echo server: Received (", message.len, " bytes): ", messageStr
30+
31+
# Echo back using writeLp
32+
await conn.writeLp(message)
33+
echo "Echo server: Echoed message back"
34+
35+
except CatchableError as e:
36+
echo "Echo server: Error processing message: ", e.msg
37+
break
38+
39+
except CancelledError as e:
40+
echo "Echo server: Connection cancelled"
41+
raise e
42+
except CatchableError as e:
43+
echo "Echo server: Exception in handler: ", e.msg
44+
finally:
45+
echo "Echo server: Connection closed"
46+
await conn.close()
47+
48+
return T.new(codecs = @[EchoCodec], handler = handle)
49+
50+
##
51+
# Create QUIC-enabled switch
52+
##
53+
proc createSwitch(ma: MultiAddress, rng: ref HmacDrbgContext): Switch =
54+
var switch = SwitchBuilder
55+
.new()
56+
.withRng(rng)
57+
.withAddress(ma)
58+
.withQuicTransport()
59+
.build()
60+
result = switch
61+
62+
##
63+
# Main server
64+
##
65+
proc main() {.async.} =
66+
let
67+
rng = newRng()
68+
localAddr = MultiAddress.init("/ip4/0.0.0.0/udp/0/quic-v1").tryGet()
69+
echoProto = EchoProto.new()
70+
71+
echo "=== Nim Echo Server for py-libp2p Interop ==="
72+
73+
# Create switch
74+
let switch = createSwitch(localAddr, rng)
75+
switch.mount(echoProto)
76+
77+
# Start server
78+
await switch.start()
79+
80+
# Print connection info
81+
echo "Peer ID: ", $switch.peerInfo.peerId
82+
echo "Listening on:"
83+
for addr in switch.peerInfo.addrs:
84+
echo " ", $addr, "/p2p/", $switch.peerInfo.peerId
85+
echo "Protocol: ", EchoCodec
86+
echo "Ready for py-libp2p connections!"
87+
echo ""
88+
89+
# Keep running
90+
try:
91+
await sleepAsync(100.hours)
92+
except CancelledError:
93+
echo "Shutting down..."
94+
finally:
95+
await switch.stop()
96+
97+
# Graceful shutdown handler
98+
proc signalHandler() {.noconv.} =
99+
echo "\nShutdown signal received"
100+
quit(0)
101+
102+
when isMainModule:
103+
setControlCHook(signalHandler)
104+
try:
105+
waitFor(main())
106+
except CatchableError as e:
107+
echo "Error: ", e.msg
108+
quit(1)
Lines changed: 31 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,61 @@
11
#!/usr/bin/env bash
2-
# Simple setup script for nim echo server interop testing
32

43
set -euo pipefail
54

5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
PROJECT_DIR="${SCRIPT_DIR}/.."
7+
68
# Colors
79
GREEN='\033[0;32m'
810
RED='\033[0;31m'
9-
YELLOW='\033[1;33m'
1011
NC='\033[0m'
1112

1213
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
13-
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
1414
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
1515

16-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17-
PROJECT_ROOT="${SCRIPT_DIR}/.."
18-
NIM_LIBP2P_DIR="${PROJECT_ROOT}/nim-libp2p"
16+
main() {
17+
log_info "Setting up nim echo server for interop testing..."
1918

20-
# Check prerequisites
21-
check_nim() {
22-
if ! command -v nim &> /dev/null; then
23-
log_error "Nim not found. Install with: curl -sSf https://nim-lang.org/choosenim/init.sh | sh"
24-
exit 1
25-
fi
26-
if ! command -v nimble &> /dev/null; then
27-
log_error "Nimble not found. Please install Nim properly."
19+
# Check if nim is available
20+
if ! command -v nim &> /dev/null || ! command -v nimble &> /dev/null; then
21+
log_error "Nim not found. Please install nim first."
2822
exit 1
2923
fi
30-
}
3124

32-
# Setup nim-libp2p dependency
33-
setup_nim_libp2p() {
34-
log_info "Setting up nim-libp2p dependency..."
25+
cd "${PROJECT_DIR}"
26+
27+
# Create logs directory
28+
mkdir -p logs
3529

36-
if [ ! -d "${NIM_LIBP2P_DIR}" ]; then
37-
log_info "Cloning nim-libp2p..."
38-
git clone https://github.com/status-im/nim-libp2p.git "${NIM_LIBP2P_DIR}"
30+
# Check if binary already exists
31+
if [[ -f "nim_echo_server" ]]; then
32+
log_info "nim_echo_server already exists, skipping build"
33+
return 0
3934
fi
4035

41-
cd "${NIM_LIBP2P_DIR}"
42-
log_info "Installing nim-libp2p dependencies..."
43-
nimble install -y --depsOnly
44-
}
36+
log_info "Installing nim-libp2p globally..."
37+
# Install libp2p globally so nim can find it
38+
nimble install -y libp2p
4539

46-
# Build nim echo server
47-
build_echo_server() {
4840
log_info "Building nim echo server..."
49-
50-
cd "${PROJECT_ROOT}"
51-
52-
# Create nimble file if it doesn't exist
53-
cat > nim_echo_test.nimble << 'EOF'
54-
# Package
55-
version = "0.1.0"
56-
author = "py-libp2p interop"
57-
description = "nim echo server for interop testing"
58-
license = "MIT"
59-
60-
# Dependencies
61-
requires "nim >= 1.6.0"
62-
requires "libp2p"
63-
requires "chronos"
64-
requires "stew"
65-
66-
# Binary
67-
bin = @["nim_echo_server"]
68-
EOF
69-
70-
# Build the server
71-
log_info "Compiling nim echo server..."
72-
nim c -d:release -d:chronicles_log_level=INFO -d:libp2p_quic_support --opt:speed --gc:orc -o:nim_echo_server nim_echo_server.nim
73-
74-
if [ -f "nim_echo_server" ]; then
41+
# Compile the echo server
42+
nim c \
43+
-d:release \
44+
-d:chronicles_log_level=INFO \
45+
--opt:speed \
46+
--mm:orc \
47+
-o:nim_echo_server \
48+
nim_echo_server.nim
49+
50+
# Verify binary was created
51+
if [[ -f "nim_echo_server" ]]; then
7552
log_info "✅ nim_echo_server built successfully"
7653
else
7754
log_error "❌ Failed to build nim_echo_server"
7855
exit 1
7956
fi
80-
}
81-
82-
main() {
83-
log_info "Setting up nim echo server for interop testing..."
84-
85-
# Create logs directory
86-
mkdir -p "${PROJECT_ROOT}/logs"
87-
88-
# Clean up any existing processes
89-
pkill -f "nim_echo_server" || true
90-
91-
check_nim
92-
setup_nim_libp2p
93-
build_echo_server
9457

95-
log_info "🎉 Setup complete! You can now run: python -m pytest test_echo_interop.py -v"
58+
log_info "🎉 Setup complete!"
9659
}
9760

9861
main "$@"

0 commit comments

Comments
 (0)