fix: strengthen TransactionList validation in fromBytes#589
Merged
Conversation
Signed-off-by: Rob Walworth <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds stricter structural validation to
Transaction.fromBytes()when deserializing aTransactionListcontaining multipleTransactionIdgroups. The changes enforce invariants that were previously unchecked: only chunked transaction types (TopicMessageSubmitTransaction,FileAppendTransaction) are permitted to have multiple groups, and for chunked types the declaredchunkInfo.totalmust match the actual group count with sequential chunk numbers.Key Changes:
TransactionListpayloads for all non-chunked transaction typeschunkInfo.totalmatches the actual number ofTransactionIdgroups forconsensusSubmitMessagechunkInfo.numbervalues are sequential (1, 2, 3…) across groupsChanges
Structural validation in
Transaction.fromBytes(Transaction.swift)Two new validation blocks are inserted after the existing
protoTransactionBodyEqualloop.Block 1 — Non-chunked type guard:
Only
consensusSubmitMessageandfileAppendlegitimately produce multipleTransactionIdgroups (one per chunk). All other transaction types must have exactly one group. ATransactionListwith multiple groups for any other type is now rejected with a descriptive error.Block 2 —
chunkInfoconsistency forconsensusSubmitMessage:For chunked HCS messages,
chunkInfo.totaldeclares the expected number of chunks. This is now validated against the actual group count, and each group'schunkInfo.numberis checked to be sequential starting at 1.Note: Swift's existing
protoTransactionBodyEqualalready validates cross-group body consistency for all types (comparing every signed transaction against the first), which covers the equivalent of C++'s Layer 2 normalization check. No change was needed there.Comment update (
TopicMessageSubmitTransaction.swift)Removed the stale comment on line 31 (
// note: no other SDK checks for correctness here... so let's not do it here either?) and replaced it with a reference to the new validation infromBytes.Tests (
ChunkedTransactionUnitTests.swift)Four new rejection tests plus retention of the existing round-trip regression test:
test_fromBytes_rejectMultiGroupNonChunkedCryptoTransferwith 2 groups → throwstest_fromBytes_rejectChunkCountMismatch_totalTooLowchunkInfo.total = 1→ throwstest_fromBytes_rejectChunkCountMismatch_totalTooHighchunkInfo.total = 3→ throwstest_fromBytes_rejectOutOfOrderChunkNumberschunkInfo.number→ throwstest_ToFromBytesTesting
swift test --filter ChunkedTransactionUnitTestsAll 5 tests pass, including the pre-existing round-trip test.
Files Changed Summary
Sources/Hiero/Transaction.swiftSources/Hiero/Topic/TopicMessageSubmitTransaction.swiftTests/HieroUnitTests/ChunkedTransactionUnitTests.swiftBreaking Changes
None. All public APIs remain unchanged. The new validation only affects
Transaction.fromBytes()when given a structurally inconsistentTransactionList. Legitimate multi-chunkTopicMessageSubmitTransactionandFileAppendTransactionpayloads continue to deserialize correctly.