les, light: changed names and abbreviations

This commit is contained in:
Zsolt Felfoldi 2017-10-11 20:44:22 +02:00
parent 21983ec47e
commit c170349973
12 changed files with 180 additions and 177 deletions

View file

@ -174,10 +174,10 @@ func (b *LesApiBackend) AccountManager() *accounts.Manager {
} }
func (b *LesApiBackend) BloomStatus() (uint64, uint64) { func (b *LesApiBackend) BloomStatus() (uint64, uint64) {
if b.eth.bbIndexer == nil { if b.eth.bloomIndexer == nil {
return 0, 0 return 0, 0
} }
sections, _, _ := b.eth.bbIndexer.Sections() sections, _, _ := b.eth.bloomIndexer.Sections()
return light.BloomTrieFrequency, sections return light.BloomTrieFrequency, sections
} }

View file

@ -63,7 +63,7 @@ type LightEthereum struct {
chainDb ethdb.Database // Block chain database chainDb ethdb.Database // Block chain database
bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests bloomRequests chan chan *bloombits.Retrieval // Channel receiving bloom data retrieval requests
bbIndexer, chtIndexer, bltIndexer *core.ChainIndexer bloomIndexer, chtIndexer, bloomTrieIndexer *core.ChainIndexer
ApiBackend *LesApiBackend ApiBackend *LesApiBackend
@ -102,19 +102,19 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
shutdownChan: make(chan bool), shutdownChan: make(chan bool),
networkId: config.NetworkId, networkId: config.NetworkId,
bloomRequests: make(chan chan *bloombits.Retrieval), bloomRequests: make(chan chan *bloombits.Retrieval),
bbIndexer: eth.NewBloomIndexer(chainDb, light.BloomTrieFrequency), bloomIndexer: eth.NewBloomIndexer(chainDb, light.BloomTrieFrequency),
chtIndexer: light.NewChtIndexer(chainDb, true), chtIndexer: light.NewChtIndexer(chainDb, true),
bltIndexer: light.NewBloomTrieIndexer(chainDb, true), bloomTrieIndexer: light.NewBloomTrieIndexer(chainDb, true),
} }
leth.relay = NewLesTxRelay(peers, leth.reqDist) leth.relay = NewLesTxRelay(peers, leth.reqDist)
leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg) leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg)
leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool) leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool)
leth.odr = NewLesOdr(chainDb, leth.chtIndexer, leth.bltIndexer, leth.bbIndexer, leth.retriever) leth.odr = NewLesOdr(chainDb, leth.chtIndexer, leth.bloomTrieIndexer, leth.bloomIndexer, leth.retriever)
if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine); err != nil { if leth.blockchain, err = light.NewLightChain(leth.odr, leth.chainConfig, leth.engine); err != nil {
return nil, err return nil, err
} }
leth.bbIndexer.Start(leth.blockchain) leth.bloomIndexer.Start(leth.blockchain)
// Rewind the chain in case of an incompatible config upgrade. // Rewind the chain in case of an incompatible config upgrade.
if compat, ok := genesisErr.(*params.ConfigCompatError); ok { if compat, ok := genesisErr.(*params.ConfigCompatError); ok {
log.Warn("Rewinding chain to upgrade configuration", "err", compat) log.Warn("Rewinding chain to upgrade configuration", "err", compat)
@ -233,14 +233,14 @@ func (s *LightEthereum) Start(srvr *p2p.Server) error {
// Ethereum protocol. // Ethereum protocol.
func (s *LightEthereum) Stop() error { func (s *LightEthereum) Stop() error {
s.odr.Stop() s.odr.Stop()
if s.bbIndexer != nil { if s.bloomIndexer != nil {
s.bbIndexer.Close() s.bloomIndexer.Close()
} }
if s.chtIndexer != nil { if s.chtIndexer != nil {
s.chtIndexer.Close() s.chtIndexer.Close()
} }
if s.bltIndexer != nil { if s.bloomTrieIndexer != nil {
s.bltIndexer.Close() s.bloomTrieIndexer.Close()
} }
s.blockchain.Stop() s.blockchain.Stop()
s.protocolManager.Stop() s.protocolManager.Stop()

View file

@ -57,7 +57,7 @@ const (
MaxReceiptFetch = 128 // Amount of transaction receipts to allow fetching per request MaxReceiptFetch = 128 // Amount of transaction receipts to allow fetching per request
MaxCodeFetch = 64 // Amount of contract codes to allow fetching per request MaxCodeFetch = 64 // Amount of contract codes to allow fetching per request
MaxProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request MaxProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request
MaxPPTProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request MaxHelperTrieProofsFetch = 64 // Amount of merkle proofs to be fetched per retrieval request
MaxTxSend = 64 // Amount of transactions to be send per request MaxTxSend = 64 // Amount of transactions to be send per request
MaxTxStatus = 256 // Amount of transactions to queried per request MaxTxStatus = 256 // Amount of transactions to queried per request
@ -318,7 +318,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
} }
} }
var reqList = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsV1Msg, SendTxMsg, SendTxV2Msg, GetTxStatusMsg, GetHeaderProofsMsg, GetProofsV2Msg, GetPPTProofsMsg} var reqList = []uint64{GetBlockHeadersMsg, GetBlockBodiesMsg, GetCodeMsg, GetReceiptsMsg, GetProofsV1Msg, SendTxMsg, SendTxV2Msg, GetTxStatusMsg, GetHeaderProofsMsg, GetProofsV2Msg, GetHelperTrieProofsMsg}
// handleMsg is invoked whenever an inbound message is received from a remote // handleMsg is invoked whenever an inbound message is received from a remote
// peer. The remote connection is torn down upon returning any error. // peer. The remote connection is torn down upon returning any error.
@ -835,7 +835,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
proofs []ChtResp proofs []ChtResp
) )
reqCnt := len(req.Reqs) reqCnt := len(req.Reqs)
if reject(uint64(reqCnt), MaxPPTProofsFetch) { if reject(uint64(reqCnt), MaxHelperTrieProofsFetch) {
return errResp(ErrRequestRejected, "") return errResp(ErrRequestRejected, "")
} }
trieDb := ethdb.NewTable(pm.chainDb, light.ChtTablePrefix) trieDb := ethdb.NewTable(pm.chainDb, light.ChtTablePrefix)
@ -862,12 +862,12 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
return p.SendHeaderProofs(req.ReqID, bv, proofs) return p.SendHeaderProofs(req.ReqID, bv, proofs)
case GetPPTProofsMsg: case GetHelperTrieProofsMsg:
p.Log().Trace("Received PPT proof request") p.Log().Trace("Received helper trie proof request")
// Decode the retrieval message // Decode the retrieval message
var req struct { var req struct {
ReqID uint64 ReqID uint64
Reqs []PPTReq Reqs []HelperTrieReq
} }
if err := msg.Decode(&req); err != nil { if err := msg.Decode(&req); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err) return errResp(ErrDecode, "msg %v: %v", msg, err)
@ -878,13 +878,13 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
auxData [][]byte auxData [][]byte
) )
reqCnt := len(req.Reqs) reqCnt := len(req.Reqs)
if reject(uint64(reqCnt), MaxPPTProofsFetch) { if reject(uint64(reqCnt), MaxHelperTrieProofsFetch) {
return errResp(ErrRequestRejected, "") return errResp(ErrRequestRejected, "")
} }
var ( var (
lastIdx uint64 lastIdx uint64
lastPPTId uint lastType uint
root common.Hash root common.Hash
tr *trie.Trie tr *trie.Trie
) )
@ -895,18 +895,18 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
if nodes.DataSize()+auxBytes >= softResponseLimit { if nodes.DataSize()+auxBytes >= softResponseLimit {
break break
} }
if tr == nil || req.PPTId != lastPPTId || req.TrieIdx != lastIdx { if tr == nil || req.HelperTrieType != lastType || req.TrieIdx != lastIdx {
var prefix string var prefix string
root, prefix = pm.getPPT(req.PPTId, req.TrieIdx) root, prefix = pm.getHelperTrie(req.HelperTrieType, req.TrieIdx)
if root != (common.Hash{}) { if root != (common.Hash{}) {
if t, err := trie.New(root, ethdb.NewTable(pm.chainDb, prefix)); err == nil { if t, err := trie.New(root, ethdb.NewTable(pm.chainDb, prefix)); err == nil {
tr = t tr = t
} }
} }
lastPPTId = req.PPTId lastType = req.HelperTrieType
lastIdx = req.TrieIdx lastIdx = req.TrieIdx
} }
if req.AuxReq == PPTAuxRoot { if req.AuxReq == auxRoot {
var data []byte var data []byte
if root != (common.Hash{}) { if root != (common.Hash{}) {
data = root[:] data = root[:]
@ -918,7 +918,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
tr.Prove(req.Key, req.FromLevel, nodes) tr.Prove(req.Key, req.FromLevel, nodes)
} }
if req.AuxReq != 0 { if req.AuxReq != 0 {
data := pm.getPPTAuxData(req) data := pm.getHelperTrieAuxData(req)
auxData = append(auxData, data) auxData = append(auxData, data)
auxBytes += len(data) auxBytes += len(data)
} }
@ -927,7 +927,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
proofs := nodes.NodeList() proofs := nodes.NodeList()
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost) bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost) pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
return p.SendPPTProofs(req.ReqID, bv, PPTResps{Proofs: proofs, AuxData: auxData}) return p.SendHelperTrieProofs(req.ReqID, bv, HelperTrieResps{Proofs: proofs, AuxData: auxData})
case HeaderProofsMsg: case HeaderProofsMsg:
if pm.odr == nil { if pm.odr == nil {
@ -949,15 +949,15 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
Obj: resp.Data, Obj: resp.Data,
} }
case PPTProofsMsg: case HelperTrieProofsMsg:
if pm.odr == nil { if pm.odr == nil {
return errResp(ErrUnexpectedResponse, "") return errResp(ErrUnexpectedResponse, "")
} }
p.Log().Trace("Received PPT proof response") p.Log().Trace("Received helper trie proof response")
var resp struct { var resp struct {
ReqID, BV uint64 ReqID, BV uint64
Data PPTResps Data HelperTrieResps
} }
if err := msg.Decode(&resp); err != nil { if err := msg.Decode(&resp); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err) return errResp(ErrDecode, "msg %v: %v", msg, err)
@ -965,7 +965,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
p.fcServer.GotReply(resp.ReqID, resp.BV) p.fcServer.GotReply(resp.ReqID, resp.BV)
deliverMsg = &Msg{ deliverMsg = &Msg{
MsgType: MsgPPTProofs, MsgType: MsgHelperTrieProofs,
ReqID: resp.ReqID, ReqID: resp.ReqID,
Obj: resp.Data, Obj: resp.Data,
} }
@ -1077,22 +1077,22 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
return nil return nil
} }
// getPPT returns the post-processed trie root for the given trie ID and section index // getHelperTrie returns the post-processed trie root for the given trie ID and section index
func (pm *ProtocolManager) getPPT(id uint, idx uint64) (common.Hash, string) { func (pm *ProtocolManager) getHelperTrie(id uint, idx uint64) (common.Hash, string) {
switch id { switch id {
case PPTChain: case htCanonical:
sectionHead := core.GetCanonicalHash(pm.chainDb, (idx+1)*light.ChtFrequency-1) sectionHead := core.GetCanonicalHash(pm.chainDb, (idx+1)*light.ChtFrequency-1)
return light.GetChtV2Root(pm.chainDb, idx, sectionHead), light.ChtTablePrefix return light.GetChtV2Root(pm.chainDb, idx, sectionHead), light.ChtTablePrefix
case PPTBloomBits: case htBloomBits:
sectionHead := core.GetCanonicalHash(pm.chainDb, (idx+1)*light.BloomTrieFrequency-1) sectionHead := core.GetCanonicalHash(pm.chainDb, (idx+1)*light.BloomTrieFrequency-1)
return light.GetBloomTrieRoot(pm.chainDb, idx, sectionHead), light.BloomTrieTablePrefix return light.GetBloomTrieRoot(pm.chainDb, idx, sectionHead), light.BloomTrieTablePrefix
} }
return common.Hash{}, "" return common.Hash{}, ""
} }
// getPPTAuxData returns requested auxiliary data for the given PPT request // getHelperTrieAuxData returns requested auxiliary data for the given HelperTrie request
func (pm *ProtocolManager) getPPTAuxData(req PPTReq) []byte { func (pm *ProtocolManager) getHelperTrieAuxData(req HelperTrieReq) []byte {
if req.PPTId == PPTChain && req.AuxReq == PPTChainAuxHeader { if req.HelperTrieType == htCanonical && req.AuxReq == auxHeader {
if len(req.Key) != 8 { if len(req.Key) != 8 {
return nil return nil
} }

View file

@ -28,16 +28,16 @@ import (
// LesOdr implements light.OdrBackend // LesOdr implements light.OdrBackend
type LesOdr struct { type LesOdr struct {
db ethdb.Database db ethdb.Database
chtIndexer, bltIndexer, bloomIndexer *core.ChainIndexer chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer
retriever *retrieveManager retriever *retrieveManager
stop chan struct{} stop chan struct{}
} }
func NewLesOdr(db ethdb.Database, chtIndexer, bltIndexer, bloomIndexer *core.ChainIndexer, retriever *retrieveManager) *LesOdr { func NewLesOdr(db ethdb.Database, chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer, retriever *retrieveManager) *LesOdr {
return &LesOdr{ return &LesOdr{
db: db, db: db,
chtIndexer: chtIndexer, chtIndexer: chtIndexer,
bltIndexer: bltIndexer, bloomTrieIndexer: bloomTrieIndexer,
bloomIndexer: bloomIndexer, bloomIndexer: bloomIndexer,
retriever: retriever, retriever: retriever,
stop: make(chan struct{}), stop: make(chan struct{}),
@ -59,9 +59,9 @@ func (odr *LesOdr) ChtIndexer() *core.ChainIndexer {
return odr.chtIndexer return odr.chtIndexer
} }
// BltIndexer returns the bloom trie chain indexer // BloomTrieIndexer returns the bloom trie chain indexer
func (odr *LesOdr) BltIndexer() *core.ChainIndexer { func (odr *LesOdr) BloomTrieIndexer() *core.ChainIndexer {
return odr.bltIndexer return odr.bloomTrieIndexer
} }
// BloomIndexer returns the bloombits chain indexer // BloomIndexer returns the bloombits chain indexer
@ -76,7 +76,7 @@ const (
MsgProofsV1 MsgProofsV1
MsgProofsV2 MsgProofsV2
MsgHeaderProofs MsgHeaderProofs
MsgPPTProofs MsgHelperTrieProofs
) )
// Msg encodes a LES message that delivers reply data for a request // Msg encodes a LES message that delivers reply data for a request

View file

@ -310,21 +310,24 @@ func (r *CodeRequest) Validate(db ethdb.Database, msg *Msg) error {
} }
const ( const (
PPTChain = iota // helper trie type constants
PPTBloomBits htCanonical = iota // Canonical hash trie
htBloomBits // BloomBits trie
PPTAuxRoot = 1 // applicable for all helper trie requests
PPTChainAuxHeader = 2 auxRoot = 1
// applicable for htCanonical
auxHeader = 2
) )
type PPTReq struct { type HelperTrieReq struct {
PPTId uint HelperTrieType uint
TrieIdx uint64 TrieIdx uint64
Key []byte Key []byte
FromLevel, AuxReq uint FromLevel, AuxReq uint
} }
type PPTResps struct { // describes all responses, not just a single one type HelperTrieResps struct { // describes all responses, not just a single one
Proofs light.NodeList Proofs light.NodeList
AuxData [][]byte AuxData [][]byte
} }
@ -351,7 +354,7 @@ func (r *ChtRequest) GetCost(peer *peer) uint64 {
case lpv1: case lpv1:
return peer.GetRequestCost(GetHeaderProofsMsg, 1) return peer.GetRequestCost(GetHeaderProofsMsg, 1)
case lpv2: case lpv2:
return peer.GetRequestCost(GetPPTProofsMsg, 1) return peer.GetRequestCost(GetHelperTrieProofsMsg, 1)
default: default:
panic(nil) panic(nil)
} }
@ -362,7 +365,7 @@ func (r *ChtRequest) CanSend(peer *peer) bool {
peer.lock.RLock() peer.lock.RLock()
defer peer.lock.RUnlock() defer peer.lock.RUnlock()
return peer.headInfo.Number >= light.PPTConfirmations && r.ChtNum <= (peer.headInfo.Number-light.PPTConfirmations)/light.ChtFrequency return peer.headInfo.Number >= light.HelperTrieConfirmations && r.ChtNum <= (peer.headInfo.Number-light.HelperTrieConfirmations)/light.ChtFrequency
} }
// Request sends an ODR request to the LES network (implementation of LesOdrRequest) // Request sends an ODR request to the LES network (implementation of LesOdrRequest)
@ -370,13 +373,13 @@ func (r *ChtRequest) Request(reqID uint64, peer *peer) error {
peer.Log().Debug("Requesting CHT", "cht", r.ChtNum, "block", r.BlockNum) peer.Log().Debug("Requesting CHT", "cht", r.ChtNum, "block", r.BlockNum)
var encNum [8]byte var encNum [8]byte
binary.BigEndian.PutUint64(encNum[:], r.BlockNum) binary.BigEndian.PutUint64(encNum[:], r.BlockNum)
req := PPTReq{ req := HelperTrieReq{
PPTId: PPTChain, HelperTrieType: htCanonical,
TrieIdx: r.ChtNum, TrieIdx: r.ChtNum,
Key: encNum[:], Key: encNum[:],
AuxReq: PPTChainAuxHeader, AuxReq: auxHeader,
} }
return peer.RequestPPTProofs(reqID, r.GetCost(peer), []PPTReq{req}) return peer.RequestHelperTrieProofs(reqID, r.GetCost(peer), []HelperTrieReq{req})
} }
// Valid processes an ODR request reply message from the LES network // Valid processes an ODR request reply message from the LES network
@ -412,8 +415,8 @@ func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error {
r.Header = proof.Header r.Header = proof.Header
r.Proof = light.NodeList(proof.Proof).NodeSet() r.Proof = light.NodeList(proof.Proof).NodeSet()
r.Td = node.Td r.Td = node.Td
case MsgPPTProofs: case MsgHelperTrieProofs:
resp := msg.Obj.(PPTResps) resp := msg.Obj.(HelperTrieResps)
if len(resp.AuxData) != 1 { if len(resp.AuxData) != 1 {
return errInvalidEntryCount return errInvalidEntryCount
} }
@ -461,7 +464,7 @@ func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error {
} }
type BloomReq struct { type BloomReq struct {
BltNum, BitIdx, SectionIdx, FromLevel uint64 BloomTrieNum, BitIdx, SectionIdx, FromLevel uint64
} }
// ODR request type for requesting headers by Canonical Hash Trie, see LesOdrRequest interface // ODR request type for requesting headers by Canonical Hash Trie, see LesOdrRequest interface
@ -470,7 +473,7 @@ type BloomRequest light.BloomRequest
// GetCost returns the cost of the given ODR request according to the serving // GetCost returns the cost of the given ODR request according to the serving
// peer's cost table (implementation of LesOdrRequest) // peer's cost table (implementation of LesOdrRequest)
func (r *BloomRequest) GetCost(peer *peer) uint64 { func (r *BloomRequest) GetCost(peer *peer) uint64 {
return peer.GetRequestCost(GetPPTProofsMsg, len(r.SectionIdxList)) return peer.GetRequestCost(GetHelperTrieProofsMsg, len(r.SectionIdxList))
} }
// CanSend tells if a certain peer is suitable for serving the given request // CanSend tells if a certain peer is suitable for serving the given request
@ -481,39 +484,39 @@ func (r *BloomRequest) CanSend(peer *peer) bool {
if peer.version < lpv2 { if peer.version < lpv2 {
return false return false
} }
return peer.headInfo.Number >= light.PPTConfirmations && r.BltNum <= (peer.headInfo.Number-light.PPTConfirmations)/light.BloomTrieFrequency return peer.headInfo.Number >= light.HelperTrieConfirmations && r.BloomTrieNum <= (peer.headInfo.Number-light.HelperTrieConfirmations)/light.BloomTrieFrequency
} }
// Request sends an ODR request to the LES network (implementation of LesOdrRequest) // Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func (r *BloomRequest) Request(reqID uint64, peer *peer) error { func (r *BloomRequest) Request(reqID uint64, peer *peer) error {
peer.Log().Debug("Requesting BloomBits", "blt", r.BltNum, "bitIdx", r.BitIdx, "sections", r.SectionIdxList) peer.Log().Debug("Requesting BloomBits", "bloomTrie", r.BloomTrieNum, "bitIdx", r.BitIdx, "sections", r.SectionIdxList)
reqs := make([]PPTReq, len(r.SectionIdxList)) reqs := make([]HelperTrieReq, len(r.SectionIdxList))
var encNumber [10]byte var encNumber [10]byte
binary.BigEndian.PutUint16(encNumber[0:2], uint16(r.BitIdx)) binary.BigEndian.PutUint16(encNumber[0:2], uint16(r.BitIdx))
for i, sectionIdx := range r.SectionIdxList { for i, sectionIdx := range r.SectionIdxList {
binary.BigEndian.PutUint64(encNumber[2:10], sectionIdx) binary.BigEndian.PutUint64(encNumber[2:10], sectionIdx)
reqs[i] = PPTReq{ reqs[i] = HelperTrieReq{
PPTId: PPTBloomBits, HelperTrieType: htBloomBits,
TrieIdx: r.BltNum, TrieIdx: r.BloomTrieNum,
Key: common.CopyBytes(encNumber[:]), Key: common.CopyBytes(encNumber[:]),
} }
} }
return peer.RequestPPTProofs(reqID, r.GetCost(peer), reqs) return peer.RequestHelperTrieProofs(reqID, r.GetCost(peer), reqs)
} }
// Valid processes an ODR request reply message from the LES network // Valid processes an ODR request reply message from the LES network
// returns true and stores results in memory if the message was a valid reply // returns true and stores results in memory if the message was a valid reply
// to the request (implementation of LesOdrRequest) // to the request (implementation of LesOdrRequest)
func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error { func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error {
log.Debug("Validating BloomBits", "blt", r.BltNum, "bitIdx", r.BitIdx, "sections", r.SectionIdxList) log.Debug("Validating BloomBits", "bloomTrie", r.BloomTrieNum, "bitIdx", r.BitIdx, "sections", r.SectionIdxList)
// Ensure we have a correct message with a single proof element // Ensure we have a correct message with a single proof element
if msg.MsgType != MsgPPTProofs { if msg.MsgType != MsgHelperTrieProofs {
return errInvalidMessageType return errInvalidMessageType
} }
resps := msg.Obj.(PPTResps) resps := msg.Obj.(HelperTrieResps)
proofs := resps.Proofs proofs := resps.Proofs
nodeSet := proofs.NodeSet() nodeSet := proofs.NodeSet()
reads := &readTraceDB{db: nodeSet} reads := &readTraceDB{db: nodeSet}
@ -526,7 +529,7 @@ func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error {
for i, idx := range r.SectionIdxList { for i, idx := range r.SectionIdxList {
binary.BigEndian.PutUint64(encNumber[2:10], idx) binary.BigEndian.PutUint64(encNumber[2:10], idx)
value, err, _ := trie.VerifyProof(r.BltRoot, encNumber[:], reads) value, err, _ := trie.VerifyProof(r.BloomTrieRoot, encNumber[:], reads)
if err != nil { if err != nil {
return err return err
} }

View file

@ -227,9 +227,9 @@ func (p *peer) SendHeaderProofs(reqID, bv uint64, proofs []ChtResp) error {
return sendResponse(p.rw, HeaderProofsMsg, reqID, bv, proofs) return sendResponse(p.rw, HeaderProofsMsg, reqID, bv, proofs)
} }
// SendPPTProofs sends a batch of PPT proofs, corresponding to the ones requested. // SendHelperTrieProofs sends a batch of HelperTrie proofs, corresponding to the ones requested.
func (p *peer) SendPPTProofs(reqID, bv uint64, resp PPTResps) error { func (p *peer) SendHelperTrieProofs(reqID, bv uint64, resp HelperTrieResps) error {
return sendResponse(p.rw, PPTProofsMsg, reqID, bv, resp) return sendResponse(p.rw, HelperTrieProofsMsg, reqID, bv, resp)
} }
// SendTxStatus sends a batch of transaction status records, corresponding to the ones requested. // SendTxStatus sends a batch of transaction status records, corresponding to the ones requested.
@ -285,23 +285,23 @@ func (p *peer) RequestProofs(reqID, cost uint64, reqs []ProofReq) error {
} }
// RequestPPTProofs fetches a batch of PPT merkle proofs from a remote node. // RequestHelperTrieProofs fetches a batch of HelperTrie merkle proofs from a remote node.
func (p *peer) RequestPPTProofs(reqID, cost uint64, reqs []PPTReq) error { func (p *peer) RequestHelperTrieProofs(reqID, cost uint64, reqs []HelperTrieReq) error {
p.Log().Debug("Fetching batch of PPT proofs", "count", len(reqs)) p.Log().Debug("Fetching batch of HelperTrie proofs", "count", len(reqs))
switch p.version { switch p.version {
case lpv1: case lpv1:
reqsV1 := make([]ChtReq, len(reqs)) reqsV1 := make([]ChtReq, len(reqs))
for i, req := range reqs { for i, req := range reqs {
if req.PPTId != PPTChain || req.AuxReq != PPTChainAuxHeader || len(req.Key) != 8 { if req.HelperTrieType != htCanonical || req.AuxReq != auxHeader || len(req.Key) != 8 {
return fmt.Errorf("Request invalid in LES/1 mode") return fmt.Errorf("Request invalid in LES/1 mode")
} }
blockNum := binary.BigEndian.Uint64(req.Key) blockNum := binary.BigEndian.Uint64(req.Key)
// convert PPT request to old CHT request // convert HelperTrie request to old CHT request
reqsV1[i] = ChtReq{ChtNum: (req.TrieIdx+1)*(light.ChtFrequency/light.ChtV1Frequency) - 1, BlockNum: blockNum, FromLevel: req.FromLevel} reqsV1[i] = ChtReq{ChtNum: (req.TrieIdx+1)*(light.ChtFrequency/light.ChtV1Frequency) - 1, BlockNum: blockNum, FromLevel: req.FromLevel}
} }
return sendRequest(p.rw, GetHeaderProofsMsg, reqID, cost, reqsV1) return sendRequest(p.rw, GetHeaderProofsMsg, reqID, cost, reqsV1)
case lpv2: case lpv2:
return sendRequest(p.rw, GetPPTProofsMsg, reqID, cost, reqs) return sendRequest(p.rw, GetHelperTrieProofsMsg, reqID, cost, reqs)
default: default:
panic(nil) panic(nil)
} }

View file

@ -73,8 +73,8 @@ const (
// Protocol messages belonging to LPV2 // Protocol messages belonging to LPV2
GetProofsV2Msg = 0x0f GetProofsV2Msg = 0x0f
ProofsV2Msg = 0x10 ProofsV2Msg = 0x10
GetPPTProofsMsg = 0x11 GetHelperTrieProofsMsg = 0x11
PPTProofsMsg = 0x12 HelperTrieProofsMsg = 0x12
SendTxV2Msg = 0x13 SendTxV2Msg = 0x13
GetTxStatusMsg = 0x14 GetTxStatusMsg = 0x14
TxStatusMsg = 0x15 TxStatusMsg = 0x15

View file

@ -46,7 +46,7 @@ type LesServer struct {
privateKey *ecdsa.PrivateKey privateKey *ecdsa.PrivateKey
quitSync chan struct{} quitSync chan struct{}
chtIndexer, bltIndexer *core.ChainIndexer chtIndexer, bloomTrieIndexer *core.ChainIndexer
} }
func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) { func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
@ -66,7 +66,7 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
quitSync: quitSync, quitSync: quitSync,
lesTopics: lesTopics, lesTopics: lesTopics,
chtIndexer: light.NewChtIndexer(eth.ChainDb(), false), chtIndexer: light.NewChtIndexer(eth.ChainDb(), false),
bltIndexer: light.NewBloomTrieIndexer(eth.ChainDb(), false), bloomTrieIndexer: light.NewBloomTrieIndexer(eth.ChainDb(), false),
} }
logger := log.New() logger := log.New()
@ -82,12 +82,12 @@ func NewLesServer(eth *eth.Ethereum, config *eth.Config) (*LesServer, error) {
logger.Info("CHT", "section", chtLastSection, "sectionHead", fmt.Sprintf("%064x", chtSectionHead), "root", fmt.Sprintf("%064x", chtRoot)) logger.Info("CHT", "section", chtLastSection, "sectionHead", fmt.Sprintf("%064x", chtSectionHead), "root", fmt.Sprintf("%064x", chtRoot))
} }
bltSectionCount, _, _ := srv.bltIndexer.Sections() bloomTrieSectionCount, _, _ := srv.bloomTrieIndexer.Sections()
if bltSectionCount != 0 { if bloomTrieSectionCount != 0 {
bltLastSection := bltSectionCount - 1 bloomTrieLastSection := bloomTrieSectionCount - 1
bltSectionHead := srv.bltIndexer.SectionHead(bltLastSection) bloomTrieSectionHead := srv.bloomTrieIndexer.SectionHead(bloomTrieLastSection)
bltRoot := light.GetBloomTrieRoot(pm.chainDb, bltLastSection, bltSectionHead) bloomTrieRoot := light.GetBloomTrieRoot(pm.chainDb, bloomTrieLastSection, bloomTrieSectionHead)
logger.Info("BloomTrie", "section", bltLastSection, "sectionHead", fmt.Sprintf("%064x", bltSectionHead), "root", fmt.Sprintf("%064x", bltRoot)) logger.Info("BloomTrie", "section", bloomTrieLastSection, "sectionHead", fmt.Sprintf("%064x", bloomTrieSectionHead), "root", fmt.Sprintf("%064x", bloomTrieRoot))
} }
srv.chtIndexer.Start(eth.BlockChain()) srv.chtIndexer.Start(eth.BlockChain())
@ -123,8 +123,8 @@ func (s *LesServer) Start(srvr *p2p.Server) {
s.protocolManager.blockLoop() s.protocolManager.blockLoop()
} }
func (s *LesServer) SetBloomBitsIndexer(bbIndexer *core.ChainIndexer) { func (s *LesServer) SetBloomBitsIndexer(bloomIndexer *core.ChainIndexer) {
bbIndexer.AddChildIndexer(s.bltIndexer) bloomIndexer.AddChildIndexer(s.bloomTrieIndexer)
} }
// Stop stops the LES service // Stop stops the LES service

View file

@ -95,8 +95,8 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
if bc.genesisBlock == nil { if bc.genesisBlock == nil {
return nil, core.ErrNoGenesis return nil, core.ErrNoGenesis
} }
if ppt, ok := trustedCheckpoints[bc.genesisBlock.Hash()]; ok { if cp, ok := trustedCheckpoints[bc.genesisBlock.Hash()]; ok {
bc.addTrustedCheckpoint(ppt) bc.addTrustedCheckpoint(cp)
} }
if err := bc.loadLastState(); err != nil { if err := bc.loadLastState(); err != nil {
@ -114,19 +114,19 @@ func NewLightChain(odr OdrBackend, config *params.ChainConfig, engine consensus.
} }
// addTrustedCheckpoint adds a trusted checkpoint to the blockchain // addTrustedCheckpoint adds a trusted checkpoint to the blockchain
func (self *LightChain) addTrustedCheckpoint(ppt trustedCheckpoint) { func (self *LightChain) addTrustedCheckpoint(cp trustedCheckpoint) {
if self.odr.ChtIndexer() != nil { if self.odr.ChtIndexer() != nil {
StoreChtRoot(self.chainDb, ppt.sectionIdx, ppt.sectionHead, ppt.chtRoot) StoreChtRoot(self.chainDb, cp.sectionIdx, cp.sectionHead, cp.chtRoot)
self.odr.ChtIndexer().AddKnownSectionHead(ppt.sectionIdx, ppt.sectionHead) self.odr.ChtIndexer().AddKnownSectionHead(cp.sectionIdx, cp.sectionHead)
} }
if self.odr.BltIndexer() != nil { if self.odr.BloomTrieIndexer() != nil {
StoreBloomTrieRoot(self.chainDb, ppt.sectionIdx, ppt.sectionHead, ppt.bltRoot) StoreBloomTrieRoot(self.chainDb, cp.sectionIdx, cp.sectionHead, cp.bloomTrieRoot)
self.odr.BltIndexer().AddKnownSectionHead(ppt.sectionIdx, ppt.sectionHead) self.odr.BloomTrieIndexer().AddKnownSectionHead(cp.sectionIdx, cp.sectionHead)
} }
if self.odr.BloomIndexer() != nil { if self.odr.BloomIndexer() != nil {
self.odr.BloomIndexer().AddKnownSectionHead(ppt.sectionIdx, ppt.sectionHead) self.odr.BloomIndexer().AddKnownSectionHead(cp.sectionIdx, cp.sectionHead)
} }
log.Info("Added trusted PPT", "chain name", ppt.name) log.Info("Added trusted checkpoint", "chain name", cp.name)
} }
func (self *LightChain) getProcInterrupt() bool { func (self *LightChain) getProcInterrupt() bool {

View file

@ -36,7 +36,7 @@ var NoOdr = context.Background()
type OdrBackend interface { type OdrBackend interface {
Database() ethdb.Database Database() ethdb.Database
ChtIndexer() *core.ChainIndexer ChtIndexer() *core.ChainIndexer
BltIndexer() *core.ChainIndexer BloomTrieIndexer() *core.ChainIndexer
BloomIndexer() *core.ChainIndexer BloomIndexer() *core.ChainIndexer
Retrieve(ctx context.Context, req OdrRequest) error Retrieve(ctx context.Context, req OdrRequest) error
} }
@ -150,10 +150,10 @@ func (req *ChtRequest) StoreResult(db ethdb.Database) {
// BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure // BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure
type BloomRequest struct { type BloomRequest struct {
OdrRequest OdrRequest
BltNum uint64 BloomTrieNum uint64
BitIdx uint BitIdx uint
SectionIdxList []uint64 SectionIdxList []uint64
BltRoot common.Hash BloomTrieRoot common.Hash
BloomBits [][]byte BloomBits [][]byte
Proofs *NodeSet Proofs *NodeSet
} }

View file

@ -150,18 +150,18 @@ func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxLi
) )
var ( var (
bltCount, sectionHeadNum uint64 bloomTrieCount, sectionHeadNum uint64
sectionHead common.Hash sectionHead common.Hash
) )
if odr.BltIndexer() != nil { if odr.BloomTrieIndexer() != nil {
bltCount, sectionHeadNum, sectionHead = odr.BltIndexer().Sections() bloomTrieCount, sectionHeadNum, sectionHead = odr.BloomTrieIndexer().Sections()
canonicalHash := core.GetCanonicalHash(db, sectionHeadNum) canonicalHash := core.GetCanonicalHash(db, sectionHeadNum)
// if the BloomTrie was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too // if the BloomTrie was injected as a trusted checkpoint, we have no canonical hash yet so we accept zero hash too
for bltCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) { for bloomTrieCount > 0 && canonicalHash != sectionHead && canonicalHash != (common.Hash{}) {
bltCount-- bloomTrieCount--
if bltCount > 0 { if bloomTrieCount > 0 {
sectionHeadNum = bltCount*BloomTrieFrequency - 1 sectionHeadNum = bloomTrieCount*BloomTrieFrequency - 1
sectionHead = odr.BltIndexer().SectionHead(bltCount - 1) sectionHead = odr.BloomTrieIndexer().SectionHead(bloomTrieCount - 1)
canonicalHash = core.GetCanonicalHash(db, sectionHeadNum) canonicalHash = core.GetCanonicalHash(db, sectionHeadNum)
} }
} }
@ -176,8 +176,8 @@ func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxLi
if err == nil { if err == nil {
result[i] = bloomBits result[i] = bloomBits
} else { } else {
if sectionIdx >= bltCount { if sectionIdx >= bloomTrieCount {
return nil, ErrNoTrustedBlt return nil, ErrNoTrustedBloomTrie
} }
reqList = append(reqList, sectionIdx) reqList = append(reqList, sectionIdx)
reqIdx = append(reqIdx, i) reqIdx = append(reqIdx, i)
@ -187,7 +187,7 @@ func GetBloomBits(ctx context.Context, odr OdrBackend, bitIdx uint, sectionIdxLi
return result, nil return result, nil
} }
r := &BloomRequest{BltRoot: GetBloomTrieRoot(db, bltCount-1, sectionHead), BltNum: bltCount - 1, BitIdx: bitIdx, SectionIdxList: reqList} r := &BloomRequest{BloomTrieRoot: GetBloomTrieRoot(db, bloomTrieCount-1, sectionHead), BloomTrieNum: bloomTrieCount - 1, BitIdx: bitIdx, SectionIdxList: reqList}
if err := odr.Retrieve(ctx, r); err != nil { if err := odr.Retrieve(ctx, r); err != nil {
return nil, err return nil, err
} else { } else {

View file

@ -37,8 +37,8 @@ import (
const ( const (
ChtFrequency = 32768 ChtFrequency = 32768
ChtV1Frequency = 4096 // as long as we want to retain LES/1 compatibility, servers generate CHTs with the old, higher frequency ChtV1Frequency = 4096 // as long as we want to retain LES/1 compatibility, servers generate CHTs with the old, higher frequency
PPTConfirmations = 2048 // number of confirmations before a server is expected to have the given PPT available HelperTrieConfirmations = 2048 // number of confirmations before a server is expected to have the given HelperTrie available
PPTProcessConfirmations = 256 // number of confirmations before a PPT is generated HelperTrieProcessConfirmations = 256 // number of confirmations before a HelperTrie is generated
) )
// trustedCheckpoint represents a set of post-processed trie roots (CHT and BloomTrie) associated with // trustedCheckpoint represents a set of post-processed trie roots (CHT and BloomTrie) associated with
@ -47,7 +47,7 @@ const (
type trustedCheckpoint struct { type trustedCheckpoint struct {
name string name string
sectionIdx uint64 sectionIdx uint64
sectionHead, chtRoot, bltRoot common.Hash sectionHead, chtRoot, bloomTrieRoot common.Hash
} }
var ( var (
@ -56,7 +56,7 @@ var (
sectionIdx: 129, sectionIdx: 129,
sectionHead: common.HexToHash("64100587c8ec9a76870056d07cb0f58622552d16de6253a59cac4b580c899501"), sectionHead: common.HexToHash("64100587c8ec9a76870056d07cb0f58622552d16de6253a59cac4b580c899501"),
chtRoot: common.HexToHash("bb4fb4076cbe6923c8a8ce8f158452bbe19564959313466989fda095a60884ca"), chtRoot: common.HexToHash("bb4fb4076cbe6923c8a8ce8f158452bbe19564959313466989fda095a60884ca"),
bltRoot: common.HexToHash("0db524b2c4a2a9520a42fd842b02d2e8fb58ff37c75cf57bd0eb82daeace6716"), bloomTrieRoot: common.HexToHash("0db524b2c4a2a9520a42fd842b02d2e8fb58ff37c75cf57bd0eb82daeace6716"),
} }
ropstenCheckpoint = trustedCheckpoint{ ropstenCheckpoint = trustedCheckpoint{
@ -64,7 +64,7 @@ var (
sectionIdx: 50, sectionIdx: 50,
sectionHead: common.HexToHash("00bd65923a1aa67f85e6b4ae67835784dd54be165c37f056691723c55bf016bd"), sectionHead: common.HexToHash("00bd65923a1aa67f85e6b4ae67835784dd54be165c37f056691723c55bf016bd"),
chtRoot: common.HexToHash("6f56dc61936752cc1f8c84b4addabdbe6a1c19693de3f21cb818362df2117f03"), chtRoot: common.HexToHash("6f56dc61936752cc1f8c84b4addabdbe6a1c19693de3f21cb818362df2117f03"),
bltRoot: common.HexToHash("aca7d7c504d22737242effc3fdc604a762a0af9ced898036b5986c3a15220208"), bloomTrieRoot: common.HexToHash("aca7d7c504d22737242effc3fdc604a762a0af9ced898036b5986c3a15220208"),
} }
) )
@ -76,7 +76,7 @@ var trustedCheckpoints = map[common.Hash]trustedCheckpoint{
var ( var (
ErrNoTrustedCht = errors.New("No trusted canonical hash trie") ErrNoTrustedCht = errors.New("No trusted canonical hash trie")
ErrNoTrustedBlt = errors.New("No trusted bloom trie") ErrNoTrustedBloomTrie = errors.New("No trusted bloom trie")
ErrNoHeader = errors.New("Header not found") ErrNoHeader = errors.New("Header not found")
chtPrefix = []byte("chtRoot-") // chtPrefix + chtNum (uint64 big endian) -> trie root hash chtPrefix = []byte("chtRoot-") // chtPrefix + chtNum (uint64 big endian) -> trie root hash
ChtTablePrefix = "cht-" ChtTablePrefix = "cht-"
@ -126,10 +126,10 @@ func NewChtIndexer(db ethdb.Database, clientMode bool) *core.ChainIndexer {
var sectionSize, confirmReq uint64 var sectionSize, confirmReq uint64
if clientMode { if clientMode {
sectionSize = ChtFrequency sectionSize = ChtFrequency
confirmReq = PPTConfirmations confirmReq = HelperTrieConfirmations
} else { } else {
sectionSize = ChtV1Frequency sectionSize = ChtV1Frequency
confirmReq = PPTProcessConfirmations confirmReq = HelperTrieProcessConfirmations
} }
return core.NewChainIndexer(db, idb, &ChtIndexerBackend{db: db, cdb: cdb, sectionSize: sectionSize}, sectionSize, confirmReq, time.Millisecond*100, "cht") return core.NewChainIndexer(db, idb, &ChtIndexerBackend{db: db, cdb: cdb, sectionSize: sectionSize}, sectionSize, confirmReq, time.Millisecond*100, "cht")
} }
@ -219,10 +219,10 @@ func NewBloomTrieIndexer(db ethdb.Database, clientMode bool) *core.ChainIndexer
var confirmReq uint64 var confirmReq uint64
if clientMode { if clientMode {
backend.parentSectionSize = BloomTrieFrequency backend.parentSectionSize = BloomTrieFrequency
confirmReq = PPTConfirmations confirmReq = HelperTrieConfirmations
} else { } else {
backend.parentSectionSize = ethBloomBitsSection backend.parentSectionSize = ethBloomBitsSection
confirmReq = PPTProcessConfirmations confirmReq = HelperTrieProcessConfirmations
} }
backend.bloomTrieRatio = BloomTrieFrequency / backend.parentSectionSize backend.bloomTrieRatio = BloomTrieFrequency / backend.parentSectionSize
backend.sectionHeads = make([]common.Hash, backend.bloomTrieRatio) backend.sectionHeads = make([]common.Hash, backend.bloomTrieRatio)