Skip to content

Commit

Permalink
handle concurrency better
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Apr 30, 2024
1 parent 27c4180 commit 4c5e74a
Show file tree
Hide file tree
Showing 2 changed files with 4,223 additions and 650 deletions.
42 changes: 42 additions & 0 deletions lib/checknode.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ import (

var (
earliestBlock map[string]int
earliestBlockMu sync.RWMutex
rpcAddr map[string]bool
rpcAddrMu sync.RWMutex
grpcAddr map[string]bool
grpcAddrMu sync.RWMutex
apiAddr map[string]bool
apiAddrMu sync.RWMutex
moniker map[string]string
monikerMu sync.RWMutex
initialNode string
nodeAddrGRPC string
totalNodesChecked int
initialChainID string
archiveNodes map[string]bool
archiveNodesMu sync.RWMutex
)

func CheckNode(nodeAddr string) {
Expand All @@ -30,22 +36,50 @@ func CheckNode(nodeAddr string) {
MarkNodeAsVisited(nodeAddr)
// Check if the node is the initial node
if initialNode == "" {
earliestBlockMu.Lock()
earliestBlock = map[string]int{}
earliestBlockMu.Unlock()

rpcAddrMu.Lock()
rpcAddr = map[string]bool{}
rpcAddrMu.Unlock()

grpcAddrMu.Lock()
grpcAddr = map[string]bool{}
grpcAddrMu.Unlock()

apiAddrMu.Lock()
apiAddr = map[string]bool{}
apiAddrMu.Unlock()

archiveNodesMu.Lock()
archiveNodes = map[string]bool{}
archiveNodesMu.Unlock()

monikerMu.Lock()
moniker = map[string]string{}
monikerMu.Unlock()

initialNode = nodeAddr
client, err := FetchClient(nodeAddr)
if err != nil {
color.Red("[%s] Failed to fetch status from %s\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
return
}
if client == nil {
color.Red("[%s] Client is nil for %s\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
return
}

ctx := context.TODO()
status, err := client.Status(ctx)
if err != nil {
color.Red("[%s] cannot fetch status\n", time.Now().Format("2006-01-02 15:04:05"))
return
}
if status == nil {
color.Red("[%s] Status is nil for %s\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
return
}
initialChainID = status.NodeInfo.Network
moniker[nodeAddr] = status.NodeInfo.Moniker
Expand All @@ -61,6 +95,10 @@ func CheckNode(nodeAddr string) {
color.Red("[%s] Failed to fetch status from %s\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
return
}
if client == nil {
color.Red("[%s] Client is nil for %s\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
return
}
netinfo, err := FetchNetInfo(client)
if err == nil {
color.Green("[%s] Got net info from %s\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
Expand All @@ -72,6 +110,10 @@ func CheckNode(nodeAddr string) {
color.Red("[%s] Failed to fetch client from %s\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
return
}
if status == nil {
color.Red("[%s] Status is nil for %s\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
return
}
// Verify chain_id
if status.NodeInfo.Network != initialChainID {
color.Red("[%s] Node %s is on a different chain_id\n", time.Now().Format("2006-01-02 15:04:05"), nodeAddr)
Expand Down
Loading

0 comments on commit 4c5e74a

Please sign in to comment.