Skip to content

Commit

Permalink
Optimize: status checker
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Apr 5, 2024
1 parent a472390 commit 331a513
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 54 deletions.
1 change: 1 addition & 0 deletions internal/storage/declare.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type IDeclare interface {
storage.Table[*Declare]
Filterable[Declare, DeclareFilter]
HashByHeight
}

// DeclareFilter -
Expand Down
1 change: 1 addition & 0 deletions internal/storage/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
type IDeploy interface {
storage.Table[*Deploy]
Filterable[Deploy, DeployFilter]
HashByHeight
}

// DeployFilter -
Expand Down
1 change: 1 addition & 0 deletions internal/storage/deploy_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type IDeployAccount interface {
storage.Table[*DeployAccount]
Filterable[DeployAccount, DeployAccountFilter]
HashByHeight
}

// DeployAccountFilter -
Expand Down
4 changes: 4 additions & 0 deletions internal/storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/storage/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type IInvoke interface {
storage.Table[*Invoke]
Filterable[Invoke, InvokeFilter]
HashByHeight
}

// InvokeFilter -
Expand Down
1 change: 1 addition & 0 deletions internal/storage/l1_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
type IL1Handler interface {
storage.Table[*L1Handler]
Filterable[L1Handler, L1HandlerFilter]
HashByHeight
}

// L1HandlerFilter -
Expand Down
39 changes: 39 additions & 0 deletions internal/storage/mock/invoke.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions internal/storage/postgres/declare.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions internal/storage/postgres/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions internal/storage/postgres/deploy_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 9 additions & 0 deletions internal/storage/postgres/deploy_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
10 changes: 10 additions & 0 deletions internal/storage/postgres/invoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions internal/storage/postgres/l1_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
69 changes: 15 additions & 54 deletions pkg/indexer/status_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 331a513

Please sign in to comment.