Skip to content

Commit

Permalink
Add feat: add store name in tracekv-emitted store traces cosmos#11646
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Dec 2, 2022
1 parent 8bf996f commit d7ff2b2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
10 changes: 9 additions & 1 deletion store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"github.com/cosmos/cosmos-sdk/store/types"
)

// storeNameCtxKey is the TraceContext metadata key that identifies
// the store which emitted a given trace.
const storeNameCtxKey = "store_name"

//----------------------------------------
// Store

Expand Down Expand Up @@ -55,7 +59,11 @@ func NewFromKVStore(

for key, store := range stores {
if cms.TracingEnabled() {
store = tracekv.NewStore(store.(types.KVStore), cms.traceWriter, cms.traceContext)
tctx := cms.traceContext.Clone().Merge(types.TraceContext{
storeNameCtxKey: key.Name(),
})

store = tracekv.NewStore(store.(types.KVStore), cms.traceWriter, tctx)
}
if cms.ListeningEnabled(key) {
store = listenkv.NewStore(store.(types.KVStore), key, listeners[key])
Expand Down
4 changes: 1 addition & 3 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,7 @@ func (rs *Store) SetTracingContext(tc types.TraceContext) types.MultiStore {
rs.traceContextMutex.Lock()
defer rs.traceContextMutex.Unlock()
if rs.traceContext != nil {
for k, v := range tc {
rs.traceContext[k] = v
}
rs.traceContext = rs.traceContext.Merge(tc)
} else {
rs.traceContext = tc
}
Expand Down
23 changes: 23 additions & 0 deletions store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,29 @@ type KVPair kv.Pair
// every trace operation.
type TraceContext map[string]interface{}

// Clone clones tc into another instance of TraceContext.
func (tc TraceContext) Clone() TraceContext {
ret := TraceContext{}
for k, v := range tc {
ret[k] = v
}

return ret
}

// Merge merges value of newTc into tc.
func (tc TraceContext) Merge(newTc TraceContext) TraceContext {
if tc == nil {
tc = TraceContext{}
}

for k, v := range newTc {
tc[k] = v
}

return tc
}

// MultiStorePersistentCache defines an interface which provides inter-block
// (persistent) caching capabilities for multiple CommitKVStores based on StoreKeys.
type MultiStorePersistentCache interface {
Expand Down

0 comments on commit d7ff2b2

Please sign in to comment.