Skip to content

Verify token transfer history at a fixed head - #11

Closed
gjwang wants to merge 2 commits into
khalidsaidi:mainfrom
gjwang:fix/allowance-outage-finding-20261106
Closed

gjwang wants to merge 2 commits into
khalidsaidi:mainfrom
gjwang:fix/allowance-outage-finding-20261106

Conversation

@gjwang

@gjwang gjwang commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

The verifier can miss a delegated transfer and its return when the project wallet's nonce and final balance do not expose either movement. In the reported approve → transferFrom → return → revoke scenario, the current verifier reads blocks 1, 100 and 202, skips 200–201, and returns 0.

This update scans the configured token's Transfer logs continuously from deployment through a fixed head, with no nonce-sampling fallback. It checks both RPC endpoints' chain IDs and head identity, validates and deduplicates event identities across pages, and pins balance/supply reads to that canonical block hash. A final head check prevents receipts or state from a later fork creating a finding.

Request attempts, response size, retained events and elapsed time have explicit budgets. An unfinished range is inconclusive (2), while an already-confirmed unledgered movement remains a finding (1). Pending/post-snapshot ledger entries cannot create false balance violations. Live allowances no longer veto complete historical coverage: they describe future spending authority. The README documents configuration and the trusted-RPC boundary; successful log responses are not independent cryptographic receipt proofs or provider-capacity guarantees.

The existing Ledger job now discovers all script Python tests, including the history regressions. Its triggers, permissions and existing 0/1/2 workflow handling remain intact.

Validation:

  • Reproduced the reporter's original roundtrip assertion against the prior PR head: returned 0 instead of 1, using mocked RPC only.
  • The same economic events/assertion return 1 after adapting mock protocol metadata for chain ID, requested topic filtering, per-block hashes and EIP-1898 references. This is not a claim that the reporter's entire five-case fixture passed unchanged.
  • 119 Python tests pass offline with network access disabled before imports. They cover the delegated roundtrip, a complete honest ledger, paging, capped/incomplete responses, wire retries, duplicate identities, pending/future nonzero entries, malformed receipts and reorgs. Counts are not accumulated across repeated runs.
  • Independent source review found four boundary issues; each has a preserved failing counterexample and a forward correction. Final source and CI-delta reviews approve the exact files.
  • No live chain request, wallet, key, ledger/contract mutation or provider benchmark was used. The commit skips CI to avoid automatically invoking the existing live-mainnet workflow for this delivery; no upstream check success is claimed.

Refs #10

@gjwang gjwang changed the title fix: preserve observed findings when allowance reads fail Verify token transfer history at a fixed head Nov 6, 2026
@Chandain

Chandain commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator

You're right. The verifier detects an unaccounted transfer and sets ok = False, but an allowance RPC failure causes an early return 2, bypassing the final return 0 if ok else 1.

I reproduced this with a regression test:

def test_confirmed_violation_survives_allowance_rpc_failure(self):
    ledger = make_mock_ledger()
    ledger["project_wallets"]["bob"] = bob

    code, output = run_main(ledger, latest_block=202)

    self.assertEqual(code, 1, msg=output)

Expected: exit code 1 (confirmed accounting violation).

Actual: exit code 2 (allowance RPC failure).

Relevant output:

eth_getLogs read block 200 to 200

UNACCOUNTED on-chain movements (no ledger entry):
  Alice -> Charlie : 40 SRCMNT

COULD NOT VERIFY: allowance for Bob unreadable
(Mock allowance RPC failure).

AssertionError: 2 != 1

The regression confirms that an infrastructure failure can override an already-confirmed accounting violation.

Regression commit: 5899f584fa7c4f0d2748e236cfb8c9634b6b78f1
Commit Link: https://github.com/Chandain/sourcemint/tree/test/allowance-failure-priority

@gjwang

gjwang commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Chandain — your regression captures the finding-versus-outage priority issue precisely.

