Skip to content

Commit

Permalink
add test case and update inner msg parser
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentysc committed Aug 12, 2022
1 parent 900cadc commit e309714
Show file tree
Hide file tree
Showing 9 changed files with 2,744 additions and 199 deletions.
213 changes: 21 additions & 192 deletions usecase/parser/inner_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ type EventType struct {

func ParseTxsResultsEvents(
msgs []interface{},
events *[]model.BlockResultsEvent,
events []model.BlockResultsEvent,
) []model.BlockResultsTxsResultLog {
var filteredEvents []model.BlockResultsEvent
var resultLog []model.BlockResultsTxsResultLog

// remove event type = tx and message action = MsgExec
for _, event := range *events {
fmt.Println("===> event: ", event)
for _, event := range events {
if event.Type == "message" && event.Attributes[0].Key == "action" {
continue
}
filteredEvents = append(filteredEvents, event)
}

parsedEvents := utils.NewParsedTxsResultsEvents(filteredEvents)

for innerMsgIndex, innerMsgInterface := range msgs {
innerMsg, ok := innerMsgInterface.(map[string]interface{})
if !ok {
Expand Down Expand Up @@ -59,7 +57,6 @@ func ParseInnerMsgsEvents(
parsedEvents *utils.ParsedTxsResultsEvents,
) []model.BlockResultsEvent {
var extractedEvents []model.BlockResultsEvent

switch innerMsgType {
// cosmos bank
case "/cosmos.bank.v1beta1.MsgSend":
Expand All @@ -74,24 +71,8 @@ func ParseInnerMsgsEvents(
extractedEvents = MsgWithdrawValidatorCommission(parsedEvents, innerMsgIndex)
case "/cosmos.distribution.v1beta1.MsgFundCommunityPool":
extractedEvents = MsgFundCommunityPool(parsedEvents, innerMsgIndex)

// cosmos staking
case "/cosmos.staking.v1beta1.MsgDelegate":
extractedEvents = MsgDelegate(parsedEvents, innerMsgIndex)
case "/cosmos.staking.v1beta1.MsgUndelegate":
extractedEvents = MsgUndelegate(parsedEvents, innerMsgIndex)
case "/cosmos.staking.v1beta1.MsgBeginRedelegate":
extractedEvents = MsgBeginRedelegate(parsedEvents, innerMsgIndex)

// cosmos gov
case "/cosmos.gov.v1beta1.MsgSubmitProposal":
extractedEvents = MsgFundCommunityPool(parsedEvents, innerMsgIndex)
case "/cosmos.gov.v1beta1.MsgVote":
extractedEvents = MsgFundCommunityPool(parsedEvents, innerMsgIndex)
case "/cosmos.gov.v1beta1.MsgDeposit":
extractedEvents = MsgFundCommunityPool(parsedEvents, innerMsgIndex)

}

return extractedEvents
}

Expand Down Expand Up @@ -119,7 +100,7 @@ func MsgSend(events *utils.ParsedTxsResultsEvents,
}

// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypes, events)
extractedEvents = extractMsgEvents(eventTypes, events)

return extractedEvents
}
Expand All @@ -128,6 +109,7 @@ func MsgSetWithdrawAddress(events *utils.ParsedTxsResultsEvents,
innerMsgIndex int,
) []model.BlockResultsEvent {
var extractedEvents []model.BlockResultsEvent
//
eventTypes := []EventType{
{
Type: "set_withdraw_address",
Expand All @@ -140,7 +122,7 @@ func MsgSetWithdrawAddress(events *utils.ParsedTxsResultsEvents,
}

// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypes, events)
extractedEvents = extractMsgEvents(eventTypes, events)
return extractedEvents
}

Expand All @@ -155,7 +137,8 @@ func MsgWithdrawDelegatorReward(events *utils.ParsedTxsResultsEvents,
},
{
Type: "coin_received",
Count: 1},
Count: 1,
},
{
Type: "transfer",
Count: 1,
Expand All @@ -181,64 +164,18 @@ func MsgWithdrawDelegatorReward(events *utils.ParsedTxsResultsEvents,
}

// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypes, events)
extractedEvents = extractMsgEvents(eventTypes, events)

if len(extractedEvents) <= 0 {
// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypesWithoutAmount, events)
extractedEvents = extractMsgEvents(eventTypesWithoutAmount, events)
}

return extractedEvents
}

func MsgWithdrawValidatorCommission(events *utils.ParsedTxsResultsEvents,
innerMsgIndex int,
) []model.BlockResultsEvent {
var extractedEvents []model.BlockResultsEvent
eventTypes := []EventType{
{
Type: "coin_spent",
Count: 1,
},
{
Type: "coin_received",
Count: 1},
{
Type: "transfer",
Count: 1},
{
Type: "message",
Count: 2,
},
{
Type: "withdraw_commission",
Count: 1,
},
}

eventTypesWithoutAmount := []EventType{
{
Type: "message",
Count: 1,
},
{
Type: "withdraw_commission",
Count: 1,
},
}

// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypes, events)
if len(extractedEvents) <= 0 {
// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypesWithoutAmount, events)
}

return extractedEvents
}

func MsgFundCommunityPool(events *utils.ParsedTxsResultsEvents,
innerMsgIndex int,
) []model.BlockResultsEvent {
var extractedEvents []model.BlockResultsEvent
eventTypes := []EventType{
Expand All @@ -258,130 +195,35 @@ func MsgFundCommunityPool(events *utils.ParsedTxsResultsEvents,
Type: "message",
Count: 2,
},
}

eventTypesWithoutAmount := []EventType{
{
Type: "message",
Count: 1,
},
}

// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypes, events)
if len(extractedEvents) <= 0 {
// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypesWithoutAmount, events)
}

return extractedEvents
}

func MsgDelegate(events *utils.ParsedTxsResultsEvents,
innerMsgIndex int,
) []model.BlockResultsEvent {
var extractedEvents []model.BlockResultsEvent
eventTypes := []EventType{
{
Type: "coin_spent",
Count: 1,
},
{
Type: "coin_received",
Count: 1,
},
{
Type: "transfer",
Count: 1,
},
{
Type: "delegate",
Type: "withdraw_commission",
Count: 1,
},
{
Type: "message",
Count: 2,
},
}

eventTypesWithoutAmount := []EventType{
{
Type: "coin_spent",
Count: 1,
},
{
Type: "coin_received",
Count: 1,
},
{
Type: "delegate",
Count: 1,
},
{
Type: "message",
Count: 2,
},
}

// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypes, events)
if len(extractedEvents) <= 0 {
// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypesWithoutAmount, events)
}

return extractedEvents
}

func MsgUndelegate(events *utils.ParsedTxsResultsEvents,
innerMsgIndex int,
) []model.BlockResultsEvent {
var extractedEvents []model.BlockResultsEvent
eventTypes := []EventType{
{
Type: "coin_spent",
Count: 1,
},
{
Type: "coin_received",
Count: 1,
},
{
Type: "transfer",
Count: 1,
},
{
Type: "unbond",
Count: 1,
},
{
Type: "message",
Count: 2,
},
}

eventTypesWithoutAmount := []EventType{
{
Type: "unbond",
Count: 1,
},
{
Type: "message",
Type: "withdraw_commission",
Count: 1,
},
}

tempEvents := *events
// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypes, events)
extractedEvents = extractMsgEvents(eventTypes, &tempEvents)
if len(extractedEvents) <= 0 {
// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypesWithoutAmount, events)
extractedEvents = extractMsgEvents(eventTypesWithoutAmount, &tempEvents)
}
events = &tempEvents

return extractedEvents
}

func MsgBeginRedelegate(events *utils.ParsedTxsResultsEvents,
func MsgFundCommunityPool(events *utils.ParsedTxsResultsEvents,
innerMsgIndex int,
) []model.BlockResultsEvent {
var extractedEvents []model.BlockResultsEvent
Expand All @@ -398,56 +240,43 @@ func MsgBeginRedelegate(events *utils.ParsedTxsResultsEvents,
Type: "transfer",
Count: 1,
},
{
Type: "unbond",
Count: 1,
},
{
Type: "message",
Count: 2,
},
}

eventTypesWithoutAmount := []EventType{
{
Type: "unbond",
Count: 1,
},
{
Type: "message",
Count: 1,
},
}

// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypes, events)
extractedEvents = extractMsgEvents(eventTypes, events)
if len(extractedEvents) <= 0 {
// extract events
extractedEvents = extractMsgEvents(innerMsgIndex, eventTypesWithoutAmount, events)
extractedEvents = extractMsgEvents(eventTypesWithoutAmount, events)
}

return extractedEvents
}

func extractMsgEvents(innerMsgIndex int, eventTypes []EventType, events *utils.ParsedTxsResultsEvents) []model.BlockResultsEvent {
func extractMsgEvents(eventTypes []EventType, events *utils.ParsedTxsResultsEvents) []model.BlockResultsEvent {
var extractedEvents []model.BlockResultsEvent

for _, eventType := range eventTypes {
for i := 0; i < int(eventType.Count); i++ {

if i > len(events.GetTypeIndex(eventType.Type))-1 {
if i > len(events.GetTypeIndex(eventType.Type)) || len(events.GetTypeIndex(eventType.Type)) <= 0 {
return []model.BlockResultsEvent{}
}
eventIndex := events.GetTypeIndex(eventType.Type)[0]
extractedEvents = append(extractedEvents, events.GetRawEvents()[eventIndex])
fmt.Println("===> eventIndex: ", eventIndex)
events.RemoveIndexType(eventType.Type, 0)
fmt.Println("===> eventType: ", events.GetTypeIndex(eventType.Type))

}
}

fmt.Println("===> extractedEvents: ", extractedEvents)

return extractedEvents
}
3 changes: 1 addition & 2 deletions usecase/parser/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func ParseBlockTxsMsgToCommands(
TxSuccess: txSuccess,
MsgIndex: msgIndex,
}
fmt.Println("===> TxHash", txHash)

var msgCommands []command.Command
var possibleSignerAddresses []string
Expand Down Expand Up @@ -1617,7 +1616,7 @@ func parseMsgExecInnerMsgs(
panic(fmt.Errorf("error parsing MsgExec.msgs to []interface{}: %v", parserParams.Msg["msgs"]))
}

parserParams.TxsResult.Log = ParseTxsResultsEvents(msgs, &parserParams.TxsResult.Events)
parserParams.TxsResult.Log = ParseTxsResultsEvents(msgs, parserParams.TxsResult.Events)

bytes, _ := json.Marshal(parserParams.TxsResult.Log)
rawLog, _ := json.Marshal(bytes)
Expand Down
Loading

0 comments on commit e309714

Please sign in to comment.