Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refine the code of gashub module #140

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions proto/cosmos/gashub/v1alpha1/event.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ package cosmos.gashub.v1alpha1;

option go_package = "github.com/cosmos/cosmos-sdk/x/gashub/types";

// EventUpdateMsgGasParams is emitted when update a msg's gas params
// EventUpdateMsgGasParams is emitted when updating a message's gas params
message EventUpdateMsgGasParams {
// msg_type_url is the type url of the message
string msg_type_url = 1;
string from_value = 2;
string to_value = 3;
// from_value is the previous gas params
string from_value = 2;
// to_value is the new gas params
string to_value = 3;
}
8 changes: 7 additions & 1 deletion proto/cosmos/gashub/v1alpha1/gashub.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ message Params {
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;

uint64 max_tx_size = 1 [(gogoproto.customname) = "MaxTxSize"];
// max_tx_size is the maximum size of a transaction's bytes.
uint64 max_tx_size = 1 [(gogoproto.customname) = "MaxTxSize"];
// min_gas_per_byte is the minimum gas to be paid per byte of a transaction's
uint64 min_gas_per_byte = 2 [(gogoproto.customname) = "MinGasPerByte"];

// msg_gas_params is the list of gas params for each msg type
repeated MsgGasParams msg_gas_params_set = 3 [(gogoproto.customname) = "MsgGasParamsSet"];
}

Expand All @@ -36,14 +39,17 @@ message MsgGasParams {
message FixedGasParams {
option (gogoproto.equal) = true;

// fixed_gas is the gas cost for a fixed type msg
uint64 fixed_gas = 1 [(gogoproto.customname) = "FixedGas"];
}

// DynamicGasParams defines the parameters for dynamic gas type.
message DynamicGasParams {
option (gogoproto.equal) = true;

// fixed_gas is the base gas cost for a dynamic type msg
uint64 fixed_gas = 1 [(gogoproto.customname) = "FixedGas"];
// gas_per_item is the gas cost for a dynamic type msg per item
uint64 gas_per_item = 2 [(gogoproto.customname) = "GasPerItem"];
}
}
2 changes: 1 addition & 1 deletion proto/cosmos/gashub/v1alpha1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/gashub/types";

// GenesisState defines the gashub module's genesis state.
message GenesisState {
// params defines all the paramaters of the module.
// params defines all the parameters of the module.
Params params = 1 [(gogoproto.nullable) = false];
}
6 changes: 4 additions & 2 deletions proto/cosmos/gashub/v1alpha1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ service Msg {
// MsgUpdateMsgGasParams represents a message to update msg gas params.
message MsgUpdateMsgGasParams {
// NOTE: The params should be updated by the gov module account after the proposal passes.
option (cosmos.msg.v1.signer) = "sender";
option (cosmos.msg.v1.signer) = "from";

option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

string from = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// from is the message signer for MsgUpdateMsgGasParams
string from = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// new_params_set is the new set of msg gas params to be updated.
repeated MsgGasParams new_params_set = 2;
}

Expand Down
4 changes: 2 additions & 2 deletions server/json_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
)

// StartJSONRPC starts the JSON-RPC server
// this server is only meant to be used for wallets connection, so the wallets can sign EIP712 typed msg
// it doesn't include other unnecessary EVM rpc apis like eth_call
// This server is exclusively designed for wallet connections to sign EIP712 typed messages.
// It does not offer any other unnecessary EVM RPC APIs, such as eth_call.
func StartJSONRPC(ctx *Context,
clientCtx client.Context,
tmRPCAddr,
Expand Down
103 changes: 6 additions & 97 deletions server/jsonrpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,122 +3,31 @@ package backend
import (
"context"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
rpctypes "github.com/evmos/ethermint/rpc/types"
"github.com/evmos/ethermint/server/config"
ethermint "github.com/evmos/ethermint/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/libs/log"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/server/config"
sdk "github.com/cosmos/cosmos-sdk/types"
rpctypes "github.com/evmos/ethermint/rpc/types"
)

// BackendI implements the EVM backend.
// this server is only meant to be used for wallets connection, so the wallets can sign EIP712 typed msg
// it doesn't include other unnecessary EVM rpc apis like eth_call
type BackendI interface {
EVMBackend
}

// EVMBackend implements the functionality shared within ethereum namespaces
// as defined by EIP-1474: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md
// Implemented by Backend.
// This server is exclusively designed for wallet connections to sign EIP712 typed messages.
// It does not offer any other unnecessary EVM RPC APIs, such as eth_call.
type EVMBackend interface {
unclezoro marked this conversation as resolved.
Show resolved Hide resolved
// Node specific queries
Accounts() ([]common.Address, error)
Syncing() (interface{}, error)
SetEtherbase(etherbase common.Address) bool
SetGasPrice(gasPrice hexutil.Big) bool
ImportRawKey(privkey, password string) (common.Address, error)
ListAccounts() ([]common.Address, error)
NewMnemonic(uid string, language keyring.Language, hdPath, bip39Passphrase string, algo keyring.SignatureAlgo) (*keyring.Record, error)
UnprotectedAllowed() bool
RPCGasCap() uint64 // global gas cap for eth_call over rpc: DoS protection
RPCEVMTimeout() time.Duration // global timeout for eth_call over rpc: DoS protection
RPCTxFeeCap() float64 // RPCTxFeeCap is the global transaction fee(price * gaslimit) cap for send-transaction variants. The unit is ether.
RPCMinGasPrice() int64

// Sign Tx
Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error)
SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error)
SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error)

// Blocks Info
BlockNumber() (hexutil.Uint64, error)
GetBlockByNumber(blockNum rpctypes.BlockNumber, fullTx bool) (map[string]interface{}, error)
GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error)
GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint
GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber) *hexutil.Uint
TendermintBlockByNumber(blockNum rpctypes.BlockNumber) (*tmrpctypes.ResultBlock, error)
TendermintBlockResultByNumber(height *int64) (*tmrpctypes.ResultBlockResults, error)
TendermintBlockByHash(blockHash common.Hash) (*tmrpctypes.ResultBlock, error)
BlockNumberFromTendermint(blockNrOrHash rpctypes.BlockNumberOrHash) (rpctypes.BlockNumber, error)
BlockNumberFromTendermintByHash(blockHash common.Hash) (*big.Int, error)
EthMsgsFromTendermintBlock(block *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) []*evmtypes.MsgEthereumTx
BlockBloom(blockRes *tmrpctypes.ResultBlockResults) (ethtypes.Bloom, error)
HeaderByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Header, error)
HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
RPCBlockFromTendermintBlock(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults, fullTx bool) (map[string]interface{}, error)
EthBlockByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Block, error)
EthBlockFromTendermintBlock(resBlock *tmrpctypes.ResultBlock, blockRes *tmrpctypes.ResultBlockResults) (*ethtypes.Block, error)

// Account Info
GetCode(address common.Address, blockNrOrHash rpctypes.BlockNumberOrHash) (hexutil.Bytes, error)
GetBalance(address common.Address, blockNrOrHash rpctypes.BlockNumberOrHash) (*hexutil.Big, error)
GetStorageAt(address common.Address, key string, blockNrOrHash rpctypes.BlockNumberOrHash) (hexutil.Bytes, error)
GetProof(address common.Address, storageKeys []string, blockNrOrHash rpctypes.BlockNumberOrHash) (*rpctypes.AccountResult, error)
GetTransactionCount(address common.Address, blockNum rpctypes.BlockNumber) (*hexutil.Uint64, error)

// Chain Info
ChainID() (*hexutil.Big, error)
ChainConfig() *params.ChainConfig
GlobalMinGasPrice() (sdk.Dec, error)
BaseFee(blockRes *tmrpctypes.ResultBlockResults) (*big.Int, error)
CurrentHeader() *ethtypes.Header
PendingTransactions() ([]*sdk.Tx, error)
GetCoinbase() (sdk.AccAddress, error)
FeeHistory(blockCount rpc.DecimalOrHex, lastBlock rpc.BlockNumber, rewardPercentiles []float64) (*rpctypes.FeeHistoryResult, error)
SuggestGasTipCap(baseFee *big.Int) (*big.Int, error)