The current PR head (28cbe58a) has moved past the allowance gate: it scans Transfer history through a fixed head and no longer queries current allowances to decide historical coverage. Two places to check:

The existing 119-test offline run covers these checks; I have not run your new branch unchanged against this head. You can run the current suite with python3 -m unittest discover -s script -p 'test_*.py' from the checkout. No live RPC is needed for those tests.

@Chandain

Chandain commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for your contribution. Both tests passed, confirming these scenarios:

  • An observed unaccounted transfer + state outage → exit 1 (UNACCOUNTED).
  • No observed violation + state outage → exit 2 (COULD NOT VERIFY).
    I also ran the full test suite using python3 -B -m unittest discover -s script -p 'test_*.py': all 119 tests passed.

Test exit 0 : PASS

Commit HEAD: ea3004c

@Chandain

Chandain commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

I haven’t pulled gjwang’s PR into this branch yet. I tested the current implementation separately on my fork.

Branch: test/no-unaccounted-and-outage

Command:
PYTHONPATH=script python3 -m unittest -v script/test_allowance_outage.py

The following cases pass:

  • No finding + no outage → verifier returns 0
    test_no_unaccounted_and_outage_is_ok

  • Finding + outage → verifier returns 1
    test_observed_unaccounted_transfer_survives_state_outage

  • No finding + outage → verifier returns 2 (inconclusive)
    test_no_observed_violation_with_state_outage_is_inconclusive

All three unit tests pass, so the unittest command itself exits with code 0.

Branch: https://github.com/Chandain/sourcemint/tree/test/no-unaccounted-and-outage

@khalidsaidi

Copy link
Copy Markdown
Owner

@gjwang — thank you, and sorry for the wait on this one too.

Your analysis is right on both counts. The nonce index could never see a transferFrom, and the allowance read I added to cover that case is only evidence while the approval is live — revoke it after the tokens come back and there is nothing left to find. And the second defect you spotted is the one I'd have argued about and lost: returning "could not verify" from the allowance handler when a violation had already been observed threw away the one piece of evidence that didn't depend on the read that failed. The three evidence states are the right shape.

Two things I had to change before it could run outside the tests, both mine to have caught in review and not criticisms of the work:

BASE_HISTORY_BLOCK_SPAN: 10000 — every free keyless endpoint refuses that range:

{'code': -32602, 'message': 'invalid params',
 'data': 'Block range too large: maximum 1000 blocks without an access key.'}

A live run failed on the first page, covered zero blocks, and reported itself inconclusive. Correct behaviour, but it would never have verified anything. It's 1000 now.

BASE_HISTORY_MAX_REQUESTS: 2048, with a 300s timeout — a complete pass is about 16,300 pages and an hour, so it couldn't have finished even with the right span.

Neither shows up offline, which is exactly why you flagged that you hadn't run it live. That was the right thing to say and it's what made them quick to find.

I also went looking for the same mistake elsewhere and found it: docs/verify.html ran the identical nonce derivation in the visitor's browser and told them "nothing can hide there." It was telling strangers something false. Now fixed the same way.

Live results on Base mainnet:

a real transfer removed from the ledger exit 1 UNACCOUNTED, while coverage was incomplete — an observed violation survives partial history, as you argued
provider refuses the range exit 2
budget exhausted exit 2

On landing it: your commit carries [skip ci], so the two required checks never ran and branch protection refuses the merge. [skip ci] is per-commit and I can't push to your fork, so I've put the same commits up as #13 where the checks actually run. Your commit and authorship are intact in it and this PR is the original — I'll close this one only once that lands, and the ledger entry will point here.

One limit I'd rather state than let someone discover: a complete pass is ~16,300 requests today and Base adds ~317,000 blocks a day, so it grows about eight minutes a week and passes the six-hour CI limit within a year. It's written up in docs/verification.md with the two ways out — a keyed endpoint, or a committed checkpoint of the last verified block and hash, which trades "every run re-derives everything" for "every run trusts the last one". If you have a better third option I'd genuinely like to hear it.

