Skip to content

Commit

Permalink
try fixing concurrencies!!11
Browse files Browse the repository at this point in the history
  • Loading branch information
okdas committed Jan 28, 2025
1 parent abef59c commit 7844336
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
28 changes: 13 additions & 15 deletions pkg/crypto/protocol/proof_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,27 @@ package protocol

import (
"crypto/sha256"

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

// SMT specification used for the proof verification.
var (
newHasher = sha256.New
SmtSpec smt.TrieSpec
)
// var (
// newHasher = sha256.New
// // SmtSpec smt.TrieSpec
// )

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),
)
}
// 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),
// )
// }

// 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 := sha256.New()
if _, err := hasher.Write(append(blockHash, []byte(sessionId)...)); err != nil {
panic(err)
}
Expand Down
27 changes: 22 additions & 5 deletions x/proof/keeper/proof_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package keeper
import (
"bytes"
"context"
"crypto/sha256"
"fmt"

cosmostelemetry "github.com/cosmos/cosmos-sdk/telemetry"
Expand Down Expand Up @@ -105,15 +106,20 @@ func (k Keeper) EnsureWellFormedProof(ctx context.Context, proof *types.Proof) e
return types.ErrProofInvalidProof.Wrapf("failed to unmarshal sparse compact merkle closest proof: %s", err)
}

smtSpec := smt.NewTrieSpec(
sha256.New(), true,
smt.WithValueHasher(nil),
)

// SparseCompactMerkeClosestProof does not implement GetValueHash, so we need to decompact it.
sparseMerkleClosestProof, err := smt.DecompactClosestProof(sparseCompactMerkleClosestProof, &protocol.SmtSpec)
sparseMerkleClosestProof, err := smt.DecompactClosestProof(sparseCompactMerkleClosestProof, &smtSpec)
if err != nil {
logger.Error(fmt.Sprintf("failed to decompact sparse merkle closest proof due to error: %v", err))
return types.ErrProofInvalidProof.Wrapf("failed to decompact sparse erkle closest proof: %s", err)
}

// Get the relay request and response from the proof.GetClosestMerkleProof.
relayBz := sparseMerkleClosestProof.GetValueHash(&protocol.SmtSpec)
relayBz := sparseMerkleClosestProof.GetValueHash(&smtSpec)
relay := &servicetypes.Relay{}
if err = k.cdc.Unmarshal(relayBz, relay); err != nil {
logger.Error(fmt.Sprintf("failed to unmarshal relay due to error: %v", err))
Expand Down Expand Up @@ -229,16 +235,21 @@ func (k Keeper) EnsureValidProofSignaturesAndClosestPath(
return types.ErrProofInvalidProof.Wrapf("failed to unmarshal sparse compact merkle closest proof: %s", err)
}

smtSpec := smt.NewTrieSpec(
sha256.New(), true,
smt.WithValueHasher(nil),
)

// SparseCompactMerkeClosestProof was intentionally compacted to reduce its onchain state size
// so it must be decompacted rather than just retrieving the value via GetValueHash (not implemented).
sparseMerkleClosestProof, err := smt.DecompactClosestProof(sparseCompactMerkleClosestProof, &protocol.SmtSpec)
sparseMerkleClosestProof, err := smt.DecompactClosestProof(sparseCompactMerkleClosestProof, &smtSpec)
if err != nil {
logger.Error(fmt.Sprintf("failed to decompact sparse merkle closest proof due to error: %v", err))
return types.ErrProofInvalidProof.Wrapf("failed to decompact sparse merkle closest proof: %s", err)
}

// Get the relay request and response from the proof.GetClosestMerkleProof.
relayBz := sparseMerkleClosestProof.GetValueHash(&protocol.SmtSpec)
relayBz := sparseMerkleClosestProof.GetValueHash(&smtSpec)
relay := &servicetypes.Relay{}
if err = k.cdc.Unmarshal(relayBz, relay); err != nil {
logger.Error(fmt.Sprintf("failed to unmarshal relay due to error: %v", err))
Expand Down Expand Up @@ -451,7 +462,13 @@ func verifyClosestProof(
proof *smt.SparseMerkleClosestProof,
claimRootHash []byte,
) error {
valid, err := smt.VerifyClosestProof(proof, claimRootHash, &protocol.SmtSpec)

smtSpec := smt.NewTrieSpec(
sha256.New(), true,
smt.WithValueHasher(nil),
)

valid, err := smt.VerifyClosestProof(proof, claimRootHash, &smtSpec)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions x/proof/keeper/proof_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"context"
"crypto/sha256"
"encoding/hex"
"testing"

Expand Down Expand Up @@ -607,14 +608,19 @@ func TestEnsureValidProof_Error(t *testing.T) {
expectedMerkleProofPath,
)

smtSpec := smt.NewTrieSpec(
sha256.New(), true,
smt.WithValueHasher(nil),
)

// Extract relayHash to check below that it's difficulty is insufficient
err = sparseCompactMerkleClosestProof.Unmarshal(proof.ClosestMerkleProof)
require.NoError(t, err)
var sparseMerkleClosestProof *smt.SparseMerkleClosestProof
sparseMerkleClosestProof, err = smt.DecompactClosestProof(sparseCompactMerkleClosestProof, &protocol.SmtSpec)
sparseMerkleClosestProof, err = smt.DecompactClosestProof(sparseCompactMerkleClosestProof, &smtSpec)
require.NoError(t, err)

relayBz := sparseMerkleClosestProof.GetValueHash(&protocol.SmtSpec)
relayBz := sparseMerkleClosestProof.GetValueHash(&smtSpec)
relayHashArr := protocol.GetRelayHashFromBytes(relayBz)
relayHash := relayHashArr[:]

Expand Down

0 comments on commit 7844336

Please sign in to comment.