Skip to content

Commit

Permalink
feat: add is synced field to peer-info command
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Nov 25, 2023
1 parent eb69305 commit 7a38671
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ func (c *Client) LastBlockTime() (uint32, error) {
return lastBlockTime.BlockTime, err
}

func (c *Client) GetNodeInfo() (*pactus.GetNodeInfoResponse, error) {
info, err := c.networkClient.GetNodeInfo(context.Background(), &pactus.GetNodeInfoRequest{})
if err != nil {
return &pactus.GetNodeInfoResponse{}, err
}

return info, err
}

func (c *Client) Close() error {
return c.conn.Close()
}
19 changes: 19 additions & 0 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,30 @@ func (b *Bot) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}

notSyncedMsg := "this peer is not synced with network, gRPC is disabled or doesn't have public IP address."
syncedMsg := "**this peer is synced with network**"

isSynced := notSyncedMsg
c, err := client.NewClient(strings.Split(peerInfo.Address, "/")[2] + ":50051")
if err != nil {
isSynced = notSyncedMsg
}
lastBlockTime, err := c.LastBlockTime()
if err != nil {
isSynced = notSyncedMsg
}
currentTime := time.Now().Unix()

if (uint32(currentTime) - lastBlockTime) < 15 {
isSynced = syncedMsg
}

msg := p.Sprintf("Peer info\n")
msg += p.Sprintf("Peer ID: %v\n", peerID)
msg += p.Sprintf("IP address: %v\n", peerInfo.Address)
msg += p.Sprintf("Agent: %v\n", peerInfo.Agent)
msg += p.Sprintf("Moniker: %v\n", peerInfo.Moniker)
msg += p.Sprintf("IsSynced: %v\n", isSynced)
_, _ = s.ChannelMessageSendReply(m.ChannelID, msg, m.Reference())
return
}
Expand Down

0 comments on commit 7a38671

Please sign in to comment.