Skip to content

Commit

Permalink
condense logging for a supply checker
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Nov 16, 2023
1 parent ce3cc5f commit f3eb109
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
15 changes: 15 additions & 0 deletions zetaclient/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"math"
"math/big"
Expand All @@ -17,6 +18,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/evm/erc20custody.sol"
"github.com/zeta-chain/protocol-contracts/pkg/contracts/evm/zetaconnector.non-eth.sol"
"github.com/zeta-chain/zetacore/common"
Expand All @@ -29,6 +31,19 @@ const (
bytesPerKB = 1000
)

func PrettyPrintStruct(val interface{}) string {
prettyStruct, err := json.MarshalIndent(
val,
"",
" ",
)
if err != nil {
log.Fatal().Err(err).Msgf("error marshalling struct")
}

return string(prettyStruct)
}

// feeRateToSatPerByte converts a fee rate in BTC/KB to sat/byte.
func feeRateToSatPerByte(rate float64) *big.Int {
// #nosec G701 always in range
Expand Down
45 changes: 32 additions & 13 deletions zetaclient/zeta_supply_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package zetaclient
import (
"fmt"

"github.com/pkg/errors"

sdkmath "cosmossdk.io/math"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/zeta-chain/zetacore/common"
"github.com/zeta-chain/zetacore/x/crosschain/types"
Expand Down Expand Up @@ -150,27 +149,47 @@ func (zs *ZetaSupplyChecker) CheckZetaTokenSupply() error {
return nil
}

type ZetaSupplyCheckLogs struct {
Logger zerolog.Logger
AbortedTxAmounts sdkmath.Int `json:"aborted_tx_amounts"`
ZetaInTransit sdkmath.Int `json:"zeta_in_transit"`
ExternalChainTotalSupply sdkmath.Int `json:"external_chain_total_supply"`
ZetaTokenSupplyOnNode sdkmath.Int `json:"zeta_token_supply_on_node"`
EthLockedAmount sdkmath.Int `json:"eth_locked_amount"`
NodeAmounts sdkmath.Int `json:"node_amounts"`
Lhs sdkmath.Int `json:"lhs"`

Check warning on line 160 in zetaclient/zeta_supply_checker.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: struct field Lhs should be LHS (revive)

Check warning on line 160 in zetaclient/zeta_supply_checker.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: struct field Lhs should be LHS (revive)
Rhs sdkmath.Int `json:"rhs"`

Check warning on line 161 in zetaclient/zeta_supply_checker.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: struct field Rhs should be RHS (revive)

Check warning on line 161 in zetaclient/zeta_supply_checker.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: struct field Rhs should be RHS (revive)
SupplyCheckSuccess bool `json:"supply_check_success"`
}

func (z ZetaSupplyCheckLogs) LogOutput() {
z.Logger.Info().Msgf(PrettyPrintStruct(z))
}

func ValidateZetaSupply(logger zerolog.Logger, abortedTxAmounts, zetaInTransit, genesisAmounts, externalChainTotalSupply, zetaTokenSupplyOnNode, ethLockedAmount sdkmath.Int) bool {
lhs := ethLockedAmount.Sub(abortedTxAmounts)
rhs := zetaTokenSupplyOnNode.Add(zetaInTransit).Add(externalChainTotalSupply).Sub(genesisAmounts)

copyZetaTokenSupplyOnNode := zetaTokenSupplyOnNode
copyGenesisAmounts := genesisAmounts
nodeAmounts := copyZetaTokenSupplyOnNode.Sub(copyGenesisAmounts)
logger.Info().Msgf("--------------------------------------------------------------------------------")
logger.Info().Msgf("aborted tx amounts : %s", abortedTxAmounts.String())
logger.Info().Msgf("zeta in transit : %s", zetaInTransit.String())
logger.Info().Msgf("external chain total supply : %s", externalChainTotalSupply.String())
logger.Info().Msgf("effective zeta supply on node : %s", nodeAmounts.String())
logger.Info().Msgf("zeta token supply on node : %s", zetaTokenSupplyOnNode.String())
logger.Info().Msgf("genesis amounts : %s", genesisAmounts.String())
logger.Info().Msgf("eth locked amount : %s", ethLockedAmount.String())
logs := ZetaSupplyCheckLogs{
Logger: logger,
AbortedTxAmounts: abortedTxAmounts,
ZetaInTransit: zetaInTransit,
ExternalChainTotalSupply: externalChainTotalSupply,
NodeAmounts: nodeAmounts,
ZetaTokenSupplyOnNode: zetaTokenSupplyOnNode,
EthLockedAmount: ethLockedAmount,
Lhs: lhs,
Rhs: rhs,
}
defer logs.LogOutput()
if !lhs.Equal(rhs) {
logger.Error().Msgf("zeta supply mismatch, lhs : %s , rhs : %s", lhs.String(), rhs.String())
logs.SupplyCheckSuccess = false
return false
}
logger.Info().Msgf("zeta supply check passed, lhs : %s , rhs : %s", lhs.String(), rhs.String())
logger.Info().Msgf("--------------------------------------------------------------------------------")
logs.SupplyCheckSuccess = true
return true
}

Expand Down

0 comments on commit f3eb109

Please sign in to comment.