diff --git a/app/modules/ws2p/lib/WS2PCluster.ts b/app/modules/ws2p/lib/WS2PCluster.ts index 8faf0aed547de23c50d999499aab9fc51774e9a9..b25aaec9f667aa64cef3497a8e7e90654c44a230 100644 --- a/app/modules/ws2p/lib/WS2PCluster.ts +++ b/app/modules/ws2p/lib/WS2PCluster.ts @@ -128,37 +128,25 @@ export class WS2PCluster { const added:WS2PHead[] = [] await Promise.all(heads.map(async (h:WS2PHead) => { try { - const step = h.step - const message = h.message - const sig = h.sig - const messageV2 = h.messageV2 - const sigV2 = h.sigV2 - let sigOK = false - let fullId = '' - let pubkey = '' - let blockstamp = '' - if (messageV2) { - if (!sigV2) { + if (h.messageV2) { + if (!h.sigV2) { throw "HEAD_MESSAGE_WRONGLY_SIGNED" } - const [,,, pub, blockstamp, ws2pId,,,,,]:string[] = messageV2.split(':') - const fullId = [pub, ws2pId].join('-') - this.headReceived(messageV2, sigV2, pub, fullId, blockstamp, step) + const [,,, pub, blockstamp, ws2pId,,,,,]:string[] = h.messageV2.split(':') + this.headReceived(h, pub, [pub, ws2pId].join('-'), blockstamp) } - if (!message) { + if (!h.message) { throw "EMPTY_MESSAGE_FOR_HEAD" } - if (message.match(WS2PConstants.HEAD_V0_REGEXP)) { - const [,, pub, blockstamp]:string[] = message.split(':') + if (h.message.match(WS2PConstants.HEAD_V0_REGEXP)) { + const [,, pub, blockstamp]:string[] = h.message.split(':') const ws2pId = (this.server.conf.ws2p && this.server.conf.ws2p.uuid) || '000000' - const fullId = [pub, ws2pId].join('-') - this.headReceived(message, sig, pub, fullId, blockstamp) + this.headReceived(h, pub, [pub, ws2pId].join('-'), blockstamp) } - else if (message.match(WS2PConstants.HEAD_V1_REGEXP)) { - const [,,, pub, blockstamp, ws2pId, software, softVersion, prefix]:string[] = message.split(':') - const sigOK = verify(message, sig, pub) - const fullId = [pub, ws2pId].join('-') - await this.headReceived(message, sig, pub, fullId, blockstamp) + else if (h.message.match(WS2PConstants.HEAD_V1_REGEXP)) { + const [,,, pub, blockstamp, ws2pId, software, softVersion, prefix]:string[] = h.message.split(':') + const fullId = + await this.headReceived(h, pub, [pub, ws2pId].join('-'), blockstamp) } } catch (e) { this.server.logger.trace(e) @@ -171,10 +159,11 @@ export class WS2PCluster { this.newHeads = [] } - private async headReceived(message:string, sig:string, pub:string, fullId:string, blockstamp:string, step?:number) { + private async headReceived(head:WS2PHead, pub:string, fullId:string, blockstamp:string) { try { - const sigOK = verify(message, sig, pub) - if (sigOK) { + const sigOK = verify(head.message, head.sig, pub) + const sigV2OK = (head.messageV2 !== undefined && head.sigV2 !== undefined) ? verify(head.messageV2, head.sigV2, pub):false + if ((sigV2OK && sigOK) || sigOK) { // Already known? if (!this.headsCache[fullId] || this.headsCache[fullId].blockstamp !== blockstamp) { // More recent? @@ -184,8 +173,8 @@ export class WS2PCluster { if (isAllowed) { const exists = await this.existsBlock(blockstamp) if (exists) { - this.headsCache[fullId] = { blockstamp, message, sig, step } - this.newHeads.push({message, sig, step}) + this.headsCache[fullId] = { blockstamp, message: head.message, sig: head.sig, messageV2: head.messageV2, sigV2: head.sigV2, step: head.step } + this.newHeads.push(head) // Cancel a pending "heads" to be spread if (this.headsTimeout) { clearTimeout(this.headsTimeout) @@ -521,11 +510,7 @@ export class WS2PCluster { const connexions = this.getAllConnections() return Promise.all(connexions.map(async (c) => { try { - if (c.version >= 2) { - await c.pushHeadsV2(heads) - } else { await c.pushHeads(heads) - } } catch (e) { this.server.logger.warn('Could not spread new HEAD info to %s WS2PID %s', c.pubkey, c.uuid) } diff --git a/app/modules/ws2p/lib/WS2PConnection.ts b/app/modules/ws2p/lib/WS2PConnection.ts index 64e85414b833e3dd55cfdd65b2d4b7d0001ccc83..752a3cf408c9a309b9780a78edb46b396fd70d3d 100644 --- a/app/modules/ws2p/lib/WS2PConnection.ts +++ b/app/modules/ws2p/lib/WS2PConnection.ts @@ -615,11 +615,7 @@ export class WS2PConnection { return this.pushData(WS2P_PUSH.PEER, 'peer', peer) } - async pushHeads(heads:{ message:string, sig:string }[]) { - return this.pushData(WS2P_PUSH.HEAD, 'heads', heads) - } - - async pushHeadsV2(heads:{ message:string, sig:string, messageV2?:string, sigV2?:string, step?:number }[]) { + async pushHeads(heads:{ message:string, sig:string, messageV2?:string, sigV2?:string, step?:number }[]) { return this.pushData(WS2P_PUSH.HEAD, 'heads', heads) }