// Tx Info
GetTransactionByHash(txHash common.Hash) (*rpctypes.RPCTransaction, error)
GetTxByEthHash(txHash common.Hash) (*ethermint.TxResult, error)
GetTxByTxIndex(height int64, txIndex uint) (*ethermint.TxResult, error)
GetTransactionByBlockAndIndex(block *tmrpctypes.ResultBlock, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)
GetTransactionReceipt(hash common.Hash) (map[string]interface{}, error)
GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)
GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockNumber, idx hexutil.Uint) (*rpctypes.RPCTransaction, error)

// Send Transaction
Resend(args evmtypes.TransactionArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error)
SendRawTransaction(data hexutil.Bytes) (common.Hash, error)
SetTxDefaults(args evmtypes.TransactionArgs) (evmtypes.TransactionArgs, error)
EstimateGas(args evmtypes.TransactionArgs, blockNrOptional *rpctypes.BlockNumber) (hexutil.Uint64, error)
DoCall(args evmtypes.TransactionArgs, blockNr rpctypes.BlockNumber) (*evmtypes.MsgEthereumTxResponse, error)
GasPrice() (*hexutil.Big, error)

// Filter API
GetLogs(hash common.Hash) ([][]*ethtypes.Log, error)
GetLogsByHeight(height *int64) ([][]*ethtypes.Log, error)
BloomStatus() (uint64, uint64)

// Tracing
TraceTransaction(hash common.Hash, config *evmtypes.TraceConfig) (interface{}, error)
TraceBlock(height rpctypes.BlockNumber, config *evmtypes.TraceConfig, block *tmrpctypes.ResultBlock) ([]*evmtypes.TxTraceResult, error)
}

var _ BackendI = (*Backend)(nil)
var _ EVMBackend = (*Backend)(nil)

// Backend implements the BackendI interface
type Backend struct {
Expand All @@ -135,7 +44,7 @@ func NewBackend(
logger log.Logger,
clientCtx client.Context,
) *Backend {
chainID, err := ethermint.ParseChainID(clientCtx.ChainID)
chainID, err := sdk.ParseChainID(clientCtx.ChainID)
if err != nil {
panic(err)
}
Expand Down
29 changes: 0 additions & 29 deletions server/jsonrpc/backend/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/trie"
rpctypes "github.com/evmos/ethermint/rpc/types"
"github.com/pkg/errors"
tmrpctypes "github.com/tendermint/tendermint/rpc/core/types"
Expand Down Expand Up @@ -140,34 +139,6 @@ func (b *Backend) RPCBlockFromTendermintBlock(
return formattedBlock, nil
}

// EthBlockByNumber returns the Ethereum Block identified by number.
func (b *Backend) EthBlockByNumber(blockNum rpctypes.BlockNumber) (*ethtypes.Block, error) {
resBlock, err := b.TendermintBlockByNumber(blockNum)
if err != nil {
return nil, err
}
if resBlock == nil {
// block not found
return nil, fmt.Errorf("block not found for height %d", blockNum)
}

return b.EthBlockFromTendermintBlock(resBlock, nil)
}

// EthBlockFromTendermintBlock returns an Ethereum Block type from Tendermint block
// EthBlockFromTendermintBlock
func (b *Backend) EthBlockFromTendermintBlock(
resBlock *tmrpctypes.ResultBlock,
blockRes *tmrpctypes.ResultBlockResults,
) (*ethtypes.Block, error) {
block := resBlock.Block

ethHeader := EthHeaderFromTendermint(block.Header)

ethBlock := ethtypes.NewBlock(ethHeader, nil, nil, nil, trie.NewStackTrie(nil))
return ethBlock, nil
}

func EthHeaderFromTendermint(header tmtypes.Header) *ethtypes.Header {
txHash := ethtypes.EmptyRootHash
if len(header.DataHash) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions server/jsonrpc/backend/chain_info.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package backend

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
rpctypes "github.com/evmos/ethermint/rpc/types"
ethermint "github.com/evmos/ethermint/types"
)

// ChainID is the chain id for the current chain config.
func (b *Backend) ChainID() (*hexutil.Big, error) {
chainID, err := ethermint.ParseChainID(b.clientCtx.ChainID)
chainID, err := sdk.ParseChainID(b.clientCtx.ChainID)
if err != nil {
panic(err)
}
Expand Down
Loading