Allow constructing `Wollet` from materialized state, skipping `Update` replay
@LeoComandini 已经在做这个了。
开始于 2026年9月23日。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 38/100
- Issue 类型
- 功能
- 描述清晰度
- 基本清楚
- 活跃度
- 冷清
- 技术栈
- rust
调研方向
阅读 lwk_wollet/src/wollet.rs 中的 Wollet::new 和 WolletConciseState,然后跟踪 lwk_wollet/src/update.rs 中的 restore_updates 和 apply_update_inner。定义物化状态的公共形状和构造函数契约,确保可以排除省略的历史记录,而不改变现有构造函数或 persister 的行为;当提议的 from_state 路径能够在不重放 Update 的情况下构造出可用的 Wollet 时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
Hi everyone,
I run a production hot wallet on top of lwk_wollet that has accumulated 30k+ transactions over its lifetime. As the update log and the in-memory caches grew, the process began to OOM-crash on boot: rehydration via Wollet::new allocates the full historical state (transactions, derivation maps, heights, timestamps) into resident memory before the wallet is usable, and on our hardware that final state crosses the available RAM budget.
We already maintain an external indexer that holds the canonical transaction history and can serve any subset of it on demand, so most of what Wollet rebuilds at boot is data we do not need to keep in process to operate. What we are missing from LWK is a way to construct a Wollet directly from a minimal, application-supplied state — bypassing the Update-replay path that forces full history into memory.
This issue describes that gap and proposes a narrow addition (Wollet::from_state) to close it, while being explicit about which existing LWK mechanisms are not sufficient and why.
Summary
lwk_wollet already exposes a public Persister trait (persister.rs:35) and a Wollet::new(network, Arc<dyn DynStore>, descriptor) constructor (wollet.rs:448) that accepts any custom backend, so applications can already plug in their own storage (database, key-value store, etc.). That part is solved.
The remaining gap is that rehydration always goes through linear replay of every persisted Update in Wollet::restore_updates (update.rs:378):
pub(crate) fn restore_updates(&mut self) -> Result<(), Error> {
for i in 0.. {
if let Some(update) = self.updates_persister.get_update(i)? {
self.apply_update_inner(update)?;
} else {
let mut next_update_index = self.updates_persister.next_update_index()?;
*next_update_index = self.updates_persister.merge_updates(i)?;
break;
}
}
Ok(())
}
I would like a constructor that takes already-materialized state and skips replay entirely, e.g. Wollet::from_state(network, descriptor, WolletFullState).
Motivation
In production we operate large, long-lived wallets whose persisted update log grows to hundreds of MB and whose fully-rehydrated Wollet reaches multi-GB resident memory. Boot time is dominated by restore_updates, and steady-state memory is dominated by the in-memory caches that replay populates. We have an external indexer that already holds the canonical transaction history and can produce a minimal materialized state on demand; we need a way to feed that state into Wollet without round-tripping through the Update log.
Why existing mechanisms do not solve this
LWK already has compaction-flavored machinery, and it is worth being explicit about why none of it closes this gap:
only_tipcoalescing (update.rs:145-159). When a newUpdateonly moves the tip and the previous one did too, the persister overwrites the previous entry instead of appending. ThePersistertrait doc explicitly asks implementors to behave this way.Update::prune(update.rs:231) and the per-batchpruneinupdate.rs:36. Strips transactions/scripts not relevant to the wallet from a singleUpdatebefore persisting.merge_threshold(wollet.rs:113, implemented inupdate.rs:91). When more thanthresholdupdates are persisted, they are folded into a single consolidatedUpdateat index0viaUpdate::merge(update.rs:314). Withwith_persisted_txs(wollet.rs:103) this is forced toSome(1), so at most oneUpdateis ever on disk.
These three mechanisms address disk footprint and boot time. They do not address resident memory, which is the motivation here.
A consolidated snapshot-style update — even a hypothetical perfect one — produces the same in-memory Wollet state as replaying the original sequence. After construction, cache.all_txs, the script/derivation maps, the height map, and the unspent set hold exactly the same data regardless of whether they were built from 10,000 small Updates or one merged one. The cost is in the final state, not in the replay path. Replacing many small persisted entries with one large entry is a change of intermediate representation, not of steady-state memory.
Concretely, in our deployment:
| Mechanism | Disk | Boot time | Resident RAM |
|---|---|---|---|
| Status quo | ~800 MB | ~30 s | ~7 GB |
| Hypothetical aggressive merge | ~50 MB | ~5 s | ~7 GB |
from_state with semantic prune |
application-controlled | ~0 s | application-controlled |
What would actually reduce resident memory
Semantic pruning, not compaction. A from_state path lets the application supply only the data it needs to operate going forward — for example:
- Only transactions producing UTXOs that are still unspent.
- Only the derivation range from
last_unusedonward. - Only the current tip, not historical tips/timestamps.
Trade-offs the application opts into when using this path:
wallet.transactions()no longer returns full history.- Reorgs deeper than the snapshot horizon require a rescan.
- Audit/tax features built on top of the wallet must source history elsewhere.
These are acceptable for our use case (transaction history lives in an external indexer); they would not be acceptable as defaults. Hence the request is an additional constructor, not a behavior change to existing ones.
Proposed shape
impl Wollet {
/// Construct a `Wollet` from already-materialized state, skipping the
/// `Update`-replay path used by `Wollet::new`.
///
/// The caller is responsible for the consistency of `state`. Fields the
/// caller omits will be reflected as-is in the resulting wallet — e.g.
/// transactions not included will not appear in `wallet.transactions()`,
/// and reorgs deeper than the snapshot horizon will require a rescan.
pub fn from_state(
network: Network,
descriptor: WolletDescriptor,
state: WolletFullState,
) -> Result<Self, Error>;
}
Where WolletFullState is a public, serializable struct exposing the fields needed to populate Cache directly: tip, scripts/paths maps, heights, unspent set, last_unused counters, and whatever subset of transactions the caller chooses to include.
The existing private WolletConciseState (wollet.rs:261) is close to what is needed for the read/scan side, but a write-side equivalent would need the transaction set too. Happy to iterate on the exact shape.
Scope and non-goals
- Pure addition; no change to existing constructors, persister semantics, or default behavior.
- Does not require touching
merge_threshold,only_tipcoalescing, orUpdate::prune. - Not proposing to remove or weaken history retention by default — applications that need full history continue to use
Wollet::new. - Migration path for existing deployments is the application's responsibility: build a
WolletFullStateonce from a full-replayedWollet, persist it externally, then usefrom_statethereafter.
Alternatives considered
- Tuning
merge_threshold— addressed above; reduces disk and boot, not RAM. - Custom
Persisterthat fabricates a single syntheticUpdate— possible today, butUpdateis not a stable serialization target for materialized state, and the replay still goes throughapply_update_inner, paying the same allocation cost. A directCacheinjection is cleaner and explicit about the contract. - Lazy/streamed transaction loading inside
Cache— larger refactor, changes semantics of existing accessors, and not needed if the application can supply a pruned state up front.
Happy to send a PR if the direction is acceptable.
- 主要语言
- Rust
- 星标
- 114
- 派生
- 69
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
Blockstream/lwk 的其他 Issue
-
Ignore entry for RUSTSEC-2020-0168 no longer matches anything (`mach` is no longer in `Cargo.lock`)可能已有人在做 @zyl71 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 72/100
Blockstream/lwk#213 ·
-
wollet: Waterfalls scan sets `last_unused` to a page-relative index past the first page可能已有人在做 @diorwave 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
Blockstream/lwk#209 ·
-
C# bindings: all generated types are `internal`, `LiquidWalletKit` 0.8.2 has no usable public API可能已有人在做 @lisboaigor 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 25/100
Blockstream/lwk#210 ·
-
Applying a merged update with a txid in both `txid_height_new` and `txid_height_delete` drops its outputs from `unspent`可能已有人在做 @Jeon1691 于 4 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 78/100
Blockstream/lwk#205 ·
-
Cache `unspent` restores an input of a deleted transaction that a known replacement still spends未关闭
难度 3/5 1-2 天 新手友好度 68/100
Blockstream/lwk#204 ·
相似的 Issue
-
Ignore entry for RUSTSEC-2026-0002 no longer matches anything (`lru` is no longer in `Cargo.lock`)未关闭
难度 1/5 1 小时以内 新手友好度 72/100
influxdata/influxdb#27767 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 78/100
shell-pool/shpool#463 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 65/100
oras-project/rust-oci-client-node#132 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
libracoder/kiri#749 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
manuscript-lang/manuscript#6389 ·
维护者通常 1 天内回复