diff --git a/store/cachemulti/store.go b/store/cachemulti/store.go index d64dc03aca12..deb1d46272dd 100644 --- a/store/cachemulti/store.go +++ b/store/cachemulti/store.go @@ -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 @@ -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]) diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 7e5d032850e3..fd3b1914ba8f 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -331,13 +331,7 @@ func (rs *Store) SetTracer(w io.Writer) types.MultiStore { 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 - } - } else { - rs.traceContext = tc - } + rs.traceContext = rs.traceContext.Merge(tc) return rs } diff --git a/store/types/store.go b/store/types/store.go index 8bb7037ea9f0..7e7db85722ed 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -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 { diff --git a/store/types/store_test.go b/store/types/store_test.go index f86144af7fbd..39087cd6a820 100644 --- a/store/types/store_test.go +++ b/store/types/store_test.go @@ -90,3 +90,120 @@ func TestTransientStoreKey(t *testing.T) { require.Equal(t, key.name, key.Name()) require.Equal(t, fmt.Sprintf("TransientStoreKey{%p, test}", key), key.String()) } + +func TestTraceContext_Clone(t *testing.T) { + tests := []struct { + name string + tc TraceContext + want TraceContext + }{ + { + "nil TraceContext yields empty TraceContext", + nil, + TraceContext{}, + }, + { + "non-nil TraceContext yields equal TraceContext", + TraceContext{ + "value": 42, + }, + TraceContext{ + "value": 42, + }, + }, + { + "non-nil TraceContext yields equal TraceContext, for more than one key", + TraceContext{ + "value": 42, + "another": 24, + "weird": "string", + }, + TraceContext{ + "value": 42, + "another": 24, + "weird": "string", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, tt.tc.Clone()) + }) + } +} + +func TestTraceContext_Clone_is_deep(t *testing.T) { + original := TraceContext{ + "value": 42, + "another": 24, + "weird": "string", + } + + clone := original.Clone() + + clone["other"] = true + + require.NotEqual(t, original, clone) +} + +func TestTraceContext_Merge(t *testing.T) { + tests := []struct { + name string + tc TraceContext + other TraceContext + want TraceContext + }{ + { + "tc is nil, other is empty, yields an empty TraceContext", + nil, + TraceContext{}, + TraceContext{}, + }, + { + "tc is nil, other is nil, yields an empty TraceContext", + nil, + nil, + TraceContext{}, + }, + { + "tc is not nil, other is nil, yields tc", + TraceContext{ + "data": 42, + }, + nil, + TraceContext{ + "data": 42, + }, + }, + { + "tc is not nil, other is not nil, yields tc + other", + TraceContext{ + "data": 42, + }, + TraceContext{ + "data2": 42, + }, + TraceContext{ + "data": 42, + "data2": 42, + }, + }, + { + "tc is not nil, other is not nil, other updates value in tc, yields tc updated with value from other", + TraceContext{ + "data": 42, + }, + TraceContext{ + "data": 24, + }, + TraceContext{ + "data": 24, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, tt.tc.Merge(tt.other)) + }) + } +}