Commit cd30eb09 authored by Hamdi Allam's avatar Hamdi Allam

expand on node.EthClient ot retrieve header by hash

parent 142e4f53
......@@ -15,7 +15,7 @@ func clampBigInt(start, end *big.Int, size uint64) *big.Int {
return end
}
// we result the allocated temp as the new end
// we re-use the allocated temp as the new end
temp.Add(start, big.NewInt(int64(size-1)))
return temp
}
......@@ -2,11 +2,14 @@ package node
import (
"context"
"errors"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
)
......@@ -22,7 +25,9 @@ const (
type EthClient interface {
FinalizedBlockHeight() (*big.Int, error)
BlockHeadersByRange(*big.Int, *big.Int) ([]*types.Header, error)
BlockHeaderByHash(common.Hash) (*types.Header, error)
RawRpcClient() *rpc.Client
}
......@@ -53,13 +58,33 @@ func (c *client) FinalizedBlockHeight() (*big.Int, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
var block *types.Block
err := c.rpcClient.CallContext(ctxwt, block, "eth_getBlockByNumber", "finalized", false)
// Local devnet is having issues with the "finalized" block tag. Switch to "latest"
// to iterate faster locally but this needs to be updated
header := new(types.Header)
err := c.rpcClient.CallContext(ctxwt, header, "eth_getBlockByNumber", "latest", false)
if err != nil {
return nil, err
}
return header.Number, nil
}
// BlockHeaderByHash retrieves the block header attributed to the supplied hash
func (c *client) BlockHeaderByHash(hash common.Hash) (*types.Header, error) {
ctxwt, cancel := context.WithTimeout(context.Background(), defaultRequestTimeout)
defer cancel()
header, err := ethclient.NewClient(c.rpcClient).HeaderByHash(ctxwt, hash)
if err != nil {
return nil, err
}
return block.Number(), nil
// sanity check on the data returned
if header.Hash() != hash {
return nil, errors.New("header mismatch")
}
return header, nil
}
// BlockHeadersByRange will retrieve block headers within the specified range -- includsive. No restrictions
......
......@@ -20,9 +20,8 @@ type Fetcher struct {
// NewFetcher instantiates a new instance of Fetcher against the supplied rpc client.
// The Fetcher will start fetching blocks starting from the supplied header unless
// nil, indicating genesis.
func NewFetcher(ethClient EthClient, fromHeader *types.Header) (*Fetcher, error) {
fetcher := &Fetcher{ethClient: ethClient, lastHeader: fromHeader}
return fetcher, nil
func NewFetcher(ethClient EthClient, fromHeader *types.Header) *Fetcher {
return &Fetcher{ethClient: ethClient, lastHeader: fromHeader}
}
// NextConfirmedHeaders retrives the next set of headers that have been
......@@ -50,7 +49,7 @@ func (f *Fetcher) NextFinalizedHeaders() ([]*types.Header, error) {
return nil, err
}
numHeaders := int64(len(headers))
numHeaders := len(headers)
if numHeaders == 0 {
return nil, nil
} else if f.lastHeader != nil && headers[0].ParentHash != f.lastHeader.Hash() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment