Skip to content

Commit

Permalink
chore: consolidate trie specs
Browse files Browse the repository at this point in the history
  • Loading branch information
red-0ne committed Jan 30, 2025
1 parent 65596cf commit 3385f72
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 116 deletions.
91 changes: 46 additions & 45 deletions api/poktroll/proof/event.pulsar.go

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

10 changes: 9 additions & 1 deletion pkg/crypto/protocol/hasher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package protocol

import "crypto/sha256"
import (
"crypto/sha256"

"github.com/pokt-network/smt"
)

const (
RelayHasherSize = sha256.Size
Expand All @@ -15,3 +19,7 @@ var (
NewRelayHasher = sha256.New
NewTrieHasher = sha256.New
)

func SMTValueHasher() smt.TrieSpecOption {
return smt.WithValueHasher(nil)
}
17 changes: 6 additions & 11 deletions pkg/crypto/protocol/proof_path.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
package protocol

import (
"crypto/sha256"

"github.com/pokt-network/smt"
)

// 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 := NewTrieHasher()
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.
// NewSMTSpec returns the SMT specification used for proof verification.
// A new hasher is created for each call to prevent concurrency issues
// from shared state.
func NewSMTSpec() *smt.TrieSpec {
trieSpec := smt.NewTrieSpec(
newHasher(), true,
smt.WithValueHasher(nil),
NewTrieHasher(), true,
SMTValueHasher(),
)

return &trieSpec
Expand Down
5 changes: 2 additions & 3 deletions pkg/relayer/session/sessiontree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package session

import (
"bytes"
"crypto/sha256"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -94,7 +93,7 @@ func NewSessionTree(

// Create the SMST from the KVStore and a nil value hasher so the proof would
// contain a non-hashed Relay that could be used to validate the proof onchain.
trie := smt.NewSparseMerkleSumTrie(treeStore, protocol.NewTrieHasher(), smt.WithValueHasher(nil))
trie := smt.NewSparseMerkleSumTrie(treeStore, protocol.NewTrieHasher(), protocol.SMTValueHasher())

logger = logger.With(
"store_path", storePath,
Expand Down Expand Up @@ -175,7 +174,7 @@ func (st *sessionTree) ProveClosest(path []byte) (compactProof *smt.SparseCompac
return nil, err
}

sessionSMT := smt.ImportSparseMerkleSumTrie(st.treeStore, sha256.New(), st.claimedRoot, smt.WithValueHasher(nil))
sessionSMT := smt.ImportSparseMerkleSumTrie(st.treeStore, protocol.NewTrieHasher(), st.claimedRoot, protocol.SMTValueHasher())

// Generate the proof and cache it along with the path for which it was generated.
// There is no ProveClosest variant that generates a compact proof directly.
Expand Down
2 changes: 1 addition & 1 deletion pkg/relayer/session/sessiontree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestSessionTree_CompactProofsAreSmallerThanNonCompactProofs(t *testing.T) {
kvStore, err := pebble.NewKVStore("")
require.NoError(t, err)

trie := smt.NewSparseMerkleSumTrie(kvStore, protocol.NewTrieHasher(), smt.WithValueHasher(nil))
trie := smt.NewSparseMerkleSumTrie(kvStore, protocol.NewTrieHasher(), protocol.SMTValueHasher())

// Insert numLeaf random leaves.
for i := 0; i < numLeafs; i++ {
Expand Down
2 changes: 1 addition & 1 deletion proto/poktroll/proof/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ message EventProofValidityChecked {
poktroll.proof.ClaimProofStatus proof_status = 3 [(gogoproto.jsontag) = "proof_status"];
// reason is the string representation of the error that led to the proof being
// marked as invalid (e.g. "invalid closest merkle proof", "invalid relay request signature")
string reason = 4 [(gogoproto.jsontag) = "reason"];
string failure_reason = 4 [(gogoproto.jsontag) = "failure_reason"];
}
2 changes: 1 addition & 1 deletion tests/integration/service/relay_mining_difficulty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func prepareSMST(
// TODO_TECHDEBT(#446): Centralize the configuration for the SMT spec.
kvStore, err := pebble.NewKVStore("")
require.NoError(t, err)
trie := smt.NewSparseMerkleSumTrie(kvStore, protocol.NewTrieHasher(), smt.WithValueHasher(nil))
trie := smt.NewSparseMerkleSumTrie(kvStore, protocol.NewTrieHasher(), protocol.SMTValueHasher())

for i := uint64(0); i < numRelays; i++ {
// DEV_NOTE: A signed mined relay is a MinedRelay type with the appropriate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func prepareRealClaim(
require.NoError(t, err)

// Prepare an SMST
trie := smt.NewSparseMerkleSumTrie(kvStore, protocol.NewTrieHasher(), smt.WithValueHasher(nil))
trie := smt.NewSparseMerkleSumTrie(kvStore, protocol.NewTrieHasher(), protocol.SMTValueHasher())

// Insert the mined relays into the SMST
for i := uint64(0); i < numRelays; i++ {
Expand Down
12 changes: 6 additions & 6 deletions x/proof/keeper/validate_proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,25 @@ func (k Keeper) validateProof(
// Set the proof status to valid by default.
proofStatus := types.ClaimProofStatus_VALIDATED
// Set the invalidity reason to an empty string by default.
invalidProofCause := ""
invalidProofReason := ""

if err := k.EnsureValidProofSignaturesAndClosestPath(ctx, &claim, &proof); err != nil {
// Set the proof status to invalid.
proofStatus = types.ClaimProofStatus_INVALID

// Set the invalidity reason to the error message.
invalidProofCause = err.Error()
invalidProofReason = err.Error()

logger.Info(fmt.Sprintf("invalid proof due to error: %v", err))
}
logger.Info(fmt.Sprintf("proof checked, validation result: %s", proofStatus))

// Create and emit an event for the proof validation result.
eventProofValidityChecked := types.EventProofValidityChecked{
Proof: &proof,
BlockHeight: uint64(sdkCtx.BlockHeight()),
ProofStatus: proofStatus,
Reason: invalidProofCause,
Proof: &proof,
BlockHeight: uint64(sdkCtx.BlockHeight()),
ProofStatus: proofStatus,
FailureReason: invalidProofReason,
}

if err := sdkCtx.EventManager().EmitTypedEvent(&eventProofValidityChecked); err != nil {
Expand Down
Loading

0 comments on commit 3385f72

Please sign in to comment.