Skip to content

Commit

Permalink
Merge pull request #61 from renproject/revert/cosmos-gas-estimation
Browse files Browse the repository at this point in the history
chain/cosmos: revert gas estimation change
  • Loading branch information
jazg authored Nov 10, 2020
2 parents 27dda1a + 75d22b8 commit a9c2e46
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 35 deletions.
31 changes: 10 additions & 21 deletions chain/cosmos/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,26 @@ import (
"github.com/renproject/pack"
)

const (
microToPico = 1000000
)

// A GasEstimator returns the gas price that is needed in order to confirm
// transactions with an estimated maximum delay of one block. As of now, Cosmos
// compatible chains do not support transaction prioritisation in the mempool.
// Hence we use constant gas price set in the micro-denomination (1e-6) of the
// underlying token. However, the gas price as returned by the gas estimation
// API is a 32-byte unsigned integer that represents the gas price in the
// pico-denomination (1e-12).
// A GasEstimator returns the gas-per-byte that is needed in order to confirm
// transactions with an estimated maximum delay of one block. In distributed
// networks that collectively build, sign, and submit transactions, it is
// important that all nodes in the network have reached consensus on the
// gas-per-byte.
type GasEstimator struct {
gasPrice float64
gasPerByte pack.U256
}

// NewGasEstimator returns a simple gas estimator that always returns the same
// amount of gas-per-byte.
func NewGasEstimator(gasPrice float64) gas.Estimator {
func NewGasEstimator(gasPerByte pack.U256) gas.Estimator {
return &GasEstimator{
gasPrice: gasPrice,
gasPerByte: gasPerByte,
}
}

// EstimateGas returns gas required per byte for Cosmos-compatible chains. This
// value is used for both the price and cap, because Cosmos-compatible chains do
// not have a distinct concept of cap. As of now, Cosmos compatible chains do
// not support transaction prioritisation in the mempool. Hence we use constant
// gas price set in the micro-denomination (1e-6) of the underlying token.
// However, the gas price as returned by the gas estimation API is a 32-byte
// unsigned integer representing gas price in the pico-denomination (1e-12).
// not have a distinct concept of cap.
func (gasEstimator *GasEstimator) EstimateGas(ctx context.Context) (pack.U256, pack.U256, error) {
gasPrice := pack.NewU256FromUint64(uint64(gasEstimator.gasPrice * microToPico))
return gasPrice, gasPrice, nil
return gasEstimator.gasPerByte, gasEstimator.gasPerByte, nil
}
13 changes: 4 additions & 9 deletions chain/cosmos/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package cosmos_test

import (
"context"
"math/rand"
"testing/quick"
"time"

"github.com/renproject/multichain/chain/cosmos"
"github.com/renproject/pack"
Expand All @@ -14,16 +12,13 @@ import (
)

var _ = Describe("Gas", func() {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
Context("when estimating gas parameters", func() {
It("should work", func() {
f := func() bool {
gasPriceMicro := r.Float64()
gasEstimator := cosmos.NewGasEstimator(gasPriceMicro)
gasPricePico, _, err := gasEstimator.EstimateGas(context.Background())
f := func(gasPerByte pack.U256) bool {
gasEstimator := cosmos.NewGasEstimator(gasPerByte)
gasPrice, _, err := gasEstimator.EstimateGas(context.Background())
Expect(err).NotTo(HaveOccurred())
expectedGasPrice := pack.NewU256FromUint64(uint64(gasPriceMicro * 1000000))
Expect(gasPricePico).To(Equal(expectedGasPrice))
Expect(gasPrice).To(Equal(gasPerByte))
return true
}
Expect(quick.Check(f, nil)).To(Succeed())
Expand Down
5 changes: 2 additions & 3 deletions chain/cosmos/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ func NewTxBuilder(options TxBuilderOptions, client *Client) account.TxBuilder {

// BuildTx consumes a list of MsgSend to build and return a cosmos transaction.
// This transaction is unsigned, and must be signed before submitting to the
// cosmos chain. Note that the gas price is represented in the pico-denomination
// (1e-12) and must be converted back to the micro-denomination (1e-6).
// cosmos chain.
func (builder txBuilder) BuildTx(ctx context.Context, from, to address.Address, value, nonce, gasLimit, gasPrice, gasCap pack.U256, payload pack.Bytes) (account.Tx, error) {
types.GetConfig().SetBech32PrefixForAccount(builder.client.hrp, builder.client.hrp+"pub")

Expand All @@ -91,7 +90,7 @@ func (builder txBuilder) BuildTx(ctx context.Context, from, to address.Address,

fees := Coins{Coin{
Denom: builder.client.opts.CoinDenom,
Amount: pack.NewU64(gasPrice.Mul(gasLimit).Div(pack.NewU256FromUint64(uint64(microToPico))).Int().Uint64()),
Amount: pack.NewU64(gasPrice.Mul(gasLimit).Int().Uint64()),
}}

accountNumber, err := builder.client.AccountNumber(ctx, from)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.14
require (
github.com/btcsuite/btcd v0.21.0-beta
github.com/btcsuite/btcutil v1.0.2
github.com/centrifuge/go-substrate-rpc-client v1.1.0
github.com/codahale/blake2 v0.0.0-20150924215134-8d10d0420cbf
github.com/cosmos/cosmos-sdk v0.39.1
github.com/ethereum/go-ethereum v1.9.20
Expand Down
Loading

0 comments on commit a9c2e46

Please sign in to comment.