Skip to content

Commit

Permalink
reduce vote extension size
Browse files Browse the repository at this point in the history
  • Loading branch information
antstalepresh committed Feb 20, 2024
1 parent e4dbc99 commit bccf555
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 77 deletions.
9 changes: 5 additions & 4 deletions proto/kujira/oracle/extension.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import "tendermint/abci/types.proto";

option go_package = "github.com/Team-Kujira/core/x/oracle/types";

message Decimal {
bytes data = 1;
}

message DenomPrice {
string denom = 1;
string price = 2 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "cosmossdk.io/math.Int"
];
Decimal price = 2 [(gogoproto.nullable) = false];
}

message VoteExtension {
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/abci/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ func (h *ProposalHandler) GetBallotByDenom(ci abci.ExtendedCommitInfo, validator

for _, denomPrice := range voteExt.Prices {
base := denomPrice.Denom
price := math.LegacyNewDecFromBigIntWithPrec(denomPrice.Price.BigInt(), math.LegacyPrecision)
price := denomPrice.Price.ToMathDec()
tmpPower := power
if !price.IsPositive() {
// Make the power of abstain vote zero
Expand Down
42 changes: 22 additions & 20 deletions x/oracle/abci/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ func TestGetBallotByDenom(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25000).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25000)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2200).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2200)),
},
},
}
Expand All @@ -96,11 +96,11 @@ func TestGetBallotByDenom(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25030).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25030)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2180).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2180)),
},
},
}
Expand Down Expand Up @@ -174,11 +174,11 @@ func TestComputeStakeWeightedPricesAndMissMap(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25000).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25000)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2200).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2200)),
},
},
}
Expand All @@ -187,11 +187,11 @@ func TestComputeStakeWeightedPricesAndMissMap(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25030).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25030)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2180).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2180)),
},
},
}
Expand Down Expand Up @@ -463,11 +463,11 @@ func TestPrepareProposal(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25000).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25000)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2200).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2200)),
},
},
}
Expand All @@ -476,11 +476,11 @@ func TestPrepareProposal(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25030).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25030)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2180).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2180)),
},
},
}
Expand Down Expand Up @@ -616,11 +616,11 @@ func TestProcessProposal(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25000).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25000)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2200).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2200)),
},
},
}
Expand All @@ -629,11 +629,11 @@ func TestProcessProposal(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25030).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25030)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2180).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2180)),
},
},
}
Expand Down Expand Up @@ -831,11 +831,11 @@ func TestPreBlocker(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25000).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25000)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2200).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2200)),
},
},
}
Expand All @@ -844,11 +844,11 @@ func TestPreBlocker(t *testing.T) {
Prices: []types.DenomPrice{
{
Denom: "BTC",
Price: math.NewIntFromBigInt(math.LegacyNewDec(25030).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(25030)),
},
{
Denom: "ETH",
Price: math.NewIntFromBigInt(math.LegacyNewDec(2180).BigInt()),
Price: types.NewDecimal(math.LegacyNewDec(2180)),
},
},
}
Expand Down Expand Up @@ -943,8 +943,10 @@ func TestPreBlocker(t *testing.T) {
require.Equal(t, res.ConsensusParamsChanged, false)

ethPrice, err := input.OracleKeeper.GetExchangeRate(input.Ctx, "ETH")
require.NoError(t, err)
require.Equal(t, ethPrice.String(), injectedVoteExtTx.StakeWeightedPrices["ETH"].String())
btcPrice, err := input.OracleKeeper.GetExchangeRate(input.Ctx, "BTC")
require.NoError(t, err)
require.Equal(t, btcPrice.String(), injectedVoteExtTx.StakeWeightedPrices["BTC"].String())
_, err = input.OracleKeeper.GetExchangeRate(input.Ctx, "LTC")
require.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion x/oracle/abci/vote_extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (h *VoteExtHandler) ExtendVoteHandler(oracleConfig OracleConfig) sdk.Extend
for denom, price := range prices.Prices {
computedPrices = append(computedPrices, types.DenomPrice{
Denom: denom,
Price: math.NewIntFromBigInt(price.BigInt()),
Price: types.NewDecimal(price),
})
}

Expand Down
35 changes: 25 additions & 10 deletions x/oracle/abci/vote_extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func TestExtendVoteHandler(t *testing.T) {

prices := make(map[string]math.LegacyDec)
for _, price := range voteExt.Prices {
prices[price.Denom] = math.LegacyNewDecFromBigIntWithPrec(price.Price.BigInt(), math.LegacyPrecision)
prices[price.Denom] = price.Price.ToMathDec()
}
require.Equal(t, prices["BTC"].String(), "47375.706652541026694000")
require.Equal(t, prices["ETH"].String(), "2649.328939436595054949")
require.Equal(t, prices["USDT"].String(), "1.000661260343873178")
require.Equal(t, prices["BTC"].String(), "47375.706600000000000000")
require.Equal(t, prices["ETH"].String(), "2649.328930000000000000")
require.Equal(t, prices["USDT"].String(), "1.000661260000000000")
}

func TestVerifyVoteExtensionHandler(t *testing.T) {
Expand All @@ -76,12 +76,9 @@ func TestVerifyVoteExtensionHandler(t *testing.T) {
Endpoint: testServer.URL,
})

btcPrice, ok := math.NewIntFromString("47375706652541026694000")
require.True(t, ok)
ethPrice, ok := math.NewIntFromString("2649328939436595054949")
require.True(t, ok)
usdtPrice, ok := math.NewIntFromString("1000661260343873178")
require.True(t, ok)
btcPrice := types.NewDecimal(math.LegacyMustNewDecFromStr("47375.706652541026694000"))
ethPrice := types.NewDecimal(math.LegacyMustNewDecFromStr("2649.328939436595054949"))
usdtPrice := types.NewDecimal(math.LegacyMustNewDecFromStr("1.000661260343873178"))
voteExt := types.VoteExtension{
Height: 3,
Prices: []types.DenomPrice{
Expand Down Expand Up @@ -121,3 +118,21 @@ func TestVerifyVoteExtensionHandler(t *testing.T) {
})
require.Error(t, err)
}

func TestVoteExtensionMarshalLength(t *testing.T) {
voteExt := types.VoteExtension{
Height: 1,
Prices: []types.DenomPrice{},
}

denoms := []string{"AKT", "AMPKUJI", "ARB", "ATOM", "AVAX", "AXL", "BAND", "BNB", "BTC", "CRO", "DOT", "DYM", "ETH", "FET", "FTM", "FUZN", "GLMR", "HNT", "INJ", "JUNO", "KAVA", "KUJI", "LINK", "LUNA", "LUNC", "MATIC", "MNTA", "NSTK", "NTRN", "OSMO", "PAXG", "QCKUJI", "QCMNTA", "RIO", "RLB", "SCRT", "SHD", "SOL", "SOMM", "STARS", "STATOM", "STKATOM", "STOSMO", "TIA", "UNI", "USDC", "USK", "WBTC", "WHALE", "WINK", "WSTETH"}
for _, denom := range denoms {
voteExt.Prices = append(voteExt.Prices, types.DenomPrice{
Denom: denom,
Price: types.NewDecimal(math.LegacyOneDec()),
})
}
voteExtBytes, err := proto.Marshal(&voteExt)
require.NoError(t, err)
require.Len(t, voteExtBytes, 868)
}
39 changes: 39 additions & 0 deletions x/oracle/types/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package types

import (
"bytes"
"encoding/binary"
"math/big"

"cosmossdk.io/math"
)

var tenInt = big.NewInt(10)

func (d Decimal) ToMathDec() math.LegacyDec {
var num uint32
err := binary.Read(bytes.NewBuffer(d.Data[:4]), binary.LittleEndian, &num)
if err != nil {
panic(err)
}
var digit = int64(d.Data[4])

Check failure on line 19 in x/oracle/types/extension.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
number := math.LegacyNewDecWithPrec(int64(num), 18)
multiplier := new(big.Int).Exp(tenInt, big.NewInt(digit), nil)
return number.MulInt(math.NewIntFromBigInt(multiplier))
}

func NewDecimal(d math.LegacyDec) Decimal {
number := d.BigInt()
digit := uint32(0)
for number.Cmp(big.NewInt(2_000_000_000)) == 1 {
number = new(big.Int).Div(number, tenInt)
digit += 1

Check warning on line 30 in x/oracle/types/extension.go

View workflow job for this annotation

GitHub Actions / lint

increment-decrement: should replace digit += 1 with digit++ (revive)
}

data := make([]byte, 4)
binary.LittleEndian.PutUint32(data, uint32(number.Uint64()))
data = append(data, byte(digit))
return Decimal{
Data: data,
}
}
Loading

0 comments on commit bccf555

Please sign in to comment.