Skip to content

Commit

Permalink
Added telemetry and removed unnecessary logs (#535)
Browse files Browse the repository at this point in the history
- Remove unnecessary `fmt.Print` statements
- Added telemetry + new endblock metric for emissions module

---------

Co-authored-by: T <relyt29@users.noreply.github.com>
  • Loading branch information
guilherme-brandao and relyt29 authored Aug 21, 2024
1 parent 91e4388 commit c49aeec
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 45 deletions.
7 changes: 0 additions & 7 deletions app/topics_handler.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package app

import (
"cosmossdk.io/log"

emissionskeeper "github.com/allora-network/allora-chain/x/emissions/keeper"

abci "github.com/cometbft/cometbft/abci/types"
Expand All @@ -23,11 +21,6 @@ func NewTopicsHandler(emissionsKeeper emissionskeeper.Keeper) *TopicsHandler {

func (th *TopicsHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
return func(ctx sdk.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) {
Logger(ctx).Debug("\n ---------------- TopicsHandler ------------------- \n")
return &abci.ResponsePrepareProposal{Txs: req.Txs}, nil
}
}

func Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "topic_handler")
}
2 changes: 0 additions & 2 deletions app/upgrades/v0_3_0/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v0_3_0 //nolint:revive // var-naming: don't use an underscore in package

import (
"context"
"fmt"

upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/allora-network/allora-chain/app/upgrades"
Expand All @@ -23,7 +22,6 @@ func CreateUpgradeHandler(
configurator module.Configurator,
) upgradetypes.UpgradeHandler {
return func(ctx context.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
fmt.Printf("Running upgrade handler for %s\n", UpgradeName)
return moduleManager.RunMigrations(ctx, configurator, vm)
}
}
2 changes: 0 additions & 2 deletions app/upgrades/v0_4_0/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package v0_4_0 //nolint:revive // var-naming: don't use an underscore in package

import (
"context"
"fmt"

upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/allora-network/allora-chain/app/upgrades"
Expand All @@ -23,7 +22,6 @@ func CreateUpgradeHandler(
configurator module.Configurator,
) upgradetypes.UpgradeHandler {
return func(ctx context.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
fmt.Printf("Running upgrade handler for %s\n", UpgradeName)
return moduleManager.RunMigrations(ctx, configurator, vm)
}
}
3 changes: 3 additions & 0 deletions scripts/l1_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ if [ ! -f $INIT_FLAG ]; then
dasel put mempool.max_txs_bytes -t int -v 2097152 -f ${APP_HOME}/config/config.toml
dasel put mempool.size -t int -v 1000 -f ${APP_HOME}/config/config.toml

#* Enable telemetry
dasel put telemetry.enabled -t bool -v true -f ${APP_HOME}/config/app.toml

touch $INIT_FLAG
fi
echo "Node is initialized"
Expand Down
12 changes: 6 additions & 6 deletions test/invariant/invariants.patch
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
diff --git a/x/emissions/module/abci.go b/x/emissions/module/abci.go
index 8f743b31..0511e66d 100644
index d775365..69b81e1 100644
--- a/x/emissions/module/abci.go
+++ b/x/emissions/module/abci.go
@@ -5,6 +5,7 @@ import (
"fmt"
@@ -6,6 +6,7 @@ import (
"time"

"cosmossdk.io/errors"
+ emissionskeeper "github.com/allora-network/allora-chain/x/emissions/keeper"
allorautils "github.com/allora-network/allora-chain/x/emissions/keeper/actor_utils"
"github.com/allora-network/allora-chain/x/emissions/module/rewards"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -12,6 +13,10 @@ import (
emissionstypes "github.com/allora-network/allora-chain/x/emissions/types"
@@ -17,6 +18,10 @@ func EndBlocker(ctx context.Context, am AppModule) error {
defer telemetry.ModuleMeasureSince(emissionstypes.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

func EndBlocker(ctx context.Context, am AppModule) error {
sdkCtx := sdk.UnwrapSDKContext(ctx)
+ invariantMessage, invariantFailed := emissionskeeper.AllInvariants(am.keeper)(sdkCtx)
+ if invariantFailed {
Expand Down
7 changes: 3 additions & 4 deletions x/emissions/keeper/actor_utils/util_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package actorutils

import (
"container/heap"
"fmt"
"math/rand"
"sort"

"github.com/allora-network/allora-chain/x/emissions/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Source: https://pkg.go.dev/container/heap#Push
Expand Down Expand Up @@ -47,7 +47,6 @@ func (pq *PriorityQueue) Push(x any) {
n := len(*pq)
item, ok := x.(*SortableItem)
if !ok {
fmt.Println("Error: Could not cast to SortableItem")
return
}
item.index = n
Expand All @@ -66,7 +65,7 @@ func (pq *PriorityQueue) Pop() any {

// Sorts the given actors by score, desc, breaking ties randomly
// Returns the top N actors as a map with the actor as the key and a boolean (True) as the value
func FindTopNByScoreDesc(n uint64, scoresByActor map[Actor]Score, randSeed BlockHeight) ([]Actor, map[string]bool) {
func FindTopNByScoreDesc(ctx sdk.Context, n uint64, scoresByActor map[Actor]Score, randSeed BlockHeight) ([]Actor, map[string]bool) {
r := rand.New(rand.NewSource(randSeed)) //nolint:gosec // G404: Use of weak random number generator (math/rand or math/rand/v2 instead of crypto/rand)
queue := &PriorityQueue{}
i := 0
Expand Down Expand Up @@ -96,7 +95,7 @@ func FindTopNByScoreDesc(n uint64, scoresByActor map[Actor]Score, randSeed Block
}
item, ok := heap.Pop(queue).(*SortableItem)
if !ok {
fmt.Println("Error: Could not cast to SortableItem")
ctx.Logger().Warn("Error: Could not cast to SortableItem")
continue
}
topN = append(topN, item.Value)
Expand Down
7 changes: 5 additions & 2 deletions x/emissions/keeper/actor_utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package actorutils
import (
"testing"

storetypes "cosmossdk.io/store/types"
alloraMath "github.com/allora-network/allora-chain/math"
"github.com/allora-network/allora-chain/x/emissions/types"
"github.com/cometbft/cometbft/crypto/secp256k1"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func TestFindTopNByScoreDesc(t *testing.T) {

key := storetypes.NewKVStoreKey("test")
topicId := uint64(0)
testCtx := testutil.DefaultContextWithDB(t, key, storetypes.NewTransientStoreKey("transient_test")).Ctx

worker1PrivateKey := secp256k1.GenPrivKey()
worker2PrivateKey := secp256k1.GenPrivKey()
Expand All @@ -33,7 +36,7 @@ func TestFindTopNByScoreDesc(t *testing.T) {
latestReputerScores[worker4Addr.String()] = types.Score{TopicId: topicId, BlockHeight: 1, Address: worker4Addr.String(), Score: alloraMath.NewDecFromInt64(20)}
latestReputerScores[worker5Addr.String()] = types.Score{TopicId: topicId, BlockHeight: 1, Address: worker5Addr.String(), Score: alloraMath.NewDecFromInt64(100)}

topActors, topActorsBool := FindTopNByScoreDesc(3, latestReputerScores, 1)
topActors, topActorsBool := FindTopNByScoreDesc(testCtx, 3, latestReputerScores, 1)
require.Equal(t, worker5Addr.String(), topActors[0])
require.Equal(t, worker1Addr.String(), topActors[1])
require.Equal(t, worker3Addr.String(), topActors[2])
Expand Down
1 change: 0 additions & 1 deletion x/emissions/keeper/inference_synthesis/network_regrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ func GetCalcSetNetworkRegrets(
if err != nil {
return errorsmod.Wrapf(err, "Error computing and building one-in forecaster regret")
}
// fmt.Printf("regret %v, forecaster %s, inferer %s, value %s\n", newOneInForecasterRegret.Value, oneInForecasterLoss.Worker, infererLoss.Worker, newOneInForecasterRegret.Value.String())
err = k.SetOneInForecasterNetworkRegret(ctx, topicId, oneInForecasterLoss.Worker, infererLoss.Worker, newOneInForecasterRegret)
if err != nil {
return errorsmod.Wrapf(err, "Error setting one-in forecaster regret")
Expand Down
6 changes: 3 additions & 3 deletions x/emissions/keeper/msgserver/msg_server_worker_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
// A tx function that accepts a individual inference and forecast and possibly returns an error
// Need to call this once per forecaster per topic inference solicitation round because protobuf does not nested repeated fields
func (ms msgServer) InsertWorkerPayload(ctx context.Context, msg *types.MsgInsertWorkerPayload) (*types.MsgInsertWorkerPayloadResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
blockHeight := sdkCtx.BlockHeight()
_, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
Expand All @@ -21,8 +23,6 @@ func (ms msgServer) InsertWorkerPayload(ctx context.Context, msg *types.MsgInser
return nil, err
}

blockHeight := sdk.UnwrapSDKContext(ctx).BlockHeight()

if err := msg.WorkerDataBundle.Validate(); err != nil {
return nil, errorsmod.Wrapf(types.ErrInvalidWorkerData,
"Worker invalid data for block: %d", blockHeight)
Expand Down Expand Up @@ -129,7 +129,7 @@ func (ms msgServer) InsertWorkerPayload(ctx context.Context, msg *types.MsgInser
if err != nil {
return nil, err
}
_, topNInferer := actorutils.FindTopNByScoreDesc(moduleParams.MaxElementsPerForecast, latestScoresForForecastedInferers, forecast.BlockHeight)
_, topNInferer := actorutils.FindTopNByScoreDesc(sdkCtx, moduleParams.MaxElementsPerForecast, latestScoresForForecastedInferers, forecast.BlockHeight)

for _, el := range forecast.ForecastElements {
if !seenInferers[el.Inferer] && topNInferer[el.Inferer] {
Expand Down
5 changes: 5 additions & 0 deletions x/emissions/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package module
import (
"context"
"fmt"
"time"

"cosmossdk.io/errors"
allorautils "github.com/allora-network/allora-chain/x/emissions/keeper/actor_utils"
"github.com/allora-network/allora-chain/x/emissions/module/rewards"
emissionstypes "github.com/allora-network/allora-chain/x/emissions/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func EndBlocker(ctx context.Context, am AppModule) error {
defer telemetry.ModuleMeasureSince(emissionstypes.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker)

sdkCtx := sdk.UnwrapSDKContext(ctx)
blockHeight := sdkCtx.BlockHeight()
sdkCtx.Logger().Debug(
Expand Down
16 changes: 7 additions & 9 deletions x/emissions/types/events.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"fmt"

alloraMath "github.com/allora-network/allora-chain/math"
sdk "github.com/cosmos/cosmos-sdk/types"
proto "github.com/cosmos/gogoproto/proto"
Expand All @@ -16,7 +14,7 @@ func EmitNewInfererScoresSetEvent(ctx sdk.Context, scores []Score) {
}
err := ctx.EventManager().EmitTypedEvent(NewScoresSetEventBase(ActorType_INFERER, scores))
if err != nil {
fmt.Println("Error emitting NewInfererScoresSetEvent: ", err)
ctx.Logger().Warn("Error emitting NewInfererScoresSetEvent: ", err.Error())
}
}

Expand All @@ -26,7 +24,7 @@ func EmitNewForecasterScoresSetEvent(ctx sdk.Context, scores []Score) {
}
err := ctx.EventManager().EmitTypedEvent(NewScoresSetEventBase(ActorType_FORECASTER, scores))
if err != nil {
fmt.Println("Error emitting NewForecasterScoresSetEvent: ", err)
ctx.Logger().Warn("Error emitting NewForecasterScoresSetEvent: ", err.Error())
}
}

Expand All @@ -36,14 +34,14 @@ func EmitNewReputerScoresSetEvent(ctx sdk.Context, scores []Score) {
}
err := ctx.EventManager().EmitTypedEvent(NewScoresSetEventBase(ActorType_REPUTER, scores))
if err != nil {
fmt.Println("Error emitting NewReputerScoresSetEvent: ", err)
ctx.Logger().Warn("Error emitting NewReputerScoresSetEvent: ", err.Error())
}
}

func EmitNewNetworkLossSetEvent(ctx sdk.Context, topicId TopicId, blockHeight BlockHeight, lossBundle ValueBundle) {
err := ctx.EventManager().EmitTypedEvent(NewNetworkLossSetEventBase(topicId, blockHeight, lossBundle))
if err != nil {
fmt.Println("Error emitting NewNetworkLossSetEvent: ", err)
ctx.Logger().Warn("Error emitting NewNetworkLossSetEvent: ", err.Error())
}
}

Expand All @@ -53,7 +51,7 @@ func EmitNewInfererRewardsSettledEvent(ctx sdk.Context, blockHeight BlockHeight,
}
err := ctx.EventManager().EmitTypedEvent(NewRewardsSetEventBase(ActorType_INFERER, blockHeight, rewards))
if err != nil {
fmt.Println("Error emitting NewInfererRewardsSettledEvent: ", err)
ctx.Logger().Warn("Error emitting NewInfererRewardsSettledEvent: ", err.Error())
}
}

Expand All @@ -63,7 +61,7 @@ func EmitNewForecasterRewardsSettledEvent(ctx sdk.Context, blockHeight BlockHeig
}
err := ctx.EventManager().EmitTypedEvent(NewRewardsSetEventBase(ActorType_FORECASTER, blockHeight, rewards))
if err != nil {
fmt.Println("Error emitting NewForecasterRewardsSettledEvent: ", err)
ctx.Logger().Warn("Error emitting NewForecasterRewardsSettledEvent: ", err.Error())
}
}

Expand All @@ -73,7 +71,7 @@ func EmitNewReputerAndDelegatorRewardsSettledEvent(ctx sdk.Context, blockHeight
}
err := ctx.EventManager().EmitTypedEvent(NewRewardsSetEventBase(ActorType_REPUTER, blockHeight, rewards))
if err != nil {
fmt.Println("Error emitting NewReputerAndDelegatorRewardsSettledEvent: ", err)
ctx.Logger().Warn("Error emitting NewReputerAndDelegatorRewardsSettledEvent: ", err.Error())
}
}

Expand Down
2 changes: 0 additions & 2 deletions x/ibc/testing/ibc_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package testing

import (
"encoding/json"
"fmt"
"testing"

"cosmossdk.io/log"
Expand Down Expand Up @@ -55,7 +54,6 @@ func alloraAppInitializer() (ibctesting.TestingApp, map[string]json.RawMessage)
simtestutil.EmptyAppOptions{},
)
if err != nil {
fmt.Printf("Initializing app error: %v\n", err)
return nil, nil
}

Expand Down
7 changes: 0 additions & 7 deletions x/mint/keeper/emissions_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package keeper_test

import (
"fmt"

cosmosMath "cosmossdk.io/math"
alloraMath "github.com/allora-network/allora-chain/math"
"github.com/allora-network/allora-chain/test/testutil"
Expand Down Expand Up @@ -146,11 +144,6 @@ func (s *IntegrationTestSuite) TestEHatTargetFromCsv() {
s.Require().NoError(err)
networkTokensStaked, err := epoch("network_tokens_staked").SdkIntTrim()
s.Require().NoError(err)
fmt.Println("f_emission", simulatorFEmission)
fmt.Println("ecosystem_tokens_total", ecosystemTokensTotal)
fmt.Println("network_tokens_circulating", networkTokensCirculating)
fmt.Println("network_tokens_staked", networkTokensStaked)
fmt.Println("network_tokens_total", networkTokensTotal)
result, err := keeper.GetTargetRewardEmissionPerUnitStakedToken(
simulatorFEmission,
ecosystemTokensTotal,
Expand Down

0 comments on commit c49aeec

Please sign in to comment.