-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic boilerplate that connects loader to base log poller
- Loading branch information
1 parent
e68ee4c
commit 9a56419
Showing
5 changed files
with
292 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,3 +40,4 @@ packages: | |
github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller: | ||
interfaces: | ||
RPCClient: | ||
EventSaver: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package logpoller | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
"github.com/smartcontractkit/chainlink-common/pkg/services" | ||
) | ||
|
||
type EventSaver interface { | ||
SaveEvent(evt ProgramEvent) error | ||
} | ||
|
||
type Service struct { | ||
// dependencies | ||
lggr logger.Logger | ||
saver EventSaver | ||
|
||
// internal | ||
loader *EncodedLogCollector | ||
mu sync.RWMutex | ||
discriminators map[string]struct{} | ||
chSave chan ProgramEvent | ||
|
||
// service state management | ||
services.Service | ||
engine *services.Engine | ||
} | ||
|
||
func New(client RPCClient, lggr logger.Logger, saver EventSaver) *Service { | ||
p := &Service{ | ||
saver: saver, | ||
discriminators: make(map[string]struct{}), | ||
chSave: make(chan ProgramEvent), | ||
} | ||
|
||
p.Service, p.engine = services.Config{ | ||
Name: "LogPollerService", | ||
NewSubServices: func(lggr logger.Logger) []services.Service { | ||
p.loader = NewEncodedLogCollector(client, p, lggr) | ||
|
||
return []services.Service{p.loader} | ||
}, | ||
Start: p.start, | ||
Close: p.close, | ||
}.NewServiceEngine(lggr) | ||
p.lggr = p.engine.SugaredLogger | ||
|
||
return p | ||
} | ||
|
||
func (p *Service) AddFilter(name string) error { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
hash := sha256.New() | ||
hash.Write([]byte(fmt.Sprintf("event:%s", name))) | ||
|
||
p.discriminators[string(hash.Sum(nil)[:8])] = struct{}{} | ||
|
||
return nil | ||
} | ||
|
||
func (p *Service) start(_ context.Context) error { | ||
p.engine.Go(p.runSaveProcess) | ||
|
||
return nil | ||
} | ||
|
||
func (p *Service) close() error { | ||
return nil | ||
} | ||
|
||
func (p *Service) Process(evt ProgramEvent) error { | ||
encodedData := strings.TrimSpace(evt.Data) | ||
data, err := base64.StdEncoding.DecodeString(encodedData) | ||
if err != nil { | ||
// don't return an error here, just log it | ||
// returning an error will trigger a retry | ||
p.lggr.Errorw("failed to base64 decode data", "err", err) | ||
|
||
return nil | ||
} | ||
|
||
// silently discard events that don't match any expected event signatures | ||
if !p.dataMatchesEventSig(data[:8]) { | ||
return nil | ||
} | ||
|
||
p.chSave <- evt | ||
|
||
return nil | ||
} | ||
|
||
func (p *Service) runSaveProcess(ctx context.Context) { | ||
// this process should ensure ordered batches before saving atomically to the database | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case evt := <-p.chSave: | ||
if err := p.saver.SaveEvent(evt); err != nil { | ||
p.lggr.Errorw("failed to save event", "err", err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (p *Service) dataMatchesEventSig(sig []byte) bool { | ||
p.mu.RLock() | ||
defer p.mu.RUnlock() | ||
|
||
_, ok := p.discriminators[string(sig)] | ||
|
||
return ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package logpoller_test | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests" | ||
|
||
"github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller" | ||
"github.com/smartcontractkit/chainlink-solana/pkg/solana/logpoller/mocks" | ||
) | ||
|
||
func TestLogPoller_ProcessAndSave(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := new(mocks.RPCClient) | ||
saver := new(testSaver) | ||
poller := logpoller.New(client, logger.Nop(), saver) | ||
|
||
require.NoError(t, poller.AddFilter("TestEvent")) | ||
|
||
clientExpectSingleEvent(client) | ||
|
||
require.NoError(t, poller.Start(tests.Context(t))) | ||
|
||
t.Cleanup(func() { | ||
require.NoError(t, poller.Close()) | ||
}) | ||
|
||
tests.AssertEventually(t, func() bool { | ||
return saver.Called() | ||
}) | ||
|
||
client.AssertExpectations(t) | ||
} | ||
|
||
type testSaver struct { | ||
called atomic.Bool | ||
count atomic.Uint64 | ||
} | ||
|
||
func (s *testSaver) SaveEvent(event logpoller.ProgramEvent) error { | ||
s.called.Store(true) | ||
s.count.Store(s.count.Load() + 1) | ||
|
||
return nil | ||
} | ||
|
||
func (s *testSaver) Called() bool { | ||
return s.called.Load() | ||
} | ||
|
||
func (s *testSaver) Count() uint64 { | ||
return s.count.Load() | ||
} |