-
Notifications
You must be signed in to change notification settings - Fork 3.9k
refactor(collections): remove core dependency #24081
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4da74ce
refactor(collections): remove core dependency
aaronc 09d01c0
remove explicit core dep
aaronc 8ebcf31
remove core/testing dep by copying the necessary stuff to internal/co…
aaronc a2427eb
corecompat docs
aaronc cde75ef
remove all core references
aaronc 67908d7
add aliases
aaronc 7e82d68
add aliases
aaronc 582b9e0
remove unneeded code
aaronc 08f1ba8
revert to use cosmos-db
aaronc da56aff
tidy
aaronc f91a55e
remove core dep again
aaronc cddcb50
rename coretesting -> testutil
aaronc bd03391
update CHANGELOG.md
aaronc e9a3d9a
Merge branch 'release/v0.53.x' into aaronc/collections-no-core
aljo242 d8af38a
Merge branch 'release/v0.53.x' into aaronc/collections-no-core
aljo242 09c3a24
go mod tidy
technicallyty f8048cc
lint
technicallyty b3767b1
Merge branch 'release/v0.53.x' into aaronc/collections-no-core
aljo242 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package corecompat | ||
|
||
// Codec defines a Binary Codec and JSON Codec for modules to encode and decode data. | ||
type Codec interface { | ||
BinaryCodec | ||
JSONCodec | ||
} | ||
|
||
// BinaryCodec defines a binary encoding and decoding interface for modules to encode and decode data. | ||
type BinaryCodec interface { | ||
Marshal(ProtoMsg) ([]byte, error) | ||
Unmarshal([]byte, ProtoMsg) error | ||
} | ||
|
||
// JSONCodec defines a JSON encoding and decoding interface for modules to encode and decode data. | ||
type JSONCodec interface { | ||
MarshalJSON(ProtoMsg) ([]byte, error) | ||
UnmarshalJSON([]byte, ProtoMsg) error | ||
} | ||
|
||
// ProtoMsg defines the legacy golang proto message interface. | ||
type ProtoMsg = interface { | ||
Reset() | ||
String() string | ||
ProtoMessage() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Package corecompat defines the store, codec and genesis interface that collections expects | ||
// or implements. These interfaces are all redefined from cosmossdk.io/core in order to | ||
// avoid a direct dependency on cosmossdk.io/core. | ||
package corecompat | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package corecompat | ||
|
||
import "io" | ||
|
||
// GenesisSource is a source for genesis data in JSON format. It may abstract over a | ||
// single JSON object or separate files for each field in a JSON object that can | ||
// be streamed over. Modules should open a separate io.ReadCloser for each field that | ||
// is required. When fields represent arrays they can efficiently be streamed | ||
// over. If there is no data for a field, this function should return nil, nil. It is | ||
// important that the caller closes the reader when done with it. | ||
type GenesisSource = func(field string) (io.ReadCloser, error) | ||
|
||
// GenesisTarget is a target for writing genesis data in JSON format. It may | ||
// abstract over a single JSON object or JSON in separate files that can be | ||
// streamed over. Modules should open a separate io.WriteCloser for each field | ||
// and should prefer writing fields as arrays when possible to support efficient | ||
// iteration. It is important the caller closers the writer AND checks the error | ||
// when done with it. It is expected that a stream of JSON data is written | ||
// to the writer. | ||
type GenesisTarget = func(field string) (io.WriteCloser, error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package corecompat | ||
|
||
import "context" | ||
|
||
// KVStoreService represents a unique, non-forgeable handle to a regular merkle-tree | ||
// backed KVStore. It should be provided as a module-scoped dependency by the runtime | ||
// module being used to build the app. | ||
type KVStoreService = interface { | ||
// OpenKVStore retrieves the KVStore from the context. | ||
OpenKVStore(context.Context) KVStore | ||
} | ||
|
||
// MemoryStoreService represents a unique, non-forgeable handle to a memory-backed | ||
// KVStore. It should be provided as a module-scoped dependency by the runtime | ||
// module being used to build the app. | ||
type MemoryStoreService = interface { | ||
// OpenMemoryStore retrieves the memory store from the context. | ||
OpenMemoryStore(context.Context) KVStore | ||
} | ||
|
||
// KVStore describes the basic interface for interacting with key-value stores. | ||
type KVStore = interface { | ||
// Get returns nil iff key doesn't exist. Errors on nil key. | ||
Get(key []byte) ([]byte, error) | ||
|
||
// Has checks if a key exists. Errors on nil key. | ||
Has(key []byte) (bool, error) | ||
|
||
// Set sets the key. Errors on nil key or value. | ||
Set(key, value []byte) error | ||
|
||
// Delete deletes the key. Errors on nil key. | ||
Delete(key []byte) error | ||
|
||
// Iterator iterates over a domain of keys in ascending order. End is exclusive. | ||
// Start must be less than end, or the Iterator is invalid. | ||
// Iterator must be closed by caller. | ||
// To iterate over entire domain, use store.Iterator(nil, nil) | ||
// CONTRACT: No writes may happen within a domain while an iterator exists over it. | ||
// Exceptionally allowed for cachekv.Store, safe to write in the modules. | ||
Iterator(start, end []byte) (Iterator, error) | ||
|
||
// ReverseIterator iterates over a domain of keys in descending order. End is exclusive. | ||
// Start must be less than end, or the Iterator is invalid. | ||
// Iterator must be closed by caller. | ||
// CONTRACT: No writes may happen within a domain while an iterator exists over it. | ||
// Exceptionally allowed for cachekv.Store, safe to write in the modules. | ||
ReverseIterator(start, end []byte) (Iterator, error) | ||
} | ||
|
||
// Iterator represents an iterator over a domain of keys. Callers must call | ||
// Close when done. No writes can happen to a domain while there exists an | ||
// iterator over it. Some backends may take out database locks to ensure this | ||
// will not happen. | ||
// | ||
// Callers must make sure the iterator is valid before calling any methods on it, | ||
// otherwise these methods will panic. | ||
type Iterator = interface { | ||
// Domain returns the start (inclusive) and end (exclusive) limits of the iterator. | ||
Domain() (start, end []byte) | ||
|
||
// Valid returns whether the current iterator is valid. Once invalid, the Iterator remains | ||
// invalid forever. | ||
Valid() bool | ||
|
||
// Next moves the iterator to the next key in the database, as defined by order of iteration. | ||
// If Valid returns false, this method will panic. | ||
Next() | ||
|
||
// Key returns the key at the current position. Panics if the iterator is invalid. | ||
// Note, the key returned should be a copy and thus safe for modification. | ||
Key() []byte | ||
|
||
// Value returns the value at the current position. Panics if the iterator is | ||
// invalid. | ||
// Note, the value returned should be a copy and thus safe for modification. | ||
Value() []byte | ||
|
||
// Error returns the last error encountered by the iterator, if any. | ||
Error() error | ||
|
||
// Close closes the iterator, releasing any allocated resources. | ||
Close() error | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package testutil | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/collections/corecompat" | ||
) | ||
|
||
type dummyKey struct{} | ||
|
||
func Context() context.Context { | ||
dummy := &dummyCtx{ | ||
stores: map[string]corecompat.KVStore{}, | ||
} | ||
|
||
ctx := context.WithValue(context.Background(), dummyKey{}, dummy) | ||
return ctx | ||
} | ||
|
||
type dummyCtx struct { | ||
// maps store by the actor. | ||
stores map[string]corecompat.KVStore | ||
} | ||
|
||
func unwrap(ctx context.Context) *dummyCtx { | ||
dummy := ctx.Value(dummyKey{}) | ||
if dummy == nil { | ||
panic("invalid ctx without dummy") | ||
} | ||
|
||
return dummy.(*dummyCtx) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package testutil | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
db "github.com/cosmos/cosmos-db" | ||
|
||
store "cosmossdk.io/collections/corecompat" | ||
) | ||
|
||
var _ store.KVStoreService = (*kvStoreService)(nil) | ||
|
||
func KVStoreService(ctx context.Context, moduleName string) store.KVStoreService { | ||
unwrap(ctx).stores[moduleName] = db.NewMemDB() | ||
return kvStoreService{ | ||
moduleName: moduleName, | ||
} | ||
} | ||
|
||
type kvStoreService struct { | ||
moduleName string | ||
} | ||
|
||
func (k kvStoreService) OpenKVStore(ctx context.Context) store.KVStore { | ||
kv, ok := unwrap(ctx).stores[k.moduleName] | ||
if !ok { | ||
panic(fmt.Sprintf("KVStoreService %s not found", k.moduleName)) | ||
} | ||
return kv | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this package feel like the right place to put this stuff?