rpc: add BlockByHash to Client (#4923)

Ethermint currently has to maintain a map height-> block hash on the store (see here) as it needs to expose the eth_getBlockByHash JSON-RPC query for Web3 compatibility. This query is currently not supported by the tendermint RPC client.
This commit is contained in:
Federico Kunze
2020-06-01 09:04:45 +04:00
committed by GitHub
parent 0572315f77
commit da924fc62d
10 changed files with 103 additions and 7 deletions
+12
View File
@@ -367,6 +367,18 @@ func (c *baseRPCClient) Block(height *int64) (*ctypes.ResultBlock, error) {
return result, nil
}
func (c *baseRPCClient) BlockByHash(hash []byte) (*ctypes.ResultBlock, error) {
result := new(ctypes.ResultBlock)
params := map[string]interface{}{
"hash": hash,
}
_, err := c.caller.Call("block_by_hash", params, result)
if err != nil {
return nil, err
}
return result, nil
}
func (c *baseRPCClient) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) {
result := new(ctypes.ResultBlockResults)
params := make(map[string]interface{})
+1
View File
@@ -65,6 +65,7 @@ type ABCIClient interface {
// and prove anything about the chain.
type SignClient interface {
Block(height *int64) (*ctypes.ResultBlock, error)
BlockByHash(hash []byte) (*ctypes.ResultBlock, error)
BlockResults(height *int64) (*ctypes.ResultBlockResults, error)
Commit(height *int64) (*ctypes.ResultCommit, error)
Validators(height *int64, page, perPage int) (*ctypes.ResultValidators, error)
+4
View File
@@ -144,6 +144,10 @@ func (c *Local) Block(height *int64) (*ctypes.ResultBlock, error) {
return core.Block(c.ctx, height)
}
func (c *Local) BlockByHash(hash []byte) (*ctypes.ResultBlock, error) {
return core.BlockByHash(c.ctx, hash)
}
func (c *Local) BlockResults(height *int64) (*ctypes.ResultBlockResults, error) {
return core.BlockResults(c.ctx, height)
}
+4
View File
@@ -150,6 +150,10 @@ func (c Client) Block(height *int64) (*ctypes.ResultBlock, error) {
return core.Block(&rpctypes.Context{}, height)
}
func (c Client) BlockByHash(hash []byte) (*ctypes.ResultBlock, error) {
return core.BlockByHash(&rpctypes.Context{}, hash)
}
func (c Client) Commit(height *int64) (*ctypes.ResultCommit, error) {
return core.Commit(&rpctypes.Context{}, height)
}
+4
View File
@@ -249,6 +249,10 @@ func TestAppCalls(t *testing.T) {
assert.True(len(appHash) > 0)
assert.EqualValues(apph, block.Block.Header.Height)
blockByHash, err := c.BlockByHash(block.BlockID.Hash)
require.NoError(err)
require.Equal(block, blockByHash)
// now check the results
blockResults, err := c.BlockResults(&txh)
require.Nil(err, "%d: %+v", i, err)