mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-24 06:25:47 +00:00
refactor: goify function
Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
parent
f6f10b54b6
commit
b8433d4279
@ -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
|
||||
}
|
||||
|
||||
@ -28,9 +28,12 @@ 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 {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@ -19,8 +19,11 @@ 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 {
|
||||
return nil, err
|
||||
}
|
||||
if msg.Initiator == validatorIdentity {
|
||||
go util.SendMqttPopInitMessagesToServer(ctx, challenge)
|
||||
}
|
||||
|
||||
|
||||
@ -29,9 +29,12 @@ 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 {
|
||||
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