Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/proof-endblocker' into cach…
Browse files Browse the repository at this point in the history
…e-relayminer
  • Loading branch information
red-0ne committed Jan 30, 2025
2 parents 77c1cb6 + 65596cf commit 6049331
Show file tree
Hide file tree
Showing 20 changed files with 386 additions and 270 deletions.
2 changes: 1 addition & 1 deletion api/poktroll/proof/event.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

146 changes: 74 additions & 72 deletions api/poktroll/proof/types.pulsar.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var allUpgrades = []upgrades.Upgrade{
upgrades.Upgrade_0_0_4,
upgrades.Upgrade_0_0_10,
upgrades.Upgrade_0_0_11,
}

// setUpgrades sets upgrade handlers for all upgrades and executes KVStore migration if an upgrade plan file exists.
Expand Down
98 changes: 98 additions & 0 deletions app/upgrades/v0.0.11.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package upgrades

import (
"context"

storetypes "cosmossdk.io/store/types"
upgradetypes "cosmossdk.io/x/upgrade/types"
cosmosTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/pokt-network/poktroll/app/keepers"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
tokenomicstypes "github.com/pokt-network/poktroll/x/tokenomics/types"
)

// Upgrade_0_0_11 is the upgrade handler for v0.0.11 Alpha TestNet upgrade
// Beta TestNet was launched with v0.0.11, so this upgrade is exclusively for Alpha TestNet.
// - Before: v0.0.10
// - After: v0.0.11
var Upgrade_0_0_11 = Upgrade{
PlanName: "v0.0.11",
CreateUpgradeHandler: func(mm *module.Manager,
keepers *keepers.Keepers,
configurator module.Configurator,
) upgradetypes.UpgradeHandler {
// Adds new parameters using ignite's config.yml as a reference. Assuming we don't need any other parameters.
// https://github.com/pokt-network/poktroll/compare/v0.0.10...v0.0.11-rc
applyNewParameters := func(ctx context.Context) (err error) {
logger := cosmosTypes.UnwrapSDKContext(ctx).Logger()
logger.Info("Starting parameter updates for v0.0.11")

// Set num_suppliers_per_session to 15
// Validate with: `poktrolld q session params --node=https://testnet-validated-validator-rpc.poktroll.com/`
sessionParams := sessiontypes.Params{
NumSuppliersPerSession: uint64(15),
}

// ALL parameters must be present when setting params.
err = keepers.SessionKeeper.SetParams(ctx, sessionParams)
if err != nil {
logger.Error("Failed to set session params", "error", err)
return err
}
logger.Info("Successfully updated session params", "new_params", sessionParams)

// Set tokenomics params. The values are based on default values for LocalNet/Beta TestNet.
// Validate with: `poktrolld q tokenomics params --node=https://testnet-validated-validator-rpc.poktroll.com/`
tokenomicsParams := tokenomicstypes.Params{
MintAllocationPercentages: tokenomicstypes.MintAllocationPercentages{
Dao: 0.1,
Proposer: 0.05,
Supplier: 0.7,
SourceOwner: 0.15,
Application: 0.0,
},
DaoRewardAddress: AlphaTestNetPnfAddress,
}

// ALL parameters must be present when setting params.
err = keepers.TokenomicsKeeper.SetParams(ctx, tokenomicsParams)
if err != nil {
logger.Error("Failed to set tokenomics params", "error", err)
return err
}
logger.Info("Successfully updated tokenomics params", "new_params", tokenomicsParams)

return
}

// The diff shows that the only new authz authorization is for the `poktroll.session.MsgUpdateParam` message.
// However, this message is already authorized for the `pokt10d07y265gmmuvt4z0w9aw880jnsr700j8yv32t` address.
// See here: poktrolld q authz grants-by-granter pokt10d07y265gmmuvt4z0w9aw880jnsr700j8yv32t --node=https://shannon-testnet-grove-seed-rpc.alpha.poktroll.com
// If this upgrade would have been applied to other networks, we could have added a separate upgrade handler for each network.

// Returns the upgrade handler for v0.0.11
return func(ctx context.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
logger := cosmosTypes.UnwrapSDKContext(ctx).Logger()
logger.Info("Starting v0.0.11 upgrade handler")

err := applyNewParameters(ctx)
if err != nil {
logger.Error("Failed to apply new parameters", "error", err)
return vm, err
}

logger.Info("Running module migrations")
vm, err = mm.RunMigrations(ctx, configurator, vm)
if err != nil {
logger.Error("Failed to run migrations", "error", err)
return vm, err
}

logger.Info("Successfully completed v0.0.11 upgrade handler")
return vm, nil
}
},
// No changes to the KVStore in this upgrade.
StoreUpgrades: storetypes.StoreUpgrades{},
}
8 changes: 3 additions & 5 deletions cmd/poktrolld/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ type PoktrollAppConfig struct {
}

