Skip to content

Commit

Permalink
Fix Guardian Message/Event Parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Oct 17, 2024
1 parent 091fdad commit 61b4ebc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
36 changes: 27 additions & 9 deletions node/pkg/accountant/submit_obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,22 +435,40 @@ func GetObservationResponses(txResp *sdktx.BroadcastTxResponse) (map[string]Obse
}

var msg sdktypes.TxMsgData

if err := msg.Unmarshal(data); err != nil {
return nil, fmt.Errorf("failed to unmarshal data: %w", err)
}

if len(msg.MsgResponses) == 0 {

fmt.Println("TXRESP -", txResp.TxResponse.Data)
fmt.Println("MSGRESP -", msg)
isMsgData := len(msg.Data) > 0
isMsgResponses := len(msg.MsgResponses) > 0

return nil, fmt.Errorf("msg responses field is empty")
}
// TODO: JOEL - REMOVE ME
fmt.Println("JOEL - TXRESP:", txResp.TxResponse)
fmt.Println("JOEL - MSG:", msg)
fmt.Println("JOEL - MSG DATA:", msg.Data)
fmt.Println("JOEL - IsMsgData:", isMsgData)
fmt.Println("JOEL - isMsgResponses:", isMsgResponses)

var execContractResp wasmdtypes.MsgExecuteContractResponse
if err := execContractResp.Unmarshal(msg.MsgResponses[0].Value); err != nil {
return nil, fmt.Errorf("failed to unmarshal ExecuteContractResponse: %w", err)

if isMsgData {

// TODO: JOEL - REMOVE ME
fmt.Println("JOEL - PARSING MSG.DATA")

if err := execContractResp.Unmarshal(msg.Data[0].Data); err != nil {
return nil, fmt.Errorf("failed to unmarshal ExecuteContractResponse from msg.Data: %w", err)
}
} else if isMsgResponses {

// TODO: JOEL - REMOVE ME
fmt.Println("JOEL - PARSING MSG.RESPONSES")

if err := execContractResp.Unmarshal(msg.MsgResponses[0].Value); err != nil {
return nil, fmt.Errorf("failed to unmarshal ExecuteContractResponse from msg.MsgResponses: %w", err)
}
} else {
return nil, fmt.Errorf("msg data & msg responses field is empty")
}

var responses ObservationResponses
Expand Down
23 changes: 18 additions & 5 deletions node/pkg/accountant/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package accountant

import (
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -141,13 +142,25 @@ type (
func parseEvent[T any](logger *zap.Logger, event tmAbci.Event, name string, contractAddress string) (*T, error) {
attrs := make(map[string]json.RawMessage)
for _, attr := range event.Attributes {
if string(attr.Key) == "_contract_address" {
if string(attr.Value) != contractAddress {
return nil, fmt.Errorf("%s event from unexpected contract: %s", name, string(attr.Value))
key := attr.Key
value := attr.Value

// decode base64 encoded keys and values (if applicable)
if keyRaw, err := base64.StdEncoding.DecodeString(key); err == nil {
key = string(keyRaw)
}

if valueRaw, err := base64.StdEncoding.DecodeString(value); err == nil {
value = string(valueRaw)
}

if key == "_contract_address" {
if value != contractAddress {
return nil, fmt.Errorf("%s event from unexpected contract: %s", name, value)
}
} else {
logger.Debug("event attribute", zap.String("event", name), zap.String("key", string(attr.Key)), zap.String("value", string(attr.Value)))
attrs[string(attr.Key)] = []byte(attr.Value)
logger.Debug("event attribute", zap.String("event", name), zap.String("key", key), zap.String("value", value))
attrs[key] = []byte(value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion node/pkg/watchers/ibc/wasm_attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (wa *WasmAttributes) Parse(logger *zap.Logger, event gjson.Result) error {
// return fmt.Errorf("event attribute value is invalid base64: %s", attribute.String())
// }

// TODO: JOEL - Some Wasm Events aren't Base64 encoded
// TODO: JOEL - Some Wasm Events aren't Base64 encoded -- or is it just wormhole tests still using base64 mock data

if _, ok := wa.m[key]; ok {
return fmt.Errorf("duplicate key in event: %s", key)
Expand Down

0 comments on commit 61b4ebc

Please sign in to comment.