diff --git a/les/backend.go b/les/backend.go index 96c828891cf6..54e150a712f5 100644 --- a/les/backend.go +++ b/les/backend.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/consensus" + istanbulBackend "github.com/ethereum/go-ethereum/consensus/istanbul/backend" "github.com/ethereum/go-ethereum/contract_comm" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/bloombits" @@ -55,12 +56,13 @@ type LightEthereum struct { shutdownChan chan bool // Handlers - peers *peerSet - txPool *light.TxPool - blockchain *light.LightChain - serverPool *serverPool - reqDist *requestDistributor - retriever *retrieveManager + peers *peerSet + txPool *light.TxPool + blockchain *light.LightChain + serverPool *serverPool + reqDist *requestDistributor + retriever *retrieveManager + chainreader *LightChainReader bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests bloomIndexer *core.ChainIndexer @@ -169,6 +171,17 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { return nil, err } leth.ApiBackend = &LesApiBackend{leth} + + leth.chainreader = &LightChainReader{ + config: leth.chainConfig, + blockchain: leth.blockchain, + } + + // If the engine is istanbul, then inject the blockchain + if istanbul, isIstanbul := leth.engine.(*istanbulBackend.Backend); isIstanbul { + istanbul.SetChain(leth.chainreader, nil) + } + return leth, nil } diff --git a/les/lightchainreader.go b/les/lightchainreader.go new file mode 100644 index 000000000000..9cc4683947dd --- /dev/null +++ b/les/lightchainreader.go @@ -0,0 +1,35 @@ +package les + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/light" + "github.com/ethereum/go-ethereum/params" +) + +type LightChainReader struct { + config *params.ChainConfig + blockchain *light.LightChain +} + +// Config returns the chain configuration. +func (lcr *LightChainReader) Config() *params.ChainConfig { + return lcr.config +} + +func (lcr *LightChainReader) CurrentHeader() *types.Header { + return lcr.blockchain.CurrentHeader() +} +func (lcr *LightChainReader) GetHeaderByNumber(number uint64) *types.Header { + return lcr.blockchain.GetHeaderByNumber(number) +} +func (lcr *LightChainReader) GetHeaderByHash(hash common.Hash) *types.Header { + return lcr.blockchain.GetHeaderByHash(hash) +} +func (lcr *LightChainReader) GetHeader(hash common.Hash, number uint64) *types.Header { + return lcr.blockchain.GetHeader(hash, number) +} +func (lcr *LightChainReader) GetBlock(hash common.Hash, number uint64) *types.Block { + panic("GetBlock cannot be called on LightChainReader") + return nil +}