simulation decoders for kava modules

simulation decoders for kava modules
This commit is contained in:
Federico Kunze 2020-04-07 11:32:12 -04:00 committed by GitHub
commit 1bdf580a61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 509 additions and 13 deletions

2
go.mod
View File

@ -6,9 +6,7 @@ require (
github.com/btcsuite/btcd v0.20.1-beta // indirect
github.com/cosmos/cosmos-sdk v0.34.4-0.20191010193331-18de630d0ae1
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d // indirect
github.com/gogo/protobuf v1.3.0
github.com/gorilla/mux v1.7.3
github.com/snikch/goodman v0.0.0-20171125024755-10e37e294daa
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.0

View File

@ -1,12 +1,32 @@
package simulation
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/kava-labs/kava/x/auction/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding auction type
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
// TODO implement this
return ""
switch {
case bytes.Equal(kvA.Key[:1], types.AuctionKeyPrefix):
var auctionA, auctionB types.Auction
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &auctionA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &auctionB)
return fmt.Sprintf("%v\n%v", auctionA, auctionB)
case bytes.Equal(kvA.Key[:1], types.AuctionByTimeKeyPrefix),
bytes.Equal(kvA.Key[:1], types.NextAuctionIDKey):
auctionIDA := binary.BigEndian.Uint64(kvA.Value)
auctionIDB := binary.BigEndian.Uint64(kvB.Value)
return fmt.Sprintf("%d\n%d", auctionIDA, auctionIDB)
default:
panic(fmt.Sprintf("invalid %s key prefix %X", types.ModuleName, kvA.Key[:1]))
}
}

View 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)
}
})
}
}

View File

@ -1,12 +1,37 @@
package simulation
import (
"bytes"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/kava-labs/kava/x/bep3/types"
)
// DecodeStore unmarshals the KVPair's Value to the module's corresponding type
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
// TODO implement this
return ""
switch {
case bytes.Equal(kvA.Key[:1], types.AtomicSwapKeyPrefix):
var swapA, swapB types.AtomicSwap
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &swapA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &swapB)
return fmt.Sprintf("%v\n%v", swapA, swapB)
case bytes.Equal(kvA.Key[:1], types.AssetSupplyKeyPrefix):
var supplyA, supplyB types.AssetSupply
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &supplyA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &supplyB)
return fmt.Sprintf("%s\n%s", supplyA, supplyB)
case bytes.Equal(kvA.Key[:1], types.AtomicSwapByBlockPrefix),
bytes.Equal(kvA.Key[:1], types.AtomicSwapLongtermStoragePrefix):
var bytesA cmn.HexBytes = kvA.Value
var bytesB cmn.HexBytes = kvA.Value
return fmt.Sprintf("%s\n%s", bytesA.String(), bytesB.String())
default:
panic(fmt.Sprintf("invalid %s key prefix %X", types.ModuleName, kvA.Key[:1]))
}
}

View 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)
}
})
}
}

View File

@ -1,12 +1,66 @@
package simulation
import (
"bytes"
"encoding/binary"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/kava-labs/kava/x/cdp/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding cdp type
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
// TODO implement this
return ""
switch {
case bytes.Equal(kvA.Key[:1], types.CdpIDKeyPrefix):
var cdpIDsA, cdpIDsB []uint64
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &cdpIDsA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &cdpIDsB)
return fmt.Sprintf("%v\n%v", cdpIDsA, cdpIDsB)
case bytes.Equal(kvA.Key[:1], types.CdpKeyPrefix):
var cdpA, cdpB types.CDP
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &cdpA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &cdpB)
return fmt.Sprintf("%v\n%v", cdpA, cdpB)
case bytes.Equal(kvA.Key[:1], types.CdpIDKey),
bytes.Equal(kvA.Key[:1], types.CollateralRatioIndexPrefix):
idA := binary.BigEndian.Uint64(kvA.Value)
idB := binary.BigEndian.Uint64(kvB.Value)
return fmt.Sprintf("%d\n%d", idA, idB)
case bytes.Equal(kvA.Key[:1], types.DebtDenomKey),
bytes.Equal(kvA.Key[:1], types.GovDenomKey):
var denomA, denomB string
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &denomA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &denomB)
return fmt.Sprintf("%s\n%s", denomA, denomB)
case bytes.Equal(kvA.Key[:1], types.DepositKeyPrefix):
var depositA, depositB types.Deposit
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &depositA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &depositB)
return fmt.Sprintf("%s\n%s", depositA, depositB)
case bytes.Equal(kvA.Key[:1], types.PrincipalKeyPrefix):
var totalA, totalB sdk.Int
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &totalA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &totalB)
return fmt.Sprintf("%s\n%s", totalA, totalB)
case bytes.Equal(kvA.Key[:1], types.PreviousBlockTimeKey),
bytes.Equal(kvA.Key[:1], types.PreviousDistributionTimeKey):
var timeA, timeB time.Time
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &timeA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &timeB)
return fmt.Sprintf("%s\n%s", timeA, timeB)
default:
panic(fmt.Sprintf("invalid %s key prefix %X", types.ModuleName, kvA.Key[:1]))
}
}

