Skip to content

Commit

Permalink
Problem: add back CacheWrapWithTrace api
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Dec 12, 2024
1 parent 142c0af commit 90a2922
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 7 deletions.
5 changes: 5 additions & 0 deletions runtime/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runtime

import (
"context"
"io"

"cosmossdk.io/core/store"
storetypes "cosmossdk.io/store/types"
Expand Down Expand Up @@ -109,6 +110,10 @@ func (kvStoreAdapter) CacheWrap() storetypes.CacheWrap {
panic("unimplemented")
}

func (kvStoreAdapter) CacheWrapWithTrace(w io.Writer, tc storetypes.TraceContext) storetypes.CacheWrap {
panic("unimplemented")
}

func (kvStoreAdapter) GetStoreType() storetypes.StoreType {
panic("unimplemented")
}
Expand Down
8 changes: 8 additions & 0 deletions server/mock/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (ms multiStore) CacheWrap() storetypes.CacheWrap {
panic("not implemented")
}

func (ms multiStore) CacheWrapWithTrace(w io.Writer, tc storetypes.TraceContext) storetypes.CacheWrap {
panic("unimplemented")
}

func (ms multiStore) TracingEnabled() bool {
panic("not implemented")
}
Expand Down Expand Up @@ -178,6 +182,10 @@ func (kv kvStore) CacheWrap() storetypes.CacheWrap {
panic("not implemented")
}

func (kv kvStore) CacheWrapWithTrace(w io.Writer, tc storetypes.TraceContext) storetypes.CacheWrap {
panic("unimplemented")
}

func (kv kvStore) GetStoreType() storetypes.StoreType {
panic("not implemented")
}
Expand Down
1 change: 1 addition & 0 deletions store/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* [#243](https://github.com/crypto-org-chain/cosmos-sdk/pull/243) Support `RunAtomic` API to use new CoW cache store.
* [#244](https://github.com/crypto-org-chain/cosmos-sdk/pull/244) Add `Discard` method to CacheWrap to discard the write buffer.
* [#258](https://github.com/crypto-org-chain/cosmos-sdk/pull/258) Add `NewFromParent` API to cachemulti store to create a new store from block-stm multiversion data structure.
* [#1043](https://github.com/crypto-org-chain/cosmos-sdk/pull/1043) Add back CacheWrapWithTrace api.

## [Unreleased]

Expand Down
11 changes: 11 additions & 0 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cachekv

import (
"io"

"cosmossdk.io/store/cachekv/internal"
"cosmossdk.io/store/internal/btree"
"cosmossdk.io/store/tracekv"
"cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -128,6 +131,14 @@ func (store *GStore[V]) CacheWrap() types.CacheWrap {
return NewGStore(store, store.isZero, store.valueLen)
}

// CacheWrapWithTrace implements the CacheWrapper interface.
func (store *GStore[V]) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
if store, ok := any(store).(*GStore[[]byte]); ok {
return NewStore(tracekv.NewStore(store, w, tc))
}
return store.CacheWrap()
}

//----------------------------------------
// Iteration

Expand Down
5 changes: 5 additions & 0 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ func (cms Store) CacheWrap() types.CacheWrap {
return cms.CacheMultiStore().(types.CacheWrap)
}

// CacheWrapWithTrace implements the CacheWrapper interface.
func (cms Store) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types.CacheWrap {
return cms.CacheWrap()
}

// Implements MultiStore.
func (cms Store) CacheMultiStore() types.CacheMultiStore {
return NewFromParent(cms.getCacheWrapper, cms.traceWriter, cms.traceContext)
Expand Down
8 changes: 8 additions & 0 deletions store/dbadapter/store.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package dbadapter

import (
"io"

dbm "github.com/cosmos/cosmos-db"

"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/tracekv"
"cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -78,5 +81,10 @@ func (dsa Store) CacheWrap() types.CacheWrap {
return cachekv.NewStore(dsa)
}

// CacheWrapWithTrace implements KVStore.
func (dsa Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
return cachekv.NewStore(tracekv.NewStore(dsa, w, tc))
}

// dbm.DB implements KVStore so we can CacheKVStore it.
var _ types.KVStore = Store{}
3 changes: 3 additions & 0 deletions store/dbadapter/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,7 @@ func TestCacheWraps(t *testing.T) {

cacheWrapper := store.CacheWrap()
require.IsType(t, &cachekv.Store{}, cacheWrapper)

cacheWrappedWithTrace := store.CacheWrapWithTrace(nil, nil)
require.IsType(t, &cachekv.Store{}, cacheWrappedWithTrace)
}
11 changes: 10 additions & 1 deletion store/gaskv/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gaskv

import "cosmossdk.io/store/types"
import (
"io"

"cosmossdk.io/store/types"
)

// ObjectValueLength is the emulated number of bytes for storing transient objects in gas accounting.
const ObjectValueLength = 16
Expand Down Expand Up @@ -115,6 +119,11 @@ func (gs *GStore[V]) CacheWrap() types.CacheWrap {
panic("cannot CacheWrap a GasKVStore")
}

// CacheWrapWithTrace implements the KVStore interface.
func (gs *GStore[V]) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types.CacheWrap {
panic("cannot CacheWrapWithTrace a GasKVStore")
}

func (gs *GStore[V]) iterator(start, end []byte, ascending bool) types.GIterator[V] {
var parent types.GIterator[V]
if ascending {
Expand Down
1 change: 1 addition & 0 deletions store/gaskv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestGasKVStoreBasic(t *testing.T) {

require.Equal(t, types.StoreTypeDB, st.GetStoreType())
require.Panics(t, func() { st.CacheWrap() })
require.Panics(t, func() { st.CacheWrapWithTrace(nil, nil) })

require.Panics(t, func() { st.Set(nil, []byte("value")) }, "setting a nil key should panic")
require.Panics(t, func() { st.Set([]byte(""), []byte("value")) }, "setting an empty key should panic")
Expand Down
7 changes: 7 additions & 0 deletions store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iavl
import (
"errors"
"fmt"
"io"

cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
dbm "github.com/cosmos/cosmos-db"
Expand All @@ -15,6 +16,7 @@ import (
"cosmossdk.io/store/internal/kv"
"cosmossdk.io/store/metrics"
pruningtypes "cosmossdk.io/store/pruning/types"
"cosmossdk.io/store/tracekv"
"cosmossdk.io/store/types"
"cosmossdk.io/store/wrapper"
)
Expand Down Expand Up @@ -191,6 +193,11 @@ func (st *Store) CacheWrap() types.CacheWrap {
return cachekv.NewStore(st)
}

// CacheWrapWithTrace implements the Store interface.
func (st *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
return cachekv.NewStore(tracekv.NewStore(st, w, tc))
}

// Implements types.KVStore.
func (st *Store) Set(key, value []byte) {
types.AssertValidKey(key)
Expand Down
3 changes: 3 additions & 0 deletions store/iavl/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,9 @@ func TestCacheWraps(t *testing.T) {

cacheWrapper := store.CacheWrap()
require.IsType(t, &cachekv.Store{}, cacheWrapper)

cacheWrappedWithTrace := store.CacheWrapWithTrace(nil, nil)
require.IsType(t, &cachekv.Store{}, cacheWrappedWithTrace)
}

func TestChangeSets(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions store/internal/btreeadaptor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package internal

import (
"io"

"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/internal/btree"
"cosmossdk.io/store/types"
Expand Down Expand Up @@ -59,3 +61,8 @@ func (ts *BTreeStore[V]) GetStoreType() types.StoreType {
func (ts *BTreeStore[V]) CacheWrap() types.CacheWrap {
return cachekv.NewGStore(ts, ts.isZero, ts.valueLen)
}

// CacheWrapWithTrace branches the underlying store.
func (ts *BTreeStore[V]) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
return cachekv.NewGStore(ts, ts.isZero, ts.valueLen)
}
11 changes: 9 additions & 2 deletions store/listenkv/store.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package listenkv

import (
"cosmossdk.io/store/cachekv"
"io"

"cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -131,5 +132,11 @@ func (s *Store) GetStoreType() types.StoreType {
// CacheWrap implements the KVStore interface. It panics as a Store
// cannot be cache wrapped.
func (s *Store) CacheWrap() types.CacheWrap {
return cachekv.NewStore(s)
panic("cannot CacheWrap a ListenKVStore")

Check warning

Code scanning / CodeQL

Panic in BeginBock or EndBlock consensus methods Warning

Possible panics in BeginBock- or EndBlock-related consensus methods could cause a chain halt
}

// CacheWrapWithTrace implements the KVStore interface. It panics as a
// Store cannot be cache wrapped.
func (s *Store) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types.CacheWrap {
panic("cannot CacheWrapWithTrace a ListenKVStore")
}
5 changes: 5 additions & 0 deletions store/listenkv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,8 @@ func TestListenKVStoreCacheWrap(t *testing.T) {
store := newEmptyListenKVStore(nil)
store.CacheWrap()
}

func TestListenKVStoreCacheWrapWithTrace(t *testing.T) {
store := newEmptyListenKVStore(nil)
require.Panics(t, func() { store.CacheWrapWithTrace(nil, nil) })
}
3 changes: 3 additions & 0 deletions store/mem/mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func TestStore(t *testing.T) {

cacheWrapper := db.CacheWrap()
require.IsType(t, &cachekv.Store{}, cacheWrapper)

cacheWrappedWithTrace := db.CacheWrapWithTrace(nil, nil)
require.IsType(t, &cachekv.Store{}, cacheWrappedWithTrace)
}

func TestCommit(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions store/mem/store.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package mem

import (
"io"

dbm "github.com/cosmos/cosmos-db"

"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/dbadapter"
pruningtypes "cosmossdk.io/store/pruning/types"
"cosmossdk.io/store/tracekv"
"cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -38,6 +41,11 @@ func (s Store) CacheWrap() types.CacheWrap {
return cachekv.NewStore(s)
}

// CacheWrapWithTrace implements KVStore.
func (s Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
return cachekv.NewStore(tracekv.NewStore(s, w, tc))
}

// Commit performs a no-op as entries are persistent between commitments.
func (s *Store) Commit() (id types.CommitID) { return }

Expand Down
10 changes: 10 additions & 0 deletions store/prefix/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package prefix
import (
"bytes"
"errors"
"io"

"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/tracekv"
"cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -83,6 +85,14 @@ func (s GStore[V]) CacheWrap() types.CacheWrap {
return cachekv.NewGStore(s, s.isZero, s.valueLen)
}

// CacheWrapWithTrace implements the KVStore interface.
func (s GStore[V]) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types.CacheWrap {
if store, ok := any(s).(*GStore[[]byte]); ok {
return cachekv.NewGStore(tracekv.NewStore(store, w, tc), store.isZero, store.valueLen)
}
return s.CacheWrap()
}

// Implements KVStore
func (s GStore[V]) Get(key []byte) V {
res := s.parent.Get(s.key(key))
Expand Down
3 changes: 3 additions & 0 deletions store/prefix/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,7 @@ func TestCacheWraps(t *testing.T) {

cacheWrapper := store.CacheWrap()
require.IsType(t, &cachekv.Store{}, cacheWrapper)

cacheWrappedWithTrace := store.CacheWrapWithTrace(nil, nil)
require.IsType(t, &cachekv.Store{}, cacheWrappedWithTrace)
}
5 changes: 5 additions & 0 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,11 @@ func (rs *Store) CacheWrap() types.CacheWrap {
return rs.CacheMultiStore().(types.CacheWrap)
}

// CacheWrapWithTrace implements the CacheWrapper interface.
func (rs *Store) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types.CacheWrap {
return rs.CacheWrap()
}

// CacheMultiStore creates ephemeral branch of the multi-store and returns a CacheMultiStore.
// It implements the MultiStore interface.
func (rs *Store) CacheMultiStore() types.CacheMultiStore {
Expand Down
6 changes: 4 additions & 2 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
sdkmaps "cosmossdk.io/store/internal/maps"
"cosmossdk.io/store/metrics"
pruningtypes "cosmossdk.io/store/pruning/types"
"cosmossdk.io/store/tracekv"
"cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -731,6 +730,9 @@ func TestCacheWraps(t *testing.T) {

cacheWrapper := multi.CacheWrap()
require.IsType(t, cachemulti.Store{}, cacheWrapper)

cacheWrappedWithTrace := multi.CacheWrapWithTrace(nil, nil)
require.IsType(t, cachemulti.Store{}, cacheWrappedWithTrace)
}

func TestTraceConcurrency(t *testing.T) {
Expand All @@ -748,7 +750,7 @@ func TestTraceConcurrency(t *testing.T) {

cms := multi.CacheMultiStore()
store1 := cms.GetKVStore(key)
cw := tracekv.NewStore(store1.CacheWrap().(types.KVStore), b, tc)
cw := store1.CacheWrapWithTrace(b, tc)
_ = cw
require.NotNil(t, store1)

Expand Down
9 changes: 7 additions & 2 deletions store/tracekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"

"cosmossdk.io/errors"
"cosmossdk.io/store/cachekv"
"cosmossdk.io/store/types"
)

Expand Down Expand Up @@ -165,7 +164,13 @@ func (tkv *Store) GetStoreType() types.StoreType {
// CacheWrap implements the KVStore interface. It panics because a Store
// cannot be branched.
func (tkv *Store) CacheWrap() types.CacheWrap {
return cachekv.NewStore(tkv)
return tkv.parent.CacheWrap()
}

// CacheWrapWithTrace implements the KVStore interface. It panics as a
// Store cannot be branched.
func (tkv *Store) CacheWrapWithTrace(_ io.Writer, _ types.TraceContext) types.CacheWrap {
panic("cannot CacheWrapWithTrace a TraceKVStore")
}

// writeOperation writes a KVStore operation to the underlying io.Writer as
Expand Down
5 changes: 5 additions & 0 deletions store/tracekv/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,8 @@ func TestTraceKVStoreCacheWrap(t *testing.T) {
store := newEmptyTraceKVStore(nil)
store.CacheWrap()
}

func TestTraceKVStoreCacheWrapWithTrace(t *testing.T) {
store := newEmptyTraceKVStore(nil)
require.Panics(t, func() { store.CacheWrapWithTrace(nil, nil) })
}
6 changes: 6 additions & 0 deletions store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,17 @@ type CacheWrap interface {

// Discard the write set
Discard()

// CacheWrapWithTrace branches a store with tracing enabled.
CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap
}

type CacheWrapper interface {
// CacheWrap branches a store.
CacheWrap() CacheWrap

// CacheWrapWithTrace branches a store with tracing enabled.
CacheWrapWithTrace(w io.Writer, tc TraceContext) CacheWrap
}

func (cid CommitID) IsZero() bool {
Expand Down

0 comments on commit 90a2922

Please sign in to comment.