Skip to content

Commit

Permalink
PRT-169 Assign tx factory with modified gas (lavanet#187)
Browse files Browse the repository at this point in the history
* Assign tx factory with modified gas

* PRT-169 Simulation missing Errors.

* PRT-169 moving vars to global consts

* PRT-169 Adding gas protection

Co-authored-by: Ran Mishael <[email protected]>
  • Loading branch information
kajeagentspi and ranlavanet authored Dec 19, 2022
1 parent bc60d64 commit 2c88407
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
15 changes: 12 additions & 3 deletions relayer/sentry/sentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,10 @@ func (s *Sentry) CompareRelaysAndReportConflict(reply0 *pairingtypes.RelayReply,
msg := conflicttypes.NewMsgDetection(s.Acc, nil, &responseConflict, nil)
s.ClientCtx.SkipConfirm = true
txFactory := tx.NewFactoryCLI(s.ClientCtx, s.cmdFlags).WithChainID("lava")
SimulateAndBroadCastTx(s.ClientCtx, txFactory, msg)
err := SimulateAndBroadCastTx(s.ClientCtx, txFactory, msg)
if err != nil {
utils.LavaFormatError("CompareRelaysAndReportConflict - SimulateAndBroadCastTx Failed", err, nil)
}
// report the conflict
return false
}
Expand Down Expand Up @@ -822,7 +825,10 @@ func (s *Sentry) discrepancyChecker(finalizedBlocksA map[int64]string, consensus
msg := conflicttypes.NewMsgDetection(s.Acc, nil, nil, nil)
s.ClientCtx.SkipConfirm = true
txFactory := tx.NewFactoryCLI(s.ClientCtx, s.cmdFlags).WithChainID("lava")
SimulateAndBroadCastTx(s.ClientCtx, txFactory, msg)
err := SimulateAndBroadCastTx(s.ClientCtx, txFactory, msg)
if err != nil {
return false, utils.LavaFormatError("discrepancyChecker - SimulateAndBroadCastTx Failed", err, nil)
}
// TODO:: should break here? is one enough or search for more?
return true, utils.LavaFormatError("Simulation: reliability discrepancy, different hashes detected for block", nil, &map[string]string{"blockNum": strconv.FormatInt(blockNum, 10), "Hashes": fmt.Sprintf("%s vs %s", blockHash, otherHash), "toIterate": fmt.Sprintf("%v", toIterate), "otherBlocks": fmt.Sprintf("%v", otherBlocks)})
}
Expand Down Expand Up @@ -875,7 +881,10 @@ func (s *Sentry) validateProviderReply(finalizedBlocks map[int64]string, latestB
msg := conflicttypes.NewMsgDetection(s.Acc, nil, nil, nil)
s.ClientCtx.SkipConfirm = true
txFactory := tx.NewFactoryCLI(s.ClientCtx, s.cmdFlags).WithChainID("lava")
SimulateAndBroadCastTx(s.ClientCtx, txFactory, msg)
err := SimulateAndBroadCastTx(s.ClientCtx, txFactory, msg)
if err != nil {
return utils.LavaFormatError("validateProviderReply - SimulateAndBroadCastTx Failed", err, nil)
}

return utils.LavaFormatError("Simulation: Provider supplied an older latest block than it has previously", nil, &map[string]string{
"session.LatestBlock": strconv.FormatInt(session.LatestBlock, 10),
Expand Down
36 changes: 31 additions & 5 deletions relayer/sentry/tx.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package sentry

import (
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/utils"
)

const (
defaultGasPrice = "0.000000001ulava"
defaultGasAdjustment = 1.5
maximumGasAllowed = 5000000
)

func SimulateAndBroadCastTx(clientCtx client.Context, txf tx.Factory, msg sdk.Msg) error {
txf = txf.WithGasPrices("0.000000001ulava")
txf = txf.WithGasAdjustment(1.5)
txf = txf.WithGasPrices(defaultGasPrice)
txf = txf.WithGasAdjustment(defaultGasAdjustment)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand All @@ -24,6 +32,10 @@ func SimulateAndBroadCastTx(clientCtx client.Context, txf tx.Factory, msg sdk.Ms
return err
}

if err := validateGas(gasUsed); err != nil {
return err
}

txf = txf.WithGas(gasUsed)

err = tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg)
Expand Down Expand Up @@ -60,9 +72,19 @@ func prepareFactory(clientCtx client.Context, txf tx.Factory) (tx.Factory, error
return txf, nil
}

func validateGas(gas uint64) error {
if gas > maximumGasAllowed {
return utils.LavaFormatError("SimulateAndBroadCastTx - Maximum gas allowed Reached", nil, &map[string]string{
"maximumGasAllowed": strconv.FormatUint(maximumGasAllowed, 10),
"gasUsed": strconv.FormatUint(gas, 10),
})
}
return nil
}

func CheckProfitabilityAndBroadCastTx(clientCtx client.Context, txf tx.Factory, msg sdk.Msg) error {
txf = txf.WithGasPrices("0.000000001ulava")
txf = txf.WithGasAdjustment(1.5)
txf = txf.WithGasPrices(defaultGasPrice)
txf = txf.WithGasAdjustment(defaultGasAdjustment)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand Down Expand Up @@ -94,7 +116,11 @@ func CheckProfitabilityAndBroadCastTx(clientCtx client.Context, txf tx.Factory,
}
}

txf.WithGas(gasUsed)
if err := validateGas(gasUsed); err != nil {
return err
}

txf = txf.WithGas(gasUsed)

gasFee := txf.GasPrices()[0]
gasFee.Amount = gasFee.Amount.MulInt64(int64(gasUsed))
Expand Down

0 comments on commit 2c88407

Please sign in to comment.