Last thing: this qualifies for a contributor reward under REWARDS.md. If you want one, reply with a Base address — the address only, never a key. No obligation, and it changes nothing about the fix landing. SRCMNT is recognition within this project; it has no market value and isn't payment for work.

khalidsaidi pushed a commit that referenced this pull request Nov 16, 2026
main's branch protection requires two checks: "Verify on-chain accounting"
and "Foundry project". Nothing has produced the second since c11b779 on
2026-10-28, which removed .github/workflows/test.yml as a duplicate of
ci.yml. The two did run the same forge commands under the same workflow
name, but test.yml's job was named "Foundry project" and ci.yml's is named
"test", so deleting it took a required context with it.

Every pull request since has been unmergeable. #9 and #12 were closed rather
than merged; #11 could not have merged either, whatever its CI status.

ci.yml's job now carries the name the rule asks for. Protection is unchanged
and no setting was touched — the check it requires simply exists again.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mggwNhgEyvfn1q6Lrdk3Q
khalidsaidi pushed a commit that referenced this pull request Nov 16, 2026
main's branch protection requires two checks: "Verify on-chain accounting"
and "Foundry project". Nothing has produced the second since c11b779 on
2026-10-28, which removed .github/workflows/test.yml as a duplicate of
ci.yml. The two did run the same forge commands under the same workflow
name, but test.yml's job was named "Foundry project" and ci.yml's is named
"test", so deleting it took a required context with it.

Every pull request since has been unmergeable. #9 and #12 were closed rather
than merged; #11 could not have merged either, whatever its CI status.

ci.yml's job now carries the name the rule asks for. Protection is unchanged
and no setting was touched — the check it requires simply exists again.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mggwNhgEyvfn1q6Lrdk3Q
@khalidsaidi

Copy link
Copy Markdown
Owner

Landed on main — your commit is in it as e651fa0, with your authorship, rebased so it sits first and the corrections sit on top. #10 is closed.

One thing I told you above was wrong, and it was wrong in a way that blamed your commit for something that wasn't its fault.

I said your PR couldn't merge because of [skip ci]. That was only half of it, and not the important half. main requires a status check called Foundry project, and nothing in this repo has produced that check since 2026-10-28. I deleted .github/workflows/test.yml in c11b779 as a duplicate of ci.yml — the two really did run the same forge commands under the same workflow name, but the job inside test.yml was named Foundry project and ci.yml's is named test. So I removed a required context and every pull request since has been unmergeable. #9 and #12 were closed rather than merged. Yours would have been blocked with or without [skip ci], and so was mine — #13 sat BLOCKED with every check green until I fixed it.

The fix is ci.yml's job carrying the name the rule asks for again (7997856). No protection setting was touched.

Two more things came out of running your scan for real rather than offline, both yours by implication and worth saying plainly:

The budget reserved requests for the canonical head re-check, but not time. A full pass spends an hour and runs the clock to the deadline, so the re-check was unaffordable the moment the scan ended and a complete, clean history still reported itself unverified. Against mainnet it came back:

COULD NOT VERIFY: fixed-head history was not confirmed canonical.
  RPC time budget exhausted before retry

reserve_seconds now holds back time the way reserve holds back requests, both lifting together (672ad8e, script/test_budget_reserve.py).

The full scan was running on every pull request — eighty minutes to check five entries, growing with the chain. A PR now confirms every entry is a real successful transfer and that balances reconcile (eight requests, under a second); the complete scan runs on main and weekly. The obvious hazard there is your own issue, so the fast mode prints NOT CHECKED: whether anything is missing from the ledger every time it passes (aca5f85).

With the full scan on main:

history coverage: 41988939..58337564 of fixed head 58337564
RESULT: OK — ledger matches trusted RPC history at the fixed head.

Complete coverage, deployment block to head, nothing skipped.

Closing this as merged-via-#13 rather than as rejected — the work is yours. The reward offer stands whenever you want to reply with a Base address; no rush and no obligation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants