Skip to content

Commit

Permalink
take out gas estimate keeper in oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari committed Jan 8, 2025
1 parent 173a9e9 commit ec052ff
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 147 deletions.
1 change: 0 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ func New(
app.BankKeeper,
app.DistrKeeper,
app.StakingKeeper,
app.GasEstimateKeeper,
distrtypes.ModuleName,
cast.ToBool(appOpts.Get("telemetry.enabled")),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
Expand Down
107 changes: 0 additions & 107 deletions x/oracle/abci/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,9 @@ func (h *ProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHandler {
return &cometabci.ResponsePrepareProposal{Txs: make([][]byte, 0)}, err
}

medianGasEstimates, err := h.generateMedianGasEstimates(ctx, req.LocalLastCommit)
if err != nil {
return &cometabci.ResponsePrepareProposal{Txs: make([][]byte, 0)}, err
}
injectedVoteExtTx := oracletypes.InjectedVoteExtensionTx{
ExchangeRateVotes: exchangeRateVotes,
ExtendedCommitInfo: extendedCommitInfoBz,
GasEstimateMedians: medianGasEstimates,
}

bz, err := injectedVoteExtTx.Marshal()
Expand Down Expand Up @@ -176,14 +171,6 @@ func (h *ProposalHandler) ProcessProposalHandler() sdk.ProcessProposalHandler {
if err := h.verifyExchangeRateVotes(injectedVoteExtTx.ExchangeRateVotes, exchangeRateVotes); err != nil {
return &cometabci.ResponseProcessProposal{Status: cometabci.ResponseProcessProposal_REJECT}, err
}
// Verify the proposer's gas estimation by computing the same median.
gasEstimateMedians, err := h.generateMedianGasEstimates(ctx, extendedCommitInfo)
if err != nil {
return &cometabci.ResponseProcessProposal{Status: cometabci.ResponseProcessProposal_REJECT}, err
}
if err := h.verifyMedianGasEstimations(injectedVoteExtTx.GasEstimateMedians, gasEstimateMedians); err != nil {
return &cometabci.ResponseProcessProposal{Status: cometabci.ResponseProcessProposal_REJECT}, err
}
}

h.logger.Info(
Expand Down Expand Up @@ -266,97 +253,3 @@ func (h *ProposalHandler) verifyExchangeRateVotes(

return nil
}

func (h *ProposalHandler) generateMedianGasEstimates(
ctx sdk.Context,
ci cometabci.ExtendedCommitInfo,
) ([]oracletypes.GasEstimate, error) {
gasEstimates := []oracletypes.GasEstimate{}

for _, vote := range ci.Votes {
if vote.BlockIdFlag != cmtproto.BlockIDFlagCommit {
continue
}

var valConsAddr sdk.ConsAddress
if err := valConsAddr.Unmarshal(vote.Validator.Address); err != nil {
h.logger.Error(
"failed to unmarshal validator consensus address",
"err", err,
)
return gasEstimates, err
}
val, err := h.stakingKeeper.GetValidatorByConsAddr(ctx, valConsAddr)
if err != nil {
h.logger.Error(
"failed to get consensus validator from staking keeper",
"err", err,
)
return gasEstimates, err
}
_, err = sdk.ValAddressFromBech32(val.OperatorAddress)
if err != nil {
return gasEstimates, err
}
}

networks := []string{}
// get contracts on registry list
params := h.oracleKeeper.GasEstimateKeeper.GetParams(ctx)
for _, contract := range params.ContractRegistry {
networks = append(networks, contract.Network)
}

for _, network := range networks {
networkEstimates := []oracletypes.GasEstimate{}

for _, vote := range ci.Votes {
var voteExt oracletypes.OracleVoteExtension
if err := voteExt.Unmarshal(vote.VoteExtension); err != nil {
h.logger.Error(
"failed to decode vote extension",
"err", err,
)
continue
}

for _, estimate := range voteExt.GasEstimates {
if estimate.Network == network {
networkEstimates = append(networkEstimates, estimate)
}
}
}

median, err := calculateMedian(networkEstimates)
if err != nil {
continue
}
gasEstimates = append(gasEstimates, median)
}

return gasEstimates, nil
}

func (h *ProposalHandler) verifyMedianGasEstimations(
injectedEstimates []oracletypes.GasEstimate,
generatedEstimates []oracletypes.GasEstimate,
) error {
if len(injectedEstimates) != len(generatedEstimates) {
return oracletypes.ErrNonEqualInjVotesLen
}

for i := range injectedEstimates {
injectedEstimate := injectedEstimates[i]
generatedEstimate := generatedEstimates[i]

if injectedEstimate.Network != generatedEstimate.Network {
return oracletypes.ErrNonEqualInjVotesRates
}

if injectedEstimate.GasEstimation != generatedEstimate.GasEstimation {
return oracletypes.ErrNonEqualInjVotesRates
}
}

return nil
}
21 changes: 0 additions & 21 deletions x/oracle/abci/voteextension.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
cometabci "github.com/cometbft/cometbft/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"

relayerClient "github.com/ojo-network/ojo-evm/relayer/relayer/client"
"github.com/ojo-network/ojo/x/oracle/keeper"
"github.com/ojo-network/ojo/x/oracle/types"
"github.com/ojo-network/price-feeder/oracle"
Expand Down Expand Up @@ -82,29 +81,9 @@ func (h *VoteExtensionHandler) ExtendVoteHandler() sdk.ExtendVoteHandler {
}
}

gasEstimates := []types.GasEstimate{}
estimateParams := h.oracleKeeper.GasEstimateKeeper.GetParams(ctx)
for _, contract := range estimateParams.ContractRegistry {
resp, err := relayerClient.EstimateGasFee(
contract.Network,
contract.Address,
estimateParams.GasLimit,
estimateParams.GasAdjustment,
)
if err != nil {
h.logger.Error("error estimating gas fee", "error", err)
continue
}
gasEstimates = append(gasEstimates, types.GasEstimate{
GasEstimation: resp.Int64(),
Network: contract.Network,
})
}

voteExt := types.OracleVoteExtension{
Height: req.Height,
ExchangeRates: filteredDecCoins,
GasEstimates: gasEstimates,
}

bz, err := voteExt.Marshal()
Expand Down
33 changes: 15 additions & 18 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ type Keeper struct {
storeKey storetypes.StoreKey
paramSpace paramstypes.Subspace

accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistributionKeeper
StakingKeeper types.StakingKeeper
GasEstimateKeeper types.GasEstimateKeeper
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
distrKeeper types.DistributionKeeper
StakingKeeper types.StakingKeeper

PriceFeeder *pricefeeder.PriceFeeder

Expand All @@ -49,7 +48,6 @@ func NewKeeper(
bankKeeper types.BankKeeper,
distrKeeper types.DistributionKeeper,
stakingKeeper types.StakingKeeper,
gasEstimateKeeper types.GasEstimateKeeper,
distrName string,
telemetryEnabled bool,
authority string,
Expand All @@ -65,18 +63,17 @@ func NewKeeper(
}

return Keeper{
cdc: cdc,
storeKey: storeKey,
paramSpace: paramspace,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
GasEstimateKeeper: gasEstimateKeeper,
PriceFeeder: &pricefeeder.PriceFeeder{},
distrName: distrName,
telemetryEnabled: telemetryEnabled,
authority: authority,
cdc: cdc,
storeKey: storeKey,
paramSpace: paramspace,
accountKeeper: accountKeeper,
bankKeeper: bankKeeper,
distrKeeper: distrKeeper,
StakingKeeper: stakingKeeper,
PriceFeeder: &pricefeeder.PriceFeeder{},
distrName: distrName,
telemetryEnabled: telemetryEnabled,
authority: authority,
}
}

Expand Down

0 comments on commit ec052ff

Please sign in to comment.