Skip to content

Commit bf691f9

Browse files
committed
fix ulc sunc
1 parent c1f6710 commit bf691f9

File tree

15 files changed

+292
-140
lines changed

15 files changed

+292
-140
lines changed

les/backend.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,28 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
126126
}
127127

128128
leth.txPool = light.NewTxPool(leth.chainConfig, leth.blockchain, leth.relay)
129-
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, true, ClientProtocolVersions, config.NetworkId, leth.eventMux, leth.engine, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.relay, quitSync, &leth.wg, config.ULC); err != nil {
129+
130+
if leth.protocolManager, err = NewProtocolManager(
131+
leth.chainConfig,
132+
true,
133+
ClientProtocolVersions,
134+
config.NetworkId,
135+
leth.eventMux,
136+
leth.engine,
137+
leth.peers,
138+
leth.blockchain,
139+
nil,
140+
chainDb,
141+
leth.odr,
142+
leth.relay,
143+
quitSync,
144+
&leth.wg,
145+
config.ULC); err != nil {
130146
return nil, err
131147
}
148+
132149
leth.ApiBackend = &LesApiBackend{leth, nil}
150+
133151
gpoParams := config.GPO
134152
if gpoParams.Default == nil {
135153
gpoParams.Default = config.GasPrice

les/fetcher.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sync"
2323
"time"
2424

25+
"fmt"
2526
"github.com/ethereum/go-ethereum/common"
2627
"github.com/ethereum/go-ethereum/common/mclock"
2728
"github.com/ethereum/go-ethereum/consensus"
@@ -58,7 +59,7 @@ type lightFetcher struct {
5859
requestChn chan bool // true if initiated from outside
5960
}
6061

61-
//lightChain
62+
//lightChain - light.LightChain interface
6263
type lightChain interface {
6364
BlockChain
6465
LockChain()
@@ -134,6 +135,7 @@ func newLightFetcher(pm *ProtocolManager) *lightFetcher {
134135

135136
// syncLoop is the main event loop of the light fetcher
136137
func (f *lightFetcher) syncLoop() {
138+
log.Warn("lightFetcher syncLoop started")
137139
requesting := false
138140
defer f.pm.wg.Done()
139141
for {
@@ -150,6 +152,7 @@ func (f *lightFetcher) syncLoop() {
150152
rq *distReq
151153
reqID uint64
152154
)
155+
153156
if !f.syncing && !(newAnnounce && s) {
154157
rq, reqID = f.nextRequest()
155158
}
@@ -405,10 +408,10 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
405408
bestTd *big.Int
406409
bestSyncing bool
407410
)
408-
if f.pm == nil || f.pm.server == nil || f.pm.server.ulc == nil {
409-
bestHash, bestAmount, bestTd, bestSyncing = f.itFindBestValuesForLes()
411+
if f.pm == nil || f.pm.ulc == nil {
412+
bestHash, bestAmount, bestTd, bestSyncing = f.findBestValuesForLes()
410413
} else {
411-
bestHash, bestAmount, bestTd, bestSyncing = f.itFindBestValuesForULC()
414+
bestHash, bestAmount, bestTd, bestSyncing = f.findBestValuesForULC()
412415
}
413416

414417
if bestTd == f.maxConfirmedTd {
@@ -427,7 +430,7 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
427430
return rq, reqID
428431
}
429432

430-
func (f *lightFetcher) itFindBestValuesForLes() (bestHash common.Hash, bestAmount uint64, bestTd *big.Int, bestSyncing bool) {
433+
func (f *lightFetcher) findBestValuesForLes() (bestHash common.Hash, bestAmount uint64, bestTd *big.Int, bestSyncing bool) {
431434
bestTd = f.maxConfirmedTd
432435
bestSyncing = false
433436

@@ -447,13 +450,14 @@ func (f *lightFetcher) itFindBestValuesForLes() (bestHash common.Hash, bestAmoun
447450
return
448451
}
449452

450-
func (f *lightFetcher) itFindBestValuesForULC() (bestHash common.Hash, bestAmount uint64, bestTd *big.Int, bestSyncing bool) {
453+
func (f *lightFetcher) findBestValuesForULC() (bestHash common.Hash, bestAmount uint64, bestTd *big.Int, bestSyncing bool) {
451454
bestTd = f.maxConfirmedTd
452455
bestSyncing = false
453456

454457
for p, fp := range f.peers {
455458
for hash, n := range fp.nodeByHash {
456-
if _, ok := f.pm.server.ulc.trusted[p.id]; !ok {
459+
if f.pm.ulc.isTrusted(p.ID()) == false {
460+
log.Warn(fmt.Sprintf("f.pm.ulc.isTrusted(p.ID()) == false "))
457461
continue
458462
}
459463

@@ -462,7 +466,7 @@ func (f *lightFetcher) itFindBestValuesForULC() (bestHash common.Hash, bestAmoun
462466
}
463467

464468
amount := f.requestAmount(p, n)
465-
if (bestTd == nil || n.td.Cmp(bestTd) > 0) && f.checkTrusted(hash, f.pm.server.ulc.minTrustedFraction) {
469+
if (bestTd == nil || n.td.Cmp(bestTd) > 0) && f.checkTrusted(hash, f.pm.ulc.minTrustedFraction) {
466470
bestHash = hash
467471
bestTd = n.td
468472
bestAmount = amount
@@ -472,6 +476,8 @@ func (f *lightFetcher) itFindBestValuesForULC() (bestHash common.Hash, bestAmoun
472476
}
473477
return
474478
}
479+
480+
// checkTrusted - check num of agreed peer for hash.
475481
func (f *lightFetcher) checkTrusted(hash common.Hash, minTrustedFraction int) bool {
476482
numPeers := len(f.peers)
477483
var numAgreed int
@@ -485,10 +491,13 @@ func (f *lightFetcher) checkTrusted(hash common.Hash, minTrustedFraction int) bo
485491

486492
return checkTrustedFractionBarrier(numAgreed, numPeers, minTrustedFraction)
487493
}
494+
495+
// checkTrustedFractionBarrier - checks is numAgreed/numPeers more minTrustedFraction.
488496
func checkTrustedFractionBarrier(numAgreed, numPeers, minTrustedFraction int) bool {
489497
return 100*numAgreed/numPeers > minTrustedFraction
490498
}
491499

500+
// newFetcherDistReqForSync creates distReq for sync
492501
func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
493502
return &distReq{
494503
getCost: func(dp distPeer) uint64 {
@@ -517,6 +526,7 @@ func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
517526

518527
}
519528

529+
// newFetcherDistReq creates distReq for
520530
func (f *lightFetcher) newFetcherDistReq(bestHash common.Hash, reqID uint64, bestAmount uint64) *distReq {
521531
return &distReq{
522532
getCost: func(dp distPeer) uint64 {
@@ -582,7 +592,7 @@ func (f *lightFetcher) processResponse(req fetchRequest, resp fetchResponse) boo
582592
}
583593

584594
checkFreq := 1
585-
if f.pm.server.ulc != nil && len(f.pm.server.ulc.trusted) > 0 {
595+
if f.pm.ulc != nil && len(f.pm.ulc.trustedKeys) > 0 {
586596
checkFreq = 0
587597
}
588598
if _, err := f.chain.InsertHeaderChain(headers, checkFreq); err != nil {

les/fetcher_test.go

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
package les
22

33
import (
4+
"crypto/rand"
45
"github.com/ethereum/go-ethereum/common"
56
"github.com/ethereum/go-ethereum/core/types"
7+
"github.com/ethereum/go-ethereum/p2p"
8+
"github.com/ethereum/go-ethereum/p2p/discover"
69
"math/big"
710
"testing"
811
)
912

10-
func TestFetcher_ULC_Peer_Selector(t *testing.T) {
13+
func TestFetcherULCPeerSelector(t *testing.T) {
14+
15+
var (
16+
id1 discover.NodeID
17+
id2 discover.NodeID
18+
id3 discover.NodeID
19+
id4 discover.NodeID
20+
)
21+
rand.Read(id1[:])
22+
rand.Read(id2[:])
23+
rand.Read(id3[:])
24+
rand.Read(id4[:])
25+
1126
ftn1 := &fetcherTreeNode{
1227
hash: common.StringToHash("1"),
1328
td: big.NewInt(1),
@@ -24,38 +39,40 @@ func TestFetcher_ULC_Peer_Selector(t *testing.T) {
2439
}
2540
lf := lightFetcher{
2641
pm: &ProtocolManager{
27-
server: &LesServer{
28-
ulc: &ulc{
29-
trusted: map[string]struct{}{
30-
"peer1": {},
31-
"peer2": {},
32-
"peer3": {},
33-
"peer4": {},
34-
},
35-
minTrustedFraction: 70,
42+
ulc: &ulc{
43+
trustedKeys: map[string]struct{}{
44+
id1.String(): {},
45+
id2.String(): {},
46+
id3.String(): {},
47+
id4.String(): {},
3648
},
49+
minTrustedFraction: 70,
3750
},
3851
},
3952
maxConfirmedTd: ftn1.td,
53+
4054
peers: map[*peer]*fetcherPeerInfo{
4155
{
42-
id: "peer1",
56+
id: "peer1",
57+
Peer: p2p.NewPeer(id1, "peer1", []p2p.Cap{}),
4358
}: {
4459
nodeByHash: map[common.Hash]*fetcherTreeNode{
4560
ftn1.hash: ftn1,
4661
ftn2.hash: ftn2,
4762
},
4863
},
4964
{
50-
id: "peer2",
65+
Peer: p2p.NewPeer(id2, "peer2", []p2p.Cap{}),
66+
id: "peer2",
5167
}: {
5268
nodeByHash: map[common.Hash]*fetcherTreeNode{
5369
ftn1.hash: ftn1,
5470
ftn2.hash: ftn2,
5571
},
5672
},
5773
{
58-
id: "peer3",
74+
id: "peer3",
75+
Peer: p2p.NewPeer(id3, "peer3", []p2p.Cap{}),
5976
}: {
6077
nodeByHash: map[common.Hash]*fetcherTreeNode{
6178
ftn1.hash: ftn1,
@@ -64,7 +81,8 @@ func TestFetcher_ULC_Peer_Selector(t *testing.T) {
6481
},
6582
},
6683
{
67-
id: "peer4",
84+
id: "peer4",
85+
Peer: p2p.NewPeer(id4, "peer4", []p2p.Cap{}),
6886
}: {
6987
nodeByHash: map[common.Hash]*fetcherTreeNode{
7088
ftn1.hash: ftn1,
@@ -80,11 +98,12 @@ func TestFetcher_ULC_Peer_Selector(t *testing.T) {
8098
},
8199
},
82100
}
83-
bestHash, bestAmount, bestTD, sync := lf.itFindBestValuesForULC()
101+
bestHash, bestAmount, bestTD, sync := lf.findBestValuesForULC()
84102

85103
if bestTD == nil {
86104
t.Fatal("Empty result")
87105
}
106+
88107
if bestTD.Cmp(ftn2.td) != 0 {
89108
t.Fatal("bad td", bestTD)
90109
}
@@ -95,20 +114,18 @@ func TestFetcher_ULC_Peer_Selector(t *testing.T) {
95114
_, _ = bestAmount, sync
96115
}
97116

98-
func TestFetcher_ProcessResponse_DisablePowValidation_Success(t *testing.T) {
117+
func TestFetcherProcessResponseDisablePowValidation(t *testing.T) {
99118
header := &types.Header{Number: big.NewInt(1)}
100119
lf := lightFetcher{
101120
pm: &ProtocolManager{
102-
server: &LesServer{
103-
ulc: &ulc{
104-
trusted: map[string]struct{}{
105-
"peer1": {},
106-
"peer2": {},
107-
"peer3": {},
108-
"peer4": {},
109-
},
110-
minTrustedFraction: 70,
121+
ulc: &ulc{
122+
trustedKeys: map[string]struct{}{
123+
"peer1": {},
124+
"peer2": {},
125+
"peer3": {},
126+
"peer4": {},
111127
},
128+
minTrustedFraction: 70,
112129
},
113130
},
114131
chain: &lightChainStub{
@@ -129,14 +146,12 @@ func TestFetcher_ProcessResponse_DisablePowValidation_Success(t *testing.T) {
129146
lf.processResponse(fetchRequest{amount: 1, hash: header.Hash()}, fetchResponse{headers: []*types.Header{header}})
130147
}
131148

132-
func TestFetcher_ProcessResponse_DisablePowValidation_Fail(t *testing.T) {
149+
func TestFetcherProcessResponseDisablePowValidationFail(t *testing.T) {
133150
header := &types.Header{Number: big.NewInt(1)}
134151
lf := lightFetcher{
135152
pm: &ProtocolManager{
136-
server: &LesServer{
137-
ulc: &ulc{
138-
trusted: map[string]struct{}{},
139-
},
153+
ulc: &ulc{
154+
trustedKeys: map[string]struct{}{},
140155
},
141156
},
142157
chain: &lightChainStub{

les/handler.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,28 @@ type ProtocolManager struct {
123123

124124
// wait group is used for graceful shutdowns during downloading
125125
// and processing
126-
wg *sync.WaitGroup
126+
wg *sync.WaitGroup
127+
ulc *ulc
127128
}
128129

129130
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
130131
// with the ethereum network.
131-
func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, protocolVersions []uint, networkId uint64, mux *event.TypeMux, engine consensus.Engine, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, txrelay *LesTxRelay, quitSync chan struct{}, wg *sync.WaitGroup, ulcConfig *eth.ULCConfig) (*ProtocolManager, error) {
132+
func NewProtocolManager(
133+
chainConfig *params.ChainConfig,
134+
lightSync bool,
135+
protocolVersions []uint,
136+
networkId uint64,
137+
mux *event.TypeMux,
138+
engine consensus.Engine,
139+
peers *peerSet,
140+
blockchain BlockChain,
141+
txpool txPool,
142+
chainDb ethdb.Database,
143+
odr *LesOdr,
144+
txrelay *LesTxRelay,
145+
quitSync chan struct{},
146+
wg *sync.WaitGroup,
147+
ulcConfig *eth.ULCConfig) (*ProtocolManager, error) {
132148
// Create the protocol manager with the base fields
133149
manager := &ProtocolManager{
134150
lightSync: lightSync,
@@ -152,7 +168,7 @@ func NewProtocolManager(chainConfig *params.ChainConfig, lightSync bool, protoco
152168
}
153169

154170
if ulcConfig != nil {
155-
manager.server = &LesServer{ulc: newULC(ulcConfig)}
171+
manager.ulc = newULC(ulcConfig)
156172
}
157173

158174
// Initiate a sub-protocol for every implemented version we can handle
@@ -259,7 +275,11 @@ func (pm *ProtocolManager) Stop() {
259275
}
260276

261277
func (pm *ProtocolManager) newPeer(pv int, nv uint64, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
262-
return newPeer(pv, nv, p, newMeteredMsgWriter(rw))
278+
var isTrusted bool
279+
if pm.ulc != nil {
280+
isTrusted = pm.ulc.isTrusted(p.ID())
281+
}
282+
return newPeer(pv, nv, isTrusted, p, newMeteredMsgWriter(rw))
263283
}
264284

265285
// handle is the callback invoked to manage the life cycle of a les peer. When
@@ -284,9 +304,11 @@ func (pm *ProtocolManager) handle(p *peer) error {
284304
p.Log().Debug("Light Ethereum handshake failed", "err", err)
285305
return err
286306
}
307+
287308
if rw, ok := p.rw.(*meteredMsgReadWriter); ok {
288309
rw.Init(p.version)
289310
}
311+
290312
// Register the peer locally
291313
if err := pm.peers.Register(p); err != nil {
292314
p.Log().Error("Light Ethereum peer registration failed", "err", err)
@@ -298,6 +320,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
298320
}
299321
pm.removePeer(p.id)
300322
}()
323+
301324
// Register the peer in the downloader. If the downloader considers it banned, we disconnect
302325
if pm.lightSync {
303326
p.lock.Lock()

0 commit comments

Comments
 (0)