Skip to content

Commit 2670ac7

Browse files
authored
throws error in createTransaction if tx is too big (#5686)
compares the estimated postedSize of the raw transaction to the maximum transaction size computed by the verifier throws a new error type, MaxTransactionSizeError, if the proposed transaction is too big
1 parent 20b2728 commit 2670ac7

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

ironfish/src/wallet/__fixtures__/wallet.test.ts.fixture

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8103,5 +8103,62 @@
81038103
"sequence": 1
81048104
}
81058105
}
8106+
],
8107+
"Wallet createTransaction should throw error if transaction exceeds maximum size": [
8108+
{
8109+
"value": {
8110+
"encrypted": false,
8111+
"version": 4,
8112+
"id": "9750689e-2fb8-4682-bb44-398e7f1d5876",
8113+
"name": "a",
8114+
"spendingKey": "bff9625704dd16050f3f47e86e7b927c182b50d0d6b592db980969a2e8da07dd",
8115+
"viewKey": "5cea66f29bcb20ce16502736e8df819966089dba2d7a04c7587873bae5b0bd056822dbb2a8b1c546de5484474323efa8f99fe2ade280a496f624e2dacc97a4b0",
8116+
"incomingViewKey": "f4ab45f0354c5bc34ae5ed59516a5b43989a760038b13a77f7fa7b806a248c01",
8117+
"outgoingViewKey": "59086f1ba18db5bdd080ef1687586b18fdb12874fbefa6382e9ac093020f436e",
8118+
"publicAddress": "3a9ccabccf1c5c43faf585c5868c52b789d4cc637f35625b00a172b323544114",
8119+
"createdAt": {
8120+
"sequence": 1,
8121+
"hash": {
8122+
"type": "Buffer",
8123+
"data": "base64:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
8124+
}
8125+
},
8126+
"scanningEnabled": true,
8127+
"proofAuthorizingKey": "3aeda7001389e5892759d9cd44cd90d6a4aba0542d90754afc55cbb1dbab5e08"
8128+
},
8129+
"head": {
8130+
"hash": {
8131+
"type": "Buffer",
8132+
"data": "base64:R5HXrp+X3xAO8VWOhHctagm0N2I4goP3XG8goyqIqoY="
8133+
},
8134+
"sequence": 1
8135+
}
8136+
},
8137+
{
8138+
"header": {
8139+
"sequence": 2,
8140+
"previousBlockHash": "4791D7AE9F97DF100EF1558E84772D6A09B43762388283F75C6F20A32A88AA86",
8141+
"noteCommitment": {
8142+
"type": "Buffer",
8143+
"data": "base64:87WziYftOAPyFtS5rVkAQ0sW+vZQ8dTfsg9Jc6hq8HA="
8144+
},
8145+
"transactionCommitment": {
8146+
"type": "Buffer",
8147+
"data": "base64:dGv/oHWnDcNihtSRMGnTPqyddl55UH8vVHAsi88hTco="
8148+
},
8149+
"target": "9282972777491357380673661573939192202192629606981189395159182914949423",
8150+
"randomness": "0",
8151+
"timestamp": 1733261439593,
8152+
"graffiti": "0000000000000000000000000000000000000000000000000000000000000000",
8153+
"noteSize": 4,
8154+
"work": "0"
8155+
},
8156+
"transactions": [
8157+
{
8158+
"type": "Buffer",
8159+
"data": "base64:AQAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGzKiP////8AAAAAQqOshS/dbjwndXBZHbs5y919S0KrvROZ3qnprQS1BgmhcAFGABDAsZ7BzweJK8KgplR92F3aPnx2yfCCVgKk5lLq06WbEKvoIt0mR6E1bFSVA7AHzgfQhn0kD14hiuJbH550P2fGq+WYcxOALA0VTINO2U4w/mLw+sjXL+eDbpQAzn0qMBKYTCphVupLlndGtove+1fBkTmiiwuGKHgV5q2iOMuv1UuPZ3oZKnRXAPiZ105dArrbQwZx6SHYLP+pjBlu1VNJQtUOeEMUevrS8L+/ry/OPmCy5mxMawi/+o7b8su2lJt1kichaSYbCxjm+9lu3rbUu4zF5KYYh+Jdg6WgO10jOBlixnMmLb5AY0CkyU5nQDPqBXWZRa2dh8MdEsGrxS0Jw8n/rInT8eQxknrD8FlJMDFk4mB2o+w/gEdsvhGvVqM010FeFO6V98HBcVHSxanTHhA6tmDuk6SwFnYlo+6EwehvBw+9PdO7B+Rts8SZoNmn40vwwJJKt2VrGTY1G6TQQVkTNfSX6dCCZy+zs7DWVXucnyE0X7/eGAHT1hVyBb/VFhf9O2XKgoSFJuO8Yh4Xp5AVw/v8mco4TcH3zqqmFNKfcWDO88uBuj/lc6z4nBjbuUlyb24gRmlzaCBub3RlIGVuY3J5cHRpb24gbWluZXIga2V5MDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwfDbJIcH+Rojdcjg3IiS2/oi/vYhkMGu0No4f1x1pFUf2uxM92ra4uXm4JgRdXzDoHQxsgICkr9TkwAtW7mwJBA=="
8160+
}
8161+
]
8162+
}
81068163
]
81078164
}

