forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
67 lines (55 loc) · 2.41 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package client
import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/JaTochNietDan/cosmos-sdk/client"
"github.com/JaTochNietDan/cosmos-sdk/codec"
clienttypes "github.com/JaTochNietDan/cosmos-sdk/x/ibc/core/02-client/types"
commitmenttypes "github.com/JaTochNietDan/cosmos-sdk/x/ibc/core/23-commitment/types"
host "github.com/JaTochNietDan/cosmos-sdk/x/ibc/core/24-host"
)
// QueryTendermintProof performs an ABCI query with the given key and returns
// the value of the query, the proto encoded merkle proof, and the height of
// the Tendermint block containing the state root. The desired tendermint height
// to perform the query should be set in the client context. The query will be
// performed at one below this height (at the IAVL version) in order to obtain
// the correct merkle proof. Proof queries at height less than or equal to 2 are
// not supported. Queries with a client context height of 0 will perform a query
// at the lastest state available.
// Issue: https://github.com/JaTochNietDan/cosmos-sdk/issues/6567
func QueryTendermintProof(clientCtx client.Context, key []byte) ([]byte, []byte, clienttypes.Height, error) {
height := clientCtx.Height
// ABCI queries at heights 1, 2 or less than or equal to 0 are not supported.
// Base app does not support queries for height less than or equal to 1.
// Therefore, a query at height 2 would be equivalent to a query at height 3.
// A height of 0 will query with the lastest state.
if height != 0 && height <= 2 {
return nil, nil, clienttypes.Height{}, fmt.Errorf("proof queries at height <= 2 are not supported")
}
// Use the IAVL height if a valid tendermint height is passed in.
// A height of 0 will query with the latest state.
if height != 0 {
height--
}
req := abci.RequestQuery{
Path: fmt.Sprintf("store/%s/key", host.StoreKey),
Height: height,
Data: key,
Prove: true,
}
res, err := clientCtx.QueryABCI(req)
if err != nil {
return nil, nil, clienttypes.Height{}, err
}
merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps)
if err != nil {
return nil, nil, clienttypes.Height{}, err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)
proofBz, err := cdc.MarshalBinaryBare(&merkleProof)
if err != nil {
return nil, nil, clienttypes.Height{}, err
}
revision := clienttypes.ParseChainID(clientCtx.ChainID)
return res.Value, proofBz, clienttypes.NewHeight(revision, uint64(res.Height)+1), nil
}