github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/store/iavl/store_ibc_adapter.go (about)

     1  package iavl
     2  
     3  import (
     4  	"fmt"
     5  
     6  	ics23 "github.com/confio/ics23/go"
     7  	storetyeps "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/store/types"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     9  	sdkerrors "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/errors"
    10  	"github.com/fibonacci-chain/fbc/libs/iavl"
    11  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    12  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto/merkle"
    13  )
    14  
    15  func (st *Store) queryWithCM40(req abci.RequestQuery) (res abci.ResponseQuery) {
    16  
    17  	if len(req.Data) == 0 {
    18  		return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrTxDecode, "query cannot be zero length"))
    19  	}
    20  
    21  	tree := st.tree
    22  
    23  	// store the height we chose in the response, with 0 being changed to the
    24  	// latest height
    25  	res.Height = getHeight(tree, req)
    26  
    27  	switch req.Path {
    28  	case "/key": // get by key
    29  		key := req.Data // data holds the key bytes
    30  		if string(key) == "ibc" {
    31  			fmt.Println(1)
    32  		}
    33  		res.Key = key
    34  		if !st.VersionExists(res.Height) {
    35  			res.Log = iavl.ErrVersionDoesNotExist.Error()
    36  			break
    37  		}
    38  
    39  		_, res.Value = tree.GetVersioned(key, res.Height)
    40  		if !req.Prove {
    41  			break
    42  		}
    43  
    44  		// Continue to prove existence/absence of value
    45  		// Must convert store.Tree to iavl.MutableTree with given version to use in CreateProof
    46  		iTree, err := tree.GetImmutable(res.Height)
    47  		if err != nil {
    48  			// sanity check: If value for given version was retrieved, immutable tree must also be retrievable
    49  			panic(fmt.Sprintf("version exists in store but could not retrieve corresponding versioned tree in store, %s", err.Error()))
    50  		}
    51  		mtree := &iavl.MutableTree{
    52  			ImmutableTree: iTree,
    53  		}
    54  
    55  		// get proof from tree and convert to merkle.Proof before adding to result
    56  		res.Proof = getProofFromTree(mtree, req.Data, res.Value != nil)
    57  	case "/subspace":
    58  		var KVs []types.KVPair
    59  
    60  		subspace := req.Data
    61  		res.Key = subspace
    62  
    63  		iterator := types.KVStorePrefixIterator(st, subspace)
    64  		for ; iterator.Valid(); iterator.Next() {
    65  			KVs = append(KVs, types.KVPair{Key: iterator.Key(), Value: iterator.Value()})
    66  		}
    67  
    68  		iterator.Close()
    69  		res.Value = cdc.MustMarshalBinaryLengthPrefixed(KVs)
    70  	default:
    71  		return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unexpected query path: %v", req.Path))
    72  	}
    73  
    74  	return res
    75  }
    76  
    77  func getProofFromTree(tree *iavl.MutableTree, key []byte, exists bool) *merkle.Proof {
    78  
    79  	var (
    80  		commitmentProof *ics23.CommitmentProof
    81  		err             error
    82  	)
    83  	//tmcrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
    84  	if exists {
    85  		// value was found
    86  		commitmentProof, err = tree.GetMembershipProof(key)
    87  		if err != nil {
    88  			// sanity check: If value was found, membership proof must be creatable
    89  			panic(fmt.Sprintf("unexpected value for empty proof: %s", err.Error()))
    90  		}
    91  	} else {
    92  		// value wasn't found
    93  		commitmentProof, err = tree.GetNonMembershipProof(key)
    94  		if err != nil {
    95  			// sanity check: If value wasn't found, nonmembership proof must be creatable
    96  			panic(fmt.Sprintf("unexpected error for nonexistence proof: %s", err.Error()))
    97  		}
    98  	}
    99  
   100  	op := storetyeps.NewIavlCommitmentOp(key, commitmentProof)
   101  
   102  	//&merkle.Proof{Ops: []merkle.ProofOp{iavl.NewValueOp(key, proof).ProofOp()}}
   103  	opp := op.ProofOp()
   104  	return &merkle.Proof{
   105  		Ops:                  []merkle.ProofOp{opp},
   106  		XXX_NoUnkeyedLiteral: struct{}{},
   107  		XXX_unrecognized:     nil,
   108  		XXX_sizecache:        0,
   109  	}
   110  }