Skip to content

Commit

Permalink
lint and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Jan 24, 2025
1 parent bfaa0ed commit 3cef275
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
17 changes: 9 additions & 8 deletions pkg/solana/logpoller/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type filters struct {
filtersMutex sync.RWMutex
loadedFilters atomic.Bool
knownPrograms map[string]uint // fast lookup to see if a base58-encoded ProgramID matches any registered filters
knownDiscriminators map[string]uint // fast lookup by first 10 characters (60-bits) of a base64-encoded discriminator
knownDiscriminators map[string]uint // fast lookup based on raw discriminator bytes as string
seqNums map[int64]int64
decoders map[int64]Decoder
discriminatorExtractor codec.DiscriminatorExtractor
Expand Down Expand Up @@ -231,13 +231,13 @@ func (fl *filters) removeFilterFromIndexes(filter Filter) {
}
}

discriminatorHead := filter.DiscriminatorRawBytes()
if refcount, ok := fl.knownDiscriminators[discriminatorHead]; ok {
discriminator := filter.DiscriminatorRawBytes()
if refcount, ok := fl.knownDiscriminators[discriminator]; ok {
refcount--
if refcount > 0 {
fl.knownDiscriminators[discriminatorHead] = refcount
fl.knownDiscriminators[discriminator] = refcount
} else {
delete(fl.knownDiscriminators, discriminatorHead)
delete(fl.knownDiscriminators, discriminator)
}
}
}
Expand Down Expand Up @@ -423,7 +423,7 @@ func (fl *filters) LoadFilters(ctx context.Context) error {
// DecodeSubKey accepts raw Borsh-encoded event data, a filter ID and a subkeyPath. It uses the decoder
// associated with that filter to decode the event and extract the subkey value from the specified subKeyPath.
// WARNING: not thread safe, should only be called while fl.filtersMutex is held and after filters have been loaded.
func (fl *filters) DecodeSubKey(ctx context.Context, raw []byte, ID int64, subKeyPath []string) (any, error) {
func (fl *filters) DecodeSubKey(ctx context.Context, lggr logger.SugaredLogger, raw []byte, ID int64, subKeyPath []string) (any, error) {
filter, ok := fl.filtersByID[ID]
if !ok {
return nil, fmt.Errorf("filter %d not found", ID)
Expand All @@ -436,8 +436,9 @@ func (fl *filters) DecodeSubKey(ctx context.Context, raw []byte, ID int64, subKe
if err != nil || decodedEvent == nil {
return nil, err
}
err = decoder.Decode(ctx, raw, decodedEvent, filter.EventName)
if err != nil {
if err = decoder.Decode(ctx, raw, decodedEvent, filter.EventName); err != nil {
err = fmt.Errorf("failed to decode sub key raw data: %v, for filter: %s, for subKeyPath: %v, err: %w", raw, subKeyPath, filter.Name, err)
lggr.Criticalw(err.Error())
return nil, err
}
return ExtractField(decodedEvent, subKeyPath)
Expand Down
9 changes: 1 addition & 8 deletions pkg/solana/logpoller/log_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,9 @@ func (lp *Service) Process(programEvent ProgramEvent) (err error) {
return err
}

// TODO isn't discrimintaor already checked above in MatchingFiltersForEncodedEvent?
if len(log.Data) < 8 {
err = fmt.Errorf("assumption violation: %w, log.Data=%s", ErrMissingDiscriminator, log.Data)
lp.lggr.Criticalw(err.Error())
return err
}

log.SubkeyValues = make([]IndexedValue, 0, len(filter.SubkeyPaths))
for _, path := range filter.SubkeyPaths {
subKeyVal, decodeSubKeyErr := lp.filters.DecodeSubKey(ctx, log.Data, filter.ID, path)
subKeyVal, decodeSubKeyErr := lp.filters.DecodeSubKey(ctx, lp.lggr, log.Data, filter.ID, path)
if decodeSubKeyErr != nil {
return decodeSubKeyErr
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/solana/logpoller/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (f Filter) MatchSameLogs(other Filter) bool {
f.EventIdl.Equal(other.EventIdl) && f.SubkeyPaths.Equal(other.SubkeyPaths)
}

// DiscriminatorRawBytes returns raw discriminator bytes as a string, this string is not base64 encoded.
// DiscriminatorRawBytes returns raw discriminator bytes as a string, this string is not base64 encoded and is always len of discriminator which is 8.
func (f Filter) DiscriminatorRawBytes() string {
return string(codec.NewDiscriminatorHashPrefix(f.EventName, false))
}
Expand Down

0 comments on commit 3cef275

Please sign in to comment.