mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-06-06 14:16:39 +00:00
move IsValidatorBlockProposer to util and add to issueMachineNFT (#118)
Signed-off-by: Lorenz Herzberger <lorenzherzberger@gmail.com>
This commit is contained in:
parent
7c31fb1d68
commit
20af6d2c46
55
util/determine_block_proposer.go
Normal file
55
util/determine_block_proposer.go
Normal 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)
|
||||||
|
}
|
@ -1,40 +1,23 @@
|
|||||||
package dao
|
package dao
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/crgimenes/go-osc"
|
"github.com/crgimenes/go-osc"
|
||||||
"github.com/planetmint/planetmint-go/config"
|
"github.com/planetmint/planetmint-go/config"
|
||||||
|
"github.com/planetmint/planetmint-go/util"
|
||||||
"github.com/planetmint/planetmint-go/x/dao/keeper"
|
"github.com/planetmint/planetmint-go/x/dao/keeper"
|
||||||
|
|
||||||
abci "github.com/cometbft/cometbft/abci/types"
|
abci "github.com/cometbft/cometbft/abci/types"
|
||||||
cometcfg "github.com/cometbft/cometbft/config"
|
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
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) {
|
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
|
||||||
logger := ctx.Logger()
|
logger := ctx.Logger()
|
||||||
proposerAddress := req.Header.GetProposerAddress()
|
proposerAddress := req.Header.GetProposerAddress()
|
||||||
|
|
||||||
// Check if node is block proposer
|
// 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
|
// TODO: implement PoP trigger
|
||||||
fmt.Println("TODO: implement PoP trigger")
|
fmt.Println("TODO: implement PoP trigger")
|
||||||
err := issueRDDL()
|
err := issueRDDL()
|
||||||
@ -60,33 +43,6 @@ func isPoPHeight(height int64) bool {
|
|||||||
return height%int64(cfg.PoPEpochs) == 0
|
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) {
|
func EndBlocker(ctx sdk.Context, req abci.RequestEndBlock, k keeper.Keeper) {
|
||||||
k.DistributeCollectedFees(ctx)
|
k.DistributeCollectedFees(ctx)
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,8 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach
|
|||||||
if !isValidIssuerLiquid {
|
if !isValidIssuerLiquid {
|
||||||
return nil, errorsmod.Wrap(types.ErrInvalidKey, "liquid")
|
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)
|
err := k.issueMachineNFT(msg.Machine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrNFTIssuanceFailed
|
return nil, types.ErrNFTIssuanceFailed
|
||||||
|
Loading…
x
Reference in New Issue
Block a user