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: sanitise event of double quotes before processing #60

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Changes from all 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
34 changes: 30 additions & 4 deletions internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"strings"
"time"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
Expand Down Expand Up @@ -59,9 +60,7 @@ func (s *Service) processEvent(
err = s.processFinalityProviderEditedEvent(ctx, bbnEvent)
case EventFinalityProviderStatusChange:
log.Debug().Msg("Processing finality provider status change event")
// TODO: fix error from this event
// https://github.com/babylonlabs-io/babylon-staking-indexer/issues/24
s.processFinalityProviderStateChangeEvent(ctx, bbnEvent)
err = s.processFinalityProviderStateChangeEvent(ctx, bbnEvent)
case EventBTCDelegationCreated:
log.Debug().Msg("Processing new BTC delegation event")
err = s.processNewBTCDelegationEvent(ctx, bbnEvent, blockHeight)
Expand Down Expand Up @@ -124,8 +123,11 @@ func parseEvent[T proto.Message](
)
}

// Sanitize the event attributes before parsing
sanitizedEvent := sanitizeEvent(event)

// Use the SDK's ParseTypedEvent function
protoMsg, err := sdk.ParseTypedEvent(event)
protoMsg, err := sdk.ParseTypedEvent(sanitizedEvent)
if err != nil {
log.Debug().Interface("raw_event", event).Msg("Raw event data")
return result, types.NewError(
Expand Down Expand Up @@ -390,3 +392,27 @@ func (s *Service) validateSlashedFinalityProviderEvent(ctx context.Context, even

return true, nil
}

func sanitizeEvent(event abcitypes.Event) abcitypes.Event {
sanitizedAttrs := make([]abcitypes.EventAttribute, len(event.Attributes))
for i, attr := range event.Attributes {
// Remove any extra quotes and ensure proper JSON formatting
value := strings.Trim(attr.Value, "\"")
// If the value isn't already a JSON value (object, array, or quoted string),
// wrap it in quotes
if !strings.HasPrefix(value, "{") && !strings.HasPrefix(value, "[") {
value = fmt.Sprintf("\"%s\"", value)
}

sanitizedAttrs[i] = abcitypes.EventAttribute{
Key: attr.Key,
Value: value,
Index: attr.Index,
}
}

return abcitypes.Event{
Type: event.Type,
Attributes: sanitizedAttrs,
}
}
Loading