View File

@ -0,0 +1,76 @@
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"
oneCoins := sdk.NewCoins(sdk.NewCoin(denom, sdk.OneInt()))
deposit := types.Deposit{CdpID: 1, Amount: oneCoins}
principal := sdk.OneInt()
prevDistTime := time.Now().UTC()
cdp := types.CDP{ID: 1, FeesUpdated: prevDistTime, Collateral: oneCoins, Principal: oneCoins, AccumulatedFees: oneCoins}
kvPairs := cmn.KVPairs{
cmn.KVPair{Key: types.CdpIDKeyPrefix, Value: cdc.MustMarshalBinaryLengthPrefixed(cdpIds)},
cmn.KVPair{Key: types.CdpKeyPrefix, Value: cdc.MustMarshalBinaryLengthPrefixed(cdp)},
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)},
{"CDP", fmt.Sprintf("%v\n%v", cdp, cdp)},
{"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)
}
})
}
}

View File

@ -1,12 +1,26 @@
package simulation
import (
"bytes"
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/kava-labs/kava/x/kavadist/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding cdp type
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
// TODO implement this
return ""
switch {
case bytes.Equal(kvA.Key[:1], types.PreviousBlockTimeKey):
var timeA, timeB time.Time
cdc.MustUnmarshalBinaryLengthPrefixed(kvA.Value, &timeA)
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &timeB)
return fmt.Sprintf("%s\n%s", timeA, timeB)
default:
panic(fmt.Sprintf("invalid %s key prefix %X", types.ModuleName, kvA.Key[:1]))
}
}

View 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)
}
})
}
}

View File

@ -1,12 +1,31 @@
package simulation
import (
"bytes"
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/kava-labs/kava/x/pricefeed/types"
)
// DecodeStore unmarshals the KVPair's Value to the corresponding pricefeed type
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
// TODO implement this
return ""
switch {
case bytes.Contains(kvA.Key, []byte(types.CurrentPricePrefix)):
var priceA, priceB types.CurrentPrice
cdc.MustUnmarshalBinaryBare(kvA.Value, &priceA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &priceB)
return fmt.Sprintf("%s\n%s", priceA, priceB)
case bytes.Contains(kvA.Key, []byte(types.RawPriceFeedPrefix)):
var postedPriceA, postedPriceB []types.PostedPrice
cdc.MustUnmarshalBinaryBare(kvA.Value, &postedPriceA)
cdc.MustUnmarshalBinaryBare(kvB.Value, &postedPriceB)
return fmt.Sprintf("%s\n%s", postedPriceA, postedPriceB)
default:
panic(fmt.Sprintf("invalid %s key prefix %X", types.ModuleName, kvA.Key[:1]))
}
}

View 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)
}
})
}
}

View File

@ -26,6 +26,6 @@ func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
cdc.MustUnmarshalBinaryLengthPrefixed(kvB.Value, &btB)
return fmt.Sprintf("%v\n%v", btA, btB)
default:
panic(fmt.Sprintf("invalid account key %X", kvA.Key))
panic(fmt.Sprintf("invalid %s key %X", types.ModuleName, kvA.Key))
}
}

View File

@ -0,0 +1,60 @@
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/cosmos/cosmos-sdk/x/auth"
"github.com/kava-labs/kava/x/validator-vesting/types"
)
func makeTestCodec() (cdc *codec.Codec) {
cdc = codec.New()
sdk.RegisterCodec(cdc)
auth.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
types.RegisterCodec(cdc)
codec.RegisterEvidences(cdc)
return
}
func TestDecodeDistributionStore(t *testing.T) {
cdc := makeTestCodec()
acc := types.ValidatorVestingAccount{SigningThreshold: 1}
now := time.Now().UTC()
kvPairs := cmn.KVPairs{
cmn.KVPair{Key: types.ValidatorVestingAccountPrefix, Value: cdc.MustMarshalBinaryBare(acc)},
cmn.KVPair{Key: types.BlocktimeKey, Value: cdc.MustMarshalBinaryLengthPrefixed(now)},
cmn.KVPair{Key: []byte{0x99}, Value: []byte{0x99}},
}
tests := []struct {
name string
expectedLog string
}{
{"ValidatorVestingAccount", fmt.Sprintf("%v\n%v", acc, acc)},
{"BlockTime", fmt.Sprintf("%s\n%s", now, now)},
{"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)
}
})
}
}