Skip to content

MAKE P2P CONNECTION MORE RESILIENT #43

@g00dnatur3

Description

@g00dnatur3

I have a ZEN pool with the p2p enabled.

Always the first two or three p2p connection attempts fail:

34|pool_zen | 2019-06-01 21:36:57 [Pool] [horizen] (Thread 1) p2p had a socket error {"errno":"ECONNRESET","code":"ECONNRESET","syscall":"read"}

To fix this issue and make the p2p connection more resilient i modified the pool.js as follows:

   let retryCount = 1000

    function SetupPeer() {
        if (!options.p2p || !options.p2p.enabled)
            return;

        if (options.testnet && !options.coin.peerMagicTestnet) {
            emitErrorLog('p2p cannot be enabled in testnet without peerMagicTestnet set in coin configuration');
            return;
        }
        else if (!options.coin.peerMagic) {
            emitErrorLog('p2p cannot be enabled without peerMagic set in coin configuration');
            return;
        }

        _this.peer = new peer(options);
        _this.peer.on('connected', function () {
            emitLog('p2p connection successful');
        }).on('connectionRejected', function () {
            emitErrorLog('p2p connection failed - likely incorrect p2p magic value');
            retryCount--
            if (retryCount > 0) {
              emitLog('p2p trying to connect again soon... retryCount: ' + retryCount)
              setTimeout(SetupPeer, 2000)
            }
        }).on('disconnected', function () {
            emitWarningLog('p2p peer node disconnected - attempting reconnection...');
        }).on('connectionFailed', function (e) {
            emitErrorLog('p2p connection failed - likely incorrect host or port');
        }).on('socketError', function (e) {
            emitErrorLog('p2p had a socket error ' + JSON.stringify(e));
        }).on('error', function (msg) {
            emitWarningLog('p2p had an error ' + msg);
        }).on('blockFound', function (hash) {
            _this.processBlockNotify(hash, 'p2p');
        });
    }

As you can see I haved added a retry.

Now this implementation is obviously NOT perfect, BUT it proves to work.

Before i would have 0 p2p connections... maybe 1 in begining then they would all die..

With the code above i am able to maintain p2p connections

FYI my horizen daemon has TLS enabled

Please include some form of retry in your code as I have layed out.

Thanks

Cheers!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions