Skip to content

Commit a702dfa

Browse files
author
Jiri Malek
committed
Merge branch 'development'
2 parents fab99f7 + aad6009 commit a702dfa

14 files changed

Lines changed: 149 additions & 87 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/graph-gophers/graphql-transport-ws v0.0.0-20200904065757-c681d7e1b135
2020
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
2121
github.com/karalabe/usb v0.0.0-20210518091819-4ea20957c210 // indirect
22-
github.com/klauspost/compress v1.11.12 // indirect
22+
github.com/klauspost/compress v1.13.0 // indirect
2323
github.com/kr/text v0.2.0 // indirect
2424
github.com/magiconair/properties v1.8.4 // indirect
2525
github.com/mattn/go-runewidth v0.0.10 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
375375
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
376376
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
377377
github.com/klauspost/compress v1.9.5/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
378-
github.com/klauspost/compress v1.11.12 h1:famVnQVu7QwryBN4jNseQdUKES71ZAOnB6UQQJPZvqk=
379-
github.com/klauspost/compress v1.11.12/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
378+
github.com/klauspost/compress v1.13.0 h1:2T7tUoQrQT+fQWdaY5rjWztFGAFwbGD04iPJg90ZiOs=
379+
github.com/klauspost/compress v1.13.0/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
380380
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
381381
github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg=
382382
github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type Database struct {
108108
// Cache represents the cache sub-system configuration.
109109
type Cache struct {
110110
Eviction time.Duration `mapstructure:"eviction"`
111+
MaxSize int `mapstructure:"size"`
111112
}
112113

113114
// Compiler represents the contract compilers configuration.

internal/config/default.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const (
5353
// defCacheEvictionTime holds default time for in-memory eviction periods
5454
defCacheEvictionTime = 15 * time.Minute
5555

56+
// defCacheMax size represents the default max size of the cache in MB
57+
defCacheMaxSize = 4096
58+
5659
// defSolCompilerPath represents the default SOL compiler path
5760
defSolCompilerPath = "/usr/bin/solc"
5861

@@ -108,13 +111,16 @@ func applyDefaults(cfg *viper.Viper) {
108111
cfg.SetDefault(keyLachesisUrl, defLachesisUrl)
109112
cfg.SetDefault(keyMongoUrl, defMongoUrl)
110113
cfg.SetDefault(keyMongoDatabase, defMongoDatabase)
111-
cfg.SetDefault(keyCacheEvictionTime, defCacheEvictionTime)
112114
cfg.SetDefault(keySolCompilerPath, defSolCompilerPath)
113115
cfg.SetDefault(keyApiPeers, defApiPeers)
114116
cfg.SetDefault(keyApiStateOrigin, defApiStateOrigin)
115117
cfg.SetDefault(keyErc20TokenMapFilePath, defTokenLogoFilePath)
116118
cfg.SetDefault(keyErc20Logos, defERC20Logo)
117119

120+
// in-memory cache
121+
cfg.SetDefault(keyCacheEvictionTime, defCacheEvictionTime)
122+
cfg.SetDefault(keyCacheMaxSize, defCacheMaxSize)
123+
118124
// server timeouts
119125
cfg.SetDefault(keyTimeoutRead, defReadTimeout)
120126
cfg.SetDefault(keyTimeoutWrite, defWriteTimeout)

internal/config/keys.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444

4545
// cache related options
4646
keyCacheEvictionTime = "cache.eviction"
47+
keyCacheMaxSize = "cache.size"
4748

4849
// contract validation related
4950
keySolCompilerPath = "compiler.sol"

internal/repository/cache/bridge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func cacheConfig(cfg *config.Config, log logger.Logger) bigcache.Config {
7777
// cache will not allocate more memory than this limit, value in MB
7878
// if value is reached then the oldest entries can be overridden for the new ones
7979
// 0 value means no size limit
80-
HardMaxCacheSize: 2048,
80+
HardMaxCacheSize: cfg.Cache.MaxSize,
8181

8282
// callback fired when the oldest entry is removed because of its expiration time or no space left
8383
// for the new entry, or because delete was called. A bitmask representing the reason will be returned.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Package cache implements bridge to fast in-memory object cache.
2+
package cache
3+
4+
import (
5+
"fantom-api-graphql/internal/types"
6+
"github.com/ethereum/go-ethereum/common"
7+
"github.com/ethereum/go-ethereum/common/hexutil"
8+
"strings"
9+
)
10+
11+
// delegationCacheKey generates cache key for the given delegation.
12+
func delegationCacheKey(adr common.Address, valID *hexutil.Big) string {
13+
var key strings.Builder
14+
key.WriteString("dlg")
15+
key.WriteString(adr.String())
16+
key.WriteString("to")
17+
key.WriteString(valID.String())
18+
return key.String()
19+
}
20+
21+
// PullDelegation tries to pull delegation from the given address to the given validator
22+
// from internal in-memory cache.
23+
func (b *MemBridge) PullDelegation(adr common.Address, valID *hexutil.Big) *types.Delegation {
24+
// try to get the account data from the cache
25+
data, err := b.cache.Get(delegationCacheKey(adr, valID))
26+
if err != nil {
27+
return nil
28+
}
29+
30+
// do we have the data?
31+
dlg := new(types.Delegation)
32+
if err := dlg.UnmarshalBSON(data); err != nil {
33+
b.log.Criticalf("can not decode delegation data from in-memory cache; %s", err.Error())
34+
return nil
35+
}
36+
return dlg
37+
}
38+
39+
// PushDelegation stored the given delegation in memory cache.
40+
func (b *MemBridge) PushDelegation(dlg *types.Delegation) {
41+
// no need to store nil
42+
if dlg == nil {
43+
return
44+
}
45+
46+
// encode account
47+
data, err := dlg.MarshalBSON()
48+
if err != nil {
49+
b.log.Criticalf("can not marshal delegation of %s to #%d; %s", dlg.Address.String(), dlg.ToStakerId.ToInt().Uint64(), err.Error())
50+
return
51+
}
52+
53+
// set the data to cache by block number
54+
if err := b.cache.Set(delegationCacheKey(dlg.Address, dlg.ToStakerId), data); err != nil {
55+
b.log.Criticalf("can not cache delegation of %s to #%d; %s", dlg.Address.String(), dlg.ToStakerId.ToInt().Uint64(), err.Error())
56+
}
57+
}

internal/repository/cache/transaction.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package cache
44
import (
55
"fantom-api-graphql/internal/types"
66
"github.com/ethereum/go-ethereum/common"
7+
"github.com/klauspost/compress/s2"
78
)
89

910
// PullTransaction extracts transaction information from the in-memory cache if available.
@@ -15,6 +16,12 @@ func (b *MemBridge) PullTransaction(hash *common.Hash) *types.Transaction {
1516
return nil
1617
}
1718

19+
// decode compressed transaction data from Snappy S2
20+
data, err = s2.Decode(nil, data)
21+
if err != nil {
22+
return nil
23+
}
24+
1825
// do we have the data?
1926
trx := new(types.Transaction)
2027
if err := trx.UnmarshalBSON(data); err != nil {
@@ -39,8 +46,15 @@ func (b *MemBridge) PushTransaction(trx *types.Transaction) {
3946
return
4047
}
4148

49+
// recover from s2 encoder panic
50+
defer func() {
51+
if r := recover(); r != nil {
52+
b.log.Criticalf("can not encode transaction")
53+
}
54+
}()
55+
4256
// set the data to cache by block number
43-
if err := b.cache.Set(trx.Hash.String(), data); err != nil {
57+
if err := b.cache.Set(trx.Hash.String(), s2.Encode(nil, data)); err != nil {
4458
b.log.Criticalf("can not cache transaction %s; %s", trx.Hash.String(), err.Error())
4559
}
4660
}

internal/repository/db/account.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ type AccountRow struct {
4848
ScHash *common.Hash `bson:"-"`
4949
}
5050

51+
// initAccountsCollection initializes the account collection with
52+
// indexes and additional parameters needed by the app.
53+
func (db *MongoDbBridge) initAccountsCollection() {
54+
db.log.Debugf("accounts collection initialized")
55+
}
56+
5157
// Account tries to load an account identified by the address given from
5258
// the off-chain database.
5359
func (db *MongoDbBridge) Account(addr *common.Address) (*types.Account, error) {
@@ -91,12 +97,6 @@ func (db *MongoDbBridge) Account(addr *common.Address) (*types.Account, error) {
9197
}, nil
9298
}
9399

94-
// initAccountsCollection initializes the account collection with
95-
// indexes and additional parameters needed by the app.
96-
func (db *MongoDbBridge) initAccountsCollection() {
97-
db.log.Debugf("accounts collection initialized")
98-
}
99-
100100
// AddAccount stores an account in the blockchain if not exists.
101101
func (db *MongoDbBridge) AddAccount(acc *types.Account) error {
102102
// do we have account data?

internal/repository/db/delegation.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func (db *MongoDbBridge) Delegation(addr *common.Address, valID *hexutil.Big) (*
6060
{types.FiDelegationToValidator, valID.String()},
6161
})
6262

63+
// do we have the data?
64+
if sr.Err() != nil {
65+
return nil, sr.Err()
66+
}
67+
6368
// try to decode
6469
var dlg types.Delegation
6570
if err := sr.Decode(&dlg); err != nil {

0 commit comments

Comments
 (0)