Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 34x 34x 34x 34x 34x 34x 34x 507x 507x 2980x 3453x 45x 413x 413x 413x 413x 413x 413x 1894x 1894x 1195x 699x 9331x 4899x 1x 4898x 2922x 1827x 1095x 1094x 1x 6767x 6767x 5942x 1283x 4659x 4658x 4658x 4658x 1x 202x 28x 174x 173x 173x 32x 174x | /* * Copyright © 2019 Lisk Foundation * * See the LICENSE file at the top-level directory of this distribution * for licensing information. * * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, * no part of this software, including this file, may be copied, modified, * propagated, or distributed except according to the terms contained in the * LICENSE file. * * Removal or modification of this copyright notice is prohibited. * */ // tslint:disable-next-line no-require-imports import shuffle = require('lodash.shuffle'); import { DEFAULT_NEW_BUCKET_COUNT, DEFAULT_NEW_BUCKET_SIZE, DEFAULT_TRIED_BUCKET_COUNT, DEFAULT_TRIED_BUCKET_SIZE, } from '../constants'; import { ExistingPeerError } from '../errors'; import { P2PEnhancedPeerInfo, P2PPeerInfo } from '../p2p_types'; import { PEER_TYPE } from '../utils'; import { NewList, NewListConfig } from './new_list'; import { TriedList, TriedListConfig } from './tried_list'; export interface PeerBookConfig { readonly newListConfig?: NewListConfig; readonly triedListConfig?: TriedListConfig; readonly secret: number; } export class PeerBook { private readonly _newPeers: NewList; private readonly _triedPeers: TriedList; public constructor({ newListConfig: newListConfig, triedListConfig: triedListConfig, secret, }: PeerBookConfig) { this._newPeers = new NewList( newListConfig ? newListConfig : { secret, numOfBuckets: DEFAULT_NEW_BUCKET_COUNT, bucketSize: DEFAULT_NEW_BUCKET_SIZE, peerType: PEER_TYPE.NEW_PEER, }, ); this._triedPeers = new TriedList( triedListConfig ? triedListConfig : { secret, numOfBuckets: DEFAULT_TRIED_BUCKET_COUNT, bucketSize: DEFAULT_TRIED_BUCKET_SIZE, peerType: PEER_TYPE.TRIED_PEER, }, ); } public get newPeers(): ReadonlyArray<P2PPeerInfo> { return this._newPeers.peerList; } public get triedPeers(): ReadonlyArray<P2PPeerInfo> { return this._triedPeers.peerList; } public get allPeers(): ReadonlyArray<P2PPeerInfo> { return [...this.newPeers, ...this.triedPeers]; } public getRandomizedPeerList( minimumPeerDiscoveryThreshold: number, maxPeerDiscoveryResponseLength: number, ): ReadonlyArray<P2PPeerInfo> { const allPeers = [...this.newPeers, ...this.triedPeers]; /* tslint:disable no-magic-numbers*/ const min = Math.ceil( Math.min(maxPeerDiscoveryResponseLength, allPeers.length * 0.25), ); const max = Math.floor( Math.min(maxPeerDiscoveryResponseLength, allPeers.length * 0.5), ); const random = Math.floor(Math.random() * (max - min + 1) + min); const randomPeerCount = Math.max( random, Math.min(minimumPeerDiscoveryThreshold, allPeers.length), ); return shuffle(allPeers).slice(0, randomPeerCount); } public getPeer(peerInfo: P2PPeerInfo): P2PPeerInfo | undefined { const triedPeer = this._triedPeers.getPeer(peerInfo.peerId); if (triedPeer) { return triedPeer; } return this._newPeers.getPeer(peerInfo.peerId); } public hasPeer(peerInfo: P2PPeerInfo): boolean { return ( this._triedPeers.hasPeer(peerInfo.peerId) || this._newPeers.hasPeer(peerInfo.peerId) ); } public addPeer(peerInfo: P2PEnhancedPeerInfo): void { if (this._triedPeers.getPeer(peerInfo.peerId)) { throw new ExistingPeerError(peerInfo); } this._newPeers.addPeer(peerInfo); } public updatePeer(peerInfo: P2PPeerInfo): boolean { if (this._triedPeers.getPeer(peerInfo.peerId)) { return this._triedPeers.updatePeer(peerInfo); } if (this._newPeers.getPeer(peerInfo.peerId)) { return this._newPeers.updatePeer(peerInfo); } return false; } public removePeer(peerInfo: P2PPeerInfo): void { this._newPeers.removePeer(peerInfo); this._triedPeers.removePeer(peerInfo); } public upgradePeer(peerInfo: P2PEnhancedPeerInfo): boolean { if (this._triedPeers.hasPeer(peerInfo.peerId)) { return true; } if (this._newPeers.hasPeer(peerInfo.peerId)) { this.removePeer(peerInfo); this._triedPeers.addPeer(peerInfo); return true; } return false; } public downgradePeer(peerInfo: P2PEnhancedPeerInfo): boolean { if (this._newPeers.hasPeer(peerInfo.peerId)) { return this._newPeers.failedConnectionAction(peerInfo); } if (this._triedPeers.hasPeer(peerInfo.peerId)) { const failed = this._triedPeers.failedConnectionAction(peerInfo); if (failed) { this.addPeer(peerInfo); } } return false; } } |