Skip to content

Commit

Permalink
add cache for accounts public key
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Jul 31, 2023
1 parent bcb779d commit 4b8e913
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
1 change: 1 addition & 0 deletions pkg/litestorage/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (s *LiteStorage) SearchAccountsByPubKey(pubKey ed25519.PublicKey) ([]tongo.
continue
}
walletAddresses = append(walletAddresses, walletAddress)
s.pubKeyByAccountID.Store(walletAddress, pubKey)
}
return walletAddresses, nil
}
30 changes: 18 additions & 12 deletions pkg/litestorage/litestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"crypto/ed25519"
"errors"
"fmt"
"github.com/tonkeeper/tongo/boc"
"math/big"
"time"

"github.com/tonkeeper/tongo/boc"

"github.com/avast/retry-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -60,7 +61,8 @@ type LiteStorage struct {
// maxGoroutines specifies a number of goroutines used to perform some time-consuming operations.
maxGoroutines int
// trackingAccounts is a list of accounts we track. Defined with ACCOUNTS env variable.
trackingAccounts map[tongo.AccountID]struct{}
trackingAccounts map[tongo.AccountID]struct{}
pubKeyByAccountID *xsync.MapOf[tongo.AccountID, ed25519.PublicKey]
}

type Options struct {
Expand Down Expand Up @@ -144,6 +146,7 @@ func NewLiteStorage(log *zap.Logger, opts ...Option) (*LiteStorage, error) {
transactionsByInMsgLT: xsync.NewTypedMapOf[inMsgCreatedLT, tongo.Bits256](hashInMsgCreatedLT),
blockCache: xsync.NewTypedMapOf[tongo.BlockIDExt, *tlb.Block](hashBlockIDExt),
accountInterfacesCache: xsync.NewTypedMapOf[tongo.AccountID, []abi.ContractInterface](hashAccountID),
pubKeyByAccountID: xsync.NewTypedMapOf[tongo.AccountID, ed25519.PublicKey](hashAccountID),
}
storage.knownAccounts["tf_pools"] = o.tfPools
storage.knownAccounts["jettons"] = o.jettons
Expand Down Expand Up @@ -447,18 +450,21 @@ func (s *LiteStorage) GetWalletPubKey(ctx context.Context, address tongo.Account
}))
defer timer.ObserveDuration()
_, result, err := abi.GetPublicKey(ctx, s.client, address)
if err != nil {
return nil, err
}
if r, ok := result.(abi.GetPublicKeyResult); ok {
i := big.Int(r.PublicKey)
b := i.Bytes()
if len(b) < 24 || len(b) > 32 {
return nil, fmt.Errorf("invalid publock key")
if err == nil {
if r, ok := result.(abi.GetPublicKeyResult); ok {
i := big.Int(r.PublicKey)
b := i.Bytes()
if len(b) < 24 || len(b) > 32 {
return nil, fmt.Errorf("invalid public key")
}
return append(make([]byte, 32-len(b)), b...), nil
}
return append(make([]byte, 32-len(b)), b...), nil
}
return nil, fmt.Errorf("can't get publick key")
pubKey, ok := s.pubKeyByAccountID.Load(address)
if ok {
return pubKey, nil
}
return nil, fmt.Errorf("can't get public key")
}

func (s *LiteStorage) ReindexAccount(ctx context.Context, accountID tongo.AccountID) error {
Expand Down

0 comments on commit 4b8e913

Please sign in to comment.