Skip to content
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

fix(store): use cache to check if public key exists #902

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func defaultConfig() *Config {

func DefaultConfigMainnet(genParams *param.Params) *Config {
conf := defaultConfig()
// TO BE DEFINED

// Store private configs
conf.Store.TxCacheSize = genParams.TransactionToLiveInterval
Expand Down
22 changes: 11 additions & 11 deletions store/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

type accountStore struct {
db *leveldb.DB
accInCache *lru.Cache[crypto.Address, *account.Account]
total int32
db *leveldb.DB
accCache *lru.Cache[crypto.Address, *account.Account]
total int32
}

func accountKey(addr crypto.Address) []byte { return append(accountPrefix, addr.Bytes()...) }
Expand All @@ -32,22 +32,22 @@ func newAccountStore(db *leveldb.DB, cacheSize int) *accountStore {
iter.Release()

return &accountStore{
db: db,
total: total,
accInCache: addrLruCache,
db: db,
total: total,
accCache: addrLruCache,
}
}

func (as *accountStore) hasAccount(addr crypto.Address) bool {
ok := as.accInCache.Contains(addr)
ok := as.accCache.Contains(addr)
if !ok {
ok = tryHas(as.db, accountKey(addr))
}
return ok
}

func (as *accountStore) account(addr crypto.Address) (*account.Account, error) {
acc, ok := as.accInCache.Get(addr)
acc, ok := as.accCache.Get(addr)
if ok {
return acc.Clone(), nil
}
Expand All @@ -62,8 +62,8 @@ func (as *accountStore) account(addr crypto.Address) (*account.Account, error) {
return nil, err
}

as.accInCache.Add(addr, acc.Clone())
return acc, nil
as.accCache.Add(addr, acc)
return acc.Clone(), nil
}

func (as *accountStore) iterateAccounts(consumer func(crypto.Address, *account.Account) (stop bool)) {
Expand Down Expand Up @@ -100,7 +100,7 @@ func (as *accountStore) updateAccount(batch *leveldb.Batch, addr crypto.Address,
if !as.hasAccount(addr) {
as.total++
}
as.accInCache.Add(addr, acc)
as.accCache.Add(addr, acc)

batch.Put(accountKey(addr), data)
}
5 changes: 1 addition & 4 deletions store/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ func TestAccountDeepCopy(t *testing.T) {

acc2, _ := td.store.Account(addr)
acc2.AddToBalance(1)
accCache, _ := td.store.accountStore.accInCache.Get(addr)
accCache, _ := td.store.accountStore.accCache.Get(addr)
assert.NotEqual(t, accCache.Hash(), acc2.Hash())

expectedAcc, _ := td.store.accountStore.accInCache.Get(addr)
assert.NotEqual(t, expectedAcc.Hash(), acc2.Hash())
}
36 changes: 33 additions & 3 deletions store/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import (
"bytes"

lru "github.com/hashicorp/golang-lru/v2"
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/sortition"
"github.com/pactus-project/pactus/types/block"
Expand All @@ -25,14 +27,21 @@

type blockStore struct {
db *leveldb.DB
pubKeyCache *lru.Cache[crypto.Address, *bls.PublicKey]
sortitionSeedCache *pairslice.PairSlice[uint32, *sortition.VerifiableSeed]
sortitionCacheSize uint32
}

func newBlockStore(db *leveldb.DB, sortitionCacheSize uint32) *blockStore {
func newBlockStore(db *leveldb.DB, sortitionCacheSize uint32, publicKeyCacheSize int) *blockStore {
pubKeyCache, err := lru.New[crypto.Address, *bls.PublicKey](publicKeyCacheSize)
if err != nil {
return nil

Check warning on line 38 in store/block.go

View check run for this annotation

Codecov / codecov/patch

store/block.go#L38

Added line #L38 was not covered by tests
}

return &blockStore{
db: db,
sortitionSeedCache: pairslice.New[uint32, *sortition.VerifiableSeed](int(sortitionCacheSize)),
pubKeyCache: pubKeyCache,
sortitionCacheSize: sortitionCacheSize,
}
}
Expand Down Expand Up @@ -75,7 +84,6 @@

pubKey := trx.PublicKey()
if pubKey != nil {
// TODO: improve my performance by caching public keys
if !bs.hasPublicKey(trx.Payload().Signer()) {
publicKeyKey := publicKeyKey(trx.Payload().Signer())
batch.Put(publicKeyKey, pubKey.Bytes())
Expand Down Expand Up @@ -142,8 +150,30 @@
return tryHas(bs.db, blockKey(height))
}

func (bs *blockStore) publicKey(addr crypto.Address) (*bls.PublicKey, error) {
if pubKey, ok := bs.pubKeyCache.Get(addr); ok {
return pubKey, nil
}

data, err := tryGet(bs.db, publicKeyKey(addr))
if err != nil {
return nil, err
}
pubKey, err := bls.PublicKeyFromBytes(data)
if err != nil {
return nil, err

Check warning on line 164 in store/block.go

View check run for this annotation

Codecov / codecov/patch

store/block.go#L164

Added line #L164 was not covered by tests
}

bs.pubKeyCache.Add(addr, pubKey)
return pubKey, err
}

func (bs *blockStore) hasPublicKey(addr crypto.Address) bool {
return tryHas(bs.db, publicKeyKey(addr))
ok := bs.pubKeyCache.Contains(addr)
if !ok {
ok = tryHas(bs.db, publicKeyKey(addr))
}
return ok
}

func (bs *blockStore) saveToCache(blockHeight uint32, sortitionSeed sortition.VerifiableSeed) {
Expand Down
19 changes: 15 additions & 4 deletions store/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"os"

"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/util/errors"
)

type Config struct {
Expand All @@ -23,8 +22,8 @@
Path: "data",
TxCacheSize: 0,
SortitionCacheSize: 0,
AccountCacheSize: 0,
PublicKeyCacheSize: 0,
AccountCacheSize: 1024,
PublicKeyCacheSize: 1024,
}
}

Expand All @@ -39,7 +38,19 @@
// BasicCheck performs basic checks on the configuration.
func (conf *Config) BasicCheck() error {
if !util.IsValidDirPath(conf.Path) {
return errors.Errorf(errors.ErrInvalidConfig, "path is not valid")
return InvalidConfigError{
Reason: "path is not valid",

Check warning on line 42 in store/config.go

View check run for this annotation

Codecov / codecov/patch

store/config.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}
}

if conf.TxCacheSize == 0 ||
conf.SortitionCacheSize == 0 ||
conf.AccountCacheSize == 0 ||
conf.PublicKeyCacheSize == 0 {
return InvalidConfigError{
Reason: "cache size set to zero",
}
}

return nil
}
17 changes: 12 additions & 5 deletions store/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ import (
)

func TestDefaultConfigCheck(t *testing.T) {
c := DefaultConfig()
assert.NoError(t, c.BasicCheck())
conf := DefaultConfig()

err := conf.BasicCheck()
assert.ErrorIs(t, InvalidConfigError{"cache size set to zero"}, err)

conf.TxCacheSize = 1
conf.SortitionCacheSize = 1
err = conf.BasicCheck()
assert.NoError(t, err)

if runtime.GOOS != "windows" {
c.Path = util.TempDirPath()
assert.NoError(t, c.BasicCheck())
assert.Equal(t, c.StorePath(), c.Path+"/store.db")
conf.Path = util.TempDirPath()
assert.NoError(t, conf.BasicCheck())
assert.Equal(t, conf.StorePath(), conf.Path+"/store.db")
}
}
9 changes: 9 additions & 0 deletions store/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
"github.com/pactus-project/pactus/crypto"
)

// InvalidConfigError is returned when the store configuration is invalid.
type InvalidConfigError struct {
Reason string
}

func (e InvalidConfigError) Error() string {
return e.Reason

Check warning on line 15 in store/errors.go

View check run for this annotation

Codecov / codecov/patch

store/errors.go#L15

Added line #L15 was not covered by tests
}

// PublicKeyNotFoundError is returned when the public key associated with an address
// is not found in the store.
type PublicKeyNotFoundError struct {
Expand Down
29 changes: 5 additions & 24 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"sync"

lru "github.com/hashicorp/golang-lru/v2"
"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/crypto/hash"
Expand Down Expand Up @@ -38,7 +37,7 @@ var (
accountPrefix = []byte{0x05}
validatorPrefix = []byte{0x07}
blockHeightPrefix = []byte{0x09}
publicKeyPrefix = []byte{0x0a}
publicKeyPrefix = []byte{0x0a} // TODO: make me 0xb on Mainnet
)

func tryGet(db *leveldb.DB, key []byte) ([]byte, error) {
Expand All @@ -64,7 +63,6 @@ type store struct {
lk sync.RWMutex

config *Config
pubKeyCache *lru.Cache[crypto.Address, *bls.PublicKey]
db *leveldb.DB
batch *leveldb.Batch
blockStore *blockStore
Expand All @@ -79,21 +77,15 @@ func NewStore(conf *Config) (Store, error) {
Compression: opt.NoCompression,
}

pubKeyCache, err := lru.New[crypto.Address, *bls.PublicKey](conf.PublicKeyCacheSize)
if err != nil {
return nil, err
}

db, err := leveldb.OpenFile(conf.StorePath(), options)
if err != nil {
return nil, err
}
s := &store{
config: conf,
db: db,
pubKeyCache: pubKeyCache,
batch: new(leveldb.Batch),
blockStore: newBlockStore(db, conf.SortitionCacheSize),
blockStore: newBlockStore(db, conf.SortitionCacheSize, conf.PublicKeyCacheSize),
txStore: newTxStore(db, conf.TxCacheSize),
accountStore: newAccountStore(db, conf.AccountCacheSize),
validatorStore: newValidatorStore(db),
Expand Down Expand Up @@ -212,21 +204,10 @@ func (s *store) SortitionSeed(blockHeight uint32) *sortition.VerifiableSeed {
}

func (s *store) PublicKey(addr crypto.Address) (*bls.PublicKey, error) {
if pubKey, ok := s.pubKeyCache.Get(addr); ok {
return pubKey, nil
}

bs, err := tryGet(s.db, publicKeyKey(addr))
if err != nil {
return nil, err
}
pubKey, err := bls.PublicKeyFromBytes(bs)
if err != nil {
return nil, err
}
s.lk.RLock()
defer s.lk.RUnlock()

s.pubKeyCache.Add(addr, pubKey)
return pubKey, err
return s.blockStore.publicKey(addr)
}

func (s *store) Transaction(id tx.ID) (*CommittedTx, error) {
Expand Down
4 changes: 2 additions & 2 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestIndexingPublicKeys(t *testing.T) {
for _, trx := range blk.Transactions() {
addr := trx.Payload().Signer()
pub, found := td.store.PublicKey(addr)
pubKeyLruCache, ok := td.store.pubKeyCache.Get(addr)
pubKeyLruCache, ok := td.store.blockStore.pubKeyCache.Get(addr)

assert.NoError(t, found)
assert.True(t, ok)
Expand All @@ -136,7 +136,7 @@ func TestIndexingPublicKeys(t *testing.T) {

randValAddress := td.RandValAddress()
pub, found := td.store.PublicKey(randValAddress)
pubKeyLruCache, ok := td.store.pubKeyCache.Get(randValAddress)
pubKeyLruCache, ok := td.store.blockStore.pubKeyCache.Get(randValAddress)

assert.Error(t, found)
assert.Nil(t, pub)
Expand Down
Loading