Skip to content

Commit 0123f0b

Browse files
committed
fix: lisening first,then connect to dispatcher
1 parent 38d5f73 commit 0123f0b

File tree

4 files changed

+179
-6
lines changed

4 files changed

+179
-6
lines changed

cmd/validator/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ func runValidator(cmd *cobra.Command, args []string) {
8989
}
9090

9191
func startValidatorService(cfg *config.ValidatorConfig) {
92+
// Create a channel to signal when the listening server is ready
93+
serverReady := make(chan struct{})
94+
9295
// Start TCP server in a goroutine
93-
go startListeningServer(cfg)
96+
go startListeningServer(cfg, serverReady)
97+
98+
// Wait for the server to be ready before connecting to dispatcher
99+
<-serverReady
94100

95101
// Connect to dispatcher and maintain connection
96102
maintainDispatcherConnection(cfg)

cmd/validator/validator_mgr.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func handleHeartbeat(conn net.Conn) {
104104
}
105105

106106
// startListeningServer starts a TCP server to listen for signing requests
107-
func startListeningServer(cfg *config.ValidatorConfig) {
107+
func startListeningServer(cfg *config.ValidatorConfig, ready chan struct{}) {
108108
// Start TCP server to accept connections
109109
var listener net.Listener
110110
var err error
@@ -123,12 +123,20 @@ func startListeningServer(cfg *config.ValidatorConfig) {
123123
logger.Fatal("Failed to start TCP server: %v", err)
124124
}
125125
}
126-
126+
127127
// Update listen address with actual port
128128
cfg.ListenAddr = listener.Addr().String()
129129
logger.Info("Validator listening on %s", cfg.ListenAddr)
130130

131-
// Accept connections
131+
// Signal that the server is ready
132+
close(ready)
133+
134+
// Start accepting connections
135+
acceptConnections(listener)
136+
}
137+
138+
// acceptConnections handles incoming connections
139+
func acceptConnections(listener net.Listener) {
132140
for {
133141
conn, err := listener.Accept()
134142
if err != nil {

scripts/start.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ start_with_docker() {
7575
cp "${DOCKER_COMPOSE_VALIDATOR}" "docker-compose-validator-${i}.yml"
7676

7777
# Update the validator ID and port in the docker-compose file
78-
sed -i "s/--id 0/--id ${i}/g" "docker-compose-validator-${i}.yml"
78+
# sed -i "s/--id 0/--id ${i}/g" "docker-compose-validator-${i}.yml"
7979
sed -i "s/- \"8081:8081\"/- \"$((8081 + i)):8081\"/g" "docker-compose-validator-${i}.yml"
8080
sed -i "s/- \"9001:9000\"/- \"$((9001 + i)):9000\"/g" "docker-compose-validator-${i}.yml"
8181
sed -i "s/- \"5433:5432\"/- \"$((5433 + i)):5432\"/g" "docker-compose-validator-${i}.yml"
@@ -129,7 +129,7 @@ start_natively() {
129129
fi
130130

131131
# Start validator
132-
"${BINARY_DIR}/validator" run --config "${CONFIG_DIR}/val_config.json" --keys="${KEYS_DIR}/validator_${i}.json" --id "${i}" --enable-db --log-level=info >logs/validator_${i}.log 2>&1 &
132+
"${BINARY_DIR}/validator" run --config "${CONFIG_DIR}/val_config.json" --keys="${KEYS_DIR}/validator_${i}.json" --enable-db --log-level=info >logs/validator_${i}.log 2>&1 &
133133
VALIDATOR_PIDS["${i}"]=$!
134134
echo "Validator ${i} started with PID: ${VALIDATOR_PIDS[${i}]}"
135135
sleep 1

tests/README.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Hetu Checkpoint Tests
2+
3+
This document provides guidelines for running tests and important test cases for the Hetu Checkpoint project.
4+
5+
## Project Setup
6+
7+
### Prerequisites
8+
- Go 1.23.4
9+
- Docker and Docker Compose
10+
- Make
11+
- Git
12+
13+
### Environment Setup
14+
15+
1. Clone the repository:
16+
```bash
17+
git clone https://github.com/hetuproject/checkpoint.git
18+
cd checkpoint
19+
```
20+
21+
2. Install dependencies:
22+
```bash
23+
make deps
24+
```
25+
26+
3. Build the project:
27+
```bash
28+
make build
29+
```
30+
31+
## Test Categories
32+
33+
### 1. Unit Tests
34+
Run unit tests:
35+
```bash
36+
make test
37+
```
38+
39+
### 2. Integration Tests
40+
Run integration tests:
41+
```bash
42+
make test-integration
43+
```
44+
45+
### 3. Smoke Tests
46+
Run smoke tests:
47+
```bash
48+
make test-smoke
49+
```
50+
51+
## Core Test Cases
52+
53+
### Dispatcher Service
54+
55+
1. **Checkpoint Creation**
56+
- Test successful checkpoint creation with valid data
57+
- Test checkpoint creation with invalid data
58+
- Test checkpoint creation with different data sizes
59+
60+
2. **Checkpoint Validation**
61+
- Test successful validation of valid checkpoints
62+
- Test validation of corrupted checkpoints
63+
- Test validation of expired checkpoints
64+
- Test validation with different validation rules
65+
66+
3. **Storage Integration**
67+
- Test checkpoint storage in database backends
68+
- Test checkpoint retrieval
69+
- Test storage cleanup
70+
- Test storage capacity limits
71+
- Test storage failure scenarios
72+
73+
4. **API Endpoints**
74+
- Test all REST API endpoints
75+
- Test API rate limiting
76+
- Test API authentication/whitelist
77+
78+
### Validator Service
79+
80+
1. **Validation Logic**
81+
- Test signature verification
82+
- Test timestamp validation
83+
- Test data integrity checks
84+
- Test custom validation rules
85+
- Test validation performance
86+
87+
2. **Integration Tests**
88+
- Test validator-dispatcher communication
89+
- Test validator-storage integration
90+
- Test validator-API integration
91+
- Test validator scaling
92+
- Test validator failover
93+
94+
### Performance Tests
95+
96+
1. **Load Testing**
97+
- Test system under high concurrent requests
98+
- Test system with large data volumes
99+
- Test system with different request patterns
100+
- Test system resource usage
101+
- Test system recovery after load
102+
103+
2. **Stress Testing**
104+
- Test system under extreme conditions
105+
- Test system with network latency
106+
- Test system with storage delays
107+
- Test system with partial failures
108+
- Test system recovery mechanisms
109+
110+
### Security Tests
111+
112+
1. **Authentication & Authorization**
113+
- Test API key validation
114+
- Test role-based access control
115+
- Test permission boundaries
116+
- Test token expiration
117+
- Test security headers
118+
119+
2. **Data Protection**
120+
- Test data encryption
121+
- Test data integrity
122+
- Test data privacy
123+
- Test secure communication
124+
- Test audit logging
125+
126+
## Test Environment
127+
128+
### Local Development
129+
```bash
130+
# Start local development environment
131+
make dev-up
132+
133+
# Run all tests
134+
make test-all
135+
136+
# Clean up
137+
make dev-down
138+
```
139+
140+
### CI/CD Pipeline
141+
Tests are automatically run in the CI/CD pipeline for:
142+
- Pull requests
143+
- Main branch commits
144+
- Release tags
145+
146+
## Test Results
147+
148+
Test results are available in:
149+
- Console output
150+
- `test-results/` directory
151+
- CI/CD pipeline artifacts
152+
153+
## Contributing
154+
155+
When adding new features:
156+
1. Add corresponding unit tests
157+
2. Add integration tests if applicable
158+
3. Update this documentation if needed
159+
4. Ensure all tests pass before submitting PR

0 commit comments

Comments
 (0)