bitpay / bitcore

A full stack for bitcoin and blockchain-based applications
https://bitcore.io/
MIT License
4.85k stars 2.09k forks source link

BitCore crashes when BWS url on existing copay wallet is changed to new bws server #1426

Closed JulianTosh closed 6 years ago

JulianTosh commented 7 years ago

I have existing wallets that point to https://bws.bitpay.com/bws/api.

When I change the url in copay to my personal instance of bws, bitcored crashes. The copay wallet will display a network connection error. When I restart bitcored and retry the copay network connection, the wallet scan completes successfully.

::ffff:xxx.xxx.xxx.xxx 2016-10-18T16:54:44.897Z "POST /bws/api/v1/addresses/scan" 200 16 10.027 "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 6 Build/MOB31H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.124 Mobile Safari/537.36" 2771ae62-6d92-4fbe-a66c-4795e03baf39 a19656671df3f66d38c0ef5d58325ca86830380edc7a0c74bc7edeabd93fa757
[2016-10-18T16:54:45.237Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/38xC..." 200 257 20.810 "bws-1.12.0" 
[2016-10-18T16:54:45.324Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/346D..." 200 257 7.666 "bws-1.12.0" 
[2016-10-18T16:54:45.370Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3J21..." 200 257 6.235 "bws-1.12.0" 
[2016-10-18T16:54:45.412Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3EAy..." 200 257 7.944 "bws-1.12.0" 
[2016-10-18T16:54:45.472Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/361H..." 200 257 7.392 "bws-1.12.0" 
[2016-10-18T16:54:45.519Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/37n2..." 200 257 6.536 "bws-1.12.0" 
[2016-10-18T16:54:45.571Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/35Hi..." 200 257 7.110 "bws-1.12.0" 
[2016-10-18T16:54:45.611Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3MwV..." 200 257 6.923 "bws-1.12.0" 
[2016-10-18T16:54:45.645Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3Eqp..." 200 257 6.007 "bws-1.12.0" 
[2016-10-18T16:54:45.706Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3M4d..." 200 257 17.122 "bws-1.12.0" 
[2016-10-18T16:54:45.765Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/39CH..." 200 257 10.702 "bws-1.12.0" 
[2016-10-18T16:54:45.831Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3H99..." 200 257 18.309 "bws-1.12.0" 
[2016-10-18T16:54:45.844Z] info: ::ffff:127.0.0.1 "POST /insight-api/addrs/txs?from=0&to=5&noAsm=1&noScriptSig=1&noSpent=1" 200 4285 95.547 "bws-1.12.0" 
::ffff:xxx.xxx.xxx.xxx 2016-10-18T16:54:45.886Z "GET /bws/api/v1/txhistory/?limit=5&r=93463" 200 - 196.409 "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 6 Build/MOB31H; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/53.0.2785.124 Mobile Safari/537.36" 2771ae62-6d92-4fbe-a66c-4795e03baf39 a19656671df3f66d38c0ef5d58325ca86830380edc7a0c74bc7edeabd93fa757
[2016-10-18T16:54:45.892Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3PVZ..." 200 257 4.917 "bws-1.12.0" 
[2016-10-18T16:54:45.923Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3FcF..." 200 257 5.073 "bws-1.12.0" 
ERR! /v1/pushnotifications/subscriptions/ :ECONNREFUSED:connect ECONNREFUSED 127.0.0.1:8000 
[2016-10-18T16:54:45.959Z] error: uncaught exception: [RangeError: Invalid status code: 0]
[2016-10-18T16:54:45.962Z] error: RangeError: Invalid status code: 0
    at ServerResponse.writeHead (_http_server.js:192:11)
    at ServerResponse.writeHead (/home/bitcore/walletServiceNode/node_modules/bitcore-wallet-service/node_modules/compression/node_modules/on-headers/index.js:55:19)
    at ServerResponse.writeHead (/home/bitcore/walletServiceNode/node_modules/bitcore-wallet-service/node_modules/morgan/node_modules/on-headers/index.js:55:19)
    at ServerResponse._implicitHeader (_http_server.js:157:8)
    at ServerResponse.end (/home/bitcore/walletServiceNode/node_modules/bitcore-wallet-service/node_modules/compression/index.js:102:14)
    at ServerResponse.send (/home/bitcore/walletServiceNode/node_modules/bitcore-wallet-service/node_modules/express/lib/response.js:205:10)
    at ServerResponse.json (/home/bitcore/walletServiceNode/node_modules/bitcore-wallet-service/node_modules/express/lib/response.js:250:15)
    at returnError (/home/bitcore/walletServiceNode/node_modules/bitcore-wallet-service/lib/expressapp.js:111:31)
    at /home/bitcore/walletServiceNode/node_modules/bitcore-wallet-service/lib/expressapp.js:626:25
    at Request._callback (/home/bitcore/walletServiceNode/node_modules/bitcore-wallet-service/lib/server.js:2974:12)
[2016-10-18T16:54:45.963Z] info: Beginning shutdown
[2016-10-18T16:54:45.963Z] info: Stopping bitcore-wallet-service
[2016-10-18T16:54:45.963Z] info: Stopping insight-api
[2016-10-18T16:54:45.966Z] info: ::ffff:127.0.0.1 "GET /insight-api/addr/3CFs..." 200 257 15.550 "bws-1.12.0" 
[2016-10-18T16:54:45.967Z] info: Stopping web
[2016-10-18T16:54:45.998Z] info: Stopping bitcoind
WARN REQUEST FAIL: http://localhost:3001/insight-api/addr/3Foo... ERROR: Error: connect ECONNREFUSED 127.0.0.1:3001 
WARN Insight error:  Error: connect ECONNREFUSED 127.0.0.1:3001
WARN Insight error:      at Object.exports._errnoException (util.js:907:11)
WARN Insight error:      at exports._exceptionWithHostPort (util.js:930:20)
WARN Insight error:      at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1077:14)
WARN Insight error:   { [Error: connect ECONNREFUSED 127.0.0.1:3001]
WARN Insight error:    code: 'ECONNREFUSED',
WARN Insight error:    errno: 'ECONNREFUSED',
WARN Insight error:    syscall: 'connect',
WARN Insight error:    address: '127.0.0.1',
WARN Insight error:    port: 3001 }
verb Notification ScanFinished { result: 'error', error: 'Insight Error' }

2016-10-18:0954 PDT
[bitcore@bitcore walletServiceNode]$ 
JulianTosh commented 7 years ago

Additionally, I'm capturing this copay crash from linux at the same moment.

chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:21
},Sha256.prototype._hash=function(){var H=new Buffer(32);return H.writeInt32BE(this._a,0),H.writeInt32BE(this._b,4),H.writeInt32BE(this._c,8),H.writeInt32BE(this._d,12),H.writeInt32BE(this._e,16),H.writeInt32BE(this._f,20),H.writeInt32BE(this._g,24),H.writeInt32BE(this._h,28),H},module.exports=Sha256}).call(this,require("buffer").Buffer)},{"./hash":48,buffer:337,inherits:46}],54:[function(require,module,exports){(function(Buffer){function Sha384(){this.init(),this._w=W,Hash.call(this,128,112)}var inherits=require("inherits"),SHA512=require("./sha512"),Hash=require("./hash"),W=new Array(160);inherits(Sha384,SHA512),Sha384.prototype.init=function(){return this._ah=3418070365,this._bh=1654270250,this._ch=2438529370,this._dh=355462360,this._eh=1731405415,this._fh=2394180231,this._gh=3675008525,this._hh=1203062813,this._al=3238371032,this._bl=914150663,this._cl=812702999,this._dl=4144912697,this._el=4290775857,this._fl=1750603025,this._

Error: More than one instance of bitcore-lib found. Please make sure to require bitcore-lib and check that submodules do not also include their own bitcore-lib dependency.
    at Object.bitcore.versionGuard (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:21:21783)
    at Object.<anonymous> (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:21:21812)
    at Object.r.65../lib/address (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:21:23673)
    at s (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:15:28261)
    at chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:15:28312
    at Object.<anonymous> (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:15:30115)
    at Object.r.3.../package.json (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:17:7643)
    at s (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:15:28261)
    at chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:15:28312
    at Object.r.11../api (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/angular.js:17:28024)
chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:74
-1==e.stack.indexOf(e.message)&&(e=e.message+"\n"+e.stack),Ia("modulerr",a,e.stack||e.message||e);}}});return b}function h(a,c){function d(b,e){if(a.hasOwnProperty(b)){if(a[b]===k)throw Ia("cdep",b+" <- "+l.join(" <- "));return a[b]}try{return l.unshift(b),a[b]=k,a[b]=c(b,e)}catch(f){throw a[b]===k&&delete a[b],f;}finally{l.shift()}}function e(a,c,f){var g=[];a=eb.$$annotate(a,b,f);for(var h=0,k=a.length;h<k;h++){var l=a[h];if("string"!==typeof l)throw Ia("itkn",l);g.push(c&&c.hasOwnProperty(l)?c[l]:
                                                                                                                                                                                                                                                                                             ^

Error: [$injector:unpr] http://errors.angularjs.org/1.5.3/$injector/unpr?p0=bwcServiceProvider%20%3C-%20bwcService%20%3C-%20sjcl%20%3C-%20profileService
    at Error (native)
    at chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:40:416
    at chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:77:7
    at Object.d [as get] (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:74:270)
    at chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:77:69
    at d (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:74:270)
    at e (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:75:1)
    at Object.invoke (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:75:86)
    at Object.$get (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:72:460)
    at Object.invoke (chrome-extension://fmfiokedacobbphpmdlmbjddcbnmgbko/public/lib/ionic.bundle.js:75:295)
dabura667 commented 7 years ago

You have push notifications enabled on Copay but your server is not configured properly to send push notifications.

Since you would need Bitpay's app store keys to register push notifications, I would say just disable push notifications on your wallet. Before switching servers.

JulianTosh commented 7 years ago

Where is the option to disable push notifications found in copay?

dabura667 commented 7 years ago

In the global app settings below the wallet list.

It's the 6th option from the top currently.

JulianTosh commented 7 years ago

This option is only present in my android client, not my Linux client. The above error was captured on the Linux client so there's no way to change this setting.

dabura667 commented 7 years ago

Hmmm... try turning it off on your android, then reset everything and see if Linux works when you import it after having turned it off in the options.

I think there might be something in the wallet metadata that tells the server to use push notifications... which doesn't make sense, but I remember having to fumble around with push notifications to migrate to my bws

toomim commented 7 years ago

I ran into this server crash too, and found an even simple testcase:

  1. Make a new wallet

Yes, that's it. When any user creates a wallet, the server dies. This should get fixed. The server should not crash just because it can't send push notifications.

Here's the error log:

erb Wallet created  livenet
"POST /bws/api/v2/wallets/" 200 51 11.256
verb Notification NewCopayer
verb Notification   copayerId
verb Notification   copayerName
"POST /bws/api/v2/wallets/xxx/copayers" 200
"PUT /bws/api/v1/preferences/" 200
"GET /bws/api/v1/txhistory/?limit=5&r=47589" 200
"GET /bws/api/v2/wallets/?includeExtendedInfo=0&twoStep=1&r=12554" 200

ERR! /v1/pushnotifications/subscriptions/ :ECONNREFUSED:connect ECONNREFUSED 127.0.0.1:8000 
[2016-12-29T06:32:31.152Z] error: uncaught exception: RangeError: Invalid status code: 0
    at ServerResponse.writeHead (_http_server.js:192:11)
    at ServerResponse.writeHead (/home/toomim/mynode/node_modules/on-headers/index.js:55:19)
    at ServerResponse.writeHead (/home/toomim/mynode/node_modules/on-headers/index.js:55:19)
    at ServerResponse._implicitHeader (_http_server.js:157:8)
    at ServerResponse.end (/home/toomim/mynode/node_modules/compression/index.js:102:14)
    at ServerResponse.send (/home/toomim/mynode/node_modules/express/lib/response.js:205:10)
    at ServerResponse.json (/home/toomim/mynode/node_modules/express/lib/response.js:250:15)
    at returnError (/home/toomim/mynode/node_modules/bitcore-wallet-service/lib/expressapp.js:111:31)
    at /home/toomim/mynode/node_modules/bitcore-wallet-service/lib/expressapp.js:654:25
    at Request._callback (/home/toomim/mynode/node_modules/bitcore-wallet-service/lib/server.js:3048:12)
[2016-12-29T06:32:31.154Z] error: RangeError: Invalid status code: 0
    at ServerResponse.writeHead (_http_server.js:192:11)
    at ServerResponse.writeHead (/home/toomim/mynode/node_modules/on-headers/index.js:55:19)
    at ServerResponse.writeHead (/home/toomim/mynode/node_modules/on-headers/index.js:55:19)
    at ServerResponse._implicitHeader (_http_server.js:157:8)
    at ServerResponse.end (/home/toomim/mynode/node_modules/compression/index.js:102:14)
    at ServerResponse.send (/home/toomim/mynode/node_modules/express/lib/response.js:205:10)
    at ServerResponse.json (/home/toomim/mynode/node_modules/express/lib/response.js:250:15)
    at returnError (/home/toomim/mynode/node_modules/bitcore-wallet-service/lib/expressapp.js:111:31)
    at /home/toomim/mynode/node_modules/bitcore-wallet-service/lib/expressapp.js:654:25
    at Request._callback (/home/toomim/mynode/node_modules/bitcore-wallet-service/lib/server.js:3048:12)
[2016-12-29T06:32:31.155Z] info: Beginning shutdown
[2016-12-29T06:32:31.155Z] info: Stopping insight-ui
[2016-12-29T06:32:31.156Z] info: Stopping bitcore-wallet-service
[2016-12-29T06:32:31.156Z] info: Stopping insight-api
[2016-12-29T06:32:31.156Z] info: Stopping web
[2016-12-29T06:32:31.157Z] info: Stopping bitcoind
[2016-12-29T06:32:31.762Z] warn: ZMQ disconnect: tcp://127.0.0.1:28332
toomim commented 7 years ago

FWIW I'm working around this bug currently by disabling push notifications from expressapp.js by adding res.json({}); return; to these two functions:

  router.post('/v1/pushnotifications/subscriptions/', function(req, res) {
    res.json({}); return;  // I added this line to abort this function
    getServerWithAuth(req, res, function(server) {
      server.pushNotificationsSubscribe(req.body, function(err, response) {
        if (err) return returnError(err, res, req);
        res.json(response);
      });
    });
  });

  router.delete('/v1/pushnotifications/subscriptions/', function(req, res) {
    res.json({}); return;  // I added this line to abort this function
    getServerWithAuth(req, res, function(server) {
      server.pushNotificationsUnsubscribe(function(err, response) {
        if (err) return returnError(err, res, req);
        res.json(response);
      });
    });
  });

Here are two proposals for more permanent solutions:

  1. Instead of the returnError() lines, just print an error, but let the server continue to run
  2. Check to see if push notifications have been enabled in config (is there even a config for this?) and if not, skip the subscription/unsubscription process.
toomim commented 7 years ago

I just posted this to https://github.com/bitpay/bitcore-wallet-service/issues/612, which seems more appropriate.