mirror of
https://github.com/planetmint/planetmint-go.git
synced 2025-11-27 15:53:41 +00:00
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:
parent
6a9c5f4407
commit
2ee9be36dc
@ -4,7 +4,9 @@ import (
|
|||||||
errorsmod "cosmossdk.io/errors"
|
errorsmod "cosmossdk.io/errors"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"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"
|
"github.com/planetmint/planetmint-go/x/dao"
|
||||||
|
|
||||||
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
|
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) {
|
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() {
|
for _, msg := range tx.GetMsgs() {
|
||||||
if sdk.MsgTypeURL(msg) == "/planetmintgo.dao.MsgReissueRDDLProposal" {
|
if sdk.MsgTypeURL(msg) == "/planetmintgo.dao.MsgReissueRDDLProposal" {
|
||||||
MsgProposal, ok := msg.(*daotypes.MsgReissueRDDLProposal)
|
MsgProposal, ok := msg.(*daotypes.MsgReissueRDDLProposal)
|
||||||
if ok {
|
if ok {
|
||||||
logger.Debug("REISSUE: receive Proposal")
|
util.GetAppLogger().Debug(ctx, "REISSUE: receive Proposal")
|
||||||
conf := config.GetConfig()
|
conf := config.GetConfig()
|
||||||
isValid := dao.IsValidReissuanceCommand(MsgProposal.GetTx(), conf.ReissuanceAsset, MsgProposal.GetBlockHeight())
|
isValid := dao.IsValidReissuanceCommand(MsgProposal.GetTx(), conf.ReissuanceAsset, MsgProposal.GetBlockHeight())
|
||||||
if !isValid {
|
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")
|
return ctx, errorsmod.Wrapf(daotypes.ErrReissuanceProposal, "error during CheckTx or ReCheckTx")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,7 +25,6 @@ type KeyFile struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetValidatorCometBFTIdentity(ctx sdk.Context) (string, bool) {
|
func GetValidatorCometBFTIdentity(ctx sdk.Context) (string, bool) {
|
||||||
logger := ctx.Logger()
|
|
||||||
conf := config.GetConfig()
|
conf := config.GetConfig()
|
||||||
|
|
||||||
cfg := cometcfg.DefaultConfig()
|
cfg := cometcfg.DefaultConfig()
|
||||||
@ -33,19 +32,19 @@ func GetValidatorCometBFTIdentity(ctx sdk.Context) (string, bool) {
|
|||||||
|
|
||||||
jsonFile, err := os.Open(jsonFilePath)
|
jsonFile, err := os.Open(jsonFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("error while opening config", err)
|
GetAppLogger().Error(ctx, string("error while opening config"), err.Error())
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
jsonBytes, err := io.ReadAll(jsonFile)
|
jsonBytes, err := io.ReadAll(jsonFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("error while reading file", err)
|
GetAppLogger().Error(ctx, "error while reading file", err.Error())
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
var keyFile KeyFile
|
var keyFile KeyFile
|
||||||
err = json.Unmarshal(jsonBytes, &keyFile)
|
err = json.Unmarshal(jsonBytes, &keyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("error while unmarshaling key file", err)
|
GetAppLogger().Error(ctx, "error while unmarshaling key file", err.Error())
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
return strings.ToLower(keyFile.Address), true
|
return strings.ToLower(keyFile.Address), true
|
||||||
|
|||||||
@ -14,10 +14,9 @@ import (
|
|||||||
|
|
||||||
func InitRDDLReissuanceProcess(ctx sdk.Context, proposerAddress string, txUnsigned string, blockHeight int64) error {
|
func InitRDDLReissuanceProcess(ctx sdk.Context, proposerAddress string, txUnsigned string, blockHeight int64) error {
|
||||||
//get_last_PoPBlockHeight() // TODO: to be read form the upcoming PoP-store
|
//get_last_PoPBlockHeight() // TODO: to be read form the upcoming PoP-store
|
||||||
logger := ctx.Logger()
|
|
||||||
// Construct the command
|
// Construct the command
|
||||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
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",
|
cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-proposal",
|
||||||
"--from", sendingValidatorAddress, "-y",
|
"--from", sendingValidatorAddress, "-y",
|
||||||
proposerAddress, txUnsigned, strconv.FormatInt(blockHeight, 10))
|
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 {
|
func SendRDDLReissuanceResult(ctx sdk.Context, proposerAddress string, txID string, blockHeight int64) error {
|
||||||
logger := ctx.Logger()
|
|
||||||
// Construct the command
|
// Construct the command
|
||||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
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",
|
cmd := exec.Command("planetmint-god", "tx", "dao", "reissue-rddl-result",
|
||||||
"--from", sendingValidatorAddress, "-y",
|
"--from", sendingValidatorAddress, "-y",
|
||||||
proposerAddress, txID, strconv.FormatInt(blockHeight, 10))
|
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 {
|
func SendLiquidAssetRegistration(ctx sdk.Context, notarizedAsset machinetypes.LiquidAsset) error {
|
||||||
logger := ctx.Logger()
|
|
||||||
// Construct the command
|
// Construct the command
|
||||||
sendingValidatorAddress := config.GetConfig().ValidatorAddress
|
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 }",
|
obj := fmt.Sprintf("{ \"MachineID\": \"%s\", \"MachineAddress\": \"%s\", \"AssetID\": \"%s\", \"Registered\": %t }",
|
||||||
notarizedAsset.MachineID, notarizedAsset.MachineAddress, notarizedAsset.AssetID, notarizedAsset.GetRegistered())
|
notarizedAsset.MachineID, notarizedAsset.MachineAddress, notarizedAsset.AssetID, notarizedAsset.GetRegistered())
|
||||||
cmd := exec.Command("planetmint-god", "tx", "machine", "notarize-liquid-asset",
|
cmd := exec.Command("planetmint-god", "tx", "machine", "notarize-liquid-asset",
|
||||||
|
|||||||
31
util/logger.go
Normal file
31
util/logger.go
Normal 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)
|
||||||
|
}
|
||||||
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
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()
|
|
||||||
proposerAddress := req.Header.GetProposerAddress()
|
proposerAddress := req.Header.GetProposerAddress()
|
||||||
|
|
||||||
// Check if node is block proposer
|
// 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) {
|
if isPoPHeight(req.Header.GetHeight()) && util.IsValidatorBlockProposer(ctx, proposerAddress) {
|
||||||
blockHeight := req.Header.GetHeight()
|
blockHeight := req.Header.GetHeight()
|
||||||
// TODO: implement PoP trigger
|
// TODO: implement PoP trigger
|
||||||
logger.Info("TODO: implement PoP trigger")
|
util.GetAppLogger().Info(ctx, "TODO: implement PoP trigger")
|
||||||
hexProposerAddress := hex.EncodeToString(proposerAddress)
|
hexProposerAddress := hex.EncodeToString(proposerAddress)
|
||||||
conf := config.GetConfig()
|
conf := config.GetConfig()
|
||||||
txUnsigned := GetReissuanceCommand(conf.ReissuanceAsset, blockHeight)
|
txUnsigned := GetReissuanceCommand(conf.ReissuanceAsset, blockHeight)
|
||||||
err := util.InitRDDLReissuanceProcess(ctx, hexProposerAddress, txUnsigned, blockHeight)
|
err := util.InitRDDLReissuanceProcess(ctx, hexProposerAddress, txUnsigned, blockHeight)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("error while initializing RDDL issuance", err)
|
util.GetAppLogger().Error(ctx, "error while initializing RDDL issuance", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import (
|
|||||||
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
||||||
|
|
||||||
"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/types"
|
"github.com/planetmint/planetmint-go/x/dao/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -90,7 +91,7 @@ func (k Keeper) DistributeCollectedFees(ctx sdk.Context) {
|
|||||||
if found {
|
if found {
|
||||||
err := k.processBalances(ctx, balances, totalStake, coinToDistribute)
|
err := k.processBalances(ctx, balances, totalStake, coinToDistribute)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Logger().Error("Error processing balances:", err)
|
util.GetAppLogger().Error(ctx, "Error processing balances:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,22 +10,18 @@ import (
|
|||||||
|
|
||||||
func (k msgServer) ReissueRDDLProposal(goCtx context.Context, msg *types.MsgReissueRDDLProposal) (*types.MsgReissueRDDLProposalResponse, error) {
|
func (k msgServer) ReissueRDDLProposal(goCtx context.Context, msg *types.MsgReissueRDDLProposal) (*types.MsgReissueRDDLProposalResponse, error) {
|
||||||
ctx := sdk.UnwrapSDKContext(goCtx)
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
||||||
logger := ctx.Logger()
|
|
||||||
|
|
||||||
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx)
|
validatorIdentity, validResult := util.GetValidatorCometBFTIdentity(ctx)
|
||||||
if validResult && msg.Proposer == validatorIdentity {
|
if validResult && msg.Proposer == validatorIdentity {
|
||||||
// 1. sign tx
|
util.GetAppLogger().Info(ctx, "REISSUE: Asset")
|
||||||
// 2. broadcast tx
|
|
||||||
logger.Debug("REISSUE: Asset")
|
|
||||||
txID, err := util.ReissueAsset(msg.Tx)
|
txID, err := util.ReissueAsset(msg.Tx)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// 3. notarize result by notarizing the liquid tx-id
|
// 3. notarize result by notarizing the liquid tx-id
|
||||||
_ = util.SendRDDLReissuanceResult(ctx, msg.GetProposer(), txID, msg.GetBlockHeight())
|
_ = util.SendRDDLReissuanceResult(ctx, msg.GetProposer(), txID, msg.GetBlockHeight())
|
||||||
//TODO verify and resolve error
|
|
||||||
} else {
|
} 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
|
var reissuance types.Reissuance
|
||||||
|
|||||||
@ -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) {
|
if k.isNFTCreationRequest(msg.Machine) && util.IsValidatorBlockProposer(ctx, ctx.BlockHeader().ProposerAddress) {
|
||||||
|
util.GetAppLogger().Info(ctx, "Issuing Machine NFT")
|
||||||
err := k.issueMachineNFT(ctx, msg.Machine)
|
err := k.issueMachineNFT(ctx, msg.Machine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, types.ErrNFTIssuanceFailed
|
return nil, types.ErrNFTIssuanceFailed
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
util.GetAppLogger().Info(ctx, "skipping Machine NFT issuance")
|
||||||
}
|
}
|
||||||
|
|
||||||
k.StoreMachine(ctx, *msg.Machine)
|
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) {
|
func (k msgServer) issueNFTAsset(ctx sdk.Context, name string, machineAddress string) (assetID string, contract string, err error) {
|
||||||
conf := config.GetConfig()
|
conf := config.GetConfig()
|
||||||
logger := ctx.Logger()
|
|
||||||
|
|
||||||
cmdName := "poetry"
|
cmdName := "poetry"
|
||||||
cmdArgs := []string{"run", "python", "issuer_service/issue2liquid.py", name, machineAddress}
|
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
|
// Execute the command
|
||||||
err = cmd.Run()
|
err = cmd.Run()
|
||||||
if err != nil {
|
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())
|
err = errorsmod.Wrap(types.ErrMachineNFTIssuance, stderr.String())
|
||||||
} else {
|
} else {
|
||||||
|
util.GetAppLogger().Info(ctx, "Liquid Token Issuance: "+stdout.String())
|
||||||
lines := strings.Split(stdout.String(), "\n")
|
lines := strings.Split(stdout.String(), "\n")
|
||||||
if len(lines) == 3 {
|
if len(lines) == 3 {
|
||||||
assetID = lines[0]
|
assetID = lines[0]
|
||||||
@ -171,10 +173,12 @@ func (k msgServer) issueMachineNFT(ctx sdk.Context, machine *types.Machine) erro
|
|||||||
notarizedAsset.Registered = true
|
notarizedAsset.Registered = true
|
||||||
assetID, contract, err := k.issueNFTAsset(ctx, machine.Name, machine.Address)
|
assetID, contract, err := k.issueNFTAsset(ctx, machine.Name, machine.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
util.GetAppLogger().Error(ctx, err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = k.registerAsset(assetID, contract)
|
err = k.registerAsset(assetID, contract)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
util.GetAppLogger().Error(ctx, err.Error())
|
||||||
notarizedAsset.Registered = false
|
notarizedAsset.Registered = false
|
||||||
}
|
}
|
||||||
// issue message with:
|
// issue message with:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user