mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-27 15:45:48 +00:00
Fix *U*Int64 parsing, add hardcore mode to ledger verifier.
This commit is contained in:
@@ -9,8 +9,17 @@ if (process.argc < 1) {
|
|||||||
var json = fs.readFileSync(process.argv[2], 'utf-8');
|
var json = fs.readFileSync(process.argv[2], 'utf-8');
|
||||||
var ledger = Ledger.from_json(JSON.parse(json));
|
var ledger = Ledger.from_json(JSON.parse(json));
|
||||||
|
|
||||||
|
// This will serialize each accountState object to binary and then back to json
|
||||||
|
// before finally serializing for hashing. This is mostly for verifying that
|
||||||
|
// ripple-libs binary codecs are working.
|
||||||
|
// Must obviously go after process.argv[2] used to specify ledger json
|
||||||
|
var hardcore = ~process.argv.indexOf('--hardcore')
|
||||||
|
|
||||||
console.log("Transaction hash in header: "+ledger.ledger_json.transaction_hash);
|
console.log("Transaction hash in header: "+ledger.ledger_json.transaction_hash);
|
||||||
console.log("Calculated transaction hash: "+ledger.calc_tx_hash().to_hex());
|
console.log("Calculated transaction hash: "+ledger.calc_tx_hash().to_hex());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
console.log("Account state hash in header: "+ledger.ledger_json.account_hash);
|
console.log("Account state hash in header: "+ledger.ledger_json.account_hash);
|
||||||
console.log("Calculated account state hash: "+ledger.calc_account_hash().to_hex());
|
hardcore && console.log("Standby, we are going hardcore!, this may take some time");
|
||||||
|
console.log("Calculated account state hash: "+ledger.calc_account_hash(hardcore).to_hex());
|
||||||
|
|||||||
@@ -37,12 +37,23 @@ Ledger.prototype.calc_tx_hash = function () {
|
|||||||
return tx_map.hash();
|
return tx_map.hash();
|
||||||
};
|
};
|
||||||
|
|
||||||
Ledger.prototype.calc_account_hash = function () {
|
Ledger.prototype.calc_account_hash = function (hardcore) {
|
||||||
var account_map = new SHAMap();
|
var account_map = new SHAMap();
|
||||||
|
|
||||||
this.ledger_json.accountState.forEach(function (le) {
|
this.ledger_json.accountState.forEach(function (le) {
|
||||||
var data = SerializedObject.from_json(le);
|
var data = SerializedObject.from_json(le);
|
||||||
|
|
||||||
|
if (hardcore) {
|
||||||
|
try {
|
||||||
|
var json = data.to_json();
|
||||||
|
data = SerializedObject.from_json(json);
|
||||||
|
} catch (e) {
|
||||||
|
console.log("erred on", le);
|
||||||
|
console.log("to_json() was", json);
|
||||||
|
console.log("e", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
account_map.add_item(le.index, data, SHAMapTreeNode.TYPE_ACCOUNT_STATE);
|
account_map.add_item(le.index, data, SHAMapTreeNode.TYPE_ACCOUNT_STATE);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -224,7 +224,10 @@ var STInt64 = exports.Int64 = new SerializedType({
|
|||||||
serialize_hex(so, hex, true); //noLength = true
|
serialize_hex(so, hex, true); //noLength = true
|
||||||
},
|
},
|
||||||
parse: function (so) {
|
parse: function (so) {
|
||||||
var result = new BigInteger(so.read(8), 256);
|
var bytes = so.read(8);
|
||||||
|
// We need to add a 0, so if the high bit is set it won't think it's a
|
||||||
|
// pessimistic numeric fraek. What doth lief?
|
||||||
|
var result = new BigInteger([0].concat(bytes), 256);
|
||||||
assert(result instanceof BigInteger);
|
assert(result instanceof BigInteger);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -282,6 +282,15 @@ describe('Serialized types', function() {
|
|||||||
types.Int64.serialize(so, 4294967295.5);
|
types.Int64.serialize(so, 4294967295.5);
|
||||||
assert.strictEqual(so.to_hex(), '00000000FFFFFFFF');
|
assert.strictEqual(so.to_hex(), '00000000FFFFFFFF');
|
||||||
});
|
});
|
||||||
|
it('Does not get confused when the high bit is set', function () {
|
||||||
|
var so = new SerializedObject();
|
||||||
|
types.Int64.serialize(so, "8B2386F26F8E232B");
|
||||||
|
assert.strictEqual(so.to_hex(), '8B2386F26F8E232B');
|
||||||
|
var so = new SerializedObject("8B2386F26F8E232B");
|
||||||
|
var num = types.Int64.parse(so);
|
||||||
|
// We get a positive number
|
||||||
|
assert.strictEqual(num.toString(16), '8b2386f26f8e232b');
|
||||||
|
});
|
||||||
it('Serialize "0123456789ABCDEF"', function () {
|
it('Serialize "0123456789ABCDEF"', function () {
|
||||||
var so = new SerializedObject();
|
var so = new SerializedObject();
|
||||||
types.Int64.serialize(so, '0123456789ABCDEF');
|
types.Int64.serialize(so, '0123456789ABCDEF');
|
||||||
|
|||||||
Reference in New Issue
Block a user