Commit 40b14f91 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[fix] Make tests pass for sync

parent 77e96e67
......@@ -26,7 +26,7 @@ import {FileDAL} from "../../lib/dal/fileDAL"
import {RemoteSynchronizer} from "./lib/sync/RemoteSynchronizer"
import {AbstractSynchronizer} from "./lib/sync/AbstractSynchronizer"
import {LocalPathSynchronizer} from "./lib/sync/LocalPathSynchronizer"
import {CommonConstants} from "../../lib/common-libs/constants";
import {CommonConstants} from "../../lib/common-libs/constants"
export const CrawlerDependency = {
duniter: {
......@@ -49,8 +49,8 @@ export const CrawlerDependency = {
return crawler.sandboxPull(server)
},
synchronize: (server:Server, onHost:string, onPort:number, upTo:number, chunkLength:number) => {
const strategy = new RemoteSynchronizer(onHost, onPort, server, chunkLength)
synchronize: (server:Server, onHost:string, onPort:number, upTo:number, chunkLength:number, allowLocalSync = false) => {
const strategy = new RemoteSynchronizer(onHost, onPort, server, chunkLength, undefined, undefined, allowLocalSync)
const remote = new Synchroniser(server, strategy)
const syncPromise = remote.sync(upTo, chunkLength)
return {
......@@ -77,6 +77,7 @@ export const CrawlerDependency = {
{ value: '--cautious', desc: 'Check blocks validity during sync (overrides --nocautious option).'},
{ value: '--nopeers', desc: 'Do not retrieve peers during sync.'},
{ value: '--nop2p', desc: 'Disables P2P downloading of blocs during sync.'},
{ value: '--localsync', desc: 'Allow to synchronize on nodes with local network IP address for `sync` command' },
{ value: '--nosources', desc: 'Do not parse sources (UD, TX) during sync (debug purposes).'},
{ value: '--nosbx', desc: 'Do not retrieve sandboxes during sync.'},
{ value: '--onlypeers', desc: 'Will only try to sync peers.'},
......@@ -127,7 +128,7 @@ export const CrawlerDependency = {
const sp = source.split(':')
const onHost = sp[0]
const onPort = parseInt(sp[1] ? sp[1] : '443') // Defaults to 443
strategy = new RemoteSynchronizer(onHost, onPort, server, CommonConstants.SYNC_BLOCKS_CHUNK, noShufflePeers === true, otherDAL)
strategy = new RemoteSynchronizer(onHost, onPort, server, CommonConstants.SYNC_BLOCKS_CHUNK, noShufflePeers === true, otherDAL, program.localsync !== undefined)
} else {
strategy = new LocalPathSynchronizer(source, server, CommonConstants.SYNC_BLOCKS_CHUNK)
}
......
......@@ -7,7 +7,7 @@ import {ISyncDownloader} from "./ISyncDownloader"
import {cliprogram} from "../../../../lib/common-libs/programOptions"
import {Keypair} from "../../../../lib/dto/ConfDTO"
import {IRemoteContacter} from "./IRemoteContacter"
import {ManualPromise, newManualPromise} from "../../../../lib/common-libs/manual-promise"
import {ManualPromise} from "../../../../lib/common-libs/manual-promise"
import {GlobalFifoPromise} from "../../../../service/GlobalFifoPromise"
import {getNanosecondsTime} from "../../../../ProcessCpuProfiler"
import {CommonConstants} from "../../../../lib/common-libs/constants"
......@@ -40,6 +40,7 @@ export class P2PSyncDownloader extends ASyncDownloader implements ISyncDownloade
private watcher:Watcher,
private logger:any,
public chunkSize: number,
public allowLocalSync: boolean,
) {
super(chunkSize)
this.TOO_LONG_TIME_DOWNLOAD = "No answer after " + this.MAX_DELAY_PER_DOWNLOAD + "ms, will retry download later.";
......@@ -48,7 +49,7 @@ export class P2PSyncDownloader extends ASyncDownloader implements ISyncDownloade
this.processing = Array.from({ length: this.numberOfChunksToDownload }).map(() => false);
this.handler = Array.from({ length: this.numberOfChunksToDownload }).map(() => null);
this.p2pCandidates = peers.map(p => new P2pCandidate(PeerDTO.fromJSONObject(p), this.keypair, this.logger))
this.p2pCandidates = peers.map(p => new P2pCandidate(PeerDTO.fromJSONObject(p), this.keypair, this.logger, allowLocalSync))
}
get maxSlots(): number {
......
......@@ -28,17 +28,16 @@ import {FsSyncDownloader} from "./FsSyncDownloader"
import {AbstractSynchronizer} from "./AbstractSynchronizer"
import {pullSandboxToLocalServer} from "../sandbox"
import * as path from 'path'
import {IRemoteContacter} from "./IRemoteContacter";
import {BMARemoteContacter} from "./BMARemoteContacter";
import {WS2PConnection, WS2PPubkeyRemoteAuth, WS2PPubkeySyncLocalAuth} from "../../../ws2p/lib/WS2PConnection";
import {WS2PRequester} from "../../../ws2p/lib/WS2PRequester";
import {WS2PMessageHandler} from "../../../ws2p/lib/impl/WS2PMessageHandler";
import {WS2PResponse} from "../../../ws2p/lib/impl/WS2PResponse";
import {DataErrors} from "../../../../lib/common-libs/errors";
import {KeyGen} from "../../../../lib/common-libs/crypto/keyring";
import {WS2PRemoteContacter} from "./WS2PRemoteContacter";
import {Keypair} from "../../../../lib/dto/ConfDTO";
import {cat} from "shelljs";
import {IRemoteContacter} from "./IRemoteContacter"
import {BMARemoteContacter} from "./BMARemoteContacter"
import {WS2PConnection, WS2PPubkeyRemoteAuth, WS2PPubkeySyncLocalAuth} from "../../../ws2p/lib/WS2PConnection"
import {WS2PRequester} from "../../../ws2p/lib/WS2PRequester"
import {WS2PMessageHandler} from "../../../ws2p/lib/impl/WS2PMessageHandler"
import {WS2PResponse} from "../../../ws2p/lib/impl/WS2PResponse"
import {DataErrors} from "../../../../lib/common-libs/errors"
import {KeyGen} from "../../../../lib/common-libs/crypto/keyring"
import {WS2PRemoteContacter} from "./WS2PRemoteContacter"
import {Keypair} from "../../../../lib/dto/ConfDTO"
const logger = NewLogger()
......@@ -62,6 +61,7 @@ export class RemoteSynchronizer extends AbstractSynchronizer {
chunkSize: number,
private noShufflePeers = false,
private otherDAL?:FileDAL,
private allowLocalSync = false,
) {
super(chunkSize)
}
......@@ -227,7 +227,7 @@ export class RemoteSynchronizer extends AbstractSynchronizer {
p2pDownloader(): ISyncDownloader {
if (!this.theP2pDownloader) {
this.theP2pDownloader = new P2PSyncDownloader(this.currency, this.server.conf.pair, this.localNumber, this.to, this.shuffledPeers, this.watcher, logger, this.chunkSize)
this.theP2pDownloader = new P2PSyncDownloader(this.currency, this.server.conf.pair, this.localNumber, this.to, this.shuffledPeers, this.watcher, logger, this.chunkSize, this.allowLocalSync)
}
return this.theP2pDownloader
}
......
......@@ -19,7 +19,8 @@ export class P2pCandidate {
constructor(
private p: PeerDTO,
private keypair: Keypair,
private logger: any
private logger: any,
private allowLocalSync: boolean,
) {
this.apiPromise = this.initAPI()
this.dlPromise = querablep(Promise.resolve(null))
......@@ -114,7 +115,7 @@ export class P2pCandidate {
try {
const apis = this.getRemoteAPIs()
const syncApi = await RemoteSynchronizer.getSyncAPI(apis, this.keypair)
if ((syncApi && syncApi.api.hostName || '').match(/^(localhost|192|127)/)) {
if (!this.allowLocalSync && ((syncApi && syncApi.api.hostName || '').match(/^(localhost|192|127)/))) {
return null
}
this.api = syncApi.api
......
......@@ -126,7 +126,7 @@ describe("CLI", function() {
it('sync 7 blocks (fast)', async () => {
// await execute(['reset', 'data']);
await execute(['sync', fakeServer.host + ':' + String(fakeServer.port), '--nocautious', '--nointeractive', '--noshuffle', '7']);
await execute(['sync', fakeServer.host + ':' + String(fakeServer.port), '--nocautious', '--nointeractive', '--noshuffle', '--localsync', '7']);
const res = await execute(['export-bc', '--nostdout']);
res[res.length - 1].should.have.property('number').equal(7);
res.should.have.length(7 + 1); // blocks #0..#7
......
......@@ -13,8 +13,8 @@
import {WS2PConstants} from "../../../app/modules/ws2p/lib/constants"
import {assertEqual, assertNotNull, createCurrencyWith2Blocks, writeBasicTestWith2Users} from "../tools/test-framework"
import {NewTestingServer, TestWS2PAPI} from "../tools/toolbox";
import {CrawlerDependency} from "../../../app/modules/crawler/index";
import {NewTestingServer, TestWS2PAPI} from "../tools/toolbox"
import {CrawlerDependency} from "../../../app/modules/crawler/index"
describe('WS2P sync', () => writeBasicTestWith2Users((test) => {
......@@ -54,7 +54,7 @@ describe('WS2P sync', () => writeBasicTestWith2Users((test) => {
const s2 = NewTestingServer({ pair: cat.keypair })
await s2.initWithDAL()
// We sync on s1
await CrawlerDependency.duniter.methods.synchronize(s2._server, ws2p.host, ws2p.port, 2, 2).syncPromise
await CrawlerDependency.duniter.methods.synchronize(s2._server, ws2p.host, ws2p.port, 2, 2, true).syncPromise
assertNotNull(await s2.dal.getCurrentBlockOrNull())
})
}))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment