Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix service request query #471

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 54 additions & 33 deletions modules/service/client/utils/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,10 @@ func QueryRequestContextByTxQuery(
if err != nil {
return requestContext, err
}

msgIndex := -1
var found bool
I:
for _, event := range txInfo.Events {
if event.Type == types.EventTypeCreateContext {
for _, attribute := range event.Attributes {
if attribute.Key == types.EventTypeMsgIndex {
paresIndex, err := strconv.ParseInt(attribute.Value, 10, 64)
if err != nil {
return requestContext, err
}
msgIndex = int(paresIndex)
}

if attribute.Key == types.AttributeKeyRequestContextID &&
attribute.Value == params.RequestContextId {
found = true
if msgIndex != -1 {
break I
}
}

if found && msgIndex != -1 {
break I
}

}
}
}

if !found {
return requestContext, fmt.Errorf("unknown request context: %s", params.RequestContextId)
// Get the msg index
msgIndex, err := findMsgIndex(txInfo, params)
if err != nil {
return requestContext, err
}
if len(txInfo.GetTx().GetMsgs()) > msgIndex {
if requestMsg, ok := txInfo.GetTx().GetMsgs()[msgIndex].(*types.MsgCallService); ok {
Expand Down Expand Up @@ -128,6 +99,56 @@ I:
return requestContext, nil
}

// findMsgIndex will find the message index in the txInfo.
func findMsgIndex(txInfo *sdk.TxResponse, params types.QueryRequestContextRequest) (int, error) {
const errUnknownContext = "unknown request context: %s"
msgIndex := -1
var found bool
if txInfo.Logs == nil {
for _, event := range txInfo.Events {
if event.Type == types.EventTypeCreateContext {
for _, attribute := range event.Attributes {
if attribute.Key == types.EventTypeMsgIndex {
paresIndex, err := strconv.ParseInt(attribute.Value, 10, 64)
if err != nil {
return msgIndex, err
}
msgIndex = int(paresIndex)
}

if attribute.Key == types.AttributeKeyRequestContextID &&
attribute.Value == params.RequestContextId {
found = true
}

if found && msgIndex != -1 {
return msgIndex, nil
}
}
}
}

return msgIndex, fmt.Errorf(errUnknownContext, params.RequestContextId)
}

// Compatible with older versions.
for i, log := range txInfo.Logs {
for _, event := range log.Events {
if event.Type == types.EventTypeCreateContext {
for _, attribute := range event.Attributes {
if attribute.Key == types.AttributeKeyRequestContextID &&
attribute.Value == params.RequestContextId {
msgIndex = i
return msgIndex, nil
}
}
}
}
}

return msgIndex, fmt.Errorf(errUnknownContext, params.RequestContextId)
}

// QueryRequestByTxQuery will query for a single request via a direct txs tags query.
func QueryRequestByTxQuery(
cliCtx client.Context, queryRoute string, requestID tmbytes.HexBytes,
Expand Down
Loading