odr.go 2.5 KB
Newer Older
F
Felix Lange 已提交
1
// Copyright 2016 The go-ethereum Authors
2 3 4 5 6 7 8 9 10 11 12 13 14 15
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
F
Felix Lange 已提交
16

17 18 19
package les

import (
20
	"context"
21 22 23

	"github.com/ethereum/go-ethereum/ethdb"
	"github.com/ethereum/go-ethereum/light"
24
	"github.com/ethereum/go-ethereum/log"
25 26
)

F
Felföldi Zsolt 已提交
27
// LesOdr implements light.OdrBackend
28
type LesOdr struct {
F
Felföldi Zsolt 已提交
29 30 31
	db        ethdb.Database
	stop      chan struct{}
	retriever *retrieveManager
32 33
}

F
Felföldi Zsolt 已提交
34
func NewLesOdr(db ethdb.Database, retriever *retrieveManager) *LesOdr {
35
	return &LesOdr{
F
Felföldi Zsolt 已提交
36 37 38
		db:        db,
		retriever: retriever,
		stop:      make(chan struct{}),
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	}
}

func (odr *LesOdr) Stop() {
	close(odr.stop)
}

func (odr *LesOdr) Database() ethdb.Database {
	return odr.db
}

const (
	MsgBlockBodies = iota
	MsgCode
	MsgReceipts
	MsgProofs
	MsgHeaderProofs
)

// Msg encodes a LES message that delivers reply data for a request
type Msg struct {
	MsgType int
	ReqID   uint64
	Obj     interface{}
}

F
Felföldi Zsolt 已提交
65 66 67 68
// Retrieve tries to fetch an object from the LES network.
// If the network retrieval was successful, it stores the object in local db.
func (self *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) {
	lreq := LesRequest(req)
69

F
Felföldi Zsolt 已提交
70
	reqID := genReqID()
71 72 73 74 75 76
	rq := &distReq{
		getCost: func(dp distPeer) uint64 {
			return lreq.GetCost(dp.(*peer))
		},
		canSend: func(dp distPeer) bool {
			p := dp.(*peer)
F
Felföldi Zsolt 已提交
77
			return lreq.CanSend(p)
78 79 80
		},
		request: func(dp distPeer) func() {
			p := dp.(*peer)
81
			cost := lreq.GetCost(p)
82 83 84 85 86
			p.fcServer.QueueRequest(reqID, cost)
			return func() { lreq.Request(reqID, p) }
		},
	}

F
Felföldi Zsolt 已提交
87
	if err = self.retriever.retrieve(ctx, reqID, rq, func(p distPeer, msg *Msg) error { return lreq.Validate(self.db, msg) }); err == nil {
88 89 90
		// retrieved from network, store in db
		req.StoreResult(self.db)
	} else {
91
		log.Debug("Failed to retrieve data from network", "err", err)
92 93 94
	}
	return
}