Skip to content

Commit 761c4a3

Browse files
committed
test: add normalizeToV1 coverage and fix unused import
- Remove unused LedgerTransactionExpanded import (fixes TS6133 build error) - Add test for v2 wrapped -> v1 flat normalization via hashLedger - Add test verifying hashTxTree matches expected transaction_hash - Validates the core v2-to-v1 conversion logic end-to-end - Addresses pdp2121 inline review request on normalizeToV1
1 parent ad60ecc commit 761c4a3

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

packages/xrpl/src/utils/hashes/hashLedger.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { APIVersion } from '../../models'
1212
import { LedgerEntry } from '../../models/ledger'
1313
import {
1414
LedgerVersionMap,
15-
LedgerTransactionExpanded,
1615
LedgerTransactionExpandedV1,
1716
LedgerTransactionExpandedV2,
1817
} from '../../models/ledger/Ledger'

packages/xrpl/test/utils/hashLedger.test.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { hashes } from '../../src/utils'
55
import requests from '../fixtures/requests'
66
import responses from '../fixtures/responses'
77

8-
const { hashLedger } = hashes
8+
const { hashLedger, hashTxTree } = hashes
99
const { hashLedger: REQUEST_FIXTURES } = requests
1010

1111
describe('hashLedger', function () {
@@ -150,4 +150,49 @@ describe('hashLedger', function () {
150150
'transactionHash in header does not match computed hash of transactions',
151151
)
152152
})
153+
154+
it('hashLedger - normalizes v2 wrapped transactions to v1 for hashing', function () {
155+
// Convert the v1 flat transactions to v2 wrapped format, then verify
156+
// hashLedger still computes the correct transaction_hash by normalizing
157+
// them back to v1 internally.
158+
const v1Ledger = JSON.parse(JSON.stringify(ledger))
159+
v1Ledger.parent_close_time = v1Ledger.close_time
160+
161+
// Wrap each v1 flat tx into v2 format: { tx_json, meta, hash, ... }
162+
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- test conversion
163+
const v2Transactions = v1Ledger.transactions.map((tx: any) => {
164+
const { hash, metaData, ledger_index: _li, ...txFields } = tx
165+
return {
166+
tx_json: txFields,
167+
meta: metaData,
168+
hash,
169+
validated: true,
170+
ledger_index: v1Ledger.ledger_index,
171+
close_time_iso: '2013-Jan-02 03:21:10.000000000 UTC',
172+
ledger_hash: v1Ledger.ledger_hash,
173+
}
174+
})
175+
176+
const v2Ledger = {
177+
...v1Ledger,
178+
transactions: v2Transactions,
179+
}
180+
181+
// Should produce the same hash because normalizeToV1 converts v2 back
182+
const hash = hashLedger(v2Ledger, { computeTreeHashes: true })
183+
assert.strictEqual(
184+
hash,
185+
hashLedger(v1Ledger, { computeTreeHashes: true }),
186+
)
187+
})
188+
189+
it('hashLedger - hashTxTree matches expected transaction_hash', function () {
190+
// Direct test of hashTxTree against the known transaction_hash from fixtures
191+
const v1Ledger = JSON.parse(JSON.stringify(ledger))
192+
193+
assert.strictEqual(
194+
hashTxTree(v1Ledger.transactions),
195+
v1Ledger.transaction_hash,
196+
)
197+
})
153198
})

0 commit comments

Comments
 (0)