// poktrollAppConfigDefaults sets default values to render in `app.toml`.
// Checkout `customAppConfigTemplate()` for additional information about each setting.
// Checkout `customAppConfigTemplate()` for additional information about each config parameter.
func poktrollAppConfigDefaults() PoktrollAppConfig {
return PoktrollAppConfig{
Telemetry: telemetry.PoktrollTelemetryConfig{
CardinalityLevel: "medium",
},
Telemetry: telemetry.DefaultConfig(),
}
}

Expand Down Expand Up @@ -104,7 +102,6 @@ func initCometBFTConfig() *cmtcfg.Config {
// return "", nil if no custom configuration is required for the application.
// TODO_MAINNET: Reconsider values - check `app.toml` for possible options.
func initAppConfig() (string, interface{}) {
// The following code snippet is just for reference.
type CustomAppConfig struct {
serverconfig.Config `mapstructure:",squash"`
Poktroll PoktrollAppConfig `mapstructure:"poktroll"`
Expand Down Expand Up @@ -140,6 +137,7 @@ func initAppConfig() (string, interface{}) {
srvCfg.GRPC.Enable = true
srvCfg.GRPCWeb.Enable = true

// Create the custom config with both server and poktroll configs
customAppConfig := CustomAppConfig{
Config: *srvCfg,
Poktroll: poktrollAppConfigDefaults(),
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (p *pocketdBin) runPocketCmd(args ...string) (*commandResult, error) {
return r, err
}

// runCurlPostCmd is a helper to run a command using the local pocketd binary with the flags provided
// runCurlCmd is a helper to run a command using the local pocketd binary with the flags provided
func (p *pocketdBin) runCurlCmd(rpcBaseURL, service, method, path, appAddr, data string, args ...string) (*commandResult, error) {
rpcUrl, err := url.Parse(rpcBaseURL)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests/reset_params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s *suite) resetAllModuleParamsToDefaults() {
s.sendAuthzExecTx(s.granteeName, resetTxJSONFile.Name())
}

// allMoudlesMsgUpdateParamsToDefaultsAny returns a slice of Any messages, each corresponding
// allModulesMsgUpdateParamsToDefaultsAny returns a slice of Any messages, each corresponding
// to a MsgUpdateParams for a module, populated with the respective default values.
func (s *suite) allModulesMsgUpdateParamsToDefaultsAny() []*codectypes.Any {
s.Helper()
Expand Down
30 changes: 16 additions & 14 deletions pkg/crypto/protocol/proof_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@ package protocol

import (
"crypto/sha256"
)

// SMT specification used for the proof verification.
var (
NewHasher = sha256.New
//SmtSpec smt.TrieSpec
"github.com/pokt-network/smt"
)

func init() {
// Use a spec that does not prehash values in the smst. This returns a nil value
// hasher for the proof verification in order to avoid hashing the value twice.
//SmtSpec = smt.NewTrieSpec(
// newHasher(), true,
// smt.WithValueHasher(nil),
//)
}
// newHasher is the hash function used by the SMT specification.
var newHasher = sha256.New

// GetPathForProof computes the path to be used for proof validation by hashing
// the block hash and session id.
func GetPathForProof(blockHash []byte, sessionId string) []byte {
hasher := NewHasher()
hasher := newHasher()
if _, err := hasher.Write(append(blockHash, []byte(sessionId)...)); err != nil {
panic(err)
}

return hasher.Sum(nil)
}

// NewSMTSpec returns the SMT specification used for the proof verification.
// It uses a new hasher at every call to avoid concurrency issues that could be
// caused by a shared hasher.
func NewSMTSpec() *smt.TrieSpec {
trieSpec := smt.NewTrieSpec(
newHasher(), true,
smt.WithValueHasher(nil),
)

return &trieSpec
}
21 changes: 12 additions & 9 deletions proto/poktroll/proof/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ message Proof {

// Claim is the serialized object stored onchain for claims pending to be proven
message Claim {
// Address of the supplier's operator that submitted this claim.
string supplier_operator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // the address of the supplier's operator that submitted this claim
// The session header of the session that this claim is for.

// Session header this claim is for.
poktroll.session.SessionHeader session_header = 2;
// Root hash returned from smt.SMST#Root().

// Root hash from smt.SMST#Root().
bytes root_hash = 3;
// Claim proof status captures the status of the proof for this claim.
// WARNING: This field MUST only be set by proofKeeper#EnsureValidProofSignaturesAndClosestPath
ClaimProofStatus proof_status = 4;

// Important: This field MUST only be set by proofKeeper#EnsureValidProofSignaturesAndClosestPath
ClaimProofStatus proof_validation_status = 4;
}

enum ProofRequirementReason {
Expand All @@ -47,10 +50,10 @@ enum ClaimProofStage {
EXPIRED = 3;
}

// ClaimProofStatus defines the status of the proof for a claim.
// The default value is NOT_FOUND, whether the proof is required or not.
// Status of proof validation for a claim
// Default is PENDING_VALIDATION regardless of proof requirement
enum ClaimProofStatus {
NOT_FOUND = 0;
VALID = 1;
PENDING_VALIDATION = 0;
VALIDATED = 1;
INVALID = 2;
}
14 changes: 14 additions & 0 deletions telemetry/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package telemetry

// Default configuration values for telemetry
const (
// DefaultCardinalityLevel represents the default cardinality level for metrics collection
DefaultCardinalityLevel = "medium"
)

// DefaultConfig returns the default telemetry configuration
func DefaultConfig() PoktrollTelemetryConfig {
return PoktrollTelemetryConfig{
CardinalityLevel: DefaultCardinalityLevel,
}
}
23 changes: 19 additions & 4 deletions telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,31 @@ import (
// Set once on initialization and remains constant during runtime.
var globalTelemetryConfig PoktrollTelemetryConfig

// PoktrollTelemetryConfig represents the telemetry protion of the custom poktroll config section in `app.toml`.
// PoktrollTelemetryConfig represents the telemetry portion of the custom poktroll config section in `app.toml`.
type PoktrollTelemetryConfig struct {
CardinalityLevel string `mapstructure:"cardinality-level"`
}

// New sets the globalTelemetryConfig for telemetry package.
func New(appOpts servertypes.AppOptions) error {
// Extract the map from appOpts.
// `poktroll.telemetry` comes from `app.toml` which is parsed into a map.
telemetryMap := appOpts.Get("poktroll.telemetry").(map[string]interface{})
// Get the poktroll config section. If it doesn't exist, use defaults
poktrollConfig := appOpts.Get("poktroll")
if poktrollConfig == nil {
globalTelemetryConfig = DefaultConfig()
return nil
}

// Try to get the telemetry subsection
poktrollMap, ok := poktrollConfig.(map[string]interface{})
if !ok {
return fmt.Errorf("invalid poktroll config format: expected map[string]interface{}, got %T", poktrollConfig)
}

telemetryMap, ok := poktrollMap["telemetry"].(map[string]interface{})
if !ok {
globalTelemetryConfig = DefaultConfig()
return nil
}

// Use mapstructure to decode the map into the struct
if err := mapstructure.Decode(telemetryMap, &globalTelemetryConfig); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion testutil/testtree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,6 @@ func NewClaim(
SupplierOperatorAddress: supplierOperatorAddr,
SessionHeader: sessionHeader,
RootHash: rootHash,
ProofStatus: prooftypes.ClaimProofStatus_NOT_FOUND,
ProofValidationStatus: prooftypes.ClaimProofStatus_PENDING_VALIDATION,
}
}
23 changes: 10 additions & 13 deletions x/proof/keeper/msg_server_submit_proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@ import (
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
)

// SubmitProof is the server handler to submit and store a proof onchain.
// A proof that's stored onchain is what leads to rewards (i.e. inflation)
// downstream, making this a critical part of the protocol.
// SubmitProof is the server message handler that stores a valid
// proof onchain, enabling downstream reward distribution.
//
// Note that the validation of the proof is done in `EnsureValidProofSignaturesAndClosestPath`.
// However, preliminary checks are done in the handler to prevent sybil or DoS attacks on
// full nodes by submitting malformed proofs.
// IMPORTANT: Full proof validation occurs in EnsureValidProofSignaturesAndClosestPath.
// This handler performs preliminary validation to prevent sybil/DoS attacks.
//
// We are playing a balance of security and efficiency here, where enough validation
// is done on proof submission, and exhaustive validation is done during the endblocker.
// There is a security & performance balance and tradeoff between the handler and end blocker:
// - Basic validation on submission (here)
// - Exhaustive validation in endblocker (EnsureValidProofSignaturesAndClosestPath)
//
// The entity sending the SubmitProof messages does not necessarily need
// to correspond to the supplier signing the proof. For example, a single entity
// could (theoretically) batch multiple proofs (signed by the corresponding supplier)
// into one transaction to save on transaction fees.
// Note: Proof submitter may differ from supplier signer, allowing batched submissions
// to optimize transaction fees.
func (k msgServer) SubmitProof(
ctx context.Context,
msg *types.MsgSubmitProof,
Expand Down Expand Up @@ -85,7 +82,7 @@ func (k msgServer) SubmitProof(
logger.Error(fmt.Sprintf("failed to ensure well-formed proof: %v", err))
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
logger.Info("checked the proof is well-formed")
logger.Info("ensured the proof is well-formed")

// Retrieve the claim associated with the proof.
// The claim should ALWAYS exist since the proof validation in EnsureWellFormedProof
Expand Down
Loading

0 comments on commit 6049331

Please sign in to comment.