Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

les, core: introduce les4 #20227

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
18 changes: 15 additions & 3 deletions core/forkid/forkid.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
)
Expand All @@ -44,6 +44,18 @@ var (
ErrLocalIncompatibleOrStale = errors.New("local incompatible or needs update")
)

// Blockchain defines all necessary method to build a forkID.
type Blockchain interface {
// Config retrieves the chain's fork configuration.
Config() *params.ChainConfig

// Genesis retrieves the chain's genesis block.
Genesis() *types.Block

// CurrentHeader retrieves the current head header of the canonical chain.
CurrentHeader() *types.Header
}

// ID is a fork identifier as defined by EIP-2124.
type ID struct {
Hash [4]byte // CRC32 checksum of the genesis block and passed fork block numbers
Expand All @@ -54,7 +66,7 @@ type ID struct {
type Filter func(id ID) error

// NewID calculates the Ethereum fork ID from the chain config and head.
func NewID(chain *core.BlockChain) ID {
func NewID(chain Blockchain) ID {
return newID(
chain.Config(),
chain.Genesis().Hash(),
Expand Down Expand Up @@ -85,7 +97,7 @@ func newID(config *params.ChainConfig, genesis common.Hash, head uint64) ID {

// NewFilter creates a filter that returns if a fork ID should be rejected or not
// based on the local chain's status.
func NewFilter(chain *core.BlockChain) Filter {
func NewFilter(chain Blockchain) Filter {
return newFilter(
chain.Config(),
chain.Genesis().Hash(),
Expand Down
27 changes: 15 additions & 12 deletions les/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/mclock"
"github.com/ethereum/go-ethereum/core/forkid"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/light"
Expand All @@ -35,6 +36,7 @@ import (
// responses.
type clientHandler struct {
ulc *ulc
forkFilter forkid.Filter
checkpoint *params.TrustedCheckpoint
fetcher *lightFetcher
downloader *downloader.Downloader
Expand All @@ -47,6 +49,7 @@ type clientHandler struct {

func newClientHandler(ulcServers []string, ulcFraction int, checkpoint *params.TrustedCheckpoint, backend *LightEthereum) *clientHandler {
handler := &clientHandler{
forkFilter: forkid.NewFilter(backend.blockchain),
checkpoint: checkpoint,
backend: backend,
closeCh: make(chan struct{}),
Expand Down Expand Up @@ -107,7 +110,7 @@ func (h *clientHandler) handle(p *peer) error {
number = head.Number.Uint64()
td = h.backend.blockchain.GetTd(hash, number)
)
if err := p.Handshake(td, hash, number, h.backend.blockchain.Genesis().Hash(), nil); err != nil {
if err := p.handshakeWithServer(td, hash, number, h.backend.blockchain.Genesis().Hash(), forkid.NewID(h.backend.blockchain), h.forkFilter); err != nil {
p.Log().Debug("Light Ethereum handshake failed", "err", err)
return err
}
Expand Down Expand Up @@ -159,8 +162,8 @@ func (h *clientHandler) handleMsg(p *peer) error {
var deliverMsg *Msg

// Handle the message depending on its contents
switch msg.Code {
case AnnounceMsg:
switch {
case msg.Code == AnnounceMsg:
p.Log().Trace("Received announce message")
var req announceData
if err := msg.Decode(&req); err != nil {
Expand Down Expand Up @@ -189,7 +192,7 @@ func (h *clientHandler) handleMsg(p *peer) error {
p.Log().Trace("Announce message content", "number", req.Number, "hash", req.Hash, "td", req.Td, "reorg", req.ReorgDepth)
h.fetcher.announce(p, &req)
}
case BlockHeadersMsg:
case msg.Code == BlockHeadersMsg:
p.Log().Trace("Received block header response message")
var resp struct {
ReqID, BV uint64
Expand All @@ -206,7 +209,7 @@ func (h *clientHandler) handleMsg(p *peer) error {
log.Debug("Failed to deliver headers", "err", err)
}
}
case BlockBodiesMsg:
case msg.Code == BlockBodiesMsg:
p.Log().Trace("Received block bodies response")
var resp struct {
ReqID, BV uint64
Expand All @@ -221,7 +224,7 @@ func (h *clientHandler) handleMsg(p *peer) error {
ReqID: resp.ReqID,
Obj: resp.Data,
}
case CodeMsg:
case msg.Code == CodeMsg:
p.Log().Trace("Received code response")
var resp struct {
ReqID, BV uint64
Expand All @@ -236,7 +239,7 @@ func (h *clientHandler) handleMsg(p *peer) error {
ReqID: resp.ReqID,
Obj: resp.Data,
}
case ReceiptsMsg:
case msg.Code == ReceiptsMsg:
p.Log().Trace("Received receipts response")
var resp struct {
ReqID, BV uint64
Expand All @@ -251,7 +254,7 @@ func (h *clientHandler) handleMsg(p *peer) error {
ReqID: resp.ReqID,
Obj: resp.Receipts,
}
case ProofsV2Msg:
case msg.Code == ProofsV2Msg:
p.Log().Trace("Received les/2 proofs response")
var resp struct {
ReqID, BV uint64
Expand All @@ -266,7 +269,7 @@ func (h *clientHandler) handleMsg(p *peer) error {
ReqID: resp.ReqID,
Obj: resp.Data,
}
case HelperTrieProofsMsg:
case msg.Code == HelperTrieProofsMsg:
p.Log().Trace("Received helper trie proof response")
var resp struct {
ReqID, BV uint64
Expand All @@ -281,7 +284,7 @@ func (h *clientHandler) handleMsg(p *peer) error {
ReqID: resp.ReqID,
Obj: resp.Data,
}
case TxStatusMsg:
case msg.Code == TxStatusMsg:
p.Log().Trace("Received tx status response")
var resp struct {
ReqID, BV uint64
Expand All @@ -296,11 +299,11 @@ func (h *clientHandler) handleMsg(p *peer) error {
ReqID: resp.ReqID,
Obj: resp.Status,
}
case StopMsg:
case msg.Code == StopMsg && p.version >= lpv3:
p.freezeServer(true)
h.backend.retriever.frozen(p)
p.Log().Debug("Service stopped")
case ResumeMsg:
case msg.Code == ResumeMsg && p.version >= lpv3:
var bv uint64
if err := msg.Decode(&bv); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err)
Expand Down
Loading