diff --git a/util/determine_block_proposer.go b/util/determine_block_proposer.go new file mode 100644 index 0000000..0f3705a --- /dev/null +++ b/util/determine_block_proposer.go @@ -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) +} diff --git a/x/dao/abci.go b/x/dao/abci.go index 039a8a5..df29e32 100644 --- a/x/dao/abci.go +++ b/x/dao/abci.go @@ -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) } diff --git a/x/machine/keeper/msg_server_attest_machine.go b/x/machine/keeper/msg_server_attest_machine.go index 03c9723..1e8bdad 100644 --- a/x/machine/keeper/msg_server_attest_machine.go +++ b/x/machine/keeper/msg_server_attest_machine.go @@ -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