Skip to content

fix: prevent double-credit in execute_transfer for SEND txs - #1600

Merged
MuncleUscles merged 1 commit into
mainfrom
fix/double-credit-execute-transfer
Apr 16, 2026
Merged

MuncleUscles merged 1 commit into
mainfrom
fix/double-credit-execute-transfer

Conversation

@MuncleUscles

Copy link
Copy Markdown
Member

Summary

`execute_transfer` in consensus/base.py unconditionally credited the recipient with `update_account_balance`. For SEND transactions created by `sim_fundAccount`, the endpoint already credits the recipient via `credit_tx_value_once` (PR #1582). The consensus worker then picks up the PENDING SEND and credits again — giving 2× the funded amount.

Why this was latent

The bug has existed since Sept 2024 (`c4c2d77c7`, original consensus code). It was hidden because `test_accounts_funding` asserted on strings (`"0x7d0" == 1000`) which always failed inconclusively. The hex-parse fix in #1595 finally made the test compare real values and surfaced the bug (`assert 2000 == 1000`).

PR #1582 made the endpoint idempotent but didn't touch the consensus side.

Fix

Swap `update_account_balance` for `credit_tx_value_once` in `execute_transfer`:

  • Faucet txs (`sim_fundAccount`): endpoint already set `value_credited=true`, so this is a no-op. No double-credit.
  • Regular user-to-user SEND: `value_credited=false` on fresh tx, so it credits once. Same behavior as before, just idempotent.

Tested

  • 57 relevant unit tests pass (consensus voting, execution mode, crash retry, contract-not-found)
  • 120 db-integration tests pass (including the new `test_migrations.py`)
  • CI integration test `test_accounts_{funding,burn,transfers}` should now pass

Follow-up (out of scope)

The sender-debit side in `execute_transfer` (line 675) is also not idempotent. For faucet txs it's skipped (`from_address=None`) but user-to-user SEND could double-debit on retry. Worth addressing separately.

Refs: #1582, #1595, #1599

@coderabbitai

coderabbitai Bot commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@MuncleUscles has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 11 minutes and 29 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 11 minutes and 29 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a21cb21a-4e1a-408f-9611-0b0822efe4a5

📥 Commits

Reviewing files that changed from the base of the PR and between a6cb266 and 0bbd4e5.

📒 Files selected for processing (1)
  • backend/consensus/base.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/double-credit-execute-transfer

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

…it on SEND

`execute_transfer` (consensus/base.py) was crediting the recipient via
unconditional `update_account_balance`. For SEND transactions created by
`sim_fundAccount`, the endpoint already credits the recipient via
`credit_tx_value_once` (PR #1582 fix). The consensus worker then picks up
the PENDING SEND and credits again in `execute_transfer` — giving 2x the
funded amount.

This bug has existed since Sept 2024 but was hidden by the pre-#1595
`test_accounts_funding` assertion comparing strings ("0x7d0" == 1000,
which always fails inconclusively). The hex-parse fix in #1595 finally
makes the test compare real values and surfaced the bug (assert 2000 == 1000).

Fix: swap update_account_balance for credit_tx_value_once in
execute_transfer. This is a no-op for faucet txs (value_credited already
true) and a one-time credit for regular user-to-user SEND.

Fixes integration test_accounts_{funding,burn,transfers} regressions.
@MuncleUscles
MuncleUscles force-pushed the fix/double-credit-execute-transfer branch from 3119d9d to 0bbd4e5 Compare April 16, 2026 21:23
@MuncleUscles
MuncleUscles merged commit 7d37c6a into main Apr 16, 2026
10 checks passed
@MuncleUscles
MuncleUscles deleted the fix/double-credit-execute-transfer branch April 16, 2026 21:39
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.117.3 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

MuncleUscles added a commit that referenced this pull request May 4, 2026
* fix(rpc): expose value_credited so execute_transfer guard works

PR #1582 added immediate-credit semantics to sim_fundAccount via
credit_tx_value_once. PR #1600 added an idempotency guard in
execute_transfer to prevent the consensus worker from crediting again:

    existing_tx = transactions_processor.get_transaction_by_hash(...)
    if existing_tx and existing_tx.get("value_credited"):
        dispatch FINALIZED; return

But _parse_transaction_data never included value_credited in its dict.
So existing_tx.get("value_credited") always returned None, the guard
never fired, and execute_transfer always credited the recipient via
update_account_balance — on top of the endpoint's credit. Result:
sim_fundAccount(addr, 1000) → balance 2000.

Symptom in CI: integration tests test_accounts_funding,
test_accounts_transfers, test_accounts_burn all asserted balance ==
expected, got expected*2 (or fund + 800 instead of fund - 200).

Verified locally: failing tests pass with the fix; the new regression
test in transactions_processor_test fails without the fix and passes
with it.

* ci(docker): retry apt-get update to ride out Ubuntu mirror sync hiccups

Three CI runs in a row failed at the docker build stage with:
  E: Failed to fetch http://security.ubuntu.com/ubuntu/dists/noble-security/
     main/binary-amd64/Packages.gz
     File has unexpected size (2017749 != 2018102). Mirror sync in progress?

Ubuntu's noble-security mirror occasionally serves stale Packages.gz files
during a sync rotation, persisting for several minutes. Wraps apt-get
update in a 5x/15s-delay retry loop in all three backend Dockerfiles
(backend, consensus-worker, database-migration) so transient mirror skew
doesn't block CI.

* test(integration): serialize mock-validator tests to fix flake under -n 8

Tests in tests/integration/icontracts/ that use the setup_validators
fixture create mock validators with test-specific mock_response
payloads. With pytest-xdist -n 8 parallelism, validators created by
different tests live in the same global pool — when one test runs
consensus, the pool may include validators with another test's mock
data. Validators with non-matching mocks return wrong outputs,
disagreement spikes, the tx errors, the test fails non-deterministically.

Symptom: test_llm_erc20 (and others) fail with execution_result=ERROR
and "json.loads(final_result)" stack traces; vote distributions like
4 disagree / 1 agree.

Fix:
- Add a pytest_collection_modifyitems hook in the icontracts conftest
  that tags every test using setup_validators with
  xdist_group(name="mock_validators"). pytest-xdist's --dist loadgroup
  routes tests with the same group to the same worker, serializing
  them.
- Pass --dist loadgroup to the parallel test command in CI.

Net effect: mock-validator tests run sequentially within one xdist
worker; all other tests still parallelize across the remaining workers.

* test(integration): fix test_llm_erc20 mock key never matching prompt

The mock_response was keyed on "The balance of the sender", a string
that no longer appears anywhere in the LLM prompt the contract issues.
That key historically came from the eq_principle criteria, which back
when the test was written was passed as part of the LLM prompt. The
contract has since refactored to use `gl.eq_principle.strict_eq`,
which runs leader/validators on identical inputs and verifies result
equality without sending the criteria to the LLM.

Result: backend/node/llm.lua's get_mock_response_from_table fell
through every key without matching, then returned the literal string
"no match" (its no-default fallback). Inside the contract,
`json.loads("no match")` raised, GenVM exited with an error receipt,
validator votes diverged depending on whose execution happened to
match the leader, the tx errored, and the test failed
non-deterministically.

Fix: key the mock on "" — llm.lua:52-54 reserves the empty-string key
as the explicit default and returns its value for any prompt. The mock
now correctly returns the expected JSON for every LLM call this
contract makes.

* Revert "test(integration): fix test_llm_erc20 mock key never matching prompt"

This reverts commit dfce5da.

* test(integration): use a real prompt substring as test_llm_erc20 mock key

The mock_response key "The balance of the sender" was a string that
no longer appears anywhere in the LLM prompt the LlmErc20 contract
issues. It used to come from gl.eq_principle's criteria back when
criteria was concatenated to the LLM prompt; the contract has since
moved to gl.eq_principle.strict_eq, which compares results without
sending criteria to the LLM.

backend/node/llm.lua matches mock_response keys against the prompt
via string.find; a non-matching key falls through to the real LLM
provider, whose output is non-deterministic. That's the source of
the long-standing test_llm_erc20 flake (json.loads failing on either
the literal "no match" sentinel or some real-LLM response).

Fix: key the mock on "transactions between users" — a substring the
contract's prompt opens with ("You keep track of transactions between
users and their balance in coins..."). The mock now reliably returns
the canned valid-JSON response for every LLM call this contract
issues.
danieljrc888 added a commit that referenced this pull request Nov 10, 2026
…fer moves value

execute_transfer read value_credited with a plain SELECT, credited the
recipient unconditionally, then set value_credited=true with no
"AND value_credited = false". Any credit that landed between the read and
the worker's commit (sim_fundAccount's credit_tx_value_once, as in the rc11
test_accounts_funding 2000-for-1000 failure) was applied twice. #1815 closes
the endpoint side; this closes the worker side for every caller.

execute_transfer now claims the credit first with the same compare-and-set
credit_tx_value_once uses (claim_tx_value_credit) and only debits/credits
when it won the row. A lost claim finalizes as a no-op transfer, like the
old guard. Debit-then-credit ordering is unchanged; on insufficient balance
the claim is released so the row stays value_credited=false and
refund_tx_value semantics are unchanged.

Lock order: claiming the tx row before inserting a new recipient's
current_state row deadlocked against credit_tx_value_once, which inserts
the account first. execute_transfer now ensures the recipient row first
(ensure_account_exists, shared by both paths), then claims.

Keeps #1748's atomic debit/credit and #1600's retry idempotency.

Tests: tests/db-sqlalchemy/test_execute_transfer_credit_claim.py covers
the endpoint credit racing in mid-transfer, the worker running against an
uncommitted endpoint credit, both concurrently, the insufficient-balance
release, and a retried transfer. Against the old execute_transfer three
fail: two credit 2000 for a 1000 SEND, and the mid-transfer endpoint
credit is not blocked by the worker.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant