Skip to content

Commit

Permalink
feat: Compatibility between the voteweighted package and ICS consumer…
Browse files Browse the repository at this point in the history
… keeper (#315)
  • Loading branch information
Eric-Warehime authored Apr 15, 2024
1 parent f06be79 commit eaac82a
Show file tree
Hide file tree
Showing 6 changed files with 274 additions and 23 deletions.
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.22.0

require (
cosmossdk.io/api v0.7.3
cosmossdk.io/client/v2 v2.0.0-20230719143845-dff6b0e26aa4
cosmossdk.io/client/v2 v2.0.0-beta.1
cosmossdk.io/collections v0.4.0
cosmossdk.io/core v0.11.0
cosmossdk.io/depinject v1.0.0-alpha.4
Expand All @@ -25,6 +25,7 @@ require (
github.com/cosmos/cosmos-sdk v0.50.5
github.com/cosmos/gogogateway v1.2.0
github.com/cosmos/gogoproto v1.4.12
github.com/cosmos/interchain-security/v5 v5.0.0-alpha1
github.com/ethereum/go-ethereum v1.13.14
github.com/gagliardetto/binary v0.8.0
github.com/gagliardetto/solana-go v1.10.0
Expand Down Expand Up @@ -117,12 +118,14 @@ require (
github.com/cockroachdb/pebble v1.1.0 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft-db v0.9.1 // indirect
github.com/cometbft/cometbft-db v0.10.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/iavl v1.0.1 // indirect
github.com/cosmos/ibc-go/modules/capability v1.0.0 // indirect
github.com/cosmos/ibc-go/v8 v8.1.0 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
Expand Down Expand Up @@ -190,7 +193,7 @@ require (
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gordonklaus/ineffassign v0.1.0 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/gorilla/handlers v1.5.2 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
Expand Down Expand Up @@ -374,6 +377,7 @@ require (
)

replace (
cosmossdk.io/client/v2 => cosmossdk.io/client/v2 v2.0.0-20230719143845-dff6b0e26aa4
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
29 changes: 20 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ cosmossdk.io/tools/confix v0.1.1 h1:aexyRv9+y15veH3Qw16lxQwo+ki7r2I+g0yNTEFEQM8=
cosmossdk.io/tools/confix v0.1.1/go.mod h1:nQVvP1tHsGXS83PonPVWJtSbddIqyjEw99L4M3rPJyQ=
cosmossdk.io/x/circuit v0.1.0 h1:IAej8aRYeuOMritczqTlljbUVHq1E85CpBqaCTwYgXs=
cosmossdk.io/x/circuit v0.1.0/go.mod h1:YDzblVE8+E+urPYQq5kq5foRY/IzhXovSYXb4nwd39w=
cosmossdk.io/x/evidence v0.1.0 h1:J6OEyDl1rbykksdGynzPKG5R/zm6TacwW2fbLTW4nCk=
cosmossdk.io/x/evidence v0.1.0/go.mod h1:hTaiiXsoiJ3InMz1uptgF0BnGqROllAN8mwisOMMsfw=
cosmossdk.io/x/feegrant v0.1.0 h1:c7s3oAq/8/UO0EiN1H5BIjwVntujVTkYs35YPvvrdQk=
cosmossdk.io/x/feegrant v0.1.0/go.mod h1:4r+FsViJRpcZif/yhTn+E0E6OFfg4n0Lx+6cCtnZElU=
cosmossdk.io/x/tx v0.13.1 h1:Mg+EMp67Pz+NukbJqYxuo8uRp7N/a9uR+oVS9pONtj8=
cosmossdk.io/x/tx v0.13.1/go.mod h1:CBCU6fsRVz23QGFIQBb1DNX2DztJCf3jWyEkHY2nJQ0=
cosmossdk.io/x/upgrade v0.1.1 h1:aoPe2gNvH+Gwt/Pgq3dOxxQVU3j5P6Xf+DaUJTDZATc=
Expand Down Expand Up @@ -411,8 +415,8 @@ github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/cometbft/cometbft v0.38.6 h1:QSgpCzrGWJ2KUq1qpw+FCfASRpE27T6LQbfEHscdyOk=
github.com/cometbft/cometbft v0.38.6/go.mod h1:8rSPxzUJYquCN8uuBgbUHOMg2KAwvr7CyUw+6ukO4nw=
github.com/cometbft/cometbft-db v0.9.1 h1:MIhVX5ja5bXNHF8EYrThkG9F7r9kSfv8BX4LWaxWJ4M=
github.com/cometbft/cometbft-db v0.9.1/go.mod h1:iliyWaoV0mRwBJoizElCwwRA9Tf7jZJOURcRZF9m60U=
github.com/cometbft/cometbft-db v0.10.0 h1:VMBQh88zXn64jXVvj39tlu/IgsGR84T7ImjS523DCiU=
github.com/cometbft/cometbft-db v0.10.0/go.mod h1:7RR7NRv99j7keWJ5IkE9iZibUTKYdtepXTp7Ra0FxKk=
github.com/consensys/bavard v0.1.13 h1:oLhMLOFGTLdlda/kma4VOJazblc7IM5y5QPd2A/YjhQ=
github.com/consensys/bavard v0.1.13/go.mod h1:9ItSMtA/dXMAiL7BG6bqW2m3NdSEObYWoH223nGHukI=
github.com/consensys/gnark-crypto v0.12.1 h1:lHH39WuuFgVHONRl3J0LRBtuYdQTumFSDtJF7HpyG8M=
Expand Down Expand Up @@ -442,8 +446,14 @@ github.com/cosmos/gogoproto v1.4.12 h1:vB6Lbe/rtnYGjQuFxkPiPYiCybqFT8QvLipDZP8Jp
github.com/cosmos/gogoproto v1.4.12/go.mod h1:LnZob1bXRdUoqMMtwYlcR3wjiElmlC+FkjaZRv1/eLY=
github.com/cosmos/iavl v1.0.1 h1:D+mYbcRO2wptYzOM1Hxl9cpmmHU1ZEt9T2Wv5nZTeUw=
github.com/cosmos/iavl v1.0.1/go.mod h1:8xIUkgVvwvVrBu81scdPty+/Dx9GqwHnAvXz4cwF7RY=
github.com/cosmos/ibc-go/modules/capability v1.0.0 h1:r/l++byFtn7jHYa09zlAdSeevo8ci1mVZNO9+V0xsLE=
github.com/cosmos/ibc-go/modules/capability v1.0.0/go.mod h1:D81ZxzjZAe0ZO5ambnvn1qedsFQ8lOwtqicG6liLBco=
github.com/cosmos/ibc-go/v8 v8.1.0 h1:pf1106wl0Cf+p1+FjXzV6odlS9DnqVunPVWCH1Uz+lQ=
github.com/cosmos/ibc-go/v8 v8.1.0/go.mod h1:o1ipS95xpdjqNcB8Drq0eI3Sn4FRLigjll42ec1ECuU=
github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=
github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0=
github.com/cosmos/interchain-security/v5 v5.0.0-alpha1 h1:hDFOAw5mSZzlaiEM7vGPImObaLRbbGiR+vIXfkzW/0Q=
github.com/cosmos/interchain-security/v5 v5.0.0-alpha1/go.mod h1:c4oYjNwdfPKAhxzkwzTkkWROXKeUNPvc4VJHyNWrRU8=
github.com/cosmos/keyring v1.2.0 h1:8C1lBP9xhImmIabyXW4c3vFjjLiBdGCmfLUfeZlV1Yo=
github.com/cosmos/keyring v1.2.0/go.mod h1:fc+wB5KTk9wQ9sDx0kFXB3A0MaeGHM9AwRStKOQ5vOA=
github.com/cosmos/ledger-cosmos-go v0.13.3 h1:7ehuBGuyIytsXbd4MP43mLeoN2LTOEnk5nvue4rK+yM=
Expand Down Expand Up @@ -533,7 +543,6 @@ github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
github.com/felixge/httpsnoop v1.0.1/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/firefart/nonamedreturns v1.0.4 h1:abzI1p7mAEPYuR4A+VLKn4eNDOycjYo2phmY9sfv40Y=
Expand Down Expand Up @@ -808,8 +817,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORR
github.com/gordonklaus/ineffassign v0.1.0 h1:y2Gd/9I7MdY1oEIt+n+rowjBNDcLQq3RsH5hwJd0f9s=
github.com/gordonklaus/ineffassign v0.1.0/go.mod h1:Qcp2HIAYhR7mNUVSIxZww3Guk4it82ghYcEXIAk+QT0=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/handlers v1.5.1 h1:9lRY6j8DEeeBT10CvO9hGW0gmky0BprnvDI5vfhUHH4=
github.com/gorilla/handlers v1.5.1/go.mod h1:t8XrUpc4KVXb7HGyJ4/cEnwQiaxrX/hz1Zv/4g96P1Q=
github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE=
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
Expand Down Expand Up @@ -869,8 +878,9 @@ github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoD
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
Expand Down Expand Up @@ -1125,8 +1135,9 @@ github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA=
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.16.4 h1:29JGrr5oVBm5ulCWet69zQkzWipVXIol6ygQUe/EzNc=
github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
Expand Down Expand Up @@ -1406,8 +1417,8 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/ulikunitz/xz v0.5.10/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8=
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
Expand Down
82 changes: 82 additions & 0 deletions pkg/math/voteweighted/ccv_compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package voteweighted

import (
"context"
"fmt"

cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
ccvtypes "github.com/cosmos/interchain-security/v5/x/ccv/consumer/types"
)

var (
_ ValidatorStore = CCVConsumerCompatKeeper{}
_ stakingtypes.ValidatorI = CCVCompat{}
)

// CCVCompat is used for compatibility between stakingtypes.ValidatorI and CrossChainValidator.
type CCVCompat struct {
stakingtypes.ValidatorI
ccv ccvtypes.CrossChainValidator
}

// GetBondedTokens returns the power of the validator as math.Int.
func (c CCVCompat) GetBondedTokens() math.Int {
return math.NewInt(c.ccv.Power)
}

// CCVConsumerCompatKeeper is used for compatibility between the consumer keeper and the ValidatorStore interface.
type CCVConsumerCompatKeeper struct {
ccvStore CCValidatorStore
}

// NewCCVConsumerCompatKeeper constructs a CCVConsumerCompatKeeper from a consumer keeper.
func NewCCVConsumerCompatKeeper(ccvStore CCValidatorStore) CCVConsumerCompatKeeper {
return CCVConsumerCompatKeeper{
ccvStore: ccvStore,
}
}

// ValidatorByConsAddr returns a compat validator from the consumer keeper.
func (c CCVConsumerCompatKeeper) ValidatorByConsAddr(ctx context.Context, addr sdk.ConsAddress) (stakingtypes.ValidatorI, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
ccv, found := c.ccvStore.GetCCValidator(sdkCtx, addr.Bytes())
if !found {
return nil, fmt.Errorf("could not find validator %s", addr.String())
}
return CCVCompat{ccv: ccv}, nil
}

// TotalBondedTokens iterates through all CCVs and returns the sum of all validator power.
func (c CCVConsumerCompatKeeper) TotalBondedTokens(ctx context.Context) (math.Int, error) {
total := math.NewInt(0)
sdkCtx := sdk.UnwrapSDKContext(ctx)
for _, ccVal := range c.ccvStore.GetAllCCValidator(sdkCtx) {
total = total.Add(math.NewInt(ccVal.Power))
}
return total, nil
}

// GetPubKeyByConsAddr returns the public key of a validator given the consensus addr.
func (c CCVConsumerCompatKeeper) GetPubKeyByConsAddr(ctx context.Context, consAddr sdk.ConsAddress) (cmtprotocrypto.PublicKey, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
val, found := c.ccvStore.GetCCValidator(sdkCtx, consAddr)
if !found {
return cmtprotocrypto.PublicKey{}, fmt.Errorf("not found CCValidator for address: %s", consAddr.String())
}

consPubKey, err := val.ConsPubKey()
if err != nil {
return cmtprotocrypto.PublicKey{}, fmt.Errorf("could not get pubkey for val %s: %w", val.String(), err)
}
tmPubKey, err := cryptocodec.ToCmtProtoPublicKey(consPubKey)
if err != nil {
return cmtprotocrypto.PublicKey{}, err
}

return tmPubKey, nil
}
9 changes: 9 additions & 0 deletions pkg/math/voteweighted/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/interchain-security/v5/x/ccv/consumer/types"
)

// ValidatorStore defines the interface contract required for calculating stake-weighted median
Expand All @@ -17,3 +18,11 @@ type ValidatorStore interface {
ValidatorByConsAddr(ctx context.Context, addr sdk.ConsAddress) (stakingtypes.ValidatorI, error)
TotalBondedTokens(ctx context.Context) (math.Int, error)
}

// CCValidatorStore defines the interface contract required for the cross chain validator consumer store.
//
//go:generate mockery --name CCValidatorStore --filename mock_cc_validator_store.go
type CCValidatorStore interface {
GetAllCCValidator(ctx sdk.Context) []types.CrossChainValidator
GetCCValidator(ctx sdk.Context, addr []byte) (types.CrossChainValidator, bool)
}
90 changes: 79 additions & 11 deletions pkg/math/voteweighted/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math/big"
"testing"

ccvtypes "github.com/cosmos/interchain-security/v5/x/ccv/consumer/types"

"cosmossdk.io/log"
sdkmath "cosmossdk.io/math"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
Expand Down Expand Up @@ -54,9 +56,14 @@ func (s *MathTestSuite) TestMedian() {
expectedPrices map[slinkytypes.CurrencyPair]*big.Int
}{
{
name: "no providers",
providerPrices: aggregator.AggregatedProviderData[string, map[slinkytypes.CurrencyPair]*big.Int]{},
validators: []validator{},
name: "no providers",
providerPrices: aggregator.AggregatedProviderData[string, map[slinkytypes.CurrencyPair]*big.Int]{},
validators: []validator{
{
stake: sdkmath.NewInt(100),
consAddr: validator1,
},
},
totalBondedTokens: sdkmath.NewInt(100),
expectedPrices: map[slinkytypes.CurrencyPair]*big.Int{},
},
Expand Down Expand Up @@ -99,6 +106,10 @@ func (s *MathTestSuite) TestMedian() {
stake: sdkmath.NewInt(50),
consAddr: validator1,
},
{
stake: sdkmath.NewInt(50),
consAddr: validator2,
},
},
totalBondedTokens: sdkmath.NewInt(100),
expectedPrices: map[slinkytypes.CurrencyPair]*big.Int{},
Expand All @@ -122,6 +133,10 @@ func (s *MathTestSuite) TestMedian() {
stake: sdkmath.NewInt(68),
consAddr: validator1,
},
{
stake: sdkmath.NewInt(32),
consAddr: validator2,
},
},
totalBondedTokens: sdkmath.NewInt(100),
expectedPrices: map[slinkytypes.CurrencyPair]*big.Int{
Expand Down Expand Up @@ -205,7 +220,7 @@ func (s *MathTestSuite) TestMedian() {
consAddr: validator3,
},
},
totalBondedTokens: sdkmath.NewInt(100),
totalBondedTokens: sdkmath.NewInt(99),
expectedPrices: map[slinkytypes.CurrencyPair]*big.Int{
{
Base: "BTC",
Expand Down Expand Up @@ -257,7 +272,7 @@ func (s *MathTestSuite) TestMedian() {
consAddr: validator3,
},
},
totalBondedTokens: sdkmath.NewInt(100),
totalBondedTokens: sdkmath.NewInt(99),
expectedPrices: map[slinkytypes.CurrencyPair]*big.Int{ // only btc/usd should be included
{
Base: "BTC",
Expand All @@ -271,15 +286,21 @@ func (s *MathTestSuite) TestMedian() {
s.Run(tc.name, func() {
// Create a mock validator store.
mockValidatorStore := s.createMockValidatorStore(tc.validators, tc.totalBondedTokens)
// Also test ICS based val keeper
ccvConsumerCompatKeeper := s.createMockCCVConsumerCompatKeeper(tc.validators)

// Compute the stake weighted median.
aggregateFn := voteweighted.Median(s.ctx, log.NewTestLogger(s.T()), mockValidatorStore, voteweighted.DefaultPowerThreshold)
result := aggregateFn(tc.providerPrices)
// Compute the stake weighted median for both staking keeper based and ICS based val stores.
defaultAggregateFn := voteweighted.Median(s.ctx, log.NewTestLogger(s.T()), mockValidatorStore, voteweighted.DefaultPowerThreshold)
defaultResult := defaultAggregateFn(tc.providerPrices)
ccvAggregateFn := voteweighted.Median(s.ctx, log.NewTestLogger(s.T()), ccvConsumerCompatKeeper, voteweighted.DefaultPowerThreshold)
ccvResult := ccvAggregateFn(tc.providerPrices)

// Verify the result.
s.Require().Len(result, len(tc.expectedPrices))
// Verify the results.
s.Require().Len(defaultResult, len(tc.expectedPrices))
s.Require().Len(ccvResult, len(tc.expectedPrices))
for currencyPair, expectedPrice := range tc.expectedPrices {
s.Require().Equal(expectedPrice, result[currencyPair])
s.Require().Equal(expectedPrice, defaultResult[currencyPair])
s.Require().Equal(expectedPrice, ccvResult[currencyPair])
}
})
}
Expand Down Expand Up @@ -438,3 +459,50 @@ func (s *MathTestSuite) createMockValidatorStore(

return store
}

func (s *MathTestSuite) createMockCCVConsumerCompatKeeper(
validators []validator,
) voteweighted.CCVConsumerCompatKeeper {
valStore := mocks.NewCCValidatorStore(s.T())
ccvCompatKeeper := voteweighted.NewCCVConsumerCompatKeeper(valStore)
mockVals := make([]ccvtypes.CrossChainValidator, len(validators))
if len(validators) != 0 {
for i, val := range validators {
valPubKey := ed25519.GenPrivKey().PubKey()
ccVal, err := ccvtypes.NewCCValidator(
validators[i].consAddr,
validators[i].stake.Int64(),
valPubKey,
)
if err != nil {
panic(err)
}
mockVals[i] = ccVal

valStore.On(
"GetCCValidator",
s.ctx,
val.consAddr.Bytes(),
).Return(
ccVal,
true,
).Maybe()
valStore.On(
"GetPubKeyByConsAddr",
s.ctx,
val.consAddr,
).Return(
valPubKey,
nil,
).Maybe()
}
}
valStore.On(
"GetAllCCValidator",
s.ctx,
).Return(
mockVals,
)

return ccvCompatKeeper
}
Loading

0 comments on commit eaac82a

Please sign in to comment.