Skip to content

fix: atomic, concurrency-safe contract_addresses.json writes - #242

Closed
OzzyHelper wants to merge 2 commits into
FilOzone:mainfrom
OzzyHelper:koan/atomic-contract-addresses
Closed

OzzyHelper wants to merge 2 commits into
FilOzone:mainfrom
OzzyHelper:koan/atomic-contract-addresses

Conversation

@OzzyHelper

@OzzyHelper OzzyHelper commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

Route every write to contract_addresses.json through one locked, atomic read-modify-write, and drop the unused duplicate writer.

Why

Startup epochs run steps as parallel threads. Epoch 5 deploys MockUSDFC and Multicall3 concurrently; both did an unlocked read-modify-write of the same JSON file, so one deployment's address could be lost. Writes also truncated the file in place, so readers in later steps (PDP SP registration, Curio env setup, status) could observe partial JSON and fail.

How

  • ContractAddresses::update(run_id, f) holds a process-wide mutex across load → mutate → write.
  • Writes go to a temp file in the same directory and are persisted (atomic rename), so readers see old or new content, never a truncated file.
  • A corrupt file is now an error instead of being silently replaced with defaults, preventing loss of already-deployed addresses.
  • MockUSDFC, Multicall3, and FOC deploy all call update; the never-called multicall3_deploy::contract_storage duplicate is removed.
  • Added #[serde(default)] on the two maps so older/partial files still parse.

Testing

  • cargo test --lib contract_addresses: 4 new tests (concurrent writers merge, section merge, readers never see partial JSON, corrupt-file refusal) — pass.
  • cargo clippy --all-targets --all-features -- -D warnings — clean.
  • Full cargo test --all-targets --all-features: 55 passed; 2 pre-existing Docker-dependent status tests fail in this sandbox (no Docker CLI).

Quality Report

Changes: 6 files changed, 250 insertions(+), 128 deletions(-)

Code scan: clean

Tests: skipped

Branch hygiene: 1 issue(s)

  • Branch is not pushed to remote

Parallel startup steps (MockUSDFC, Multicall3, FOC deploy) each did an
unlocked read-modify-write of the shared contract_addresses.json. Two can
run in the same epoch, so one step's entries could be silently lost; the
non-atomic write also let concurrent readers (PDP registration, Curio
env setup, status) observe truncated JSON.

All writers now go through ContractAddresses::update, which holds a
process-wide mutex for the whole read-modify-write and writes via a
temporary file renamed into place. Corrupt files are rejected instead of
being replaced, so a bad write cannot erase deployed addresses.

Tests cover concurrent writers merging, section merges, readers never
seeing partial JSON, and corrupt-file refusal.
Its save_contract_address was never called and duplicated the racy
JSON writer from usdfc_deploy. Multicall3 now writes through the shared
ContractAddresses::update path.
@FilOzzy FilOzzy added this to FOC Nov 19, 2026
@github-project-automation github-project-automation Bot moved this to 📌 Triage in FOC Nov 19, 2026
@Kubuxu

Kubuxu commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

too complicated for what it is worth right now

@Kubuxu Kubuxu closed this Nov 19, 2026
@github-project-automation github-project-automation Bot moved this from 📌 Triage to 🎉 Done in FOC Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🎉 Done

Development

Successfully merging this pull request may close these issues.

2 participants