Skip to content

Commit

Permalink
Merge remote-tracking branch 'pokt/main' into issues/553/fix/relayer-…
Browse files Browse the repository at this point in the history
…session-manager

* pokt/main:
  [Observables] chore: add `ReplayObservable#SubscribeFromLatestBufferedOffset()` (#647)
  [Observability] Add claim relays counter (#644)
  [Code Health] chore: log unused error when updating relay mining difficulty (#683)
  [Testing] chore: uncomment proof CLI query tests (#668)
  • Loading branch information
bryanchriswhite committed Jul 12, 2024
2 parents 23bc4a9 + 208a228 commit 2ce42b8
Show file tree
Hide file tree
Showing 10 changed files with 545 additions and 295 deletions.
311 changes: 240 additions & 71 deletions localnet/grafana-dashboards/stress-test-dashboard.json

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions telemetry/event_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,33 @@ func ClaimComputeUnitsCounter(
)
}

// ClaimRelaysCounter increments a counter which tracks the number of relays
// represented by on-chain claims at the given ClaimProofStage.
// If err is not nil, the counter is not incremented and an "error" label is added
// with the error's message. I.e., Prometheus will ingest this event.
func ClaimRelaysCounter(
claimProofStage prooftypes.ClaimProofStage,
numRelays uint64,
err error,
) {
incrementAmount := numRelays
labels := []metrics.Label{
{Name: "unit", Value: "relays"},
{Name: "claim_proof_stage", Value: claimProofStage.String()},
}

// Ensure the counter is not incremented if there was an error.
if err != nil {
incrementAmount = 0
}

telemetry.IncrCounterWithLabels(
[]string{eventTypeMetricKey},
float32(incrementAmount),
labels,
)
}

// ClaimCounter increments a counter which tracks the number of claims at the given
// ClaimProofStage.
// If err is not nil, the counter is not incremented but Prometheus will ingest this event.
Expand Down
8 changes: 2 additions & 6 deletions x/proof/keeper/msg_server_create_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ func (k msgServer) CreateClaim(
defer func() {
// Only increment these metrics counters if handling a new claim.
if !isExistingClaim {
// TODO_IMPROVE: We could track on-chain relays here with claim.GetNumRelays().
telemetry.ClaimCounter(types.ClaimProofStage_CLAIMED, 1, err)
telemetry.ClaimComputeUnitsCounter(
types.ClaimProofStage_CLAIMED,
numComputeUnits,
err,
)
telemetry.ClaimRelaysCounter(types.ClaimProofStage_CLAIMED, numRelays, err)
telemetry.ClaimComputeUnitsCounter(types.ClaimProofStage_CLAIMED, numComputeUnits, err)
}
}()

Expand Down
8 changes: 2 additions & 6 deletions x/proof/keeper/msg_server_submit_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,9 @@ func (k msgServer) SubmitProof(
defer func() {
// Only increment these metrics counters if handling a new claim.
if !isExistingProof {
// TODO_IMPROVE: We could track on-chain relays here with claim.GetNumRelays().
telemetry.ClaimCounter(types.ClaimProofStage_PROVEN, 1, err)
telemetry.ClaimComputeUnitsCounter(
types.ClaimProofStage_PROVEN,
numComputeUnits,
err,
)
telemetry.ClaimRelaysCounter(types.ClaimProofStage_PROVEN, numRelays, err)
telemetry.ClaimComputeUnitsCounter(types.ClaimProofStage_PROVEN, numComputeUnits, err)
}
}()

Expand Down
319 changes: 171 additions & 148 deletions x/proof/module/query_proof_test.go
Original file line number Diff line number Diff line change
@@ -1,150 +1,173 @@
package proof_test

// TODO_BLOCKER(@Olshansk): Add these tests back in after merging on-chain Proof persistence.
// Prevent strconv unused error
// var _ = strconv.IntSize
//
// func networkWithProofObjects(t *testing.T, n int) (*network.Network, []types.Proof) {
// t.Helper()
// cfg := network.DefaultConfig()
// state := types.GenesisState{}
// for i := 0; i < n; i++ {
// proof := types.Proof{
// Index: strconv.Itoa(i),
//
// }
// nullify.Fill(&proof)
// state.ProofList = append(state.ProofList, proof)
// }
// buf, err := cfg.Codec.MarshalJSON(&state)
// require.NoError(t, err)
// cfg.GenesisState[types.ModuleName] = buf
// return network.New(t, cfg), state.ProofList
// }
//
// func TestShowProof(t *testing.T) {
// net, proofs := networkWithProofObjects(t, 2)
//
// ctx := net.Validators[0].ClientCtx
// common := []string{
// fmt.Sprintf("--%s=json", tmcli.OutputFlag),
// }
// tests := []struct {
// desc string
// idIndex string
//
// args []string
// expectedErr error
// proof types.Proof
// }{
// {
// desc: "found",
// idIndex: proofs[0].Index,
//
// args: common,
// proof: proofs[0],
// },
// {
// desc: "not found",
// idIndex: strconv.Itoa(100000),
//
// args: common,
// expectedErr: status.Error(codes.NotFound, "not found"),
// },
// }
// for _, test := range tests {
// t.Run(test.desc, func(t *testing.T) {
// args := []string{
// test.idIndex,
//
// }
// args = append(args, test.args...)
// out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdShowProof(), args)
// if test.expectedErr != nil {
// stat, ok := status.FromError(test.expectedErr)
// require.True(t, ok)
// require.ErrorIs(t, stat.Err(), test.expectedErr)
// } else {
// require.NoError(t, err)
// var resp types.QueryGetProofResponse
// require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
// require.NotNil(t, resp.Proof)
// require.Equal(t,
// nullify.Fill(&test.proof),
// nullify.Fill(&resp.Proof),
// )
// }
// })
// }
// }
//
// func TestListProof(t *testing.T) {
// net, proofs := networkWithProofObjects(t, 5)
//
// ctx := net.Validators[0].ClientCtx
// request := func(next []byte, offset, limit uint64, total bool) []string {
// args := []string{
// fmt.Sprintf("--%s=json", tmcli.OutputFlag),
// }
// if next == nil {
// args = append(args, fmt.Sprintf("--%s=%d", flags.FlagOffset, offset))
// } else {
// args = append(args, fmt.Sprintf("--%s=%s", flags.FlagPageKey, next))
// }
// args = append(args, fmt.Sprintf("--%s=%d", flags.FlagLimit, limit))
// if total {
// args = append(args, fmt.Sprintf("--%s", flags.FlagCountTotal))
// }
// return args
// }
//
// t.Run("ByOffset", func(t *testing.T) {
// step := 2
// for i := 0; i < len(proofs); i += step {
// args := request(nil, uint64(i), uint64(step), false)
// out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListProof(), args)
// require.NoError(t, err)
// var resp types.QueryAllProofsResponse
// require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
// require.LessOrEqual(t, len(resp.Proof), step)
// require.Subset(t,
// nullify.Fill(proofs),
// nullify.Fill(resp.Proof),
// )
// }
// })
//
// t.Run("ByKey", func(t *testing.T) {
// step := 2
// var next []byte
// for i := 0; i < len(proofs); i += step {
// args := request(next, 0, uint64(step), false)
// out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListProof(), args)
// require.NoError(t, err)
// var resp types.QueryAllProofsResponse
// require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
// require.LessOrEqual(t, len(resp.Proof), step)
// require.Subset(t,
// nullify.Fill(proofs),
// nullify.Fill(resp.Proof),
// )
// next = resp.Pagination.NextKey
// }
// })
//
// TODO_TEST: add "BySupplierAddress", "BySession", "ByHeight" tests.
//
// t.Run("Total", func(t *testing.T) {
// args := request(nil, 0, uint64(len(proofs)), true)
// out, err := clitestutil.ExecTestCLICmd(ctx, cli.CmdListProof(), args)
// require.NoError(t, err)
// var resp types.QueryAllProofsResponse
// require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
// require.NoError(t, err)
// require.Equal(t, len(proofs), int(resp.Pagination.Total))
// require.ElementsMatch(t,
// nullify.Fill(proofs),
// nullify.Fill(resp.Proof),
// )
// })
// }
import (
"fmt"
"testing"

cometcli "github.com/cometbft/cometbft/libs/cli"
"github.com/cosmos/cosmos-sdk/client/flags"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/pokt-network/poktroll/testutil/network"
"github.com/pokt-network/poktroll/testutil/nullify"
"github.com/pokt-network/poktroll/testutil/sample"
proof "github.com/pokt-network/poktroll/x/proof/module"
"github.com/pokt-network/poktroll/x/proof/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
)

func networkWithProofObjects(t *testing.T, n int) (*network.Network, []types.Proof) {
t.Helper()
cfg := network.DefaultConfig()
state := types.GenesisState{}
for i := 0; i < n; i++ {
proof := types.Proof{
SupplierAddress: sample.AccAddress(),
SessionHeader: &sessiontypes.SessionHeader{
SessionId: "mock_session_id",
// Other fields omitted and unused for these tests.
},
// CloseseMerkleProof not required for these tests.
ClosestMerkleProof: nil,
}
nullify.Fill(&proof)
state.ProofList = append(state.ProofList, proof)
}
buf, err := cfg.Codec.MarshalJSON(&state)
require.NoError(t, err)
cfg.GenesisState[types.ModuleName] = buf
return network.New(t, cfg), state.ProofList
}

func TestShowProof(t *testing.T) {
net, proofs := networkWithProofObjects(t, 2)

ctx := net.Validators[0].ClientCtx
common := []string{
fmt.Sprintf("--%s=json", cometcli.OutputFlag),
}
tests := []struct {
desc string
sessionId string
supplierAddr string

args []string
expectedErr error
proof types.Proof
}{
{
desc: "found",
supplierAddr: proofs[0].SupplierAddress,
sessionId: proofs[0].SessionHeader.SessionId,

args: common,
proof: proofs[0],
},
{
desc: "not found",
supplierAddr: sample.AccAddress(),
sessionId: proofs[0].SessionHeader.SessionId,

args: common,
expectedErr: status.Error(codes.NotFound, "not found"),
},
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
args := []string{
test.sessionId,
test.supplierAddr,
}
args = append(args, test.args...)
out, err := clitestutil.ExecTestCLICmd(ctx, proof.CmdShowProof(), args)
if test.expectedErr != nil {
stat, ok := status.FromError(test.expectedErr)
require.True(t, ok)
require.ErrorIs(t, stat.Err(), test.expectedErr)
} else {
require.NoError(t, err)
var resp types.QueryGetProofResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NotNil(t, resp.Proof)
require.Equal(t,
nullify.Fill(&test.proof),
nullify.Fill(&resp.Proof),
)
}
})
}
}

func TestListProof(t *testing.T) {
net, proofs := networkWithProofObjects(t, 5)

ctx := net.Validators[0].ClientCtx
request := func(next []byte, offset, limit uint64, total bool) []string {
args := []string{
fmt.Sprintf("--%s=json", cometcli.OutputFlag),
}
if next == nil {
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagOffset, offset))
} else {
args = append(args, fmt.Sprintf("--%s=%s", flags.FlagPageKey, next))
}
args = append(args, fmt.Sprintf("--%s=%d", flags.FlagLimit, limit))
if total {
args = append(args, fmt.Sprintf("--%s", flags.FlagCountTotal))
}
return args
}

t.Run("ByOffset", func(t *testing.T) {
step := 2
for i := 0; i < len(proofs); i += step {
args := request(nil, uint64(i), uint64(step), false)
out, err := clitestutil.ExecTestCLICmd(ctx, proof.CmdListProof(), args)
require.NoError(t, err)
var resp types.QueryAllProofsResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.Proofs), step)
require.Subset(t,
nullify.Fill(proofs),
nullify.Fill(resp.Proofs),
)
}
})

t.Run("ByKey", func(t *testing.T) {
step := 2
var next []byte
for i := 0; i < len(proofs); i += step {
args := request(next, 0, uint64(step), false)
out, err := clitestutil.ExecTestCLICmd(ctx, proof.CmdListProof(), args)
require.NoError(t, err)
var resp types.QueryAllProofsResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.LessOrEqual(t, len(resp.Proofs), step)
require.Subset(t,
nullify.Fill(proofs),
nullify.Fill(resp.Proofs),
)
next = resp.Pagination.NextKey
}
})

//TODO_TEST: add "ByHeight" test.

t.Run("Total", func(t *testing.T) {
args := request(nil, 0, uint64(len(proofs)), true)
out, err := clitestutil.ExecTestCLICmd(ctx, proof.CmdListProof(), args)
require.NoError(t, err)
var resp types.QueryAllProofsResponse
require.NoError(t, net.Config.Codec.UnmarshalJSON(out.Bytes(), &resp))
require.NoError(t, err)
require.Equal(t, len(proofs), int(resp.Pagination.Total))
require.ElementsMatch(t,
nullify.Fill(proofs),
nullify.Fill(resp.Proofs),
)
})
}
Loading

0 comments on commit 2ce42b8

Please sign in to comment.