-
Notifications
You must be signed in to change notification settings - Fork 27
feat: add SDK support for large transaction streaming #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
netbonus
wants to merge
1
commit into
dev
Choose a base branch
from
nb/bigtx
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| /** | ||
| * Large Transaction Streaming Tests | ||
| * | ||
| * Tests SDK support for streaming Keccak-256 hashing for large transactions (~102KB). | ||
| * Firmware v0.18.0+ enables streaming mode to handle transactions up to ~102KB vs the | ||
| * previous ~3KB limit. These tests verify SDK compatibility with the new streaming feature. | ||
| */ | ||
| import { beforeAll, describe, expect, it, vi } from 'vitest'; | ||
| import { TransactionSerializable } from 'viem'; | ||
| import { getClient, sign } from '../../../api'; | ||
| import { randomBytes } from '../../../util'; | ||
| import { setupClient } from '../../utils/setup'; | ||
| import { signAndCompareTransaction } from '../../utils/viemComparison'; | ||
|
|
||
| // Set a long timeout for these tests as they involve large data transfers and user interaction. | ||
| vi.setConfig({ testTimeout: 60000 }); | ||
|
|
||
| describe('[EVM] Large Transaction Streaming', () => { | ||
| beforeAll(async () => { | ||
| await setupClient(); | ||
| }); | ||
|
|
||
| /** | ||
| * Baseline: Ensures small transactions are unaffected by the new streaming logic. | ||
| */ | ||
| it('Small transaction baseline', async () => { | ||
| const tx: TransactionSerializable = { | ||
| to: '0xe242e54155b1abc71fc118065270cecaaf8b7768', | ||
| value: 1n, | ||
| data: '0x', | ||
| nonce: 0, | ||
| gas: 21_000n, | ||
| maxFeePerGas: 20_000_000_000n, | ||
| maxPriorityFeePerGas: 1_000_000_000n, | ||
| chainId: 1, | ||
| type: 'eip1559', | ||
| }; | ||
|
|
||
| await signAndCompareTransaction(tx, 'Small transaction baseline'); | ||
| }); | ||
|
|
||
| /** | ||
| * Edge case: Transaction just over the old limit (~3.1KB) triggers streaming mode. | ||
| * Verifies the firmware correctly transitions from buffering to streaming mode. | ||
| */ | ||
| it('Transaction at streaming threshold (~3.1KB)', async () => { | ||
| const dataSize = 3100; // Just over the old 1-frame limit | ||
| const edgeData = `0x${randomBytes(dataSize).toString('hex')}`; | ||
|
|
||
| const tx: TransactionSerializable = { | ||
| to: '0xe242e54155b1abc71fc118065270cecaaf8b7768', | ||
| value: 1n, | ||
| data: edgeData, | ||
| nonce: 1, | ||
| gas: 100_000n, | ||
| maxFeePerGas: 20_000_000_000n, | ||
| maxPriorityFeePerGas: 1_000_000_000n, | ||
| chainId: 1, | ||
| type: 'eip1559', | ||
| }; | ||
|
|
||
| await signAndCompareTransaction(tx, 'Streaming trigger edge case'); | ||
| }); | ||
|
|
||
| /** | ||
| * Maximum size: Transaction using the full ~102KB payload capacity. | ||
| * Verifies SDK can successfully build, send, and get a valid signature for max size. | ||
| */ | ||
| it('Maximum size transaction (~102KB)', async () => { | ||
| const client = await getClient(); | ||
| if (!client) throw new Error('Client not setup'); | ||
| const fwConstants = client.getFwConstants(); | ||
| const maxDataSz = | ||
| fwConstants.genericSigning.baseDataSz + | ||
| fwConstants.extraDataMaxFrames * fwConstants.extraDataFrameSz; | ||
|
|
||
| const largeData = `0x${randomBytes(maxDataSz).toString('hex')}`; | ||
|
|
||
| const tx: TransactionSerializable = { | ||
| to: '0xe242e54155b1abc71fc118065270cecaaf8b7768', | ||
| value: 1n, | ||
| data: largeData, | ||
| nonce: 2, | ||
| gas: 2_000_000n, // High gas limit for large data | ||
| maxFeePerGas: 20_000_000_000n, | ||
| maxPriorityFeePerGas: 1_000_000_000n, | ||
| chainId: 1, | ||
| type: 'eip1559', | ||
| }; | ||
|
|
||
| await signAndCompareTransaction(tx, 'Max size transaction'); | ||
| }); | ||
|
|
||
| /** | ||
| * Preemptive rejection: Transaction exceeding the 102KB limit. | ||
| * Ensures SDK validation rejects oversized transactions before sending to device. | ||
| */ | ||
| it('Reject transaction exceeding max size (>102KB)', async () => { | ||
| const client = await getClient(); | ||
| if (!client) throw new Error('Client not setup'); | ||
| const fwConstants = client.getFwConstants(); | ||
| const maxDataSz = | ||
| fwConstants.genericSigning.baseDataSz + | ||
| fwConstants.extraDataMaxFrames * fwConstants.extraDataFrameSz; | ||
| const oversizedData = `0x${randomBytes(maxDataSz + 1).toString('hex')}`; | ||
|
|
||
| const tx: TransactionSerializable = { | ||
| to: '0xe242e54155b1abc71fc118065270cecaaf8b7768', | ||
| value: 1n, | ||
| data: oversizedData, | ||
| nonce: 3, | ||
| gas: 2_000_000n, | ||
| maxFeePerGas: 20_000_000_000n, | ||
| maxPriorityFeePerGas: 1_000_000_000n, | ||
| chainId: 1, | ||
| type: 'eip1559', | ||
| }; | ||
|
|
||
| await expect(sign(tx)).rejects.toThrow(/Data field too large/); | ||
| }); | ||
|
|
||
| /** | ||
| * Timeout recovery: Verifies clean recovery after a timeout during multi-chunk transfer. | ||
| * Device and SDK should cleanly recover to sign subsequent transactions. | ||
| */ | ||
| it('Recover after timeout during streaming', async () => { | ||
| const client = await getClient(); | ||
| if (!client) throw new Error('Client not setup'); | ||
| const originalTimeout = client.timeout; | ||
|
|
||
| try { | ||
| // Set a very short timeout to force a failure. | ||
| client.timeout = 10; // 10ms | ||
|
|
||
| const largeData = `0x${randomBytes(10000).toString('hex')}`; // 10KB, requires multiple chunks | ||
| const txLarge: TransactionSerializable = { | ||
| to: '0xe242e54155b1abc71fc118065270cecaaf8b7768', | ||
| value: 1n, | ||
| data: largeData, | ||
| nonce: 4, | ||
| gas: 500_000n, | ||
| maxFeePerGas: 20_000_000_000n, | ||
| maxPriorityFeePerGas: 1_000_000_000n, | ||
| chainId: 1, | ||
| type: 'eip1559', | ||
| }; | ||
|
|
||
| // Expect this to fail with a timeout | ||
| await expect(sign(txLarge)).rejects.toThrow(/Timeout/); | ||
|
|
||
| // Reset timeout to its original value | ||
| client.timeout = originalTimeout; | ||
|
|
||
| // Allow a moment for any state to clear | ||
| await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
|
|
||
| // Now, try a small transaction and expect it to succeed | ||
| const txSmall: TransactionSerializable = { | ||
| to: '0xe242e54155b1abc71fc118065270cecaaf8b7768', | ||
| value: 1n, | ||
| data: '0x', | ||
| nonce: 5, // Use a new nonce to avoid replacement errors | ||
| gas: 21_000n, | ||
| maxFeePerGas: 20_000_000_000n, | ||
| maxPriorityFeePerGas: 1_000_000_000n, | ||
| chainId: 1, | ||
| type: 'eip1559', | ||
| }; | ||
|
|
||
| // This should succeed, proving the device recovered cleanly. | ||
| await signAndCompareTransaction(txSmall, 'Timeout recovery transaction'); | ||
| } finally { | ||
| // Ensure timeout is always reset, even if the test fails | ||
| client.timeout = originalTimeout; | ||
| } | ||
| }); | ||
| }); |
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: should we update the jsdocs including the timeout param?