Skip to content

Commit

Permalink
Merge branch 'master' into deser
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Mar 15, 2022
2 parents 7666c36 + dbac57f commit 6e44957
Show file tree
Hide file tree
Showing 45 changed files with 1,072 additions and 497 deletions.
10 changes: 2 additions & 8 deletions action/protocol/execution/evm/evmstatedbadapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,8 @@ func initMockStateManager(ctrl *gomock.Controller) (*mock_chainmanager.MockState
cb.Delete(ns, cfg.Key, "failed to delete state")
return 0, nil
}).AnyTimes()
sm.EXPECT().Snapshot().DoAndReturn(
func() int {
return cb.Snapshot()
}).AnyTimes()
sm.EXPECT().Revert(gomock.Any()).DoAndReturn(
func(snapshot int) error {
return cb.Revert(snapshot)
}).AnyTimes()
sm.EXPECT().Snapshot().DoAndReturn(cb.Snapshot).AnyTimes()
sm.EXPECT().Revert(gomock.Any()).DoAndReturn(cb.RevertSnapshot).AnyTimes()
return sm, nil
}

Expand Down
13 changes: 11 additions & 2 deletions db/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var (
ErrNotExist = errors.New("not exist in DB")
// ErrOutOfBound indicates an out of bound error
ErrOutOfBound = errors.New("out of bound")
// ErrUnexpectedType indicates an invalid casting
ErrUnexpectedType = errors.New("unexpected type")
)

