refactor(bank): use collections for state management (#15293)
Co-authored-by: testinginprod <testinginprod@somewhere.idk>
This commit is contained in:
co-authored by
testinginprod
parent
138e0e1d3c
commit
2b7a1102ed
@@ -1,10 +1,47 @@
|
||||
package codec
|
||||
|
||||
import (
|
||||
gogotypes "github.com/cosmos/gogoproto/types"
|
||||
|
||||
"cosmossdk.io/collections"
|
||||
collcodec "cosmossdk.io/collections/codec"
|
||||
|
||||
"github.com/cosmos/gogoproto/proto"
|
||||
)
|
||||
|
||||
// BoolValue implements a ValueCodec that saves the bool value
|
||||
// as if it was a prototypes.BoolValue. Required for backwards
|
||||
// compatibility of state.
|
||||
var BoolValue collcodec.ValueCodec[bool] = boolValue{}
|
||||
|
||||
type boolValue struct{}
|
||||
|
||||
func (boolValue) Encode(value bool) ([]byte, error) {
|
||||
return (&gogotypes.BoolValue{Value: value}).Marshal()
|
||||
}
|
||||
|
||||
func (boolValue) Decode(b []byte) (bool, error) {
|
||||
v := new(gogotypes.BoolValue)
|
||||
err := v.Unmarshal(b)
|
||||
return v.Value, err
|
||||
}
|
||||
|
||||
func (boolValue) EncodeJSON(value bool) ([]byte, error) {
|
||||
return collections.BoolValue.EncodeJSON(value)
|
||||
}
|
||||
|
||||
func (boolValue) DecodeJSON(b []byte) (bool, error) {
|
||||
return collections.BoolValue.DecodeJSON(b)
|
||||
}
|
||||
|
||||
func (boolValue) Stringify(value bool) string {
|
||||
return collections.BoolValue.Stringify(value)
|
||||
}
|
||||
|
||||
func (boolValue) ValueType() string {
|
||||
return "protobuf/bool"
|
||||
}
|
||||
|
||||
type protoMessage[T any] interface {
|
||||
*T
|
||||
proto.Message
|
||||
|
||||
@@ -3,16 +3,35 @@ package codec
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"cosmossdk.io/collections/colltest"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
"github.com/cosmos/gogoproto/types"
|
||||
gogotypes "github.com/cosmos/gogoproto/types"
|
||||
)
|
||||
|
||||
func TestCollectionsCorrectness(t *testing.T) {
|
||||
cdc := NewProtoCodec(codectypes.NewInterfaceRegistry())
|
||||
t.Run("CollValue", func(t *testing.T) {
|
||||
colltest.TestValueCodec(t, CollValue[types.UInt64Value](cdc), types.UInt64Value{
|
||||
colltest.TestValueCodec(t, CollValue[gogotypes.UInt64Value](cdc), gogotypes.UInt64Value{
|
||||
Value: 500,
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("BoolValue", func(t *testing.T) {
|
||||
colltest.TestValueCodec(t, BoolValue, true)
|
||||
colltest.TestValueCodec(t, BoolValue, false)
|
||||
|
||||
// asserts produced bytes are equal
|
||||
valueAssert := func(b bool) {
|
||||
wantBytes, err := (&gogotypes.BoolValue{Value: b}).Marshal()
|
||||
require.NoError(t, err)
|
||||
gotBytes, err := BoolValue.Encode(b)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, wantBytes, gotBytes)
|
||||
}
|
||||
|
||||
valueAssert(true)
|
||||
valueAssert(false)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user