Merge pull request #77 from smh/feature/reject-message
Implemented Reject message
This commit is contained in:
commit
4d9f52ec98
@ -3,19 +3,65 @@
|
||||
var Message = require('../message');
|
||||
var inherits = require('util').inherits;
|
||||
var bitcore = require('bitcore-lib');
|
||||
var BufferUtil = bitcore.util.buffer;
|
||||
var utils = require('../utils');
|
||||
var BufferReader = bitcore.encoding.BufferReader;
|
||||
var BufferWriter = bitcore.encoding.BufferWriter;
|
||||
|
||||
// todo: add payload: https://en.bitcoin.it/wiki/Protocol_documentation#reject
|
||||
/**
|
||||
* The reject message is sent when messages are rejected.
|
||||
*
|
||||
* @see https://en.bitcoin.it/wiki/Protocol_documentation#reject
|
||||
* @param {Object=} arg - properties for the reject message
|
||||
* @param {String=} arg.message - type of message rejected
|
||||
* @param {Number=} arg.ccode - code relating to rejected message
|
||||
* @param {String=} arg.reason - text version of reason for rejection
|
||||
* @param {Buffer=} arg.data - Optional extra data provided by some errors.
|
||||
* @param {Object} options
|
||||
* @extends Message
|
||||
* @constructor
|
||||
*/
|
||||
function RejectMessage(arg, options) {
|
||||
if (!arg) {
|
||||
arg = {};
|
||||
}
|
||||
Message.call(this, options);
|
||||
this.command = 'reject';
|
||||
this.message = arg.message;
|
||||
this.ccode = arg.ccode;
|
||||
this.reason = arg.reason;
|
||||
this.data = arg.data;
|
||||
}
|
||||
inherits(RejectMessage, Message);
|
||||
|
||||
RejectMessage.prototype.setPayload = function() {};
|
||||
RejectMessage.CCODE = {
|
||||
REJECT_MALFORMED: 0x01,
|
||||
REJECT_INVALID: 0x10,
|
||||
REJECT_OBSOLETE: 0x11,
|
||||
REJECT_DUPLICATE: 0x12,
|
||||
REJECT_NONSTANDARD: 0x40,
|
||||
REJECT_DUST: 0x41,
|
||||
REJECT_INSUFFICIENTFEE: 0x42,
|
||||
REJECT_CHECKPOINT: 0x43
|
||||
};
|
||||
|
||||
RejectMessage.prototype.setPayload = function(payload) {
|
||||
var parser = new BufferReader(payload);
|
||||
this.message = parser.readVarLengthBuffer().toString('utf-8');
|
||||
this.ccode = parser.readUInt8();
|
||||
this.reason = parser.readVarLengthBuffer().toString('utf-8');
|
||||
this.data = parser.readAll();
|
||||
utils.checkFinished(parser);
|
||||
};
|
||||
|
||||
RejectMessage.prototype.getPayload = function() {
|
||||
return BufferUtil.EMPTY_BUFFER;
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarintNum(this.message.length);
|
||||
bw.write(new Buffer(this.message, 'utf-8'));
|
||||
bw.writeUInt8(this.ccode);
|
||||
bw.writeVarintNum(this.reason.length);
|
||||
bw.write(new Buffer(this.reason, 'utf-8'));
|
||||
bw.write(this.data);
|
||||
return bw.toBuffer();
|
||||
};
|
||||
|
||||
module.exports = RejectMessage;
|
||||
|
||||
@ -34,7 +34,7 @@
|
||||
"message": "f9beb4d9616c65727400000000000000bc0000004fe68fe973010000003766404f00000000b305434f00000000f2030000f1030000001027000048ee00000064000000004653656520626974636f696e2e6f72672f666562323020696620796f7520686176652074726f75626c6520636f6e6e656374696e67206166746572203230204665627275617279004730450221008389df45f0703f39ec8c1cc42c13810ffcae14995bb648340219e353b63b53eb022009ec65e1c1aaeec1fd334c6b684bde2b3f573060d5b70c3a46723326e4e8a4f1"
|
||||
},
|
||||
"reject": {
|
||||
"message": "f9beb4d972656a656374000000000000000000005df6e0e2"
|
||||
"message": "f9beb4d972656a6563740000000000003a000000f44033160274780015696e73756666696369656e74207072696f72697479165d408c9dd26fbdcb42b07546f6c37752301e3772f3ae441baf569e78839111"
|
||||
},
|
||||
"notfound": {
|
||||
"message": "f9beb4d96e6f74666f756e6400000000250000001d33d53201010000003a4af715be220eae7b2657582869daddf79ac4afb4a0e1cafa5b57e1afb8dfe2"
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
var should = require('chai').should();
|
||||
var expect = require('chai').expect;
|
||||
var P2P = require('../../../');
|
||||
var Messages = P2P.Messages;
|
||||
var sinon = require('sinon');
|
||||
@ -263,6 +264,43 @@ describe('Command Messages', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Reject', function() {
|
||||
it('should set properties from arg in constructor', function() {
|
||||
var message = messages.Reject({
|
||||
message: 'tx',
|
||||
ccode: 0x01,
|
||||
reason: 'transaction is malformed',
|
||||
data: new Buffer('12345678901234567890123456789012', 'hex')
|
||||
});
|
||||
message.message.should.equal('tx');
|
||||
message.ccode.should.equal(0x01);
|
||||
message.reason.should.equal('transaction is malformed');
|
||||
message.data.toString('hex').should.equal('12345678901234567890123456789012');
|
||||
});
|
||||
it('should let arg be optional in constructor', function() {
|
||||
var message = messages.Reject();
|
||||
expect(message.message).to.be.undefined;
|
||||
expect(message.ccode).to.be.undefined;
|
||||
expect(message.reason).to.be.undefined;
|
||||
expect(message.data).to.be.undefined;
|
||||
});
|
||||
it('should write payload correctly', function() {
|
||||
var message = messages.Reject({
|
||||
message: 'tx',
|
||||
ccode: 0x01,
|
||||
reason: 'transaction is malformed',
|
||||
data: new Buffer('12345678901234567890123456789012', 'hex')
|
||||
});
|
||||
var payload = message.getPayload();
|
||||
message = messages.Reject();
|
||||
message.setPayload(payload);
|
||||
message.message.should.equal('tx');
|
||||
message.ccode.should.equal(0x01);
|
||||
message.reason.should.equal('transaction is malformed');
|
||||
message.data.toString('hex').should.equal('12345678901234567890123456789012');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Version', function() {
|
||||
it('should set the default relay property as true', function() {
|
||||
var message = messages.Version();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user