fix(core): fix solana priority fee calculation
What changed, and why it matters
This commit fixes how Trezor hardware wallets estimate Solana transaction priority fees when the user does not set an explicit compute-unit limit. Previously the code always assumed a flat 200,000 compute units, which could make the displayed fee much lower or much higher than what the Solana network would actually charge. The patch now follows Solana's real rules: built-in programs cost 3,000 units each, unknown programs cost 200,000 units each, and the total is capped at 1.4 million units. It also rejects duplicate compute-budget instructions, matching what the Solana runtime does. The main risk is that a user might approve a transaction based on an incorrect fee estimate, but the actual funds moved still depend on the transaction's instructions, not the estimate.
Treat as a functional correctness fix with user-safety implications. Review whether the new default-unit accounting and duplicate-instruction rejection match the referenced Solana Agave implementation. Consider whether any previously signed transaction could have been displayed with a materially wrong fee, and whether user-facing release notes should mention the improved fee accuracy.
Security signals we found
Incorrect fee estimation could mislead users about transaction cost
Added duplicate compute-budget instruction validation prevents parsing ambiguity
Aligns Trezor fee model with Solana runtime defaults and limits
Fixes over/under-estimation of priority fees for transactions without explicit compute unit limits
Evidence from the diff
The change replaces the single constant SOLANA_COMPUTE_UNIT_LIMIT with per-instruction defaults (SOLANA_BUILTIN_INSTRUCTION_COMPUTE_UNIT_LIMIT = 3000, SOLANA_DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT = 200000) and a cap (SOLANA_MAX_COMPUTE_UNIT_LIMIT = 1400000). It adds a new validation pass (_check_compute_budget_instructions) that raises DataError on duplicate SetComputeUnitLimit or SetComputeUnitPrice instructions, mirroring Solana runtime behavior. The priority-fee calculation now sums per-instruction default units when no explicit limit is present, clamps the result to the network maximum, and multiplies by the unit price. Tests cover default limits, explicit limits, clamping, rounding, and duplicate-instruction rejection.
Changed components
core/src/apps/solana/transaction/__init__.pycore/src/apps/solana/constants.pycore/tests/test_apps.solana.transaction.pyInspect captured patch +376 / −233
### core/.changelog.d/+solana_priority_fee.fixed
@@ -0,0 +1 @@
+Solana: Fixed priority fee calculation for transactions without an explicit compute unit limit.
### core/src/apps/solana/constants.py
@@ -3,7 +3,18 @@
ADDRESS_SIZE = const(32)
SOLANA_BASE_FEE_LAMPORTS = const(5000)
-SOLANA_COMPUTE_UNIT_LIMIT = const(200000)
+
+# Default compute unit limit, if not set by the transaction:
+# https://github.com/anza-xyz/agave/blob/a203300045b405f86c9c84308b2eae76af69ac3b/compute-budget-instruction/src/compute_budget_instruction_details.rs#L196-L219
+SOLANA_BUILTIN_INSTRUCTION_COMPUTE_UNIT_LIMIT = const(3000)
+SOLANA_DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT = const(200000)
+SOLANA_MAX_COMPUTE_UNIT_LIMIT = const(1400000)
+# Builtins may become non-builtins, so only the recognized ones are listed:
+# https://github.com/anza-xyz/agave/blob/a203300045b405f86c9c84308b2eae76af69ac3b/builtins-default-costs/src/lib.rs#L108-L120
+SOLANA_BUILTIN_PROGRAM_IDS = (
+ "11111111111111111111111111111111", # System
+ "ComputeBudget111111111111111111111111111111",
+)
# 1 lamport has 1M microlamports
MICROLAMPORTS_PER_LAMPORT = const(1000000)
### core/src/apps/solana/transaction/__init__.py
@@ -10,7 +10,10 @@
from ..constants import (
MICROLAMPORTS_PER_LAMPORT,
SOLANA_BASE_FEE_LAMPORTS,
- SOLANA_COMPUTE_UNIT_LIMIT,
+ SOLANA_BUILTIN_INSTRUCTION_COMPUTE_UNIT_LIMIT,
+ SOLANA_BUILTIN_PROGRAM_IDS,
+ SOLANA_DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT,
+ SOLANA_MAX_COMPUTE_UNIT_LIMIT,
)
from ..types import AddressType, is_address_reference
from .instruction import Instruction
@@ -81,6 +84,7 @@ class Transaction:
def __init__(self, serialized_tx: AnyBytes) -> None:
self._parse_transaction(serialized_tx)
self._create_instructions()
+ self._check_compute_budget_instructions()
self._determine_if_blind_signing()
def _parse_transaction(self, serialized_tx: AnyBytes) -> None:
@@ -285,6 +289,17 @@ def _create_instructions(self) -> None:
self.instructions.append(instruction)
+ def _check_compute_budget_instructions(self) -> None:
+ # Duplicates are rejected by the runtime:
+ # https://github.com/anza-xyz/agave/blob/a203300045b405f86c9c84308b2eae76af69ac3b/compute-budget-instruction/src/compute_budget_instruction_details.rs#L155-L189
+ seen_instruction_ids = []
+ for instruction in self.instructions:
+ if instruction.program_id != COMPUTE_BUDGET_PROGRAM_ID:
+ continue
+ if instruction.instruction_id in seen_instruction_ids:
+ raise DataError("Duplicate compute budget instruction")
+ seen_instruction_ids.append(instruction.instruction_id)
+
def _determine_if_blind_signing(self) -> None:
for instruction in self.instructions:
if (
@@ -327,26 +342,32 @@ def _calculate_priority_fee(self) -> int:
return self.config_priority_fee
unit_price = 0
- is_unit_price_set = False
- unit_limit = SOLANA_COMPUTE_UNIT_LIMIT
- is_unit_limit_set = False
+ unit_limit = None
+ # Used when the unit limit is not set explicitly.
+ default_unit_limit = 0
for instruction in self.instructions:
if instruction.program_id == COMPUTE_BUDGET_PROGRAM_ID:
+ # Duplicates are rejected in _check_compute_budget_instructions().
if (
instruction.instruction_id
== COMPUTE_BUDGET_PROGRAM_ID_INS_SET_COMPUTE_UNIT_LIMIT
- and not is_unit_limit_set
):
unit_limit = instruction.units
- is_unit_limit_set = True
elif (
instruction.instruction_id
== COMPUTE_BUDGET_PROGRAM_ID_INS_SET_COMPUTE_UNIT_PRICE
- and not is_unit_price_set
):
unit_price = instruction.lamports
- is_unit_price_set = True
+
+ if instruction.program_id in SOLANA_BUILTIN_PROGRAM_IDS:
+ default_unit_limit += SOLANA_BUILTIN_INSTRUCTION_COMPUTE_UNIT_LIMIT
+ else:
+ default_unit_limit += SOLANA_DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT
+
+ if unit_limit is None:
+ unit_limit = default_unit_limit
+ unit_limit = min(unit_limit, SOLANA_MAX_COMPUTE_UNIT_LIMIT)
priority_fee = unit_price * unit_limit # in microlamports
return (
### core/tests/test_apps.solana.transaction.py
@@ -568,5 +568,115 @@ def test_fee(self):
self.assertEqual(fee.priority, priority_fee)
+UNKNOWN_PROGRAM = b"u" * 32
+VOTE_PROGRAM = base58.decode("Vote111111111111111111111111111111111111111")
+
+
+def set_compute_unit_limit(units: int) -> RawInstruction:
+ return (3, (), b"\x02" + units.to_bytes(4, "little"))
+
+
+def set_compute_unit_price(microlamports: int) -> RawInstruction:
+ return (3, (), b"\x03" + microlamports.to_bytes(8, "little"))
+
+
+UNKNOWN_INSTRUCTION: RawInstruction = (4, (0,), b"\x00")
+# Builtin, but not in SOLANA_BUILTIN_PROGRAM_IDS
+VOTE_INSTRUCTION: RawInstruction = (5, (0,), b"\x00\x00\x00\x00")
+
+
+@unittest.skipUnless(not utils.BITCOIN_ONLY, "altcoin")
+class TestSolanaTransactionFee(unittest.TestCase):
+ def build_transaction(self, instructions: Sequence[RawInstruction]) -> Transaction:
+ accounts = (
+ SIGNER,
+ RECIPIENT,
+ SYSTEM_PROGRAM,
+ COMPUTE_BUDGET_PROGRAM,
+ UNKNOWN_PROGRAM,
+ VOTE_PROGRAM,
+ )
+ return Transaction(build_tx((1, 0, 4), accounts, instructions))
+
+ def assert_fee(
+ self, instructions: Sequence[RawInstruction], expected_priority_fee: int
+ ):
+ fee = self.build_transaction(instructions).calculate_fee()
+ assert fee is not None
+ self.assertEqual(fee.base, 5000)
+ self.assertEqual(fee.priority, expected_priority_fee)
+ self.assertEqual(fee.rent, 0)
+ self.assertEqual(fee.total, 5000 + expected_priority_fee)
+
+ def test_no_compute_budget(self):
+ self.assert_fee([TRANSFER_INSTRUCTION], 0)
+ self.assert_fee([UNKNOWN_INSTRUCTION] * 6, 0)
+
+ def test_default_unit_limit(self):
+ # price of 1 lamport per compute unit
+ price = 1_000_000
+ # 3000 units per builtin instruction
+ self.assert_fee([set_compute_unit_price(price), TRANSFER_INSTRUCTION], 6000)
+ # 200000 units per non-builtin instruction
+ self.assert_fee(
+ [set_compute_unit_price(price), TRANSFER_INSTRUCTION, UNKNOWN_INSTRUCTION],
+ 206000,
+ )
+ # unrecognized builtins are overestimated
+ self.assert_fee([set_compute_unit_price(price), VOTE_INSTRUCTION], 203000)
+ # clamped to 1400000 units
+ self.assert_fee(
+ [set_compute_unit_price(price)] + [UNKNOWN_INSTRUCTION] * 7, 1400000
+ )
+
+ def test_default_unit_limit_rounding(self):
+ # 1203000 * 1_000_000_000 microlamports
+ self.assert_fee(
+ [set_compute_unit_price(1_000_000_000)] + [UNKNOWN_INSTRUCTION] * 6,
+ 1203000000,
+ )
+ # 6000 * 1 microlamports, rounded up
+ self.assert_fee([set_compute_unit_price(1), TRANSFER_INSTRUCTION], 1)
+
+ def test_explicit_unit_limit(self):
+ price = 1_000_000
+ self.assert_fee(
+ [
+ set_compute_unit_limit(300),
+ set_compute_unit_price(price),
+ UNKNOWN_INSTRUCTION,
+ UNKNOWN_INSTRUCTION,
+ ],
+ 300,
+ )
+ self.assert_fee(
+ [set_compute_unit_price(price), set_compute_unit_limit(0)]
+ + [UNKNOWN_INSTRUCTION] * 6,
+ 0,
+ )
+ # clamped to 1400000 units
+ self.assert_fee(
+ [set_compute_unit_limit(2_000_000), set_compute_unit_price(price)],
+ 1400000,
+ )
+ # no price, no priority fee
+ self.assert_fee([set_compute_unit_limit(300), UNKNOWN_INSTRUCTION], 0)
+
+ def test_duplicate_compute_budget_instructions(self):
+ vectors = (
+ [set_compute_unit_price(1), set_compute_unit_price(2)],
+ [set_compute_unit_limit(1), set_compute_unit_limit(2)],
+ [
+ set_compute_unit_price(1),
+ set_compute_unit_limit(1),
+ TRANSFER_INSTRUCTION,
+ set_compute_unit_price(1),
+ ],
+ )
+ for instructions in vectors:
+ with self.assertRaises(DataError):
+ self.build_transaction(instructions)
+
+
if __name__ == "__main__":
unittest.main()
### tests/ui_tests/fixtures.json
[binary or diff unavailable]Why this scored 39/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.