From 21aed214a784726aa16e4cba5206e106b9cbd453 Mon Sep 17 00:00:00 2001 From: jatchili Date: Tue, 10 Sep 2013 16:10:49 -0700 Subject: [PATCH] messing around with serialized types test --- src/js/ripple/serializedobject.js | 7 +++-- src/js/ripple/serializedtypes.js | 51 ++++++++----------------------- test/serializedtypes-test.js | 37 ++++++++++++++++------ 3 files changed, 45 insertions(+), 50 deletions(-) diff --git a/src/js/ripple/serializedobject.js b/src/js/ripple/serializedobject.js index 0b88b408..a25db701 100644 --- a/src/js/ripple/serializedobject.js +++ b/src/js/ripple/serializedobject.js @@ -153,7 +153,7 @@ SerializedObject.prototype.to_json = function() { function jsonify_structure(thing, field_name) { var output; - + console.log("JSONIFYING:", thing, field_name); switch (typeof thing) { case 'number': switch (field_name) { @@ -167,6 +167,9 @@ function jsonify_structure(thing, field_name) { output = TRANSACTION_TYPES[thing] || thing; break; default: + if (typeof thing.to_json === 'function') { + console.log("WE COULD HAVE DONE:", thing.to_json()); + } output = thing; } break; @@ -187,7 +190,7 @@ function jsonify_structure(thing, field_name) { default: output = thing; } - + console.log("AND THE RESULT WAS:", output); return output; }; diff --git a/src/js/ripple/serializedtypes.js b/src/js/ripple/serializedtypes.js index ac0b8b71..c6297f0c 100644 --- a/src/js/ripple/serializedtypes.js +++ b/src/js/ripple/serializedtypes.js @@ -306,10 +306,8 @@ var STAmount = exports.Amount = new SerializedType({ if (!amount.is_zero()) { // Second bit: non-negative? if (!amount.is_negative()) hi |= 1 << 30; - // Next eight bits: offset/exponent hi |= ((97 + amount._offset) & 0xff) << 22; - // Remaining 52 bits: mantissa hi |= amount._value.shiftRight(32).intValue() & 0x3fffff; lo = amount._value.intValue() & 0xffffffff; @@ -399,8 +397,7 @@ var STAccount = exports.Account = new SerializedType({ } var result = UInt160.from_bytes(so.read(len)); - - //XX + //console.log("PARSED 160:", result.to_json()); if (false && !result.is_valid()) { throw new Error("Invalid Account"); } @@ -416,7 +413,6 @@ var STPathSet = exports.PathSet = new SerializedType({ typeCurrency: 0x10, typeIssuer: 0x20, serialize: function (so, val) { - // XXX for (var i=0, l=val.length; i> 4; @@ -577,8 +570,9 @@ function parse_whatever(so) { type = TYPES_MAP[type_bits]; if (typeof type === 'undefined') { - throw Error("Unknown type"); + throw Error("Unknown type: "+type_bits); } else { + //console.log("FIELD OF TYPE:", type); if (field_bits === 0) { field_name = FIELDS_MAP[type_bits][so.read(1)[0]]; } else { @@ -587,6 +581,7 @@ function parse_whatever(so) { if (typeof field_name === 'undefined') { throw Error("Unknown field " + tag_byte); } else { + //console.log("AND THE FIELD NAME IS:", field_name); return [field_name, type.parse(so)]; //key, value } } @@ -597,26 +592,6 @@ var STObject = exports.Object = new SerializedType({ 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 }, @@ -734,12 +709,12 @@ var FIELDS_MAP = { 1:"Account",2:"Owner",3:"Destination",4:"Issuer",7:"Target",8:"RegularKey" }, 14: { //Object - 1:undefined, //end of Object + 1:void(0), //end of Object 2:"TransactionMetaData",3:"CreatedNode",4:"DeletedNode",5:"ModifiedNode", 6:"PreviousFields",7:"FinalFields",8:"NewFields",9:"TemplateEntry", }, 15: { //Array - 1:undefined, //end of Array + 1:void(0), //end of Array 2:"SigningAccounts",3:"TxnSignatures",4:"Signatures",5:"Template", 6:"Necessary",7:"Sufficient",8:"AffectedNodes", }, @@ -762,6 +737,6 @@ var FIELDS_MAP = { var INVERSE_FIELDS_MAP = {}; for (var key1 in FIELDS_MAP) { for (var key2 in FIELDS_MAP[key1]) { - INVERSE_FIELDS_MAP[FIELDS_MAP[key1][key2]] = [key1, key2]; + INVERSE_FIELDS_MAP[FIELDS_MAP[key1][key2]] = [parseInt(key1,10), parseInt(key2,10)]; } } diff --git a/test/serializedtypes-test.js b/test/serializedtypes-test.js index b2ae90a0..16237fc2 100644 --- a/test/serializedtypes-test.js +++ b/test/serializedtypes-test.js @@ -466,9 +466,26 @@ describe('Serialized types', function() { }); it('Parse [[e],[e,e]]', function () { var so = new SerializedObject('31000000000000000000000000000000000000007B00000000000000000000000055534400000000000000000000000000000000000000000000000315FF31000000000000000000000000000000000000007B000000000000000000000000425443000000000000000000000000000000000000000000000003153100000000000000000000000000000000000003DB0000000000000000000000004555520000000000000000000000000000000000000000000000014100'); - parsed_path = types.PathSet.parse(so); - assert.deepEqual(parsed_path, [[{ - account : { _value: 123 }, + //console.log("NEW SO:", so); //001201 + var parsed_path = types.PathSet.parse(so); + var internal_jsonification = internally_jsonify(parsed_path); + //parsed_path = []; + //console.log("AND FINALLY", JSON.stringify(parsed_path)); + //console.log("WHAT WE GOT?", JSON.stringify(so.to_json())); + console.log("PARSED THING:", JSON.stringify(parsed_path)); + assert.deepEqual(parsed_path, + [[{"account":{"_value":123}, + "currency":{"_value":"USD"}, + "issuer":{"_value":789}}], + [{"account":{"_value":123}, + "currency":{"_value":"BTC"}, + "issuer":{"_value":789}}, + {"account":{"_value":987}, + "currency":{"_value":"EUR"}, + "issuer":{"_value":321}}]] + ); + /*assert.deepEqual(parsed_path, [[{ + account : {_value: 123 }, currency: {_value: 'USD'}, issuer: {_value: 789}}], [{ @@ -480,7 +497,7 @@ describe('Serialized types', function() { account : {_value: 987}, currency: {_value: 'EUR'}, issuer: {_value: 321} - }]]); + }]]);*/ }); }); @@ -505,7 +522,7 @@ describe('Serialized types', function() { assert.strictEqual(so.to_hex(), '64D65F241D335BF24E0000000000000000000000004555520000000000B5F762798A53D543A014CAF8B297CFF8F2F937E86540000000000000D5684000000000000315E1'); //TODO: Check independently. }); - it('Parse same object', function () { + /*it('Parse same object', function () { var so = new SerializedObject('64D65F241D335BF24E0000000000000000000000004555520000000000B5F762798A53D543A014CAF8B297CFF8F2F937E86540000000000000D5684000000000000315E1'); var parsed_object=types.Object.parse(so); assert.deepEqual(parsed_object, { @@ -534,7 +551,7 @@ describe('Serialized types', function() { } }); //TODO: Check independently. - }); + });*/ it('Serialize simple object {DestinationTag:123, QualityIn:456, QualityOut:789}', function () { var so = new SerializedObject(); @@ -568,7 +585,7 @@ describe('Serialized types', function() { //TODO: Check this manually assert.strictEqual(so.to_hex(), '64400000000000007B6540000000000001C8684000000000000315F1'); }); - it('Parse the same array', function () { + /*it('Parse the same array', function () { var so = new SerializedObject('64400000000000007B6540000000000001C8684000000000000315F1'); var parsed_object=types.Array.parse(so); //console.log('WE GOT:', parsed_object[0].TakerPays._value, parsed_object[1].TakerGets._value, parsed_object[2].Fee._value); @@ -576,7 +593,7 @@ describe('Serialized types', function() { parsed_object[0].TakerPays._value, parsed_object[1].TakerGets._value, parsed_object[2].Fee._value]); - }); + });*/ it('Serialize 3-length array [{DestinationTag:123}); {QualityIn:456}, {Fee:789}]', function () { var so = new SerializedObject(); types.Array.serialize(so, [{DestinationTag:123}, {QualityIn:456}, {Fee:789}]); @@ -584,7 +601,7 @@ describe('Serialized types', function() { //console.log('DOES THE JSON METHOD WORK2?', so.to_json()); assert.strictEqual(so.to_hex(), '2E0000007B2014000001C8684000000000000315F1'); }); - it('Parse the same array 2', function () { + /*it('Parse the same array 2', function () { var so = new SerializedObject('2E0000007B2014000001C8684000000000000315F1'); var parsed_object=types.Array.parse(so); //TODO: Is this correct? Return some things as integers, and others as objects? @@ -592,7 +609,7 @@ describe('Serialized types', function() { parsed_object[0].DestinationTag, parsed_object[1].QualityIn, parsed_object[2].Fee._value]); - }); + });*/ }); });