Merge remote-tracking branch 'origin/sim-store-decoder' into kd-pricefeed-cleanup

This commit is contained in:
Kevin Davis 2020-04-06 10:00:00 -04:00
commit 174af29582
13 changed files with 454 additions and 50 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,60 @@
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.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,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)
}
})
}
}

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

@ -13,14 +13,14 @@ import (
// DecodeStore unmarshals the KVPair's Value to the corresponding pricefeed type
func DecodeStore(cdc *codec.Codec, kvA, kvB cmn.KVPair) string {
switch {
case bytes.Contains(kvA.Key[:1], []byte(types.CurrentPricePrefix)):
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[:1], []byte(types.RawPriceFeedPrefix)):
var postedPriceA, postedPriceB types.PostedPrices
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)

View File

@ -1,31 +1,20 @@
package simulation_test
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/app"
"github.com/kava-labs/kava/x/pricefeed/simulation"
"github.com/kava-labs/kava/x/pricefeed/types"
"github.com/stretchr/testify/suite"
cmn "github.com/tendermint/tendermint/libs/common"
tmtime "github.com/tendermint/tendermint/types/time"
)
type decoderTest struct {
name string
expectedLog string
}
type DecoderTestSuite struct {
suite.Suite
tests []decoderTest
}
func makeTestCodec() (cdc *codec.Codec) {
cdc = codec.New()
sdk.RegisterCodec(cdc)
@ -34,30 +23,35 @@ func makeTestCodec() (cdc *codec.Codec) {
return
}
func (suite *DecoderTestSuite) TestDecodeStore() {
func TestDecodeDistributionStore(t *testing.T) {
cdc := makeTestCodec()
price := types.NewCurrentPrice("bnb:usd", sdk.MustNewDecFromStr("12.0"))
_, addrs := app.GeneratePrivKeyAddressPairs(1)
rawPrices := types.PostedPrices{
types.NewPostedPrice("bnb:usd", addrs[0], sdk.MustNewDecFromStr("12.0"), tmtime.Now().Add(time.Hour*2)),
}
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: types.CurrentPriceKey("bnb:usd"), Value: cdc.MustMarshalBinaryBare(price)},
cmn.KVPair{Key: types.RawPriceKey("bnb:usd"), Value: cdc.MustMarshalBinaryBare(rawPrices)},
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}},
}
decoderTests := []decoderTest{
decoderTest{"current price", fmt.Sprintf("%s\n%s", price, price)},
decoderTest{"raw prices", fmt.Sprintf("%s\n%s", rawPrices, rawPrices)},
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, t := range decoderTests {
suite.Run(t.name, func() {
suite.Equal(t.expectedLog, simulation.DecodeStore(cdc, kvPairs[i], kvPairs[i]))
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)
}
})
}
}
func TestDecoderTestSuite(t *testing.T) {
suite.Run(t, new(DecoderTestSuite))
}

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/internal/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)
}
})
}
}