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(stf,server/v2/cometbft): fix default events + improve codec handling #22837

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions server/v2/cometbft/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,19 @@ func (t txServer[T]) Simulate(ctx context.Context, req *txtypes.SimulateRequest)
msgResponses = append(msgResponses, anyMsg)
}

event, err := intoABCIEvents(txResult.Events, map[string]struct{}{}, false)
if err != nil {
return nil, status.Errorf(codes.Unknown, "failed to convert events: %v", err)
}

return &txtypes.SimulateResponse{
GasInfo: &sdk.GasInfo{
GasUsed: txResult.GasUsed,
GasWanted: txResult.GasWanted,
},
Result: &sdk.Result{
MsgResponses: msgResponses,
Events: event,
},
}, nil
}
Expand Down
43 changes: 43 additions & 0 deletions server/v2/stf/stf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package stf

import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
corecontext "cosmossdk.io/core/context"
Expand Down Expand Up @@ -358,12 +360,53 @@ func (s STF[T]) runTxMsgs(
e.EventIndex = int32(j + 1)
events = append(events, e)
}

// add message event
events = append(events, createMessageEvent(msg, int32(i+1), int32(len(execCtx.events)+1)))
}

consumed := execCtx.meter.Limit() - execCtx.meter.Remaining()
return msgResps, consumed, events, nil
}

// Create a message event, with two kv: action, the type url of the message
// and module, the module of the message.
func createMessageEvent(msg transaction.Msg, msgIndex, eventIndex int32) event.Event {
// Assumes that module name is the second element of the msg type URL
// e.g. "cosmos.bank.v1beta1.MsgSend" => "bank"
// It returns an empty string if the input is not a valid type URL
getModuleNameFromTypeURL := func(input string) string {
moduleName := strings.Split(input, ".")
if len(moduleName) > 1 {
return moduleName[1]
}

return ""
}

return event.Event{
MsgIndex: msgIndex,
EventIndex: eventIndex,
Type: "message",
Attributes: func() ([]appdata.EventAttribute, error) {
typeURL := msgTypeURL(msg)
return []appdata.EventAttribute{
{Key: "action", Value: "/" + typeURL},
{Key: "module", Value: getModuleNameFromTypeURL(typeURL)},
}, nil
},
Comment on lines +392 to +397
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for msgTypeURL.

The function should handle potential errors from msgTypeURL.

Add error handling:

 Attributes: func() ([]appdata.EventAttribute, error) {
   typeURL := msgTypeURL(msg)
+  if typeURL == "" {
+    return nil, fmt.Errorf("failed to get type URL for message")
+  }
   return []appdata.EventAttribute{
     {Key: "action", Value: "/" + typeURL},
     {Key: "module", Value: getModuleNameFromTypeURL(typeURL)},
   }, nil
 },

Committable suggestion skipped: line range outside the PR's diff.

Data: func() (json.RawMessage, error) {
typeURL := msgTypeURL(msg)
attrs := []appdata.EventAttribute{
{Key: "action", Value: "/" + typeURL},
{Key: "module", Value: getModuleNameFromTypeURL(typeURL)},
}

return json.Marshal(attrs)
},
}
}

// preBlock executes the pre block logic.
func (s STF[T]) preBlock(
ctx *executionContext,
Expand Down
23 changes: 18 additions & 5 deletions server/v2/stf/stf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ func TestSTF(t *testing.T) {
}
// check TxEvents
events := txResult.Events
if len(events) != 6 {
t.Fatalf("Expected 6 TxEvents, got %d", len(events))
if len(events) != 7 {
t.Fatalf("Expected 7 TxEvents, got %d", len(events))
}
for i, event := range events {
if event.BlockStage != appdata.TxProcessingStage {
Expand All @@ -235,7 +235,8 @@ func TestSTF(t *testing.T) {
if event.TxIndex != 1 {
t.Errorf("Expected TxIndex 1, got %d", event.TxIndex)
}
if event.EventIndex != int32(i%2+1) {
if event.EventIndex != int32(i%2+1) &&
(event.Type == "message" && event.EventIndex != 3) { // special case for message event type as it happens in the msg handling flow
t.Errorf("Expected EventIndex %d, got %d", i%2+1, event.EventIndex)
}

Expand All @@ -247,7 +248,7 @@ func TestSTF(t *testing.T) {
t.Errorf("Expected 1 or 2 attributes, got %d", len(attrs))
}

if len(attrs) == 2 {
if len(attrs) == 2 && event.Type != "message" {
if attrs[1].Key != "index" || attrs[1].Value != "2" {
t.Errorf("Expected attribute key 'index' and value '2', got key '%s' and value '%s'", attrs[1].Key, attrs[1].Value)
}
Expand All @@ -273,7 +274,19 @@ func TestSTF(t *testing.T) {
if attrs[0].Key != "msg" || attrs[0].Value != "&BoolValue{Value:true,XXX_unrecognized:[],}" {
t.Errorf("Expected msg attribute with value '&BoolValue{Value:true,XXX_unrecognized:[],}', got '%s'", attrs[0].Value)
}
case 4, 5:
case 4:
if event.Type != "message" {
t.Errorf("Expected event type 'message', got %s", event.Type)
}

if event.MsgIndex != 1 {
t.Errorf("Expected MsgIndex 1, got %d", event.MsgIndex)
}

if attrs[0].Key != "action" || attrs[0].Value != "/google.protobuf.BoolValue" {
t.Errorf("Expected msg attribute with value '/google.protobuf.BoolValue', got '%s'", attrs[0].Value)
}
case 5, 6:
if event.Type != "post-tx-exec" {
t.Errorf("Expected event type 'post-tx-exec', got %s", event.Type)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/systemtests/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestSimulateTx_GRPC(t *testing.T) {
require.NoError(t, err)
// Check the result and gas used are correct.
//
// The 12 events are:
// The 10 events are:
// - Sending Fee to the pool: coin_spent, coin_received and transfer
// - tx.* events: tx.fee, tx.acc_seq, tx.signature
// - Sending Amount to recipient: coin_spent, coin_received and transfer
Expand Down
Loading