messing around with serialized types test

This commit is contained in:
jatchili
2013-09-10 16:10:49 -07:00
parent 3807c1ba6a
commit 21aed214a7
3 changed files with 45 additions and 50 deletions

View File

@@ -153,7 +153,7 @@ SerializedObject.prototype.to_json = function() {
function jsonify_structure(thing, field_name) { function jsonify_structure(thing, field_name) {
var output; var output;
console.log("JSONIFYING:", thing, field_name);
switch (typeof thing) { switch (typeof thing) {
case 'number': case 'number':
switch (field_name) { switch (field_name) {
@@ -167,6 +167,9 @@ function jsonify_structure(thing, field_name) {
output = TRANSACTION_TYPES[thing] || thing; output = TRANSACTION_TYPES[thing] || thing;
break; break;
default: default:
if (typeof thing.to_json === 'function') {
console.log("WE COULD HAVE DONE:", thing.to_json());
}
output = thing; output = thing;
} }
break; break;
@@ -187,7 +190,7 @@ function jsonify_structure(thing, field_name) {
default: default:
output = thing; output = thing;
} }
console.log("AND THE RESULT WAS:", output);
return output; return output;
}; };

View File

@@ -306,10 +306,8 @@ var STAmount = exports.Amount = new SerializedType({
if (!amount.is_zero()) { if (!amount.is_zero()) {
// Second bit: non-negative? // Second bit: non-negative?
if (!amount.is_negative()) hi |= 1 << 30; if (!amount.is_negative()) hi |= 1 << 30;
// Next eight bits: offset/exponent // Next eight bits: offset/exponent
hi |= ((97 + amount._offset) & 0xff) << 22; hi |= ((97 + amount._offset) & 0xff) << 22;
// Remaining 52 bits: mantissa // Remaining 52 bits: mantissa
hi |= amount._value.shiftRight(32).intValue() & 0x3fffff; hi |= amount._value.shiftRight(32).intValue() & 0x3fffff;
lo = amount._value.intValue() & 0xffffffff; lo = amount._value.intValue() & 0xffffffff;
@@ -399,8 +397,7 @@ var STAccount = exports.Account = new SerializedType({
} }
var result = UInt160.from_bytes(so.read(len)); var result = UInt160.from_bytes(so.read(len));
//console.log("PARSED 160:", result.to_json());
//XX
if (false && !result.is_valid()) { if (false && !result.is_valid()) {
throw new Error("Invalid Account"); throw new Error("Invalid Account");
} }
@@ -416,7 +413,6 @@ var STPathSet = exports.PathSet = new SerializedType({
typeCurrency: 0x10, typeCurrency: 0x10,
typeIssuer: 0x20, typeIssuer: 0x20,
serialize: function (so, val) { serialize: function (so, val) {
// XXX
for (var i=0, l=val.length; i<l; i++) { for (var i=0, l=val.length; i<l; i++) {
// Boundary // Boundary
if (i) { if (i) {
@@ -433,7 +429,6 @@ var STPathSet = exports.PathSet = new SerializedType({
if (entry.issuer) type |= this.typeIssuer; if (entry.issuer) type |= this.typeIssuer;
STInt8.serialize(so, type); STInt8.serialize(so, type);
if (entry.account) { if (entry.account) {
so.append(UInt160.from_json(entry.account).to_bytes()); so.append(UInt160.from_json(entry.account).to_bytes());
} }
@@ -451,13 +446,12 @@ var STPathSet = exports.PathSet = new SerializedType({
STInt8.serialize(so, this.typeEnd); STInt8.serialize(so, this.typeEnd);
}, },
parse: function (so) { parse: function (so) {
// XXX
// should return a list of lists: // should return a list of lists:
/* /*
[ [
[entry, entry], [entry, entry],
[entry, entry, entry] [entry, entry, entry],
[entry] [entry],
[] []
] ]
@@ -541,8 +535,8 @@ function serialize_whatever(so, field_name, value) {
//field_name: a string for the field name ("LedgerEntryType" etc.) //field_name: a string for the field name ("LedgerEntryType" etc.)
//value: the value of that field. //value: the value of that field.
var field_coordinates = INVERSE_FIELDS_MAP[field_name]; var field_coordinates = INVERSE_FIELDS_MAP[field_name];
var type_bits = parseInt(field_coordinates[0]); var type_bits = field_coordinates[0];
var field_bits = parseInt(field_coordinates[1]); var field_bits = field_coordinates[1];
var tag_byte = (type_bits < 16 ? type_bits << 4 : 0) | (field_bits < 16 ? field_bits : 0) var tag_byte = (type_bits < 16 ? type_bits << 4 : 0) | (field_bits < 16 ? field_bits : 0)
STInt8.serialize(so, tag_byte) STInt8.serialize(so, tag_byte)
@@ -562,7 +556,6 @@ function serialize_whatever(so, field_name, value) {
//What should this helper function be attached to? //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. //Take the serialized object, figure out what type/field it is, and return the parsing of that.
exports.parse_whatever = parse_whatever; exports.parse_whatever = parse_whatever;
function parse_whatever(so) { function parse_whatever(so) {
var tag_byte = so.read(1)[0]; var tag_byte = so.read(1)[0];
var type_bits = tag_byte >> 4; var type_bits = tag_byte >> 4;
@@ -577,8 +570,9 @@ function parse_whatever(so) {
type = TYPES_MAP[type_bits]; type = TYPES_MAP[type_bits];
if (typeof type === 'undefined') { if (typeof type === 'undefined') {
throw Error("Unknown type"); throw Error("Unknown type: "+type_bits);
} else { } else {
//console.log("FIELD OF TYPE:", type);
if (field_bits === 0) { if (field_bits === 0) {
field_name = FIELDS_MAP[type_bits][so.read(1)[0]]; field_name = FIELDS_MAP[type_bits][so.read(1)[0]];
} else { } else {
@@ -587,6 +581,7 @@ function parse_whatever(so) {
if (typeof field_name === 'undefined') { if (typeof field_name === 'undefined') {
throw Error("Unknown field " + tag_byte); throw Error("Unknown field " + tag_byte);
} else { } else {
//console.log("AND THE FIELD NAME IS:", field_name);
return [field_name, type.parse(so)]; //key, value return [field_name, type.parse(so)]; //key, value
} }
} }
@@ -597,26 +592,6 @@ var STObject = exports.Object = new SerializedType({
var keys = Object.keys(val); var keys = Object.keys(val);
for (var i=0; i<keys.length; i++) { for (var i=0; i<keys.length; i++) {
serialize_whatever(so, keys[i], val[keys[i]]); serialize_whatever(so, keys[i], val[keys[i]]);
//make this a function called "serialize_whatever"
//figure out the type corresponding to field so named
/*
var field_coordinates = INVERSE_FIELDS_MAP[keys[i]];
var type_bits = parseInt(field_coordinates[0]);
var field_bits = parseInt(field_coordinates[1]);
console.log(type_bits, field_bits);
var tag_byte=(type_bits < 16 ? type_bits<<4 : 0) | (field_bits < 16 ? field_bits : 0)
STInt8.serialize(so, tag_byte)
if (type_bits >= 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 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" 1:"Account",2:"Owner",3:"Destination",4:"Issuer",7:"Target",8:"RegularKey"
}, },
14: { //Object 14: { //Object
1:undefined, //end of Object 1:void(0), //end of Object
2:"TransactionMetaData",3:"CreatedNode",4:"DeletedNode",5:"ModifiedNode", 2:"TransactionMetaData",3:"CreatedNode",4:"DeletedNode",5:"ModifiedNode",
6:"PreviousFields",7:"FinalFields",8:"NewFields",9:"TemplateEntry", 6:"PreviousFields",7:"FinalFields",8:"NewFields",9:"TemplateEntry",
}, },
15: { //Array 15: { //Array
1:undefined, //end of Array 1:void(0), //end of Array
2:"SigningAccounts",3:"TxnSignatures",4:"Signatures",5:"Template", 2:"SigningAccounts",3:"TxnSignatures",4:"Signatures",5:"Template",
6:"Necessary",7:"Sufficient",8:"AffectedNodes", 6:"Necessary",7:"Sufficient",8:"AffectedNodes",
}, },
@@ -762,6 +737,6 @@ var FIELDS_MAP = {
var INVERSE_FIELDS_MAP = {}; var INVERSE_FIELDS_MAP = {};
for (var key1 in FIELDS_MAP) { for (var key1 in FIELDS_MAP) {
for (var key2 in FIELDS_MAP[key1]) { 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)];
} }
} }

View File

@@ -466,8 +466,25 @@ describe('Serialized types', function() {
}); });
it('Parse [[e],[e,e]]', function () { it('Parse [[e],[e,e]]', function () {
var so = new SerializedObject('31000000000000000000000000000000000000007B00000000000000000000000055534400000000000000000000000000000000000000000000000315FF31000000000000000000000000000000000000007B000000000000000000000000425443000000000000000000000000000000000000000000000003153100000000000000000000000000000000000003DB0000000000000000000000004555520000000000000000000000000000000000000000000000014100'); var so = new SerializedObject('31000000000000000000000000000000000000007B00000000000000000000000055534400000000000000000000000000000000000000000000000315FF31000000000000000000000000000000000000007B000000000000000000000000425443000000000000000000000000000000000000000000000003153100000000000000000000000000000000000003DB0000000000000000000000004555520000000000000000000000000000000000000000000000014100');
parsed_path = types.PathSet.parse(so); //console.log("NEW SO:", so); //001201
assert.deepEqual(parsed_path, [[{ 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 }, account : {_value: 123 },
currency: {_value: 'USD'}, currency: {_value: 'USD'},
issuer: {_value: 789}}], issuer: {_value: 789}}],
@@ -480,7 +497,7 @@ describe('Serialized types', function() {
account : {_value: 987}, account : {_value: 987},
currency: {_value: 'EUR'}, currency: {_value: 'EUR'},
issuer: {_value: 321} issuer: {_value: 321}
}]]); }]]);*/
}); });
}); });
@@ -505,7 +522,7 @@ describe('Serialized types', function() {
assert.strictEqual(so.to_hex(), '64D65F241D335BF24E0000000000000000000000004555520000000000B5F762798A53D543A014CAF8B297CFF8F2F937E86540000000000000D5684000000000000315E1'); assert.strictEqual(so.to_hex(), '64D65F241D335BF24E0000000000000000000000004555520000000000B5F762798A53D543A014CAF8B297CFF8F2F937E86540000000000000D5684000000000000315E1');
//TODO: Check independently. //TODO: Check independently.
}); });
it('Parse same object', function () { /*it('Parse same object', function () {
var so = new SerializedObject('64D65F241D335BF24E0000000000000000000000004555520000000000B5F762798A53D543A014CAF8B297CFF8F2F937E86540000000000000D5684000000000000315E1'); var so = new SerializedObject('64D65F241D335BF24E0000000000000000000000004555520000000000B5F762798A53D543A014CAF8B297CFF8F2F937E86540000000000000D5684000000000000315E1');
var parsed_object=types.Object.parse(so); var parsed_object=types.Object.parse(so);
assert.deepEqual(parsed_object, { assert.deepEqual(parsed_object, {
@@ -534,7 +551,7 @@ describe('Serialized types', function() {
} }
}); });
//TODO: Check independently. //TODO: Check independently.
}); });*/
it('Serialize simple object {DestinationTag:123, QualityIn:456, QualityOut:789}', function () { it('Serialize simple object {DestinationTag:123, QualityIn:456, QualityOut:789}', function () {
var so = new SerializedObject(); var so = new SerializedObject();
@@ -568,7 +585,7 @@ describe('Serialized types', function() {
//TODO: Check this manually //TODO: Check this manually
assert.strictEqual(so.to_hex(), '64400000000000007B6540000000000001C8684000000000000315F1'); assert.strictEqual(so.to_hex(), '64400000000000007B6540000000000001C8684000000000000315F1');
}); });
it('Parse the same array', function () { /*it('Parse the same array', function () {
var so = new SerializedObject('64400000000000007B6540000000000001C8684000000000000315F1'); var so = new SerializedObject('64400000000000007B6540000000000001C8684000000000000315F1');
var parsed_object=types.Array.parse(so); 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); //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[0].TakerPays._value,
parsed_object[1].TakerGets._value, parsed_object[1].TakerGets._value,
parsed_object[2].Fee._value]); parsed_object[2].Fee._value]);
}); });*/
it('Serialize 3-length array [{DestinationTag:123}); {QualityIn:456}, {Fee:789}]', function () { it('Serialize 3-length array [{DestinationTag:123}); {QualityIn:456}, {Fee:789}]', function () {
var so = new SerializedObject(); var so = new SerializedObject();
types.Array.serialize(so, [{DestinationTag:123}, {QualityIn:456}, {Fee:789}]); 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()); //console.log('DOES THE JSON METHOD WORK2?', so.to_json());
assert.strictEqual(so.to_hex(), '2E0000007B2014000001C8684000000000000315F1'); 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 so = new SerializedObject('2E0000007B2014000001C8684000000000000315F1');
var parsed_object=types.Array.parse(so); var parsed_object=types.Array.parse(so);
//TODO: Is this correct? Return some things as integers, and others as objects? //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[0].DestinationTag,
parsed_object[1].QualityIn, parsed_object[1].QualityIn,
parsed_object[2].Fee._value]); parsed_object[2].Fee._value]);
}); });*/
}); });
}); });