# Collateral and emission

> Driving the collateral queue, and putting your own ERG and LIT into it.

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

**Level:** Advanced

*Assumes you have read how collateral and the permit work in [Lithos Protocol](https://lithosprotocol.org/docs/intro).*

The `emission` block covers two different jobs that share a timer. The first is **driving the queue**,
which every client does and which costs only transaction fees. The second is **joining the queue with
your own funds**, which locks ERG and LIT until a miner spends the box, and which ships off.

This is maintenance that runs whether or not you ever mine a block.

> **Danger: A queued position cannot be withdrawn**
>
> Once your ERG and permit are in the queue, neither returns until a block spends the box. There is no
> cancel path, by design. Everything under [Lending your own funds](#lending-your-own-funds) is a
> commitment rather than a setting you can back out of.

## Driving the queue

| Setting | Default | Range | What it does |
|---|---|---|---|
| `emission.enabled` | `true` | `true` or `false` | Drive the collateral queue at all |
| `emission.queueInterval` | 240000 | 1000 to 3600000 ms | How often to look at the head of the queue |
| `emission.maxQueueSpends` | 4 | 1 to 128 | Emission spends chained in one pass, which is how deep into the queue a pass reaches |
| `emission.queueScanLimit` | 500 | 1 to 100000 | Queue boxes paged through when looking for the head |
| `emission.txFee` | 1000000 | The network minimum fee to 1000000000 nanoERG | Fee on emission transactions |

Activating the head of the queue turns a waiting position into a live collateral box, which is what
keeps collateral boxes available for everyone to mine against. Clearing removes a box whose lender key
is already active and which would otherwise hold the whole queue still.

**Both are permissionless, and clients are encouraged to keep this on so that pool liveness is mainained.** 

`txFee` is floored at the network minimum because a transaction below it will not relay.

## Queue work in your own blocks

When you mine with inserted transactions on, your block also carries queue spends without a fee,
built from the confirmed chain. The following setting caps them:

| Setting | Default | Range | What it does |
|---|---|---|---|
| `emission.candidateActivates` | 5 | 0 to 100 | Activates your block carries when there is no Clear to take |

What your block carries depends on what is already waiting in the mempool, as the following table shows:

| Waiting in the mempool | Your block carries |
|---|---|
| No queue spend | Activates up to `candidateActivates`, for one block. A block later the queue pass sends them with a fee instead |
| Anything, with a Clear within reach | Every queue spend up to the `emissions` source's `maxTxs`, replacing what is waiting |
| Only joins | Nothing. The queue pass runs straight away, so its Activates follow the joins |
| Other queue spends | Nothing. Your node adds the waiting ones to the block itself |

**A Clear pays whoever builds it** the forfeited queue box, about 2.9 ERG plus its permit. That is
why your block takes one even when another client has already sent theirs. Joins waiting in the
mempool are left out of that block, and their clients send them again. Nobody's principal is at risk.

While the active set is still filling, each Activate takes about 7 KB of your block. That is why
`candidateActivates` sits below the `emissions` source's `maxTxs` of 20, which Clears may fill.

## Chaining off the mempool

The following setting decides how far a queue pass looks into the mempool:

| Setting | Default | Range | What it does |
|---|---|---|---|
| `emission.maxChainDepth` | 16 | 0 to 4096 | Unconfirmed spends of the emission box to follow |

Queue passes always build on the newest emission box, confirmed or not. Following the unconfirmed
spends behind it is what lets a pass activate queue boxes that joins still in the mempool created.
Boxes further along the chain are not seen until they confirm.

**The unconfirmed spends being chained onto belong to other lenders.** A queue pass chains onto them
and never replaces them in the mempool.

## Lending your own funds

| Setting | Default | Range | What it does |
|---|---|---|---|
| `emission.autoCollateralize` | `false` | `true` or `false` | Put your own ERG and LIT into the queue |
| `emission.collateralizeInterval` | 900000 | 1000 to 86400000 ms | How often to check whether more collateral is wanted |
| `emission.maxJoinsPerRun` | 5 | 1 to 128 | Queue boxes created in one pass |
| `emission.maxOwnCollateral` | 10 | 0 to 2000 | Your boxes live at once, across the queue, live collateral and the active set |
| `emission.maxLenderKeys` | 32 | 1 to 1000, and at most `node.numAddresses` | Wallet addresses dedicated to lending |
| `emission.maxPermitPerJoin` | 40000000000000 | Any positive value, in LIT base units | The most LIT to post as a permit on one box |
| `emission.priorityFeeNanoErgs` | 0 | `0`, or 1000000 to 3000000000 nanoERG | ERG each position adds above the 2.915 ERG floor, to be spent sooner |

### What a position costs

Each live box locks **2.915 ERG plus its permit** until a miner spends it. So `maxOwnCollateral` is
your total exposure ceiling, not a throughput setting: at the default of 10 you can have about 29 ERG
plus ten permits committed at once.

`maxPermitPerJoin` is a **price you are willing to pay, not a fee**. The permit rises with the queue
backlog, from a floor of 2,000 LIT toward a ceiling of 40,000. When the thermostat passes your cap, the
run stops rather than paying it. The default is the protocol ceiling itself, so out of the box price
never blocks a join.

The value is in base units at 9 decimals, so 40,000 LIT is `40000000000000`.

### Paying for priority

A miner who finds a block spends the collateral box that pays them most, so a position that offers
more is spent ahead of the ones that offer nothing. `priorityFeeNanoErgs` is that offer, for the
positions `autoCollateralize` creates. A join from the web panel sets its own. Each position then locks
`2.915 ERG + priorityFeeNanoErgs`, and when a miner spends it the fee divides as follows:

| Share | Goes to |
|---|---|
| One fifth | The miner who spends the box |
| Four fifths | The rollup that block opens, paid to every miner who submits to it |

Because transaction fees in a Lithos block go to the lender, paying for priority works like a fee
future: you pay to be spent sooner, for example when you expect the blocks ahead to carry high fees.

**A position that offers nothing is still spent eventually.** Miners running this client draw any box
that has been live for 7200 blocks, about 10 days on mainnet, ahead of every priority fee. Each miner
can change that age with
[`stratum.candidate.clearanceAge`](https://lithosprotocol.org/docs/configuration/candidate#which-box-your-block-spends), so treat it as
typical rather than guaranteed.

`0` posts at the floor and offers nothing. Any other value must be at least `1000000` (0.001 ERG), so
the miner's fifth is worth more than the output it arrives in. Miners running this client compare
priority fees in [steps of 0.001 ERG](https://lithosprotocol.org/docs/configuration/candidate#which-box-your-block-spends), so a fee between
two steps ranks with the lower one.

> **Warning: Past 0.085 ERG the block reward no longer covers your principal**
>
> The lender receives the 3 ERG block reward plus the block's transaction fees. Above `85000000`
> (0.085 ERG) the priority fee takes the principal past 3 ERG, so getting it all back depends on the
> fees in the block that spends your box. The client allows it, and logs a warning on every
> self-collateralization pass that posts above this.

### One key per box, and why it matters

The active set holds distinct **keys**. One live box needs one address, and a key already in the set
produces a box nobody can activate.

> **Danger: A reused lender key forfeits the whole box**
>
> A queue box whose key is already active can only be cleared, and clearing forfeits both the principal
> and the permit to whoever builds the clearing transaction. This is why `maxLenderKeys` may not exceed
> `node.numAddresses`, and why the client
> [refuses a manual join while `autoCollateralize` is on](#why-a-join-answers-409):
> the automated pass owns key selection and a manual join could take a key it is already using.

Startup checks the pair and names the value to raise:

```text
emission.maxLenderKeys
    64 exceeds node.numAddresses (32). Addresses past index 31 would receive funds this
    client cannot sign for. Raise node.numAddresses to at least 64
```

### Where lender keys come from

Each lender key is an address in your Ergo node's wallet. After startup, the client creates addresses
through the node wallet until the wallet holds `maxLenderKeys` of them, and logs how many it created.
If that fails, each later queue pass tries again until the wallet holds them all.

Creating a key needs the node wallet unlocked. The client unlocks it with `node.pass`, so you do not
unlock it by hand after a node restart. See
[The client unlocks the node wallet itself](https://lithosprotocol.org/docs/configuration/node#the-client-unlocks-the-node-wallet-itself).

## Joining by hand

You can join the queue yourself from the **Join** tab of the Collateral web panel, or with
`POST /collateral/join`. Quote the join first, which the panel does as you edit it, or with
`POST /collateral/join/check`. The quote reports whether the client accepts the join, in the following
fields:

| Field | What it reports |
|---|---|
| `manualJoinAllowed` | Whether `POST /collateral/join` accepts this join now |
| `blockedReason` | Why not, when `manualJoinAllowed` is `false` |
| `fitting` | The most positions a join accepts right now |
| `freeLenderKeys` | Lender keys free to take a position without creating a new one |
| `keysDerivable` | Lender keys the client may still create, up to `maxLenderKeys` |

**A join is all or nothing.** If fewer positions fit than you ask for, the client sends nothing and
answers `422`, with the error detail ending in `fitting=N`. Ask again for at most N positions.

When more than one reason applies, the quote reports the first in the following order:

| `blockedReason` | Cause | What to do |
|---|---|---|
| `AUTO_COLLATERALIZE` | `autoCollateralize` is on | Set it to `false` and restart the client |
| `BUDGET_EXHAUSTED` | `maxOwnCollateral` leaves room for fewer positions than you asked for | Ask for fewer, or raise `maxOwnCollateral` |
| `WALLET_LOCKED` | The join needs new lender keys, and the client could not unlock the node wallet to create them | Read the warning in the client log, and check `node.pass` |
| `NO_LENDER_KEYS` | Free and creatable keys together cover fewer positions than you asked for | Ask for fewer, or raise `maxLenderKeys` |
| `INSUFFICIENT_FUNDS` | The spendable balance cannot cover the join | Top up the wallet, or wait for [held boxes](https://lithosprotocol.org/docs/configuration/wallet#seeing-which-boxes-are-held) to clear |

See [Quote joining the queue](https://lithosprotocol.org/docs/api/check-collateral-join) and
[Join the collateral queue](https://lithosprotocol.org/docs/api/join-collateral-queue) for every field.

### Why a join answers 409

While `emission.autoCollateralize` is `true`, `POST /collateral/join` answers `409` and sends nothing,
and every quote reports `AUTO_COLLATERALIZE`. The automated pass owns lender-key selection, and a join
made by hand could take a key it is already using. To join by hand, set `autoCollateralize = false`
and restart the client.

With `autoCollateralize` off, a `409` means the emission box moved between the quote and the send.
Quote again and resend.

## Next steps

- [Wallet and consolidation](https://lithosprotocol.org/docs/configuration/wallet): where the ERG for these transactions comes from.
- [Node connection and security](https://lithosprotocol.org/docs/configuration/node): `node.numAddresses`, which caps your lender keys.
