Lint utils directory (#1563)

* Lint the utils directory

* Modify computeLedgerHash to take new Ledger format.
This commit is contained in:
Nathan Nichols
2021-09-10 12:32:38 -07:00
committed by Mayukha Vadari
parent 33f83947f1
commit 633032ddd8
29 changed files with 921 additions and 883 deletions

View File

@@ -1,34 +1,45 @@
import { assert } from 'chai'
import { ValidationError } from '../../src/common/errors'
import { computeLedgerHeaderHash } from '../../src/utils'
import { computeLedgerHash } from '../../src/utils'
import requests from '../fixtures/requests'
import responses from '../fixtures/responses'
import { assertResultMatch } from '../testUtils'
const { computeLedgerHash: REQUEST_FIXTURES } = requests
describe('computeLedgerHash', function () {
let ledger
beforeEach(function () {
ledger = JSON.parse(JSON.stringify(responses.getLedger.full))
if (ledger.rawState != null) {
ledger.accountState = JSON.parse(ledger.rawState)
}
})
it('given corrupt data - should fail', function () {
const ledger = JSON.parse(JSON.stringify(responses.getLedger.full))
ledger.transactions[0].rawTransaction =
'{"Account":"r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV","Amount":"12000000000","Destination":"rLQBHVhFnaC5gLEkgr6HgBJJ3bgeZHg9cj","Fee":"10","Flags":0,"Sequence":62,"SigningPubKey":"034AADB09CFF4A4804073701EC53C3510CDC95917C2BB0150FB742D0C66E6CEE9E","TransactionType":"Payment","TxnSignature":"3045022022EB32AECEF7C644C891C19F87966DF9C62B1F34BABA6BE774325E4BB8E2DD62022100A51437898C28C2B297112DF8131F2BB39EA5FE613487DDD611525F1796264639","hash":"3B1A4E1C9BB6A7208EB146BCDB86ECEA6068ED01466D933528CA2B4C64F753EF","meta":{"AffectedNodes":[{"CreatedNode":{"LedgerEntryType":"AccountRoot","LedgerIndex":"4C6ACBD635B0F07101F7FA25871B0925F8836155462152172755845CE691C49E","NewFields":{"Account":"rLQBHVhFnaC5gLEkgr6HgBJJ3bgeZHg9cj","Balance":"10000000000","Sequence":1}}},{"ModifiedNode":{"FinalFields":{"Account":"r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV","Balance":"981481999380","Flags":0,"OwnerCount":0,"Sequence":63},"LedgerEntryType":"AccountRoot","LedgerIndex":"B33FDD5CF3445E1A7F2BE9B06336BEBD73A5E3EE885D3EF93F7E3E2992E46F1A","PreviousFields":{"Balance":"991481999390","Sequence":62},"PreviousTxnID":"2485FDC606352F1B0785DA5DE96FB9DBAF43EB60ECBB01B7F6FA970F512CDA5F","PreviousTxnLgrSeq":31317}}],"TransactionIndex":0,"TransactionResult":"tesSUCCESS"},"ledger_index":38129}'
ledger.parentCloseTime = ledger.closeTime
let hash: string
ledger.transactions[0] = JSON.parse(
'{"Account":"r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV","Amount":"12000000000","Destination":"rLQBHVhFnaC5gLEkgr6HgBJJ3bgeZHg9cj","Fee":"10","Flags":0,"Sequence":62,"SigningPubKey":"034AADB09CFF4A4804073701EC53C3510CDC95917C2BB0150FB742D0C66E6CEE9E","TransactionType":"Payment","TxnSignature":"3045022022EB32AECEF7C644C891C19F87966DF9C62B1F34BABA6BE774325E4BB8E2DD62022100A51437898C28C2B297112DF8131F2BB39EA5FE613487DDD611525F1796264639","hash":"3B1A4E1C9BB6A7208EB146BCDB86ECEA6068ED01466D933528CA2B4C64F753EF","metaData":{"AffectedNodes":[{"CreatedNode":{"LedgerEntryType":"AccountRoot","LedgerIndex":"4C6ACBD635B0F07101F7FA25871B0925F8836155462152172755845CE691C49E","NewFields":{"Account":"rLQBHVhFnaC5gLEkgr6HgBJJ3bgeZHg9cj","Balance":"10000000000","Sequence":1}}},{"ModifiedNode":{"FinalFields":{"Account":"r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV","Balance":"981481999380","Flags":0,"OwnerCount":0,"Sequence":63},"LedgerEntryType":"AccountRoot","LedgerIndex":"B33FDD5CF3445E1A7F2BE9B06336BEBD73A5E3EE885D3EF93F7E3E2992E46F1A","PreviousFields":{"Balance":"991481999390","Sequence":62},"PreviousTxnID":"2485FDC606352F1B0785DA5DE96FB9DBAF43EB60ECBB01B7F6FA970F512CDA5F","PreviousTxnLgrSeq":31317}}],"TransactionIndex":0,"TransactionResult":"tesSUCCESS"},"ledger_index":38129}',
)
ledger.parent_close_time = ledger.close_time
let hash
try {
hash = computeLedgerHeaderHash(ledger, { computeTreeHashes: true })
hash = computeLedgerHash(ledger, { computeTreeHashes: true })
} catch (error) {
assert(error instanceof ValidationError)
assert.strictEqual(
error.message,
'transactionHash in header does not match computed hash of transactions',
)
assert.deepStrictEqual(error.data, {
transactionHashInHeader:
'DB83BF807416C5B3499A73130F843CF615AB8E797D79FE7D330ADF1BFA93951A',
computedHashOfTransactions:
'EAA1ADF4D627339450F0E95EA88B7069186DD64230BAEBDCF3EEC4D616A9FC68',
})
if (error instanceof ValidationError) {
assert.strictEqual(
error.message,
'transactionHash in header does not match computed hash of transactions',
)
assert.deepStrictEqual(error.data, {
transactionHashInHeader:
'DB83BF807416C5B3499A73130F843CF615AB8E797D79FE7D330ADF1BFA93951A',
computedHashOfTransactions:
'EAA1ADF4D627339450F0E95EA88B7069186DD64230BAEBDCF3EEC4D616A9FC68',
})
}
return
}
assert(
@@ -38,77 +49,68 @@ describe('computeLedgerHash', function () {
})
it('given ledger without raw transactions - should throw', function () {
const ledger = JSON.parse(JSON.stringify(responses.getLedger.full))
delete ledger.transactions[0].rawTransaction
delete ledger.transactions
ledger.parentCloseTime = ledger.closeTime
let hash: string
try {
hash = computeLedgerHeaderHash(ledger, { computeTreeHashes: true })
} catch (error) {
assert(error instanceof ValidationError)
assert.strictEqual(error.message, 'ledger is missing raw transactions')
return
}
assert(
false,
`Should throw ValidationError instead of producing hash: ${hash}`,
assert.throws(
() => computeLedgerHash(ledger, { computeTreeHashes: true }),
ValidationError,
'transactions is missing from the ledger',
)
})
it('given ledger without state or transactions - only compute ledger hash', function () {
const ledger = JSON.parse(JSON.stringify(responses.getLedger.full))
assert.strictEqual(
ledger.transactions[0].rawTransaction,
'{"Account":"r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV","Amount":"10000000000","Destination":"rLQBHVhFnaC5gLEkgr6HgBJJ3bgeZHg9cj","Fee":"10","Flags":0,"Sequence":62,"SigningPubKey":"034AADB09CFF4A4804073701EC53C3510CDC95917C2BB0150FB742D0C66E6CEE9E","TransactionType":"Payment","TxnSignature":"3045022022EB32AECEF7C644C891C19F87966DF9C62B1F34BABA6BE774325E4BB8E2DD62022100A51437898C28C2B297112DF8131F2BB39EA5FE613487DDD611525F1796264639","hash":"3B1A4E1C9BB6A7208EB146BCDB86ECEA6068ED01466D933528CA2B4C64F753EF","meta":{"AffectedNodes":[{"CreatedNode":{"LedgerEntryType":"AccountRoot","LedgerIndex":"4C6ACBD635B0F07101F7FA25871B0925F8836155462152172755845CE691C49E","NewFields":{"Account":"rLQBHVhFnaC5gLEkgr6HgBJJ3bgeZHg9cj","Balance":"10000000000","Sequence":1}}},{"ModifiedNode":{"FinalFields":{"Account":"r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV","Balance":"981481999380","Flags":0,"OwnerCount":0,"Sequence":63},"LedgerEntryType":"AccountRoot","LedgerIndex":"B33FDD5CF3445E1A7F2BE9B06336BEBD73A5E3EE885D3EF93F7E3E2992E46F1A","PreviousFields":{"Balance":"991481999390","Sequence":62},"PreviousTxnID":"2485FDC606352F1B0785DA5DE96FB9DBAF43EB60ECBB01B7F6FA970F512CDA5F","PreviousTxnLgrSeq":31317}}],"TransactionIndex":0,"TransactionResult":"tesSUCCESS"},"ledger_index":38129}',
ledger.transactions[0] = JSON.parse(
'{"Account":"r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV","Amount":"10000000000","Destination":"rLQBHVhFnaC5gLEkgr6HgBJJ3bgeZHg9cj","Fee":"10","Flags":0,"Sequence":62,"SigningPubKey":"034AADB09CFF4A4804073701EC53C3510CDC95917C2BB0150FB742D0C66E6CEE9E","TransactionType":"Payment","TxnSignature":"3045022022EB32AECEF7C644C891C19F87966DF9C62B1F34BABA6BE774325E4BB8E2DD62022100A51437898C28C2B297112DF8131F2BB39EA5FE613487DDD611525F1796264639","hash":"3B1A4E1C9BB6A7208EB146BCDB86ECEA6068ED01466D933528CA2B4C64F753EF","metaData":{"AffectedNodes":[{"CreatedNode":{"LedgerEntryType":"AccountRoot","LedgerIndex":"4C6ACBD635B0F07101F7FA25871B0925F8836155462152172755845CE691C49E","NewFields":{"Account":"rLQBHVhFnaC5gLEkgr6HgBJJ3bgeZHg9cj","Balance":"10000000000","Sequence":1}}},{"ModifiedNode":{"FinalFields":{"Account":"r3kmLJN5D28dHuH8vZNUZpMC43pEHpaocV","Balance":"981481999380","Flags":0,"OwnerCount":0,"Sequence":63},"LedgerEntryType":"AccountRoot","LedgerIndex":"B33FDD5CF3445E1A7F2BE9B06336BEBD73A5E3EE885D3EF93F7E3E2992E46F1A","PreviousFields":{"Balance":"991481999390","Sequence":62},"PreviousTxnID":"2485FDC606352F1B0785DA5DE96FB9DBAF43EB60ECBB01B7F6FA970F512CDA5F","PreviousTxnLgrSeq":31317}}],"TransactionIndex":0,"TransactionResult":"tesSUCCESS"},"ledger_index":38129}',
)
ledger.parentCloseTime = ledger.closeTime
const computeLedgerHash = computeLedgerHeaderHash
function testCompute(ledgerToCompute, expectedError): void {
let hash = computeLedgerHash(ledgerToCompute)
ledger.parent_close_time = ledger.close_time
function testCompute(ledgerToTest, expectedError): void {
const hash = computeLedgerHash(ledgerToTest)
assert.strictEqual(
hash,
'E6DB7365949BF9814D76BCC730B01818EB9136A89DB224F3F9F5AAE4569D758E',
)
// fail if required to compute tree hashes
try {
hash = computeLedgerHash(ledgerToCompute, { computeTreeHashes: true })
} catch (error) {
assert(error instanceof ValidationError)
assert.strictEqual(error.message, expectedError)
return
}
assert(
false,
`Should throw ValidationError instead of producing hash: ${hash}`,
assert.throws(
() => computeLedgerHash(ledgerToTest, { computeTreeHashes: true }),
ValidationError,
expectedError,
)
}
const transactions = ledger.transactions
delete ledger.transactions
testCompute(ledger, 'transactions property is missing from the ledger')
delete ledger.rawState
testCompute(ledger, 'transactions property is missing from the ledger')
testCompute(ledger, 'transactions is missing from the ledger')
delete ledger.accountState
testCompute(ledger, 'transactions is missing from the ledger')
ledger.transactions = transactions
testCompute(ledger, 'rawState property is missing from the ledger')
testCompute(ledger, 'accountState is missing from the ledger')
})
it('wrong hash', function () {
const ledger = JSON.parse(JSON.stringify(responses.getLedger.full))
assertResultMatch(ledger, responses.getLedger.full, 'getLedger')
const newLedger = {
...ledger,
parentCloseTime: ledger.closeTime,
stateHash:
parent_close_time: ledger.close_time,
account_hash:
'D9ABF622DA26EEEE48203085D4BC23B0F77DC6F8724AC33D975DA3CA492D2E44',
}
assert.throws(() => {
computeLedgerHeaderHash(newLedger)
}, /does not match computed hash of state/u)
assert.throws(
() => {
computeLedgerHash(newLedger, { computeTreeHashes: true })
},
ValidationError,
'does not match computed hash of state',
)
})
it('computeLedgerHash', function () {
const header = REQUEST_FIXTURES.header
const ledgerHash = computeLedgerHeaderHash(header)
const ledgerHash = computeLedgerHash(header)
assert.strictEqual(
ledgerHash,
'F4D865D83EB88C1A1911B9E90641919A1314F36E1B099F8E95FE3B7C77BE3349',
@@ -119,21 +121,27 @@ describe('computeLedgerHash', function () {
const header = {
...REQUEST_FIXTURES.header,
transactionHash: undefined,
rawTransactions: JSON.stringify(REQUEST_FIXTURES.transactions),
rawTransactions: REQUEST_FIXTURES.transactions,
}
const ledgerHash = computeLedgerHeaderHash(header)
const ledgerHash = computeLedgerHash(header)
assert.strictEqual(
ledgerHash,
'F4D865D83EB88C1A1911B9E90641919A1314F36E1B099F8E95FE3B7C77BE3349',
)
})
it('computeLedgerHash - incorrent transaction_hash', function () {
it('computeLedgerHash - incorrect transaction_hash', function () {
const header = {
...REQUEST_FIXTURES.header,
transactionHash:
transaction_hash:
'325EACC5271322539EEEC2D6A5292471EF1B3E72AE7180533EFC3B8F0AD435C9',
transactions: REQUEST_FIXTURES.transactions as any,
}
assert.throws(() => computeLedgerHeaderHash(header))
assert.throws(
() => computeLedgerHash(header, { computeTreeHashes: true }),
ValidationError,
'transactionHash in header does not match computed hash of transactions',
)
})
})