2016-04-29 17:45:13 -07:00
|
|
|
/*!
|
2018-02-01 13:28:31 -08:00
|
|
|
* errors.js - error objects for hsk
|
2018-02-01 13:40:45 -08:00
|
|
|
* Copyright (c) 2017-2018, Christopher Jeffrey (MIT License).
|
2018-02-01 13:28:31 -08:00
|
|
|
* https://github.com/handshakecompany/hsk
|
2016-04-29 17:45:13 -07:00
|
|
|
*/
|
|
|
|
|
|
2016-06-13 01:06:01 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-02-03 22:47:26 -08:00
|
|
|
/**
|
|
|
|
|
* @module protocol/errors
|
|
|
|
|
*/
|
|
|
|
|
|
2017-06-29 20:54:07 -07:00
|
|
|
const assert = require('assert');
|
2016-04-29 17:45:13 -07:00
|
|
|
|
|
|
|
|
/**
|
2017-11-16 19:46:19 -08:00
|
|
|
* Verify Error
|
2016-04-29 17:45:13 -07:00
|
|
|
* An error thrown during verification. Can be either
|
|
|
|
|
* a mempool transaction validation error or a blockchain
|
|
|
|
|
* block verification error. Ultimately used to send
|
|
|
|
|
* `reject` packets to peers.
|
|
|
|
|
* @extends Error
|
2016-05-23 00:49:52 -07:00
|
|
|
* @param {Block|TX} msg
|
2017-02-03 22:47:26 -08:00
|
|
|
* @param {String} code - Reject packet code.
|
2016-04-29 17:45:13 -07:00
|
|
|
* @param {String} reason - Reject packet reason.
|
|
|
|
|
* @param {Number} score - Ban score increase
|
|
|
|
|
* (can be -1 for no reject packet).
|
2017-02-22 06:35:35 -08:00
|
|
|
* @param {Boolean} malleated
|
2016-04-29 17:45:13 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-11-16 19:46:19 -08:00
|
|
|
class VerifyError extends Error {
|
|
|
|
|
/**
|
|
|
|
|
* Create a verify error.
|
|
|
|
|
* @constructor
|
|
|
|
|
* @param {Block|TX} msg
|
|
|
|
|
* @param {String} code - Reject packet code.
|
|
|
|
|
* @param {String} reason - Reject packet reason.
|
|
|
|
|
* @param {Number} score - Ban score increase
|
|
|
|
|
* (can be -1 for no reject packet).
|
|
|
|
|
* @param {Boolean} malleated
|
|
|
|
|
*/
|
2016-04-29 17:45:13 -07:00
|
|
|
|
2017-11-16 19:46:19 -08:00
|
|
|
constructor(msg, code, reason, score, malleated) {
|
|
|
|
|
super();
|
2016-05-23 00:49:52 -07:00
|
|
|
|
2017-11-16 19:46:19 -08:00
|
|
|
assert(typeof code === 'string');
|
|
|
|
|
assert(typeof reason === 'string');
|
|
|
|
|
assert(score >= 0);
|
2017-01-09 22:36:10 -08:00
|
|
|
|
2017-11-16 19:46:19 -08:00
|
|
|
this.type = 'VerifyError';
|
|
|
|
|
this.message = '';
|
|
|
|
|
this.code = code;
|
|
|
|
|
this.reason = reason;
|
|
|
|
|
this.score = score;
|
|
|
|
|
this.hash = msg.hash('hex');
|
|
|
|
|
this.malleated = malleated || false;
|
2017-07-13 11:12:28 -07:00
|
|
|
|
2017-11-16 19:46:19 -08:00
|
|
|
this.message = `Verification failure: ${reason}`
|
2018-03-02 19:26:21 -08:00
|
|
|
+ ` (code=${code} score=${score} hash=${msg.hash('hex')})`;
|
2016-04-29 17:45:13 -07:00
|
|
|
|
2017-11-16 19:46:19 -08:00
|
|
|
if (Error.captureStackTrace)
|
|
|
|
|
Error.captureStackTrace(this, VerifyError);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-29 17:45:13 -07:00
|
|
|
|
2016-05-15 18:07:06 -07:00
|
|
|
/*
|
|
|
|
|
* Expose
|
|
|
|
|
*/
|
|
|
|
|
|
2016-05-13 09:23:57 -07:00
|
|
|
exports.VerifyError = VerifyError;
|