les: implement PPTProofsMsg

This commit is contained in:
Zsolt Felfoldi 2017-08-13 11:46:22 +02:00
parent c76e52132f
commit 976133a71a
5 changed files with 365 additions and 56 deletions

View file

@ -847,6 +847,73 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
return p.SendHeaderProofs(req.ReqID, bv, proofs)
case GetPPTProofsMsg:
p.Log().Trace("Received PPT proof request")
// Decode the retrieval message
var req struct {
ReqID uint64
Reqs []PPTReq
}
if err := msg.Decode(&req); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err)
}
// Gather state data until the fetch or network limits is reached
var (
auxBytes int
auxData [][]byte
)
reqCnt := len(req.Reqs)
if reject(uint64(reqCnt), MaxPPTProofsFetch) {
return errResp(ErrRequestRejected, "")
}
var (
lastIdx uint64
lastPPTId uint
root common.Hash
tr *trie.Trie
)
nodes := light.NewNodeSet()
for _, req := range req.Reqs {
if nodes.DataSize()+auxBytes >= softResponseLimit {
break
}
if tr == nil || req.PPTId != lastPPTId || req.TrieIdx != lastIdx {
var prefix string
root, prefix = pm.getPPT(req.PPTId, req.TrieIdx)
if root != (common.Hash{}) {
if t, err := trie.New(root, ethdb.NewTable(pm.chainDb, prefix)); err == nil {
tr = t
}
}
lastPPTId = req.PPTId
lastIdx = req.TrieIdx
}
if req.AuxReq == PPTAuxRoot {
var data []byte
if root != (common.Hash{}) {
data = root[:]
}
auxData = append(auxData, data)
auxBytes += len(data)
} else {
if tr != nil {
tr.Prove(req.Key, req.FromLevel, nodes)
}
if req.AuxReq != 0 {
data := pm.getPPTAuxData(req)
auxData = append(auxData, data)
auxBytes += len(data)
}
}
}
proofs := nodes.NodeList()
bv, rcost := p.fcClient.RequestProcessed(costs.baseCost + uint64(reqCnt)*costs.reqCost)
pm.server.fcCostStats.update(msg.Code, uint64(reqCnt), rcost)
return p.SendPPTProofs(req.ReqID, bv, PPTResps{Proofs: proofs, AuxData: auxData})
case HeaderProofsMsg:
if pm.odr == nil {
return errResp(ErrUnexpectedResponse, "")
@ -867,6 +934,27 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
Obj: resp.Data,
}
case PPTProofsMsg:
if pm.odr == nil {
return errResp(ErrUnexpectedResponse, "")
}
p.Log().Trace("Received PPT proof response")
var resp struct {
ReqID, BV uint64
Data PPTResps
}
if err := msg.Decode(&resp); err != nil {
return errResp(ErrDecode, "msg %v: %v", msg, err)
}
p.fcServer.GotReply(resp.ReqID, resp.BV)
deliverMsg = &Msg{
MsgType: MsgPPTProofs,
ReqID: resp.ReqID,
Obj: resp.Data,
}
case SendTxMsg:
if pm.txpool == nil {
return errResp(ErrUnexpectedResponse, "")
@ -905,6 +993,30 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
return nil
}
// getPPT 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) {
switch id {
case PPTChain:
return light.GetChtRoot(pm.chainDb, idx), light.ChtTablePrefix
case PPTBloomBits:
return light.GetBloomTrieRoot(pm.chainDb, idx), light.BloomTrieTablePrefix
}
return common.Hash{}, ""
}
// getPPTAuxData returns requested auxiliary data for the given PPT request
func (pm *ProtocolManager) getPPTAuxData(req PPTReq) []byte {
if req.PPTId == PPTChain && req.AuxReq == PPTChainAuxHeader {
if len(req.Key) != 8 {
return nil
}
blockNum := binary.BigEndian.Uint64(req.Key)
hash := core.GetCanonicalHash(pm.chainDb, blockNum)
return core.GetHeaderRLP(pm.chainDb, hash, blockNum)
}
return nil
}
// NodeInfo retrieves some protocol metadata about the running host node.
func (self *ProtocolManager) NodeInfo() *eth.EthNodeInfo {
return &eth.EthNodeInfo{

View file

@ -54,6 +54,7 @@ const (
MsgProofsV1
MsgProofsV2
MsgHeaderProofs
MsgPPTProofs
)
// Msg encodes a LES message that delivers reply data for a request

View file

@ -36,13 +36,14 @@ import (
var (
errInvalidMessageType = errors.New("invalid message type")
errMultipleEntries = errors.New("multiple response entries")
errInvalidEntryCount = errors.New("invalid number of response entries")
errHeaderUnavailable = errors.New("header unavailable")
errTxHashMismatch = errors.New("transaction hash mismatch")
errUncleHashMismatch = errors.New("uncle hash mismatch")
errReceiptHashMismatch = errors.New("receipt hash mismatch")
errDataHashMismatch = errors.New("data hash mismatch")
errCHTHashMismatch = errors.New("cht hash mismatch")
errCHTNumberMismatch = errors.New("cht number mismatch")
errUselessNodes = errors.New("useless nodes in merkle proof nodeset")
)
@ -65,6 +66,8 @@ func LesRequest(req light.OdrRequest) LesOdrRequest {
return (*CodeRequest)(r)
case *light.ChtRequest:
return (*ChtRequest)(r)
case *light.BloomRequest:
return (*BloomRequest)(r)
default:
return nil
}
@ -102,7 +105,7 @@ func (r *BlockRequest) Validate(db ethdb.Database, msg *Msg) error {
}
bodies := msg.Obj.([]*types.Body)
if len(bodies) != 1 {
return errMultipleEntries
return errInvalidEntryCount
}
body := bodies[0]
@ -158,7 +161,7 @@ func (r *ReceiptsRequest) Validate(db ethdb.Database, msg *Msg) error {
}
receipts := msg.Obj.([]types.Receipts)
if len(receipts) != 1 {
return errMultipleEntries
return errInvalidEntryCount
}
receipt := receipts[0]
@ -205,12 +208,12 @@ func (r *TrieRequest) CanSend(peer *peer) bool {
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func (r *TrieRequest) Request(reqID uint64, peer *peer) error {
peer.Log().Debug("Requesting trie proof", "root", r.Id.Root, "key", r.Key)
req := &ProofReq{
req := ProofReq{
BHash: r.Id.BlockHash,
AccKey: r.Id.AccKey,
Key: r.Key,
}
return peer.RequestProofs(reqID, r.GetCost(peer), []*ProofReq{req})
return peer.RequestProofs(reqID, r.GetCost(peer), []ProofReq{req})
}
// Valid processes an ODR request reply message from the LES network
@ -275,11 +278,11 @@ func (r *CodeRequest) CanSend(peer *peer) bool {
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func (r *CodeRequest) Request(reqID uint64, peer *peer) error {
peer.Log().Debug("Requesting code data", "hash", r.Hash)
req := &CodeReq{
req := CodeReq{
BHash: r.Id.BlockHash,
AccKey: r.Id.AccKey,
}
return peer.RequestCode(reqID, r.GetCost(peer), []*CodeReq{req})
return peer.RequestCode(reqID, r.GetCost(peer), []CodeReq{req})
}
// Valid processes an ODR request reply message from the LES network
@ -294,7 +297,7 @@ func (r *CodeRequest) Validate(db ethdb.Database, msg *Msg) error {
}
reply := msg.Obj.([][]byte)
if len(reply) != 1 {
return errMultipleEntries
return errInvalidEntryCount
}
data := reply[0]
@ -306,10 +309,33 @@ func (r *CodeRequest) Validate(db ethdb.Database, msg *Msg) error {
return nil
}
type ChtReq struct {
ChtNum, BlockNum, FromLevel uint64
const (
PPTChain = iota
PPTBloomBits
PPTAuxRoot = 1
PPTChainAuxHeader = 2
)
type PPTReq struct {
PPTId uint
TrieIdx uint64
Key []byte
FromLevel, AuxReq uint
}
type PPTResps struct { // describes all responses, not just a single one
Proofs light.NodeList
AuxData [][]byte
}
// legacy LES/1
type ChtReq struct {
ChtNum, BlockNum uint64
FromLevel uint
}
// legacy LES/1
type ChtResp struct {
Header *types.Header
Proof []rlp.RawValue
@ -321,7 +347,14 @@ type ChtRequest light.ChtRequest
// GetCost returns the cost of the given ODR request according to the serving
// peer's cost table (implementation of LesOdrRequest)
func (r *ChtRequest) GetCost(peer *peer) uint64 {
return peer.GetRequestCost(GetHeaderProofsMsg, 1)
switch peer.version {
case lpv1:
return peer.GetRequestCost(GetHeaderProofsMsg, 1)
case lpv2:
return peer.GetRequestCost(GetPPTProofsMsg, 1)
default:
panic(nil)
}
}
// CanSend tells if a certain peer is suitable for serving the given request
@ -329,17 +362,21 @@ func (r *ChtRequest) CanSend(peer *peer) bool {
peer.lock.RLock()
defer peer.lock.RUnlock()
return r.ChtNum <= (peer.headInfo.Number-light.ChtConfirmations)/light.ChtFrequency
return peer.headInfo.Number >= light.ChtConfirmations && r.ChtNum <= (peer.headInfo.Number-light.ChtConfirmations)/light.ChtFrequency
}
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func (r *ChtRequest) Request(reqID uint64, peer *peer) error {
peer.Log().Debug("Requesting CHT", "cht", r.ChtNum, "block", r.BlockNum)
req := &ChtReq{
ChtNum: r.ChtNum,
BlockNum: r.BlockNum,
var encNum [8]byte
binary.BigEndian.PutUint64(encNum[:], r.BlockNum)
req := PPTReq{
PPTId: PPTChain,
TrieIdx: r.ChtNum,
Key: encNum[:],
AuxReq: PPTChainAuxHeader,
}
return peer.RequestHeaderProofs(reqID, r.GetCost(peer), []*ChtReq{req})
return peer.RequestPPTProofs(reqID, r.GetCost(peer), []PPTReq{req})
}
// Valid processes an ODR request reply message from the LES network
@ -348,35 +385,157 @@ func (r *ChtRequest) Request(reqID uint64, peer *peer) error {
func (r *ChtRequest) Validate(db ethdb.Database, msg *Msg) error {
log.Debug("Validating CHT", "cht", r.ChtNum, "block", r.BlockNum)
// Ensure we have a correct message with a single proof element
if msg.MsgType != MsgHeaderProofs {
switch msg.MsgType {
case MsgHeaderProofs: // LES/1 backwards compatibility
proofs := msg.Obj.([]ChtResp)
if len(proofs) != 1 {
return errInvalidEntryCount
}
proof := proofs[0]
// Verify the CHT
var encNumber [8]byte
binary.BigEndian.PutUint64(encNumber[:], r.BlockNum)
value, err, _ := trie.VerifyProof(r.ChtRoot, encNumber[:], light.NodeList(proof.Proof).NodeSet())
if err != nil {
return err
}
var node light.ChtNode
if err := rlp.DecodeBytes(value, &node); err != nil {
return err
}
if node.Hash != proof.Header.Hash() {
return errCHTHashMismatch
}
// Verifications passed, store and return
r.Header = proof.Header
r.Proof = light.NodeList(proof.Proof).NodeSet()
r.Td = node.Td
case MsgPPTProofs:
resp := msg.Obj.(PPTResps)
if len(resp.AuxData) != 1 {
return errInvalidEntryCount
}
pdb := resp.Proofs.NodeSet()
headerEnc := resp.AuxData[0]
if len(headerEnc) == 0 {
return errHeaderUnavailable
}
header := new(types.Header)
if err := rlp.DecodeBytes(headerEnc, header); err != nil {
return errHeaderUnavailable
}
// Verify the CHT
var encNumber [8]byte
binary.BigEndian.PutUint64(encNumber[:], r.BlockNum)
cdb := pdb.ReadCache()
value, err, _ := trie.VerifyProof(r.ChtRoot, encNumber[:], cdb)
if err != nil {
return fmt.Errorf("merkle proof verification failed: %v", err)
}
if pdb.KeyCount() != cdb.KeyCount() {
return errUselessNodes
}
var node light.ChtNode
if err := rlp.DecodeBytes(value, &node); err != nil {
return err
}
if node.Hash != header.Hash() {
return errCHTHashMismatch
}
if r.BlockNum != header.Number.Uint64() {
return errCHTNumberMismatch
}
// Verifications passed, store and return
r.Header = header
r.Proof = pdb
r.Td = node.Td
default:
return errInvalidMessageType
}
proofs := msg.Obj.([]ChtResp)
if len(proofs) != 1 {
return errMultipleEntries
}
proof := proofs[0]
// Verify the CHT
var encNumber [8]byte
binary.BigEndian.PutUint64(encNumber[:], r.BlockNum)
value, err, _ := trie.VerifyProof(r.ChtRoot, encNumber[:], light.NodeList(proof.Proof).NodeSet())
if err != nil {
return err
}
var node light.ChtNode
if err := rlp.DecodeBytes(value, &node); err != nil {
return err
}
if node.Hash != proof.Header.Hash() {
return errCHTHashMismatch
}
// Verifications passed, store and return
r.Header = proof.Header
r.Proof = proof.Proof
r.Td = node.Td
return nil
}
type BloomReq struct {
BltNum, BitIdx, SectionIdx, FromLevel uint64
}
// ODR request type for requesting headers by Canonical Hash Trie, see LesOdrRequest interface
type BloomRequest light.BloomRequest
// GetCost returns the cost of the given ODR request according to the serving
// peer's cost table (implementation of LesOdrRequest)
func (r *BloomRequest) GetCost(peer *peer) uint64 {
return peer.GetRequestCost(GetPPTProofsMsg, len(r.SectionIdxList))
}
// CanSend tells if a certain peer is suitable for serving the given request
func (r *BloomRequest) CanSend(peer *peer) bool {
peer.lock.RLock()
defer peer.lock.RUnlock()
if peer.version < lpv2 {
return false
}
return peer.headInfo.Number >= light.BloomTrieConfirmations && r.BltNum <= (peer.headInfo.Number-light.BloomTrieConfirmations)/light.BloomTrieFrequency
}
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
func (r *BloomRequest) Request(reqID uint64, peer *peer) error {
peer.Log().Debug("Requesting BloomBits", "blt", r.BltNum, "bitIdx", r.BitIdx, "sections", r.SectionIdxList)
reqs := make([]PPTReq, len(r.SectionIdxList))
var encNumber [10]byte
binary.BigEndian.PutUint16(encNumber[0:2], uint16(r.BitIdx))
for i, sectionIdx := range r.SectionIdxList {
binary.BigEndian.PutUint64(encNumber[2:10], sectionIdx)
reqs[i] = PPTReq{
PPTId: PPTBloomBits,
TrieIdx: r.BltNum,
Key: common.CopyBytes(encNumber[:]),
}
}
return peer.RequestPPTProofs(reqID, r.GetCost(peer), reqs)
}
// 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
// to the request (implementation of LesOdrRequest)
func (r *BloomRequest) Validate(db ethdb.Database, msg *Msg) error {
log.Debug("Validating BloomBits", "blt", r.BltNum, "bitIdx", r.BitIdx, "sections", r.SectionIdxList)
// Ensure we have a correct message with a single proof element
if msg.MsgType != MsgPPTProofs {
return errInvalidMessageType
}
resps := msg.Obj.(PPTResps)
proofs := resps.Proofs
pdb := proofs.NodeSet()
cdb := pdb.ReadCache()
r.BloomBits = make([][]byte, len(r.SectionIdxList))
// Verify the proofs
var encNumber [10]byte
binary.BigEndian.PutUint16(encNumber[0:2], uint16(r.BitIdx))
for i, idx := range r.SectionIdxList {
binary.BigEndian.PutUint64(encNumber[2:10], idx)
value, err, _ := trie.VerifyProof(r.BltRoot, encNumber[:], cdb)
if err != nil {
return err
}
r.BloomBits[i] = value
}
if pdb.KeyCount() != cdb.KeyCount() {
return errUselessNodes
}
r.Proofs = pdb
return nil
}

View file

@ -18,6 +18,7 @@
package les
import (
"encoding/binary"
"errors"
"fmt"
"math/big"
@ -198,7 +199,7 @@ func (p *peer) SendReceiptsRLP(reqID, bv uint64, receipts []rlp.RawValue) error
return sendResponse(p.rw, ReceiptsMsg, reqID, bv, receipts)
}
// SendProofs sends a batch of merkle proofs, corresponding to the ones requested.
// SendProofs sends a batch of legacy LES/1 merkle proofs, corresponding to the ones requested.
func (p *peer) SendProofs(reqID, bv uint64, proofs proofsData) error {
return sendResponse(p.rw, ProofsV1Msg, reqID, bv, proofs)
}
@ -208,11 +209,16 @@ func (p *peer) SendProofsV2(reqID, bv uint64, proofs light.NodeList) error {
return sendResponse(p.rw, ProofsV2Msg, reqID, bv, proofs)
}
// SendHeaderProofs sends a batch of header proofs, corresponding to the ones requested.
// SendHeaderProofs sends a batch of legacy LES/1 header proofs, corresponding to the ones requested.
func (p *peer) SendHeaderProofs(reqID, bv uint64, proofs []ChtResp) error {
return sendResponse(p.rw, HeaderProofsMsg, reqID, bv, proofs)
}
// SendPPTProofs sends a batch of PPT proofs, corresponding to the ones requested.
func (p *peer) SendPPTProofs(reqID, bv uint64, resp PPTResps) error {
return sendResponse(p.rw, PPTProofsMsg, reqID, bv, resp)
}
// RequestHeadersByHash fetches a batch of blocks' headers corresponding to the
// specified header query, based on the hash of an origin block.
func (p *peer) RequestHeadersByHash(reqID, cost uint64, origin common.Hash, amount int, skip int, reverse bool) error {
@ -236,7 +242,7 @@ func (p *peer) RequestBodies(reqID, cost uint64, hashes []common.Hash) error {
// RequestCode fetches a batch of arbitrary data from a node's known state
// data, corresponding to the specified hashes.
func (p *peer) RequestCode(reqID, cost uint64, reqs []*CodeReq) error {
func (p *peer) RequestCode(reqID, cost uint64, reqs []CodeReq) error {
p.Log().Debug("Fetching batch of codes", "count", len(reqs))
return sendRequest(p.rw, GetCodeMsg, reqID, cost, reqs)
}
@ -248,7 +254,7 @@ func (p *peer) RequestReceipts(reqID, cost uint64, hashes []common.Hash) error {
}
// RequestProofs fetches a batch of merkle proofs from a remote node.
func (p *peer) RequestProofs(reqID, cost uint64, reqs []*ProofReq) error {
func (p *peer) RequestProofs(reqID, cost uint64, reqs []ProofReq) error {
p.Log().Debug("Fetching batch of proofs", "count", len(reqs))
switch p.version {
case lpv1:
@ -261,10 +267,26 @@ func (p *peer) RequestProofs(reqID, cost uint64, reqs []*ProofReq) error {
}
// RequestHeaderProofs fetches a batch of header merkle proofs from a remote node.
func (p *peer) RequestHeaderProofs(reqID, cost uint64, reqs []*ChtReq) error {
p.Log().Debug("Fetching batch of header proofs", "count", len(reqs))
return sendRequest(p.rw, GetHeaderProofsMsg, reqID, cost, reqs)
// RequestPPTProofs fetches a batch of PPT merkle proofs from a remote node.
func (p *peer) RequestPPTProofs(reqID, cost uint64, reqs []PPTReq) error {
p.Log().Debug("Fetching batch of PPT proofs", "count", len(reqs))
switch p.version {
case lpv1:
reqsV1 := make([]ChtReq, len(reqs))
for i, req := range reqs {
if req.PPTId != PPTChain || req.AuxReq != PPTChainAuxHeader || len(req.Key) != 8 {
return fmt.Errorf("Request invalid in LES/1 mode")
}
blockNum := binary.BigEndian.Uint64(req.Key)
// convert PPT request to old CHT request
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)
case lpv2:
return sendRequest(p.rw, GetPPTProofsMsg, reqID, cost, reqs)
default:
panic(nil)
}
}
func (p *peer) SendTxs(reqID, cost uint64, txs types.Transactions) error {

View file

@ -26,7 +26,6 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/rlp"
)
// NoOdr is the default context passed to an ODR capable function when the ODR
@ -126,14 +125,14 @@ func (req *ReceiptsRequest) StoreResult(db ethdb.Database) {
core.WriteBlockReceipts(db, req.Hash, req.Number, req.Receipts)
}
// TrieRequest is the ODR request type for state/storage trie entries
// ChtRequest is the ODR request type for state/storage trie entries
type ChtRequest struct {
OdrRequest
ChtNum, BlockNum uint64
ChtRoot common.Hash
Header *types.Header
Td *big.Int
Proof []rlp.RawValue
Proof *NodeSet
}
// StoreResult stores the retrieved data in local database
@ -143,5 +142,21 @@ func (req *ChtRequest) StoreResult(db ethdb.Database) {
hash, num := req.Header.Hash(), req.Header.Number.Uint64()
core.WriteTd(db, hash, num, req.Td)
core.WriteCanonicalHash(db, hash, num)
//storeProof(db, req.Proof)
}
// BloomRequest is the ODR request type for retrieving bloom filters from a CHT structure
type BloomRequest struct {
OdrRequest
BltNum, BitIdx uint64
SectionIdxList []uint64
BltRoot common.Hash
BloomBits [][]byte
Proofs *NodeSet
}
// StoreResult stores the retrieved data in local database
func (req *BloomRequest) StoreResult(db ethdb.Database) {
for i, sectionIdx := range req.SectionIdxList {
core.StoreBloomBits(db, req.BitIdx, sectionIdx, req.BloomBits[i])
}
}