Skip to content

Commit

Permalink
Merge pull request #133 from zhiqiang-bianjie/irisnet-ibc-rest-apis
Browse files Browse the repository at this point in the history
return query result proof
  • Loading branch information
SegueII authored Nov 20, 2019
2 parents bb4ea54 + 6a8da59 commit 4cfe60f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 103 deletions.
18 changes: 18 additions & 0 deletions client/context/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package context

import (
"fmt"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -106,6 +108,22 @@ func (ctx CLIContext) QueryABCI(req abci.RequestQuery) (abci.ResponseQuery, erro
return ctx.queryABCI(req)
}

// QueryABCI performs a query to a Tendermint node with the provide RequestQuery.
// It returns the ResultQuery obtained from the query.
func (ctx CLIContext) QueryABCIWithProof(r *http.Request, module string, data []byte) (abci.ResponseQuery, error) {
proveStr := r.FormValue("prove")
prove := false
if ok, err := strconv.ParseBool(proveStr); err != nil {
prove = ok
}
req := abci.RequestQuery{
Path: fmt.Sprintf("store/%s/key", module),
Data: data,
Prove: prove,
}
return ctx.queryABCI(req)
}

// QuerySubspace performs a query to a Tendermint node with the provided
// store name and subspace. It returns key value pair and height of the query
// upon success or an error if the query fails.
Expand Down
48 changes: 10 additions & 38 deletions x/ibc/02-client/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,19 @@ func queryConsensusStateHandlerFn(cliCtx context.CLIContext, queryRoute string)
return
}

// return proof if the prove query param is set to true
proveStr := r.FormValue("prove")
prove := false
if strings.ToLower(strings.TrimSpace(proveStr)) == Prove {
prove = true
}

bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryClientStateParams(clientID))
res, err := cliCtx.QueryABCIWithProof(r, "ibc", types.PrefixKeyConsensusState(clientID))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryConsensusState),
Data: bz,
Prove: prove,
}

res, err := cliCtx.QueryABCI(req)
if err != nil {
var cs tendermint.ConsensusState
if err := cliCtx.Codec.UnmarshalBinaryLengthPrefixed(res.Value, &cs); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(res.Height)
rest.PostProcessResponse(w, cliCtx, res)
rest.PostProcessResponse(w, cliCtx, types.NewConsensusStateResponse(clientID, cs, res.Proof, res.Height))
}
}

Expand All @@ -94,33 +80,19 @@ func queryClientStateHandlerFn(cliCtx context.CLIContext, queryRoute string) htt
return
}

// return proof if the prove query param is set to true
proveStr := r.FormValue("prove")
prove := false
if strings.ToLower(strings.TrimSpace(proveStr)) == Prove {
prove = true
}

bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryClientStateParams(clientID))
res, err := cliCtx.QueryABCIWithProof(r, "ibc", types.PrefixKeyClientState(clientID))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryClientState),
Data: bz,
Prove: prove,
}

res, err := cliCtx.QueryABCI(req)
if err != nil {
var state types.State
if err := cliCtx.Codec.UnmarshalBinaryLengthPrefixed(res.Value, &state); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(res.Height)
rest.PostProcessResponse(w, cliCtx, res)
rest.PostProcessResponse(w, cliCtx, types.NewClientStateResponse(clientID, state, res.Proof, res.Height))
}
}

Expand Down
10 changes: 10 additions & 0 deletions x/ibc/02-client/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ func KeyConsensusState(clientID string) []byte {
func KeyRoot(clientID string, height uint64) []byte {
return []byte(RootPath(clientID, height))
}

// PrefixKeyClientState returns the prefix store key for the client state
func PrefixKeyClientState(clientID string) []byte {
return append(KeyPrefixClient, KeyClientState(clientID)...)
}

// PrefixKeyConsensusState returns the prefix store key for the consensus state
func PrefixKeyConsensusState(clientID string) []byte {
return append(KeyPrefixClient, KeyConsensusState(clientID)...)
}
53 changes: 10 additions & 43 deletions x/ibc/03-connection/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package rest
import (
"fmt"
"net/http"
"strings"

"github.com/gorilla/mux"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types"
Expand All @@ -33,33 +30,18 @@ func queryConnectionHandlerFn(cliCtx context.CLIContext, queryRoute string) http
return
}

// return proof if the prove query param is set to true
proveStr := r.FormValue("prove")
prove := false
if strings.ToLower(strings.TrimSpace(proveStr)) == Prove {
prove = true
}

bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryConnectionParams(connectionID))
res, err := cliCtx.QueryABCIWithProof(r, "ibc", types.PrefixKeyConnection(connectionID))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryConnection),
Data: bz,
Prove: prove,
}

