Integrate ripple-hashes (#1039)

* Update TxParser and Mocha

* Convert ripple-hashes to TypeScript and add into ripple-lib along with unit tests

* Switch casing to camelCase

* Document intToVuc

* Convert Slots to numbers, add exception if number is outside 0-15

* Remove Shamap v2 support

* Improve naming
This commit is contained in:
Tyler Storm
2019-10-01 23:35:11 -07:00
committed by Elliot Lee
parent b6bddd3b0e
commit 14e6bf5ef9
14 changed files with 617 additions and 21 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

127
test/hashes-test.js Normal file
View File

@@ -0,0 +1,127 @@
/* eslint-disable max-len, valid-jsdoc */
'use strict';
var assert = require('assert');
var fs = require('fs');
var hashes = require('../src/common/hashes');
/**
* @param ledgerIndex {Number}
* Expects a corresponding ledger dump in $repo/test/fixtures/rippled folder
*/
function createLedgerTest(ledgerIndex) {
describe(String(ledgerIndex), function() {
var path = __dirname + '/fixtures/rippled/ledger-full-' + ledgerIndex + '.json';
var ledgerRaw = fs.readFileSync(path);
var ledgerJSON = JSON.parse(ledgerRaw);
var hasAccounts = Array.isArray(ledgerJSON.accountState)
&& ledgerJSON.accountState.length > 0;
if (hasAccounts) {
it('has account_hash of ' + ledgerJSON.account_hash, function() {
assert.equal(ledgerJSON.account_hash,
hashes.computeStateTreeHash(ledgerJSON.accountState, 1));
});
}
it('has transaction_hash of ' + ledgerJSON.transaction_hash, function() {
assert.equal(ledgerJSON.transaction_hash,
hashes.computeTransactionTreeHash(ledgerJSON.transactions, 1));
});
});
}
describe('Ledger', function() {
// This is the first recorded ledger with a non empty transaction set
createLedgerTest(38129);
// Because, why not.
createLedgerTest(40000);
// 1311 AffectedNodes, no accounts
createLedgerTest(7501326);
describe('calcAccountRootEntryHash', function() {
it('will calculate the AccountRoot entry hash for rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', function() {
var account = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
var expectedEntryHash = '2B6AC232AA4C4BE41BF49D2459FA4A0347E1B543A4C92FCEE0821C0201E2E9A8';
var actualEntryHash = hashes.computeAccountHash(account);
assert.equal(actualEntryHash, expectedEntryHash);
});
});
describe('calcRippleStateEntryHash', function() {
it('will calculate the RippleState entry hash for rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh and rB5TihdPbKgMrkFqrqUC3yLdE8hhv4BdeY in USD', function() {
var account1 = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
var account2 = 'rB5TihdPbKgMrkFqrqUC3yLdE8hhv4BdeY';
var currency = 'USD';
var expectedEntryHash = 'C683B5BB928F025F1E860D9D69D6C554C2202DE0D45877ADB3077DA4CB9E125C';
var actualEntryHash1 = hashes.computeTrustlineHash(
account1, account2, currency);
var actualEntryHash2 = hashes.computeTrustlineHash(
account2, account1, currency);
assert.equal(actualEntryHash1, expectedEntryHash);
assert.equal(actualEntryHash2, expectedEntryHash);
});
it('will calculate the RippleState entry hash for r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV and rUAMuQTfVhbfqUDuro7zzy4jj4Wq57MPTj in UAM', function() {
var account1 = 'r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV';
var account2 = 'rUAMuQTfVhbfqUDuro7zzy4jj4Wq57MPTj';
var currency = 'UAM';
var expectedEntryHash = 'AE9ADDC584358E5847ADFC971834E471436FC3E9DE6EA1773DF49F419DC0F65E';
var actualEntryHash1 = hashes.computeTrustlineHash(
account1, account2, currency);
var actualEntryHash2 = hashes.computeTrustlineHash(
account2, account1, currency);
assert.equal(actualEntryHash1, expectedEntryHash);
assert.equal(actualEntryHash2, expectedEntryHash);
});
});
describe('calcOfferEntryHash', function() {
it('will calculate the Offer entry hash for r32UufnaCGL82HubijgJGDmdE5hac7ZvLw, sequence 137', function() {
var account = 'r32UufnaCGL82HubijgJGDmdE5hac7ZvLw';
var sequence = 137;
var expectedEntryHash = '03F0AED09DEEE74CEF85CD57A0429D6113507CF759C597BABB4ADB752F734CE3';
var actualEntryHash = hashes.computeOrderHash(account, sequence);
assert.equal(actualEntryHash, expectedEntryHash);
});
});
describe('computeSignerListHash', function() {
it('will calculate the SignerList index for r32UufnaCGL82HubijgJGDmdE5hac7ZvLw', function() {
var account = 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh';
var expectedEntryHash = '778365D5180F5DF3016817D1F318527AD7410D83F8636CF48C43E8AF72AB49BF';
var actualEntryHash = hashes.computeSignerListHash(account);
assert.equal(actualEntryHash, expectedEntryHash);
});
});
describe('calcEscrowEntryHash', function() {
it('will calculate the Escrow entry hash for rDx69ebzbowuqztksVDmZXjizTd12BVr4x, sequence 84', function() {
var account = 'rDx69ebzbowuqztksVDmZXjizTd12BVr4x';
var sequence = 84;
var expectedEntryHash = '61E8E8ED53FA2CEBE192B23897071E9A75217BF5A410E9CB5B45AAB7AECA567A';
var actualEntryHash = hashes.computeEscrowHash(account, sequence);
assert.equal(actualEntryHash, expectedEntryHash);
});
});
describe('calcPaymentChannelEntryHash', function() {
it('will calculate the PaymentChannel entry hash for rDx69ebzbowuqztksVDmZXjizTd12BVr4x and rLFtVprxUEfsH54eCWKsZrEQzMDsx1wqso, sequence 82', function() {
var account = 'rDx69ebzbowuqztksVDmZXjizTd12BVr4x';
var dstAccount = 'rLFtVprxUEfsH54eCWKsZrEQzMDsx1wqso'
var sequence = 82;
var expectedEntryHash = 'E35708503B3C3143FB522D749AAFCC296E8060F0FB371A9A56FAE0B1ED127366';
var actualEntryHash = hashes.computePaymentChannelHash(account, dstAccount, sequence);
assert.equal(actualEntryHash, expectedEntryHash);
});
});
});

71
test/shamap-test.js Normal file
View File

@@ -0,0 +1,71 @@
/* eslint-disable max-len, valid-jsdoc */
'use strict';
var assert = require('assert');
var SHAMap = require('../src/common/hashes/shamap').SHAMap;
var TYPE_TRANSACTION_NO_METADATA = require('../src/common/hashes/shamap').NodeType.TRANSACTION_NO_METADATA
var HEX_ZERO = '00000000000000000000000000000000' +
'00000000000000000000000000000000';
/**
* Generates data to hash for testing
* @param {number} v int value
* @returns {string} 64 length hex string
*/
function intToVuc(v) {
var ret = '';
for (var i = 0; i < 32; i++) {
ret += '0';
ret += v.toString(16).toUpperCase();
}
return ret;
}
/**
* @param shamap {Object}
* @param keys {Array}
* @param hashes {Array}
*/
function fillShamapTest(shamap, keys, hashes) {
for (var i = 0; i < keys.length; i++) {
var data = intToVuc(i);
shamap.addItem(keys[i].toUpperCase(), data, TYPE_TRANSACTION_NO_METADATA);
assert.equal(shamap.hash, hashes[i]);
}
}
describe('SHAMap', function() {
describe('#addItem', function() {
it('will add new nodes to v1', function() {
var keys = [
'b92891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8',
'b92881fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8',
'b92691fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8',
'b92791fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8',
'b91891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8',
'b99891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8',
'f22891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8',
'292891fe4ef6cee585fdc6fda1e09eb4d386363158ec3321b8123e5a772c6ca8'
];
var hashesv1 = [
'B7387CFEA0465759ADC718E8C42B52D2309D179B326E239EB5075C64B6281F7F',
'FBC195A9592A54AB44010274163CB6BA95F497EC5BA0A8831845467FB2ECE266',
'4E7D2684B65DFD48937FFB775E20175C43AF0C94066F7D5679F51AE756795B75',
'7A2F312EB203695FFD164E038E281839EEF06A1B99BFC263F3CECC6C74F93E07',
'395A6691A372387A703FB0F2C6D2C405DAF307D0817F8F0E207596462B0E3A3E',
'D044C0A696DE3169CC70AE216A1564D69DE96582865796142CE7D98A84D9DDE4',
'76DCC77C4027309B5A91AD164083264D70B77B5E43E08AEDA5EBF94361143615',
'DF4220E93ADC6F5569063A01B4DC79F8DB9553B6A3222ADE23DEA02BBE7230E5'
];
var shamapv1 = new SHAMap();
assert.equal(shamapv1.hash, HEX_ZERO);
fillShamapTest(shamapv1, keys, hashesv1);
});
});
});