planetmint-go/util/determine_block_proposer.go
Jürgen Eckel 4f5b1e5777
Multi validator setup in test cases (#333)
* Initializing rootDir of dao and machine keeper with the home path of the validator key material
* added Block height logging of context decorator
* removed SetRoot usage
* fixed data races of the attest machine go-routine
* reproduction of the issue
* fixed testing URL issue
* refactored the machine-nft functions/mock
* fixed keeper.param read-bug that increased the gas prices in an inconsistent way
* increased the validator number to 3 for all e2e tests
* added go routine to attest machine workflow

---------

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
Co-authored-by: Julian Strobl <jmastr@mailbox.org>
Co-authored-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
2024-03-05 11:37:01 +01:00

58 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) (string, bool) {
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 "", false
}
jsonBytes, err := io.ReadAll(jsonFile)
if err != nil {
GetAppLogger().Error(ctx, "error while reading file", err.Error())
return "", false
}
var keyFile KeyFile
err = json.Unmarshal(jsonBytes, &keyFile)
if err != nil {
GetAppLogger().Error(ctx, "error while unmarshaling key file", err.Error())
return "", false
}
return strings.ToLower(keyFile.Address), true
}
func IsValidatorBlockProposer(ctx sdk.Context, proposerAddress []byte, rootDir string) bool {
validatorIdentity, validResult := GetValidatorCometBFTIdentity(ctx, rootDir)
if !validResult {
return false
}
hexProposerAddress := hex.EncodeToString(proposerAddress)
return hexProposerAddress == validatorIdentity
}