From 7ef52ce013bad5b09387612ff5cefaedb7215d57 Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Mon, 26 Jul 2021 11:35:08 -0300 Subject: [PATCH 01/33] miner namespace --- ethereum/rpc/apis.go | 11 +++++ ethereum/rpc/namespaces/miner/api.go | 40 +++++++++++++++ ethereum/rpc/namespaces/miner/unsupported.go | 51 ++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 ethereum/rpc/namespaces/miner/api.go create mode 100644 ethereum/rpc/namespaces/miner/unsupported.go diff --git a/ethereum/rpc/apis.go b/ethereum/rpc/apis.go index 0b93bc5edb..d139fc430a 100644 --- a/ethereum/rpc/apis.go +++ b/ethereum/rpc/apis.go @@ -10,6 +10,7 @@ import ( "github.com/tharsis/ethermint/ethereum/rpc/namespaces/debug" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth/filters" + "github.com/tharsis/ethermint/ethereum/rpc/namespaces/miner" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/net" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/personal" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/txpool" @@ -27,6 +28,7 @@ const ( NetNamespace = "net" TxPoolNamespace = "txpool" DebugNamespace = "debug" + MinerNamespace = "miner" apiVersion = "1.0" ) @@ -104,6 +106,15 @@ func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpccl Public: true, }, ) + case MinerNamespace: + apis = append(apis, + rpc.API{ + Namespace: MinerNamespace, + Version: apiVersion, + Service: miner.NewMinerAPI(ctx), + Public: true, + }, + ) default: ctx.Logger.Error("invalid namespace value", "namespace", selectedAPIs[index]) } diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go new file mode 100644 index 0000000000..89528bcd09 --- /dev/null +++ b/ethereum/rpc/namespaces/miner/api.go @@ -0,0 +1,40 @@ +package miner + +import ( + "github.com/cosmos/cosmos-sdk/server" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/tendermint/tendermint/libs/log" +) + +// API is the miner prefixed set of APIs in the Miner JSON-RPC spec. +type API struct { + ctx *server.Context + logger log.Logger +} + +// NewMinerAPI creates an instance of the Miner API. +func NewMinerAPI( + ctx *server.Context, +) *API { + return &API{ + ctx: ctx, + logger: ctx.Logger.With("module", "miner"), + } +} + +// SetEtherbase sets the etherbase of the miner +func (api *API) SetEtherbase(etherbase common.Address) bool { + //api.e.SetEtherbase(etherbase) + return true +} + +// SetGasPrice sets the minimum accepted gas price for the miner. +func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { + // api.e.lock.Lock() + // api.e.gasPrice = (*big.Int)(&gasPrice) + // api.e.lock.Unlock() + + // api.e.txPool.SetGasPrice((*big.Int)(&gasPrice)) + return false +} diff --git a/ethereum/rpc/namespaces/miner/unsupported.go b/ethereum/rpc/namespaces/miner/unsupported.go new file mode 100644 index 0000000000..109f256f5a --- /dev/null +++ b/ethereum/rpc/namespaces/miner/unsupported.go @@ -0,0 +1,51 @@ +package miner + +import ( + "errors" + + "github.com/ethereum/go-ethereum/common/hexutil" +) + +// GetHashrate returns the current hashrate for local CPU miner and remote miner. +// Unsupported in Ethermint +func (api *API) GetHashrate() uint64 { + api.logger.Debug("miner_getHashrate") + api.logger.Info("Unsupported function") + return 0 +} + +// SetExtra sets the extra data string that is included when this miner mines a block. +// Unsupported in Ethermint +func (api *API) SetExtra(extra string) (bool, error) { + api.logger.Debug("miner_setExtra") + api.logger.Info("Unsupported function") + return false, errors.New("Unsupported function") +} + +// SetGasLimit sets the gaslimit to target towards during mining. +// Unsupported in Ethermint +func (api *API) SetGasLimit(gasLimit hexutil.Uint64) bool { + api.logger.Debug("miner_setGasLimit") + api.logger.Info("Unsupported function") + return false +} + +// Start starts the miner with the given number of threads. If threads is nil, +// the number of workers started is equal to the number of logical CPUs that are +// usable by this process. If mining is already running, this method adjust the +// number of threads allowed to use and updates the minimum price required by the +// transaction pool. +// Unsupported in Ethermint +func (api *API) Start(threads *int) error { + api.logger.Debug("miner_start") + api.logger.Info("Unsupported function") + return errors.New("Unsupported function") +} + +// Stop terminates the miner, both at the consensus engine level as well as at +// the block creation level. +// Unsupported in Ethermint +func (api *API) Stop() { + api.logger.Debug("miner_stop") + api.logger.Info("Unsupported function") +} From dce20db3e7c979a71a45fbf19db79b91b2bc9e65 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Mon, 26 Jul 2021 18:50:39 +0200 Subject: [PATCH 02/33] SetGasPrice call --- ethereum/rpc/namespaces/miner/api.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 89528bcd09..b126825dfc 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -2,6 +2,8 @@ package miner import ( "github.com/cosmos/cosmos-sdk/server" + "github.com/cosmos/cosmos-sdk/server/config" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/tendermint/tendermint/libs/log" @@ -31,10 +33,24 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { // SetGasPrice sets the minimum accepted gas price for the miner. func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { - // api.e.lock.Lock() - // api.e.gasPrice = (*big.Int)(&gasPrice) - // api.e.lock.Unlock() - - // api.e.txPool.SetGasPrice((*big.Int)(&gasPrice)) - return false + api.logger.Info(api.ctx.Viper.ConfigFileUsed()) + appConf, err := config.ParseConfig(api.ctx.Viper) + if err != nil { + // TODO: fix this error format + api.logger.Error("failed to parse %s: %w", api.ctx.Viper.ConfigFileUsed(), err) + return false + } + // TODO: should this value be wei? + coinsValue := gasPrice.ToInt().String() + unit := "aphoton" + c, err := sdk.ParseDecCoins(coinsValue + unit) + if err != nil { + // TODO: fix this error format + api.logger.Error("failed to parse coins %s, %s: %w", coinsValue, unit, err) + return false + } + appConf.SetMinGasPrices(c) + config.WriteConfigFile(api.ctx.Viper.ConfigFileUsed(), appConf) + api.logger.Info("Your configuration file was modified. Please RESTART your node.", "value", coinsValue+unit) + return true } From 60808e06b75ea292ec9f8fe9d820e7653cc453b8 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Mon, 26 Jul 2021 21:53:48 +0200 Subject: [PATCH 03/33] Added note plus fixed error logging --- ethereum/rpc/namespaces/miner/api.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index b126825dfc..cd8f8e0fd5 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -36,17 +36,16 @@ func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { api.logger.Info(api.ctx.Viper.ConfigFileUsed()) appConf, err := config.ParseConfig(api.ctx.Viper) if err != nil { - // TODO: fix this error format - api.logger.Error("failed to parse %s: %w", api.ctx.Viper.ConfigFileUsed(), err) + api.logger.Error("failed to parse file.", "file", api.ctx.Viper.ConfigFileUsed(), "error:", err.Error()) return false } - // TODO: should this value be wei? + // NOTE: To allow values less that 1 aphoton, we need to divide the gasPrice here using some constant + // If we want to work the same as go-eth we should just use the gasPrice as an int without converting it coinsValue := gasPrice.ToInt().String() unit := "aphoton" c, err := sdk.ParseDecCoins(coinsValue + unit) if err != nil { - // TODO: fix this error format - api.logger.Error("failed to parse coins %s, %s: %w", coinsValue, unit, err) + api.logger.Error("failed to parse coins", "coins", coinsValue, "error", err.Error()) return false } appConf.SetMinGasPrices(c) From 609677e0e144f34948e514364b4f2b75952cdf21 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Mon, 26 Jul 2021 22:20:38 +0200 Subject: [PATCH 04/33] Refactor to use the keyring in the miner namespace --- ethereum/rpc/apis.go | 2 +- ethereum/rpc/namespaces/eth/api.go | 29 ++++++++++++++++------------ ethereum/rpc/namespaces/miner/api.go | 23 ++++++++++++++++++---- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/ethereum/rpc/apis.go b/ethereum/rpc/apis.go index d139fc430a..c4244b06d5 100644 --- a/ethereum/rpc/apis.go +++ b/ethereum/rpc/apis.go @@ -111,7 +111,7 @@ func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpccl rpc.API{ Namespace: MinerNamespace, Version: apiVersion, - Service: miner.NewMinerAPI(ctx), + Service: miner.NewMinerAPI(ctx, clientCtx), Public: true, }, ) diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index a1bb84b7a8..b5aff2ad13 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -47,18 +47,7 @@ type PublicAPI struct { nonceLock *rpctypes.AddrLocker } -// NewPublicAPI creates an instance of the public ETH Web3 API. -func NewPublicAPI( - logger log.Logger, - clientCtx client.Context, - backend backend.Backend, - nonceLock *rpctypes.AddrLocker, -) *PublicAPI { - epoch, err := ethermint.ParseChainID(clientCtx.ChainID) - if err != nil { - panic(err) - } - +func AddKeyringToClientCtx(clientCtx client.Context) *client.Context { algos, _ := clientCtx.Keyring.SupportedAlgorithms() if !algos.Contains(hd.EthSecp256k1) { @@ -76,6 +65,22 @@ func NewPublicAPI( clientCtx = clientCtx.WithKeyring(kr) } + return &clientCtx +} + +// NewPublicAPI creates an instance of the public ETH Web3 API. +func NewPublicAPI( + logger log.Logger, + clientCtx client.Context, + backend backend.Backend, + nonceLock *rpctypes.AddrLocker, +) *PublicAPI { + epoch, err := ethermint.ParseChainID(clientCtx.ChainID) + if err != nil { + panic(err) + } + + clientCtx = *AddKeyringToClientCtx(clientCtx) api := &PublicAPI{ ctx: context.Background(), diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index cd8f8e0fd5..2f6e0d269e 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -1,27 +1,42 @@ package miner import ( + "math/big" + + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/tendermint/tendermint/libs/log" + ethapi "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" + ethermint "github.com/tharsis/ethermint/types" ) // API is the miner prefixed set of APIs in the Miner JSON-RPC spec. type API struct { - ctx *server.Context - logger log.Logger + ctx *server.Context + logger log.Logger + chainIDEpoch *big.Int + clientCtx client.Context } // NewMinerAPI creates an instance of the Miner API. func NewMinerAPI( ctx *server.Context, + clientCtx client.Context, ) *API { + epoch, err := ethermint.ParseChainID(clientCtx.ChainID) + if err != nil { + panic(err) + } + return &API{ - ctx: ctx, - logger: ctx.Logger.With("module", "miner"), + ctx: ctx, + clientCtx: *ethapi.AddKeyringToClientCtx(clientCtx), + chainIDEpoch: epoch, + logger: ctx.Logger.With("module", "miner"), } } From f9d4a01e0dbc1774b4418c373b9d7d2e89001f1e Mon Sep 17 00:00:00 2001 From: Guillermo Date: Tue, 27 Jul 2021 15:42:31 +0200 Subject: [PATCH 05/33] Changed keyring function return --- ethereum/rpc/namespaces/eth/api.go | 6 +++--- ethereum/rpc/namespaces/miner/api.go | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index b5aff2ad13..6249fa6378 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -47,7 +47,7 @@ type PublicAPI struct { nonceLock *rpctypes.AddrLocker } -func AddKeyringToClientCtx(clientCtx client.Context) *client.Context { +func AddKeyringToClientCtx(clientCtx client.Context) client.Context { algos, _ := clientCtx.Keyring.SupportedAlgorithms() if !algos.Contains(hd.EthSecp256k1) { @@ -65,7 +65,7 @@ func AddKeyringToClientCtx(clientCtx client.Context) *client.Context { clientCtx = clientCtx.WithKeyring(kr) } - return &clientCtx + return clientCtx } // NewPublicAPI creates an instance of the public ETH Web3 API. @@ -80,7 +80,7 @@ func NewPublicAPI( panic(err) } - clientCtx = *AddKeyringToClientCtx(clientCtx) + clientCtx = AddKeyringToClientCtx(clientCtx) api := &PublicAPI{ ctx: context.Background(), diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 2f6e0d269e..51506338cc 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -34,7 +34,7 @@ func NewMinerAPI( return &API{ ctx: ctx, - clientCtx: *ethapi.AddKeyringToClientCtx(clientCtx), + clientCtx: ethapi.AddKeyringToClientCtx(clientCtx), chainIDEpoch: epoch, logger: ctx.Logger.With("module", "miner"), } From bfc58d8860b7353e92772c24c685eb2a21db9915 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Tue, 27 Jul 2021 15:44:17 +0200 Subject: [PATCH 06/33] Added more detailed logs to unsupported functions --- ethereum/rpc/namespaces/miner/unsupported.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/unsupported.go b/ethereum/rpc/namespaces/miner/unsupported.go index 109f256f5a..c6fb86ca92 100644 --- a/ethereum/rpc/namespaces/miner/unsupported.go +++ b/ethereum/rpc/namespaces/miner/unsupported.go @@ -10,7 +10,7 @@ import ( // Unsupported in Ethermint func (api *API) GetHashrate() uint64 { api.logger.Debug("miner_getHashrate") - api.logger.Info("Unsupported function") + api.logger.Info("Unsupported rpc function: miner_getHashrate") return 0 } @@ -18,15 +18,15 @@ func (api *API) GetHashrate() uint64 { // Unsupported in Ethermint func (api *API) SetExtra(extra string) (bool, error) { api.logger.Debug("miner_setExtra") - api.logger.Info("Unsupported function") - return false, errors.New("Unsupported function") + api.logger.Info("Unsupported rpc function: miner_setExtra") + return false, errors.New("Unsupported rpc function: miner_setExtra") } // SetGasLimit sets the gaslimit to target towards during mining. // Unsupported in Ethermint func (api *API) SetGasLimit(gasLimit hexutil.Uint64) bool { api.logger.Debug("miner_setGasLimit") - api.logger.Info("Unsupported function") + api.logger.Info("Unsupported rpc function: miner_setGasLimit") return false } @@ -38,8 +38,8 @@ func (api *API) SetGasLimit(gasLimit hexutil.Uint64) bool { // Unsupported in Ethermint func (api *API) Start(threads *int) error { api.logger.Debug("miner_start") - api.logger.Info("Unsupported function") - return errors.New("Unsupported function") + api.logger.Info("Unsupported rpc function: miner_start") + return errors.New("Unsupported rpc function: miner_start") } // Stop terminates the miner, both at the consensus engine level as well as at @@ -47,5 +47,5 @@ func (api *API) Start(threads *int) error { // Unsupported in Ethermint func (api *API) Stop() { api.logger.Debug("miner_stop") - api.logger.Info("Unsupported function") + api.logger.Info("Unsupported rpc function: miner_stop") } From d7b5f8728bf1928c8e1fae272b89eed3695eac14 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Tue, 27 Jul 2021 16:01:04 +0200 Subject: [PATCH 07/33] Reverted changes on ethapi and just using a refrence to it on miner --- ethereum/rpc/apis.go | 2 +- ethereum/rpc/namespaces/eth/api.go | 29 +++---- ethereum/rpc/namespaces/miner/api.go | 117 ++++++++++++++++++++++----- 3 files changed, 110 insertions(+), 38 deletions(-) diff --git a/ethereum/rpc/apis.go b/ethereum/rpc/apis.go index c4244b06d5..7a6319b890 100644 --- a/ethereum/rpc/apis.go +++ b/ethereum/rpc/apis.go @@ -111,7 +111,7 @@ func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpccl rpc.API{ Namespace: MinerNamespace, Version: apiVersion, - Service: miner.NewMinerAPI(ctx, clientCtx), + Service: miner.NewMinerAPI(ctx, ethAPI), Public: true, }, ) diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index 6249fa6378..a1bb84b7a8 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -47,7 +47,18 @@ type PublicAPI struct { nonceLock *rpctypes.AddrLocker } -func AddKeyringToClientCtx(clientCtx client.Context) client.Context { +// NewPublicAPI creates an instance of the public ETH Web3 API. +func NewPublicAPI( + logger log.Logger, + clientCtx client.Context, + backend backend.Backend, + nonceLock *rpctypes.AddrLocker, +) *PublicAPI { + epoch, err := ethermint.ParseChainID(clientCtx.ChainID) + if err != nil { + panic(err) + } + algos, _ := clientCtx.Keyring.SupportedAlgorithms() if !algos.Contains(hd.EthSecp256k1) { @@ -65,22 +76,6 @@ func AddKeyringToClientCtx(clientCtx client.Context) client.Context { clientCtx = clientCtx.WithKeyring(kr) } - return clientCtx -} - -// NewPublicAPI creates an instance of the public ETH Web3 API. -func NewPublicAPI( - logger log.Logger, - clientCtx client.Context, - backend backend.Backend, - nonceLock *rpctypes.AddrLocker, -) *PublicAPI { - epoch, err := ethermint.ParseChainID(clientCtx.ChainID) - if err != nil { - panic(err) - } - - clientCtx = AddKeyringToClientCtx(clientCtx) api := &PublicAPI{ ctx: context.Background(), diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 51506338cc..66b99d9c33 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -1,48 +1,125 @@ package miner import ( - "math/big" - - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/tendermint/tendermint/libs/log" - ethapi "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" - ethermint "github.com/tharsis/ethermint/types" + "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" ) // API is the miner prefixed set of APIs in the Miner JSON-RPC spec. type API struct { - ctx *server.Context - logger log.Logger - chainIDEpoch *big.Int - clientCtx client.Context + ctx *server.Context + logger log.Logger + ethAPI *eth.PublicAPI } // NewMinerAPI creates an instance of the Miner API. func NewMinerAPI( ctx *server.Context, - clientCtx client.Context, + ethAPI *eth.PublicAPI, ) *API { - epoch, err := ethermint.ParseChainID(clientCtx.ChainID) - if err != nil { - panic(err) - } - return &API{ - ctx: ctx, - clientCtx: ethapi.AddKeyringToClientCtx(clientCtx), - chainIDEpoch: epoch, - logger: ctx.Logger.With("module", "miner"), + ctx: ctx, + ethAPI: ethAPI, + logger: ctx.Logger.With("module", "miner"), } } // SetEtherbase sets the etherbase of the miner func (api *API) SetEtherbase(etherbase common.Address) bool { - //api.e.SetEtherbase(etherbase) + // api.logger.Debug("miner_setEtherbase") + + // // Look up the wallet containing the requested signer + // _, err := api.clientCtx.Keyring.KeyByAddress(sdk.AccAddress(args.From.Bytes())) + // if err != nil { + // e.logger.Error("failed to find key in keyring", "address", args.From, "error", err.Error()) + // return common.Hash{}, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error()) + // } + + // args, err = e.setTxDefaults(args) + // if err != nil { + // return common.Hash{}, err + // } + + // msg := args.ToTransaction() + + // if err := msg.ValidateBasic(); err != nil { + // e.logger.Debug("tx failed basic validation", "error", err.Error()) + // return common.Hash{}, err + // } + + // // TODO: get from chain config + // signer := ethtypes.LatestSignerForChainID(args.ChainID.ToInt()) + + // // Sign transaction + // if err := msg.Sign(signer, e.clientCtx.Keyring); err != nil { + // e.logger.Debug("failed to sign tx", "error", err.Error()) + // return common.Hash{}, err + // } + + // // Assemble transaction from fields + // builder, ok := e.clientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder) + // if !ok { + // e.logger.Error("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error()) + // } + + // option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{}) + // if err != nil { + // e.logger.Error("codectypes.NewAnyWithValue failed to pack an obvious value", "error", err.Error()) + // return common.Hash{}, err + // } + + // builder.SetExtensionOptions(option) + // err = builder.SetMsgs(msg) + // if err != nil { + // e.logger.Error("builder.SetMsgs failed", "error", err.Error()) + // } + + // // Query params to use the EVM denomination + // res, err := e.queryClient.QueryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{}) + // if err != nil { + // e.logger.Error("failed to query evm params", "error", err.Error()) + // return common.Hash{}, err + // } + + // txData, err := evmtypes.UnpackTxData(msg.Data) + // if err != nil { + // e.logger.Error("failed to unpack tx data", "error", err.Error()) + // return common.Hash{}, err + // } + + // fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))} + // builder.SetFeeAmount(fees) + // builder.SetGasLimit(msg.GetGas()) + + // // Encode transaction by default Tx encoder + // txEncoder := e.clientCtx.TxConfig.TxEncoder() + // txBytes, err := txEncoder(builder.GetTx()) + // if err != nil { + // e.logger.Error("failed to encode eth tx using default encoder", "error", err.Error()) + // return common.Hash{}, err + // } + + // txHash := msg.AsTransaction().Hash() + + // // Broadcast transaction in sync mode (default) + // // NOTE: If error is encountered on the node, the broadcast will not return an error + // syncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastSync) + // rsp, err := syncCtx.BroadcastTx(txBytes) + // if err != nil || rsp.Code != 0 { + // if err == nil { + // err = errors.New(rsp.RawLog) + // } + // e.logger.Error("failed to broadcast tx", "error", err.Error()) + // return txHash, err + // } + + // // Return transaction hash + // return txHash, nil return true } From 61742f7429a5e6137fd0557205c0dbd9290ede05 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Tue, 27 Jul 2021 17:13:37 +0200 Subject: [PATCH 08/33] Creating a transaction --- ethereum/rpc/namespaces/eth/api.go | 14 +- ethereum/rpc/namespaces/miner/api.go | 194 +++++++++++++++------------ 2 files changed, 116 insertions(+), 92 deletions(-) diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index a1bb84b7a8..4e922a89de 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -95,6 +95,14 @@ func (e *PublicAPI) ClientCtx() client.Context { return e.clientCtx } +func (e *PublicAPI) QueryClient() *rpctypes.QueryClient { + return e.queryClient +} + +func (e *PublicAPI) Ctx() context.Context { + return e.ctx +} + // ProtocolVersion returns the supported Ethereum protocol version. func (e *PublicAPI) ProtocolVersion() hexutil.Uint { e.logger.Debug("eth_protocolVersion") @@ -370,7 +378,7 @@ func (e *PublicAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, erro return common.Hash{}, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error()) } - args, err = e.setTxDefaults(args) + args, err = e.SetTxDefaults(args) if err != nil { return common.Hash{}, err } @@ -994,9 +1002,9 @@ func (e *PublicAPI) GetProof(address common.Address, storageKeys []string, block }, nil } -// setTxDefaults populates tx message with default values in case they are not +// SetTxDefaults populates tx message with default values in case they are not // provided on the args -func (e *PublicAPI) setTxDefaults(args rpctypes.SendTxArgs) (rpctypes.SendTxArgs, error) { +func (e *PublicAPI) SetTxDefaults(args rpctypes.SendTxArgs) (rpctypes.SendTxArgs, error) { if args.GasPrice == nil { // TODO: Change to either: diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 66b99d9c33..eb2acb4d47 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -1,13 +1,21 @@ package miner import ( + "errors" + + "github.com/cosmos/cosmos-sdk/client/flags" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" + authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/tendermint/tendermint/libs/log" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" + rpctypes "github.com/tharsis/ethermint/ethereum/rpc/types" + evmtypes "github.com/tharsis/ethermint/x/evm/types" ) // API is the miner prefixed set of APIs in the Miner JSON-RPC spec. @@ -31,95 +39,103 @@ func NewMinerAPI( // SetEtherbase sets the etherbase of the miner func (api *API) SetEtherbase(etherbase common.Address) bool { - // api.logger.Debug("miner_setEtherbase") - - // // Look up the wallet containing the requested signer - // _, err := api.clientCtx.Keyring.KeyByAddress(sdk.AccAddress(args.From.Bytes())) - // if err != nil { - // e.logger.Error("failed to find key in keyring", "address", args.From, "error", err.Error()) - // return common.Hash{}, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error()) - // } - - // args, err = e.setTxDefaults(args) - // if err != nil { - // return common.Hash{}, err - // } - - // msg := args.ToTransaction() - - // if err := msg.ValidateBasic(); err != nil { - // e.logger.Debug("tx failed basic validation", "error", err.Error()) - // return common.Hash{}, err - // } - - // // TODO: get from chain config - // signer := ethtypes.LatestSignerForChainID(args.ChainID.ToInt()) - - // // Sign transaction - // if err := msg.Sign(signer, e.clientCtx.Keyring); err != nil { - // e.logger.Debug("failed to sign tx", "error", err.Error()) - // return common.Hash{}, err - // } - - // // Assemble transaction from fields - // builder, ok := e.clientCtx.TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder) - // if !ok { - // e.logger.Error("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error()) - // } - - // option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{}) - // if err != nil { - // e.logger.Error("codectypes.NewAnyWithValue failed to pack an obvious value", "error", err.Error()) - // return common.Hash{}, err - // } - - // builder.SetExtensionOptions(option) - // err = builder.SetMsgs(msg) - // if err != nil { - // e.logger.Error("builder.SetMsgs failed", "error", err.Error()) - // } - - // // Query params to use the EVM denomination - // res, err := e.queryClient.QueryClient.Params(e.ctx, &evmtypes.QueryParamsRequest{}) - // if err != nil { - // e.logger.Error("failed to query evm params", "error", err.Error()) - // return common.Hash{}, err - // } - - // txData, err := evmtypes.UnpackTxData(msg.Data) - // if err != nil { - // e.logger.Error("failed to unpack tx data", "error", err.Error()) - // return common.Hash{}, err - // } - - // fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))} - // builder.SetFeeAmount(fees) - // builder.SetGasLimit(msg.GetGas()) - - // // Encode transaction by default Tx encoder - // txEncoder := e.clientCtx.TxConfig.TxEncoder() - // txBytes, err := txEncoder(builder.GetTx()) - // if err != nil { - // e.logger.Error("failed to encode eth tx using default encoder", "error", err.Error()) - // return common.Hash{}, err - // } - - // txHash := msg.AsTransaction().Hash() - - // // Broadcast transaction in sync mode (default) - // // NOTE: If error is encountered on the node, the broadcast will not return an error - // syncCtx := e.clientCtx.WithBroadcastMode(flags.BroadcastSync) - // rsp, err := syncCtx.BroadcastTx(txBytes) - // if err != nil || rsp.Code != 0 { - // if err == nil { - // err = errors.New(rsp.RawLog) - // } - // e.logger.Error("failed to broadcast tx", "error", err.Error()) - // return txHash, err - // } - - // // Return transaction hash - // return txHash, nil + api.logger.Debug("miner_setEtherbase") + + list, err := api.ethAPI.ClientCtx().Keyring.List() + if err != nil && len(list) > 0 { + api.logger.Debug("Could not get list of addresses") + return false + } + + addr := common.BytesToAddress(list[0].GetPubKey().Address()) + + api.logger.Info(addr.String()) + + args := rpctypes.SendTxArgs{} + args.From = addr + // TODO: set this as the message info + // delegatorAddress := addr + // withdrawAddress := etherbase + args.Data = &hexutil.Bytes{} + args, err = api.ethAPI.SetTxDefaults(args) + if err != nil { + api.logger.Debug("Unable to parse transaction args", "error", err.Error()) + return false + } + + msg := args.ToTransaction() + + if err := msg.ValidateBasic(); err != nil { + api.logger.Debug("tx failed basic validation", "error", err.Error()) + return false + } + + signer := ethtypes.LatestSignerForChainID(args.ChainID.ToInt()) + + // Sign transaction + if err := msg.Sign(signer, api.ethAPI.ClientCtx().Keyring); err != nil { + api.logger.Debug("failed to sign tx", "error", err.Error()) + return false + } + + // Assemble transaction from fields + builder, ok := api.ethAPI.ClientCtx().TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder) + if !ok { + api.logger.Error("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error()) + } + + option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{}) + if err != nil { + api.logger.Error("codectypes.NewAnyWithValue failed to pack an obvious value", "error", err.Error()) + return false + } + + builder.SetExtensionOptions(option) + err = builder.SetMsgs(msg) + if err != nil { + api.logger.Error("builder.SetMsgs failed", "error", err.Error()) + } + + // Query params to use the EVM denomination + res, err := api.ethAPI.QueryClient().QueryClient.Params(api.ethAPI.Ctx(), &evmtypes.QueryParamsRequest{}) + if err != nil { + api.logger.Error("failed to query evm params", "error", err.Error()) + return false + } + + txData, err := evmtypes.UnpackTxData(msg.Data) + if err != nil { + api.logger.Error("failed to unpack tx data", "error", err.Error()) + return false + } + + fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))} + builder.SetFeeAmount(fees) + builder.SetGasLimit(msg.GetGas()) + + // Encode transaction by default Tx encoder + txEncoder := api.ethAPI.ClientCtx().TxConfig.TxEncoder() + txBytes, err := txEncoder(builder.GetTx()) + if err != nil { + api.logger.Error("failed to encode eth tx using default encoder", "error", err.Error()) + return false + } + + txHash := msg.AsTransaction().Hash() + + // Broadcast transaction in sync mode (default) + // NOTE: If error is encountered on the node, the broadcast will not return an error + syncCtx := api.ethAPI.ClientCtx().WithBroadcastMode(flags.BroadcastSync) + rsp, err := syncCtx.BroadcastTx(txBytes) + if err != nil || rsp.Code != 0 { + if err == nil { + err = errors.New(rsp.RawLog) + } + api.logger.Error("failed to broadcast tx", "error", err.Error()) + return false + } + + api.logger.Info("Broadcasting tx...", "hash", txHash) return true } From 292ccfc721fc07b7a2699b25103ced9e2cd73e8a Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Tue, 27 Jul 2021 12:47:13 -0300 Subject: [PATCH 09/33] fix condition --- ethereum/rpc/namespaces/miner/api.go | 2 +- init.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index eb2acb4d47..83f56f0e5c 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -42,7 +42,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { api.logger.Debug("miner_setEtherbase") list, err := api.ethAPI.ClientCtx().Keyring.List() - if err != nil && len(list) > 0 { + if err != nil || len(list) < 1 { api.logger.Debug("Could not get list of addresses") return false } diff --git a/init.sh b/init.sh index 733d0554b6..4d8d0f1d01 100755 --- a/init.sh +++ b/init.sh @@ -86,4 +86,4 @@ if [[ $1 == "pending" ]]; then fi # Start the node (remove the --pruning=nothing flag if historical queries are not needed) -ethermintd start --pruning=nothing $TRACE --log_level $LOGLEVEL --minimum-gas-prices=0.0001aphoton --evm-rpc.api eth,txpool,personal,net,debug,web3 +ethermintd start --pruning=nothing $TRACE --log_level $LOGLEVEL --minimum-gas-prices=0.0001aphoton --evm-rpc.api eth,txpool,personal,net,debug,web3,miner From 0e527ce0af00c68b1bfc0496efc9acbd70ab0ee4 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Tue, 27 Jul 2021 18:07:58 +0200 Subject: [PATCH 10/33] Error string not capitalized --- ethereum/rpc/namespaces/miner/unsupported.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/unsupported.go b/ethereum/rpc/namespaces/miner/unsupported.go index c6fb86ca92..922388e0bb 100644 --- a/ethereum/rpc/namespaces/miner/unsupported.go +++ b/ethereum/rpc/namespaces/miner/unsupported.go @@ -19,7 +19,7 @@ func (api *API) GetHashrate() uint64 { func (api *API) SetExtra(extra string) (bool, error) { api.logger.Debug("miner_setExtra") api.logger.Info("Unsupported rpc function: miner_setExtra") - return false, errors.New("Unsupported rpc function: miner_setExtra") + return false, errors.New("unsupported rpc function: miner_setExtra") } // SetGasLimit sets the gaslimit to target towards during mining. @@ -39,7 +39,7 @@ func (api *API) SetGasLimit(gasLimit hexutil.Uint64) bool { func (api *API) Start(threads *int) error { api.logger.Debug("miner_start") api.logger.Info("Unsupported rpc function: miner_start") - return errors.New("Unsupported rpc function: miner_start") + return errors.New("unsupported rpc function: miner_start") } // Stop terminates the miner, both at the consensus engine level as well as at From 0af193d67df4cb7154923408423966328e72db78 Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Wed, 28 Jul 2021 11:42:22 -0300 Subject: [PATCH 11/33] suggested changes to setEtherbase --- ethereum/rpc/apis.go | 2 +- ethereum/rpc/backend/backend.go | 27 +++++++++ ethereum/rpc/namespaces/eth/api.go | 20 +------ ethereum/rpc/namespaces/miner/api.go | 87 ++++++++++------------------ 4 files changed, 62 insertions(+), 74 deletions(-) diff --git a/ethereum/rpc/apis.go b/ethereum/rpc/apis.go index 7a6319b890..9c31c8917e 100644 --- a/ethereum/rpc/apis.go +++ b/ethereum/rpc/apis.go @@ -111,7 +111,7 @@ func GetRPCAPIs(ctx *server.Context, clientCtx client.Context, tmWSClient *rpccl rpc.API{ Namespace: MinerNamespace, Version: apiVersion, - Service: miner.NewMinerAPI(ctx, ethAPI), + Service: miner.NewMinerAPI(ctx, ethAPI, evmBackend), Public: true, }, ) diff --git a/ethereum/rpc/backend/backend.go b/ethereum/rpc/backend/backend.go index 10909f4ff3..0213b218e5 100644 --- a/ethereum/rpc/backend/backend.go +++ b/ethereum/rpc/backend/backend.go @@ -45,6 +45,8 @@ type Backend interface { // Used by log filter GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, error) BloomStatus() (uint64, uint64) + + GetCoinbase() (sdk.AccAddress, error) } var _ Backend = (*EVMBackend)(nil) @@ -417,3 +419,28 @@ func (e *EVMBackend) GetLogsByNumber(blockNum types.BlockNumber) ([][]*ethtypes. func (e *EVMBackend) BloomStatus() (uint64, uint64) { return 4096, 0 } + +// GetCoinbase is the address that staking rewards will be send to (alias for Etherbase). +func (e *EVMBackend) GetCoinbase() (sdk.AccAddress, error) { + node, err := e.clientCtx.GetNode() + if err != nil { + return nil, err + } + + status, err := node.Status(e.ctx) + if err != nil { + return nil, err + } + + req := &evmtypes.QueryValidatorAccountRequest{ + ConsAddress: sdk.ConsAddress(status.ValidatorInfo.Address).String(), + } + + res, err := e.queryClient.ValidatorAccount(e.ctx, req) + if err != nil { + return nil, err + } + + address, _ := sdk.AccAddressFromBech32(res.AccountAddress) + return address, nil +} diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index 4e922a89de..0f4e8b063b 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -142,26 +142,10 @@ func (e *PublicAPI) Syncing() (interface{}, error) { func (e *PublicAPI) Coinbase() (string, error) { e.logger.Debug("eth_coinbase") - node, err := e.clientCtx.GetNode() + toAddr, err := e.backend.GetCoinbase() if err != nil { - return "", err + return "", nil } - - status, err := node.Status(e.ctx) - if err != nil { - return "", err - } - - req := &evmtypes.QueryValidatorAccountRequest{ - ConsAddress: sdk.ConsAddress(status.ValidatorInfo.Address).String(), - } - - res, err := e.queryClient.ValidatorAccount(e.ctx, req) - if err != nil { - return "", err - } - - toAddr, _ := sdk.AccAddressFromBech32(res.AccountAddress) ethAddr := common.BytesToAddress(toAddr.Bytes()) return ethAddr.Hex(), nil } diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 83f56f0e5c..b5ddf5b8db 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -4,36 +4,39 @@ import ( "errors" "github.com/cosmos/cosmos-sdk/client/flags" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" + "github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" + distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" - ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/tendermint/tendermint/libs/log" + tmtypes "github.com/tendermint/tendermint/types" + "github.com/tharsis/ethermint/ethereum/rpc/backend" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" - rpctypes "github.com/tharsis/ethermint/ethereum/rpc/types" - evmtypes "github.com/tharsis/ethermint/x/evm/types" ) // API is the miner prefixed set of APIs in the Miner JSON-RPC spec. type API struct { - ctx *server.Context - logger log.Logger - ethAPI *eth.PublicAPI + ctx *server.Context + logger log.Logger + ethAPI *eth.PublicAPI + backend backend.Backend } // NewMinerAPI creates an instance of the Miner API. func NewMinerAPI( ctx *server.Context, ethAPI *eth.PublicAPI, + backend backend.Backend, ) *API { return &API{ - ctx: ctx, - ethAPI: ethAPI, - logger: ctx.Logger.With("module", "miner"), + ctx: ctx, + ethAPI: ethAPI, + logger: ctx.Logger.With("module", "miner"), + backend: backend, } } @@ -41,77 +44,51 @@ func NewMinerAPI( func (api *API) SetEtherbase(etherbase common.Address) bool { api.logger.Debug("miner_setEtherbase") - list, err := api.ethAPI.ClientCtx().Keyring.List() - if err != nil || len(list) < 1 { - api.logger.Debug("Could not get list of addresses") - return false - } - - addr := common.BytesToAddress(list[0].GetPubKey().Address()) - - api.logger.Info(addr.String()) - - args := rpctypes.SendTxArgs{} - args.From = addr - // TODO: set this as the message info - // delegatorAddress := addr - // withdrawAddress := etherbase - args.Data = &hexutil.Bytes{} - args, err = api.ethAPI.SetTxDefaults(args) + addr, err := api.backend.GetCoinbase() if err != nil { - api.logger.Debug("Unable to parse transaction args", "error", err.Error()) + api.logger.Debug("failed to get address") return false } - msg := args.ToTransaction() + api.logger.Info("Etherbase account ", addr.String()) + + withdrawAddr := sdk.AccAddress(etherbase.Bytes()) + msg := distributiontypes.NewMsgSetWithdrawAddress(addr, withdrawAddr) if err := msg.ValidateBasic(); err != nil { api.logger.Debug("tx failed basic validation", "error", err.Error()) return false } - signer := ethtypes.LatestSignerForChainID(args.ChainID.ToInt()) - - // Sign transaction - if err := msg.Sign(signer, api.ethAPI.ClientCtx().Keyring); err != nil { - api.logger.Debug("failed to sign tx", "error", err.Error()) - return false - } - // Assemble transaction from fields builder, ok := api.ethAPI.ClientCtx().TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder) if !ok { api.logger.Error("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error()) } - option, err := codectypes.NewAnyWithValue(&evmtypes.ExtensionOptionsEthereumTx{}) - if err != nil { - api.logger.Error("codectypes.NewAnyWithValue failed to pack an obvious value", "error", err.Error()) - return false - } - - builder.SetExtensionOptions(option) err = builder.SetMsgs(msg) if err != nil { api.logger.Error("builder.SetMsgs failed", "error", err.Error()) } - // Query params to use the EVM denomination - res, err := api.ethAPI.QueryClient().QueryClient.Params(api.ethAPI.Ctx(), &evmtypes.QueryParamsRequest{}) + txFactory := tx.Factory{} + txFactory = txFactory. + WithChainID(api.ethAPI.ClientCtx().ChainID). + WithKeybase(api.ethAPI.ClientCtx().Keyring). + WithTxConfig(api.ethAPI.ClientCtx().TxConfig) + + keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(addr) if err != nil { - api.logger.Error("failed to query evm params", "error", err.Error()) return false } - txData, err := evmtypes.UnpackTxData(msg.Data) - if err != nil { - api.logger.Error("failed to unpack tx data", "error", err.Error()) + if err := tx.Sign(txFactory, keyInfo.GetName(), builder, false); err != nil { return false } - fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))} - builder.SetFeeAmount(fees) - builder.SetGasLimit(msg.GetGas()) + // fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))} + // builder.SetFeeAmount(fees) + // builder.SetGasLimit(msg.GetGas()) // Encode transaction by default Tx encoder txEncoder := api.ethAPI.ClientCtx().TxConfig.TxEncoder() @@ -121,7 +98,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - txHash := msg.AsTransaction().Hash() + tmHash := common.BytesToHash(tmtypes.Tx(txBytes).Hash()) // Broadcast transaction in sync mode (default) // NOTE: If error is encountered on the node, the broadcast will not return an error @@ -135,7 +112,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - api.logger.Info("Broadcasting tx...", "hash", txHash) + api.logger.Info("Broadcasting tx...", "hash", tmHash) return true } From 37c1950bbccfeed7e3bf04e23e390bc5cb099d52 Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Wed, 28 Jul 2021 11:43:10 -0300 Subject: [PATCH 12/33] change log level --- ethereum/rpc/namespaces/miner/unsupported.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/unsupported.go b/ethereum/rpc/namespaces/miner/unsupported.go index 922388e0bb..67c4a81974 100644 --- a/ethereum/rpc/namespaces/miner/unsupported.go +++ b/ethereum/rpc/namespaces/miner/unsupported.go @@ -10,7 +10,7 @@ import ( // Unsupported in Ethermint func (api *API) GetHashrate() uint64 { api.logger.Debug("miner_getHashrate") - api.logger.Info("Unsupported rpc function: miner_getHashrate") + api.logger.Debug("Unsupported rpc function: miner_getHashrate") return 0 } @@ -18,7 +18,7 @@ func (api *API) GetHashrate() uint64 { // Unsupported in Ethermint func (api *API) SetExtra(extra string) (bool, error) { api.logger.Debug("miner_setExtra") - api.logger.Info("Unsupported rpc function: miner_setExtra") + api.logger.Debug("Unsupported rpc function: miner_setExtra") return false, errors.New("unsupported rpc function: miner_setExtra") } @@ -26,7 +26,7 @@ func (api *API) SetExtra(extra string) (bool, error) { // Unsupported in Ethermint func (api *API) SetGasLimit(gasLimit hexutil.Uint64) bool { api.logger.Debug("miner_setGasLimit") - api.logger.Info("Unsupported rpc function: miner_setGasLimit") + api.logger.Debug("Unsupported rpc function: miner_setGasLimit") return false } @@ -38,7 +38,7 @@ func (api *API) SetGasLimit(gasLimit hexutil.Uint64) bool { // Unsupported in Ethermint func (api *API) Start(threads *int) error { api.logger.Debug("miner_start") - api.logger.Info("Unsupported rpc function: miner_start") + api.logger.Debug("Unsupported rpc function: miner_start") return errors.New("unsupported rpc function: miner_start") } @@ -47,5 +47,5 @@ func (api *API) Start(threads *int) error { // Unsupported in Ethermint func (api *API) Stop() { api.logger.Debug("miner_stop") - api.logger.Info("Unsupported rpc function: miner_stop") + api.logger.Debug("Unsupported rpc function: miner_stop") } From 6efe47cb70c6f7457e21cd444ec93ccdc681949d Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Wed, 28 Jul 2021 12:50:37 -0300 Subject: [PATCH 13/33] minor changes --- ethereum/rpc/namespaces/miner/api.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index b5ddf5b8db..bf18581aa6 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -44,16 +44,16 @@ func NewMinerAPI( func (api *API) SetEtherbase(etherbase common.Address) bool { api.logger.Debug("miner_setEtherbase") - addr, err := api.backend.GetCoinbase() + delAddr, err := api.backend.GetCoinbase() if err != nil { api.logger.Debug("failed to get address") return false } - api.logger.Info("Etherbase account ", addr.String()) + api.logger.Info("Etherbase account ", delAddr.String()) withdrawAddr := sdk.AccAddress(etherbase.Bytes()) - msg := distributiontypes.NewMsgSetWithdrawAddress(addr, withdrawAddr) + msg := distributiontypes.NewMsgSetWithdrawAddress(delAddr, withdrawAddr) if err := msg.ValidateBasic(); err != nil { api.logger.Debug("tx failed basic validation", "error", err.Error()) @@ -77,12 +77,13 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { WithKeybase(api.ethAPI.ClientCtx().Keyring). WithTxConfig(api.ethAPI.ClientCtx().TxConfig) - keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(addr) + keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(delAddr) if err != nil { return false } if err := tx.Sign(txFactory, keyInfo.GetName(), builder, false); err != nil { + api.logger.Error("failed to sign tx", "error", err.Error()) return false } From b10cc83761dad5636dedbe1183404c21e06861c4 Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Wed, 28 Jul 2021 14:10:04 -0300 Subject: [PATCH 14/33] minor changes --- ethereum/rpc/namespaces/eth/api.go | 2 +- ethereum/rpc/namespaces/miner/api.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index 0f4e8b063b..2a7774f1b3 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -144,7 +144,7 @@ func (e *PublicAPI) Coinbase() (string, error) { toAddr, err := e.backend.GetCoinbase() if err != nil { - return "", nil + return "", err } ethAddr := common.BytesToAddress(toAddr.Bytes()) return ethAddr.Hex(), nil diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index bf18581aa6..528e8a41f5 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -50,7 +50,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - api.logger.Info("Etherbase account ", delAddr.String()) + api.logger.Debug("Etherbase account ", delAddr.String()) withdrawAddr := sdk.AccAddress(etherbase.Bytes()) msg := distributiontypes.NewMsgSetWithdrawAddress(delAddr, withdrawAddr) @@ -71,6 +71,10 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { api.logger.Error("builder.SetMsgs failed", "error", err.Error()) } + // fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))} + // builder.SetFeeAmount(fees) + // builder.SetGasLimit(msg.GetGas()) + txFactory := tx.Factory{} txFactory = txFactory. WithChainID(api.ethAPI.ClientCtx().ChainID). @@ -87,10 +91,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - // fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))} - // builder.SetFeeAmount(fees) - // builder.SetGasLimit(msg.GetGas()) - // Encode transaction by default Tx encoder txEncoder := api.ethAPI.ClientCtx().TxConfig.TxEncoder() txBytes, err := txEncoder(builder.GetTx()) From 78e0a4680191de3afd90f41f5be354c3a8804bbf Mon Sep 17 00:00:00 2001 From: Guillermo Date: Wed, 28 Jul 2021 22:07:36 +0200 Subject: [PATCH 15/33] Sending tx to test the endpoint --- ethereum/rpc/namespaces/miner/api.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 528e8a41f5..d6c8e744fd 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -2,6 +2,7 @@ package miner import ( "errors" + "math/big" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/tx" @@ -71,15 +72,19 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { api.logger.Error("builder.SetMsgs failed", "error", err.Error()) } - // fees := sdk.Coins{sdk.NewCoin(res.Params.EvmDenom, sdk.NewIntFromBigInt(txData.Fee()))} - // builder.SetFeeAmount(fees) - // builder.SetGasLimit(msg.GetGas()) + // TODO: do not hardcode the value + value := big.NewInt(10120) + fees := sdk.Coins{sdk.NewCoin("aphoton", sdk.NewIntFromBigInt(value))} + builder.SetFeeAmount(fees) + builder.SetGasLimit(100000000) txFactory := tx.Factory{} txFactory = txFactory. WithChainID(api.ethAPI.ClientCtx().ChainID). WithKeybase(api.ethAPI.ClientCtx().Keyring). - WithTxConfig(api.ethAPI.ClientCtx().TxConfig) + WithTxConfig(api.ethAPI.ClientCtx().TxConfig). + // TODO: set current nonce + WithSequence(1) keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(delAddr) if err != nil { From 66c9aa40b1d6891c6f0d256d2946e96303f6d22e Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Wed, 28 Jul 2021 17:37:20 -0300 Subject: [PATCH 16/33] get tx nonce --- ethereum/rpc/namespaces/miner/api.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index d6c8e744fd..d716e75d88 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -17,6 +17,7 @@ import ( tmtypes "github.com/tendermint/tendermint/types" "github.com/tharsis/ethermint/ethereum/rpc/backend" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" + rpctypes "github.com/tharsis/ethermint/ethereum/rpc/types" ) // API is the miner prefixed set of APIs in the Miner JSON-RPC spec. @@ -78,13 +79,20 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { builder.SetFeeAmount(fees) builder.SetGasLimit(100000000) + delCommonAddr := common.BytesToAddress(delAddr.Bytes()) + nonce, err := api.ethAPI.GetTransactionCount(delCommonAddr, rpctypes.EthPendingBlockNumber) + if err != nil { + api.logger.Error("failed to get nonce", "error", err.Error()) + return false + } + txFactory := tx.Factory{} txFactory = txFactory. WithChainID(api.ethAPI.ClientCtx().ChainID). WithKeybase(api.ethAPI.ClientCtx().Keyring). WithTxConfig(api.ethAPI.ClientCtx().TxConfig). // TODO: set current nonce - WithSequence(1) + WithSequence(uint64(*nonce)) keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(delAddr) if err != nil { From 4807d4deb108e3c90a3dc1f8140bddc6eeb899b2 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Wed, 28 Jul 2021 23:23:02 +0200 Subject: [PATCH 17/33] Using aphoton const and changing the logger to debug --- ethereum/rpc/namespaces/miner/api.go | 35 +++++++++++++++++----------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index d716e75d88..6e04b5ff61 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -65,7 +65,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { // Assemble transaction from fields builder, ok := api.ethAPI.ClientCtx().TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder) if !ok { - api.logger.Error("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error()) + api.logger.Debug("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error()) } err = builder.SetMsgs(msg) @@ -73,16 +73,22 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { api.logger.Error("builder.SetMsgs failed", "error", err.Error()) } - // TODO: do not hardcode the value - value := big.NewInt(10120) - fees := sdk.Coins{sdk.NewCoin("aphoton", sdk.NewIntFromBigInt(value))} + denom, err := sdk.GetBaseDenom() + if err != nil { + api.logger.Debug("Could not get the denom of smallest unit registered.") + return false + } + + // TODO: is there a way to calculate this message fee and gas limit? + value := big.NewInt(10000) + fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(value))} builder.SetFeeAmount(fees) - builder.SetGasLimit(100000000) + builder.SetGasLimit(80000) delCommonAddr := common.BytesToAddress(delAddr.Bytes()) nonce, err := api.ethAPI.GetTransactionCount(delCommonAddr, rpctypes.EthPendingBlockNumber) if err != nil { - api.logger.Error("failed to get nonce", "error", err.Error()) + api.logger.Debug("failed to get nonce", "error", err.Error()) return false } @@ -91,7 +97,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { WithChainID(api.ethAPI.ClientCtx().ChainID). WithKeybase(api.ethAPI.ClientCtx().Keyring). WithTxConfig(api.ethAPI.ClientCtx().TxConfig). - // TODO: set current nonce WithSequence(uint64(*nonce)) keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(delAddr) @@ -100,7 +105,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { } if err := tx.Sign(txFactory, keyInfo.GetName(), builder, false); err != nil { - api.logger.Error("failed to sign tx", "error", err.Error()) + api.logger.Debug("failed to sign tx", "error", err.Error()) return false } @@ -108,7 +113,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { txEncoder := api.ethAPI.ClientCtx().TxConfig.TxEncoder() txBytes, err := txEncoder(builder.GetTx()) if err != nil { - api.logger.Error("failed to encode eth tx using default encoder", "error", err.Error()) + api.logger.Debug("failed to encode eth tx using default encoder", "error", err.Error()) return false } @@ -122,11 +127,11 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { if err == nil { err = errors.New(rsp.RawLog) } - api.logger.Error("failed to broadcast tx", "error", err.Error()) + api.logger.Debug("failed to broadcast tx", "error", err.Error()) return false } - api.logger.Info("Broadcasting tx...", "hash", tmHash) + api.logger.Info("Broadcasted tx to set delegator's withdraw address.", "hash", tmHash) return true } @@ -139,9 +144,13 @@ func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { return false } // NOTE: To allow values less that 1 aphoton, we need to divide the gasPrice here using some constant - // If we want to work the same as go-eth we should just use the gasPrice as an int without converting it + // If we want to function the same as go-eth we should just use the gasPrice as an int without converting it coinsValue := gasPrice.ToInt().String() - unit := "aphoton" + unit, err := sdk.GetBaseDenom() + if err != nil { + api.logger.Debug("Could not get the denom of smallest unit registered.") + return false + } c, err := sdk.ParseDecCoins(coinsValue + unit) if err != nil { api.logger.Error("failed to parse coins", "coins", coinsValue, "error", err.Error()) From 6179c5b6381b3780013465fcc6c4e089c8511352 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Wed, 28 Jul 2021 23:29:20 +0200 Subject: [PATCH 18/33] Using default RPC gas limit constant --- ethereum/rpc/namespaces/miner/api.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 6e04b5ff61..98c5aa8da1 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -18,6 +18,7 @@ import ( "github.com/tharsis/ethermint/ethereum/rpc/backend" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" rpctypes "github.com/tharsis/ethermint/ethereum/rpc/types" + ethermint "github.com/tharsis/ethermint/types" ) // API is the miner prefixed set of APIs in the Miner JSON-RPC spec. @@ -83,7 +84,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { value := big.NewInt(10000) fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(value))} builder.SetFeeAmount(fees) - builder.SetGasLimit(80000) + builder.SetGasLimit(ethermint.DefaultRPCGasLimit) delCommonAddr := common.BytesToAddress(delAddr.Bytes()) nonce, err := api.ethAPI.GetTransactionCount(delCommonAddr, rpctypes.EthPendingBlockNumber) From 5e025d60eb133f35ebab2e44ff64a53c287e175a Mon Sep 17 00:00:00 2001 From: Ramiro Carlucho Date: Thu, 29 Jul 2021 10:07:29 -0300 Subject: [PATCH 19/33] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Renames and log changes Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- ethereum/rpc/namespaces/eth/api.go | 4 ++-- ethereum/rpc/namespaces/miner/api.go | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index 7af7d47b87..ac34ee7b4b 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -145,11 +145,11 @@ func (e *PublicAPI) Syncing() (interface{}, error) { func (e *PublicAPI) Coinbase() (string, error) { e.logger.Debug("eth_coinbase") - toAddr, err := e.backend.GetCoinbase() + coinbase, err := e.backend.GetCoinbase() if err != nil { return "", err } - ethAddr := common.BytesToAddress(toAddr.Bytes()) + ethAddr := common.BytesToAddress(coinbase.Bytes()) return ethAddr.Hex(), nil } diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 98c5aa8da1..0edc924493 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -49,11 +49,10 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { delAddr, err := api.backend.GetCoinbase() if err != nil { - api.logger.Debug("failed to get address") + api.logger.Debug("failed to get coinbase address", "error", err.Error()) return false } - api.logger.Debug("Etherbase account ", delAddr.String()) withdrawAddr := sdk.AccAddress(etherbase.Bytes()) msg := distributiontypes.NewMsgSetWithdrawAddress(delAddr, withdrawAddr) @@ -132,7 +131,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - api.logger.Info("Broadcasted tx to set delegator's withdraw address.", "hash", tmHash) + api.logger.Debug("broadcasted tx to set miner withdraw address (etherbase)", "hash", tmHash.String()) return true } From e89bc48eeab41c0844eaca314e557fc2c8e649b9 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Fri, 30 Jul 2021 19:56:40 +0200 Subject: [PATCH 20/33] pair programming session --- ethereum/rpc/namespaces/miner/api.go | 53 +++++++++++++++++++++------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 0edc924493..e20d56f7f7 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -9,12 +9,17 @@ import ( "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" + + // txtypes "github.com/cosmos/cosmos-sdk/types/tx" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/tendermint/tendermint/libs/log" tmtypes "github.com/tendermint/tendermint/types" + "github.com/tharsis/ethermint/ethereum/rpc/backend" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" rpctypes "github.com/tharsis/ethermint/ethereum/rpc/types" @@ -53,7 +58,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - withdrawAddr := sdk.AccAddress(etherbase.Bytes()) msg := distributiontypes.NewMsgSetWithdrawAddress(delAddr, withdrawAddr) @@ -79,8 +83,21 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } + // req := &txtypes.SimulateRequest{ + // TxBytes: nil, + // } + + // res, err := api.ethAPI.QueryClient().Simulate(api.ethAPI.Ctx(), req) + // if err != nil { + // api.logger.Debug("failed to simulate transaction to obtain gas estimation", "error", err.Error()) + // return false + // } + + // res.GasInfo.GasUsed + // TODO: is there a way to calculate this message fee and gas limit? value := big.NewInt(10000) + fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(value))} builder.SetFeeAmount(fees) builder.SetGasLimit(ethermint.DefaultRPCGasLimit) @@ -131,11 +148,14 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } + // ethermintd tx distribution withdraw-all-rewards + api.logger.Debug("broadcasted tx to set miner withdraw address (etherbase)", "hash", tmHash.String()) return true } // SetGasPrice sets the minimum accepted gas price for the miner. +// NOTE: explicar que solo recibe enteros func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { api.logger.Info(api.ctx.Viper.ConfigFileUsed()) appConf, err := config.ParseConfig(api.ctx.Viper) @@ -143,21 +163,28 @@ func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { api.logger.Error("failed to parse file.", "file", api.ctx.Viper.ConfigFileUsed(), "error:", err.Error()) return false } + // NOTE: To allow values less that 1 aphoton, we need to divide the gasPrice here using some constant // If we want to function the same as go-eth we should just use the gasPrice as an int without converting it - coinsValue := gasPrice.ToInt().String() - unit, err := sdk.GetBaseDenom() - if err != nil { - api.logger.Debug("Could not get the denom of smallest unit registered.") - return false - } - c, err := sdk.ParseDecCoins(coinsValue + unit) - if err != nil { - api.logger.Error("failed to parse coins", "coins", coinsValue, "error", err.Error()) - return false + + var unit string + minGasPrices := appConf.GetMinGasPrices() + + // fetch the base denom from the sdk Config in case it's not currently defined on the node config + if len(minGasPrices) == 0 || minGasPrices.Empty() { + unit, err = sdk.GetBaseDenom() + if err != nil { + api.logger.Debug("Could not get the denom of smallest unit registered.") + return false + } + } else { + unit = minGasPrices[0].Denom } - appConf.SetMinGasPrices(c) + + c := sdk.NewDecCoin(unit, sdk.NewIntFromBigInt(gasPrice.ToInt())) + + appConf.SetMinGasPrices(sdk.DecCoins{c}) config.WriteConfigFile(api.ctx.Viper.ConfigFileUsed(), appConf) - api.logger.Info("Your configuration file was modified. Please RESTART your node.", "value", coinsValue+unit) + api.logger.Info("Your configuration file was modified. Please RESTART your node.", "gas-price", c.String()) return true } From f243ad8a2f00bb42655ba7c4dddb232026169334 Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Fri, 30 Jul 2021 16:13:59 -0300 Subject: [PATCH 21/33] get gas --- ethereum/rpc/namespaces/miner/api.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index e20d56f7f7..fad0911e78 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -94,14 +94,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { // } // res.GasInfo.GasUsed - - // TODO: is there a way to calculate this message fee and gas limit? - value := big.NewInt(10000) - - fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(value))} - builder.SetFeeAmount(fees) - builder.SetGasLimit(ethermint.DefaultRPCGasLimit) - delCommonAddr := common.BytesToAddress(delAddr.Bytes()) nonce, err := api.ethAPI.GetTransactionCount(delCommonAddr, rpctypes.EthPendingBlockNumber) if err != nil { @@ -116,6 +108,18 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { WithTxConfig(api.ethAPI.ClientCtx().TxConfig). WithSequence(uint64(*nonce)) + _, gas, err := tx.CalculateGas(api.ethAPI.ClientCtx(), txFactory, msg) + if err != nil { + api.logger.Debug("failed to calculate gas", "error", err.Error()) + } + //txFactory = txFactory.WithGas(gas) + + // TODO: is there a way to calculate this message fee and gas limit? + value := big.NewInt(10000) + fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(value))} + builder.SetFeeAmount(fees) + builder.SetGasLimit(ethermint.DefaultRPCGasLimit) + keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(delAddr) if err != nil { return false From ef576c584b598847de5d2f776c64c5ddfa5ddf4a Mon Sep 17 00:00:00 2001 From: Guillermo Date: Mon, 2 Aug 2021 16:10:03 +0200 Subject: [PATCH 22/33] Set gas prices note added --- ethereum/rpc/namespaces/miner/api.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index fad0911e78..a01f6fbc48 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -115,7 +115,8 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { //txFactory = txFactory.WithGas(gas) // TODO: is there a way to calculate this message fee and gas limit? - value := big.NewInt(10000) + // value := big.NewInt(gas) + value := new(big.Int).SetUint64(gas) fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(value))} builder.SetFeeAmount(fees) builder.SetGasLimit(ethermint.DefaultRPCGasLimit) @@ -159,7 +160,8 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { } // SetGasPrice sets the minimum accepted gas price for the miner. -// NOTE: explicar que solo recibe enteros +// NOTE: this function accepts only integers to have the same interface than go-eth +// to use float values, the gas prices must be configured using the configuration file func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { api.logger.Info(api.ctx.Viper.ConfigFileUsed()) appConf, err := config.ParseConfig(api.ctx.Viper) @@ -168,9 +170,6 @@ func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { return false } - // NOTE: To allow values less that 1 aphoton, we need to divide the gasPrice here using some constant - // If we want to function the same as go-eth we should just use the gasPrice as an int without converting it - var unit string minGasPrices := appConf.GetMinGasPrices() From adb4cf92f164431d47e0d2fe78021257b20e48b4 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Mon, 2 Aug 2021 17:00:12 +0200 Subject: [PATCH 23/33] Setting fess and max gas --- ethereum/rpc/namespaces/miner/api.go | 42 +++++++++++++++------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index a01f6fbc48..b5c8340e5d 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -2,6 +2,7 @@ package miner import ( "errors" + "fmt" "math/big" "github.com/cosmos/cosmos-sdk/client/flags" @@ -10,7 +11,6 @@ import ( "github.com/cosmos/cosmos-sdk/server/config" sdk "github.com/cosmos/cosmos-sdk/types" - // txtypes "github.com/cosmos/cosmos-sdk/types/tx" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types" @@ -23,7 +23,6 @@ import ( "github.com/tharsis/ethermint/ethereum/rpc/backend" "github.com/tharsis/ethermint/ethereum/rpc/namespaces/eth" rpctypes "github.com/tharsis/ethermint/ethereum/rpc/types" - ethermint "github.com/tharsis/ethermint/types" ) // API is the miner prefixed set of APIs in the Miner JSON-RPC spec. @@ -83,17 +82,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - // req := &txtypes.SimulateRequest{ - // TxBytes: nil, - // } - - // res, err := api.ethAPI.QueryClient().Simulate(api.ethAPI.Ctx(), req) - // if err != nil { - // api.logger.Debug("failed to simulate transaction to obtain gas estimation", "error", err.Error()) - // return false - // } - - // res.GasInfo.GasUsed delCommonAddr := common.BytesToAddress(delAddr.Bytes()) nonce, err := api.ethAPI.GetTransactionCount(delCommonAddr, rpctypes.EthPendingBlockNumber) if err != nil { @@ -106,20 +94,36 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { WithChainID(api.ethAPI.ClientCtx().ChainID). WithKeybase(api.ethAPI.ClientCtx().Keyring). WithTxConfig(api.ethAPI.ClientCtx().TxConfig). - WithSequence(uint64(*nonce)) + WithSequence(uint64(*nonce)). + WithGasAdjustment(1.11) _, gas, err := tx.CalculateGas(api.ethAPI.ClientCtx(), txFactory, msg) if err != nil { api.logger.Debug("failed to calculate gas", "error", err.Error()) + return false + } + + api.logger.Error("gas", "value", fmt.Sprintf("%d", gas)) + txFactory = txFactory.WithGas(gas) + + // Fetch minimun gas price to calculate fees using the configuration. + appConf, err := config.ParseConfig(api.ctx.Viper) + if err != nil { + api.logger.Error("failed to parse file.", "file", api.ctx.Viper.ConfigFileUsed(), "error:", err.Error()) + return false + } + + minGasPrices := appConf.GetMinGasPrices() + if len(minGasPrices) == 0 || minGasPrices.Empty() { + api.logger.Debug("the minimun fee is not set") + return false } - //txFactory = txFactory.WithGas(gas) + minGasPriceValue := minGasPrices[0].Amount - // TODO: is there a way to calculate this message fee and gas limit? - // value := big.NewInt(gas) - value := new(big.Int).SetUint64(gas) + value := new(big.Int).SetUint64(gas * minGasPriceValue.Ceil().TruncateInt().Uint64()) fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(value))} builder.SetFeeAmount(fees) - builder.SetGasLimit(ethermint.DefaultRPCGasLimit) + builder.SetGasLimit(gas) keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(delAddr) if err != nil { From 0faf64e295d5f7025fd8fe5a6840f1e5334561de Mon Sep 17 00:00:00 2001 From: ramacarlucho Date: Mon, 2 Aug 2021 16:44:10 -0300 Subject: [PATCH 24/33] delete unnecessary log --- ethereum/rpc/namespaces/miner/api.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index b5c8340e5d..6ca444f88a 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -2,7 +2,6 @@ package miner import ( "errors" - "fmt" "math/big" "github.com/cosmos/cosmos-sdk/client/flags" @@ -103,7 +102,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - api.logger.Error("gas", "value", fmt.Sprintf("%d", gas)) txFactory = txFactory.WithGas(gas) // Fetch minimun gas price to calculate fees using the configuration. From 7757254844f2b785f814a2a4546c114a30251ef6 Mon Sep 17 00:00:00 2001 From: Ramiro Carlucho Date: Tue, 3 Aug 2021 14:11:56 -0300 Subject: [PATCH 25/33] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit return false in case of error Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- ethereum/rpc/namespaces/miner/api.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 6ca444f88a..da966bab73 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -68,11 +68,13 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { builder, ok := api.ethAPI.ClientCtx().TxConfig.NewTxBuilder().(authtx.ExtensionOptionsTxBuilder) if !ok { api.logger.Debug("clientCtx.TxConfig.NewTxBuilder returns unsupported builder", "error", err.Error()) + return false } err = builder.SetMsgs(msg) if err != nil { api.logger.Error("builder.SetMsgs failed", "error", err.Error()) + return false } denom, err := sdk.GetBaseDenom() From 1a09f3f8abbc43660d3d9b2ac1f65c78d3359b36 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Tue, 3 Aug 2021 19:21:47 +0200 Subject: [PATCH 26/33] Suggested changes applied --- ethereum/rpc/namespaces/eth/api.go | 6 +++--- ethereum/rpc/namespaces/miner/api.go | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index ac34ee7b4b..aa06ff51c5 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -365,7 +365,7 @@ func (e *PublicAPI) SendTransaction(args rpctypes.SendTxArgs) (common.Hash, erro return common.Hash{}, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error()) } - args, err = e.SetTxDefaults(args) + args, err = e.setTxDefaults(args) if err != nil { return common.Hash{}, err } @@ -983,9 +983,9 @@ func (e *PublicAPI) GetProof(address common.Address, storageKeys []string, block }, nil } -// SetTxDefaults populates tx message with default values in case they are not +// setTxDefaults populates tx message with default values in case they are not // provided on the args -func (e *PublicAPI) SetTxDefaults(args rpctypes.SendTxArgs) (rpctypes.SendTxArgs, error) { +func (e *PublicAPI) setTxDefaults(args rpctypes.SendTxArgs) (rpctypes.SendTxArgs, error) { if args.GasPrice == nil { // TODO: Change to either: diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index da966bab73..96ae4fe1f9 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -127,6 +127,7 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { keyInfo, err := api.ethAPI.ClientCtx().Keyring.KeyByAddress(delAddr) if err != nil { + api.logger.Debug("failed to get the wallet address using the keyring", "error", err.Error()) return false } From 2987a8b328666cfcfcce4fa8d99deedc90bac1c1 Mon Sep 17 00:00:00 2001 From: Guillermo Date: Tue, 3 Aug 2021 19:49:04 +0200 Subject: [PATCH 27/33] Updated changelog and json_rpc docs --- CHANGELOG.md | 1 + docs/basics/json_rpc.md | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c34e80ddd..c21a120f14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [tharsis#272](https://github.com/tharsis/ethermint/pull/272) do binary search to estimate gas accurately * (rpc) [#313](https://github.com/tharsis/ethermint/pull/313) Implement internal debug namespace (Not including logger functions nor traces). * (rpc) [#349](https://github.com/tharsis/ethermint/pull/349) Implement configurable JSON-RPC APIs to manage enabled namespaces. +* (rpc) [#377](https://github.com/tharsis/ethermint/pull/377) Implement miner namespace, `SetEtherbase` and `SetGasPrice` are working as intended. All the other calls are not relevant to `ethermint`, these calls use the same interface as `go-eth` but return `unsupported`. ### Bug Fixes diff --git a/docs/basics/json_rpc.md b/docs/basics/json_rpc.md index 315e4994f5..5e51df0982 100644 --- a/docs/basics/json_rpc.md +++ b/docs/basics/json_rpc.md @@ -157,12 +157,13 @@ ethermintd start --evm-rpc.api eth,txpool,personal,net,debug,web3 | `les_latestCheckpoint` | Les | | | | `les_getCheckpoint` | Les | | | | `les_getCheckpointContractAddress` | Les | | | -| `miner_getHashrate` | Miner | | | -| `miner_setExtra` | Miner | | | -| `miner_setGasPrice` | Miner | | | -| `miner_start` | Miner | | | -| `miner_stop` | Miner | | | -| `miner_setEtherbase` | Miner | | | +| `miner_getHashrate` | Miner | N/A | Not relevant to Ethermint | +| `miner_setExtra` | Miner | N/A | Not relevant to Ethermint | +| `miner_setGasPrice` | Miner | ✔ | | +| `miner_start` | Miner | N/A | Not relevant to Ethermint | +| `miner_stop` | Miner | N/A | Not relevant to Ethermint | +| `miner_setGasLimit` | Miner | N/A | Not relevant to Ethermint | +| `miner_setEtherbase` | Miner | ✔ | | | `txpool_content` | TXPool | ✔ | | | `txpool_inspect` | TXPool | ✔ | | | `txpool_status` | TXPool | ✔ | | From 6acf94c9f449ea994cfc6dc01eb2819f984b505c Mon Sep 17 00:00:00 2001 From: Ramiro Carlucho Date: Tue, 3 Aug 2021 15:41:56 -0300 Subject: [PATCH 28/33] Update CHANGELOG.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c21a120f14..cfde85c776 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,7 +76,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [tharsis#272](https://github.com/tharsis/ethermint/pull/272) do binary search to estimate gas accurately * (rpc) [#313](https://github.com/tharsis/ethermint/pull/313) Implement internal debug namespace (Not including logger functions nor traces). * (rpc) [#349](https://github.com/tharsis/ethermint/pull/349) Implement configurable JSON-RPC APIs to manage enabled namespaces. -* (rpc) [#377](https://github.com/tharsis/ethermint/pull/377) Implement miner namespace, `SetEtherbase` and `SetGasPrice` are working as intended. All the other calls are not relevant to `ethermint`, these calls use the same interface as `go-eth` but return `unsupported`. +* (rpc) [#377](https://github.com/tharsis/ethermint/pull/377) Implement `miner_` namespace. `miner_setEtherbase` and `miner_setGasPrice` are working as intended. All the other calls are not applicable and return `unsupported`. ### Bug Fixes From 11c56f9370e7fd99a276555b165d504bc480c821 Mon Sep 17 00:00:00 2001 From: Guillermo Paoletti Date: Wed, 4 Aug 2021 09:24:06 +0200 Subject: [PATCH 29/33] Update ethereum/rpc/namespaces/miner/api.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- ethereum/rpc/namespaces/miner/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 96ae4fe1f9..cf119598f9 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -41,7 +41,7 @@ func NewMinerAPI( return &API{ ctx: ctx, ethAPI: ethAPI, - logger: ctx.Logger.With("module", "miner"), + logger: ctx.Logger.With("api", "miner"), backend: backend, } } From 106d70940ecd79ad1fcbe9ae0f580b11bc464ce1 Mon Sep 17 00:00:00 2001 From: Guillermo Paoletti Date: Wed, 4 Aug 2021 09:24:15 +0200 Subject: [PATCH 30/33] Update ethereum/rpc/namespaces/miner/api.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- ethereum/rpc/namespaces/miner/api.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index cf119598f9..1e0749919d 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -158,7 +158,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - // ethermintd tx distribution withdraw-all-rewards api.logger.Debug("broadcasted tx to set miner withdraw address (etherbase)", "hash", tmHash.String()) return true From bbd1567393caae6cdc5272a81462778eccc0e4d7 Mon Sep 17 00:00:00 2001 From: Guillermo Paoletti Date: Wed, 4 Aug 2021 09:24:41 +0200 Subject: [PATCH 31/33] Update ethereum/rpc/namespaces/miner/api.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- ethereum/rpc/namespaces/miner/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 1e0749919d..6f2e468248 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -170,7 +170,7 @@ func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { api.logger.Info(api.ctx.Viper.ConfigFileUsed()) appConf, err := config.ParseConfig(api.ctx.Viper) if err != nil { - api.logger.Error("failed to parse file.", "file", api.ctx.Viper.ConfigFileUsed(), "error:", err.Error()) + api.logger.Debug("failed to parse config file", "file", api.ctx.Viper.ConfigFileUsed(), "error", err.Error()) return false } From a5e5fd3be65ba7918be50d5a75a5388c46817651 Mon Sep 17 00:00:00 2001 From: Guillermo Paoletti Date: Wed, 4 Aug 2021 09:24:51 +0200 Subject: [PATCH 32/33] Update ethereum/rpc/namespaces/miner/api.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- ethereum/rpc/namespaces/miner/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index 6f2e468248..ab6bc687ba 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -181,7 +181,7 @@ func (api *API) SetGasPrice(gasPrice hexutil.Big) bool { if len(minGasPrices) == 0 || minGasPrices.Empty() { unit, err = sdk.GetBaseDenom() if err != nil { - api.logger.Debug("Could not get the denom of smallest unit registered.") + api.logger.Debug("could not get the denom of smallest unit registered", "error", err.Error()) return false } } else { From 41b51f6d37d895822bef298405d9f0fd79e002fa Mon Sep 17 00:00:00 2001 From: Guillermo Date: Wed, 4 Aug 2021 09:58:55 +0200 Subject: [PATCH 33/33] Using the same coin denom as the gas price for the fee --- ethereum/rpc/namespaces/miner/api.go | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/ethereum/rpc/namespaces/miner/api.go b/ethereum/rpc/namespaces/miner/api.go index ab6bc687ba..936b5d0014 100644 --- a/ethereum/rpc/namespaces/miner/api.go +++ b/ethereum/rpc/namespaces/miner/api.go @@ -77,12 +77,21 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - denom, err := sdk.GetBaseDenom() + // Fetch minimun gas price to calculate fees using the configuration. + appConf, err := config.ParseConfig(api.ctx.Viper) if err != nil { - api.logger.Debug("Could not get the denom of smallest unit registered.") + api.logger.Error("failed to parse file.", "file", api.ctx.Viper.ConfigFileUsed(), "error:", err.Error()) return false } + minGasPrices := appConf.GetMinGasPrices() + if len(minGasPrices) == 0 || minGasPrices.Empty() { + api.logger.Debug("the minimun fee is not set") + return false + } + minGasPriceValue := minGasPrices[0].Amount + denom := minGasPrices[0].Denom + delCommonAddr := common.BytesToAddress(delAddr.Bytes()) nonce, err := api.ethAPI.GetTransactionCount(delCommonAddr, rpctypes.EthPendingBlockNumber) if err != nil { @@ -106,20 +115,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { txFactory = txFactory.WithGas(gas) - // Fetch minimun gas price to calculate fees using the configuration. - appConf, err := config.ParseConfig(api.ctx.Viper) - if err != nil { - api.logger.Error("failed to parse file.", "file", api.ctx.Viper.ConfigFileUsed(), "error:", err.Error()) - return false - } - - minGasPrices := appConf.GetMinGasPrices() - if len(minGasPrices) == 0 || minGasPrices.Empty() { - api.logger.Debug("the minimun fee is not set") - return false - } - minGasPriceValue := minGasPrices[0].Amount - value := new(big.Int).SetUint64(gas * minGasPriceValue.Ceil().TruncateInt().Uint64()) fees := sdk.Coins{sdk.NewCoin(denom, sdk.NewIntFromBigInt(value))} builder.SetFeeAmount(fees) @@ -158,7 +153,6 @@ func (api *API) SetEtherbase(etherbase common.Address) bool { return false } - api.logger.Debug("broadcasted tx to set miner withdraw address (etherbase)", "hash", tmHash.String()) return true }