Skip to content

fix: Increase maximum code body length for contract deploys #1793

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

Merged
merged 2 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/transactions/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export const COMPRESSED_PUBKEY_LENGTH_BYTES = 32;
export const UNCOMPRESSED_PUBKEY_LENGTH_BYTES = 64;
export const MEMO_MAX_LENGTH_BYTES = 34;

// https://github.com/stacks-network/stacks-core/blob/31d048c6c345c8cb7be38283385e54870b1c3c83/stacks-common/src/codec/mod.rs#L206
// messages can't be bigger than 16MB plus the preamble and relayers
const MAX_PAYLOAD_LEN = 1 + 16 * 1024 * 1024;
const PREAMBLE_ENCODED_SIZE = 165;
const MAX_RELAYERS_LEN = 16;
const PEER_ADDRESS_ENCODED_SIZE = 16;
const HASH160_ENCODED_SIZE = 20;
const NEIGHBOR_ADDRESS_ENCODED_SIZE = PEER_ADDRESS_ENCODED_SIZE + 2 + HASH160_ENCODED_SIZE;
const RELAY_DATA_ENCODED_SIZE = NEIGHBOR_ADDRESS_ENCODED_SIZE + 4;
export const STRING_MAX_LENGTH =
MAX_PAYLOAD_LEN + (PREAMBLE_ENCODED_SIZE + MAX_RELAYERS_LEN * RELAY_DATA_ENCODED_SIZE);

/**
* The type of transaction (payload) that is being serialized.
* Used internally for serializing and deserializing transactions.
Expand Down
3 changes: 2 additions & 1 deletion packages/transactions/src/wire/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
COMPRESSED_PUBKEY_LENGTH_BYTES,
ClarityVersion,
FungibleConditionCode,
STRING_MAX_LENGTH,
MEMO_MAX_LENGTH_BYTES,
NonFungibleConditionCode,
PayloadType,
Expand Down Expand Up @@ -541,7 +542,7 @@ export function deserializePayload(serialized: string | Uint8Array | BytesReader
throw new Error(`Cannot recognize ClarityVersion: ${n}`);
});
const smartContractName = deserializeLPString(bytesReader);
const codeBody = deserializeLPString(bytesReader, 4, 100_000);
const codeBody = deserializeLPString(bytesReader, 4, STRING_MAX_LENGTH);
return createSmartContractPayload(smartContractName, codeBody, clarityVersion);
}
case PayloadType.PoisonMicroblock:
Expand Down
25 changes: 25 additions & 0 deletions packages/transactions/tests/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
AddressHashMode,
AuthType,
FungibleConditionCode,
PayloadType,
PostConditionMode,
} from '../src/constants';
import { createStacksPublicKey, privateKeyToPublic, publicKeyToHex } from '../src/keys';
Expand All @@ -46,6 +47,8 @@ import {
serializeTransaction,
transactionToHex,
} from '../src/transaction';
import fs from 'node:fs';
import assert from 'node:assert';

beforeEach(() => {
fetchMock.resetMocks();
Expand Down Expand Up @@ -450,6 +453,28 @@ test('Coinbase pay to alt contract principal recipient deserialization', () => {
expect(deserializedTx.transactionVersion).toBe(TransactionVersion.Testnet);
});

test('deserialize wtf tx', () => {
const largeTxDeployBody = fs.readFileSync(
'./packages/transactions/tests/tx-contract-deploy-large-hex.txt',
'utf8'
);
const reader = new BytesReader(largeTxDeployBody);
const deserialized = deserializeTransaction(reader);
expect(deserialized.payload).toBeTruthy();
assert(deserialized.payload.payloadType === PayloadType.VersionedSmartContract);
expect(deserialized.payload.contractName.content).toBe('tcat-01');
expect(
deserialized.payload.codeBody.content.startsWith(
`(use-trait nma 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.trait-sip-010.sip-010-trait) (use-trait nmb 'SP2AKWJYC7BNY18W1XXKPGP0YVEK63QJG4793Z2D4.sip-010-trait-ft-standard.sip-010-trait) (use-trait nmc 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.dao-traits-v4.sip010-ft-trait`
)
);
expect(
deserialized.payload.codeBody.content.endsWith(
`(use-trait nma 'SP102V8P0F7JX67ARQ77WEA3D3CFB5XW39REDT0AM.trait-sip-010.sip-010-trait) (use-trait nmb 'SP2AKWJYC7BNY18W1XXKPGP0YVEK63QJG4793Z2D4.sip-010-trait-ft-standard.sip-010-trait) (use-trait nmc 'SP2ZNGJ85ENDY6QRHQ5P2D4FXKGZWCKTB2T0Z55KS.dao-traits-v4.sip010-ft-trait`
)
);
});

describe(serializeTransaction.name, () => {
const serializedTx =
'0x8080000000040055a0a92720d20398211cd4c7663d65d018efcc1f00000000000000030000000000000000010118da31f542913e8c56961b87ee4794924e655a28a2034e37ef4823eeddf074747285bd6efdfbd84eecdf62cffa7c1864e683c688f4c105f4db7429066735b4e2010200000000050000000000000000000000000000000000000000000000000000000000000000061aba27f99e007c7f605a8305e318c1abde3cd220ac0b68656c6c6f5f776f726c64';
Expand Down

Large diffs are not rendered by default.

Loading