-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
140 lines (115 loc) · 3.04 KB
/
main.go
File metadata and controls
140 lines (115 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"time"
)
type BlockNumberResponse struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Result string `json:"result"` // Block number as a hex string (e.g., "0x5B9AC0")
}
func getBlockNumber(rpcURL string) (int64, error) {
// JSON-RPC payload
currentTime := time.Now()
id := rand.Intn(100)
payload := map[string]interface{}{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": []interface{}{},
"id": id,
}
// Convert payload to JSON
jsonData, err := json.Marshal(payload)
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return 0, err
}
rpc := rpcURL
if rpc == "" {
rpc = "http://127.0.0.1:8545"
}
// Send the request
resp, err := http.Post(rpc, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
fmt.Println("Error sending request:", err)
return 0, err
}
defer resp.Body.Close()
// Read the response
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return 0, err
}
// Unmarshal the response into BlockNumberResponse struct
var blockNumberResp BlockNumberResponse
err = json.Unmarshal(body, &blockNumberResp)
if err != nil {
fmt.Println("Error unmarshalling response:", err)
return 0, err
}
// Print the block number in hex format
fmt.Println("currentTime:", currentTime, "ID ", id, "Block Number (Hex):", blockNumberResp.Result)
// Optionally, convert the hex block number to a decimal
var blockNumber int64
_, err = fmt.Sscanf(blockNumberResp.Result, "0x%x", &blockNumber)
if err != nil {
fmt.Println("Error converting hex to decimal:", err)
return 0, err
}
// Print the block number in decimal format
fmt.Println("currentTime:", currentTime, "ID ", id, "Block Number (Decimal):", blockNumber)
return blockNumber, nil
}
type BlockResponse struct {
BlockNumberHex string `json:"block_number_hex"`
BlockNumberDecimal int64 `json:"block_number_decimal"`
Status string `json:"status"`
}
func checkSync(w http.ResponseWriter, r *http.Request) {
rpcURL := r.URL.Query().Get("rpc")
blockNumberFirst, err := getBlockNumber(rpcURL)
if err != nil {
w.WriteHeader(500)
return
}
time.Sleep(30 * time.Second)
blockNumberSecond, err := getBlockNumber(rpcURL)
if err != nil {
w.WriteHeader(500)
return
}
blockNumber := blockNumberSecond - blockNumberFirst
// Return both hex and decimal formats
response := BlockResponse{
BlockNumberHex: fmt.Sprintf("0x%x", blockNumberSecond),
BlockNumberDecimal: blockNumberSecond,
}
if blockNumber != 0 {
response.Status = "synced"
w.WriteHeader(200)
} else {
response.Status = "not_synced"
w.WriteHeader(500)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
func main() {
http.HandleFunc("/", checkSync)
port := os.Getenv("PORT")
if port == "" {
port = "9999"
}
fmt.Printf("Starting server on port %s\n", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
panic(err)
}
}