Skip to content

Commit d5357cc

Browse files
feat: support fixed gas for vote and prevote transactions (#337)
* feat: support fixed gas for vote and prevote transactions * update config * changelog
1 parent 405a0f2 commit d5357cc

File tree

7 files changed

+56
-25
lines changed

7 files changed

+56
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
4545

4646
## Unreleased
4747

48+
## v2.4.0
49+
50+
- [337](https://github.com/ojo-network/price-feeder/pull/337) feat: support fixed gas for vote and prevote transactions.
51+
52+
## Old
53+
4854
### Improvements
4955
- [48](https://github.com/ojo-network/price-feeder/pull/48) Update goreleaser to have release process for umee price-feeder
5056
- [55](https://github.com/ojo-network/price-feeder/pull/55) Update DockerFile to work in umee's e2e test

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,14 @@ Chain rules for checking the free oracle transactions are:
6262
- must be only prevote or vote
6363
- gas is limited to [`MaxMsgGasUsage`](https://github.com/ojo-network/ojo/blob/main/ante/fee.go#L13) constant.
6464

65-
So, if you don't want to pay for gas, TX must be below `MaxMsgGasUsage`. If you set too much gas (which is what is happening when when you set `gas_adjustment` to 2), then the tx will allocate 2x gas, and hence will go above the free quota, so you would need to attach fee to pay for that gas.
65+
So, if you don't want to pay for gas, TX must be below `MaxMsgGasUsage`. If you set too much gas (which is what is happening when you use high `gas_adjustment`, eg more than 2), then the tx will allocate 2x gas, and hence will go above the free quota, so you would need to attach fee to pay for that gas.
6666
The easiest is to just set constant gas. We recommend 10k below the `MaxMsgGasUsage`.
6767

68-
Note that either `gas_adjustment` or `gas` can be used. Both can not be set.
68+
In the PF config file you can set either:
69+
70+
* `gas_adjustment`
71+
* or `gas_prevote` and `gas_vote` - fixed amount of gas used for `MsgAggregateExchangeRatePrevote` and `MsgAggregateExchangeRateVote` transactions respectively.
72+
6973
## Configuration
7074

7175
### `telemetry`

cmd/price-feeder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ func priceFeederCmdHandler(cmd *cobra.Command, args []string) error {
150150
cfg.Account.Validator,
151151
cfg.RPC.GRPCEndpoint,
152152
cfg.GasAdjustment,
153-
cfg.Gas,
153+
cfg.GasPrevote,
154+
cfg.GasVote,
154155
)
155156
if err != nil {
156157
return err

config/config.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67
"time"
78

89
"github.com/cosmos/cosmos-sdk/telemetry"
@@ -47,7 +48,8 @@ type (
4748
RPC RPC `mapstructure:"rpc" validate:"required,gt=0,dive,required"`
4849
Telemetry telemetry.Config `mapstructure:"telemetry"`
4950
GasAdjustment float64 `mapstructure:"gas_adjustment"`
50-
Gas uint64 `mapstructure:"gas"`
51+
GasVote uint64 `mapstructure:"gas_vote"`
52+
GasPrevote uint64 `mapstructure:"gas_prevote"`
5153
ProviderTimeout string `mapstructure:"provider_timeout"`
5254
ProviderMinOverride bool `mapstructure:"provider_min_override"`
5355
ProviderEndpoints []provider.Endpoint `mapstructure:"provider_endpoints" validate:"dive"`
@@ -169,11 +171,18 @@ func (c Config) validateDeviations() error {
169171
}
170172

171173
func (c Config) validateGas() error {
172-
if c.Gas <= 0 && c.GasAdjustment <= 0 {
173-
return fmt.Errorf("gas or gas adjustment must be set")
174+
var errs []string
175+
if (c.GasPrevote > 0) != (c.GasVote > 0) {
176+
errs = append(errs, "if gas_prevote is set, then gas_vote must be set as well; similarly, if gas_vote is set, then gas_prevote must be set as well")
174177
}
175-
if c.GasAdjustment > 0 && c.Gas > 0 {
176-
return fmt.Errorf("gas and gas adjustment may not both be set")
178+
if c.GasVote <= 0 && c.GasAdjustment <= 0 {
179+
errs = append(errs, "either gas_vote and gas_prevote must be set or gas_adjustment must be set")
180+
}
181+
if c.GasAdjustment > 0 && c.GasVote > 0 {
182+
errs = append(errs, "gas and gas adjustment may not both be set")
183+
}
184+
if len(errs) > 0 {
185+
return errors.New(strings.Join(errs, ". "))
177186
}
178187
return nil
179188
}

oracle/client/client.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ type (
4343
Encoding testutil.TestEncodingConfig
4444
GasPrices string
4545
GasAdjustment float64
46-
Gas uint64
46+
GasPrevote uint64
47+
GasVote uint64
4748
GRPCEndpoint string
4849
KeyringPassphrase string
4950
ChainHeight *ChainHeight
@@ -68,7 +69,8 @@ func NewOracleClient(
6869
validatorAddrString string,
6970
grpcEndpoint string,
7071
gasAdjustment float64,
71-
gas uint64,
72+
gasPrevote uint64,
73+
gasVote uint64,
7274
) (OracleClient, error) {
7375
oracleAddr, err := sdk.AccAddressFromBech32(oracleAddrString)
7476
if err != nil {
@@ -89,7 +91,8 @@ func NewOracleClient(
8991
ValidatorAddrString: validatorAddrString,
9092
Encoding: umeeapp.MakeEncodingConfig(),
9193
GasAdjustment: gasAdjustment,
92-
Gas: gas,
94+
GasPrevote: gasPrevote,
95+
GasVote: gasVote,
9396
GRPCEndpoint: grpcEndpoint,
9497
}
9598

@@ -138,7 +141,7 @@ func (r *passReader) Read(p []byte) (n int, err error) {
138141
// BroadcastTx attempts to broadcast a signed transaction. If it fails, a few re-attempts
139142
// will be made until the transaction succeeds or ultimately times out or fails.
140143
// Ref: https://github.com/terra-money/oracle-feeder/blob/baef2a4a02f57a2ffeaa207932b2e03d7fb0fb25/feeder/src/vote.ts#L230
141-
func (oc OracleClient) BroadcastTx(nextBlockHeight, timeoutHeight int64, msgs ...sdk.Msg) error {
144+
func (oc OracleClient) BroadcastTx(nextBlockHeight, timeoutHeight int64, isPrevote bool, msgs sdk.Msg) error {
142145
maxBlockHeight := nextBlockHeight + timeoutHeight
143146
lastCheckHeight := nextBlockHeight - 1
144147

@@ -147,7 +150,7 @@ func (oc OracleClient) BroadcastTx(nextBlockHeight, timeoutHeight int64, msgs ..
147150
return err
148151
}
149152

150-
factory, err := oc.CreateTxFactory()
153+
factory, err := oc.CreateTxFactory(isPrevote)
151154
if err != nil {
152155
return err
153156
}
@@ -166,7 +169,7 @@ func (oc OracleClient) BroadcastTx(nextBlockHeight, timeoutHeight int64, msgs ..
166169
// set last check height to latest block height
167170
lastCheckHeight = latestBlockHeight
168171

169-
resp, err := BroadcastTx(clientCtx, factory, msgs...)
172+
resp, err := BroadcastTx(clientCtx, factory, msgs)
170173
if resp != nil && resp.Code != 0 {
171174
telemetry.IncrCounter(1, "failure", "tx", "code")
172175
err = fmt.Errorf("invalid response code from tx: %d", resp.Code)
@@ -266,7 +269,7 @@ func (oc OracleClient) CreateClientContext() (client.Context, error) {
266269

267270
// CreateTxFactory creates an SDK Factory instance used for transaction
268271
// generation, signing and broadcasting.
269-
func (oc OracleClient) CreateTxFactory() (tx.Factory, error) {
272+
func (oc OracleClient) CreateTxFactory(isPrevote bool) (tx.Factory, error) {
270273
clientCtx, err := oc.CreateClientContext()
271274
if err != nil {
272275
return tx.Factory{}, err
@@ -283,11 +286,15 @@ func (oc OracleClient) CreateTxFactory() (tx.Factory, error) {
283286
WithSignMode(signing.SignMode_SIGN_MODE_DIRECT).
284287
WithSimulateAndExecute(true), nil
285288
}
289+
gas := oc.GasVote
290+
if isPrevote {
291+
gas = oc.GasVote
292+
}
286293
return tx.Factory{}.
287294
WithAccountRetriever(clientCtx.AccountRetriever).
288295
WithChainID(oc.ChainID).
289296
WithTxConfig(clientCtx.TxConfig).
290-
WithGas(oc.Gas).
297+
WithGas(gas).
291298
WithGasPrices(oc.GasPrices).
292299
WithKeybase(clientCtx.Keyring).
293300
WithSignMode(signing.SignMode_SIGN_MODE_DIRECT).

oracle/oracle.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -557,24 +557,24 @@ func (o *Oracle) tick(ctx context.Context) error {
557557

558558
exchangeRatesStr := GenerateExchangeRatesString(o.prices)
559559
hash := oracletypes.GetAggregateVoteHash(salt, exchangeRatesStr, valAddr)
560-
preVoteMsg := &oracletypes.MsgAggregateExchangeRatePrevote{
561-
Hash: hash.String(), // hash of prices from the oracle
562-
Feeder: o.oracleClient.OracleAddrString,
563-
Validator: valAddr.String(),
564-
}
565-
566560
isPrevoteOnlyTx := o.previousPrevote == nil
561+
567562
if isPrevoteOnlyTx {
568563
// This timeout could be as small as oracleVotePeriod-indexInVotePeriod,
569564
// but we give it some extra time just in case.
570565
//
571566
// Ref : https://github.com/terra-money/oracle-feeder/blob/baef2a4a02f57a2ffeaa207932b2e03d7fb0fb25/feeder/src/vote.ts#L222
567+
preVoteMsg := &oracletypes.MsgAggregateExchangeRatePrevote{
568+
Hash: hash.String(), // hash of prices from the oracle
569+
Feeder: o.oracleClient.OracleAddrString,
570+
Validator: valAddr.String(),
571+
}
572572
o.logger.Info().
573573
Str("hash", hash.String()).
574574
Str("validator", preVoteMsg.Validator).
575575
Str("feeder", preVoteMsg.Feeder).
576576
Msg("broadcasting pre-vote")
577-
if err := o.oracleClient.BroadcastTx(nextBlockHeight, oracleVotePeriod*2, preVoteMsg); err != nil {
577+
if err := o.oracleClient.BroadcastTx(nextBlockHeight, oracleVotePeriod*2, isPrevoteOnlyTx, preVoteMsg); err != nil {
578578
return err
579579
}
580580

@@ -606,6 +606,7 @@ func (o *Oracle) tick(ctx context.Context) error {
606606
if err := o.oracleClient.BroadcastTx(
607607
nextBlockHeight,
608608
oracleVotePeriod-indexInVotePeriod,
609+
isPrevoteOnlyTx,
609610
voteMsg,
610611
); err != nil {
611612
return err

price-feeder.example.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
config_dir = "umee-provider-config"
2-
3-
gas_adjustment = 1.9
42
provider_timeout = "1000000s"
53

4+
gas_prevote = 55000
5+
gas_vote = 160000
6+
# instead of fixed gas settings, gas adjustment can be used:
7+
# gas_adjustment = 1.9
8+
69
[server]
710
listen_addr = "0.0.0.0:7171"
811
read_timeout = "20s"

0 commit comments

Comments
 (0)