Use Buffer type for vector in error encode/decode

This commit is contained in:
Kagami Hiiragi 2015-02-11 17:53:49 +03:00
parent 3ca97f294c
commit 3f0b881a58
3 changed files with 26 additions and 13 deletions

View File

@ -282,6 +282,7 @@ var inv = exports.inv = {
*/ */
encodePayload: function(inventory) { encodePayload: function(inventory) {
assert(inventory.length <= 50000, "Too many inventory entires"); assert(inventory.length <= 50000, "Too many inventory entires");
// TODO(Kagami): Validate vectors length.
var bufs = [structs.var_int.encode(inventory.length)].concat(inventory); var bufs = [structs.var_int.encode(inventory.length)].concat(inventory);
return Buffer.concat(bufs); return Buffer.concat(bufs);
}, },
@ -378,18 +379,28 @@ var error = exports.error = {
assert(buf.length >= 4, "Buffer is too small"); assert(buf.length >= 4, "Buffer is too small");
var decodedFatal = structs.var_int.decode(buf); var decodedFatal = structs.var_int.decode(buf);
var decodedBanTime = structs.var_int.decode(decodedFatal.rest); 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 decodedVectorLength = structs.var_int.decode(decodedBanTime.rest);
// NOTE(Kagami): Inventory vector should be only 32-byte in size but
// currently we don't ensure it.
var vectorLength = decodedVectorLength.value;
var rest = decodedVectorLength.rest;
assert(rest.length >= vectorLength, "Buffer is too small");
var vector = new Buffer(vectorLength);
rest.copy(vector);
rest = rest.slice(vectorLength);
var decodedErrorText = structs.var_str.decode(rest);
var length = ( var length = (
decodedFatal.length + decodedFatal.length +
decodedBanTime.length + decodedBanTime.length +
decodedVector.length + decodedVectorLength.length + vectorLength +
decodedErrorText.length decodedErrorText.length
); );
return { return {
fatal: decodedFatal.value, fatal: decodedFatal.value,
banTime: decodedBanTime.value, banTime: decodedBanTime.value,
vector: decodedVector.str, vector: vector,
errorText: decodedErrorText.str, errorText: decodedErrorText.str,
// Real data length. // Real data length.
length: length, length: length,
@ -414,13 +425,14 @@ var error = exports.error = {
encodePayload: function(opts) { encodePayload: function(opts) {
var fatal = opts.fatal || error.WARNING; var fatal = opts.fatal || error.WARNING;
var banTime = opts.banTime || 0; var banTime = opts.banTime || 0;
var vector = opts.vector || ""; // TODO(Kagami): Validate vector length.
var errorText = opts.errorText || ""; var vector = opts.vector || new Buffer(0);
return Buffer.concat([ return Buffer.concat([
structs.var_int.encode(fatal), structs.var_int.encode(fatal),
structs.var_int.encode(banTime), structs.var_int.encode(banTime),
structs.var_str.encode(vector), structs.var_int.encode(vector.length),
structs.var_str.encode(errorText), vector,
structs.var_str.encode(opts.errorText),
]); ]);
}, },
}; };

View File

@ -821,7 +821,7 @@ exports.inv_vect = {
/** /**
* Encode inventory vector. * Encode inventory vector.
* @param {Buffer} buf - Payload to calculate the inventory vector for * @param {Buffer} buf - Payload to calculate the inventory vector for
* @return {Buffer} Encoded `inv_vect`. * @return {Buffer} A 32-byte encoded `inv_vect`.
*/ */
encode: function(buf) { encode: function(buf) {
return bmcrypto.sha512(bmcrypto.sha512(buf)).slice(0, 32); return bmcrypto.sha512(bmcrypto.sha512(buf)).slice(0, 32);

View File

@ -576,21 +576,22 @@ describe("Message types", function() {
var res = error.decode(encoded); var res = error.decode(encoded);
expect(res.fatal).to.equal(0); expect(res.fatal).to.equal(0);
expect(res.banTime).to.equal(0); expect(res.banTime).to.equal(0);
expect(res.vector).to.equal(""); expect(res.vector).to.have.length(0);
expect(res.errorText).to.equal("test"); expect(res.errorText).to.equal("test");
expect(res.length).to.equal(8); expect(res.length).to.equal(8);
var vector = inv_vect.encode(Buffer("test"));
var res = error.decode(error.encode({ var res = error.decode(error.encode({
fatal: error.FATAL, fatal: error.FATAL,
banTime: 120, banTime: 120,
vector: "123", vector: vector,
errorText: "fatal error", errorText: "fatal error",
})); }));
expect(res.fatal).to.equal(2); expect(res.fatal).to.equal(2);
expect(res.banTime).to.equal(120); expect(res.banTime).to.equal(120);
expect(res.vector).to.equal("123"); expect(bufferEqual(res.vector, vector)).to.be.true;
expect(res.errorText).to.equal("fatal error"); expect(res.errorText).to.equal("fatal error");
expect(res.length).to.equal(18); expect(res.length).to.equal(47);
}); });
}); });
}); });