Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: precompiled contract poc #172

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,37 @@ func (store *Store) CacheWrapWithTrace(w io.Writer, tc types.TraceContext) types
return NewStore(tracekv.NewStore(store, w, tc))
}

// Copy implements deep copy of CacheKVStore
// TODO(dudong2): frequent calls to deep copy are a big bottleneck for performance, so need to benchmark
func (store *Store) Copy() types.CacheKVStore {
store.mtx.Lock()
defer store.mtx.Unlock()

// deep copy of cValue
cacheCopied := make(map[string]*cValue, len(store.cache))
for key, val := range store.cache {
valueCopied := make([]byte, len(val.value))
copy(valueCopied, val.value)
cacheCopied[key] = &cValue{
value: valueCopied,
dirty: val.dirty,
}
}

// unsortedCache only track the key
unsortedCacheCopied := make(map[string]struct{}, len(store.unsortedCache))
for key := range store.unsortedCache {
unsortedCacheCopied[key] = struct{}{}
}

return &Store{
cache: cacheCopied,
unsortedCache: unsortedCacheCopied,
sortedCache: store.sortedCache.Copy(),
parent: store.parent,
}
}

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

Expand Down
20 changes: 20 additions & 0 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,23 @@ func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
}
return store.(types.KVStore)
}

// Copy returns an deep copy of CacheMultiStore
// TODO(dudong2): frequent calls to deep copy are a big bottleneck for performance, so need to benchmark
func (cms Store) Copy() types.CacheMultiStore {
// deep copy of CacheKVStore underlying CacheMultiStore
storesCopied := make(map[types.StoreKey]types.CacheWrap, len(cms.stores))
for key, store := range cms.stores {
if store, ok := store.(*cachekv.Store); ok {
storesCopied[key] = store.Copy()
}
}

return Store{
db: cms.db.Copy(),
stores: storesCopied,
keys: cms.keys,
traceWriter: cms.traceWriter,
traceContext: cms.traceContext,
}
}
6 changes: 5 additions & 1 deletion store/types/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ type MultiStore interface {
// From MultiStore.CacheMultiStore()....
type CacheMultiStore interface {
MultiStore
Write() // Writes operations to underlying KVStore
Write() // Writes operations to underlying KVStore
Copy() CacheMultiStore // deep copy of CacheMultiStore
}

// CommitMultiStore is an interface for a MultiStore without cache capabilities.
Expand Down Expand Up @@ -244,6 +245,9 @@ type CacheKVStore interface {

// Writes operations to underlying KVStore
Write()

// deep copy of CacheKVStore
Copy() CacheKVStore
}

// CommitKVStore is an interface for MultiStore.
Expand Down
Loading