Get eth block by height

This commit is contained in:
Martin Boehm 2018-03-26 13:57:40 +02:00
parent a5c4dd0d70
commit 3dcbaeb34e
2 changed files with 25 additions and 2 deletions

View File

@ -284,7 +284,13 @@ func (b *EthRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
defer cancel()
var raw json.RawMessage
err := b.rpc.CallContext(ctx, &raw, "eth_getBlockByHash", ethcommon.HexToHash(hash), true)
var err error
if hash != "" {
err = b.rpc.CallContext(ctx, &raw, "eth_getBlockByHash", ethcommon.HexToHash(hash), true)
} else {
err = b.rpc.CallContext(ctx, &raw, "eth_getBlockByNumber", fmt.Sprintf("%#x", height), true)
}
if err != nil {
return nil, err
} else if len(raw) == 0 {

View File

@ -244,7 +244,7 @@ func TestEthRPC_GetBlock(t *testing.T) {
wantErr bool
}{
{
name: "2870000",
name: "2870000 by hash",
fields: fields{
b: setupEthRPC(),
},
@ -260,6 +260,23 @@ func TestEthRPC_GetBlock(t *testing.T) {
},
wantTxCount: 12,
},
{
name: "2870000 by height",
fields: fields{
b: setupEthRPC(),
},
args: args{
height: 2870000,
},
want: &bchain.Block{
BlockHeader: bchain.BlockHeader{
Hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895",
Height: 2870000,
Confirmations: int(uint32(bh.Number.Uint64()) - 2870000),
},
},
wantTxCount: 12,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {