Skip to content

Commit

Permalink
feat: store privacy data with cache (#1308)
Browse files Browse the repository at this point in the history
* feat: store privacy data with cache

* feat: test case
  • Loading branch information
zengchen221 authored Dec 31, 2020
1 parent 72f3e57 commit de43bfe
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 33 deletions.
21 changes: 15 additions & 6 deletions ledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type LedgerStore interface {
EventBus() event.EventBus
Get(k []byte, c ...storage.Cache) ([]byte, error)
GetObject(k []byte, c ...storage.Cache) (interface{}, []byte, error)
Put(k []byte, v interface{}, c ...storage.Cache) error
Iterator([]byte, []byte, func([]byte, []byte) error) error
IteratorObject(prefix []byte, end []byte, fn func([]byte, interface{}) error) error
GenerateSendBlock(block *types.StateBlock, amount types.Balance, prk ed25519.PrivateKey) (*types.StateBlock, error)
Expand Down Expand Up @@ -673,14 +674,22 @@ func (l *Ledger) GetObject(k []byte, c ...storage.Cache) (interface{}, []byte, e
return nil, v, nil
}

func (l *Ledger) Put(key []byte, value interface{}) error {
c := l.cache.GetCache()
return c.Put(key, value)
func (l *Ledger) Put(key []byte, value interface{}, c ...storage.Cache) error {
if len(c) > 0 {
return c[0].Put(key, value)
} else {
c := l.cache.GetCache()
return c.Put(key, value)
}
}

func (l *Ledger) Delete(k []byte) error {
c := l.cache.GetCache()
return c.Delete(k)
func (l *Ledger) Delete(k []byte, c ...storage.Cache) error {
if len(c) > 0 {
return c[0].Delete(k)
} else {
c := l.cache.GetCache()
return c.Delete(k)
}
}

func (l *Ledger) getFromStore(key []byte, batch ...storage.Batch) ([]byte, error) {
Expand Down
18 changes: 9 additions & 9 deletions ledger/ledger_privacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@ import (
)

type PrivacyStore interface {
AddBlockPrivatePayload(hash types.Hash, payload []byte) error
DeleteBlockPrivatePayload(hash types.Hash) error
GetBlockPrivatePayload(hash types.Hash) ([]byte, error)
AddBlockPrivatePayload(hash types.Hash, payload []byte, c ...storage.Cache) error
DeleteBlockPrivatePayload(hash types.Hash, c ...storage.Cache) error
GetBlockPrivatePayload(hash types.Hash, c ...storage.Cache) ([]byte, error)
}

func (l *Ledger) AddBlockPrivatePayload(hash types.Hash, payload []byte) error {
func (l *Ledger) AddBlockPrivatePayload(hash types.Hash, payload []byte, c ...storage.Cache) error {
if len(payload) == 0 {
payload = []byte{1}
}
k, err := storage.GetKeyOfParts(storage.KeyPrefixPrivatePayload, hash)
if err != nil {
return err
}
return l.store.Put(k, payload)
return l.Put(k, payload, c...)
}

func (l *Ledger) DeleteBlockPrivatePayload(hash types.Hash) error {
func (l *Ledger) DeleteBlockPrivatePayload(hash types.Hash, c ...storage.Cache) error {
k, err := storage.GetKeyOfParts(storage.KeyPrefixPrivatePayload, hash)
if err != nil {
return err
}
return l.store.Delete(k)
return l.Delete(k, c...)
}

func (l *Ledger) GetBlockPrivatePayload(hash types.Hash) ([]byte, error) {
func (l *Ledger) GetBlockPrivatePayload(hash types.Hash, c ...storage.Cache) ([]byte, error) {
k, err := storage.GetKeyOfParts(storage.KeyPrefixPrivatePayload, hash)
if err != nil {
return nil, err
}

pl, err := l.store.Get(k)
pl, err := l.Get(k, c...)
if err != nil {
if err == storage.KeyNotFound {
return nil, errors.New("block private payload not found")
Expand Down
32 changes: 32 additions & 0 deletions ledger/ledger_privacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,35 @@ func TestLedger_BlockPrivatePayload(t *testing.T) {
t.Fatal("deleted payload exist")
}
}

func TestLedger_BlockPrivatePayload_Cache(t *testing.T) {
teardownTestCase, l := setupTestCase(t)
defer teardownTestCase(t)

blk := mock.StateBlockWithoutWork()
blkHash := blk.GetHash()
pl := util.RandomFixedBytes(256)

err := l.AddBlockPrivatePayload(blkHash, pl, l.cache.GetCache())
if err != nil {
t.Fatal(err)
}

plRet, err := l.GetBlockPrivatePayload(blkHash, l.cache.GetCache())
if err != nil {
t.Fatal(err)
}
if bytes.Compare(plRet, pl) != 0 {
t.Fatal("payload not equal")
}

err = l.DeleteBlockPrivatePayload(blkHash, l.cache.GetCache())
if err != nil {
t.Fatal(err)
}

plRet, err = l.GetBlockPrivatePayload(blkHash, l.cache.GetCache())
if err == nil {
t.Fatal("deleted payload exist")
}
}
29 changes: 29 additions & 0 deletions ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,35 @@ func TestLedger_Cache(t *testing.T) {
t.Log(l.GetCacheStat())
}

func TestLedger_Put(t *testing.T) {
teardownTestCase, l := setupTestCase(t)
defer teardownTestCase(t)

c := l.cache.GetCache()
// get from ledger cache
key2 := []byte{1, 2, 4}
if err := l.Put(key2, []byte{4, 5, 6}, c); err != nil {
t.Fatal(err)
}
if i, r, err := l.GetObject(key2); err != nil || i == nil || r != nil {
t.Fatal(err, i, r)
} else {
t.Log(i)
}

// ledger put
key3 := []byte{1, 2, 5}
if err := l.Put(key3, []byte{4, 5, 6}, l.cache.GetCache()); err != nil {
t.Fatal(err)
}
if err := l.Delete(key3, l.cache.GetCache()); err != nil {
t.Fatal(err)
}
if i, r, err := l.GetObject(key3); err != storage.KeyNotFound || i != nil || r != nil {
t.Fatal(err, i, r)
}
}

func TestLedger_UCache(t *testing.T) {
teardownTestCase, l := setupTestCase(t)
defer teardownTestCase(t)
Expand Down
76 changes: 59 additions & 17 deletions mock/mocks/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rpc/grpc/apis/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestLedgerAPI_AccountHistoryTopn(t *testing.T) {
t.Fatal(err)
}
if len(r.GetBlocks()) != 4 {
t.Fatal()
t.Fatal(len(r.GetBlocks()))
}
}

Expand Down

0 comments on commit de43bfe

Please sign in to comment.