Implement messages.error

This commit is contained in:
Kagami Hiiragi 2015-01-16 03:08:56 +03:00
parent 1f5ae113b0
commit 1d8f694c84
3 changed files with 101 additions and 12 deletions

View File

@ -36,13 +36,13 @@ API documentation is available [here](https://bitchan.github.io/bitmessage/docs/
- [x] message encodings - [x] message encodings
- [x] service features - [x] service features
- [x] pubkey features - [x] pubkey features
- [ ] Message types - [x] Message types
- [x] version - [x] version
- [x] verack - [x] verack
- [x] addr - [x] addr
- [x] inv - [x] inv
- [x] getdata - [x] getdata
- [ ] error - [x] error
- [x] object - [x] object
- [ ] Object types - [ ] Object types
- [ ] getpubkey - [ ] getpubkey
@ -58,9 +58,6 @@ API documentation is available [here](https://bitchan.github.io/bitmessage/docs/
- [x] getRipe - [x] getRipe
- [x] fromRandom - [x] fromRandom
- [ ] fromPassphrase - [ ] fromPassphrase
- [ ] Message
- [ ] encrypt
- [ ] decrypt
- [x] UserAgent - [x] UserAgent
- [ ] Parse PyBitmessage configs - [ ] Parse PyBitmessage configs
- [ ] keys.dat - [ ] keys.dat

View File

@ -1,6 +1,8 @@
/** /**
* Working with messages. * Working with messages.
* @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_types} * @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_types}
* @see {@link https://bitmessage.org/wiki/Protocol_specification_v3#Message_types}
* @see {@link https://bitmessage.org/Bitmessage%20Technical%20Paper.pdf}
* @module bitmessage/messages * @module bitmessage/messages
*/ */
@ -23,8 +25,7 @@ exports.version = {
/** /**
* Decode `version` payload. * Decode `version` payload.
* NOTE: `nonce` is copied. * NOTE: `nonce` is copied.
* @param {Buffer} buf - Buffer that starts with encoded `version` * @param {Buffer} buf - `version` payload
* payload
* @return {Object} Decoded `version` structure. * @return {Object} Decoded `version` structure.
*/ */
decode: function(buf) { decode: function(buf) {
@ -107,7 +108,7 @@ exports.version = {
exports.addr = { exports.addr = {
/** /**
* Decode `addr` payload. * Decode `addr` payload.
* @param {Buffer} buf - Buffer that starts with encoded `addr` payload * @param {Buffer} buf - `addr` payload
* @return {Object} Decoded `addr` structure. * @return {Object} Decoded `addr` structure.
*/ */
decode: function(buf) { decode: function(buf) {
@ -150,7 +151,7 @@ exports.addr = {
var inv = exports.inv = { var inv = exports.inv = {
/** /**
* Decode `inv` payload. * Decode `inv` payload.
* @param {Buffer} buf - Buffer that starts with encoded `inv` payload * @param {Buffer} buf - `inv` payload
* @return {Object} Decoded `inv` structure. * @return {Object} Decoded `inv` structure.
*/ */
decode: function(buf) { decode: function(buf) {
@ -173,7 +174,7 @@ var inv = exports.inv = {
/** /**
* Encode `inv` payload. * Encode `inv` payload.
* @param {Buffer[]} inventory - Inventory vectors (encoded) * @param {Buffer[]} inventory - Inventory vector list (encoded)
* @return {Buffer} Encoded `inv` payload. * @return {Buffer} Encoded `inv` payload.
*/ */
encode: function(inventory) { encode: function(inventory) {
@ -192,6 +193,74 @@ var inv = exports.inv = {
*/ */
exports.getdata = inv; exports.getdata = inv;
/**
* `error` message.
* @see {@link https://bitmessage.org/wiki/Protocol_specification_v3#error}
* @namespace
*/
var error = exports.error = {
/**
* Just a warning.
*/
WARNING: 0,
/**
* It's an error, something was going wrong (e.g. an object got lost).
*/
ERROR: 1,
/**
* It's a fatal error. The node will drop the line for that error and
* maybe ban you for some time.
*/
FATAL: 2,
/**
* Decode `error` payload.
* @param {Buffer} buf - `error` payload
* @return {Object} Decoded `error` structure.
*/
decode: function(buf) {
assert(buf.length >= 4, "Buffer is too small");
var decodedFatal = structs.var_int.decode(buf);
var decodedBanTime = structs.var_int.decode(decodedFatal.rest);
var decodedVector = structs.var_str.decode(decodedBanTime.rest);
var decodedErrorText = structs.var_str.decode(decodedVector.rest);
var length = (
decodedFatal.length +
decodedBanTime.length +
decodedVector.length +
decodedErrorText.length
);
return {
fatal: decodedFatal.value,
banTime: decodedBanTime.value,
vector: decodedVector.str,
errorText: decodedErrorText.str,
// Real data length.
length: length,
};
},
/**
* Encode `error` payload.
* @param {Object} opts - Error options
* @return {Buffer} Encoded `error` payload.
*/
encode: function(opts) {
var fatal = opts.fatal || error.WARNING;
var banTime = opts.banTime || 0;
var vector = opts.vector || "";
var errorText = opts.errorText || "";
return Buffer.concat([
structs.var_int.encode(fatal),
structs.var_int.encode(banTime),
structs.var_str.encode(vector),
structs.var_str.encode(errorText),
]);
},
};
/** /**
* `object` message. An `object` is a message which is shared throughout * `object` message. An `object` is a message which is shared throughout
* a stream. It is the only message which propagates; all others are * a stream. It is the only message which propagates; all others are
@ -203,8 +272,7 @@ exports.object = {
/** /**
* Decode `object` payload. * Decode `object` payload.
* NOTE: `nonce` and `payload` are copied. * NOTE: `nonce` and `payload` are copied.
* @param {Buffer} buf - Buffer that starts with encoded `object` * @param {Buffer} buf - `object` payload
* payload
* @return {Object} Decoded `object` structure. * @return {Object} Decoded `object` structure.
*/ */
decode: function(buf) { decode: function(buf) {

24
test.js
View File

@ -21,6 +21,7 @@ var messages = bitmessage.messages;
var version = messages.version; var version = messages.version;
var addr = messages.addr; var addr = messages.addr;
var inv = messages.inv; var inv = messages.inv;
var error = messages.error;
var object = messages.object; var object = messages.object;
var WIF = bitmessage.WIF; var WIF = bitmessage.WIF;
var POW = bitmessage.POW; var POW = bitmessage.POW;
@ -354,6 +355,29 @@ describe("Message types", function() {
}); });
}); });
describe("error", function() {
it("should encode and decode", function() {
var res = error.decode(error.encode({errorText: "test"}));
expect(res.fatal).to.equal(0);
expect(res.banTime).to.equal(0);
expect(res.vector).to.equal("");
expect(res.errorText).to.equal("test");
expect(res.length).to.equal(8);
var res = error.decode(error.encode({
fatal: error.FATAL,
banTime: 120,
vector: "123",
errorText: "fatal error",
}));
expect(res.fatal).to.equal(2);
expect(res.banTime).to.equal(120);
expect(res.vector).to.equal("123");
expect(res.errorText).to.equal("fatal error");
expect(res.length).to.equal(18);
});
});
describe("object", function() { describe("object", function() {
it("should encode and decode", function() { it("should encode and decode", function() {
var nonce = Buffer(8); var nonce = Buffer(8);