move IsValidatorBlockProposer to util and add to issueMachineNFT (#118)

Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
Lorenz Herzberger 2023-10-03 17:33:16 +02:00 committed by GitHub
parent 7c31fb1d68
commit 20af6d2c46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 47 deletions

View File

@ -0,0 +1,55 @@
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 IsValidatorBlockProposer(ctx sdk.Context, proposerAddress []byte) bool {
logger := ctx.Logger()
conf := config.GetConfig()
cfg := cometcfg.DefaultConfig()
jsonFilePath := filepath.Join(conf.ConfigRootDir, cfg.PrivValidatorKey)
hexProposerAddress := hex.EncodeToString(proposerAddress)
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 hexProposerAddress == strings.ToLower(keyFile.Address)
}

View File

@ -1,40 +1,23 @@
package dao
import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/crgimenes/go-osc"
"github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/util"
"github.com/planetmint/planetmint-go/x/dao/keeper"
abci "github.com/cometbft/cometbft/abci/types"
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 BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
logger := ctx.Logger()
proposerAddress := req.Header.GetProposerAddress()
// Check if node is block proposer
if isPoPHeight(req.Header.GetHeight()) && isValidatorBlockProposer(ctx, proposerAddress) {
if isPoPHeight(req.Header.GetHeight()) && util.IsValidatorBlockProposer(ctx, proposerAddress) {
// TODO: implement PoP trigger
fmt.Println("TODO: implement PoP trigger")
err := issueRDDL()
@ -60,33 +43,6 @@ func isPoPHeight(height int64) bool {
return height%int64(cfg.PoPEpochs) == 0
}
func isValidatorBlockProposer(ctx sdk.Context, proposerAddress []byte) bool {
logger := ctx.Logger()
conf := config.GetConfig()
cfg := cometcfg.DefaultConfig()
jsonFilePath := filepath.Join(conf.ConfigRootDir, cfg.PrivValidatorKey)
hexProposerAddress := hex.EncodeToString(proposerAddress)
jsonFile, err := os.Open(jsonFilePath)
if err != nil {
logger.Error("error while opening config", err)
}
jsonBytes, err := io.ReadAll(jsonFile)
if err != nil {
logger.Error("error while reading file", err)
}
var keyFile KeyFile
err = json.Unmarshal(jsonBytes, &keyFile)
if err != nil {
logger.Error("error while unmarshaling key file", err)
}
return hexProposerAddress == strings.ToLower(keyFile.Address)
}
func EndBlocker(ctx sdk.Context, req abci.RequestEndBlock, k keeper.Keeper) {
k.DistributeCollectedFees(ctx)
}

View File

@ -42,7 +42,8 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach
if !isValidIssuerLiquid {
return nil, errorsmod.Wrap(types.ErrInvalidKey, "liquid")
}
if k.isNFTCreationRequest(msg.Machine) {
if k.isNFTCreationRequest(msg.Machine) && util.IsValidatorBlockProposer(ctx, ctx.BlockHeader().ProposerAddress) {
err := k.issueMachineNFT(msg.Machine)
if err != nil {
return nil, types.ErrNFTIssuanceFailed