From 65f8a777e3e4dce65bbb6d313758346a9bcf1974 Mon Sep 17 00:00:00 2001 From: aalu1418 <50029043+aalu1418@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:20:59 -0600 Subject: [PATCH] wip: slot height tracking --- pkg/monitoring/chain_reader.go | 5 ++++ pkg/monitoring/mocks/ChainReader.go | 24 ++++++++++++++++ pkg/monitoring/source_slotheight.go | 43 +++++++++++++++++++++++++++++ pkg/monitoring/types/types.go | 5 ++++ 4 files changed, 77 insertions(+) create mode 100644 pkg/monitoring/source_slotheight.go create mode 100644 pkg/monitoring/types/types.go diff --git a/pkg/monitoring/chain_reader.go b/pkg/monitoring/chain_reader.go index 760f4f88c..11f19e384 100644 --- a/pkg/monitoring/chain_reader.go +++ b/pkg/monitoring/chain_reader.go @@ -17,6 +17,7 @@ type ChainReader interface { GetBalance(ctx context.Context, account solana.PublicKey, commitment rpc.CommitmentType) (out *rpc.GetBalanceResult, err error) GetSignaturesForAddressWithOpts(ctx context.Context, account solana.PublicKey, opts *rpc.GetSignaturesForAddressOpts) (out []*rpc.TransactionSignature, err error) GetTransaction(ctx context.Context, txSig solana.Signature, opts *rpc.GetTransactionOpts) (out *rpc.GetTransactionResult, err error) + GetSlot(ctx context.Context) (slot uint64, err error) } func NewChainReader(client *rpc.Client) ChainReader { @@ -50,3 +51,7 @@ func (c *chainReader) GetSignaturesForAddressWithOpts(ctx context.Context, accou func (c *chainReader) GetTransaction(ctx context.Context, txSig solana.Signature, opts *rpc.GetTransactionOpts) (out *rpc.GetTransactionResult, err error) { return c.client.GetTransaction(ctx, txSig, opts) } + +func (c *chainReader) GetSlot(ctx context.Context) (uint64, error) { + return c.client.GetSlot(ctx, rpc.CommitmentProcessed) // get latest height +} diff --git a/pkg/monitoring/mocks/ChainReader.go b/pkg/monitoring/mocks/ChainReader.go index cec4d9d5f..609625c10 100644 --- a/pkg/monitoring/mocks/ChainReader.go +++ b/pkg/monitoring/mocks/ChainReader.go @@ -102,6 +102,30 @@ func (_m *ChainReader) GetSignaturesForAddressWithOpts(ctx context.Context, acco return r0, r1 } +// GetSlot provides a mock function with given fields: ctx +func (_m *ChainReader) GetSlot(ctx context.Context) (uint64, error) { + ret := _m.Called(ctx) + + var r0 uint64 + var r1 error + if rf, ok := ret.Get(0).(func(context.Context) (uint64, error)); ok { + return rf(ctx) + } + if rf, ok := ret.Get(0).(func(context.Context) uint64); ok { + r0 = rf(ctx) + } else { + r0 = ret.Get(0).(uint64) + } + + if rf, ok := ret.Get(1).(func(context.Context) error); ok { + r1 = rf(ctx) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GetState provides a mock function with given fields: ctx, account, commitment func (_m *ChainReader) GetState(ctx context.Context, account solana.PublicKey, commitment rpc.CommitmentType) (pkgsolana.State, uint64, error) { ret := _m.Called(ctx, account, commitment) diff --git a/pkg/monitoring/source_slotheight.go b/pkg/monitoring/source_slotheight.go new file mode 100644 index 000000000..154110116 --- /dev/null +++ b/pkg/monitoring/source_slotheight.go @@ -0,0 +1,43 @@ +package monitoring + +import ( + "context" + + commonMonitoring "github.com/smartcontractkit/chainlink-common/pkg/monitoring" + + "github.com/smartcontractkit/chainlink-solana/pkg/monitoring/types" +) + +func NewSlotHeightSourceFactory( + client ChainReader, + log commonMonitoring.Logger, +) commonMonitoring.NetworkSourceFactory { + return &slotHeightSourceFactory{ + client, + log, + } +} + +type slotHeightSourceFactory struct { + client ChainReader + log commonMonitoring.Logger +} + +func (s *slotHeightSourceFactory) NewSource( + _ commonMonitoring.ChainConfig, + _ []commonMonitoring.NodeConfig, +) (commonMonitoring.Source, error) { + return &slotHeightSource{s.client}, nil +} + +func (s *slotHeightSourceFactory) GetType() string { + return types.SlotHeightType +} + +type slotHeightSource struct { + client ChainReader +} + +func (t *slotHeightSource) Fetch(ctx context.Context) (interface{}, error) { + return t.client.GetSlot(ctx) // TODO: wrap the type to make it clear which type it is? +} diff --git a/pkg/monitoring/types/types.go b/pkg/monitoring/types/types.go new file mode 100644 index 000000000..b0a358a16 --- /dev/null +++ b/pkg/monitoring/types/types.go @@ -0,0 +1,5 @@ +package types + +const ( + SlotHeightType = "slot_height" +)