From 331a513c8570bef228efd258a3b01fe593b7a9b1 Mon Sep 17 00:00:00 2001 From: Artem Date: Fri, 5 Apr 2024 17:03:59 +0200 Subject: [PATCH] Optimize: status checker --- internal/storage/declare.go | 1 + internal/storage/deploy.go | 1 + internal/storage/deploy_account.go | 1 + internal/storage/interface.go | 4 ++ internal/storage/invoke.go | 1 + internal/storage/l1_handler.go | 1 + internal/storage/mock/invoke.go | 39 +++++++++++ internal/storage/postgres/declare.go | 10 +++ internal/storage/postgres/deploy.go | 10 +++ internal/storage/postgres/deploy_account.go | 10 +++ .../storage/postgres/deploy_account_test.go | 9 +++ internal/storage/postgres/invoke.go | 10 +++ internal/storage/postgres/l1_handler.go | 10 +++ pkg/indexer/status_checker.go | 69 ++++--------------- 14 files changed, 122 insertions(+), 54 deletions(-) diff --git a/internal/storage/declare.go b/internal/storage/declare.go index 0c424bb..fe0db3f 100644 --- a/internal/storage/declare.go +++ b/internal/storage/declare.go @@ -12,6 +12,7 @@ import ( type IDeclare interface { storage.Table[*Declare] Filterable[Declare, DeclareFilter] + HashByHeight } // DeclareFilter - diff --git a/internal/storage/deploy.go b/internal/storage/deploy.go index 08edf7c..5967c6c 100644 --- a/internal/storage/deploy.go +++ b/internal/storage/deploy.go @@ -13,6 +13,7 @@ import ( type IDeploy interface { storage.Table[*Deploy] Filterable[Deploy, DeployFilter] + HashByHeight } // DeployFilter - diff --git a/internal/storage/deploy_account.go b/internal/storage/deploy_account.go index 403e91b..38b2458 100644 --- a/internal/storage/deploy_account.go +++ b/internal/storage/deploy_account.go @@ -14,6 +14,7 @@ import ( type IDeployAccount interface { storage.Table[*DeployAccount] Filterable[DeployAccount, DeployAccountFilter] + HashByHeight } // DeployAccountFilter - diff --git a/internal/storage/interface.go b/internal/storage/interface.go index 39c127a..bd98707 100644 --- a/internal/storage/interface.go +++ b/internal/storage/interface.go @@ -25,6 +25,10 @@ type Heightable interface { GetId() uint64 } +type HashByHeight interface { + HashByHeight(ctx context.Context, height uint64) ([]byte, error) +} + // Filterable - type Filterable[M storage.Model, F any] interface { Filter(ctx context.Context, flt []F, opts ...FilterOption) ([]M, error) diff --git a/internal/storage/invoke.go b/internal/storage/invoke.go index 455df55..8b332d0 100644 --- a/internal/storage/invoke.go +++ b/internal/storage/invoke.go @@ -14,6 +14,7 @@ import ( type IInvoke interface { storage.Table[*Invoke] Filterable[Invoke, InvokeFilter] + HashByHeight } // InvokeFilter - diff --git a/internal/storage/l1_handler.go b/internal/storage/l1_handler.go index d2121ad..d79aab4 100644 --- a/internal/storage/l1_handler.go +++ b/internal/storage/l1_handler.go @@ -14,6 +14,7 @@ import ( type IL1Handler interface { storage.Table[*L1Handler] Filterable[L1Handler, L1HandlerFilter] + HashByHeight } // L1HandlerFilter - diff --git a/internal/storage/mock/invoke.go b/internal/storage/mock/invoke.go index ebd93ae..55dccf4 100644 --- a/internal/storage/mock/invoke.go +++ b/internal/storage/mock/invoke.go @@ -162,6 +162,45 @@ func (c *IInvokeGetByIDCall) DoAndReturn(f func(context.Context, uint64) (*stora return c } +// HashByHeight mocks base method. +func (m *MockIInvoke) HashByHeight(ctx context.Context, height uint64) ([]byte, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "HashByHeight", ctx, height) + ret0, _ := ret[0].([]byte) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// HashByHeight indicates an expected call of HashByHeight. +func (mr *MockIInvokeMockRecorder) HashByHeight(ctx, height any) *IInvokeHashByHeightCall { + mr.mock.ctrl.T.Helper() + call := mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HashByHeight", reflect.TypeOf((*MockIInvoke)(nil).HashByHeight), ctx, height) + return &IInvokeHashByHeightCall{Call: call} +} + +// IInvokeHashByHeightCall wrap *gomock.Call +type IInvokeHashByHeightCall struct { + *gomock.Call +} + +// Return rewrite *gomock.Call.Return +func (c *IInvokeHashByHeightCall) Return(arg0 []byte, arg1 error) *IInvokeHashByHeightCall { + c.Call = c.Call.Return(arg0, arg1) + return c +} + +// Do rewrite *gomock.Call.Do +func (c *IInvokeHashByHeightCall) Do(f func(context.Context, uint64) ([]byte, error)) *IInvokeHashByHeightCall { + c.Call = c.Call.Do(f) + return c +} + +// DoAndReturn rewrite *gomock.Call.DoAndReturn +func (c *IInvokeHashByHeightCall) DoAndReturn(f func(context.Context, uint64) ([]byte, error)) *IInvokeHashByHeightCall { + c.Call = c.Call.DoAndReturn(f) + return c +} + // IsNoRows mocks base method. func (m *MockIInvoke) IsNoRows(err error) bool { m.ctrl.T.Helper() diff --git a/internal/storage/postgres/declare.go b/internal/storage/postgres/declare.go index 047531f..d6a5116 100644 --- a/internal/storage/postgres/declare.go +++ b/internal/storage/postgres/declare.go @@ -43,3 +43,13 @@ func (d *Declare) Filter(ctx context.Context, fltr []storage.DeclareFilter, opts err = query.Scan(ctx) return } + +func (d *Declare) HashByHeight(ctx context.Context, height uint64) (hash []byte, err error) { + err = d.DB().NewSelect(). + Model((*storage.Declare)(nil)). + Column("hash"). + Where("height = ?", height). + Limit(1). + Scan(ctx, &hash) + return +} diff --git a/internal/storage/postgres/deploy.go b/internal/storage/postgres/deploy.go index c6dcb9a..e509a9d 100644 --- a/internal/storage/postgres/deploy.go +++ b/internal/storage/postgres/deploy.go @@ -44,3 +44,13 @@ func (d *Deploy) Filter(ctx context.Context, fltr []storage.DeployFilter, opts . err = query.Scan(ctx) return } + +func (d *Deploy) HashByHeight(ctx context.Context, height uint64) (hash []byte, err error) { + err = d.DB().NewSelect(). + Model((*storage.Deploy)(nil)). + Column("hash"). + Where("height = ?", height). + Limit(1). + Scan(ctx, &hash) + return +} diff --git a/internal/storage/postgres/deploy_account.go b/internal/storage/postgres/deploy_account.go index 9e9fb38..365cd6f 100644 --- a/internal/storage/postgres/deploy_account.go +++ b/internal/storage/postgres/deploy_account.go @@ -44,3 +44,13 @@ func (d *DeployAccount) Filter(ctx context.Context, fltr []storage.DeployAccount err = query.Scan(ctx) return } + +func (d *DeployAccount) HashByHeight(ctx context.Context, height uint64) (hash []byte, err error) { + err = d.DB().NewSelect(). + Model((*storage.DeployAccount)(nil)). + Column("hash"). + Where("height = ?", height). + Limit(1). + Scan(ctx, &hash) + return +} diff --git a/internal/storage/postgres/deploy_account_test.go b/internal/storage/postgres/deploy_account_test.go index 7316098..500ff4e 100644 --- a/internal/storage/postgres/deploy_account_test.go +++ b/internal/storage/postgres/deploy_account_test.go @@ -176,6 +176,15 @@ func (s *DeployAccountTestSuite) TestFilterByParsedCalldata() { s.Require().Len(deploys, 3) } +func (s *DeployAccountTestSuite) TestHashByHeight() { + ctx, ctxCancel := context.WithTimeout(context.Background(), 5*time.Second) + defer ctxCancel() + + hash, err := s.storage.DeployAccount.HashByHeight(ctx, 154958) + s.Require().NoError(err) + s.Require().Len(hash, 32) +} + func TestSuiteDeployAccount_Run(t *testing.T) { suite.Run(t, new(DeployAccountTestSuite)) } diff --git a/internal/storage/postgres/invoke.go b/internal/storage/postgres/invoke.go index 3847fe8..c6bfbd7 100644 --- a/internal/storage/postgres/invoke.go +++ b/internal/storage/postgres/invoke.go @@ -50,3 +50,13 @@ func (invoke *Invoke) Filter(ctx context.Context, fltr []storage.InvokeFilter, o Scan(ctx, &result) return result, err } + +func (invoke *Invoke) HashByHeight(ctx context.Context, height uint64) (hash []byte, err error) { + err = invoke.DB().NewSelect(). + Model((*storage.Invoke)(nil)). + Column("hash"). + Where("height = ?", height). + Limit(1). + Scan(ctx, &hash) + return +} diff --git a/internal/storage/postgres/l1_handler.go b/internal/storage/postgres/l1_handler.go index e4d5d46..7862c71 100644 --- a/internal/storage/postgres/l1_handler.go +++ b/internal/storage/postgres/l1_handler.go @@ -47,3 +47,13 @@ func (l1 *L1Handler) Filter(ctx context.Context, fltr []storage.L1HandlerFilter, err = query.Scan(ctx) return result, err } + +func (l1 *L1Handler) HashByHeight(ctx context.Context, height uint64) (hash []byte, err error) { + err = l1.DB().NewSelect(). + Model((*storage.L1Handler)(nil)). + Column("hash"). + Where("height = ?", height). + Limit(1). + Scan(ctx, &hash) + return +} diff --git a/pkg/indexer/status_checker.go b/pkg/indexer/status_checker.go index 07ff31b..d5ceae8 100644 --- a/pkg/indexer/status_checker.go +++ b/pkg/indexer/status_checker.go @@ -10,15 +10,10 @@ import ( "github.com/dipdup-io/starknet-indexer/pkg/indexer/receiver" "github.com/dipdup-io/workerpool" sdk "github.com/dipdup-net/indexer-sdk/pkg/storage" - "github.com/pkg/errors" "github.com/rs/zerolog" "github.com/rs/zerolog/log" ) -var ( - errCantFindTransactionsInBlock = errors.New("ca't find transactions in block") -) - type acceptedOnL2 struct { Height uint64 TransactionHash []byte @@ -158,93 +153,59 @@ func (checker *statusChecker) init(ctx context.Context) error { return nil } -func byHeight[T sdk.Model, F any](ctx context.Context, src storage.Filterable[T, F], fltr F) (t T, err error) { - tx, err := src.Filter(ctx, []F{fltr}, storage.WithLimitFilter(1), storage.WithMultiSort("time desc", "id desc")) - if err != nil { - return t, err - } - if len(tx) == 0 { - return t, errors.Wrapf( - errCantFindTransactionsInBlock, - "model = %s", t.TableName()) - } - - return tx[0], nil -} - func (checker *statusChecker) addIndexedBlockToQueue(ctx context.Context, block storage.Block) error { if block.InvokeCount > 0 { - tx, err := byHeight[storage.Invoke, storage.InvokeFilter](ctx, checker.invoke, storage.InvokeFilter{ - Height: storage.IntegerFilter{ - Eq: block.Height, - }, - }) + hash, err := checker.invoke.HashByHeight(ctx, block.Height) if err != nil { return err } checker.acceptedOnL2.Push(acceptedOnL2{ - TransactionHash: tx.Hash, - Height: tx.Height, + TransactionHash: hash, + Height: block.Height, }) return nil } if block.DeployCount > 0 { - tx, err := byHeight[storage.Deploy, storage.DeployFilter](ctx, checker.deploys, storage.DeployFilter{ - Height: storage.IntegerFilter{ - Eq: block.Height, - }, - }) + hash, err := checker.deploys.HashByHeight(ctx, block.Height) if err != nil { return err } checker.acceptedOnL2.Push(acceptedOnL2{ - TransactionHash: tx.Hash, - Height: tx.Height, + TransactionHash: hash, + Height: block.Height, }) return nil } if block.DeployAccountCount > 0 { - tx, err := byHeight[storage.DeployAccount, storage.DeployAccountFilter](ctx, checker.deployAccounts, storage.DeployAccountFilter{ - Height: storage.IntegerFilter{ - Eq: block.Height, - }, - }) + hash, err := checker.deployAccounts.HashByHeight(ctx, block.Height) if err != nil { return err } checker.acceptedOnL2.Push(acceptedOnL2{ - TransactionHash: tx.Hash, - Height: tx.Height, + TransactionHash: hash, + Height: block.Height, }) return nil } if block.DeclareCount > 0 { - tx, err := byHeight[storage.Declare, storage.DeclareFilter](ctx, checker.declares, storage.DeclareFilter{ - Height: storage.IntegerFilter{ - Eq: block.Height, - }, - }) + hash, err := checker.declares.HashByHeight(ctx, block.Height) if err != nil { return err } checker.acceptedOnL2.Push(acceptedOnL2{ - TransactionHash: tx.Hash, - Height: tx.Height, + TransactionHash: hash, + Height: block.Height, }) return nil } if block.L1HandlerCount > 0 { - tx, err := byHeight[storage.L1Handler, storage.L1HandlerFilter](ctx, checker.l1Handlers, storage.L1HandlerFilter{ - Height: storage.IntegerFilter{ - Eq: block.Height, - }, - }) + hash, err := checker.l1Handlers.HashByHeight(ctx, block.Height) if err != nil { return err } checker.acceptedOnL2.Push(acceptedOnL2{ - TransactionHash: tx.Hash, - Height: tx.Height, + TransactionHash: hash, + Height: block.Height, }) return nil }