Skip to content

Commit 2b1424c

Browse files
committed
address comment 2
1 parent 066d639 commit 2b1424c

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

api/api.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -461,13 +461,12 @@ func (api *Server) ReadContract(ctx context.Context, in *iotexapi.ReadContractRe
461461
if err := sc.LoadProto(in.Execution); err != nil {
462462
return nil, status.Error(codes.InvalidArgument, err.Error())
463463
}
464-
if d, ok := api.readCache.Get(sc.Contract() + string(sc.Data())); ok {
464+
key := hash.Hash160b(append([]byte(sc.Contract()), sc.Data()...))
465+
if d, ok := api.readCache.Get(key); ok {
465466
res := iotexapi.ReadContractResponse{}
466-
if err := proto.Unmarshal(d, &res); err != nil {
467-
log.L().Error("failed to unmarshal cached read", zap.Error(err))
468-
return nil, status.Error(codes.Internal, err.Error())
467+
if err := proto.Unmarshal(d, &res); err == nil {
468+
return &res, nil
469469
}
470-
return &res, nil
471470
}
472471

473472
if in.CallerAddress == action.EmptyAddress {
@@ -506,7 +505,7 @@ func (api *Server) ReadContract(ctx context.Context, in *iotexapi.ReadContractRe
506505
Receipt: receipt.ConvertToReceiptPb(),
507506
}
508507
if d, err := proto.Marshal(&res); err == nil {
509-
api.readCache.Put(sc.Contract()+string(sc.Data()), d)
508+
api.readCache.Put(key, d)
510509
}
511510
return &res, nil
512511
}
@@ -996,7 +995,7 @@ func (api *Server) readState(ctx context.Context, p protocol.Protocol, height st
996995
Method: methodName,
997996
Args: arguments,
998997
}
999-
if d, ok := api.readCache.Get(key.String()); ok {
998+
if d, ok := api.readCache.Get(key.Hash()); ok {
1000999
var h uint64
10011000
if height != "" {
10021001
h, _ = strconv.ParseUint(height, 0, 64)
@@ -1031,7 +1030,7 @@ func (api *Server) readState(ctx context.Context, p protocol.Protocol, height st
10311030
// old data, wrap to history state reader
10321031
d, h, err := p.ReadState(ctx, factory.NewHistoryStateReader(api.sf, rp.GetEpochHeight(inputEpochNum)), methodName, arguments...)
10331032
if err == nil {
1034-
api.readCache.Put(key.String(), d)
1033+
api.readCache.Put(key.Hash(), d)
10351034
}
10361035
return d, h, err
10371036
}
@@ -1040,7 +1039,7 @@ func (api *Server) readState(ctx context.Context, p protocol.Protocol, height st
10401039
// TODO: need to distinguish user error and system error
10411040
d, h, err := p.ReadState(ctx, api.sf, methodName, arguments...)
10421041
if err == nil {
1043-
api.readCache.Put(key.String(), d)
1042+
api.readCache.Put(key.Hash(), d)
10441043
}
10451044
return d, h, err
10461045
}

api/read_cache.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"sync"
66

7+
"github.com/iotexproject/go-pkgs/hash"
78
"go.uber.org/zap"
89

910
"github.com/iotexproject/iotex-core/blockchain/block"
@@ -23,25 +24,25 @@ type (
2324
ReadCache struct {
2425
total, hit int
2526
lock sync.RWMutex
26-
bins map[string][]byte
27+
bins map[hash.Hash160][]byte
2728
}
2829
)
2930

30-
// String returns the key as a string
31-
func (k *ReadKey) String() string {
31+
// Hash returns the hash of key's json string
32+
func (k *ReadKey) Hash() hash.Hash160 {
3233
b, _ := json.Marshal(k)
33-
return string(b)
34+
return hash.Hash160b(b)
3435
}
3536

3637
// NewReadCache returns a new read cache
3738
func NewReadCache() *ReadCache {
3839
return &ReadCache{
39-
bins: make(map[string][]byte),
40+
bins: make(map[hash.Hash160][]byte),
4041
}
4142
}
4243

4344
// Get reads according to key
44-
func (rc *ReadCache) Get(key string) ([]byte, bool) {
45+
func (rc *ReadCache) Get(key hash.Hash160) ([]byte, bool) {
4546
rc.lock.RLock()
4647
defer rc.lock.RUnlock()
4748

@@ -58,7 +59,7 @@ func (rc *ReadCache) Get(key string) ([]byte, bool) {
5859
}
5960

6061
// Put writes according to key
61-
func (rc *ReadCache) Put(key string, value []byte) {
62+
func (rc *ReadCache) Put(key hash.Hash160, value []byte) {
6263
rc.lock.Lock()
6364
rc.bins[key] = value
6465
rc.lock.Unlock()
@@ -68,7 +69,7 @@ func (rc *ReadCache) Put(key string, value []byte) {
6869
func (rc *ReadCache) Clear() {
6970
rc.lock.Lock()
7071
rc.bins = nil
71-
rc.bins = make(map[string][]byte)
72+
rc.bins = make(map[hash.Hash160][]byte)
7273
rc.lock.Unlock()
7374
}
7475

api/read_cache_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ package api
33
import (
44
"testing"
55

6+
"github.com/iotexproject/go-pkgs/hash"
67
"github.com/stretchr/testify/require"
78
)
89

910
func TestReadKey(t *testing.T) {
1011
r := require.New(t)
1112

12-
var keys []string
13+
var keys []hash.Hash160
1314
for _, v := range []ReadKey{
1415
{"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1}, []byte{2, 3, 4, 5, 6, 7, 8}}},
1516
{"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1, 2}, []byte{3, 4, 5, 6, 7, 8}}},
1617
{"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1, 2, 3}, []byte{4, 5, 6, 7, 8}}},
1718
{"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1, 2, 3, 4, 5}, []byte{6, 7, 8}}},
1819
{"staking", "10", []byte("activeBuckets"), [][]byte{[]byte{0, 1, 2, 3, 4, 5, 6, 7}, []byte{8}}},
1920
} {
20-
keys = append(keys, v.String())
21+
keys = append(keys, v.Hash())
2122
}
2223

2324
// all keys are different
@@ -34,13 +35,13 @@ func TestReadCache(t *testing.T) {
3435

3536
c := NewReadCache()
3637
rcTests := []struct {
37-
k string
38+
k hash.Hash160
3839
v []byte
3940
}{
40-
{"1", []byte{1}},
41-
{"2", []byte{2}},
42-
{"3", []byte{1}},
43-
{"4", []byte{2}},
41+
{hash.Hash160b([]byte{1}), []byte{1}},
42+
{hash.Hash160b([]byte{2}), []byte{2}},
43+
{hash.Hash160b([]byte{3}), []byte{1}},
44+
{hash.Hash160b([]byte{4}), []byte{2}},
4445
}
4546
for _, v := range rcTests {
4647
d, ok := c.Get(v.k)

0 commit comments

Comments
 (0)