refactor(collections): remove core dependency (#24081)
Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io> Co-authored-by: Tyler <48813565+technicallyty@users.noreply.github.com>
This commit is contained in:
parent
a340fa39d0
commit
2ccf08a3fe
@ -31,6 +31,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
### Improvements
|
||||
|
||||
* [#24081](https://github.com/cosmos/cosmos-sdk/pull/24081) Remove `cosmossdk.io/core` dependency.
|
||||
|
||||
## [v1.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/collections%2Fv1.1.0)
|
||||
|
||||
### Improvements
|
||||
|
||||
@ -7,13 +7,13 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/core/store"
|
||||
"cosmossdk.io/core/testing"
|
||||
store "cosmossdk.io/collections/corecompat"
|
||||
"cosmossdk.io/collections/internal/testutil"
|
||||
)
|
||||
|
||||
func deps() (store.KVStoreService, context.Context) {
|
||||
ctx := coretesting.Context()
|
||||
kv := coretesting.KVStoreService(ctx, "test")
|
||||
ctx := testutil.Context()
|
||||
kv := testutil.KVStoreService(ctx, "test")
|
||||
return kv, ctx
|
||||
}
|
||||
|
||||
|
||||
@ -5,7 +5,7 @@ import (
|
||||
|
||||
db "github.com/cosmos/cosmos-db"
|
||||
|
||||
"cosmossdk.io/core/store"
|
||||
store "cosmossdk.io/collections/corecompat"
|
||||
)
|
||||
|
||||
type contextStoreKey struct{}
|
||||
|
||||
26
collections/corecompat/codec.go
Normal file
26
collections/corecompat/codec.go
Normal file
@ -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()
|
||||
}
|
||||
4
collections/corecompat/doc.go
Normal file
4
collections/corecompat/doc.go
Normal file
@ -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
|
||||
20
collections/corecompat/genesis.go
Normal file
20
collections/corecompat/genesis.go
Normal file
@ -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)
|
||||
84
collections/corecompat/store.go
Normal file
84
collections/corecompat/store.go
Normal file
@ -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
|
||||
}
|
||||
@ -8,7 +8,7 @@ import (
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/core/appmodule"
|
||||
appmodule "cosmossdk.io/collections/corecompat"
|
||||
)
|
||||
|
||||
func TestDefaultGenesis(t *testing.T) {
|
||||
|
||||
@ -3,12 +3,10 @@ module cosmossdk.io/collections
|
||||
go 1.23.2
|
||||
|
||||
require (
|
||||
cosmossdk.io/core v1.0.0
|
||||
cosmossdk.io/core/testing v0.0.2
|
||||
cosmossdk.io/schema v1.0.0
|
||||
github.com/cosmos/cosmos-db v1.1.1
|
||||
github.com/cosmos/gogoproto v1.7.0
|
||||
github.com/google/go-cmp v0.7.0
|
||||
github.com/google/go-cmp v0.6.0
|
||||
github.com/stretchr/testify v1.10.0
|
||||
github.com/tidwall/btree v1.7.0
|
||||
google.golang.org/protobuf v1.36.6
|
||||
@ -46,7 +44,7 @@ require (
|
||||
github.com/spf13/cast v1.7.1 // indirect
|
||||
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
|
||||
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
|
||||
golang.org/x/sys v0.29.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
@ -30,10 +30,6 @@ cloud.google.com/go/storage v1.5.0/go.mod h1:tpKbwo567HUNpVclU5sGELwQWBDZ8gh0Zeo
|
||||
cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohlUTyfDhBk=
|
||||
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
|
||||
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
|
||||
cosmossdk.io/core v1.0.0 h1:e7XBbISOytLBOXMVwpRPixThXqEkeLGlg8no/qpgS8U=
|
||||
cosmossdk.io/core v1.0.0/go.mod h1:mKIp3RkoEmtqdEdFHxHwWAULRe+79gfdOvmArrLDbDc=
|
||||
cosmossdk.io/core/testing v0.0.2 h1:TXAmsnHa1C5lLiVN1z+rnkiyNS6sNwcGJCb6OouaNv8=
|
||||
cosmossdk.io/core/testing v0.0.2/go.mod h1:6Y3QaoNXsgeHiV4+9oYF/IqMFqCK5cJClonw/OXxHlY=
|
||||
cosmossdk.io/schema v1.0.0 h1:/diH4XJjpV1JQwuIozwr+A4uFuuwanFdnw2kKeiXwwQ=
|
||||
cosmossdk.io/schema v1.0.0/go.mod h1:RDAhxIeNB4bYqAlF4NBJwRrgtnciMcyyg0DOKnhNZQQ=
|
||||
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
|
||||
@ -153,8 +149,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
||||
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
|
||||
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
|
||||
@ -348,8 +344,8 @@ golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81R
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
|
||||
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
@ -366,8 +362,8 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
@ -411,8 +407,8 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
|
||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
@ -420,8 +416,8 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
|
||||
@ -8,7 +8,7 @@ import (
|
||||
"cosmossdk.io/collections"
|
||||
"cosmossdk.io/collections/colltest"
|
||||
"cosmossdk.io/collections/indexes"
|
||||
"cosmossdk.io/core/testing"
|
||||
"cosmossdk.io/collections/internal/testutil"
|
||||
)
|
||||
|
||||
type company struct {
|
||||
@ -45,8 +45,8 @@ func newTestIndexedMap(schema *collections.SchemaBuilder) *collections.IndexedMa
|
||||
}
|
||||
|
||||
func TestIndexedMap(t *testing.T) {
|
||||
ctx := coretesting.Context()
|
||||
sk := coretesting.KVStoreService(ctx, "test")
|
||||
ctx := testutil.Context()
|
||||
sk := testutil.KVStoreService(ctx, "test")
|
||||
|
||||
schema := collections.NewSchemaBuilder(sk)
|
||||
|
||||
@ -125,7 +125,7 @@ func newInferIndex(schema *collections.SchemaBuilder) *inferIndex {
|
||||
}
|
||||
|
||||
func TestIndexedMapInfer(t *testing.T) {
|
||||
sk := coretesting.KVStoreService(coretesting.Context(), "test")
|
||||
sk := testutil.KVStoreService(testutil.Context(), "test")
|
||||
schema := collections.NewSchemaBuilder(sk)
|
||||
|
||||
_, err := collections.NewIndexedMapSafe(schema, collections.NewPrefix(0), "im", collections.StringKey, colltest.MockValueCodec[company](), newInferIndex(schema))
|
||||
|
||||
@ -3,13 +3,13 @@ package indexes
|
||||
import (
|
||||
"context"
|
||||
|
||||
"cosmossdk.io/core/store"
|
||||
"cosmossdk.io/core/testing"
|
||||
store "cosmossdk.io/collections/corecompat"
|
||||
"cosmossdk.io/collections/internal/testutil"
|
||||
)
|
||||
|
||||
func deps() (store.KVStoreService, context.Context) {
|
||||
ctx := coretesting.Context()
|
||||
kv := coretesting.KVStoreService(ctx, "test")
|
||||
ctx := testutil.Context()
|
||||
kv := testutil.KVStoreService(ctx, "test")
|
||||
return kv, ctx
|
||||
}
|
||||
|
||||
|
||||
32
collections/internal/testutil/context.go
Normal file
32
collections/internal/testutil/context.go
Normal file
@ -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)
|
||||
}
|
||||
31
collections/internal/testutil/store.go
Normal file
31
collections/internal/testutil/store.go
Normal file
@ -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
|
||||
}
|
||||
@ -7,7 +7,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"cosmossdk.io/collections/codec"
|
||||
"cosmossdk.io/core/store"
|
||||
store "cosmossdk.io/collections/corecompat"
|
||||
)
|
||||
|
||||
// ErrInvalidIterator is returned when an Iterate call resulted in an invalid iterator.
|
||||
|
||||
@ -6,12 +6,12 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
"cosmossdk.io/core/testing"
|
||||
"cosmossdk.io/collections/internal/testutil"
|
||||
)
|
||||
|
||||
func TestLookupMap(t *testing.T) {
|
||||
ctx := coretesting.Context()
|
||||
sk := coretesting.KVStoreService(ctx, "test")
|
||||
ctx := testutil.Context()
|
||||
sk := testutil.KVStoreService(ctx, "test")
|
||||
schema := collections.NewSchemaBuilder(sk)
|
||||
|
||||
lm := collections.NewLookupMap(schema, collections.NewPrefix("hi"), "lm", collections.Uint64Key, collections.Uint64Value)
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"cosmossdk.io/collections/codec"
|
||||
"cosmossdk.io/core/store"
|
||||
store "cosmossdk.io/collections/corecompat"
|
||||
)
|
||||
|
||||
// Map represents the basic collections object.
|
||||
|
||||
@ -10,7 +10,7 @@ import (
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
collcodec "cosmossdk.io/collections/codec"
|
||||
corecodec "cosmossdk.io/core/codec"
|
||||
corecodec "cosmossdk.io/collections/corecompat"
|
||||
)
|
||||
|
||||
// BoolValue implements a ValueCodec that saves the bool value
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
"cosmossdk.io/collections/colltest"
|
||||
coretesting "cosmossdk.io/core/testing"
|
||||
"cosmossdk.io/collections/internal/testutil"
|
||||
)
|
||||
|
||||
func TestQuad(t *testing.T) {
|
||||
@ -19,8 +19,8 @@ func TestQuad(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestQuadRange(t *testing.T) {
|
||||
ctx := coretesting.Context()
|
||||
sk := coretesting.KVStoreService(ctx, "test")
|
||||
ctx := testutil.Context()
|
||||
sk := testutil.KVStoreService(ctx, "test")
|
||||
schema := collections.NewSchemaBuilder(sk)
|
||||
// this is a key composed of 4 parts: uint64, string, []byte, bool
|
||||
kc := collections.QuadKeyCodec(collections.Uint64Key, collections.StringKey, collections.BytesKey, collections.BoolKey)
|
||||
|
||||
@ -7,8 +7,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"cosmossdk.io/core/appmodule"
|
||||
"cosmossdk.io/core/store"
|
||||
core "cosmossdk.io/collections/corecompat"
|
||||
)
|
||||
|
||||
// SchemaBuilder is used for building schemas. The Build method should always
|
||||
@ -20,7 +19,7 @@ type SchemaBuilder struct {
|
||||
}
|
||||
|
||||
// NewSchemaBuilderFromAccessor creates a new schema builder from the provided store accessor function.
|
||||
func NewSchemaBuilderFromAccessor(accessorFunc func(ctx context.Context) store.KVStore) *SchemaBuilder {
|
||||
func NewSchemaBuilderFromAccessor(accessorFunc func(ctx context.Context) core.KVStore) *SchemaBuilder {
|
||||
return &SchemaBuilder{
|
||||
schema: &Schema{
|
||||
storeAccessor: accessorFunc,
|
||||
@ -33,7 +32,7 @@ func NewSchemaBuilderFromAccessor(accessorFunc func(ctx context.Context) store.K
|
||||
// NewSchemaBuilder creates a new schema builder from the provided store key.
|
||||
// Callers should always call the SchemaBuilder.Build method when they are
|
||||
// done adding collections to the schema.
|
||||
func NewSchemaBuilder(service store.KVStoreService) *SchemaBuilder {
|
||||
func NewSchemaBuilder(service core.KVStoreService) *SchemaBuilder {
|
||||
return NewSchemaBuilderFromAccessor(service.OpenKVStore)
|
||||
}
|
||||
|
||||
@ -125,22 +124,22 @@ var nameRegex = regexp.MustCompile("^" + NameRegex + "$")
|
||||
// methods for importing/exporting genesis data and for schema reflection for
|
||||
// clients.
|
||||
type Schema struct {
|
||||
storeAccessor func(context.Context) store.KVStore
|
||||
storeAccessor func(context.Context) core.KVStore
|
||||
collectionsOrdered []string
|
||||
collectionsByPrefix map[string]Collection
|
||||
collectionsByName map[string]Collection
|
||||
}
|
||||
|
||||
// NewSchema creates a new schema for the provided KVStoreService.
|
||||
func NewSchema(service store.KVStoreService) Schema {
|
||||
return NewSchemaFromAccessor(func(ctx context.Context) store.KVStore {
|
||||
func NewSchema(service core.KVStoreService) Schema {
|
||||
return NewSchemaFromAccessor(func(ctx context.Context) core.KVStore {
|
||||
return service.OpenKVStore(ctx)
|
||||
})
|
||||
}
|
||||
|
||||
// NewMemoryStoreSchema creates a new schema for the provided MemoryStoreService.
|
||||
func NewMemoryStoreSchema(service store.MemoryStoreService) Schema {
|
||||
return NewSchemaFromAccessor(func(ctx context.Context) store.KVStore {
|
||||
func NewMemoryStoreSchema(service core.MemoryStoreService) Schema {
|
||||
return NewSchemaFromAccessor(func(ctx context.Context) core.KVStore {
|
||||
return service.OpenMemoryStore(ctx)
|
||||
})
|
||||
}
|
||||
@ -153,7 +152,7 @@ func NewMemoryStoreSchema(service store.MemoryStoreService) Schema {
|
||||
// NewSchemaFromAccessor(func(ctx context.Context) store.KVStore {
|
||||
// return sdk.UnwrapSDKContext(ctx).KVStore(kvStoreKey)
|
||||
// }
|
||||
func NewSchemaFromAccessor(accessor func(context.Context) store.KVStore) Schema {
|
||||
func NewSchemaFromAccessor(accessor func(context.Context) core.KVStore) Schema {
|
||||
return Schema{
|
||||
storeAccessor: accessor,
|
||||
collectionsByName: map[string]Collection{},
|
||||
@ -168,7 +167,7 @@ func (s Schema) IsOnePerModuleType() {}
|
||||
func (s Schema) IsAppModule() {}
|
||||
|
||||
// DefaultGenesis implements the appmodule.HasGenesis.DefaultGenesis method.
|
||||
func (s Schema) DefaultGenesis(target appmodule.GenesisTarget) error {
|
||||
func (s Schema) DefaultGenesis(target core.GenesisTarget) error {
|
||||
for _, name := range s.collectionsOrdered {
|
||||
err := s.defaultGenesis(target, name)
|
||||
if err != nil {
|
||||
@ -179,7 +178,7 @@ func (s Schema) DefaultGenesis(target appmodule.GenesisTarget) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Schema) defaultGenesis(target appmodule.GenesisTarget, name string) error {
|
||||
func (s Schema) defaultGenesis(target core.GenesisTarget, name string) error {
|
||||
wc, err := target(name)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -195,7 +194,7 @@ func (s Schema) defaultGenesis(target appmodule.GenesisTarget, name string) erro
|
||||
}
|
||||
|
||||
// ValidateGenesis implements the appmodule.HasGenesis.ValidateGenesis method.
|
||||
func (s Schema) ValidateGenesis(source appmodule.GenesisSource) error {
|
||||
func (s Schema) ValidateGenesis(source core.GenesisSource) error {
|
||||
for _, name := range s.collectionsOrdered {
|
||||
err := s.validateGenesis(source, name)
|
||||
if err != nil {
|
||||
@ -205,7 +204,7 @@ func (s Schema) ValidateGenesis(source appmodule.GenesisSource) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Schema) validateGenesis(source appmodule.GenesisSource, name string) error {
|
||||
func (s Schema) validateGenesis(source core.GenesisSource, name string) error {
|
||||
rc, err := source(name)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -226,7 +225,7 @@ func (s Schema) validateGenesis(source appmodule.GenesisSource, name string) err
|
||||
}
|
||||
|
||||
// InitGenesis implements the appmodule.HasGenesis.InitGenesis method.
|
||||
func (s Schema) InitGenesis(ctx context.Context, source appmodule.GenesisSource) error {
|
||||
func (s Schema) InitGenesis(ctx context.Context, source core.GenesisSource) error {
|
||||
for _, name := range s.collectionsOrdered {
|
||||
err := s.initGenesis(ctx, source, name)
|
||||
if err != nil {
|
||||
@ -237,7 +236,7 @@ func (s Schema) InitGenesis(ctx context.Context, source appmodule.GenesisSource)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Schema) initGenesis(ctx context.Context, source appmodule.GenesisSource, name string) error {
|
||||
func (s Schema) initGenesis(ctx context.Context, source core.GenesisSource, name string) error {
|
||||
rc, err := source(name)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -258,7 +257,7 @@ func (s Schema) initGenesis(ctx context.Context, source appmodule.GenesisSource,
|
||||
}
|
||||
|
||||
// ExportGenesis implements the appmodule.HasGenesis.ExportGenesis method.
|
||||
func (s Schema) ExportGenesis(ctx context.Context, target appmodule.GenesisTarget) error {
|
||||
func (s Schema) ExportGenesis(ctx context.Context, target core.GenesisTarget) error {
|
||||
for _, name := range s.collectionsOrdered {
|
||||
err := s.exportGenesis(ctx, target, name)
|
||||
if err != nil {
|
||||
@ -269,7 +268,7 @@ func (s Schema) ExportGenesis(ctx context.Context, target appmodule.GenesisTarge
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s Schema) exportGenesis(ctx context.Context, target appmodule.GenesisTarget, name string) error {
|
||||
func (s Schema) exportGenesis(ctx context.Context, target core.GenesisTarget, name string) error {
|
||||
wc, err := target(name)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@ -7,7 +7,7 @@ import (
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
"cosmossdk.io/collections/colltest"
|
||||
coretesting "cosmossdk.io/core/testing"
|
||||
"cosmossdk.io/collections/internal/testutil"
|
||||
)
|
||||
|
||||
func TestTriple(t *testing.T) {
|
||||
@ -19,8 +19,8 @@ func TestTriple(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTripleRange(t *testing.T) {
|
||||
ctx := coretesting.Context()
|
||||
sk := coretesting.KVStoreService(ctx, "test")
|
||||
ctx := testutil.Context()
|
||||
sk := testutil.KVStoreService(ctx, "test")
|
||||
schema := collections.NewSchemaBuilder(sk)
|
||||
// this is a key composed of 3 parts: uint64, string, []byte
|
||||
kc := collections.TripleKeyCodec(collections.Uint64Key, collections.StringKey, collections.BytesKey)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user