mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-26 08:15:19 +00:00
decoder tests
This commit is contained in:
parent
9817a10ca6
commit
3f4aba1c7f
58
x/auction/simulation/decoder_test.go
Normal file
58
x/auction/simulation/decoder_test.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package simulation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/auction/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func makeTestCodec() (cdc *codec.Codec) {
|
||||||
|
cdc = codec.New()
|
||||||
|
sdk.RegisterCodec(cdc)
|
||||||
|
types.RegisterCodec(cdc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeDistributionStore(t *testing.T) {
|
||||||
|
cdc := makeTestCodec()
|
||||||
|
|
||||||
|
oneCoin := sdk.NewCoin("coin", sdk.OneInt())
|
||||||
|
auction := types.NewSurplusAuction("me", oneCoin, "coin", time.Now().UTC())
|
||||||
|
|
||||||
|
kvPairs := cmn.KVPairs{
|
||||||
|
cmn.KVPair{Key: types.AuctionKeyPrefix, Value: cdc.MustMarshalBinaryLengthPrefixed(&auction)},
|
||||||
|
cmn.KVPair{Key: types.AuctionByTimeKeyPrefix, Value: sdk.Uint64ToBigEndian(2)},
|
||||||
|
cmn.KVPair{Key: types.NextAuctionIDKey, Value: sdk.Uint64ToBigEndian(10)},
|
||||||
|
cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
expectedLog string
|
||||||
|
}{
|
||||||
|
{"Auction", fmt.Sprintf("%v\n%v", auction, auction)},
|
||||||
|
{"AuctionByTime", "2\n2"},
|
||||||
|
{"NextAuctionI", "10\n10"},
|
||||||
|
{"other", ""},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
i, tt := i, tt
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
switch i {
|
||||||
|
case len(tests) - 1:
|
||||||
|
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
|
||||||
|
default:
|
||||||
|
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
61
x/bep3/simulation/decoder_test.go
Normal file
61
x/bep3/simulation/decoder_test.go
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package simulation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/bep3/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func makeTestCodec() (cdc *codec.Codec) {
|
||||||
|
cdc = codec.New()
|
||||||
|
sdk.RegisterCodec(cdc)
|
||||||
|
types.RegisterCodec(cdc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeDistributionStore(t *testing.T) {
|
||||||
|
cdc := makeTestCodec()
|
||||||
|
|
||||||
|
oneCoin := sdk.NewCoin("coin", sdk.OneInt())
|
||||||
|
swap := types.NewAtomicSwap(sdk.Coins{oneCoin}, nil, 10, 100, nil, nil, "otherChainSender", "otherChainRec", 200, types.Completed, true, types.Outgoing)
|
||||||
|
supply := types.AssetSupply{Denom: "coin", IncomingSupply: oneCoin, OutgoingSupply: oneCoin, CurrentSupply: oneCoin, Limit: oneCoin}
|
||||||
|
bz := cmn.HexBytes([]byte{1, 2})
|
||||||
|
|
||||||
|
kvPairs := cmn.KVPairs{
|
||||||
|
cmn.KVPair{Key: types.AtomicSwapKeyPrefix, Value: cdc.MustMarshalBinaryLengthPrefixed(swap)},
|
||||||
|
cmn.KVPair{Key: types.AssetSupplyKeyPrefix, Value: cdc.MustMarshalBinaryLengthPrefixed(supply)},
|
||||||
|
cmn.KVPair{Key: types.AtomicSwapByBlockPrefix, Value: bz},
|
||||||
|
cmn.KVPair{Key: types.AtomicSwapByBlockPrefix, Value: bz},
|
||||||
|
cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
expectedLog string
|
||||||
|
}{
|
||||||
|
{"AtomicSwap", fmt.Sprintf("%v\n%v", swap, swap)},
|
||||||
|
{"AssetSupply", fmt.Sprintf("%v\n%v", supply, supply)},
|
||||||
|
{"AtomicSwapByBlock", fmt.Sprintf("%s\n%s", bz, bz)},
|
||||||
|
{"AtomicSwapLongtermStorage", fmt.Sprintf("%s\n%s", bz, bz)},
|
||||||
|
{"other", ""},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
i, tt := i, tt
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
switch i {
|
||||||
|
case len(tests) - 1:
|
||||||
|
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
|
||||||
|
default:
|
||||||
|
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
72
x/cdp/simulation/decoder_test.go
Normal file
72
x/cdp/simulation/decoder_test.go
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package simulation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/cdp/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func makeTestCodec() (cdc *codec.Codec) {
|
||||||
|
cdc = codec.New()
|
||||||
|
sdk.RegisterCodec(cdc)
|
||||||
|
codec.RegisterCrypto(cdc)
|
||||||
|
types.RegisterCodec(cdc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeDistributionStore(t *testing.T) {
|
||||||
|
cdc := makeTestCodec()
|
||||||
|
|
||||||
|
cdpIds := []uint64{1, 2, 3, 4, 5}
|
||||||
|
denom := "denom"
|
||||||
|
deposit := types.Deposit{CdpID: 1, Amount: sdk.NewCoins(sdk.NewCoin(denom, sdk.OneInt()))}
|
||||||
|
principal := sdk.OneInt()
|
||||||
|
prevDistTime := time.Now().UTC()
|
||||||
|
|
||||||
|
kvPairs := cmn.KVPairs{
|
||||||
|
cmn.KVPair{Key: types.CdpIDKeyPrefix, Value: cdc.MustMarshalBinaryLengthPrefixed(cdpIds)},
|
||||||
|
cmn.KVPair{Key: types.CdpIDKey, Value: sdk.Uint64ToBigEndian(2)},
|
||||||
|
cmn.KVPair{Key: types.CollateralRatioIndexPrefix, Value: sdk.Uint64ToBigEndian(10)},
|
||||||
|
cmn.KVPair{Key: []byte(types.DebtDenomKey), Value: cdc.MustMarshalBinaryLengthPrefixed(denom)},
|
||||||
|
cmn.KVPair{Key: []byte(types.GovDenomKey), Value: cdc.MustMarshalBinaryLengthPrefixed(denom)},
|
||||||
|
cmn.KVPair{Key: []byte(types.DepositKeyPrefix), Value: cdc.MustMarshalBinaryLengthPrefixed(deposit)},
|
||||||
|
cmn.KVPair{Key: []byte(types.PrincipalKeyPrefix), Value: cdc.MustMarshalBinaryLengthPrefixed(principal)},
|
||||||
|
cmn.KVPair{Key: []byte(types.PreviousBlockTimeKey), Value: cdc.MustMarshalBinaryLengthPrefixed(prevDistTime)},
|
||||||
|
cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
expectedLog string
|
||||||
|
}{
|
||||||
|
{"CdpIDs", fmt.Sprintf("%v\n%v", cdpIds, cdpIds)},
|
||||||
|
{"CdpID", "2\n2"},
|
||||||
|
{"CollateralRatioIndex", "10\n10"},
|
||||||
|
{"DebtDenom", fmt.Sprintf("%s\n%s", denom, denom)},
|
||||||
|
{"GovDenom", fmt.Sprintf("%s\n%s", denom, denom)},
|
||||||
|
{"DepositKeyPrefix", fmt.Sprintf("%v\n%v", deposit, deposit)},
|
||||||
|
{"Principal", fmt.Sprintf("%v\n%v", principal, principal)},
|
||||||
|
{"PreviousDistributionTime", fmt.Sprintf("%s\n%s", prevDistTime, prevDistTime)},
|
||||||
|
{"other", ""},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
i, tt := i, tt
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
switch i {
|
||||||
|
case len(tests) - 1:
|
||||||
|
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
|
||||||
|
default:
|
||||||
|
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
54
x/kavadist/simulation/decoder_test.go
Normal file
54
x/kavadist/simulation/decoder_test.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package simulation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/kavadist/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func makeTestCodec() (cdc *codec.Codec) {
|
||||||
|
cdc = codec.New()
|
||||||
|
sdk.RegisterCodec(cdc)
|
||||||
|
codec.RegisterCrypto(cdc)
|
||||||
|
types.RegisterCodec(cdc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeDistributionStore(t *testing.T) {
|
||||||
|
cdc := makeTestCodec()
|
||||||
|
|
||||||
|
prevBlockTime := time.Now().UTC()
|
||||||
|
|
||||||
|
kvPairs := cmn.KVPairs{
|
||||||
|
cmn.KVPair{Key: []byte(types.PreviousBlockTimeKey), Value: cdc.MustMarshalBinaryLengthPrefixed(prevBlockTime)},
|
||||||
|
cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
expectedLog string
|
||||||
|
}{
|
||||||
|
{"PreviousBlockTime", fmt.Sprintf("%s\n%s", prevBlockTime, prevBlockTime)},
|
||||||
|
{"other", ""},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
i, tt := i, tt
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
switch i {
|
||||||
|
case len(tests) - 1:
|
||||||
|
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
|
||||||
|
default:
|
||||||
|
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -13,13 +13,13 @@ import (
|
|||||||
// DecodeStore unmarshals the KVPair's Value to the corresponding pricefeed type
|
// DecodeStore unmarshals the KVPair's Value to the corresponding pricefeed type
|
||||||
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
|
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
|
||||||
switch {
|
switch {
|
||||||
case bytes.Contains(kvA.Key[:1], []byte(types.CurrentPricePrefix)):
|
case bytes.Contains(kvA.Key, []byte(types.CurrentPricePrefix)):
|
||||||
var priceA, priceB types.CurrentPrice
|
var priceA, priceB types.CurrentPrice
|
||||||
cdc.MustUnmarshalBinaryBare(kvA.Value, &priceA)
|
cdc.MustUnmarshalBinaryBare(kvA.Value, &priceA)
|
||||||
cdc.MustUnmarshalBinaryBare(kvB.Value, &priceB)
|
cdc.MustUnmarshalBinaryBare(kvB.Value, &priceB)
|
||||||
return fmt.Sprintf("%s\n%s", priceA, priceB)
|
return fmt.Sprintf("%s\n%s", priceA, priceB)
|
||||||
|
|
||||||
case bytes.Contains(kvA.Key[:1], []byte(types.RawPriceFeedPrefix)):
|
case bytes.Contains(kvA.Key, []byte(types.RawPriceFeedPrefix)):
|
||||||
var postedPriceA, postedPriceB types.PostedPrice
|
var postedPriceA, postedPriceB types.PostedPrice
|
||||||
cdc.MustUnmarshalBinaryBare(kvA.Value, &postedPriceA)
|
cdc.MustUnmarshalBinaryBare(kvA.Value, &postedPriceA)
|
||||||
cdc.MustUnmarshalBinaryBare(kvB.Value, &postedPriceB)
|
cdc.MustUnmarshalBinaryBare(kvB.Value, &postedPriceB)
|
||||||
|
57
x/pricefeed/simulation/decoder_test.go
Normal file
57
x/pricefeed/simulation/decoder_test.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package simulation
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
|
cmn "github.com/tendermint/tendermint/libs/common"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
|
"github.com/kava-labs/kava/x/pricefeed/types"
|
||||||
|
)
|
||||||
|
|
||||||
|
func makeTestCodec() (cdc *codec.Codec) {
|
||||||
|
cdc = codec.New()
|
||||||
|
sdk.RegisterCodec(cdc)
|
||||||
|
codec.RegisterCrypto(cdc)
|
||||||
|
types.RegisterCodec(cdc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeDistributionStore(t *testing.T) {
|
||||||
|
cdc := makeTestCodec()
|
||||||
|
|
||||||
|
currentPrice := types.CurrentPrice{MarketID: "current", Price: sdk.OneDec()}
|
||||||
|
postedPrice := types.PostedPrice{MarketID: "posted", Price: sdk.OneDec(), Expiry: time.Now().UTC()}
|
||||||
|
|
||||||
|
kvPairs := cmn.KVPairs{
|
||||||
|
cmn.KVPair{Key: []byte(types.CurrentPricePrefix), Value: cdc.MustMarshalBinaryBare(currentPrice)},
|
||||||
|
cmn.KVPair{Key: []byte(types.RawPriceFeedPrefix), Value: cdc.MustMarshalBinaryBare(postedPrice)},
|
||||||
|
cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}},
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
expectedLog string
|
||||||
|
}{
|
||||||
|
{"CurrentPrice", fmt.Sprintf("%v\n%v", currentPrice, currentPrice)},
|
||||||
|
{"PostedPrice", fmt.Sprintf("%s\n%s", postedPrice, postedPrice)},
|
||||||
|
{"other", ""},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
i, tt := i, tt
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
switch i {
|
||||||
|
case len(tests) - 1:
|
||||||
|
require.Panics(t, func() { DecodeStore(cdc, kvPairs[i], kvPairs[i]) }, tt.name)
|
||||||
|
default:
|
||||||
|
require.Equal(t, tt.expectedLog, DecodeStore(cdc, kvPairs[i], kvPairs[i]), tt.name)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user