Skip to content

Commit

Permalink
Handle escape value from event (#16)
Browse files Browse the repository at this point in the history
* chore: remove processing by category

* fix: handle escaped string from event value
  • Loading branch information
jrwbabylonlab authored Oct 16, 2024
1 parent cc5f0db commit cca588f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 111 deletions.
2 changes: 1 addition & 1 deletion config/config-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ btc:
rpc-user: rpcuser
rpc-pass: rpcpass
bbn:
rpc-addr: https://rpc.devnet.babylonchain.io:443
rpc-addr: https://rpc.devnet.babylonlabs.io:443
timeout: 30s
poller:
param-polling-interval: 60s
Expand Down
2 changes: 1 addition & 1 deletion config/config-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ btc:
rpc-user: rpcuser
rpc-pass: rpcpass
bbn:
rpc-addr: https://rpc.devnet.babylonchain.io:443
rpc-addr: https://rpc.devnet.babylonlabs.io:443
timeout: 30s
poller:
param-polling-interval: 10s
Expand Down
36 changes: 14 additions & 22 deletions internal/services/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils"
abcitypes "github.com/cometbft/cometbft/abci/types"
"github.com/rs/zerolog/log"
)
Expand Down Expand Up @@ -51,29 +52,19 @@ func (s *Service) StartBbnEventProcessor(ctx context.Context) {

// Entry point for processing events
func (s *Service) processEvent(ctx context.Context, event BbnEvent) {
switch event.Category {
case BlockCategory:
s.processBbnBlockEvent(ctx, event.Event)
case TxCategory:
s.processBbnTxEvent(ctx, event.Event)
default:
log.Fatal().Msgf("Unknown event category: %s", event.Category)
}
}

func (s *Service) processBbnTxEvent(ctx context.Context, event abcitypes.Event) {
switch EventTypes(event.Type) {
// Note: We no longer need to check for the event category here. We can directly
// process the event based on its type.
bbnEvent := event.Event
switch EventTypes(bbnEvent.Type) {
case EventFinalityProviderCreatedType:
s.processNewFinalityProviderEvent(ctx, event)
log.Debug().Msg("Processing new finality provider event")
s.processNewFinalityProviderEvent(ctx, bbnEvent)
case EventFinalityProviderEditedType:
s.processFinalityProviderEditedEvent(ctx, event)
}
}

func (s *Service) processBbnBlockEvent(ctx context.Context, event abcitypes.Event) {
switch EventTypes(event.Type) {
case EventFinalityProviderStateChangeType:
s.processFinalityProviderStateChangeEvent(ctx, event)
log.Debug().Msg("Processing finality provider edited event")
s.processFinalityProviderEditedEvent(ctx, bbnEvent)
case EventFinalityProviderStatusChange:
log.Debug().Msg("Processing finality provider status change event")
s.processFinalityProviderStateChangeEvent(ctx, bbnEvent)
}
}

Expand Down Expand Up @@ -108,7 +99,8 @@ func parseEvent[T any](

// Populate the attribute map from the event's attributes
for _, attr := range event.Attributes {
attributeMap[attr.Key] = attr.Value
// Unescape the attribute value
attributeMap[attr.Key] = utils.SafeUnescape(attr.Value)
}

// Marshal the attributeMap into JSON
Expand Down
22 changes: 6 additions & 16 deletions internal/services/finality-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ import (
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/db/model"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/types"
"github.com/babylonlabs-io/babylon-staking-indexer/internal/utils/state"
bbntypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
abcitypes "github.com/cometbft/cometbft/abci/types"
)

const (
EventFinalityProviderCreatedType EventTypes = "babylon.btcstaking.v1.EventFinalityProviderCreated"
EventFinalityProviderEditedType EventTypes = "babylon.btcstaking.v1.EventFinalityProviderEdited"
EventFinalityProviderStateChangeType EventTypes = "babylon.btcstaking.v1.EventFinalityProviderStateChange"
EventFinalityProviderCreatedType EventTypes = "babylon.btcstaking.v1.EventFinalityProviderCreated"
EventFinalityProviderEditedType EventTypes = "babylon.btcstaking.v1.EventFinalityProviderEdited"
EventFinalityProviderStatusChange EventTypes = "babylon.btcstaking.v1.EventFinalityProviderStatusChange"
)

func (s *Service) processNewFinalityProviderEvent(
Expand Down Expand Up @@ -77,7 +76,7 @@ func (s *Service) processFinalityProviderStateChangeEvent(
ctx context.Context, event abcitypes.Event,
) *types.Error {
finalityProviderStateChange, err := parseEvent[bbntypes.EventFinalityProviderStatusChange](
EventFinalityProviderStateChangeType, event,
EventFinalityProviderStatusChange, event,
)
if err != nil {
return err
Expand All @@ -86,24 +85,15 @@ func (s *Service) processFinalityProviderStateChangeEvent(
return err
}

fp, dbErr := s.db.GetFinalityProviderByBtcPk(ctx, finalityProviderStateChange.BtcPk)
// Check FP exists
_, dbErr := s.db.GetFinalityProviderByBtcPk(ctx, finalityProviderStateChange.BtcPk)
if dbErr != nil {
return types.NewError(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Errorf("failed to get finality provider by btc public key: %w", dbErr),
)
}
if ok := state.IsQualifiedStateForFinalityProviderStateChange(fp.State, finalityProviderStateChange.NewState); !ok {
return types.NewErrorWithMsg(
http.StatusInternalServerError,
types.InternalServiceError,
fmt.Sprintf(
"finality provider state change from %s to %s is not allowed",
fp.State, finalityProviderStateChange.NewState,
),
)
}

// If all validations pass, update the finality provider state
if err := s.db.UpdateFinalityProviderState(
Expand Down
31 changes: 0 additions & 31 deletions internal/types/finaltiy-provider.go

This file was deleted.

40 changes: 0 additions & 40 deletions internal/utils/state/finality-provider-state.go

This file was deleted.

12 changes: 12 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"runtime"
"strconv"
"strings"

"github.com/btcsuite/btcd/chaincfg"
Expand Down Expand Up @@ -77,3 +78,14 @@ func shortFuncName(fullName string) string {
}
return fullName
}

// SafeUnescape removes quotes from a string if it is quoted.
// Including the escape character.
func SafeUnescape(s string) string {
unquoted, err := strconv.Unquote(s)
if err != nil {
// Return the original string if unquoting fails
return s
}
return unquoted
}

0 comments on commit cca588f

Please sign in to comment.