type (
Expand Down Expand Up @@ -61,11 +63,18 @@ type (
// A local cache is added to provide fast retrieval of pending Put/Delete entries
CachedBatch interface {
KVStoreBatch
Snapshot
// Get gets a record by (namespace, key)
Get(string, []byte) ([]byte, error)
}

// Snapshot defines an interface which supports snapshot related functions
Snapshot interface {
// Snapshot takes a snapshot of current cached batch
Snapshot() int
// Revert sets the cached batch to the state at the given snapshot
Revert(int) error
// RevertSnapshot sets the cached batch to the state at the given snapshot
RevertSnapshot(int) error
// ResetSnapshots() clears all snapshots
ResetSnapshots()
}
)
36 changes: 28 additions & 8 deletions db/batch/batch_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,12 @@ func (b *baseKVStoreBatch) truncate(size int) {

// NewCachedBatch returns a new cached batch buffer
func NewCachedBatch() CachedBatch {
return &cachedBatch{
cb := &cachedBatch{
kvStoreBatch: newBaseKVStoreBatch(),
batchShots: make([]int, 0),
caches: []KVStoreCache{NewKVCache()},
keyTags: map[hash.Hash160][]int{},
tagKeys: [][]hash.Hash160{{}},
}
cb.clear()

return cb
}

func (cb *cachedBatch) Translate(wit WriteInfoTranslate) KVStoreBatch {
Expand Down Expand Up @@ -235,7 +234,6 @@ func (cb *cachedBatch) currentCache() KVStoreCache {

func (cb *cachedBatch) clear() {
cb.kvStoreBatch.Clear()
// clear all saved snapshots
cb.tag = 0
cb.batchShots = nil
cb.batchShots = make([]int, 0)
Expand Down Expand Up @@ -315,8 +313,7 @@ func (cb *cachedBatch) Snapshot() int {
return cb.tag
}

// Revert sets the cached batch to the state at the given snapshot
func (cb *cachedBatch) Revert(snapshot int) error {
func (cb *cachedBatch) RevertSnapshot(snapshot int) error {
cb.lock.Lock()
defer cb.lock.Unlock()
// throw error if the snapshot number does not exist
Expand All @@ -342,6 +339,29 @@ func (cb *cachedBatch) Revert(snapshot int) error {
return nil
}

func (cb *cachedBatch) ResetSnapshots() {
cb.lock.Lock()
defer cb.lock.Unlock()

cb.tag = 0
cb.batchShots = nil
cb.batchShots = make([]int, 0)
if len(cb.caches) > 1 {
if err := cb.caches[0].Append(cb.caches[1:]...); err != nil {
panic(errors.Wrap(err, "failed to reset snapshots"))
}
cb.caches = cb.caches[:1]
}
keys := make([]hash.Hash160, 0, len(cb.keyTags))
for key := range cb.keyTags {
keys = append(keys, key)
}
for _, key := range keys {
cb.keyTags[key] = []int{0}
}
cb.tagKeys = [][]hash.Hash160{keys}
}

func (cb *cachedBatch) CheckFillPercent(ns string) (float64, bool) {
return cb.kvStoreBatch.CheckFillPercent(ns)
}
Expand Down
22 changes: 11 additions & 11 deletions db/batch/batch_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ func TestCachedBatch(t *testing.T) {
return wi.WriteType() == Delete
})))
require.Equal(3, cb.Size())
require.Error(cb.Revert(-1))
require.Error(cb.Revert(si + 1))
require.NoError(cb.Revert(si))
require.Error(cb.RevertSnapshot(-1))
require.Error(cb.RevertSnapshot(si + 1))
require.NoError(cb.RevertSnapshot(si))
require.Equal(1, cb.Size())
require.True(bytes.Equal([]byte{}, cb.Translate(func(wi *WriteInfo) *WriteInfo {
if wi.WriteType() != Delete {
Expand Down Expand Up @@ -173,9 +173,9 @@ func TestSnapshot(t *testing.T) {
require.Equal(8, cb.Size())

// snapshot 2
require.Error(cb.Revert(3))
require.Error(cb.Revert(-1))
require.NoError(cb.Revert(2))
require.Error(cb.RevertSnapshot(3))
require.Error(cb.RevertSnapshot(-1))
require.NoError(cb.RevertSnapshot(2))
_, err = cb.Get(bucket1, testK2[0])
require.Equal(ErrAlreadyDeleted, err)
v, err = cb.Get(bucket1, testK1[1])
Expand All @@ -196,11 +196,11 @@ func TestSnapshot(t *testing.T) {
require.Equal(testV2[1], v)

// snapshot 1
require.NoError(cb.Revert(2))
require.NoError(cb.RevertSnapshot(2))
v, err = cb.Get(bucket1, testK2[2])
require.NoError(err)
require.Equal(testV2[2], v)
require.NoError(cb.Revert(1))
require.NoError(cb.RevertSnapshot(1))
_, err = cb.Get(bucket1, testK1[0])
require.Equal(ErrAlreadyDeleted, err)
v, err = cb.Get(bucket1, testK1[1])
Expand All @@ -216,8 +216,8 @@ func TestSnapshot(t *testing.T) {
require.Equal(ErrNotExist, err)

// snapshot 0
require.Error(cb.Revert(2))
require.NoError(cb.Revert(0))
require.Error(cb.RevertSnapshot(2))
require.NoError(cb.RevertSnapshot(0))
v, err = cb.Get(bucket1, testK1[0])
require.NoError(err)
require.Equal(testV1[0], v)
Expand Down Expand Up @@ -269,7 +269,7 @@ func BenchmarkCachedBatch_Snapshot(b *testing.B) {
cb.Delete(bucket1, k[:], "")
_, err = cb.Get(bucket1, k[:])
require.Error(b, err)
cb.Revert(sn)
cb.RevertSnapshot(sn)
value, err = cb.Get(bucket1, k[:])
require.NoError(b, err)
require.True(b, bytes.Equal(value, v[:]))
Expand Down
29 changes: 27 additions & 2 deletions db/batch/kv_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type (
Evict(hash.Hash160)
// Clear clear the cache
Clear()
// Append appends caches
Append(...KVStoreCache) error
}

// kvCache implements KVStoreCache interface
Expand Down Expand Up @@ -54,6 +56,10 @@ func (c *kvCache) Read(k hash.Hash160) ([]byte, error) {

// Write puts a record into cache
func (c *kvCache) Write(k hash.Hash160, v []byte) {
c.write(k, v)
}

func (c *kvCache) write(k hash.Hash160, v []byte) {
c.cache[k] = v
delete(c.deleted, k)
}
Expand All @@ -63,13 +69,16 @@ func (c *kvCache) WriteIfNotExist(k hash.Hash160, v []byte) error {
if _, ok := c.cache[k]; ok {
return ErrAlreadyExist
}
c.cache[k] = v
delete(c.deleted, k)
c.write(k, v)
return nil
}

// Evict deletes a record from cache
func (c *kvCache) Evict(k hash.Hash160) {
c.evict(k)
}

func (c *kvCache) evict(k hash.Hash160) {
delete(c.cache, k)
c.deleted[k] = struct{}{}
}
Expand All @@ -81,3 +90,19 @@ func (c *kvCache) Clear() {
c.cache = make(map[hash.Hash160][]byte)
c.deleted = make(map[hash.Hash160]struct{})
}

func (c *kvCache) Append(caches ...KVStoreCache) error {
for _, cc := range caches {
cc, ok := cc.(*kvCache)
if !ok {
return ErrUnexpectedType
}
for key, value := range cc.cache {
c.write(key, value)
}
for key := range cc.deleted {
c.evict(key)
}
}
return nil
}
11 changes: 7 additions & 4 deletions db/kvstorewithbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (

type (
withBuffer interface {
Snapshot() int
Revert(int) error
batch.Snapshot
SerializeQueue(batch.WriteInfoSerialize, batch.WriteInfoFilter) []byte
MustPut(string, []byte, []byte)
MustDelete(string, []byte)
Expand Down Expand Up @@ -141,8 +140,12 @@ func (kvb *kvStoreWithBuffer) Snapshot() int {
return kvb.buffer.Snapshot()
}

func (kvb *kvStoreWithBuffer) Revert(sid int) error {
return kvb.buffer.Revert(sid)
func (kvb *kvStoreWithBuffer) RevertSnapshot(sid int) error {
return kvb.buffer.RevertSnapshot(sid)
}

func (kvb *kvStoreWithBuffer) ResetSnapshots() {
kvb.buffer.ResetSnapshots()
}

func (kvb *kvStoreWithBuffer) SerializeQueue(
Expand Down
8 changes: 4 additions & 4 deletions db/kvstorewithbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func TestFlusher(t *testing.T) {
require.Equal(t, 1, kvb.Snapshot())
})
t.Run("Revert", func(t *testing.T) {
buffer.EXPECT().Revert(gomock.Any()).Return(expectedError).Times(1)
require.Equal(t, expectedError, kvb.Revert(1))
buffer.EXPECT().Revert(gomock.Any()).Return(nil).Times(1)
require.NoError(t, kvb.Revert(1))
buffer.EXPECT().RevertSnapshot(gomock.Any()).Return(expectedError).Times(1)
require.Equal(t, expectedError, kvb.RevertSnapshot(1))
buffer.EXPECT().RevertSnapshot(gomock.Any()).Return(nil).Times(1)
require.NoError(t, kvb.RevertSnapshot(1))
})
t.Run("Size", func(t *testing.T) {
buffer.EXPECT().Size().Return(5).Times(1)
Expand Down
6 changes: 0 additions & 6 deletions ioctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ type (
SetAlias(string, string) error
// DeleteAlias delete alias from the default config file
DeleteAlias(string) error
// PrintInfo print the command result or the question query
PrintInfo(string)
// IsCryptoSm2 return true if use sm2 cryptographic algorithm, false if not use
IsCryptoSm2() bool
}
Expand Down Expand Up @@ -265,10 +263,6 @@ func (c *client) writeAlias() error {
return nil
}

func (c *client) PrintInfo(info string) {
fmt.Println(info)
}

func (c *client) IsCryptoSm2() bool {
return c.cryptoSm2
}
Expand Down
Loading

0 comments on commit 6e44957

Please sign in to comment.