ironfish/src/wallet/errors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export class MaxMemoLengthError extends Error {
3434
}
3535
}
3636

37+
export class MaxTransactionSizeError extends Error {
38+
name = this.constructor.name
39+
}
40+
3741
export class DuplicateAccountNameError extends Error {
3842
name = this.constructor.name
3943

ironfish/src/wallet/wallet.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { BufferMap, BufferSet } from 'buffer-map'
66
import { v4 as uuid } from 'uuid'
77
import { Assert } from '../assert'
88
import { Blockchain } from '../blockchain'
9-
import { VerificationResultReason } from '../consensus'
9+
import { VerificationResultReason, Verifier } from '../consensus'
1010
import { RawTransaction } from '../primitives'
1111
import { TransactionVersion } from '../primitives/transaction'
1212
import {
@@ -28,6 +28,7 @@ import {
2828
DuplicateAccountNameError,
2929
DuplicateSpendingKeyError,
3030
MaxMemoLengthError,
31+
MaxTransactionSizeError,
3132
} from './errors'
3233
import { toAccountImport } from './exporter'
3334
import { AssetStatus, Wallet } from './wallet'
@@ -1459,6 +1460,33 @@ describe('Wallet', () => {
14591460
// no spends needed
14601461
expect(rawTransaction.spends.length).toBe(0)
14611462
})
1463+
1464+
it('should throw error if transaction exceeds maximum size', async () => {
1465+
const { node } = nodeTest
1466+
1467+
const account = await useAccountFixture(node.wallet, 'a')
1468+
1469+
const block1 = await useMinerBlockFixture(node.chain, undefined, account, node.wallet)
1470+
await expect(node.chain).toAddBlock(block1)
1471+
await node.wallet.scan()
1472+
1473+
// Mock verifier to only allow transactions of size 0
1474+
jest.spyOn(Verifier, 'getMaxTransactionBytes').mockImplementation((_) => 0)
1475+
1476+
const promise = nodeTest.wallet.createTransaction({
1477+
account: account,
1478+
fee: 0n,
1479+
outputs: [
1480+
{
1481+
publicAddress: account.publicAddress,
1482+
amount: 1n,
1483+
memo: Buffer.alloc(0),
1484+
assetId: Asset.nativeId(),
1485+
},
1486+
],
1487+
})
1488+
await expect(promise).rejects.toThrow(MaxTransactionSizeError)
1489+
})
14621490
})
14631491

14641492
describe('getTransactionStatus', () => {

ironfish/src/wallet/wallet.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
DuplicateMultisigSecretNameError,
5252
DuplicateSpendingKeyError,
5353
MaxMemoLengthError,
54+
MaxTransactionSizeError,
5455
NotEnoughFundsError,
5556
} from './errors'
5657
import { isMultisigSignerImport } from './exporter'
@@ -934,6 +935,15 @@ export class Wallet {
934935
})
935936
}
936937

938+
const maxTransactionSize = Verifier.getMaxTransactionBytes(
939+
this.consensus.parameters.maxBlockSizeBytes,
940+
)
941+
if (raw.postedSize() > maxTransactionSize) {
942+
throw new MaxTransactionSizeError(
943+
`Proposed transaction is larger than maximum transaction size of ${maxTransactionSize} bytes`,
944+
)
945+
}
946+
937947
return raw
938948
} finally {
939949
unlock()

0 commit comments

Comments
 (0)