Fix broken chains after restore#904
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #904 +/- ##
==========================================
+ Coverage 70.51% 70.59% +0.08%
==========================================
Files 139 139
Lines 27685 27704 +19
==========================================
+ Hits 19522 19558 +36
+ Misses 8163 8146 -17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Fixes broken blockchain continuity after account restore/resync by ensuring the Nostr chain-event cache is rebuilt/invalidated alongside restored/resynced blocks, and by preventing publishing blocks when the previous-chain event is missing.
Changes:
- Rebuild and invalidate Nostr chain-event cache during restore and chain resync (identity/company/bill).
- Remove the
validflag fromNostrChainEventand addremove_chain_eventsto purge stale cache entries. - Add a publish-time guard to reject publishing non-genesis blocks when the previous event is missing.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/bcr-ebill-transport/src/test_utils.rs | Extends mocks for the new remove_chain_events API. |
| crates/bcr-ebill-transport/src/nostr_transport.rs | Updates persisted chain-event payload to match the new NostrChainEvent shape. |
| crates/bcr-ebill-transport/src/lib.rs | Wires nostr_chain_event_store into processors/restore service constructors. |
| crates/bcr-ebill-transport/src/handler/restore.rs | Stores recovered identity-chain events into the chain-event store during restore. |
| crates/bcr-ebill-transport/src/handler/public_chain_helpers.rs | Removes valid from as_chain_store_event and its call sites. |
| crates/bcr-ebill-transport/src/handler/mod.rs | Updates mock trait surface to include remove_chain_events. |
| crates/bcr-ebill-transport/src/handler/identity_chain_event_processor.rs | Purges and repopulates identity chain-event cache during resync. |
| crates/bcr-ebill-transport/src/handler/identity_chain_event_handler.rs | Adapts event persistence to new NostrChainEvent shape. |
| crates/bcr-ebill-transport/src/handler/company_invite_handler.rs | Stops persisting “invalid/malicious” fork events; stores only inserted chain events. |
| crates/bcr-ebill-transport/src/handler/company_chain_event_processor.rs | Purges and repopulates company chain-event cache during resync. |
| crates/bcr-ebill-transport/src/handler/company_chain_event_handler.rs | Adapts event persistence to new NostrChainEvent shape. |
| crates/bcr-ebill-transport/src/handler/bill_invite_handler.rs | Stops persisting “invalid/malicious” fork events; stores only inserted chain events. |
| crates/bcr-ebill-transport/src/handler/bill_chain_event_processor.rs | Purges and repopulates bill chain-event cache during resync. |
| crates/bcr-ebill-transport/src/handler/bill_chain_event_handler.rs | Adapts event persistence to new NostrChainEvent shape. |
| crates/bcr-ebill-transport/src/block_transport.rs | Adds publish-time validation to avoid orphaned blocks (plus tests). |
| crates/bcr-ebill-persistence/src/nostr.rs | Adds remove_chain_events and removes valid from NostrChainEvent. |
| crates/bcr-ebill-persistence/src/db/nostr_chain_event.rs | Drops valid column usage; updates queries and adds delete-by-chain helper. |
| crates/bcr-ebill-core/src/protocol/blockchain/bill/chain.rs | Minor closure variable rename in sort comparator. |
| crates/bcr-ebill-api/src/tests/mod.rs | Updates mock trait surface to include remove_chain_events. |
Comments suppressed due to low confidence (3)
crates/bcr-ebill-transport/src/handler/company_chain_event_handler.rs:113
- The error log message in
store_eventsays "Failed to store bill chain..." even though this is the company chain handler. This makes logs misleading when diagnosing chain resync/restore behavior; update the message to refer to the company chain/event handler.
{
error!("Failed to store bill chain nostr event into event store {e}");
}
crates/bcr-ebill-persistence/src/db/nostr_chain_event.rs:55
find_chain_events/find_latest_block_events/find_root_eventnow query all chain events without filtering on the legacyvalidflag. Existing databases may already contain rows withvalid = falsefrom the previous implementation; after this change those previously-invalid events will be returned and can become "latest" tips, reintroducing disconnected-chain behavior. Consider a migration/cleanup step (e.g., deletevalid = falserows) or keep filtering tovalid = truefor backwards compatibility until old data is purged.
async fn find_all_chain_events(
&self,
chain_id: String,
chain_type: BlockchainType,
) -> Result<Vec<NostrChainEventDb>> {
let mut bindings = Bindings::default();
bindings.add(DB_TABLE, Self::TABLE)?;
bindings.add(CHAIN_ID, chain_id.to_owned())?;
bindings.add(CHAIN_TYPE, chain_type)?;
let result: Vec<NostrChainEventDb> = self.db
.query(format!(
"SELECT * FROM type::table(${DB_TABLE}) WHERE {CHAIN_ID} = ${CHAIN_ID} AND {CHAIN_TYPE} = ${CHAIN_TYPE} ORDER BY {BLOCK_HEIGHT} DESC"
).as_str(), bindings)
.await?;
Ok(result)
}
crates/bcr-ebill-transport/src/handler/identity_chain_event_handler.rs:112
- The error log message in
store_eventsays "Failed to store bill chain..." even though this is the identity chain handler. This makes production logs misleading when diagnosing restore/resync issues; update the message to refer to the identity chain/event handler.
{
error!("Failed to store bill chain nostr event into event store {e}");
}
📝 Description
Fixes #902. Issue was that on restore we only restored the plain block but not the event chain cache. This leads to disconnected block published after restore. Now we write the cache when processing restore blocks. I discovered another issue (blockchains disconnect after re-sync for the same reason, cache can have invalid tips) which is also fixed with this PR.
Relates to #902
✅ Checklist
Please ensure the following tasks are completed before requesting a review:
cargo fmt.cargo clippy.🚀 Changes Made
Bug Fixes:
Other Changes:
💡 How to Test
Please provide clear instructions on how reviewers can test your changes:
📋 Review Guidelines
Please focus on the following while reviewing: