Convert simple proof to ics23 proof (#6390)
* convert simple proof to ics23 proof * add CHANGELOG entries * Apply suggestions from code review Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
@@ -21,7 +21,6 @@ func RequireProof(subpath string) bool {
|
||||
// more proof ops?
|
||||
func DefaultProofRuntime() (prt *merkle.ProofRuntime) {
|
||||
prt = merkle.NewProofRuntime()
|
||||
prt.RegisterOpDecoder(merkle.ProofOpSimpleValue, merkle.SimpleValueOpDecoder)
|
||||
prt.RegisterOpDecoder(storetypes.ProofOpIAVLCommitment, storetypes.CommitmentOpDecoder)
|
||||
prt.RegisterOpDecoder(storetypes.ProofOpSimpleMerkleCommitment, storetypes.CommitmentOpDecoder)
|
||||
return
|
||||
|
||||
@@ -109,36 +109,6 @@ func TestVerifyMultiStoreQueryProof(t *testing.T) {
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestVerifyMultiStoreQueryProofEmptyStore(t *testing.T) {
|
||||
// Create main tree for testing.
|
||||
db := dbm.NewMemDB()
|
||||
store := NewStore(db)
|
||||
iavlStoreKey := types.NewKVStoreKey("iavlStoreKey")
|
||||
|
||||
store.MountStoreWithDB(iavlStoreKey, types.StoreTypeIAVL, nil)
|
||||
err := store.LoadVersion(0)
|
||||
require.NoError(t, err)
|
||||
cid := store.Commit() // Commit with empty iavl store.
|
||||
|
||||
// Get Proof
|
||||
res := store.Query(abci.RequestQuery{
|
||||
Path: "/iavlStoreKey/key", // required path to get key/value+proof
|
||||
Data: []byte("MYKEY"),
|
||||
Prove: true,
|
||||
})
|
||||
require.NotNil(t, res.Proof)
|
||||
|
||||
// Verify proof.
|
||||
prt := DefaultProofRuntime()
|
||||
err = prt.VerifyAbsence(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY")
|
||||
require.Nil(t, err)
|
||||
|
||||
// Verify (bad) proof.
|
||||
prt = DefaultProofRuntime()
|
||||
err = prt.VerifyValue(res.Proof, cid.Hash, "/iavlStoreKey/MYKEY", []byte("MYVALUE"))
|
||||
require.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestVerifyMultiStoreQueryProofAbsence(t *testing.T) {
|
||||
// Create main tree for testing.
|
||||
db := dbm.NewMemDB()
|
||||
|
||||
@@ -19,6 +19,9 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/store/transient"
|
||||
"github.com/cosmos/cosmos-sdk/store/types"
|
||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
||||
|
||||
ics23tendermint "github.com/confio/ics23-tendermint"
|
||||
ics23 "github.com/confio/ics23/go"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -575,12 +578,24 @@ func (ci commitInfo) Hash() []byte {
|
||||
}
|
||||
|
||||
func (ci commitInfo) ProofOp(storeName string) merkle.ProofOp {
|
||||
_, proofs, _ := merkle.SimpleProofsFromMap(ci.toMap())
|
||||
cmap := ci.toMap()
|
||||
_, proofs, _ := merkle.SimpleProofsFromMap(cmap)
|
||||
proof := proofs[storeName]
|
||||
if proof == nil {
|
||||
panic(fmt.Sprintf("ProofOp for %s but not registered store name", storeName))
|
||||
}
|
||||
return merkle.NewSimpleValueOp([]byte(storeName), proof).ProofOp()
|
||||
// convert merkle.SimpleProof to CommitmentProof
|
||||
existProof, err := ics23tendermint.ConvertExistenceProof(proof, []byte(storeName), cmap[storeName])
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("could not convert simple proof to existence proof: %w", err))
|
||||
}
|
||||
commitmentProof := &ics23.CommitmentProof{
|
||||
Proof: &ics23.CommitmentProof_Exist{
|
||||
Exist: existProof,
|
||||
},
|
||||
}
|
||||
|
||||
return types.NewSimpleMerkleCommitmentOp([]byte(storeName), commitmentProof).ProofOp()
|
||||
}
|
||||
|
||||
func (ci commitInfo) CommitID() types.CommitID {
|
||||
|
||||
Reference in New Issue
Block a user