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
|
package errormsg
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
CouldNotGetValidatorIdentity = "could not get validator identity"
|
||||||
ErrorInvalidCreator = "invalid creator address (%s)"
|
ErrorInvalidCreator = "invalid creator address (%s)"
|
||||||
InvalidRequest = "invalid request"
|
InvalidRequest = "invalid request"
|
||||||
)
|
)
|
||||||
|
@ -23,35 +23,37 @@ type KeyFile struct {
|
|||||||
PrivKey Key `json:"priv-key"`
|
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()
|
cfg := cometcfg.DefaultConfig()
|
||||||
jsonFilePath := filepath.Join(rootDir, cfg.PrivValidatorKey)
|
jsonFilePath := filepath.Join(rootDir, cfg.PrivValidatorKey)
|
||||||
|
|
||||||
jsonFile, err := os.Open(jsonFilePath)
|
jsonFile, err := os.Open(jsonFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
GetAppLogger().Error(ctx, "error while opening config", err.Error())
|
GetAppLogger().Error(ctx, "error while opening config", err.Error())
|
||||||
return "", false
|
return
|
||||||
}
|
}
|
||||||
jsonBytes, err := io.ReadAll(jsonFile)
|
jsonBytes, err := io.ReadAll(jsonFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
GetAppLogger().Error(ctx, "error while reading file", err.Error())
|
GetAppLogger().Error(ctx, "error while reading file", err.Error())
|
||||||
return "", false
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var keyFile KeyFile
|
var keyFile KeyFile
|
||||||
err = json.Unmarshal(jsonBytes, &keyFile)
|
err = json.Unmarshal(jsonBytes, &keyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
GetAppLogger().Error(ctx, "error while unmarshaling key file", err.Error())
|
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 {
|
func IsValidatorBlockProposer(ctx sdk.Context, proposerAddress []byte, rootDir string) (result bool) {
|
||||||
validatorIdentity, validResult := GetValidatorCometBFTIdentity(ctx, rootDir)
|
validatorIdentity, err := GetValidatorCometBFTIdentity(ctx, rootDir)
|
||||||
if !validResult {
|
if err != nil {
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
hexProposerAddress := hex.EncodeToString(proposerAddress)
|
hexProposerAddress := hex.EncodeToString(proposerAddress)
|
||||||
return hexProposerAddress == validatorIdentity
|
result = hexProposerAddress == validatorIdentity
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
errorsmod "cosmossdk.io/errors"
|
errorsmod "cosmossdk.io/errors"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/planetmint/planetmint-go/errormsg"
|
||||||
"github.com/planetmint/planetmint-go/util"
|
"github.com/planetmint/planetmint-go/util"
|
||||||
"github.com/planetmint/planetmint-go/x/dao/types"
|
"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())
|
util.GetAppLogger().Info(ctx, distributionRequestTag+"storing distribution: "+msg.GetDistribution().String())
|
||||||
k.StoreDistributionOrder(ctx, *msg.GetDistribution())
|
k.StoreDistributionOrder(ctx, *msg.GetDistribution())
|
||||||
|
|
||||||
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
validatorIdentity, err := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||||
if !validResult || msg.Distribution.GetProposer() != validatorIdentity {
|
if err != nil {
|
||||||
util.GetAppLogger().Info(ctx, distributionRequestTag+"Not the proposer. valid result: %t proposer: %s validator identity: %s", validResult, msg.Distribution.GetProposer(), validatorIdentity)
|
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
|
return &types.MsgDistributionRequestResponse{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,10 +4,15 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/planetmint/planetmint-go/errormsg"
|
||||||
"github.com/planetmint/planetmint-go/util"
|
"github.com/planetmint/planetmint-go/util"
|
||||||
"github.com/planetmint/planetmint-go/x/dao/types"
|
"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) {
|
func (k msgServer) InitPop(goCtx context.Context, msg *types.MsgInitPop) (*types.MsgInitPopResponse, error) {
|
||||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||||
|
|
||||||
@ -19,8 +24,12 @@ func (k msgServer) InitPop(goCtx context.Context, msg *types.MsgInitPop) (*types
|
|||||||
|
|
||||||
k.StoreChallenge(ctx, challenge)
|
k.StoreChallenge(ctx, challenge)
|
||||||
|
|
||||||
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
validatorIdentity, err := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||||
if validResult && msg.Initiator == validatorIdentity {
|
if err != nil {
|
||||||
|
util.GetAppLogger().Error(ctx, initPopTag+errormsg.CouldNotGetValidatorIdentity+": "+err.Error())
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if msg.Initiator == validatorIdentity {
|
||||||
go util.SendMqttPopInitMessagesToServer(ctx, challenge)
|
go util.SendMqttPopInitMessagesToServer(ctx, challenge)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
errorsmod "cosmossdk.io/errors"
|
errorsmod "cosmossdk.io/errors"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
"github.com/planetmint/planetmint-go/errormsg"
|
||||||
"github.com/planetmint/planetmint-go/util"
|
"github.com/planetmint/planetmint-go/util"
|
||||||
"github.com/planetmint/planetmint-go/x/dao/types"
|
"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()
|
reissuance.LastIncludedPop = msg.GetLastIncludedPop()
|
||||||
k.StoreReissuance(ctx, reissuance)
|
k.StoreReissuance(ctx, reissuance)
|
||||||
|
|
||||||
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
validatorIdentity, err := util.GetValidatorCometBFTIdentity(ctx, k.RootDir)
|
||||||
if !validResult || msg.Proposer != validatorIdentity {
|
if err != nil {
|
||||||
util.GetAppLogger().Info(ctx, reissueTag+"Not the proposer. valid result: %t proposer: %s validator identity: %s", validResult, msg.Proposer, validatorIdentity)
|
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
|
return &types.MsgReissueRDDLProposalResponse{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user