-
Notifications
You must be signed in to change notification settings - Fork 44
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
LogPoller Boilerplate #950
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The interface for LogPoller (as well as all of the data structures that will be passed between QueryKey & LogPoller) is fully specified in the design doc. In principle, that should be enough for QueryKey to be implemented, and should be considered the source of truth--so I'd recommend starting there, and if there is anything missing or that needs to be updated in light of more recent conversations we can discuss. |
||
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 | ||
} |
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() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to accept a slice of events, more like this:
But we already have an ORM method that does basically that, so we can probably just use that unless we want to wrap it with some conversion layer:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works. This method is only a placeholder and can use the ORM method instead.