planetmint-go/util/determine_block_proposer.go
Julian Strobl 26457a7a6f
refactor: goify function (#337)
* chore: log in case of error

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2024-03-06 14:51:27 +01:00

60 lines
1.4 KiB
Go

package util
import (
"encoding/hex"
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
cometcfg "github.com/cometbft/cometbft/config"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type Key struct {
Type string `json:"type"`
Value string `json:"value"`
}
type KeyFile struct {
Address string `json:"address"`
PubKey Key `json:"pub-key"`
PrivKey Key `json:"priv-key"`
}
func GetValidatorCometBFTIdentity(ctx sdk.Context, rootDir string) (validatorIdentity string, err error) {
cfg := cometcfg.DefaultConfig()
jsonFilePath := filepath.Join(rootDir, cfg.PrivValidatorKey)
jsonFile, err := os.Open(jsonFilePath)
if err != nil {
GetAppLogger().Error(ctx, "error while opening config", err.Error())
return
}
jsonBytes, err := io.ReadAll(jsonFile)
if err != nil {
GetAppLogger().Error(ctx, "error while reading file", err.Error())
return
}
var keyFile KeyFile
err = json.Unmarshal(jsonBytes, &keyFile)
if err != nil {
GetAppLogger().Error(ctx, "error while unmarshaling key file", err.Error())
return
}
validatorIdentity = strings.ToLower(keyFile.Address)
return
}
func IsValidatorBlockProposer(ctx sdk.Context, proposerAddress []byte, rootDir string) (result bool) {
validatorIdentity, err := GetValidatorCometBFTIdentity(ctx, rootDir)
if err != nil {
return
}
hexProposerAddress := hex.EncodeToString(proposerAddress)
result = hexProposerAddress == validatorIdentity
return
}