cmd, eth, les: fix up ultra light config integration
This commit is contained in:
@@ -110,12 +110,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
|
||||
bloomRequests: make(chan chan *bloombits.Retrieval),
|
||||
bloomIndexer: eth.NewBloomIndexer(chainDb, params.BloomBitsBlocksClient, params.HelperTrieConfirmations),
|
||||
}
|
||||
|
||||
var trustedNodes []string
|
||||
if leth.config.ULC != nil {
|
||||
trustedNodes = leth.config.ULC.TrustedServers
|
||||
}
|
||||
leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg, trustedNodes)
|
||||
leth.serverPool = newServerPool(chainDb, quitSync, &leth.wg, leth.config.UltraLightServers)
|
||||
leth.retriever = newRetrieveManager(peers, leth.reqDist, leth.serverPool)
|
||||
leth.relay = newLesTxRelay(peers, leth.retriever)
|
||||
|
||||
@@ -159,11 +154,11 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) {
|
||||
oracle = params.CheckpointOracles[genesisHash]
|
||||
}
|
||||
registrar := newCheckpointOracle(oracle, leth.getLocalCheckpoint)
|
||||
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, checkpoint, light.DefaultClientIndexerConfig, config.ULC, true, config.NetworkId, leth.eventMux, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.serverPool, registrar, quitSync, &leth.wg, nil); err != nil {
|
||||
if leth.protocolManager, err = NewProtocolManager(leth.chainConfig, checkpoint, light.DefaultClientIndexerConfig, config.UltraLightServers, config.UltraLightFraction, true, config.NetworkId, leth.eventMux, leth.peers, leth.blockchain, nil, chainDb, leth.odr, leth.serverPool, registrar, quitSync, &leth.wg, nil); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if leth.protocolManager.isULCEnabled() {
|
||||
log.Warn("Ultra light client is enabled", "trustedNodes", len(leth.protocolManager.ulc.trustedKeys), "minTrustedFraction", leth.protocolManager.ulc.minTrustedFraction)
|
||||
if leth.protocolManager.ulc != nil {
|
||||
log.Warn("Ultra light client is enabled", "servers", len(config.UltraLightServers), "fraction", config.UltraLightFraction)
|
||||
leth.blockchain.DisableCheckFreq()
|
||||
}
|
||||
return leth, nil
|
||||
|
@@ -469,23 +469,18 @@ func (f *lightFetcher) findBestRequest() (bestHash common.Hash, bestAmount uint6
|
||||
|
||||
// isTrustedHash checks if the block can be trusted by the minimum trusted fraction.
|
||||
func (f *lightFetcher) isTrustedHash(hash common.Hash) bool {
|
||||
if !f.pm.isULCEnabled() {
|
||||
// If ultra light cliet mode is disabled, trust all hashes
|
||||
if f.pm.ulc == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
var numAgreed int
|
||||
for p, fp := range f.peers {
|
||||
if !p.isTrusted {
|
||||
continue
|
||||
// Ultra light enabled, only trust after enough confirmations
|
||||
var agreed int
|
||||
for peer, info := range f.peers {
|
||||
if peer.trusted && info.nodeByHash[hash] != nil {
|
||||
agreed++
|
||||
}
|
||||
if _, ok := fp.nodeByHash[hash]; !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
numAgreed++
|
||||
}
|
||||
|
||||
return 100*numAgreed/len(f.pm.ulc.trustedKeys) >= f.pm.ulc.minTrustedFraction
|
||||
return 100*agreed/len(f.pm.ulc.keys) >= f.pm.ulc.fraction
|
||||
}
|
||||
|
||||
func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
|
||||
@@ -498,16 +493,15 @@ func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if p.isOnlyAnnounce {
|
||||
if p.onlyAnnounce {
|
||||
return false
|
||||
}
|
||||
|
||||
fp := f.peers[p]
|
||||
return fp != nil && fp.nodeByHash[bestHash] != nil
|
||||
},
|
||||
request: func(dp distPeer) func() {
|
||||
if f.pm.isULCEnabled() {
|
||||
//keep last trusted header before sync
|
||||
if f.pm.ulc != nil {
|
||||
// Keep last trusted header before sync
|
||||
f.setLastTrustedHeader(f.chain.CurrentHeader())
|
||||
}
|
||||
go func() {
|
||||
@@ -533,10 +527,9 @@ func (f *lightFetcher) newFetcherDistReq(bestHash common.Hash, reqID uint64, bes
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
if p.isOnlyAnnounce {
|
||||
if p.onlyAnnounce {
|
||||
return false
|
||||
}
|
||||
|
||||
fp := f.peers[p]
|
||||
if fp == nil {
|
||||
return false
|
||||
@@ -708,36 +701,30 @@ func (f *lightFetcher) checkSyncedHeaders(p *peer) {
|
||||
p.Log().Debug("Unknown peer to check sync headers")
|
||||
return
|
||||
}
|
||||
|
||||
n := fp.lastAnnounced
|
||||
var td *big.Int
|
||||
|
||||
var h *types.Header
|
||||
if f.pm.isULCEnabled() {
|
||||
var unapprovedHashes []common.Hash
|
||||
// Overwrite last announced for ULC mode
|
||||
h, unapprovedHashes = f.lastTrustedTreeNode(p)
|
||||
//rollback untrusted blocks
|
||||
f.chain.Rollback(unapprovedHashes)
|
||||
//overwrite to last trusted
|
||||
n = fp.nodeByHash[h.Hash()]
|
||||
var (
|
||||
node = fp.lastAnnounced
|
||||
td *big.Int
|
||||
)
|
||||
if f.pm.ulc != nil {
|
||||
// Roll back untrusted blocks
|
||||
h, unapproved := f.lastTrustedTreeNode(p)
|
||||
f.chain.Rollback(unapproved)
|
||||
node = fp.nodeByHash[h.Hash()]
|
||||
}
|
||||
|
||||
//find last valid block
|
||||
for n != nil {
|
||||
if td = f.chain.GetTd(n.hash, n.number); td != nil {
|
||||
// Find last valid block
|
||||
for node != nil {
|
||||
if td = f.chain.GetTd(node.hash, node.number); td != nil {
|
||||
break
|
||||
}
|
||||
n = n.parent
|
||||
node = node.parent
|
||||
}
|
||||
|
||||
// Now n is the latest downloaded/approved header after syncing
|
||||
if n == nil {
|
||||
// Now node is the latest downloaded/approved header after syncing
|
||||
if node == nil {
|
||||
p.Log().Debug("Synchronisation failed")
|
||||
go f.pm.removePeer(p.id)
|
||||
return
|
||||
}
|
||||
header := f.chain.GetHeader(n.hash, n.number)
|
||||
header := f.chain.GetHeader(node.hash, node.number)
|
||||
f.newHeaders([]*types.Header{header}, []*big.Int{td})
|
||||
}
|
||||
|
||||
|
@@ -36,22 +36,22 @@ func TestFetcherULCPeerSelector(t *testing.T) {
|
||||
lf := lightFetcher{
|
||||
pm: &ProtocolManager{
|
||||
ulc: &ulc{
|
||||
trustedKeys: map[string]struct{}{
|
||||
id1.String(): {},
|
||||
id2.String(): {},
|
||||
id3.String(): {},
|
||||
id4.String(): {},
|
||||
keys: map[string]bool{
|
||||
id1.String(): true,
|
||||
id2.String(): true,
|
||||
id3.String(): true,
|
||||
id4.String(): true,
|
||||
},
|
||||
minTrustedFraction: 70,
|
||||
fraction: 70,
|
||||
},
|
||||
},
|
||||
maxConfirmedTd: ftn1.td,
|
||||
|
||||
peers: map[*peer]*fetcherPeerInfo{
|
||||
{
|
||||
id: "peer1",
|
||||
Peer: p2p.NewPeer(id1, "peer1", []p2p.Cap{}),
|
||||
isTrusted: true,
|
||||
id: "peer1",
|
||||
Peer: p2p.NewPeer(id1, "peer1", []p2p.Cap{}),
|
||||
trusted: true,
|
||||
}: {
|
||||
nodeByHash: map[common.Hash]*fetcherTreeNode{
|
||||
ftn1.hash: ftn1,
|
||||
@@ -59,9 +59,9 @@ func TestFetcherULCPeerSelector(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
Peer: p2p.NewPeer(id2, "peer2", []p2p.Cap{}),
|
||||
id: "peer2",
|
||||
isTrusted: true,
|
||||
Peer: p2p.NewPeer(id2, "peer2", []p2p.Cap{}),
|
||||
id: "peer2",
|
||||
trusted: true,
|
||||
}: {
|
||||
nodeByHash: map[common.Hash]*fetcherTreeNode{
|
||||
ftn1.hash: ftn1,
|
||||
@@ -69,9 +69,9 @@ func TestFetcherULCPeerSelector(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "peer3",
|
||||
Peer: p2p.NewPeer(id3, "peer3", []p2p.Cap{}),
|
||||
isTrusted: true,
|
||||
id: "peer3",
|
||||
Peer: p2p.NewPeer(id3, "peer3", []p2p.Cap{}),
|
||||
trusted: true,
|
||||
}: {
|
||||
nodeByHash: map[common.Hash]*fetcherTreeNode{
|
||||
ftn1.hash: ftn1,
|
||||
@@ -80,9 +80,9 @@ func TestFetcherULCPeerSelector(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "peer4",
|
||||
Peer: p2p.NewPeer(id4, "peer4", []p2p.Cap{}),
|
||||
isTrusted: true,
|
||||
id: "peer4",
|
||||
Peer: p2p.NewPeer(id4, "peer4", []p2p.Cap{}),
|
||||
trusted: true,
|
||||
}: {
|
||||
nodeByHash: map[common.Hash]*fetcherTreeNode{
|
||||
ftn1.hash: ftn1,
|
||||
|
@@ -31,7 +31,6 @@ import (
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/core/state"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/eth/downloader"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
@@ -130,7 +129,7 @@ type ProtocolManager struct {
|
||||
|
||||
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
|
||||
// with the ethereum network.
|
||||
func NewProtocolManager(chainConfig *params.ChainConfig, checkpoint *params.TrustedCheckpoint, indexerConfig *light.IndexerConfig, ulcConfig *eth.ULCConfig, client bool, networkId uint64, mux *event.TypeMux, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, serverPool *serverPool, registrar *checkpointOracle, quitSync chan struct{}, wg *sync.WaitGroup, synced func() bool) (*ProtocolManager, error) {
|
||||
func NewProtocolManager(chainConfig *params.ChainConfig, checkpoint *params.TrustedCheckpoint, indexerConfig *light.IndexerConfig, ulcServers []string, ulcFraction int, client bool, networkId uint64, mux *event.TypeMux, peers *peerSet, blockchain BlockChain, txpool txPool, chainDb ethdb.Database, odr *LesOdr, serverPool *serverPool, registrar *checkpointOracle, quitSync chan struct{}, wg *sync.WaitGroup, synced func() bool) (*ProtocolManager, error) {
|
||||
// Create the protocol manager with the base fields
|
||||
manager := &ProtocolManager{
|
||||
client: client,
|
||||
@@ -157,10 +156,14 @@ func NewProtocolManager(chainConfig *params.ChainConfig, checkpoint *params.Trus
|
||||
manager.reqDist = odr.retriever.dist
|
||||
}
|
||||
|
||||
if ulcConfig != nil {
|
||||
manager.ulc = newULC(ulcConfig)
|
||||
if ulcServers != nil {
|
||||
ulc, err := newULC(ulcServers, ulcFraction)
|
||||
if err != nil {
|
||||
log.Warn("Failed to initialize ultra light client", "err", err)
|
||||
} else {
|
||||
manager.ulc = ulc
|
||||
}
|
||||
}
|
||||
|
||||
removePeer := manager.removePeer
|
||||
if disableClientRemovePeer {
|
||||
removePeer = func(id string) {}
|
||||
@@ -247,11 +250,11 @@ func (pm *ProtocolManager) runPeer(version uint, p *p2p.Peer, rw p2p.MsgReadWrit
|
||||
}
|
||||
|
||||
func (pm *ProtocolManager) newPeer(pv int, nv uint64, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
|
||||
var isTrusted bool
|
||||
if pm.isULCEnabled() {
|
||||
isTrusted = pm.ulc.isTrusted(p.ID())
|
||||
var trusted bool
|
||||
if pm.ulc != nil {
|
||||
trusted = pm.ulc.trusted(p.ID())
|
||||
}
|
||||
return newPeer(pv, nv, isTrusted, p, newMeteredMsgWriter(rw))
|
||||
return newPeer(pv, nv, trusted, p, newMeteredMsgWriter(rw))
|
||||
}
|
||||
|
||||
// handle is the callback invoked to manage the life cycle of a les peer. When
|
||||
@@ -1197,14 +1200,6 @@ func (pm *ProtocolManager) txStatus(hash common.Hash) light.TxStatus {
|
||||
return stat
|
||||
}
|
||||
|
||||
// isULCEnabled returns true if we can use ULC
|
||||
func (pm *ProtocolManager) isULCEnabled() bool {
|
||||
if pm.ulc == nil || len(pm.ulc.trustedKeys) == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// downloaderPeerNotify implements peerSetNotify
|
||||
type downloaderPeerNotify ProtocolManager
|
||||
|
||||
|
@@ -588,7 +588,7 @@ func TestStopResumeLes3(t *testing.T) {
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
clock := &mclock.Simulated{}
|
||||
testCost := testBufLimit / 10
|
||||
pm, _, err := newTestProtocolManager(false, 0, nil, nil, nil, db, nil, testCost, clock)
|
||||
pm, _, err := newTestProtocolManager(false, 0, nil, nil, nil, db, nil, 0, testCost, clock)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create protocol manager: %v", err)
|
||||
}
|
||||
|
@@ -167,7 +167,7 @@ func testIndexers(db ethdb.Database, odr light.OdrBackend, config *light.Indexer
|
||||
// newTestProtocolManager creates a new protocol manager for testing purposes,
|
||||
// with the given number of blocks already known, potential notification
|
||||
// channels for different events and relative chain indexers array.
|
||||
func newTestProtocolManager(lightSync bool, blocks int, odr *LesOdr, indexers []*core.ChainIndexer, peers *peerSet, db ethdb.Database, ulcConfig *eth.ULCConfig, testCost uint64, clock mclock.Clock) (*ProtocolManager, *backends.SimulatedBackend, error) {
|
||||
func newTestProtocolManager(lightSync bool, blocks int, odr *LesOdr, indexers []*core.ChainIndexer, peers *peerSet, db ethdb.Database, ulcServers []string, ulcFraction int, testCost uint64, clock mclock.Clock) (*ProtocolManager, *backends.SimulatedBackend, error) {
|
||||
var (
|
||||
evmux = new(event.TypeMux)
|
||||
engine = ethash.NewFaker()
|
||||
@@ -219,7 +219,7 @@ func newTestProtocolManager(lightSync bool, blocks int, odr *LesOdr, indexers []
|
||||
}
|
||||
reg = newCheckpointOracle(config, getLocal)
|
||||
}
|
||||
pm, err := NewProtocolManager(gspec.Config, nil, indexConfig, ulcConfig, lightSync, NetworkId, evmux, peers, chain, pool, db, odr, nil, reg, exitCh, new(sync.WaitGroup), func() bool { return true })
|
||||
pm, err := NewProtocolManager(gspec.Config, nil, indexConfig, ulcServers, ulcFraction, lightSync, NetworkId, evmux, peers, chain, pool, db, odr, nil, reg, exitCh, new(sync.WaitGroup), func() bool { return true })
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -249,8 +249,8 @@ func newTestProtocolManager(lightSync bool, blocks int, odr *LesOdr, indexers []
|
||||
// with the given number of blocks already known, potential notification channels
|
||||
// for different events and relative chain indexers array. In case of an error, the
|
||||
// constructor force-fails the test.
|
||||
func newTestProtocolManagerMust(t *testing.T, lightSync bool, blocks int, odr *LesOdr, indexers []*core.ChainIndexer, peers *peerSet, db ethdb.Database, ulcConfig *eth.ULCConfig) (*ProtocolManager, *backends.SimulatedBackend) {
|
||||
pm, backend, err := newTestProtocolManager(lightSync, blocks, odr, indexers, peers, db, ulcConfig, 0, &mclock.System{})
|
||||
func newTestProtocolManagerMust(t *testing.T, lightSync bool, blocks int, odr *LesOdr, indexers []*core.ChainIndexer, peers *peerSet, db ethdb.Database, ulcServers []string, ulcFraction int) (*ProtocolManager, *backends.SimulatedBackend) {
|
||||
pm, backend, err := newTestProtocolManager(lightSync, blocks, odr, indexers, peers, db, ulcServers, ulcFraction, 0, &mclock.System{})
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create protocol manager: %v", err)
|
||||
}
|
||||
@@ -395,7 +395,7 @@ func newServerEnv(t *testing.T, blocks int, protocol int, waitIndexers func(*cor
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
indexers := testIndexers(db, nil, light.TestServerIndexerConfig)
|
||||
|
||||
pm, b := newTestProtocolManagerMust(t, false, blocks, nil, indexers, nil, db, nil)
|
||||
pm, b := newTestProtocolManagerMust(t, false, blocks, nil, indexers, nil, db, nil, 0)
|
||||
peer, _ := newTestPeer(t, "peer", protocol, pm, true, 0)
|
||||
|
||||
cIndexer, bIndexer, btIndexer := indexers[0], indexers[1], indexers[2]
|
||||
@@ -441,8 +441,8 @@ func newClientServerEnv(t *testing.T, blocks int, protocol int, waitIndexers fun
|
||||
|
||||
odr.SetIndexers(lcIndexer, lbtIndexer, lbIndexer)
|
||||
|
||||
pm, b := newTestProtocolManagerMust(t, false, blocks, nil, indexers, peers, db, nil)
|
||||
lpm, lb := newTestProtocolManagerMust(t, true, 0, odr, lIndexers, lPeers, ldb, nil)
|
||||
pm, b := newTestProtocolManagerMust(t, false, blocks, nil, indexers, peers, db, nil, 0)
|
||||
lpm, lb := newTestProtocolManagerMust(t, true, 0, odr, lIndexers, lPeers, ldb, nil, 0)
|
||||
|
||||
startIndexers := func(clientMode bool, pm *ProtocolManager) {
|
||||
if clientMode {
|
||||
|
@@ -108,7 +108,7 @@ func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err erro
|
||||
},
|
||||
canSend: func(dp distPeer) bool {
|
||||
p := dp.(*peer)
|
||||
if !p.isOnlyAnnounce {
|
||||
if !p.onlyAnnounce {
|
||||
return lreq.CanSend(p)
|
||||
}
|
||||
return false
|
||||
|
32
les/peer.go
32
les/peer.go
@@ -110,21 +110,21 @@ type peer struct {
|
||||
fcParams flowcontrol.ServerParams
|
||||
fcCosts requestCostTable
|
||||
|
||||
isTrusted bool
|
||||
isOnlyAnnounce bool
|
||||
trusted bool
|
||||
onlyAnnounce bool
|
||||
chainSince, chainRecent uint64
|
||||
stateSince, stateRecent uint64
|
||||
}
|
||||
|
||||
func newPeer(version int, network uint64, isTrusted bool, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
|
||||
func newPeer(version int, network uint64, trusted bool, p *p2p.Peer, rw p2p.MsgReadWriter) *peer {
|
||||
return &peer{
|
||||
Peer: p,
|
||||
rw: rw,
|
||||
version: version,
|
||||
network: network,
|
||||
id: fmt.Sprintf("%x", p.ID().Bytes()),
|
||||
isTrusted: isTrusted,
|
||||
errCh: make(chan error, 1),
|
||||
Peer: p,
|
||||
rw: rw,
|
||||
version: version,
|
||||
network: network,
|
||||
id: fmt.Sprintf("%x", p.ID().Bytes()),
|
||||
trusted: trusted,
|
||||
errCh: make(chan error, 1),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -591,7 +591,7 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
|
||||
} else {
|
||||
//on client node
|
||||
p.announceType = announceTypeSimple
|
||||
if p.isTrusted {
|
||||
if p.trusted {
|
||||
p.announceType = announceTypeSigned
|
||||
}
|
||||
send = send.add("announceType", p.announceType)
|
||||
@@ -652,22 +652,22 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
|
||||
} else {
|
||||
//mark OnlyAnnounce server if "serveHeaders", "serveChainSince", "serveStateSince" or "txRelay" fields don't exist
|
||||
if recv.get("serveChainSince", &p.chainSince) != nil {
|
||||
p.isOnlyAnnounce = true
|
||||
p.onlyAnnounce = true
|
||||
}
|
||||
if recv.get("serveRecentChain", &p.chainRecent) != nil {
|
||||
p.chainRecent = 0
|
||||
}
|
||||
if recv.get("serveStateSince", &p.stateSince) != nil {
|
||||
p.isOnlyAnnounce = true
|
||||
p.onlyAnnounce = true
|
||||
}
|
||||
if recv.get("serveRecentState", &p.stateRecent) != nil {
|
||||
p.stateRecent = 0
|
||||
}
|
||||
if recv.get("txRelay", nil) != nil {
|
||||
p.isOnlyAnnounce = true
|
||||
p.onlyAnnounce = true
|
||||
}
|
||||
|
||||
if p.isOnlyAnnounce && !p.isTrusted {
|
||||
if p.onlyAnnounce && !p.trusted {
|
||||
return errResp(ErrUselessPeer, "peer cannot serve requests")
|
||||
}
|
||||
|
||||
@@ -689,7 +689,7 @@ func (p *peer) Handshake(td *big.Int, head common.Hash, headNum uint64, genesis
|
||||
recv.get("checkpoint/value", &p.checkpoint)
|
||||
recv.get("checkpoint/registerHeight", &p.checkpointNumber)
|
||||
|
||||
if !p.isOnlyAnnounce {
|
||||
if !p.onlyAnnounce {
|
||||
for msgCode := range reqAvgTimeCost {
|
||||
if p.fcCosts[msgCode] == nil {
|
||||
return errResp(ErrUselessPeer, "peer does not support message %d", msgCode)
|
||||
|
@@ -29,9 +29,9 @@ func TestPeerHandshakeSetAnnounceTypeToAnnounceTypeSignedForTrustedPeer(t *testi
|
||||
|
||||
//peer to connect(on ulc side)
|
||||
p := peer{
|
||||
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
||||
version: protocol_version,
|
||||
isTrusted: true,
|
||||
Peer: p2p.NewPeer(id, "test peer", []p2p.Cap{}),
|
||||
version: protocol_version,
|
||||
trusted: true,
|
||||
rw: &rwStub{
|
||||
WriteHook: func(recvList keyValueList) {
|
||||
//checking that ulc sends to peer allowedRequests=onlyAnnounceRequests and announceType = announceTypeSigned
|
||||
@@ -140,7 +140,7 @@ func TestPeerHandshakeDefaultAllRequests(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if p.isOnlyAnnounce {
|
||||
if p.onlyAnnounce {
|
||||
t.Fatal("Incorrect announceType")
|
||||
}
|
||||
}
|
||||
@@ -196,8 +196,8 @@ func TestPeerHandshakeClientReceiveOnlyAnnounceRequestsHeaders(t *testing.T) {
|
||||
return l
|
||||
},
|
||||
},
|
||||
network: test_networkid,
|
||||
isTrusted: true,
|
||||
network: test_networkid,
|
||||
trusted: true,
|
||||
}
|
||||
|
||||
err := p.Handshake(td, hash, headNum, genesis, nil)
|
||||
@@ -205,8 +205,8 @@ func TestPeerHandshakeClientReceiveOnlyAnnounceRequestsHeaders(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !p.isOnlyAnnounce {
|
||||
t.Fatal("isOnlyAnnounce must be true")
|
||||
if !p.onlyAnnounce {
|
||||
t.Fatal("onlyAnnounce must be true")
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -77,7 +77,7 @@ func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) {
|
||||
archiveMode: e.ArchiveMode(),
|
||||
quitSync: quitSync,
|
||||
lesTopics: lesTopics,
|
||||
onlyAnnounce: config.OnlyAnnounce,
|
||||
onlyAnnounce: config.UltraLightOnlyAnnounce,
|
||||
}
|
||||
srv.costTracker, srv.minCapacity = newCostTracker(e.ChainDb(), config)
|
||||
|
||||
@@ -103,7 +103,7 @@ func NewLesServer(e *eth.Ethereum, config *eth.Config) (*LesServer, error) {
|
||||
}
|
||||
registrar := newCheckpointOracle(oracle, srv.getLocalCheckpoint)
|
||||
// TODO(rjl493456442) Checkpoint is useless for les server, separate handler for client and server.
|
||||
pm, err := NewProtocolManager(e.BlockChain().Config(), nil, light.DefaultServerIndexerConfig, config.ULC, false, config.NetworkId, e.EventMux(), newPeerSet(), e.BlockChain(), e.TxPool(), e.ChainDb(), nil, nil, registrar, quitSync, new(sync.WaitGroup), e.Synced)
|
||||
pm, err := NewProtocolManager(e.BlockChain().Config(), nil, light.DefaultServerIndexerConfig, config.UltraLightServers, config.UltraLightFraction, false, config.NetworkId, e.EventMux(), newPeerSet(), e.BlockChain(), e.TxPool(), e.ChainDb(), nil, nil, registrar, quitSync, new(sync.WaitGroup), e.Synced)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@@ -130,7 +130,7 @@ func (self *lesTxRelay) send(txs types.Transactions, count int) {
|
||||
return peer.GetTxRelayCost(len(ll), len(enc))
|
||||
},
|
||||
canSend: func(dp distPeer) bool {
|
||||
return !dp.(*peer).isOnlyAnnounce && dp.(*peer) == pp
|
||||
return !dp.(*peer).onlyAnnounce && dp.(*peer) == pp
|
||||
},
|
||||
request: func(dp distPeer) func() {
|
||||
peer := dp.(*peer)
|
||||
|
40
les/ulc.go
40
les/ulc.go
@@ -17,38 +17,38 @@
|
||||
package les
|
||||
|
||||
import (
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"errors"
|
||||
|
||||
"github.com/ethereum/go-ethereum/log"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
)
|
||||
|
||||
type ulc struct {
|
||||
trustedKeys map[string]struct{}
|
||||
minTrustedFraction int
|
||||
keys map[string]bool
|
||||
fraction int
|
||||
}
|
||||
|
||||
// newULC creates and returns a ultra light client instance.
|
||||
func newULC(ulcConfig *eth.ULCConfig) *ulc {
|
||||
if ulcConfig == nil {
|
||||
return nil
|
||||
}
|
||||
m := make(map[string]struct{}, len(ulcConfig.TrustedServers))
|
||||
for _, id := range ulcConfig.TrustedServers {
|
||||
// newULC creates and returns an ultra light client instance.
|
||||
func newULC(servers []string, fraction int) (*ulc, error) {
|
||||
keys := make(map[string]bool)
|
||||
for _, id := range servers {
|
||||
node, err := enode.Parse(enode.ValidSchemes, id)
|
||||
if err != nil {
|
||||
log.Debug("Failed to parse trusted server", "id", id, "err", err)
|
||||
log.Warn("Failed to parse trusted server", "id", id, "err", err)
|
||||
continue
|
||||
}
|
||||
m[node.ID().String()] = struct{}{}
|
||||
keys[node.ID().String()] = true
|
||||
}
|
||||
return &ulc{m, ulcConfig.MinTrustedFraction}
|
||||
if len(keys) == 0 {
|
||||
return nil, errors.New("no trusted servers")
|
||||
}
|
||||
return &ulc{
|
||||
keys: keys,
|
||||
fraction: fraction,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// isTrusted return an indicator that whether the specified peer is trusted.
|
||||
func (u *ulc) isTrusted(p enode.ID) bool {
|
||||
if u.trustedKeys == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := u.trustedKeys[p.String()]
|
||||
return ok
|
||||
// trusted return an indicator that whether the specified peer is trusted.
|
||||
func (u *ulc) trusted(p enode.ID) bool {
|
||||
return u.keys[p.String()]
|
||||
}
|
||||
|
@@ -28,7 +28,6 @@ import (
|
||||
"github.com/ethereum/go-ethereum/common/mclock"
|
||||
"github.com/ethereum/go-ethereum/core/rawdb"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
"github.com/ethereum/go-ethereum/light"
|
||||
"github.com/ethereum/go-ethereum/p2p"
|
||||
"github.com/ethereum/go-ethereum/p2p/enode"
|
||||
@@ -36,22 +35,15 @@ import (
|
||||
|
||||
func TestULCSyncWithOnePeer(t *testing.T) {
|
||||
f := newFullPeerPair(t, 1, 4)
|
||||
ulcConfig := ð.ULCConfig{
|
||||
MinTrustedFraction: 100,
|
||||
TrustedServers: []string{f.Node.String()},
|
||||
}
|
||||
|
||||
l := newLightPeer(t, ulcConfig)
|
||||
l := newLightPeer(t, []string{f.Node.String()}, 100)
|
||||
|
||||
if reflect.DeepEqual(f.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
|
||||
t.Fatal("blocks are equal")
|
||||
}
|
||||
|
||||
_, _, err := connectPeers(f, l, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.PM.fetcher.lock.Lock()
|
||||
l.PM.fetcher.nextRequest()
|
||||
l.PM.fetcher.lock.Unlock()
|
||||
@@ -63,24 +55,17 @@ func TestULCSyncWithOnePeer(t *testing.T) {
|
||||
|
||||
func TestULCReceiveAnnounce(t *testing.T) {
|
||||
f := newFullPeerPair(t, 1, 4)
|
||||
ulcConfig := ð.ULCConfig{
|
||||
MinTrustedFraction: 100,
|
||||
TrustedServers: []string{f.Node.String()},
|
||||
}
|
||||
|
||||
l := newLightPeer(t, ulcConfig)
|
||||
l := newLightPeer(t, []string{f.Node.String()}, 100)
|
||||
fPeer, lPeer, err := connectPeers(f, l, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.PM.synchronise(fPeer)
|
||||
|
||||
//check that the sync is finished correctly
|
||||
if !reflect.DeepEqual(f.PM.blockchain.CurrentHeader().Hash(), l.PM.blockchain.CurrentHeader().Hash()) {
|
||||
t.Fatal("sync doesn't work")
|
||||
}
|
||||
|
||||
l.PM.peers.lock.Lock()
|
||||
if len(l.PM.peers.peers) == 0 {
|
||||
t.Fatal("peer list should not be empty")
|
||||
@@ -101,16 +86,7 @@ func TestULCReceiveAnnounce(t *testing.T) {
|
||||
func TestULCShouldNotSyncWithTwoPeersOneHaveEmptyChain(t *testing.T) {
|
||||
f1 := newFullPeerPair(t, 1, 4)
|
||||
f2 := newFullPeerPair(t, 2, 0)
|
||||
ulcConf := &ulc{minTrustedFraction: 100, trustedKeys: make(map[string]struct{})}
|
||||
ulcConf.trustedKeys[f1.Node.ID().String()] = struct{}{}
|
||||
ulcConf.trustedKeys[f2.Node.ID().String()] = struct{}{}
|
||||
ulcConfig := ð.ULCConfig{
|
||||
MinTrustedFraction: 100,
|
||||
TrustedServers: []string{f1.Node.String(), f2.Node.String()},
|
||||
}
|
||||
l := newLightPeer(t, ulcConfig)
|
||||
l.PM.ulc.minTrustedFraction = 100
|
||||
|
||||
l := newLightPeer(t, []string{f1.Node.String(), f2.Node.String()}, 100)
|
||||
_, _, err := connectPeers(f1, l, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -119,7 +95,6 @@ func TestULCShouldNotSyncWithTwoPeersOneHaveEmptyChain(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.PM.fetcher.lock.Lock()
|
||||
l.PM.fetcher.nextRequest()
|
||||
l.PM.fetcher.lock.Unlock()
|
||||
@@ -134,27 +109,19 @@ func TestULCShouldNotSyncWithThreePeersOneHaveEmptyChain(t *testing.T) {
|
||||
f2 := newFullPeerPair(t, 2, 4)
|
||||
f3 := newFullPeerPair(t, 3, 0)
|
||||
|
||||
ulcConfig := ð.ULCConfig{
|
||||
MinTrustedFraction: 60,
|
||||
TrustedServers: []string{f1.Node.String(), f2.Node.String(), f3.Node.String()},
|
||||
}
|
||||
|
||||
l := newLightPeer(t, ulcConfig)
|
||||
l := newLightPeer(t, []string{f1.Node.String(), f2.Node.String(), f3.Node.String()}, 60)
|
||||
_, _, err := connectPeers(f1, l, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, _, err = connectPeers(f2, l, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, _, err = connectPeers(f3, l, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.PM.fetcher.lock.Lock()
|
||||
l.PM.fetcher.nextRequest()
|
||||
l.PM.fetcher.lock.Unlock()
|
||||
@@ -213,7 +180,7 @@ func connectPeers(full, light pairPeer, version int) (*peer, *peer, error) {
|
||||
func newFullPeerPair(t *testing.T, index int, numberOfblocks int) pairPeer {
|
||||
db := rawdb.NewMemoryDatabase()
|
||||
|
||||
pmFull, _ := newTestProtocolManagerMust(t, false, numberOfblocks, nil, nil, nil, db, nil)
|
||||
pmFull, _ := newTestProtocolManagerMust(t, false, numberOfblocks, nil, nil, nil, db, nil, 0)
|
||||
|
||||
peerPairFull := pairPeer{
|
||||
Name: "full node",
|
||||
@@ -229,7 +196,7 @@ func newFullPeerPair(t *testing.T, index int, numberOfblocks int) pairPeer {
|
||||
}
|
||||
|
||||
// newLightPeer creates node with light sync mode
|
||||
func newLightPeer(t *testing.T, ulcConfig *eth.ULCConfig) pairPeer {
|
||||
func newLightPeer(t *testing.T, ulcServers []string, ulcFraction int) pairPeer {
|
||||
peers := newPeerSet()
|
||||
dist := newRequestDistributor(peers, make(chan struct{}), &mclock.System{})
|
||||
rm := newRetrieveManager(peers, dist, nil)
|
||||
@@ -237,12 +204,11 @@ func newLightPeer(t *testing.T, ulcConfig *eth.ULCConfig) pairPeer {
|
||||
|
||||
odr := NewLesOdr(ldb, light.DefaultClientIndexerConfig, rm)
|
||||
|
||||
pmLight, _ := newTestProtocolManagerMust(t, true, 0, odr, nil, peers, ldb, ulcConfig)
|
||||
pmLight, _ := newTestProtocolManagerMust(t, true, 0, odr, nil, peers, ldb, ulcServers, ulcFraction)
|
||||
peerPairLight := pairPeer{
|
||||
Name: "ulc node",
|
||||
PM: pmLight,
|
||||
}
|
||||
|
||||
key, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
t.Fatal("generate key err:", err)
|
||||
|
Reference in New Issue
Block a user