mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-03-30 15:08:28 +00:00
refactor: goify function (#337)
* chore: log in case of error Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
parent
f6f10b54b6
commit
26457a7a6f
@ -1,6 +1,7 @@
|
||||
package errormsg
|
||||
|
||||
var (
|
||||
CouldNotGetValidatorIdentity = "could not get validator identity"
|
||||
ErrorInvalidCreator = "invalid creator address (%s)"
|
||||
InvalidRequest = "invalid request"
|
||||
)
|
||||
|
@ -23,35 +23,37 @@ type KeyFile struct {
|
||||
PrivKey Key `json:"priv-key"`
|
||||
}
|
||||
|
||||
func GetValidatorCometBFTIdentity(ctx sdk.Context, rootDir string) (string, bool) {
|
||||
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 "", false
|
||||
return
|
||||
}
|
||||
jsonBytes, err := io.ReadAll(jsonFile)
|
||||
if err != nil {
|
||||
GetAppLogger().Error(ctx, "error while reading file", err.Error())
|
||||
return "", false
|
||||
return
|
||||
}
|
||||
|
||||
var keyFile KeyFile
|
||||
err = json.Unmarshal(jsonBytes, &keyFile)
|
||||
if err != nil {
|
||||
GetAppLogger().Error(ctx, "error while unmarshaling key file", err.Error())
|
||||
return "", false
|
||||
return
|
||||
}
|
||||
return strings.ToLower(keyFile.Address), true
|
||||
validatorIdentity = strings.ToLower(keyFile.Address)
|
||||
return
|
||||
}
|
||||
|
||||
func IsValidatorBlockProposer(ctx sdk.Context, proposerAddress []byte, rootDir string) bool {
|
||||
validatorIdentity, validResult := GetValidatorCometBFTIdentity(ctx, rootDir)
|
||||
if !validResult {
|
||||
return false
|
||||
func IsValidatorBlockProposer(ctx sdk.Context, proposerAddress []byte, rootDir string) (result bool) {
|
||||
validatorIdentity, err := GetValidatorCometBFTIdentity(ctx, rootDir)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
hexProposerAddress := hex.EncodeToString(proposerAddress)
|
||||
return hexProposerAddress == validatorIdentity
|
||||
result = hexProposerAddress == validatorIdentity
|
||||
return
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
|
||||
errorsmod "cosmossdk.io/errors"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/planetmint/planetmint-go/errormsg"
|
||||
"github.com/planetmint/planetmint-go/util"
|
||||
"github.com/planetmint/planetmint-go/x/dao/types"
|
||||
)
|
||||
@ -28,9 +29,13 @@ func (k msgServer) DistributionRequest(goCtx context.Context, msg *types.MsgDist
|
||||
util.GetAppLogger().Info(ctx, distributionRequestTag+"storing distribution: "+msg.GetDistribution().String())
|
||||
k.StoreDistributionOrder(ctx, *msg.GetDistribution())
|
||||
|
||||
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||
if !validResult || msg.Distribution.GetProposer() != validatorIdentity {
|
||||
util.GetAppLogger().Info(ctx, distributionRequestTag+"Not the proposer. valid result: %t proposer: %s validator identity: %s", validResult, msg.Distribution.GetProposer(), validatorIdentity)
|
||||
validatorIdentity, err := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||
if err != nil {
|
||||
util.GetAppLogger().Error(ctx, distributionRequestTag+errormsg.CouldNotGetValidatorIdentity+": "+err.Error())
|
||||
return nil, err
|
||||
}
|
||||
if msg.Distribution.GetProposer() != validatorIdentity {
|
||||
util.GetAppLogger().Info(ctx, distributionRequestTag+"Not the proposer. proposer: %s validator identity: %s", msg.Distribution.GetProposer(), validatorIdentity)
|
||||
return &types.MsgDistributionRequestResponse{}, nil
|
||||
}
|
||||
|
||||
|
@ -4,10 +4,15 @@ import (
|
||||
"context"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/planetmint/planetmint-go/errormsg"
|
||||
"github.com/planetmint/planetmint-go/util"
|
||||
"github.com/planetmint/planetmint-go/x/dao/types"
|
||||
)
|
||||
|
||||
var (
|
||||
initPopTag = "init pop tag: "
|
||||
)
|
||||
|
||||
func (k msgServer) InitPop(goCtx context.Context, msg *types.MsgInitPop) (*types.MsgInitPopResponse, error) {
|
||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||
|
||||
@ -19,8 +24,12 @@ func (k msgServer) InitPop(goCtx context.Context, msg *types.MsgInitPop) (*types
|
||||
|
||||
k.StoreChallenge(ctx, challenge)
|
||||
|
||||
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||
if validResult && msg.Initiator == validatorIdentity {
|
||||
validatorIdentity, err := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||
if err != nil {
|
||||
util.GetAppLogger().Error(ctx, initPopTag+errormsg.CouldNotGetValidatorIdentity+": "+err.Error())
|
||||
return nil, err
|
||||
}
|
||||
if msg.Initiator == validatorIdentity {
|
||||
go util.SendMqttPopInitMessagesToServer(ctx, challenge)
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
|
||||
errorsmod "cosmossdk.io/errors"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/planetmint/planetmint-go/errormsg"
|
||||
"github.com/planetmint/planetmint-go/util"
|
||||
"github.com/planetmint/planetmint-go/x/dao/types"
|
||||
)
|
||||
@ -29,9 +30,13 @@ func (k msgServer) ReissueRDDLProposal(goCtx context.Context, msg *types.MsgReis
|
||||
reissuance.LastIncludedPop = msg.GetLastIncludedPop()
|
||||
k.StoreReissuance(ctx, reissuance)
|
||||
|
||||
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||
if !validResult || msg.Proposer != validatorIdentity {
|
||||
util.GetAppLogger().Info(ctx, reissueTag+"Not the proposer. valid result: %t proposer: %s validator identity: %s", validResult, msg.Proposer, validatorIdentity)
|
||||
validatorIdentity, err := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||
if err != nil {
|
||||
util.GetAppLogger().Error(ctx, reissueTag+errormsg.CouldNotGetValidatorIdentity+": "+err.Error())
|
||||
return nil, err
|
||||
}
|
||||
if msg.Proposer != validatorIdentity {
|
||||
util.GetAppLogger().Info(ctx, reissueTag+"Not the proposer. proposer: %s validator identity: %s", msg.Proposer, validatorIdentity)
|
||||
return &types.MsgReissueRDDLProposalResponse{}, nil
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user