From 63038d36035263767f4b7c2b36a25ca504698f2d Mon Sep 17 00:00:00 2001 From: jatchili Date: Mon, 12 Aug 2013 15:19:04 -0700 Subject: [PATCH] basic tests for serializedtypes.js --- src/js/ripple/serializedtypes.js | 261 ++++++++++++++++++++++++++++--- test/serializedtypes-test.js | 118 +++++++++++++- 2 files changed, 353 insertions(+), 26 deletions(-) diff --git a/src/js/ripple/serializedtypes.js b/src/js/ripple/serializedtypes.js index 9b14fc55..075ea8ac 100644 --- a/src/js/ripple/serializedtypes.js +++ b/src/js/ripple/serializedtypes.js @@ -66,7 +66,7 @@ SerializedType.serialize_varint = function (so, val) { }; -SerializedType.parse_varint = function (so) { +SerializedType.prototype.parse_varint = function (so) { var b1 = so.read(1)[0], b2, b3; if (b1 <= 192) { return b1; @@ -374,6 +374,7 @@ var STAccount = exports.Account = new SerializedType({ }, parse: function (so) { var len = this.parse_varint(so); + console.log("KKKKKKKKKKK",len); if (len !== 20) { throw new Error("Non-standard-length account ID"); } @@ -399,7 +400,7 @@ var STPathSet = exports.PathSet = new SerializedType({ for (var j = 0, l2 = val[i].length; j < l2; j++) { var entry = val[i][j]; - + //if (entry.hasOwnProperty("_value")) {entry = entry._value;} var type = 0; if (entry.account) type |= this.typeAccount; @@ -442,28 +443,37 @@ var STPathSet = exports.PathSet = new SerializedType({ while (true) { //TODO: try/catch this loop, and catch when we run out of data without reaching the end of the data structure. var tag_byte = so.read(1)[0]; //Now determine: is this an end, boundary, or entry-begin-tag? - - if (tag_byte == typeEnd) { //We're done. + //console.log("Tag byte:", tag_byte); + if (tag_byte == this.typeEnd) { //We're done. + //console.log("End."); if (current_path) { //close the current path, if there is one, path_list.push(current_path); } break; //and conclude. - } else if (tag_byte == typeBoundary { + } else if (tag_byte == this.typeBoundary) { + //console.log("Boundary"); if (current_path) { //close the current path, if there is one, path_list.push(current_path); } current_path = []; //and start a new one. } else { //It's an entry-begin tag. + //console.log("It's an entry-begin tag."); var entry = {}; - if (tag_byte & typeAccount) { - entry.account = STAccount.parse(so.read(20)) + if (tag_byte & this.typeAccount) { + //console.log("entry.account"); + /*var bta = so.read(20); + console.log("BTA:", bta);*/ + entry.account = STHash160.parse(so); } - if (tag_byte & typeCurrency) { - entry.currency = STCurrency.parse(so.read(20)) + if (tag_byte & this.typeCurrency) { + //console.log("entry.currency"); + entry.currency = STCurrency.parse(so) } - if (tag_byte & typeIssuer) { - entry.issuer = UInt160.parse(so.read(20)); //should know to use Base58? + if (tag_byte & this.typeIssuer) { + //console.log("entry.issuer"); + entry.issuer = STHash160.parse(so); //should know to use Base58? + //console.log("DONE WITH ISSUER!"); } if (entry.account || entry.currency || entry.issuer) { current_path.push(entry); @@ -477,34 +487,235 @@ var STPathSet = exports.PathSet = new SerializedType({ }); var STVector256 = exports.Vector256 = new SerializedType({ - serialize: function (so, val) { - // XXX - throw new Error("Serializing Vector256 not implemented"); + serialize: function (so, val) { //Assume val is an array of STHash256 objects. + var length_as_varint = SerializedType.serialize_varint(so, val.length); + for (var i = 0; i= 16) { + STInt8.serialize(so, type_bits) + } + if (field_bits >= 16) { + STInt8.serialize(so, field_bits) + } + var serialized_object_type = TYPES_MAP[type_bits]; + //do something with val[keys] and val[keys[i]]; + serialized_object_type.serialize(so, value); +} + + +//What should this helper function be attached to? +//Take the serialized object, figure out what type/field it is, and return the parsing of that. +function parse_whatever(so) { + var tag_byte = so.read(1)[0]; + var type_bits = tag_byte >> 4; + var field_bits = tag_byte & 0x0f; + var type; + var field_name; + if (type_bits === 0) { + type = TYPES_MAP[so.read(1)[0]]; + } else { + type = TYPES_MAP[type_bits]; + } + if (field_bits === 0) { + field_name = FIELDS_MAP[type_bits][so.read(1)[0]]; + } else { + field_name = FIELDS_MAP[type_bits][field_bits]; + } + if ("undefined" === typeof field_name) { + return; + } else { + return [field_name, type.parse(so)]; //key, value + } +} + + + var STObject = exports.Object = new SerializedType({ serialize: function (so, val) { - // XXX - throw new Error("Serializing Object not implemented"); + var keys = Object.keys(val); + for (var i=0; i= 16) { + STInt8.serialize(so, type_bits) + } + if (field_bits >= 16) { + STInt8.serialize(so, field_bits) + } + var serialized_object_type = TYPES_MAP[type_bits]; + //do something with val[keys] and val[keys[i]]; + serialized_object_type.serialize(so, val[keys[i]]); + */ + } + STInt8.serialize(so, 0xe1); //Object ending marker }, parse: function (so) { - // XXX - throw new Error("Parsing Object not implemented"); + var output = {}; + while (true) { + var key_and_value = parse_whatever(so); + if ("undefined" === typeof key_and_value) { //Careful: are there any legitimate cases where we'd get this? + break; + } else { + output[key_and_value[0]] = key_and_value[1]; + } + } + return output; } }); var STArray = exports.Array = new SerializedType({ - serialize: function (so, val) { - // XXX - throw new Error("Serializing Array not implemented"); + serialize: function (so, val) { + for (var i=0; i