2016-04-29 17:45:13 -07:00
|
|
|
/*!
|
|
|
|
|
* errors.js - error objects for bcoin
|
|
|
|
|
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
|
2017-02-03 22:47:26 -08:00
|
|
|
* Copyright (c) 2014-2017, Christopher Jeffrey (MIT License).
|
2016-06-09 16:18:50 -07:00
|
|
|
* https://github.com/bcoin-org/bcoin
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
2016-12-14 05:56:58 -08:00
|
|
|
var assert = require('assert');
|
2016-11-19 10:45:31 -08:00
|
|
|
var util = require('../utils/util');
|
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.
|
|
|
|
|
* @constructor
|
|
|
|
|
* @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
|
|
|
* @property {String} code
|
|
|
|
|
* @property {Buffer} hash
|
|
|
|
|
* @property {Number} height (will be the coinbase height if not present).
|
|
|
|
|
* @property {Number} score
|
|
|
|
|
* @property {String} message
|
2017-02-22 06:35:35 -08:00
|
|
|
* @property {Boolean} malleated
|
2016-04-29 17:45:13 -07:00
|
|
|
*/
|
|
|
|
|
|
2017-02-22 06:35:35 -08:00
|
|
|
function VerifyError(msg, code, reason, score, malleated) {
|
2016-04-29 17:45:13 -07:00
|
|
|
Error.call(this);
|
|
|
|
|
|
|
|
|
|
if (Error.captureStackTrace)
|
|
|
|
|
Error.captureStackTrace(this, VerifyError);
|
|
|
|
|
|
2016-12-13 17:53:29 -08:00
|
|
|
assert(typeof code === 'string');
|
2016-12-14 07:05:14 -08:00
|
|
|
assert(typeof reason === 'string');
|
2016-12-13 17:53:29 -08:00
|
|
|
assert(score >= 0);
|
2016-05-23 00:49:52 -07:00
|
|
|
|
2017-01-09 22:36:10 -08:00
|
|
|
this.type = 'VerifyError';
|
|
|
|
|
this.message = '';
|
2016-04-29 17:45:13 -07:00
|
|
|
this.code = code;
|
2016-12-13 17:53:29 -08:00
|
|
|
this.reason = reason;
|
2016-04-29 17:45:13 -07:00
|
|
|
this.score = score;
|
2017-02-22 06:35:35 -08:00
|
|
|
this.malleated = malleated || false;
|
2017-01-09 22:36:10 -08:00
|
|
|
|
2016-12-13 17:53:29 -08:00
|
|
|
this.message = 'Verification failure: ' + reason
|
|
|
|
|
+ ' (code=' + code + ', score=' + score
|
2017-02-22 06:35:35 -08:00
|
|
|
+ ', hash=' + msg.rhash()
|
2016-12-13 17:53:29 -08:00
|
|
|
+ ')';
|
2016-04-29 17:45:13 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-19 10:45:31 -08:00
|
|
|
util.inherits(VerifyError, Error);
|
2016-04-29 17:45:13 -07:00
|
|
|
|
2016-11-19 05:29:16 -08:00
|
|
|
/**
|
|
|
|
|
* Verication result.
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
function VerifyResult() {
|
|
|
|
|
this.reason = 'unknown';
|
|
|
|
|
this.score = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-15 18:07:06 -07:00
|
|
|
/*
|
|
|
|
|
* Expose
|
|
|
|
|
*/
|
|
|
|
|
|
2016-05-13 09:23:57 -07:00
|
|
|
exports.VerifyError = VerifyError;
|
2016-11-19 05:29:16 -08:00
|
|
|
exports.VerifyResult = VerifyResult;
|