Skip to content

Commit

Permalink
Merge pull request #325 from bandprotocol/v25-patch-yoda
Browse files Browse the repository at this point in the history
Update yoda to rely on data from request_id instead of event
  • Loading branch information
RogerKSI authored Sep 12, 2023
2 parents a269f74 + fda0803 commit 294da33
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 116 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [v2.5.3](https://github.com/bandprotocol/chain/releases/tag/v2.5.3)

- (bump) Use cometbft v0.34.29
- (yoda) Get information of requests through endpoint instead of events

## [v2.5.2](https://github.com/bandprotocol/chain/releases/tag/v2.5.2)

- (bump) Use cosmos-sdk v0.45.16 / ibc-go v4.3.1 / cometbft v0.34.28
Expand Down
37 changes: 0 additions & 37 deletions yoda/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package yoda

import (
"fmt"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -16,42 +15,6 @@ type rawRequest struct {
calldata string
}

// GetRawRequests returns the list of all raw data requests in the given log.
func GetRawRequests(log sdk.ABCIMessageLog) ([]rawRequest, error) {
dataSourceIDs := GetEventValues(log, types.EventTypeRawRequest, types.AttributeKeyDataSourceID)
dataSourceHashList := GetEventValues(log, types.EventTypeRawRequest, types.AttributeKeyDataSourceHash)
externalIDs := GetEventValues(log, types.EventTypeRawRequest, types.AttributeKeyExternalID)
calldataList := GetEventValues(log, types.EventTypeRawRequest, types.AttributeKeyCalldata)

if len(dataSourceIDs) != len(externalIDs) {
return nil, fmt.Errorf("Inconsistent data source count and external ID count")
}
if len(dataSourceIDs) != len(calldataList) {
return nil, fmt.Errorf("Inconsistent data source count and calldata count")
}

var reqs []rawRequest
for idx := range dataSourceIDs {
dataSourceID, err := strconv.Atoi(dataSourceIDs[idx])
if err != nil {
return nil, fmt.Errorf("Failed to parse data source id: %s", err.Error())
}

externalID, err := strconv.Atoi(externalIDs[idx])
if err != nil {
return nil, fmt.Errorf("Failed to parse external id: %s", err.Error())
}

reqs = append(reqs, rawRequest{
dataSourceID: types.DataSourceID(dataSourceID),
dataSourceHash: dataSourceHashList[idx],
externalID: types.ExternalID(externalID),
calldata: calldataList[idx],
})
}
return reqs, nil
}

// GetEventValues returns the list of all values in the given log with the given type and key.
func GetEventValues(log sdk.ABCIMessageLog, evType string, evKey string) (res []string) {
for _, ev := range log.Events {
Expand Down
101 changes: 23 additions & 78 deletions yoda/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,102 +45,47 @@ func handleTransaction(c *Context, l *Logger, tx abci.TxResult) {
}

func handleRequestLog(c *Context, l *Logger, log sdk.ABCIMessageLog) {
idStr, err := GetEventValue(log, types.EventTypeRequest, types.AttributeKeyID)
if err != nil {
l.Debug(":cold_sweat: Failed to parse request id with error: %s", err.Error())
return
}
idStrs := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyID)

id, err := strconv.Atoi(idStr)
if err != nil {
l.Error(":cold_sweat: Failed to convert %s to integer with error: %s", c, idStr, err.Error())
return
for _, idStr := range idStrs {
id, err := strconv.Atoi(idStr)
if err != nil {
l.Error(":cold_sweat: Failed to convert %s to integer with error: %s", c, idStr, err.Error())
return
}

// If id is in pending requests list, then skip it.
if c.pendingRequests[types.RequestID(id)] {
l.Debug(":eyes: Request is in pending list, then skip")
return
}

go handleRequest(c, l, types.RequestID(id))
}
}

func handleRequest(c *Context, l *Logger, id types.RequestID) {
l = l.With("rid", id)

// If id is in pending requests list, then skip it.
if c.pendingRequests[types.RequestID(id)] {
l.Debug(":eyes: Request is in pending list, then skip")
req, err := GetRequest(c, l, id)
if err != nil {
l.Error(":skull: Failed to get request with error: %s", c, err.Error())
return
}

// Skip if not related to this validator
validators := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyValidator)
hasMe := false
for _, validator := range validators {
if validator == c.validator.String() {
for _, val := range req.RequestedValidators {
if val == c.validator.String() {
hasMe = true
break
}
}

if !hasMe {
l.Debug(":next_track_button: Skip request not related to this validator")
return
}

l.Info(":delivery_truck: Processing incoming request event")

reqs, err := GetRawRequests(log)
if err != nil {
l.Error(":skull: Failed to parse raw requests with error: %s", c, err.Error())
}

keyIndex := c.nextKeyIndex()
key := c.keys[keyIndex]

reports, execVersions := handleRawRequests(c, l, types.RequestID(id), reqs, key)

rawAskCount := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyAskCount)
if len(rawAskCount) != 1 {
panic("Fail to get ask count")
}
askCount := MustAtoi(rawAskCount[0])

rawMinCount := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyMinCount)
if len(rawMinCount) != 1 {
panic("Fail to get min count")
}
minCount := MustAtoi(rawMinCount[0])

rawCallData := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyCalldata)
if len(rawCallData) != 1 {
panic("Fail to get call data")
}
callData, err := hex.DecodeString(rawCallData[0])
if err != nil {
l.Error(":skull: Fail to parse call data: %s", c, err.Error())
}

var clientID string
rawClientID := GetEventValues(log, types.EventTypeRequest, types.AttributeKeyClientID)
if len(rawClientID) > 0 {
clientID = rawClientID[0]
}

c.pendingMsgs <- ReportMsgWithKey{
msg: types.NewMsgReportData(types.RequestID(id), reports, c.validator),
execVersion: execVersions,
keyIndex: keyIndex,
feeEstimationData: FeeEstimationData{
askCount: askCount,
minCount: minCount,
callData: callData,
rawRequests: reqs,
clientID: clientID,
},
}
}

func handlePendingRequest(c *Context, l *Logger, id types.RequestID) {
req, err := GetRequest(c, l, id)
if err != nil {
l.Error(":skull: Failed to get request with error: %s", c, err.Error())
return
}

l.Info(":delivery_truck: Processing pending request")
l.Info(":delivery_truck: Processing request")

keyIndex := c.nextKeyIndex()
key := c.keys[keyIndex]
Expand Down
2 changes: 1 addition & 1 deletion yoda/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func runImpl(c *Context, l *Logger) error {
l.Info(":mag: Found %d pending requests", len(pendingRequests.RequestIDs))
for _, id := range pendingRequests.RequestIDs {
c.pendingRequests[types.RequestID(id)] = true
go handlePendingRequest(c, l.With("rid", id), types.RequestID(id))
go handleRequest(c, l, types.RequestID(id))
}

for {
Expand Down

0 comments on commit 294da33

Please sign in to comment.