Skip to content

Commit

Permalink
Won't show challenge timeout error
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Mar 29, 2023
1 parent fa95a95 commit 433ea40
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 15 deletions.
2 changes: 1 addition & 1 deletion __tests__/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ClientTest {
new Promise(resolve => this.alice.onConnect(resolve)),
new Promise(resolve => this.bob.onConnect(resolve)),
]);
await new Promise(resolve => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 10000));
expect(this.alice.isReady).toBe(true);
expect(this.bob.isReady).toBe(true);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class SessionTest {
new Promise(resolve => this.alice.onConnect(resolve)),
new Promise(resolve => this.bob.onConnect(resolve)),
]);
await new Promise(resolve => setTimeout(resolve, 1000));
await new Promise(resolve => setTimeout(resolve, 10000));
expect(this.alice.isReady).toBe(true);
expect(this.bob.isReady).toBe(true);
});
Expand Down
25 changes: 21 additions & 4 deletions dist/nkn.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,10 @@ class Client {
}

let challengeDone;
let challengeHandler = new Promise(async (resolve, reject) => {
let challengeHandler = new Promise((resolve, reject) => {
challengeDone = resolve;
setTimeout(() => {
reject(new Error('Challenge timeout'));
reject(new common.errors.ChallengeTimeoutError());
}, consts.waitForChallengeTimeout);
});

Expand All @@ -722,7 +722,9 @@ class Client {
data.Signature = common.util.bytesToHex(req.Signature);
}
} catch (e) {
console.log(e);
if (!e instanceof common.errors.ChallengeTimeoutError) {
console.log(e);
}
}

ws.send(JSON.stringify(data));
Expand Down Expand Up @@ -1682,7 +1684,7 @@ async function sign(privateKey, message) {
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.rpcRespErrCodes = exports.WrongPasswordError = exports.UnknownError = exports.ServerError = exports.RpcTimeoutError = exports.RpcError = exports.NotEnoughBalanceError = exports.InvalidWalletVersionError = exports.InvalidWalletFormatError = exports.InvalidResponseError = exports.InvalidDestinationError = exports.InvalidArgumentError = exports.InvalidAddressError = exports.DecryptionError = exports.DataSizeTooLargeError = exports.ClientNotReadyError = exports.AddrNotAllowedError = void 0;
exports.rpcRespErrCodes = exports.WrongPasswordError = exports.UnknownError = exports.ServerError = exports.RpcTimeoutError = exports.RpcError = exports.NotEnoughBalanceError = exports.InvalidWalletVersionError = exports.InvalidWalletFormatError = exports.InvalidResponseError = exports.InvalidDestinationError = exports.InvalidArgumentError = exports.InvalidAddressError = exports.DecryptionError = exports.DataSizeTooLargeError = exports.ClientNotReadyError = exports.ChallengeTimeoutError = exports.AddrNotAllowedError = void 0;
const rpcRespErrCodes = {
success: 0,
wrongNode: 48001,
Expand Down Expand Up @@ -1941,6 +1943,21 @@ class RpcError extends Error {
}

exports.RpcError = RpcError;

class ChallengeTimeoutError extends Error {
constructor(message = 'challenge timeout', ...params) {
super(message, ...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, ChallengeTimeoutError);
}

this.name = 'ChallengeTimeoutError';
}

}

exports.ChallengeTimeoutError = ChallengeTimeoutError;
},{}],9:[function(require,module,exports){
'use strict';

Expand Down
2 changes: 1 addition & 1 deletion dist/nkn.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions lib/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -700,10 +700,10 @@ class Client {
}

let challengeDone;
let challengeHandler = new Promise(async (resolve, reject) => {
let challengeHandler = new Promise((resolve, reject) => {
challengeDone = resolve;
setTimeout(() => {
reject(new Error('Challenge timeout'));
reject(new common.errors.ChallengeTimeoutError());
}, consts.waitForChallengeTimeout);
});

Expand All @@ -721,7 +721,9 @@ class Client {
data.Signature = common.util.bytesToHex(req.Signature);
}
} catch (e) {
console.log(e);
if (!e instanceof common.errors.ChallengeTimeoutError) {
console.log(e);
}
}

ws.send(JSON.stringify(data));
Expand Down
19 changes: 17 additions & 2 deletions lib/common/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.rpcRespErrCodes = exports.WrongPasswordError = exports.UnknownError = exports.ServerError = exports.RpcTimeoutError = exports.RpcError = exports.NotEnoughBalanceError = exports.InvalidWalletVersionError = exports.InvalidWalletFormatError = exports.InvalidResponseError = exports.InvalidDestinationError = exports.InvalidArgumentError = exports.InvalidAddressError = exports.DecryptionError = exports.DataSizeTooLargeError = exports.ClientNotReadyError = exports.AddrNotAllowedError = void 0;
exports.rpcRespErrCodes = exports.WrongPasswordError = exports.UnknownError = exports.ServerError = exports.RpcTimeoutError = exports.RpcError = exports.NotEnoughBalanceError = exports.InvalidWalletVersionError = exports.InvalidWalletFormatError = exports.InvalidResponseError = exports.InvalidDestinationError = exports.InvalidArgumentError = exports.InvalidAddressError = exports.DecryptionError = exports.DataSizeTooLargeError = exports.ClientNotReadyError = exports.ChallengeTimeoutError = exports.AddrNotAllowedError = void 0;
const rpcRespErrCodes = {
success: 0,
wrongNode: 48001,
Expand Down Expand Up @@ -261,4 +261,19 @@ class RpcError extends Error {

}

exports.RpcError = RpcError;
exports.RpcError = RpcError;

class ChallengeTimeoutError extends Error {
constructor(message = 'challenge timeout', ...params) {
super(message, ...params);

if (Error.captureStackTrace) {
Error.captureStackTrace(this, ChallengeTimeoutError);
}

this.name = 'ChallengeTimeoutError';
}

}

exports.ChallengeTimeoutError = ChallengeTimeoutError;
8 changes: 5 additions & 3 deletions src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,10 +614,10 @@ export default class Client {
}

let challengeDone: Function;
let challengeHandler: Promise<SaltAndSignature> = new Promise<SaltAndSignature>(async (resolve, reject) => {
let challengeHandler: Promise<SaltAndSignature> = new Promise<SaltAndSignature>((resolve, reject) => {
challengeDone = resolve;
setTimeout(() => {
reject(new Error('Challenge timeout'));
reject(new common.errors.ChallengeTimeoutError());
}, waitForChallengeTimeout);
});

Expand All @@ -633,7 +633,9 @@ export default class Client {
data.Signature = common.util.bytesToHex(req.Signature);
}
} catch (e) {
console.log(e);
if (!e instanceof common.errors.ChallengeTimeoutError) {
console.log(e);
}
}
ws.send(JSON.stringify(data));
this.shouldReconnect = true;
Expand Down
10 changes: 10 additions & 0 deletions src/common/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,13 @@ export class RpcError extends Error {
this.name = 'RpcError';
}
}

export class ChallengeTimeoutError extends Error {
constructor(message = 'challenge timeout', ...params) {
super(message, ...params);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, ChallengeTimeoutError);
}
this.name = 'ChallengeTimeoutError';
}
}

0 comments on commit 433ea40

Please sign in to comment.