diff --git a/app/lib/dal/sqliteDAL/PeerDAL.ts b/app/lib/dal/sqliteDAL/PeerDAL.ts index 299ec9b6ef516f5b728dbf9146ac1bc0d898e3ce..2fef0a5a521685a0c8dc4500a04019fbcb895713 100644 --- a/app/lib/dal/sqliteDAL/PeerDAL.ts +++ b/app/lib/dal/sqliteDAL/PeerDAL.ts @@ -1,7 +1,10 @@ -import {SQLiteDriver} from "../drivers/SQLiteDriver"; -import {AbstractSQLite} from "./AbstractSQLite"; +import {SQLiteDriver} from "../drivers/SQLiteDriver" +import {AbstractSQLite} from "./AbstractSQLite" + +export class DBPeer { + + readonly documentType = "peer" -export interface DBPeer { version: number currency: string status: string @@ -12,8 +15,21 @@ export interface DBPeer { pubkey: string block: string signature: string - endpoints: string + endpoints: string[] raw: string + + json() { + return { + version: this.version, + currency: this.currency, + endpoints: this.endpoints, + status: this.status, + block: this.block, + signature: this.signature, + raw: this.raw, + pubkey: this.pubkey + } + } } export class PeerDAL extends AbstractSQLite<DBPeer> { diff --git a/app/lib/dto/PeerDTO.ts b/app/lib/dto/PeerDTO.ts new file mode 100644 index 0000000000000000000000000000000000000000..afa25ce9d8a87a005974b3001c484dfa5bf7b091 --- /dev/null +++ b/app/lib/dto/PeerDTO.ts @@ -0,0 +1,181 @@ +import {DBPeer} from "../dal/sqliteDAL/PeerDAL" +import {hashf} from "../common" + +const constants = require('../constants') + +export class PeerDTO { + + readonly documentType = "peer" + member = false + + constructor( + public version:number, + public currency:string, + public pubkey:string, + public blockstamp:string, + public endpoints:string[], + public signature:string, + public status:string, + public statusTS:number, + member = false + ) { + this.member = member + } + + get block() { + return this.blockstamp + } + + blockNumber() { + return parseInt(this.blockstamp) + } + + keyID() { + return this.pubkey && this.pubkey.length > 10 ? this.pubkey.substring(0, 10) : "Unknown" + } + + getRaw() { + let raw = "" + raw += "Version: " + this.version + "\n" + raw += "Type: Peer\n" + raw += "Currency: " + this.currency + "\n" + raw += "PublicKey: " + this.pubkey + "\n" + raw += "Block: " + this.blockstamp + "\n" + raw += "Endpoints:" + "\n" + for(const ep of this.endpoints) { + raw += ep + "\n" + } + return raw + } + + getRawSigned() { + return this.getRaw() + this.signature + "\n" + } + + json() { + return { + version: this.version, + currency: this.currency, + endpoints: this.endpoints, + status: this.status, + block: this.block, + signature: this.signature, + raw: this.getRawSigned(), + pubkey: this.pubkey + } + } + + getBMA() { + let bma:any = null; + this.endpoints.forEach((ep) => { + const matches = !bma && ep.match(constants.BMA_REGEXP); + if (matches) { + bma = { + "dns": matches[2] || '', + "ipv4": matches[4] || '', + "ipv6": matches[6] || '', + "port": matches[8] || 9101 + }; + } + }); + return bma || {}; + }; + + getDns() { + const bma = this.getBMA(); + return bma.dns ? bma.dns : null; + } + + getIPv4() { + const bma = this.getBMA(); + return bma.ipv4 ? bma.ipv4 : null; + } + + getIPv6() { + const bma = this.getBMA(); + return bma.ipv6 ? bma.ipv6 : null; + } + + getPort() { + const bma = this.getBMA(); + return bma.port ? bma.port : null; + } + + getHostPreferDNS() { + const bma = this.getBMA(); + return (bma.dns ? bma.dns : + (bma.ipv4 ? bma.ipv4 : + (bma.ipv6 ? bma.ipv6 : ''))) + } + + getURL() { + const bma = this.getBMA(); + let base = this.getHostPreferDNS(); + if(bma.port) + base += ':' + bma.port; + return base; + } + + hasValid4(bma:any) { + return !!(bma.ipv4 && !bma.ipv4.match(/^127.0/) && !bma.ipv4.match(/^192.168/)) + } + + getNamedURL() { + return this.getURL() + } + + isReachable() { + return !!(this.getURL()) + } + + containsEndpoint(ep:string) { + return this.endpoints.reduce((found:boolean, endpoint:string) => found || endpoint == ep, false) + } + + endpointSum() { + return this.endpoints.join('_') + } + + getHash() { + return hashf(this.getRawSigned()) + } + + toDBPeer(): DBPeer { + const p = new DBPeer() + p.version = this.version + p.currency = this.currency + p.status = this.status || "DOWN" + p.statusTS = this.statusTS || 0 + p.hash = this.getHash() + p.first_down = 0 + p.last_try = 0 + p.pubkey = this.pubkey + p.block = this.block + p.signature = this.signature + p.endpoints = this.endpoints + p.raw = this.getRawSigned() + return p + } + + static blockNumber(blockstamp:string) { + return parseInt(blockstamp) + } + + static fromJSONObject(obj:any) { + return new PeerDTO( + obj.version, + obj.currency, + obj.pubkey || obj.issuer, + obj.blockstamp || obj.block, + obj.endpoints, + obj.signature || obj.sig, + obj.status || "DOWN", + obj.statusTS || 0, + obj.member + ) + } + + static endpoint2host(endpoint:string) { + return PeerDTO.fromJSONObject({ endpoints: [endpoint] }).getURL() + } +} \ No newline at end of file diff --git a/app/lib/entity/peer.js b/app/lib/entity/peer.js deleted file mode 100644 index ef6cdc29ebc675d84f9894df4a63b2e4ef560e0c..0000000000000000000000000000000000000000 --- a/app/lib/entity/peer.js +++ /dev/null @@ -1,132 +0,0 @@ -"use strict"; -const _ = require('underscore'); -const rawer = require('duniter-common').rawer; -const constants = require('../constants'); - -module.exports = Peer; - -function Peer(json) { - - this.documentType = 'peer'; - - _(json).keys().forEach((key) => { - this[key] = json[key]; - }); - - // block == blockstamp - this.blockstamp = this.blockstamp || this.block - this.block = this.block || this.blockstamp - - this.endpoints = this.endpoints || []; - this.statusTS = this.statusTS || 0; - - this.keyID = () => this.pubkey && this.pubkey.length > 10 ? this.pubkey.substring(0, 10) : "Unknown"; - - this.copyValues = (to) => { - ["version", "currency", "pub", "endpoints", "hash", "status", "statusTS", "block", "signature"].forEach((key)=> { - to[key] = this[key]; - }); - }; - - this.copyValuesFrom = (from) => { - ["version", "currency", "pub", "endpoints", "block", "signature"].forEach((key) => { - this[key] = from[key]; - }); - }; - - this.json = () => { - const obj = {}; - ["version", "currency", "endpoints", "status", "block", "signature"].forEach((key) => { - obj[key] = this[key]; - }); - obj.raw = this.getRaw(); - obj.pubkey = this.pubkey; - return obj; - }; - - this.getBMA = () => { - let bma = null; - this.endpoints.forEach((ep) => { - const matches = !bma && ep.match(constants.BMA_REGEXP); - if (matches) { - bma = { - "dns": matches[2] || '', - "ipv4": matches[4] || '', - "ipv6": matches[6] || '', - "port": matches[8] || 9101 - }; - } - }); - return bma || {}; - }; - - this.getDns = () => { - const bma = this.getBMA(); - return bma.dns ? bma.dns : null; - }; - - this.getIPv4 = () => { - const bma = this.getBMA(); - return bma.ipv4 ? bma.ipv4 : null; - }; - - this.getIPv6 = () => { - const bma = this.getBMA(); - return bma.ipv6 ? bma.ipv6 : null; - }; - - this.getPort = () => { - const bma = this.getBMA(); - return bma.port ? bma.port : null; - }; - - this.getHostPreferDNS = () => { - const bma = this.getBMA(); - return (bma.dns ? bma.dns : - (bma.ipv4 ? bma.ipv4 : - (bma.ipv6 ? bma.ipv6 : ''))); - }; - - this.getURL = () => { - const bma = this.getBMA(); - let base = this.getHostPreferDNS(); - if(bma.port) - base += ':' + bma.port; - return base; - }; - - this.hasValid4 = (bma) => bma.ipv4 && !bma.ipv4.match(/^127.0/) && !bma.ipv4.match(/^192.168/) ? true : false; - - this.getNamedURL = () => this.getURL(); - - this.getRaw = () => rawer.getPeerWithoutSignature(this); - - this.getRawSigned = () => rawer.getPeer(this); - - this.isReachable = () => { - return this.getURL() ? true : false; - }; - - this.containsEndpoint = (ep) => this.endpoints.reduce((found, endpoint) => found || endpoint == ep, false); - - this.endpointSum = () => this.endpoints.join('_'); - - this.blockNumber = () => this.block.match(/^(\d+)-/)[1]; -} - -Peer.statics = {}; - -Peer.statics.peerize = function(p) { - return p != null ? new Peer(p) : null; -}; - -Peer.statics.fromJSON = Peer.statics.peerize; - -Peer.statics.endpoint2host = (endpoint) => Peer.statics.peerize({ endpoints: [endpoint] }).getURL(); - -Peer.statics.endpointSum = (obj) => Peer.statics.peerize(obj).endpointSum(); - -Peer.statics.blockNumber = (obj) => { - const peer = Peer.statics.peerize(obj); - return peer ? peer.blockNumber() : -1; -}; diff --git a/app/lib/streams/multicaster.ts b/app/lib/streams/multicaster.ts index 9b855017dea5b070690d116fceb12804eae13b74..98b16508768d3e4269d903735e937f0a1a6c5826 100644 --- a/app/lib/streams/multicaster.ts +++ b/app/lib/streams/multicaster.ts @@ -7,10 +7,10 @@ import {IdentityDTO} from "../dto/IdentityDTO" import {CertificationDTO} from "../dto/CertificationDTO" import {MembershipDTO} from "../dto/MembershipDTO" import {TransactionDTO} from "../dto/TransactionDTO" +import {PeerDTO} from "../dto/PeerDTO" const request = require('request'); const constants = require('../../lib/constants'); -const Peer = require('../../lib/entity/peer'); const logger = require('../logger').NewLogger('multicaster'); const WITH_ISOLATION = true; @@ -103,19 +103,19 @@ export class Multicaster extends stream.Transform { return this.forward({ type: 'Peer', uri: '/network/peering/peers', - transform: Peer.statics.peerize, - getObj: (peering:any) => { + transform: (obj:any) => PeerDTO.fromJSONObject(obj), + getObj: (peering:PeerDTO) => { return { peer: peering.getRawSigned() }; }, - getDocID: (doc:any) => doc.keyID() + '#' + doc.block.match(/(\d+)-/)[1], + getDocID: (doc:PeerDTO) => doc.keyID() + '#' + doc.blockNumber(), withIsolation: WITH_ISOLATION, - onError: (resJSON:any, peering:any, to:any) => { - const sentPeer = Peer.statics.peerize(peering); - if (Peer.statics.blockNumber(resJSON.peer) > sentPeer.blockNumber()) { + onError: (resJSON:{ peer:{ block:string, endpoints:string[] }}, peering:any, to:any) => { + const sentPeer = PeerDTO.fromJSONObject(peering) + if (PeerDTO.blockNumber(resJSON.peer.block) > sentPeer.blockNumber()) { this.push({ outdated: true, peer: resJSON.peer }); - logger.warn('Outdated peer document (%s) sent to %s', sentPeer.keyID() + '#' + sentPeer.block.match(/(\d+)-/)[1], to); + logger.warn('Outdated peer document (%s) sent to %s', sentPeer.keyID() + '#' + sentPeer.blockNumber(), to); } return Promise.resolve(); } @@ -162,7 +162,7 @@ export class Multicaster extends stream.Transform { } // Parallel treatment for superfast propagation await Promise.all(peers.map(async (p) => { - let peer = Peer.statics.peerize(p); + let peer = PeerDTO.fromJSONObject(p) const namedURL = peer.getNamedURL(); logger.debug(' `--> to peer %s [%s] (%s)', peer.keyID(), peer.member ? 'member' : '------', namedURL); try { diff --git a/app/lib/streams/router.ts b/app/lib/streams/router.ts index 169f2c3963d1741dacdcf7b55f4f9f99c675fb8c..d38021a37f5f08c9902c213626ad2f6c1c3957bb 100644 --- a/app/lib/streams/router.ts +++ b/app/lib/streams/router.ts @@ -2,8 +2,8 @@ import * as stream from "stream" import {PeeringService} from "../../service/PeeringService" import {FileDAL} from "../dal/fileDAL" import {DBPeer} from "../dal/sqliteDAL/PeerDAL" +import {PeerDTO} from "../dto/PeerDTO" -const Peer = require('../entity/peer'); const constants = require('../constants'); export class RouterStream extends stream.Transform { @@ -75,7 +75,7 @@ export class RouterStream extends stream.Transform { this.push({ 'type': type, 'obj': obj, - 'peers': (peers || []).map(Peer.statics.peerize) + 'peers': (peers || []).map((p:any) => PeerDTO.fromJSONObject(p)) }) } @@ -96,10 +96,11 @@ export class RouterStream extends stream.Transform { nonmembers = RouterStream.chooseXin(nonmembers, isSelfDocument ? constants.NETWORK.MAX_NON_MEMBERS_TO_FORWARD_TO_FOR_SELF_DOCUMENTS : constants.NETWORK.MAX_NON_MEMBERS_TO_FORWARD_TO); let mainRoutes:any = members.map((p:any) => (p.member = true) && p).concat(nonmembers); let mirrors = await this.peeringService.mirrorEndpoints(); - return mainRoutes.concat(mirrors.map((mep, index) => { return { + const peersToRoute:DBPeer[] = mainRoutes.concat(mirrors.map((mep, index) => { return { pubkey: 'M' + index + '_' + this.peeringService.pubkey, endpoints: [mep] }})); + return peersToRoute.map(p => PeerDTO.fromJSONObject(p)) } } diff --git a/app/service/PeeringService.ts b/app/service/PeeringService.ts index 63b9448d13a8658213ddac416342e9b534527fb9..85fd86039f52edfbeaf7a02daf6efb73f9eabca4 100644 --- a/app/service/PeeringService.ts +++ b/app/service/PeeringService.ts @@ -4,6 +4,7 @@ import {FileDAL} from "../lib/dal/fileDAL" import {DBPeer} from "../lib/dal/sqliteDAL/PeerDAL" import {DBBlock} from "../lib/db/DBBlock" import {Multicaster} from "../lib/streams/multicaster" +import {PeerDTO} from "../lib/dto/PeerDTO" const util = require('util'); const _ = require('underscore'); @@ -15,7 +16,6 @@ const dos2unix = require('duniter-common').dos2unix; const hashf = require('duniter-common').hashf; const rawer = require('duniter-common').rawer; const constants = require('../lib/constants'); -const Peer = require('../lib/entity/peer'); export interface Keyring { publicKey:string @@ -52,7 +52,7 @@ export class PeeringService { if (!thePeer) { thePeer = await this.generateSelfPeer(this.conf, 0) } - return Peer.statics.peerize(thePeer); + return PeerDTO.fromJSONObject(thePeer) } async mirrorEndpoints() { @@ -60,7 +60,7 @@ export class PeeringService { return this.getOtherEndpoints(localPeer.endpoints, this.conf); } - checkPeerSignature(p:DBPeer) { + checkPeerSignature(p:PeerDTO) { const raw = rawer.getPeerWithoutSignature(p); const sig = p.signature; const pub = p.pubkey; @@ -72,7 +72,8 @@ export class PeeringService { this.logger.info('⬇ PEER %s', peering.pubkey.substr(0, 8)) // Force usage of local currency name, do not accept other currencies documents peering.currency = this.conf.currency || peering.currency; - let thePeer = new Peer(peering); + let thePeerDTO = PeerDTO.fromJSONObject(peering) + let thePeer = thePeerDTO.toDBPeer() let sp = thePeer.block.split('-'); const blockNumber = parseInt(sp[0]); let blockHash = sp[1]; @@ -82,7 +83,7 @@ export class PeeringService { return GlobalFifoPromise.pushFIFO(async () => { try { if (makeCheckings) { - let goodSignature = this.checkPeerSignature(thePeer); + let goodSignature = this.checkPeerSignature(thePeerDTO) if (!goodSignature) { throw 'Signature from a peer must match'; } @@ -103,12 +104,12 @@ export class PeeringService { sigTime = block ? block.medianTime : 0; thePeer.statusTS = sigTime; let found = await this.dal.getPeerOrNull(thePeer.pubkey); - let peerEntity = Peer.statics.peerize(found || thePeer); + let peerEntityOld = PeerDTO.fromJSONObject(found || thePeer) if(found){ // Already existing peer const sp2 = found.block.split('-'); const previousBlockNumber = parseInt(sp2[0]); - const interfacesChanged = Peer.statics.endpointSum(thePeer) != Peer.statics.endpointSum(peerEntity); + const interfacesChanged = thePeerDTO.endpointSum() != peerEntityOld.endpointSum() const isOutdatedDocument = blockNumber < previousBlockNumber && !eraseIfAlreadyRecorded; const isAlreadyKnown = blockNumber == previousBlockNumber && !eraseIfAlreadyRecorded; if (isOutdatedDocument){ @@ -118,27 +119,34 @@ export class PeeringService { } else if (isAlreadyKnown) { throw constants.ERRORS.PEER_DOCUMENT_ALREADY_KNOWN; } - peerEntity = Peer.statics.peerize(found); + peerEntityOld = PeerDTO.fromJSONObject(found) if (interfacesChanged) { // Warns the old peer of the change const caster = new Multicaster(); - caster.sendPeering(Peer.statics.peerize(peerEntity), Peer.statics.peerize(thePeer)); + caster.sendPeering(PeerDTO.fromJSONObject(peerEntityOld), PeerDTO.fromJSONObject(thePeer)) } - thePeer.copyValues(peerEntity); - peerEntity.sigDate = new Date(sigTime * 1000); + peerEntityOld.version = thePeer.version + peerEntityOld.currency = thePeer.currency + peerEntityOld.pubkey = thePeer.pubkey + peerEntityOld.endpoints = thePeer.endpoints + peerEntityOld.status = thePeer.status + peerEntityOld.signature = thePeer.signature } // Set the peer as UP again + const peerEntity = peerEntityOld.toDBPeer() + peerEntity.statusTS = thePeer.statusTS + peerEntity.block = thePeer.block peerEntity.status = 'UP'; peerEntity.first_down = null; peerEntity.last_try = null; - peerEntity.hash = String(hashf(peerEntity.getRawSigned())).toUpperCase(); - peerEntity.raw = peerEntity.getRaw(); + peerEntity.hash = peerEntityOld.getHash() + peerEntity.raw = peerEntityOld.getRaw(); await this.dal.savePeer(peerEntity); this.logger.info('✔ PEER %s', peering.pubkey.substr(0, 8)) - let savedPeer = Peer.statics.peerize(peerEntity); + let savedPeer = PeerDTO.fromJSONObject(peerEntity).toDBPeer() if (peerEntity.pubkey == this.selfPubkey) { const localEndpoint = await this.server.getMainEndpoint(this.conf); - const localNodeNotListed = !peerEntity.containsEndpoint(localEndpoint); + const localNodeNotListed = !peerEntityOld.containsEndpoint(localEndpoint); const current = localNodeNotListed && (await this.dal.getCurrentBlockOrNull()); if (!localNodeNotListed) { const indexOfThisNode = peerEntity.endpoints.indexOf(localEndpoint); @@ -188,7 +196,7 @@ export class PeeringService { logger.info('Sibling endpoints:', otherPotentialEndpoints); let reals = await otherPotentialEndpoints.map(async (theEndpoint:string) => { let real = true; - let remote = Peer.statics.endpoint2host(theEndpoint); + let remote = PeerDTO.endpoint2host(theEndpoint) try { // We test only BMA APIs, because other may exist and we cannot judge against them yet if (theEndpoint.startsWith('BASIC_MERKLED_API')) { @@ -230,8 +238,8 @@ export class PeeringService { block: targetBlock ? [targetBlock.number, targetBlock.hash].join('-') : constants.PEER.SPECIAL_BLOCK, endpoints: _.uniq([endpoint].concat(toConserve).concat(this.conf.endpoints || [])) }; - const raw2 = dos2unix(new Peer(p2).getRaw()); - logger.info('External access:', new Peer(p2).getURL()); + const raw2 = dos2unix(PeerDTO.fromJSONObject(p2).getRaw()); + logger.info('External access:', PeerDTO.fromJSONObject(p2).getURL()) logger.debug('Generating server\'s peering entry based on block#%s...', p2.block.split('-')[0]); p2.signature = await this.server.sign(raw2); p2.pubkey = this.selfPubkey; diff --git a/test/dal/dal.js b/test/dal/dal.js index 07f85bef3eb4b186a3eb0f4b8e3b0f24df092a1c..0c74f6d19bdd972bdf34e24ed668da56b33ac6c1 100644 --- a/test/dal/dal.js +++ b/test/dal/dal.js @@ -6,7 +6,7 @@ var assert = require('assert'); var dal = require('../../app/lib/dal/fileDAL').FileDAL var dir = require('../../app/lib/system/directory'); var constants = require('../../app/lib/constants'); -var Peer = require('../../app/lib/entity/peer'); +var PeerDTO = require('../../app/lib/dto/PeerDTO').PeerDTO var mocks = { peer1: { @@ -113,7 +113,7 @@ describe("DAL", function(){ }); it('should have 1 peer if 1 is created', function(){ - return fileDAL.savePeer(new Peer(mocks.peer1)) + return fileDAL.savePeer(PeerDTO.fromJSONObject(mocks.peer1)) .then(() => fileDAL.listAllPeers()) .then(function(peers){ peers.should.have.length(1); diff --git a/test/integration/documents-currency.js b/test/integration/documents-currency.js index e23d944a1a7551873c1fbfe903d574d3a91ecdb5..b774680df01953763df4d4e848ebb4b657ec0047 100644 --- a/test/integration/documents-currency.js +++ b/test/integration/documents-currency.js @@ -5,7 +5,6 @@ const co = require('co'); const should = require('should'); const user = require('./tools/user'); const commit = require('./tools/commit'); -const Peer = require('../../app/lib/entity/peer'); const s1 = toolbox_1.NewTestingServer({ currency: 'currency_one', pair: { diff --git a/test/integration/documents-currency.ts b/test/integration/documents-currency.ts index 033f8ffbffe7ded3621bddf556989283e8a09b4d..e76deaa883be224c064513fb8edd0e611483c306 100644 --- a/test/integration/documents-currency.ts +++ b/test/integration/documents-currency.ts @@ -4,7 +4,6 @@ const co = require('co'); const should = require('should'); const user = require('./tools/user'); const commit = require('./tools/commit'); -const Peer = require('../../app/lib/entity/peer'); const s1 = NewTestingServer({ currency: 'currency_one', diff --git a/test/integration/peer-outdated.js b/test/integration/peer-outdated.js index 8226959266994addbcf80d768dfeef7edd85c8f8..fbc91d6e430456efeddd545e05cb42434df19590 100644 --- a/test/integration/peer-outdated.js +++ b/test/integration/peer-outdated.js @@ -10,7 +10,7 @@ const commit = require('./tools/commit'); const until = require('./tools/until'); const toolbox = require('./tools/toolbox'); const Multicaster = require('../../app/lib/streams/multicaster').Multicaster -const Peer = require('../../app/lib/entity/peer'); +const PeerDTO = require('../../app/lib/dto/PeerDTO').PeerDTO const s1 = toolbox.server({ pair: { @@ -62,7 +62,7 @@ describe("Peer document expiry", function() { it('sending back V1 peer document should return the latest known one', () => co(function*() { let res; try { - yield s1.post('/network/peering/peers', { peer: Peer.statics.peerize(peer1V1).getRawSigned() }); + yield s1.post('/network/peering/peers', { peer: PeerDTO.fromJSONObject(peer1V1).getRawSigned() }); } catch (e) { res = e; } @@ -78,7 +78,7 @@ describe("Peer document expiry", function() { obj.should.have.property("outdated").equal(true); resolve(); })); - caster.sendPeering(Peer.statics.peerize(peer1V1), Peer.statics.peerize(peer1V1)); + caster.sendPeering(PeerDTO.fromJSONObject(peer1V1), PeerDTO.fromJSONObject(peer1V1)); }); })); diff --git a/test/integration/peerings.js b/test/integration/peerings.js index c3eb6695b3f31eb6ef7dea50f266b3da5e543a79..dbbd98fed35691e21dc218564b3c1dc7b08f6346 100644 --- a/test/integration/peerings.js +++ b/test/integration/peerings.js @@ -14,7 +14,7 @@ const sync = require('./tools/sync'); const contacter = require('duniter-crawler').duniter.methods.contacter; const until = require('./tools/until'); const multicaster = require('../../app/lib/streams/multicaster'); -const Peer = require('../../app/lib/entity/peer'); +const PeerDTO = require('../../app/lib/dto/PeerDTO').PeerDTO const expectJSON = httpTest.expectJSON; @@ -114,9 +114,9 @@ describe("Network", function() { yield sync(0, 0, s1, s2); // Server 3 syncs block 0 yield sync(0, 0, s1, s3); - yield nodeS1.getPeer().then((peer) => nodeS2.postPeer(new Peer(peer).getRawSigned())).catch(e => console.error(e)) - yield nodeS2.getPeer().then((peer) => nodeS1.postPeer(new Peer(peer).getRawSigned())).catch(e => console.error(e)) - yield nodeS3.getPeer().then((peer) => nodeS1.postPeer(new Peer(peer).getRawSigned())).catch(e => console.error(e)) + yield nodeS1.getPeer().then((peer) => nodeS2.postPeer(PeerDTO.fromJSONObject(peer).getRawSigned())).catch(e => console.error(e)) + yield nodeS2.getPeer().then((peer) => nodeS1.postPeer(PeerDTO.fromJSONObject(peer).getRawSigned())).catch(e => console.error(e)) + yield nodeS3.getPeer().then((peer) => nodeS1.postPeer(PeerDTO.fromJSONObject(peer).getRawSigned())).catch(e => console.error(e)) yield commitS1(); yield [ until(s2, 'block', 1), diff --git a/test/integration/peers-same-pubkey.js b/test/integration/peers-same-pubkey.js index 96b0c267810c8db162276f63ae185a3a22496977..12bc7d221aa4273cc9d1651c4b678653e5c29e36 100644 --- a/test/integration/peers-same-pubkey.js +++ b/test/integration/peers-same-pubkey.js @@ -10,7 +10,7 @@ const sync = require('./tools/sync'); const until = require('./tools/until'); const toolbox = require('./tools/toolbox'); const multicaster = require('../../app/lib/streams/multicaster'); -const Peer = require('../../app/lib/entity/peer'); +const PeerDTO = require('../../app/lib/dto/PeerDTO').PeerDTO const catKeyPair = { pair: { @@ -53,7 +53,7 @@ describe("Peer document", function() { // // s2 syncs from s1 yield sync(0, 2, s1, s2); yield [ - s1.get('/network/peering').then((peer) => s2.post('/network/peering/peers', { peer: new Peer(peer).getRawSigned() })), // peer#2 + s1.get('/network/peering').then((peer) => s2.post('/network/peering/peers', { peer: PeerDTO.fromJSONObject(peer).getRawSigned() })), // peer#2 until(s2, 'peer', 1) ]; @@ -67,7 +67,7 @@ describe("Peer document", function() { const peer1 = yield s1.get('/network/peering'); peer1.should.have.property("block").match(/^2-/); yield [ - s3.post('/network/peering/peers', { peer: new Peer(peer1).getRawSigned() }), // peer#3 + s3.post('/network/peering/peers', { peer: PeerDTO.fromJSONObject(peer1).getRawSigned() }), // peer#3 until(s3, 'peer', 2) ]; const peer3 = yield s3.get('/network/peering'); diff --git a/test/integration/start_generate_blocks.js b/test/integration/start_generate_blocks.js index 4f1d8bb82b1f098b655a0eb72d3c05178c1aefbf..112d548f908c0ec1a0b6332e111b000198428190 100644 --- a/test/integration/start_generate_blocks.js +++ b/test/integration/start_generate_blocks.js @@ -10,7 +10,7 @@ const httpTest = require('./tools/http'); const commit = require('./tools/commit'); const until = require('./tools/until'); const multicaster = require('../../app/lib/streams/multicaster'); -const Peer = require('../../app/lib/entity/peer'); +const PeerDTO = require('../../app/lib/dto/PeerDTO').PeerDTO const contacter = require('duniter-crawler').duniter.methods.contacter; const sync = require('./tools/sync'); @@ -91,9 +91,9 @@ describe("Generation", function() { yield sync(0, 0, s1, s2); // Let each node know each other let peer1 = yield nodeS1.getPeer(); - yield nodeS2.postPeer(new Peer(peer1).getRawSigned()); + yield nodeS2.postPeer(PeerDTO.fromJSONObject(peer1).getRawSigned()); let peer2 = yield nodeS2.getPeer(); - yield nodeS1.postPeer(new Peer(peer2).getRawSigned()); + yield nodeS1.postPeer(PeerDTO.fromJSONObject(peer2).getRawSigned()); s1.startBlockComputation(); yield until(s2, 'block', 1); s2.startBlockComputation(); diff --git a/test/integration/tools/node.js b/test/integration/tools/node.js index 2ae154985c77842ff103be57c5c6da5d691052fe..43241a360e0fb97925fd091769844944c3a51614 100644 --- a/test/integration/tools/node.js +++ b/test/integration/tools/node.js @@ -7,7 +7,7 @@ var contacter = require('duniter-crawler').duniter.methods.contacter; var duniter = require('../../../index'); var multicaster = require('../../../app/lib/streams/multicaster'); var ConfDTO = require('../../../app/lib/dto/ConfDTO').ConfDTO -var Peer = require('../../../app/lib/entity/peer'); +var PeerDTO = require('../../../app/lib/dto/PeerDTO').PeerDTO var user = require('./user'); var http = require('./http'); const bma = require('duniter-bma').duniter.methods.bma; @@ -272,7 +272,7 @@ function Node (dbName, options) { this.submitPeer = function(peer, done) { return post('/network/peering/peers', { - "peer": Peer.statics.peerize(peer).getRawSigned() + "peer": PeerDTO.fromJSONObject(peer).getRawSigned() }, done); }; diff --git a/test/integration/tools/toolbox.js b/test/integration/tools/toolbox.js index 4a0e0ec0db3a814a079af5f4c95adca1ba7054c9..54927ea799ec0640d0d19802cd13221a79301384 100644 --- a/test/integration/tools/toolbox.js +++ b/test/integration/tools/toolbox.js @@ -11,6 +11,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const server_1 = require("../../../server"); const BlockDTO_1 = require("../../../app/lib/dto/BlockDTO"); const IdentityDTO_1 = require("../../../app/lib/dto/IdentityDTO"); +const PeerDTO_1 = require("../../../app/lib/dto/PeerDTO"); const _ = require('underscore'); const rp = require('request-promise'); const httpTest = require('../tools/http'); @@ -18,7 +19,6 @@ const sync = require('../tools/sync'); const commit = require('../tools/commit'); const user = require('../tools/user'); const until = require('../tools/until'); -const Peer = require('../../../app/lib/entity/peer'); const bma = require('duniter-bma').duniter.methods.bma; const multicaster = require('../../../app/lib/streams/multicaster'); const dtos = require('duniter-bma').duniter.methods.dtos; @@ -300,7 +300,7 @@ class TestingServer { return __awaiter(this, void 0, void 0, function* () { let p = yield this.get('/network/peering'); yield otherServer.post('/network/peering/peers', { - peer: Peer.statics.peerize(p).getRawSigned() + peer: PeerDTO_1.PeerDTO.fromJSONObject(p).getRawSigned() }); }); } diff --git a/test/integration/tools/toolbox.ts b/test/integration/tools/toolbox.ts index d6da48060c45f7dce641400dfe52997ea2de822c..e3ec93af89fdb9bbd93a77bd35c98e244cca5bc3 100644 --- a/test/integration/tools/toolbox.ts +++ b/test/integration/tools/toolbox.ts @@ -5,6 +5,7 @@ import {BlockDTO} from "../../../app/lib/dto/BlockDTO" import * as stream from "stream" import {RevocationDTO} from "../../../app/lib/dto/RevocationDTO" import {IdentityDTO} from "../../../app/lib/dto/IdentityDTO" +import {PeerDTO} from "../../../app/lib/dto/PeerDTO" const _ = require('underscore'); const rp = require('request-promise'); @@ -13,7 +14,6 @@ const sync = require('../tools/sync'); const commit = require('../tools/commit'); const user = require('../tools/user'); const until = require('../tools/until'); -const Peer = require('../../../app/lib/entity/peer'); const bma = require('duniter-bma').duniter.methods.bma; const multicaster = require('../../../app/lib/streams/multicaster'); const dtos = require('duniter-bma').duniter.methods.dtos; @@ -372,7 +372,7 @@ export class TestingServer { async sharePeeringWith(otherServer:TestingServer) { let p = await this.get('/network/peering'); await otherServer.post('/network/peering/peers', { - peer: Peer.statics.peerize(p).getRawSigned() + peer: PeerDTO.fromJSONObject(p).getRawSigned() }); } diff --git a/test/integration/tools/user.js b/test/integration/tools/user.js index 72bb1fc8b4707e8bf7e95c193596766f3c7ae68c..6ef3ede138759bd5de0264f3e72c876fcffc3b09 100644 --- a/test/integration/tools/user.js +++ b/test/integration/tools/user.js @@ -13,7 +13,7 @@ const constants = require('../../../app/lib/constants'); const CertificationDTO = require('../../../app/lib/dto/CertificationDTO').CertificationDTO const MembershipDTO = require('../../../app/lib/dto/MembershipDTO').MembershipDTO const RevocationDTO = require('../../../app/lib/dto/RevocationDTO').RevocationDTO -const Peer = require('../../../app/lib/entity/peer'); +const PeerDTO = require('../../../app/lib/dto/PeerDTO').PeerDTO const TransactionDTO = require('../../../app/lib/dto/TransactionDTO').TransactionDTO module.exports = function (uid, url, node) { @@ -317,7 +317,7 @@ function User (uid, options, node) { }; this.makePeer = (endpoints, overrideProps) => co(function*() { - const peer = Peer.statics.fromJSON({ + const peer = PeerDTO.fromJSONObject({ currency: node.server.conf.currency, pubkey: pub, block: '2-00008DF633FC158F9DB4864ABED696C1AA0FE5D617A7B5F7AB8DE7CA2EFCD4CB', @@ -326,7 +326,7 @@ function User (uid, options, node) { _.extend(peer, overrideProps || {}); const rawPeer = rawer.getPeerWithoutSignature(peer); peer.signature = keyring.Key(pub, sec).signSync(rawPeer); - return Peer.statics.fromJSON(peer); + return PeerDTO.fromJSONObject(peer) }); function post(uri, data, done) { diff --git a/test/integration/transactions-pruning.js b/test/integration/transactions-pruning.js index afb4126208041671ddfc859847c48f5bd7575f38..d17dd46660fe7204998f99efa73fbe5f8bb8a5f1 100644 --- a/test/integration/transactions-pruning.js +++ b/test/integration/transactions-pruning.js @@ -4,9 +4,7 @@ const co = require('co'); const should = require('should'); const user = require('./tools/user'); const commit = require('./tools/commit'); -const until = require('./tools/until'); const toolbox = require('./tools/toolbox'); -const Peer = require('../../app/lib/entity/peer'); const constants = require('../../app/lib/constants'); const common = require('duniter-common');