From b99102cd432675a82a665ae92d42285819f48570 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 11 Dec 2024 22:50:45 +0100 Subject: [PATCH] more readable --- server/v2/stf/stf.go | 69 ++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/server/v2/stf/stf.go b/server/v2/stf/stf.go index fbea637f34f9..c8e7a58a6ffb 100644 --- a/server/v2/stf/stf.go +++ b/server/v2/stf/stf.go @@ -361,45 +361,50 @@ func (s STF[T]) runTxMsgs( events = append(events, e) } - // create message event (including message.action and message.module) - events = append(events, event.Event{ - MsgIndex: int32(i + 1), - EventIndex: int32(len(execCtx.events) + 1), - Type: "message", - Attributes: func() ([]appdata.EventAttribute, error) { - typeURL := msgTypeURL(msg) - return []appdata.EventAttribute{ - {Key: "message.action", Value: "/" + typeURL}, - {Key: "message.module", Value: getModuleNameFromTypeURL(typeURL)}, - }, nil - }, - Data: func() (json.RawMessage, error) { - typeURL := msgTypeURL(msg) - attrs := []appdata.EventAttribute{ - {Key: "message.action", Value: "/" + typeURL}, - {Key: "message.module", Value: getModuleNameFromTypeURL(typeURL)}, - } - - return json.Marshal(attrs) - }, - }) - + // 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 } -// 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 -func getModuleNameFromTypeURL(input string) string { - moduleName := strings.Split(input, ".") - if len(moduleName) > 1 { - return moduleName[1] - } +// 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 "" + } + + return event.Event{ + MsgIndex: msgIndex, + EventIndex: eventIndex, + Type: "message", + Attributes: func() ([]appdata.EventAttribute, error) { + typeURL := msgTypeURL(msg) + return []appdata.EventAttribute{ + {Key: "message.action", Value: "/" + typeURL}, + {Key: "message.module", Value: getModuleNameFromTypeURL(typeURL)}, + }, nil + }, + Data: func() (json.RawMessage, error) { + typeURL := msgTypeURL(msg) + attrs := []appdata.EventAttribute{ + {Key: "message.action", Value: "/" + typeURL}, + {Key: "message.module", Value: getModuleNameFromTypeURL(typeURL)}, + } + + return json.Marshal(attrs) + }, + } } // preBlock executes the pre block logic.