Skip to content
Snippets Groups Projects
Commit 414c5ece authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[fix] #1135 Never allow to loose all the WS2P connections

parent 68087678
Branches
Tags
No related merge requests found
...@@ -100,6 +100,7 @@ export class WS2PCluster { ...@@ -100,6 +100,7 @@ export class WS2PCluster {
async headsReceived(heads:[{ message:string, sig:string }]) { async headsReceived(heads:[{ message:string, sig:string }]) {
const added:{ message:string, sig:string }[] = [] const added:{ message:string, sig:string }[] = []
await Promise.all(heads.map(async (h:{ message:string, sig:string }) => { await Promise.all(heads.map(async (h:{ message:string, sig:string }) => {
try {
const message = h.message const message = h.message
const sig = h.sig const sig = h.sig
if (!message) { if (!message) {
...@@ -178,6 +179,9 @@ export class WS2PCluster { ...@@ -178,6 +179,9 @@ export class WS2PCluster {
throw "HEAD_MESSAGE_WRONGLY_SIGNED" throw "HEAD_MESSAGE_WRONGLY_SIGNED"
} }
} }
} catch (e) {
this.server.logger.trace(e)
}
})) }))
this.server.push({ this.server.push({
ws2p: 'heads', ws2p: 'heads',
...@@ -339,6 +343,10 @@ export class WS2PCluster { ...@@ -339,6 +343,10 @@ export class WS2PCluster {
// Trim the eventual extra connections // Trim the eventual extra connections
setTimeout(() => this.trimClientConnections(), WS2PConstants.CONNEXION_TIMEOUT) setTimeout(() => this.trimClientConnections(), WS2PConstants.CONNEXION_TIMEOUT)
} }
}
listenServerFlow() {
let connectingToNodesByFlow = false
// Also listen for network updates, and connect to new nodes // Also listen for network updates, and connect to new nodes
this.server.pipe(es.mapSync((data:any) => { this.server.pipe(es.mapSync((data:any) => {
...@@ -375,6 +383,21 @@ export class WS2PCluster { ...@@ -375,6 +383,21 @@ export class WS2PCluster {
this.server.logger.warn(e) this.server.logger.warn(e)
} }
} }
// WS2P disconnection
else if (data.ws2p === 'disconnected') {
const nbConnections = this.getAllConnections().length
if (nbConnections < WS2PConstants.CONNECTIONS_LOW_LEVEL && !connectingToNodesByFlow) {
try {
connectingToNodesByFlow = true
await this.connectToWS2Peers()
} catch (e) {
throw e
} finally {
connectingToNodesByFlow = false
}
}
}
})() })()
return data return data
...@@ -594,6 +617,7 @@ export class WS2PCluster { ...@@ -594,6 +617,7 @@ export class WS2PCluster {
// Pull blocks right on start // Pull blocks right on start
const init = async () => { const init = async () => {
try { try {
await this.listenServerFlow()
await this.connectToWS2Peers() await this.connectToWS2Peers()
await this.pullBlocks() await this.pullBlocks()
} catch (e) { } catch (e) {
......
...@@ -15,6 +15,7 @@ export const WS2PConstants = { ...@@ -15,6 +15,7 @@ export const WS2PConstants = {
MAX_LEVEL_1_PEERS: 10, MAX_LEVEL_1_PEERS: 10,
MAX_LEVEL_2_PEERS: 10, MAX_LEVEL_2_PEERS: 10,
CONNECTIONS_LOW_LEVEL: 3,
BAN_DURATION_IN_SECONDS: 120, BAN_DURATION_IN_SECONDS: 120,
ERROR_RECALL_DURATION_IN_SECONDS: 60, ERROR_RECALL_DURATION_IN_SECONDS: 60,
......
...@@ -82,10 +82,12 @@ describe("WS2P heads propagation", function() { ...@@ -82,10 +82,12 @@ describe("WS2P heads propagation", function() {
}) })
}) })
it('should be able to connect to s3 if the we increase size limit', async () => { it('should be able to receive HEADs', async () => {
b3 = s1.commit({ time: now }) b3 = s1.commit({ time: now })
await s2.waitToHaveBlock(3) await Promise.all([
await s2.waitForHeads(1) s2.waitToHaveBlock(3),
s2.waitForHeads(1)
])
await s1.expect('/network/ws2p/info', (res:any) => { await s1.expect('/network/ws2p/info', (res:any) => {
assert.equal(res.peers.level1, 0) assert.equal(res.peers.level1, 0)
assert.equal(res.peers.level2, 1) assert.equal(res.peers.level2, 1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment