Skip to content

Commit

Permalink
Merge pull request #79 from Fairblock/feat/prometheus-metrics
Browse files Browse the repository at this point in the history
Add metrics
  • Loading branch information
p0p3yee authored Dec 29, 2023
2 parents 838bc88 + 8d1bd52 commit cb2e08a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion x/keyshare/keeper/msg_server_send_keyshare.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/hex"
"fairyring/x/keyshare/types"
"fmt"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
"strconv"

distIBE "github.com/FairBlock/DistributedIBE"
Expand Down Expand Up @@ -66,6 +68,7 @@ func (k msgServer) SendKeyshare(goCtx context.Context, msg *types.MsgSendKeyshar
// Parse the keyshare & commitment then verify it
_, _, err := parseKeyShareCommitment(suite, msg.Message, commitments.Commitments[msg.KeyShareIndex-1], uint32(msg.KeyShareIndex), ibeID)
if err != nil {
defer telemetry.IncrCounter(1, types.KeyTotalInvalidKeyShareSubmitted)
k.Logger(ctx).Error(fmt.Sprintf("Error in parsing & verifying keyshare & commitment: %s", err.Error()))
k.Logger(ctx).Error(fmt.Sprintf("KeyShare is: %v | Commitment is: %v | Index: %d", msg.Message, commitments.Commitments, msg.KeyShareIndex))
// Invalid Share, slash validator
Expand Down Expand Up @@ -143,11 +146,12 @@ func (k msgServer) SendKeyshare(goCtx context.Context, msg *types.MsgSendKeyshar
)

// Check if there is an aggregated key exists
_, found = k.GetAggregatedKeyShare(ctx, msg.BlockHeight)
aggrKeyData, found := k.GetAggregatedKeyShare(ctx, msg.BlockHeight)

// If there is not enough keyshares to aggregate OR there is already an aggregated key
// Only continue the code if there is enough keyshare to aggregate & no aggregated key for current height
if int64(len(stateKeyShares)) < expectedThreshold || found {
defer telemetry.IncrCounterWithLabels([]string{types.KeyTotalValidKeyShareSubmitted}, 1, []metrics.Label{telemetry.NewLabel("aggrkey", aggrKeyData.Data)})
return &types.MsgSendKeyshareResponse{
Creator: msg.Creator,
Keyshare: msg.Message,
Expand Down Expand Up @@ -207,6 +211,8 @@ func (k msgServer) SendKeyshare(goCtx context.Context, msg *types.MsgSendKeyshar

k.Logger(ctx).Info(fmt.Sprintf("Aggregated Decryption Key for Block %d: %s", msg.BlockHeight, skHex))

defer telemetry.IncrCounterWithLabels([]string{types.KeyTotalValidKeyShareSubmitted}, 1, []metrics.Label{telemetry.NewLabel("aggrkey", skHex)})

ctx.EventManager().EmitEvent(
sdk.NewEvent(types.KeyShareAggregatedEventType,
sdk.NewAttribute(types.KeyShareAggregatedEventBlockHeight, strconv.FormatUint(msg.BlockHeight, 10)),
Expand Down
2 changes: 2 additions & 0 deletions x/keyshare/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
peptypes "fairyring/x/pep/types"
"fmt"
"github.com/cosmos/cosmos-sdk/telemetry"
"strconv"

// this line is used by starport scaffolding # 1
Expand Down Expand Up @@ -265,6 +266,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val
// After being slashed, his/her last submitted height will be set to the current block
// So he/she won't be slashed in the next block instead he/she will be slashed if he didn't submit for N block again.
am.keeper.SetLastSubmittedHeight(ctx, eachValidator.Validator, strconv.FormatInt(ctx.BlockHeight(), 10))
telemetry.IncrCounter(1, types.KeyTotalIdleValSlashed)
}

return []abci.ValidatorUpdate{}
Expand Down
6 changes: 6 additions & 0 deletions x/keyshare/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ const (
QueuedPubKeyCreatedEventPubkey = "queued-pubkey-created-pubkey"
)

const (
KeyTotalIdleValSlashed = "total_idle_validator_slashed"
KeyTotalValidKeyShareSubmitted = "total_valid_key_share"
KeyTotalInvalidKeyShareSubmitted = "total_invalid_key_share"
)

var (
// PortKey defines the key to store the port ID in store
PortKey = KeyPrefix("keyshare-port-")
Expand Down
3 changes: 3 additions & 0 deletions x/pep/keeper/msg_server_submit_encrypted_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fairyring/x/pep/types"
"fmt"
"github.com/cosmos/cosmos-sdk/telemetry"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -70,5 +71,7 @@ func (k msgServer) SubmitEncryptedTx(goCtx context.Context, msg *types.MsgSubmit
),
)

defer telemetry.IncrCounter(1, types.KeyTotalEncryptedTxSubmitted)

return &types.MsgSubmitEncryptedTxResponse{}, nil
}
5 changes: 4 additions & 1 deletion x/pep/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/telemetry"

cosmosmath "cosmossdk.io/math"

Expand Down Expand Up @@ -230,7 +231,7 @@ func (am AppModule) processFailedEncryptedTx(ctx sdk.Context, tx types.Encrypted
if ctx.GasMeter().GasConsumed() > startConsumedGas {
actualGasConsumed = ctx.GasMeter().GasConsumed() - startConsumedGas
}

defer telemetry.IncrCounter(1, types.KeyTotalFailedEncryptedTx)
am.handleGasConsumption(ctx, creatorAddr, cosmosmath.NewIntFromUint64(actualGasConsumed), tx.ChargedGas)
}

Expand Down Expand Up @@ -538,6 +539,8 @@ func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
sdk.NewAttribute(types.EncryptedTxExecutedEventIndex, strconv.FormatUint(eachTx.Index, 10)),
),
)

telemetry.IncrCounter(1, types.KeyTotalSuccessEncryptedTx)
}

am.keeper.RemoveAllEncryptedTxFromHeight(ctx, h)
Expand Down
6 changes: 6 additions & 0 deletions x/pep/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ const (
KeyShareVerificationReason = "keyshare-verification-reason"
)

const (
KeyTotalEncryptedTxSubmitted = "total_encrypted_tx_submitted"
KeyTotalSuccessEncryptedTx = "total_success_encrypted_tx"
KeyTotalFailedEncryptedTx = "total_failed_encrypted_tx"
)

func KeyPrefix(p string) []byte {
return []byte(p)
}

0 comments on commit cb2e08a

Please sign in to comment.