forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: collections out of sync with released version (#567)
* Problem: collections out of sync with released version Solution: - sync with collections/v0.4.0, prepare for hacking. * replace collections * sync core to v0.11.0 * tidy
- Loading branch information
Showing
34 changed files
with
1,132 additions
and
216 deletions.
There are no files selected for viewing
This file contains 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 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 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,47 @@ | ||
package codec | ||
|
||
// NewAltValueCodec returns a new AltValueCodec. canonicalValueCodec is the codec that you want the value | ||
// to be encoded and decoded as, alternativeDecoder is a function that will attempt to decode the value | ||
// in case the canonicalValueCodec fails to decode it. | ||
func NewAltValueCodec[V any](canonicalValueCodec ValueCodec[V], alternativeDecoder func([]byte) (V, error)) ValueCodec[V] { | ||
return AltValueCodec[V]{ | ||
canonicalValueCodec: canonicalValueCodec, | ||
alternativeDecoder: alternativeDecoder, | ||
} | ||
} | ||
|
||
// AltValueCodec is a codec that can decode a value from state in an alternative format. | ||
// This is useful for migrating data from one format to another. For example, in x/bank | ||
// balances were initially encoded as sdk.Coin, now they are encoded as math.Int. | ||
// The AltValueCodec will be trying to decode the value as math.Int, and if that fails, | ||
// it will attempt to decode it as sdk.Coin. | ||
// NOTE: if the canonical format can also decode the alternative format, then this codec | ||
// will produce undefined and undesirable behavior. | ||
type AltValueCodec[V any] struct { | ||
canonicalValueCodec ValueCodec[V] | ||
alternativeDecoder func([]byte) (V, error) | ||
} | ||
|
||
// Decode will attempt to decode the value from state using the canonical value codec. | ||
// If it fails to decode, it will attempt to decode the value using the alternative decoder. | ||
func (a AltValueCodec[V]) Decode(b []byte) (V, error) { | ||
v, err := a.canonicalValueCodec.Decode(b) | ||
if err != nil { | ||
return a.alternativeDecoder(b) | ||
} | ||
return v, nil | ||
} | ||
|
||
// Below there is the implementation of ValueCodec relying on the canonical value codec. | ||
|
||
func (a AltValueCodec[V]) Encode(value V) ([]byte, error) { return a.canonicalValueCodec.Encode(value) } | ||
|
||
func (a AltValueCodec[V]) EncodeJSON(value V) ([]byte, error) { | ||
return a.canonicalValueCodec.EncodeJSON(value) | ||
} | ||
|
||
func (a AltValueCodec[V]) DecodeJSON(b []byte) (V, error) { return a.canonicalValueCodec.DecodeJSON(b) } | ||
|
||
func (a AltValueCodec[V]) Stringify(value V) string { return a.canonicalValueCodec.Stringify(value) } | ||
|
||
func (a AltValueCodec[V]) ValueType() string { return a.canonicalValueCodec.ValueType() } |
This file contains 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,53 @@ | ||
package codec_test | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"cosmossdk.io/collections/codec" | ||
"cosmossdk.io/collections/colltest" | ||
) | ||
|
||
type altValue struct { | ||
Value uint64 `json:"value"` | ||
} | ||
|
||
func TestAltValueCodec(t *testing.T) { | ||
// we assume we want to migrate the value from json(altValue) to just be | ||
// the raw value uint64. | ||
canonical := codec.KeyToValueCodec(codec.NewUint64Key[uint64]()) | ||
alternative := func(v []byte) (uint64, error) { | ||
var alt altValue | ||
err := json.Unmarshal(v, &alt) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return alt.Value, nil | ||
} | ||
|
||
cdc := codec.NewAltValueCodec(canonical, alternative) | ||
|
||
t.Run("decodes alternative value", func(t *testing.T) { | ||
expected := uint64(100) | ||
alternativeEncodedBytes, err := json.Marshal(altValue{Value: expected}) | ||
require.NoError(t, err) | ||
got, err := cdc.Decode(alternativeEncodedBytes) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, got) | ||
}) | ||
|
||
t.Run("decodes canonical value", func(t *testing.T) { | ||
expected := uint64(100) | ||
canonicalEncodedBytes, err := cdc.Encode(expected) | ||
require.NoError(t, err) | ||
got, err := cdc.Decode(canonicalEncodedBytes) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, got) | ||
}) | ||
|
||
t.Run("conformance", func(t *testing.T) { | ||
colltest.TestValueCodec(t, cdc, uint64(100)) | ||
}) | ||
} |
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,53 +1,55 @@ | ||
module cosmossdk.io/collections | ||
|
||
go 1.21 | ||
go 1.20 | ||
|
||
require ( | ||
cosmossdk.io/core v0.11.0 | ||
github.com/cosmos/cosmos-db v1.0.2 | ||
github.com/stretchr/testify v1.9.0 | ||
cosmossdk.io/core v0.10.0 | ||
github.com/cosmos/cosmos-db v1.0.0 | ||
github.com/stretchr/testify v1.8.4 | ||
pgregory.net/rapid v1.1.0 | ||
) | ||
|
||
require ( | ||
cosmossdk.io/api v0.7.5 // indirect | ||
cosmossdk.io/api v0.7.0 // indirect | ||
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect | ||
github.com/DataDog/zstd v1.5.5 // indirect | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/cockroachdb/errors v1.11.1 // indirect | ||
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect | ||
github.com/cockroachdb/pebble v1.1.0 // indirect | ||
github.com/cockroachdb/pebble v0.0.0-20230525220056-bb4fc9527b3b // indirect | ||
github.com/cockroachdb/redact v1.1.5 // indirect | ||
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect | ||
github.com/cosmos/cosmos-proto v1.0.0-beta.5 // indirect | ||
github.com/cosmos/cosmos-proto v1.0.0-beta.3 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/fsnotify/fsnotify v1.6.0 // indirect | ||
github.com/getsentry/sentry-go v0.27.0 // indirect | ||
github.com/getsentry/sentry-go v0.23.0 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.4 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/golang/snappy v0.0.4 // indirect | ||
github.com/google/btree v1.1.2 // indirect | ||
github.com/klauspost/compress v1.17.7 // indirect | ||
github.com/klauspost/compress v1.16.5 // indirect | ||
github.com/kr/pretty v0.3.1 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/linxGnu/grocksdb v1.8.12 // indirect | ||
github.com/linxGnu/grocksdb v1.7.16 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect | ||
github.com/onsi/gomega v1.20.0 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/prometheus/client_golang v1.18.0 // indirect | ||
github.com/prometheus/client_model v0.6.0 // indirect | ||
github.com/prometheus/common v0.47.0 // indirect | ||
github.com/prometheus/procfs v0.12.0 // indirect | ||
github.com/rogpeppe/go-internal v1.12.0 // indirect | ||
github.com/spf13/cast v1.6.0 // indirect | ||
github.com/prometheus/client_golang v1.16.0 // indirect | ||
github.com/prometheus/client_model v0.4.0 // indirect | ||
github.com/prometheus/common v0.44.0 // indirect | ||
github.com/prometheus/procfs v0.10.1 // indirect | ||
github.com/rogpeppe/go-internal v1.11.0 // indirect | ||
github.com/spf13/cast v1.5.1 // indirect | ||
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect | ||
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect | ||
golang.org/x/net v0.20.0 // indirect | ||
golang.org/x/sys v0.17.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect | ||
google.golang.org/grpc v1.58.3 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect | ||
golang.org/x/net v0.15.0 // indirect | ||
golang.org/x/sys v0.12.0 // indirect | ||
golang.org/x/text v0.13.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect | ||
google.golang.org/grpc v1.58.0 // indirect | ||
google.golang.org/protobuf v1.31.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) | ||
|
||
replace cosmossdk.io/core => ../core |
Oops, something went wrong.