planetmint-go/util/determine_block_proposer.go
Julian Strobl 5b25d4cefc
Improve linter setup (#186)
* [linter] Add `musttag`

Enforce field tags in (un)marshaled structs.

* [linter] Add `nestif`

Reports deeply nested if statements.

* [linter] Add `noctx`

Finds sending http request without context.Context.

* [linter] Add `paralleltest`

Paralleltest detects missing usage of t.Parallel() method in your Go
test.

* [linter] Add `tagalign`

Check that struct tags are well aligned.

* [linter] Add `tagliatelle`

Checks the struct tags.

* [linter] Add `whitespace`

Tool for detection of leading and trailing whitespace.

* [paralleltest] Exclude files bc of data race in tests

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2023-11-17 10:56:25 +01:00

62 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"
"github.com/planetmint/planetmint-go/config"
)
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) (string, bool) {
logger := ctx.Logger()
conf := config.GetConfig()
cfg := cometcfg.DefaultConfig()
jsonFilePath := filepath.Join(conf.ConfigRootDir, cfg.PrivValidatorKey)
jsonFile, err := os.Open(jsonFilePath)
if err != nil {
logger.Error("error while opening config", err)
return "", false
}
jsonBytes, err := io.ReadAll(jsonFile)
if err != nil {
logger.Error("error while reading file", err)
return "", false
}
var keyFile KeyFile
err = json.Unmarshal(jsonBytes, &keyFile)
if err != nil {
logger.Error("error while unmarshaling key file", err)
return "", false
}
return strings.ToLower(keyFile.Address), true
}
func IsValidatorBlockProposer(ctx sdk.Context, proposerAddress []byte) bool {
validatorIdentity, validResult := GetValidatorCometBFTIdentity(ctx)
if !validResult {
return false
}
hexProposerAddress := hex.EncodeToString(proposerAddress)
return hexProposerAddress == validatorIdentity
}