From e5364c48e952ddab577959b52f749f62025a0aa1 Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 8 Jun 2020 12:29:32 +0200 Subject: [PATCH 1/4] rpc: implement eth_coinbase --- rpc/backend.go | 4 +++- rpc/eth_api.go | 17 ++++++++++++++--- tests/rpc_test.go | 12 ++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/rpc/backend.go b/rpc/backend.go index 9362745c5..41a1b7a34 100644 --- a/rpc/backend.go +++ b/rpc/backend.go @@ -47,13 +47,15 @@ func NewEthermintBackend(cliCtx context.CLIContext) *EthermintBackend { // BlockNumber returns the current block number. func (e *EthermintBackend) BlockNumber() (hexutil.Uint64, error) { - res, _, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/blockNumber", types.ModuleName), nil) + res, height, err := e.cliCtx.QueryWithData(fmt.Sprintf("custom/%s/blockNumber", types.ModuleName), nil) if err != nil { return hexutil.Uint64(0), err } var out types.QueryResBlockNumber e.cliCtx.Codec.MustUnmarshalJSON(res, &out) + + e.cliCtx.WithHeight(height) return hexutil.Uint64(out.Number), nil } diff --git a/rpc/eth_api.go b/rpc/eth_api.go index 3f69b8400..1f0314e08 100644 --- a/rpc/eth_api.go +++ b/rpc/eth_api.go @@ -90,9 +90,20 @@ func (e *PublicEthAPI) Syncing() (interface{}, error) { }, nil } -// Coinbase returns this node's coinbase address. Not used in Ethermint. -func (e *PublicEthAPI) Coinbase() (addr common.Address) { - return +// Coinbase is the address that staking rewards will be send to (alias for Etherbase). +func (e *PublicEthAPI) Coinbase() (common.Address, error) { + height, err := e.BlockNumber() + if err != nil { + return common.Address{}, err + } + blockNumber := int64(height) + + block, err := e.cliCtx.Client.Block(&blockNumber) + if err != nil { + return common.Address{}, err + } + + return common.BytesToAddress(block.Block.ProposerAddress.Bytes()), nil } // Mining returns whether or not this node is currently mining. Always false. diff --git a/tests/rpc_test.go b/tests/rpc_test.go index 8ccfe46e6..7b5562920 100644 --- a/tests/rpc_test.go +++ b/tests/rpc_test.go @@ -139,6 +139,18 @@ func TestEth_blockNumber(t *testing.T) { t.Logf("Got block number: %s\n", res.String()) } +func TestEth_coinbase(t *testing.T) { + zeroAddress := hexutil.Bytes(ethcmn.Address{}.Bytes()) + rpcRes := call(t, "eth_coinbase", []string{}) + + var res hexutil.Bytes + err := res.UnmarshalJSON(rpcRes.Result) + require.NoError(t, err) + + t.Logf("Got coinbase block proposer: %s\n", res.String()) + require.NotEqual(t, zeroAddress.String(), res.String(), "expected: %s got: %s\n", zeroAddress.String(), res.String()) +} + func TestEth_GetBalance(t *testing.T) { rpcRes := call(t, "eth_getBalance", []string{addrA, zeroString}) From 48564f7ad578a9b263961a6bd15b1f39332178ae Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 8 Jun 2020 12:32:49 +0200 Subject: [PATCH 2/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ee10056e..6a3d053b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,6 +61,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * Remove evm `CodeKey` and `BlockKey`in favor of a prefix `Store`. * Set `BlockBloom` during `EndBlock` instead of `BeginBlock`. * `Commit` state object and `Finalize` storage after `InitGenesis` setup. +* (rpc) [\#325](https://github.com/ChainSafe/ethermint/pull/325) `eth_coinbase` JSON-RPC query now returns the block proposer's validator address. ### Features From 43a3800392118d1d0dc48232c31a7a2cacd5d2bd Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 8 Jun 2020 18:18:36 +0200 Subject: [PATCH 3/4] address comments from review --- rpc/eth_api.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rpc/eth_api.go b/rpc/eth_api.go index 1f0314e08..c10ef046a 100644 --- a/rpc/eth_api.go +++ b/rpc/eth_api.go @@ -92,18 +92,17 @@ func (e *PublicEthAPI) Syncing() (interface{}, error) { // Coinbase is the address that staking rewards will be send to (alias for Etherbase). func (e *PublicEthAPI) Coinbase() (common.Address, error) { - height, err := e.BlockNumber() + node, err := e.cliCtx.GetNode() if err != nil { return common.Address{}, err } - blockNumber := int64(height) - block, err := e.cliCtx.Client.Block(&blockNumber) + status, err := node.Status() if err != nil { return common.Address{}, err } - return common.BytesToAddress(block.Block.ProposerAddress.Bytes()), nil + return common.BytesToAddress(status.ValidatorInfo.Address.Bytes()), nil } // Mining returns whether or not this node is currently mining. Always false. From d82b76fb69315bc10561abdf4311bffa9a47c8cb Mon Sep 17 00:00:00 2001 From: Federico Kunze Date: Mon, 8 Jun 2020 18:20:32 +0200 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a3d053b9..d091783f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -61,7 +61,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * Remove evm `CodeKey` and `BlockKey`in favor of a prefix `Store`. * Set `BlockBloom` during `EndBlock` instead of `BeginBlock`. * `Commit` state object and `Finalize` storage after `InitGenesis` setup. -* (rpc) [\#325](https://github.com/ChainSafe/ethermint/pull/325) `eth_coinbase` JSON-RPC query now returns the block proposer's validator address. +* (rpc) [\#325](https://github.com/ChainSafe/ethermint/pull/325) `eth_coinbase` JSON-RPC query now returns the node's validator address. ### Features