Added logger a logger struct to log with a TAG (#198)

* added util.looger struct and methods
* extended the logging

---------

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2023-11-27 14:47:03 +01:00 committed by GitHub
parent 6a9c5f4407
commit 2ee9be36dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 57 additions and 27 deletions

View File

@ -4,7 +4,9 @@ import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/util"
"github.com/planetmint/planetmint-go/x/dao"
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
)
@ -15,16 +17,16 @@ func NewCheckReissuanceDecorator() CheckReissuanceDecorator {
}
func (cmad CheckReissuanceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
logger := ctx.Logger()
for _, msg := range tx.GetMsgs() {
if sdk.MsgTypeURL(msg) == "/planetmintgo.dao.MsgReissueRDDLProposal" {
MsgProposal, ok := msg.(*daotypes.MsgReissueRDDLProposal)
if ok {
logger.Debug("REISSUE: receive Proposal")
util.GetAppLogger().Debug(ctx, "REISSUE: receive Proposal")
conf := config.GetConfig()
isValid := dao.IsValidReissuanceCommand(MsgProposal.GetTx(), conf.ReissuanceAsset, MsgProposal.GetBlockHeight())
if !isValid {
logger.Debug("REISSUE: Invalid Proposal")
util.GetAppLogger().Info(ctx, "REISSUE: error during CheckTx or ReCheckTx")
return ctx, errorsmod.Wrapf(daotypes.ErrReissuanceProposal, "error during CheckTx or ReCheckTx")
}
}

View File

@ -25,7 +25,6 @@ type KeyFile struct {
}
func GetValidatorCometBFTIdentity(ctx sdk.Context) (string, bool) {
logger := ctx.Logger()
conf := config.GetConfig()
cfg := cometcfg.DefaultConfig()
@ -33,19 +32,19 @@ func GetValidatorCometBFTIdentity(ctx sdk.Context) (string, bool) {
jsonFile, err := os.Open(jsonFilePath)
if err != nil {
logger.Error("error while opening config", err)
GetAppLogger().Error(ctx, string("error while opening config"), err.Error())
return "", false
}
jsonBytes, err := io.ReadAll(jsonFile)
if err != nil {
logger.Error("error while reading file", err)
GetAppLogger().Error(ctx, "error while reading file", err.Error())
return "", false
}
var keyFile KeyFile
err = json.Unmarshal(jsonBytes, &keyFile)
if err != nil {
logger.Error("error while unmarshaling key file", err)
GetAppLogger().Error(ctx, "error while unmarshaling key file", err.Error())
return "", false
}
return strings.ToLower(keyFile.Address), true

View File

@ -14,10 +14,9 @@ import (
func InitRDDLReissuanceProcess(ctx sdk.Context, proposerAddress string, txUnsigned string, blockHeight int64) error {
//get_last_PoPBlockHeight() // TODO: to be read form the upcoming PoP-store
logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Proposal")
GetAppLogger().Info(ctx, "REISSUE: create Proposal")
cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-proposal",
"--from", sendingValidatorAddress, "-y",
proposerAddress, txUnsigned, strconv.FormatInt(blockHeight, 10))
@ -38,10 +37,9 @@ func InitRDDLReissuanceProcess(ctx sdk.Context, proposerAddress string, txUnsign
}
func SendRDDLReissuanceResult(ctx sdk.Context, proposerAddress string, txID string, blockHeight int64) error {
logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Result")
GetAppLogger().Info(ctx, "REISSUE: create Result")
cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-result",
"--from", sendingValidatorAddress, "-y",
proposerAddress, txID, strconv.FormatInt(blockHeight, 10))
@ -61,10 +59,9 @@ func SendRDDLReissuanceResult(ctx sdk.Context, proposerAddress string, txID stri
}
func SendLiquidAssetRegistration(ctx sdk.Context, notarizedAsset machinetypes.LiquidAsset) error {
logger := ctx.Logger()
// Construct the command
sendingValidatorAddress := config.GetConfig().ValidatorAddress
logger.Debug("REISSUE: create Result")
GetAppLogger().Info(ctx, "REISSUE: create Result")
obj := fmt.Sprintf("{ \"MachineID\": \"%s\", \"MachineAddress\": \"%s\", \"AssetID\": \"%s\", \"Registered\": %t }",
notarizedAsset.MachineID, notarizedAsset.MachineAddress, notarizedAsset.AssetID, notarizedAsset.GetRegistered())
cmd := exec.Command("planetmint-god", "tx", "machine", "notarize-liquid-asset",

31
util/logger.go Normal file
View File

@ -0,0 +1,31 @@
package util
import sdk "github.com/cosmos/cosmos-sdk/types"
type AppLogger struct {
}
var globalApplicationLoggerTag string
func init() {
// Initialize the package-level variable
globalApplicationLoggerTag = "[app] "
}
func GetAppLogger() *AppLogger {
return &AppLogger{}
}
func (logger *AppLogger) Info(ctx sdk.Context, msg string, keyvals ...interface{}) {
ctx.Logger().Info(globalApplicationLoggerTag+msg, keyvals)
}
func (logger *AppLogger) Debug(ctx sdk.Context, msg string, keyvals ...interface{}) {
ctx.Logger().Debug(globalApplicationLoggerTag+msg, keyvals)
}
func (logger *AppLogger) Error(ctx sdk.Context, msg string, keyvals ...interface{}) {
ctx.Logger().Error(globalApplicationLoggerTag+msg, keyvals)
}

View File

@ -12,7 +12,7 @@ import (
)
func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
logger := ctx.Logger()
proposerAddress := req.Header.GetProposerAddress()
// Check if node is block proposer
@ -20,13 +20,13 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper)
if isPoPHeight(req.Header.GetHeight()) && util.IsValidatorBlockProposer(ctx, proposerAddress) {
blockHeight := req.Header.GetHeight()
// TODO: implement PoP trigger
logger.Info("TODO: implement PoP trigger")
util.GetAppLogger().Info(ctx, "TODO: implement PoP trigger")
hexProposerAddress := hex.EncodeToString(proposerAddress)
conf := config.GetConfig()
txUnsigned := GetReissuanceCommand(conf.ReissuanceAsset, blockHeight)
err := util.InitRDDLReissuanceProcess(ctx, hexProposerAddress, txUnsigned, blockHeight)
if err != nil {
logger.Error("error while initializing RDDL issuance", err)
util.GetAppLogger().Error(ctx, "error while initializing RDDL issuance", err)
}
}
}

View File

@ -13,6 +13,7 @@ import (
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/util"
"github.com/planetmint/planetmint-go/x/dao/types"
)
@ -90,7 +91,7 @@ func (k Keeper) DistributeCollectedFees(ctx sdk.Context) {
if found {
err := k.processBalances(ctx, balances, totalStake, coinToDistribute)
if err != nil {
ctx.Logger().Error("Error processing balances:", err)
util.GetAppLogger().Error(ctx, "Error processing balances:", err)
}
}
}

View File

@ -10,22 +10,18 @@ import (
func (k msgServer) ReissueRDDLProposal(goCtx context.Context, msg *types.MsgReissueRDDLProposal) (*types.MsgReissueRDDLProposalResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
logger := ctx.Logger()
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx)
if validResult && msg.Proposer == validatorIdentity {
// 1. sign tx
// 2. broadcast tx
logger.Debug("REISSUE: Asset")
util.GetAppLogger().Info(ctx, "REISSUE: Asset")
txID, err := util.ReissueAsset(msg.Tx)
if err == nil {
// 3. notarize result by notarizing the liquid tx-id
_ = util.SendRDDLReissuanceResult(ctx, msg.GetProposer(), txID, msg.GetBlockHeight())
//TODO verify and resolve error
} else {
logger.Debug("REISSUE: Asset reissuance failure")
util.GetAppLogger().Error(ctx, "REISSUE: Asset reissuance failure")
}
//TODO: reissuance need to be initiated otherwise
}
var reissuance types.Reissuance

View File

@ -53,10 +53,13 @@ func (k msgServer) AttestMachine(goCtx context.Context, msg *types.MsgAttestMach
}
if k.isNFTCreationRequest(msg.Machine) && util.IsValidatorBlockProposer(ctx, ctx.BlockHeader().ProposerAddress) {
util.GetAppLogger().Info(ctx, "Issuing Machine NFT")
err := k.issueMachineNFT(ctx, msg.Machine)
if err != nil {
return nil, types.ErrNFTIssuanceFailed
}
} else {
util.GetAppLogger().Info(ctx, "skipping Machine NFT issuance")
}
k.StoreMachine(ctx, *msg.Machine)
@ -79,8 +82,6 @@ func validateExtendedPublicKey(issuer string, cfg chaincfg.Params) bool {
func (k msgServer) issueNFTAsset(ctx sdk.Context, name string, machineAddress string) (assetID string, contract string, err error) {
conf := config.GetConfig()
logger := ctx.Logger()
cmdName := "poetry"
cmdArgs := []string{"run", "python", "issuer_service/issue2liquid.py", name, machineAddress}
@ -98,9 +99,10 @@ func (k msgServer) issueNFTAsset(ctx sdk.Context, name string, machineAddress st
// Execute the command
err = cmd.Run()
if err != nil {
logger.Error("cmd.Run() failed with %s\n", err)
util.GetAppLogger().Error(ctx, "Issue2Liquid.py failed with %s\n", err)
err = errorsmod.Wrap(types.ErrMachineNFTIssuance, stderr.String())
} else {
util.GetAppLogger().Info(ctx, "Liquid Token Issuance: "+stdout.String())
lines := strings.Split(stdout.String(), "\n")
if len(lines) == 3 {
assetID = lines[0]
@ -171,10 +173,12 @@ func (k msgServer) issueMachineNFT(ctx sdk.Context, machine *types.Machine) erro
notarizedAsset.Registered = true
assetID, contract, err := k.issueNFTAsset(ctx, machine.Name, machine.Address)
if err != nil {
util.GetAppLogger().Error(ctx, err.Error())
return err
}
err = k.registerAsset(assetID, contract)
if err != nil {
util.GetAppLogger().Error(ctx, err.Error())
notarizedAsset.Registered = false
}
// issue message with: