Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
Add thread safety for discord posts
Browse files Browse the repository at this point in the history
  • Loading branch information
agouin committed May 11, 2022
1 parent da0e575 commit 4e00ebe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cmd/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
type DiscordNotificationService struct {
webhookID string
webhookToken string
postMutex *sync.Mutex
}

func formattedTime(t time.Time) string {
Expand All @@ -37,6 +38,7 @@ func NewDiscordNotificationService(webhookID, webhookToken string) *DiscordNotif
return &DiscordNotificationService{
webhookID: webhookID,
webhookToken: webhookToken,
postMutex: &sync.Mutex{},
}
}

Expand Down Expand Up @@ -167,23 +169,26 @@ func (service *DiscordNotificationService) UpdateValidatorRealtimeStatus(
client := service.client()
defer client.Close(ctx)
if vm.DiscordStatusMessageID != nil {

service.postMutex.Lock()
_, err := client.UpdateMessage(snowflake.Snowflake(*vm.DiscordStatusMessageID), discord.WebhookMessageUpdate{
Embeds: &[]discord.Embed{
getCurrentStatsEmbed(stats, vm),
},
}, rest.WithCtx(ctx))
service.postMutex.Unlock()
if err != nil {
fmt.Printf("Error updating discord message: %v\n", err)
return
}
} else {
service.postMutex.Lock()
message, err := client.CreateMessage(discord.WebhookMessageCreate{
Username: config.Notifications.Discord.Username,
Embeds: []discord.Embed{
getCurrentStatsEmbed(stats, vm),
},
}, rest.WithCtx(ctx))
service.postMutex.Unlock()
if err != nil {
fmt.Printf("Error sending discord message: %v\n", err)
return
Expand Down Expand Up @@ -228,6 +233,7 @@ func (service *DiscordNotificationService) SendValidatorAlertNotification(
defer cancel()
client := service.client()
defer client.Close(ctx)
service.postMutex.Lock()
_, err := client.CreateMessage(discord.WebhookMessageCreate{
Username: config.Notifications.Discord.Username,
Content: toNotify,
Expand All @@ -239,6 +245,7 @@ func (service *DiscordNotificationService) SendValidatorAlertNotification(
},
},
}, rest.WithCtx(ctx))
service.postMutex.Unlock()
if err != nil {
fmt.Printf("Error sending discord message: %v\n", err)
}
Expand All @@ -257,6 +264,7 @@ func (service *DiscordNotificationService) SendValidatorAlertNotification(
defer cancel()
client := service.client()
defer client.Close(ctx)
service.postMutex.Lock()
_, err := client.CreateMessage(discord.WebhookMessageCreate{
Username: config.Notifications.Discord.Username,
Content: toNotify,
Expand All @@ -268,6 +276,7 @@ func (service *DiscordNotificationService) SendValidatorAlertNotification(
},
},
}, rest.WithCtx(ctx))
service.postMutex.Unlock()
if err != nil {
fmt.Printf("Error sending discord message: %v\n", err)
}
Expand Down
19 changes: 19 additions & 0 deletions cmd/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"encoding/hex"
"fmt"
"reflect"
"sync"
Expand Down Expand Up @@ -34,6 +35,21 @@ func monitorValidator(
return
}

addr, err := hex.DecodeString("4658F97E7B70B55D69D26C100C1F9D7CE1B7160C")
if err != nil {
errs = append(errs, err)
return
}

bech32Encoded, err := bech32.ConvertAndEncode("penumbravalcons", addr)

if err != nil {
errs = append(errs, err)
return
}

fmt.Printf("Us bech32: %s\n", bech32Encoded)

valInfo, err := getSigningInfo(client, vm.Address)
slashingPeriod := int64(10000)
if err != nil {
Expand Down Expand Up @@ -89,7 +105,9 @@ func monitorValidator(
break
}
found := false
fmt.Printf("Us: %v\n", hexAddress)
for _, voter := range block.Block.LastCommit.Signatures {
fmt.Printf("Voter: %v\n", voter.ValidatorAddress)
if reflect.DeepEqual(voter.ValidatorAddress, bytes.HexBytes(hexAddress)) {
if block.Block.Height > stats.LastSignedBlockHeight {
stats.LastSignedBlockHeight = block.Block.Height
Expand Down Expand Up @@ -121,6 +139,7 @@ func monitorValidator(
break
}
for _, voter := range block.Block.LastCommit.Signatures {
fmt.Printf("Voter: %v\n", voter)
if reflect.DeepEqual(voter.ValidatorAddress, bytes.HexBytes(hexAddress)) {
stats.LastSignedBlockHeight = block.Block.Height
stats.LastSignedBlockTimestamp = block.Block.Time
Expand Down

0 comments on commit 4e00ebe

Please sign in to comment.