# Wallet and consolidation

> How the client selects and bounds its own UTXOs, and when it merges them.

Source: https://lithosprotocol.org/docs/configuration/wallet

**Level:** Advanced

*Assumes you know what a UTXO is and why a fragmented wallet is a problem.*

The `wallet` block controls how the client reads your node wallet, how many boxes it may hold in
flight, whether it splits your wallet when you first register, whether it merges small boxes back
together, and when it moves your lending rewards. These knobs help control how your wallet is used in the client,
and how to deal with UTXO growth inside of it. A miner with a fragmented wallet wants
different behaviour from one with a handful of large boxes.

**Most defaults reproduce the constant they replaced**, so leaving the block out changes little. The
following features ship on and act on your wallet by themselves:

- [Cancelling stale sends](#when-held-boxes-are-released) that your node dropped from its mempool.
- [Splitting your wallet](#splitting-your-wallet-when-you-first-register) once your first registration
  confirms.
- [Sweeping lending rewards](#sweeping-lending-rewards) to your first address.

## Selection and paging

| Setting | Default | Range | What it does |
|---|---|---|---|
| `wallet.max-inputs` | 75 | 1 to 4096 | Inputs one selection may take. Also the ceiling on a single consolidation |
| `wallet.page-size` | 500 | 1 to 10000 | Boxes per node read while walking the wallet |
| `wallet.max-input-bytes` | 4096 | 512 to 1048576 | Largest box the client will hydrate. Anything above is skipped, not fatal |
| `wallet.max-descriptors` | 2048 | 1 to 1000000 | Box descriptors held in the selection cache |
| `wallet.max-descriptor-bytes` | 1048576 | 1024 to 1073741824 | Memory that cache may occupy |

A wallet of large boxes reaches the byte bound before the count bound, which is why both exist.

## Timeouts

| Setting | Default | Range | What it does |
|---|---|---|---|
| `wallet.inventory-walk-timeout-ms` | 240000 | 1000 to 3600000 ms | Budget for one whole-inventory walk |
| `wallet.reservation-timeout-ms` | 30000 | 1000 to 600000 ms | How long a caller waits for funding |
| `wallet.node-call-timeout-ms` | 30000 | 1000 to 600000 ms | Whole-call deadline on the engine's node client, retries included |
| `wallet.node-read-timeout-ms` | 30000 | 1000 to 600000 ms | Idle-read deadline on the same client, per socket read |
| `wallet.node-max-response-bytes` | 2097152 | 65536 to 268435456 | Ceiling on one node response |

**An inventory walk fails rather than returning a prefix.** A partial answer would misreport what the
wallet holds, and consolidation would then act on the wrong total.

Funding is serialised on one thread, so `reservation-timeout-ms` bounds the queue behind a slow node
read rather than the read itself. Raise it before raising the pool if joins are timing out on a busy
node.

> **Warning: Three settings move together, and not in the direction people expect**
>
> `node-call-timeout-ms` is the one that bites a large wallet. A page of `/wallet/boxes/unspent` that
> takes longer than this fails outright, and no caller-side timeout can rescue it.
> 
> It is coupled to `page-size` backwards from intuition: **a bigger page means fewer calls but more work
> per call**, so raising `page-size` makes each call more likely to hit the timeout, and more likely to
> approach `node-max-response-bytes` as well. If you raise the page size on a slow node, raise all three.

## In-flight ceilings

| Setting | Default | Range | What it does |
|---|---|---|---|
| `wallet.max-wallet-inputs` | 512 | 1 to 100000 | Wallet inputs the engine may own at once across everything in flight |
| `wallet.max-optional-inputs` | 362 | 1 to 100000, and at most `max-wallet-inputs` | The share of that which optional work may take |

The gap between them is deliberate and it is a reserve. The default of 362 is exactly two full
transactions' worth below the ceiling, at `512 - 2 × 75`.

**That reserve is what a NISP submission or a fraud proof funds itself from while the optional queue is
saturated.** Without it a long consolidation pass could hold every input the engine owns, and a
submission would find nothing to fund a bond with.

Startup refuses `max-optional-inputs` above `max-wallet-inputs` and says so. Raising the optional share
is what lets a big consolidation pass hold more boxes at once, so raise `max-wallet-inputs` alongside
it or the reserve for critical work shrinks.

### Seeing which boxes are held

A box the client has taken for a transaction in flight, or for a transaction carried in your own
blocks, is held: nothing else can spend it until that work confirms or fails. `GET /wallet/balances`
reports the held boxes in the following fields:

| Field | What it reports |
|---|---|
| `reservedBoxes` | Wallet boxes held for transactions in flight or for your own blocks |
| `reservedNanoErgs` | The value of the held boxes, in nanoERG. Counts only boxes whose details the client has cached, so it can read low |

The spendable balance, `nanoErgs` on the same response, leaves held boxes out, and a join quote is
priced against it. If a quote reports a shortfall while your node wallet shows enough ERG, these two
fields show how much is held. See [Wallet balances](https://lithosprotocol.org/docs/api/get-wallet-balances).

### When held boxes are released

A box held for a send stays held until the chain shows what happened to that send. When a block spends
one of the send's inputs, the client releases its boxes, and those of every send built on it, without any
setting involved. The following settings cover a send that leaves your node's mempool with none of its
inputs spent, for example after a node restart:

| Setting | Default | Range | What it does |
|---|---|---|---|
| `wallet.reconcile.cancel-stale-sends` | `true` | `true` or `false` | Cancel such a send on chain |
| `wallet.reconcile.cancel-after-blocks` | 20 | 5 to 1440 | Blocks a send stays out of the mempool before it is cancelled |
| `wallet.reconcile.hold-ceiling-ms` | 21600000 | 3600000 to 604800000 ms | Longest a box stays held without proof of what happened to it |

To cancel a send, the client sends one of its wallet inputs back to your first address and pays the
0.001 ERG minimum fee. Once that transaction confirms, the old send can never be mined, and the client
frees the rest of its boxes. If the old send is mined first, the cancellation fails and costs nothing.
The client never cancels a NISP submission or a fraud proof.

**Past `hold-ceiling-ms`, the client settles the send from the chain alone.** It drops the inputs the
chain has spent and frees the unspent ones. If the send is mined after that, a freed box may also have
funded another transaction, and one of the two fails. The client logs every cancellation and every
release at the ceiling at `WARN`.

A send still in your node's mempool is never cancelled or released, however long it waits.

## Splitting your wallet when you first register

A wallet of one box makes every transaction wait on the change of the one before it. If your node drops
one of those transactions, every transaction built on its change is dropped too, a NISP submission
included. Once your first registration in the Miner Dictionary confirms, the client splits your largest
plain box into smaller ones, so a NISP submission, a fraud proof and a swap can each fund themselves
from a box of their own. [Committing your difficulty](https://lithosprotocol.org/docs/configuration/runtime#committing-your-difficulty)
explains registration. The following settings control the split:

| Setting | Default | Range | What it does |
|---|---|---|---|
| `wallet.seed.on-initial-commitment` | `true` | `true` or `false` | Split the wallet once your first registration confirms |
| `wallet.seed.target-utxos` | 32 | 2 to 256, and below `wallet.consolidation.target-utxos` while consolidation is on | Plain boxes to aim for, counting the box that is split |

A plain box holds no tokens and is not a mining reward. The split is one transaction that pays the
0.001 ERG minimum fee, and every new box stays at your first address. The client sizes the split by the
following rules:

- Each new box holds 0.05 ERG, or twice your NISP bond plus fees when that is more. [What submitting
  costs](../startup-intro.mdx#what-submitting-costs) explains how the bond is priced.
- Every plain box you already hold that can pay a fee and leave change counts toward `target-utxos`.
- Boxes no larger than a new box may hold at most half your plain ERG. A wallet under about 3 ERG
  therefore gets fewer boxes than the default target, and keeps one large box for large spends.
- The split box keeps at least one new box's worth of ERG.
- Only a box that is in a block and not held for another send can be split.

**The split runs once, and only for a new registration.** The client checks once a minute until your
registration confirms. It splits only a registration declared within the last 720 blocks, about a day
at Ergo's 2-minute block time, so a miner registered before that keeps the wallet as it is. A restart
within that day checks again and finds nothing to do once the wallet has reached its target.

If the split ends short of `target-utxos`, the client logs at `WARN` how much plain ERG a full split
needs. For a wallet holding one box of 1.2 ERG and new boxes of 0.05 ERG, the warning reads:

```text
Wallet seed ended at 13 of 32 plain boxes. A full seed needs about 3.100 ERG in plain boxes and this
wallet held 1.200 ERG. ERG added now is seeded only if the client restarts within about a day of
registering
```

To fill the rest, send the difference between the two figures to your first address in one
transaction, then restart the client within the day.
Like any box, a new box left unspent for four years owes up to about 0.1 ERG of storage rent.

Startup refuses a `target-utxos` at or above `wallet.consolidation.target-utxos` while consolidation
is on, because consolidation would merge the new boxes back together. With `on-initial-commitment` off,
the client never splits your wallet.

Arbitrage keeps its stake outside the wallet. With `batching.arb.autoReserve` on, the client sends the
stake from your wallet to your arb address when none is there, and that ERG leaves your wallet balance
until you reclaim it. [Funding the arb address](https://lithosprotocol.org/docs/configuration/batching#funding-the-arb-address) explains when
and how.

## Consolidation

Consolidation merges your own wallet boxes back together. It ships **off**, because a miner who never
fragments their wallet gains nothing from it.

| Setting | Default | Range | What it does |
|---|---|---|---|
| `wallet.consolidation.enabled` | `false` | `true` or `false` | Run consolidation at all |
| `wallet.consolidation.target-utxos` | 500 | 1 to 2147483647 | Wallet size to aim for |
| `wallet.consolidation.interval-ms` | 300000 | 1000 to 86400000 ms | Shortest gap between passes |
| `wallet.consolidation.min-inputs` | 100 | 2 to 4096 | Inputs a pass must be able to merge before it is worth a fee |
| `wallet.consolidation.num-transactions` | 1 | 1 to 1000 | Independent consolidations one pass may build |
| `wallet.consolidation.attempt-timeout-ms` | 300000 | 10000 to 86400000 ms, and longer than the inventory walk | How long one pass may take end to end |
| `wallet.consolidation.request-timeout-ms` | 40000 | 1000 to 600000 ms | How long a pass waits on the engine |

Above `target-utxos` one pass merges the excess. At or below it nothing happens.

**It is the lowest-priority work the engine does.** It runs only when nothing else is in flight, so
`interval-ms` is a floor rather than a schedule, and a busy client may go much longer between passes.

`min-inputs` exists because merging two boxes pays a fee to remove one box. Anything below the floor is
not worth building.

### Reducing a wallet of thousands

One transaction removes at most `max-inputs - 1` boxes, which is 74 at the defaults. A wallet of
several thousand boxes would take many passes at one transaction each.

`num-transactions` is the lever. Each consolidation spends its own boxes and pays its own fee, and
**they do not chain**, so a rejection costs only its own transaction rather than invalidating the rest.

> **Warning: The attempt timeout must exceed the inventory walk**
>
> A pass includes a whole wallet walk, then signing, then every send. The walk alone may take
> `inventory-walk-timeout-ms`, so an attempt budget at or below that expires before it can send anything.
> Startup refuses the pair:
> 
> ```text
> wallet.consolidation.attempt-timeout-ms
>     240000 is not longer than wallet.inventory-walk-timeout-ms (240000), so a pass whose
>     wallet walk takes that long expires before it can send
> ```

## Sweeping lending rewards

When a block spends one of your collateral boxes, the coinbase pays you 3 ERG plus that block's
transaction fees. The coinbase is locked for 720 blocks, about a day at Ergo's 2-minute block time.
Once it unlocks, it still pays for nothing until it is moved, because the client never funds a
transaction from a reward box directly. The reward sweep moves every unlocked reward to your first
address, EIP-3 index 0, where it becomes spendable ERG:

| Setting | Default | Range | What it does |
|---|---|---|---|
| `wallet.reward-sweep.enabled` | `true` | `true` or `false` | Sweep unlocked rewards on a timer |
| `wallet.reward-sweep.interval-ms` | 1800000 | 60000 to 86400000 ms | Shortest gap between sweeps |

The client considers a sweep each time it refreshes its view of the wallet, about every ten minutes,
so `interval-ms` is a floor rather than a schedule. A sweep starts when the interval has passed, no
other sweep is running, and at least one reward has unlocked. The client logs each sweep at `INFO`
with the box count.

A sweep takes the smallest rewards first, up to `max-inputs` per transaction, so dust merges into the
early transactions. Each transaction pays the 0.001 ERG minimum fee.

**Claim** on the web panel's Wallet page or Collateral portfolio, or `POST /wallet/rewards/claim`,
sweeps at once whatever `interval-ms` says. With the sweep off, unlocked rewards stay where they are
until you claim them. See [Claim unlocked block rewards](https://lithosprotocol.org/docs/api/claim-wallet-rewards).

## Next steps

- [Collateral and emission](https://lithosprotocol.org/docs/configuration/emission): the work that spends what this wallet selects.
- [Synchronization and storage](https://lithosprotocol.org/docs/configuration/sync): the other large background subsystem.