res, err := cliCtx.QueryABCI(req)
if err != nil {
var connection types.ConnectionEnd
if err := cliCtx.Codec.UnmarshalBinaryLengthPrefixed(res.Value, &connection); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(res.Height)
rest.PostProcessResponse(w, cliCtx, res)
rest.PostProcessResponse(w, cliCtx, types.NewConnectionResponse(connectionID, connection, res.Proof, res.Height))
}
}

Expand All @@ -73,32 +55,17 @@ func queryClientConnectionsHandlerFn(cliCtx context.CLIContext, queryRoute strin
return
}

// return proof if the prove query param is set to true
proveStr := r.FormValue("prove")
prove := false
if strings.ToLower(strings.TrimSpace(proveStr)) == Prove {
prove = true
}

bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryClientConnectionsParams(clientID))
res, err := cliCtx.QueryABCIWithProof(r, "ibc", types.PrefixKeyClientConnections(clientID))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryClientConnections),
Data: bz,
Prove: prove,
}

res, err := cliCtx.QueryABCI(req)
if err != nil {
var paths []string
if err := cliCtx.Codec.UnmarshalBinaryLengthPrefixed(res.Value, &paths); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(res.Height)
rest.PostProcessResponse(w, cliCtx, res)
rest.PostProcessResponse(w, cliCtx, types.NewClientConnectionsResponse(clientID, paths, res.Proof, res.Height))
}
}
10 changes: 10 additions & 0 deletions x/ibc/03-connection/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ func KeyConnection(connectionID string) []byte {
func KeyClientConnections(clientID string) []byte {
return []byte(ClientConnectionsPath(clientID))
}

// PrefixKeyConnection returns the prefix store key for a particular connection
func PrefixKeyConnection(connectionID string) []byte {
return append(KeyPrefixConnection, KeyConnection(connectionID)...)
}

// PrefixKeyClientConnections returns the prefix store key for the the connectios of a given client
func PrefixKeyClientConnections(clientID string) []byte {
return append(KeyPrefixConnection, KeyClientConnections(clientID)...)
}
27 changes: 5 additions & 22 deletions x/ibc/04-channel/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package rest
import (
"fmt"
"net/http"
"strings"

"github.com/gorilla/mux"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types"
abci "github.com/tendermint/tendermint/abci/types"
)

const (
Expand All @@ -32,32 +30,17 @@ func queryChannelHandlerFn(cliCtx context.CLIContext, queryRoute string) http.Ha
return
}

// return proof if the prove query param is set to true
proveStr := r.FormValue("prove")
prove := false
if strings.ToLower(strings.TrimSpace(proveStr)) == Prove {
prove = true
}

bz, err := cliCtx.Codec.MarshalJSON(types.NewQueryChannelParams(portID, channelID))
res, err := cliCtx.QueryABCIWithProof(r, "ibc", types.PrefixKeyChannel(portID, channelID))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

req := abci.RequestQuery{
Path: fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryChannel),
Data: bz,
Prove: prove,
}

res, err := cliCtx.QueryABCI(req)
if err != nil {
var channel types.Channel
if err := cliCtx.Codec.UnmarshalBinaryLengthPrefixed(res.Value, &channel); err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(res.Height)
rest.PostProcessResponse(w, cliCtx, res)
rest.PostProcessResponse(w, cliCtx, types.NewChannelResponse(portID, channelID, channel, res.Proof, res.Height))
}
}
5 changes: 5 additions & 0 deletions x/ibc/04-channel/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func KeyChannel(portID, channelID string) []byte {
return []byte(ChannelPath(portID, channelID))
}

// PrefixKeyChannel returns the prefix store key for a particular channel
func PrefixKeyChannel(portID, channelID string) []byte {
return append(KeyPrefixChannel, KeyChannel(portID, channelID)...)
}

// KeyChannelCapabilityPath returns the store key for the capability key of a
// particular channel binded to a specific port
func KeyChannelCapabilityPath(portID, channelID string) []byte {
Expand Down

0 comments on commit 4cfe60f

Please sign in to comment.