Skip to content

Commit

Permalink
Add sample Starknet P2P tests utilizing synthetic node and get header…
Browse files Browse the repository at this point in the history
… request (#1)

Please **ignore the /protocols/ directory.** It's copied directly from
Juno with minor changes. Temporarly I couldn't import the package
because of many not needed, heavy Rust deps. For more details, see:
[NethermindEth/juno#2084](NethermindEth/juno#2084).

PR added demo p2p tests for starkness network, including:
1. Conformance test for verifying block header
2. Performance test for block header
3. Peer discovery
  • Loading branch information
wojciechos authored Sep 13, 2024
2 parents 5a60574 + 89778b6 commit a7e81ff
Show file tree
Hide file tree
Showing 31 changed files with 9,512 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Run Tests and Benchmarks

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test-and-benchmark:
name: Run p2p tests and benchmarks
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'

- name: Install dependencies
run: go mod download

- name: Set TARGET_PEER_ADDRESS environment variable
run: echo "TARGET_PEER_ADDRESS=/ip4/35.237.66.77/tcp/7777/p2p/12D3KooWR8ikUDiinyE5wgdYiqsdLfJRsBDYKGii6L3oyoipVEaV" >> $GITHUB_ENV

- name: Run performance benchmarks
run: go test -v -bench=. -benchtime=1x ./tests/performance
env:
TARGET_PEER_ADDRESS: ${{ env.TARGET_PEER_ADDRESS }}

- name: Run conformance tests
run: go test -v ./tests/conformance
env:
TARGET_PEER_ADDRESS: ${{ env.TARGET_PEER_ADDRESS }}
141 changes: 141 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Starknet P2P Tests

This project contains peer-to-peer (P2P) tests for the Starknet network. It is inspired by Ziggurat (https://github.com/runziggurat) but written in Go.

## Purpose

The main purposes of these tests are:

1. To ensure reliable and efficient communication between nodes in the Starknet network.
2. To validate the correct implementation of the P2P protocol used in Starknet.
3. To identify and address any potential issues or vulnerabilities in the P2P layer.
4. To assess the conformance, performance, and resilience of the Starknet P2P network.

## Project Structure

This project is written in Go and organized as follows:

- `tests/`: Contains all test files
- `conformance/`: Conformance tests
- `performance/`: Performance tests
- `resilience/`: Resilience tests (to be implemented)
- `config/`: Contains configuration files
- `config.go`: Defines configuration options and environment variables
- `tools/`: Contains utility tools
- `synthetic_node.go`: Implements the synthetic node for testing

## Getting Started

### Prerequisites

- Go 1.23 or later

### Installation

1. Clone the repository:
```
git clone https://github.com/NethermindEth/starknet-p2p-tests.git
```

2. Navigate to the project directory:
```
cd starknet-p2p-tests
```

3. Install dependencies:
```
go mod tidy
```

## Configuration

The project uses environment variables for configuration, which are defined in the `config/config.go` file.

[More info](#environment-variables)

## Running Tests

Before running the tests, ensure that your environment is properly configured:

1. Set the necessary environment variables (e.g., `TARGET_PEER_ADDRESS`) if you want to override the defaults.

2. To run all tests:
```
go test ./tests/...
```
For verbose output:
```
go test -v ./tests/...
```

3. To run specific test categories:
```
go test ./tests/conformance
go test ./tests/performance
```

4. For performance tests, you may need to increase the default timeout:
```
go test -timeout 5m ./tests/performance
```

## Environment Variables

The following environment variables are available for configuration:

- `TARGET_PEER_ADDRESS`: The address of the target Starknet node (default: "/ip4/35.237.66.77/tcp/7777/p2p/12D3KooWR8ikUDiinyE5wgdYiqsdLfJRsBDYKGii6L3oyoipVEaV")
- `DEFAULT_TEST_TIMEOUT`: The default timeout for tests (default: "30s")
- `SYNTHETIC_LISTEN_ADDRS`: The listen addresses for the synthetic node (default: "/ip4/0.0.0.0/tcp/0")
- `NETWORK_NAME`: The name of the Starknet network being tested (default: "sepolia")


Note: For performance tests, you may need to increase the default timeout. Use the -timeout flag, e.g., `go test -timeout 5m ./tests/performance`

## Test Cases

The project currently includes the following test cases:

1. Conformance Tests:
- `getBlock_test.go`: Tests the conformance of the getBlock functionality.
- ...
- ...
- ...
2. Performance Tests:
- `getBlocks_test.go`: Evaluates the performance of retrieving multiple blocks.
- ...
- ...
- ...

## Sample Test Output

```
=== RUN TestSyntheticNodeMultipleBlockHeadersRequest/Basic_Consecutive_Blocks
utils.go:48: INFO: Created new synthetic node address=[/ip4/127.0.0.1/tcp/63979 /ip4/192.168.215.4/tcp/63979] id=12D3KooWC2xdz7kxnsLMCDY851hqvtNcsmMuS8xQRf2KG1MjKByb
utils.go:48: INFO: Connecting to peer: address=/ip4/35.237.66.77/tcp/7777/p2p/12D3KooWR8ikUDiinyE5wgdYiqsdLfJRsBDYKGii6L3oyoipVEaV
utils.go:48: INFO: Successfully connected to peer id=12D3KooWR8ikUDiinyE5wgdYiqsdLfJRsBDYKGii6L3oyoipVEaV
getBlock_test.go:71: Requesting headers from block 1, limit 5, step 1
utils.go:48: INFO: Requesting block headers start=1 limit=5 step=1
utils.go:48: INFO: Received block headers count=6
getBlock_test.go:76: Received 6 responses
getBlock_test.go:108: Header 0: Block Number: 1, BlockHash: 0x065f21e3bf9acb006889a6a0cfa53811204cb2a08e830bf61238547a454471af
getBlock_test.go:113: Block Number: 1, Time: 2023-11-20 10:05:24 +0000 UTC, Protocol Version: 0.12.3
getBlock_test.go:108: Header 1: Block Number: 2, BlockHash: 0x06a592792d61b9eddbf7686bff0cdc318b5875efcbb363fd0cd4308a1a07660d
getBlock_test.go:113: Block Number: 2, Time: 2023-11-20 10:19:41 +0000 UTC, Protocol Version: 0.12.3
getBlock_test.go:108: Header 2: Block Number: 3, BlockHash: 0x00f8648fbf10da5adca08c3307dd221fa19da3fe8e339ade0452545e9e643c31
getBlock_test.go:113: Block Number: 3, Time: 2023-11-20 10:57:44 +0000 UTC, Protocol Version: 0.12.3
getBlock_test.go:108: Header 3: Block Number: 4, BlockHash: 0x05001dd2dd55c1cdfc347f7d353f63f245ad7dbba56b4bc720a6c80841156cb6
getBlock_test.go:113: Block Number: 4, Time: 2023-11-20 11:34:31 +0000 UTC, Protocol Version: 0.12.3
getBlock_test.go:108: Header 4: Block Number: 5, BlockHash: 0x058b18d1ff10100a677e324d1b26dc2aa07e07f8e0d44e18ea3fc3e8aec1e323
getBlock_test.go:113: Block Number: 5, Time: 2023-11-20 12:08:02 +0000 UTC, Protocol Version: 0.12.3
getBlock_test.go:95: Received Fin message at position 5
utils.go:34: INFO: Closing synthetic node
```
## Contributing

We welcome contributions to improve and expand these tests. Please read our contributing guidelines before submitting pull requests.



## Contact

For any questions or concerns regarding these Starknet P2P tests written in Go, please open an issue in this repository.
36 changes: 36 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package config

import (
"fmt"
"os"
"strings"
"time"
)

var (
TargetPeerAddress string
DefaultTestTimeout time.Duration
SyntheticListenAddrs []string
NetworkName string
)

func init() {
TargetPeerAddress = getEnv("TARGET_PEER_ADDRESS", "/ip4/127.0.0.1/tcp/7777/p2p/12D3KooWLBUjEPyTiACzQZ3K1oqBXRqHwRFvAUHrm561pWWbJkYf")
timeoutStr := getEnv("DEFAULT_TEST_TIMEOUT", "300s")
var err error
DefaultTestTimeout, err = time.ParseDuration(timeoutStr)
if err != nil {
fmt.Printf("Error parsing DEFAULT_TEST_TIMEOUT: %v. Defaulting to 30 seconds.\n", err)
DefaultTestTimeout = 30 * time.Second
}

SyntheticListenAddrs = strings.Split(getEnv("SYNTHETIC_LISTEN_ADDRS", "/ip4/0.0.0.0/tcp/0"), ",")
NetworkName = getEnv("NETWORK_NAME", "sepolia")
}

func getEnv(key, fallback string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return fallback
}
139 changes: 139 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
module starknet-p2p-tests

go 1.23

require github.com/stretchr/testify v1.9.0

require (
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/containerd/cgroups v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/gosigar v0.14.3 // indirect
github.com/flynn/noise v1.1.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/boxo v0.21.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/ipld/go-ipld-prime v0.21.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/koron/go-ssdp v0.0.4 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect
github.com/libp2p/go-libp2p-kbucket v0.6.3 // indirect
github.com/libp2p/go-libp2p-record v0.2.0 // indirect
github.com/libp2p/go-libp2p-routing-helpers v0.7.4 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-nat v0.2.0 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
github.com/libp2p/go-reuseport v0.4.0 // indirect
github.com/libp2p/go-yamux/v4 v4.0.1 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/miekg/dns v1.1.61 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multiaddr-dns v0.3.1 // indirect
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.5.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.19.1 // indirect
github.com/opencontainers/runtime-spec v1.2.0 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pion/datachannel v1.5.8 // indirect
github.com/pion/dtls/v2 v2.2.12 // indirect
github.com/pion/ice/v2 v2.3.34 // indirect
github.com/pion/interceptor v0.1.29 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/mdns v0.0.12 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtcp v1.2.14 // indirect
github.com/pion/rtp v1.8.8 // indirect
github.com/pion/sctp v1.8.20 // indirect
github.com/pion/sdp/v3 v3.0.9 // indirect
github.com/pion/srtp/v2 v2.0.20 // indirect
github.com/pion/stun v0.6.1 // indirect
github.com/pion/transport/v2 v2.2.10 // indirect
github.com/pion/turn/v2 v2.1.6 // indirect
github.com/pion/webrtc/v3 v3.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/polydawn/refmt v0.89.0 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/quic-go v0.45.2 // indirect
github.com/quic-go/webtransport-go v0.8.0 // indirect
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/wlynxg/anet v0.0.3 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.uber.org/dig v1.17.1 // indirect
go.uber.org/fx v1.22.1 // indirect
go.uber.org/mock v0.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.23.0 // indirect
gonum.org/v1/gonum v0.15.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
)

require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fxamacker/cbor/v2 v2.7.0
github.com/libp2p/go-libp2p v0.36.2
github.com/libp2p/go-libp2p-kad-dht v0.26.1
github.com/montanaflynn/stats v0.7.1
github.com/multiformats/go-multiaddr v0.13.0
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit a7e81ff

Please sign in to comment.