Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

rpc: implement eth_coinbase #325

Merged
merged 7 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion rpc/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 14 additions & 3 deletions rpc/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like it returns the coinbase address on the current block, not the coinbase address of this node?
https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_coinbase

not sure if this is functionally correct, what is this used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated it to return the validator address of the node 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :D

}

// Mining returns whether or not this node is currently mining. Always false.
Expand Down
12 changes: 12 additions & 0 deletions tests/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down