Skip to content

Zebrad: add rollback tip height command - #28

Open
endophysics wants to merge 27 commits into
ShieldedLabs:mainfrom
endophysics:rollback-tip-height
Open

Zebrad: add rollback tip height command#28
endophysics wants to merge 27 commits into
ShieldedLabs:mainfrom
endophysics:rollback-tip-height

Conversation

@endophysics

@endophysics endophysics commented May 16, 2026

Copy link
Copy Markdown
Contributor

Adds zebrad rollback-tip-height --height <height> to safely roll back local state by rebuilding a bounded copy through verified block replay, then atomically swapping it into place while keeping the previous state as a backup.
Added it because during sync I would sometimes get stuck on a local branch when mining was enabled, which would then force me to restart the process.

Also invalidates tip-dependent GUI wallet/Zaino caches after rollback and fixes staking/delegation replay during checkpoint copying so rebuilt state preserves accounting correctly.

@endophysics
endophysics force-pushed the rollback-tip-height branch from 3dea123 to 35a1f92 Compare May 16, 2026 20:32
@endophysics
endophysics force-pushed the rollback-tip-height branch from 35a1f92 to c9c4ee9 Compare June 14, 2026 22:03
@Zk-nd3r

Zk-nd3r commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Swap-and-restore instead of in-place finalized mutation is the right call.

Two on the staking replay:

  1. checkpoint_replay_bond_rewards / checkpoint_replay_reward_total reimplement the reward math the non-finalized path already does via finalized.bond_rewards. Two copies of consensus math will drift. Can it call the same function instead of mirroring it?
  2. The active-bond set is computed separately in chain.rs and delegation.rs. If they ever disagree, the pool tally and per-bond credits desync. Worth a test that replays a staking block and checks the derived rewards match the non-finalized output.

@Zk-nd3r

Zk-nd3r commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Nice, this looks like it addresses the staking replay concerns from my earlier comment. bond_rewards_for_active_bonds is now the single reward-split source, checkpoint_replay_reward_total is gone, and the finalized pool tally now comes from the same per-bond replay output. I also checked the live call sites and they all feed POS_BLOCK_REWARD_ZATS.

The one test gap I still see is checkpoint replay end-to-end: the new test covers the helper path, but not a staking block driven through both non-finalized and checkpoint/finalized replay with equal bond rewards/value pools. I think that would be worth adding.

One separate rollback edge: rollback_tip_height.rs swaps the active DB before resetting tip-dependent wallet/Zaino caches. If cache reset fails, the command returns an error after rollback, leaving stale caches beside the new tip. Maybe reset caches before the swap, or restore the old DB if cache reset fails?

Testing: git diff --check origin/dev...HEAD passes. The clean zebra-state --lib test target is currently blocked by unrelated NearTipChains compile errors outside this PR.

@endophysics
endophysics force-pushed the rollback-tip-height branch from e1ca96c to e7c5e70 Compare July 4, 2026 15:28
@endophysics
endophysics changed the base branch from dev to main July 6, 2026 18:20
pmttavara and others added 24 commits July 8, 2026 05:40
- Address the issue that the commit that caused this regression was trying to address, but in a different way that is less likely to expose a memory leak
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
API
---

```
{ "CreateNewDelegationBond":  { "amount_zats": <power-of-10 zat value>, "target_finalizer": "<32-byte finalizer ID>" }, }
{ "RetargetDelegationBond":   { "bond_key": "<32-byte bond pubkey>",    "target_finalizer": "<32-byte finalizer ID>" }, }
{ "BeginDelegationUnbonding": { "bond_key": "<32-byte bond pubkey>"  }, }
{ "WithdrawDelegationBond":   { "bond_key": "<32-byte bond pubkey>"  }, }
```

Example Usage
-------------

First, create a bond:
```sh
$ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "wallet_staking_action", "params": [{"CreateNewDelegationBond": { "amount_zats": 100000000, "target_finalizer": "f18b65c340e048dfd4e9c104fbdbbd842e026c87e657318b8c69270288170cd4" }}], "id": 1}' http://127.0.0.1:8232/

{"jsonrpc":"2.0","id":1,"result":"{ \"txid\": 2b795c1abdc811027fdc11b48d4ed14725be79181b481d3d740927eae0ab4b8b, \"staking_action\": StakingAction { kind: \"CreateNewDelegationBond\", unique_public_key: \"f8416a451081e1b3b33ca032f0cc4195bbe8daf9cfd6c834399ffbb73908b3e8\", challenge: \"0000000000000000000000000000000000000000000000000000000000000000\", signature: \"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\", target_finalizer: \"f18b65c340e048dfd4e9c104fbdbbd842e026c87e657318b8c69270288170cd4\", amount_zats: 100000000 } }"}
```

Then, use the `unique_public_key` from that result as the `bond_key` for retargeting:
```
$ curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "wallet_staking_action", "params": [{"RetargetDelegationBond": { "bond_key": "f8416a451081e1b3b33ca032f0cc4195bbe8daf9cfd6c834
399ffbb73908b3e8", "target_finalizer": "904a343aa5a8a6bf3e930acc12bc7755d68b0ed7fb70cc28e6ae6268d7d197cd" }}], "id": 1}' http://127.0.0.1:8232/

{"jsonrpc":"2.0","id":1,"result":"{ \"txid\": 852655308157bb99ff0bbc4439a3350cbfce309360bcb7059835c008408a4d8b, \"staking_action\": StakingAction { kind: \"RetargetDelegationBond\", unique_public_key: \"f8416a451081e1b3b33ca032f0cc4195bbe8daf9cfd6c834399ffbb73908b3e8\", challenge: \"0000000000000000000000000000000000000000000000000000000000000000\", signature: \"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\", target_finalizer: \"904a343aa5a8a6bf3e930acc12bc7755d68b0ed7fb70cc28e6ae6268d7d197cd\" } }"}
```
@endophysics
endophysics force-pushed the rollback-tip-height branch from e7c5e70 to c78ac72 Compare July 31, 2026 10:51
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.

5 participants