jmwallet.history_reconstruction
jmwallet.history_reconstruction
On-chain CoinJoin history reconstruction for imported wallets.
A wallet recovered from seed has no local history.csv: every past
CoinJoin, send, and deposit is invisible to jm-wallet history even though
the transactions are fully recoverable from chain data. This module rebuilds a
best-effort history the same way the legacy joinmarket-clientserver
wallet-tool history command classifies transactions on the fly, but
persists the result as regular history rows tagged source="onchain" so
they are clearly distinguishable from authoritative protocol-time rows.
Heuristics (all pure functions of on-chain data):
- A transaction is a CoinJoin when its output structure matches the
equal-output pattern (:func:
jmcore.bitcoin.analyze_coinjoin_outputs). - Our role in a CoinJoin follows from the net value change of our coins:
a maker earns a fee (
net >= 0), a taker pays fees (net < 0). - Maker
fee_receivedis the net gain (cjfee earned minus the mining-fee contribution; the two cannot be separated on-chain). - Taker fees lump maker fees and our mining-fee share together in
total_maker_fees_paid(again inseparable on-chain). - Non-CoinJoin transactions become
send(we funded inputs) ordeposit(we only received) rows.
Known limitations: counterparty nicks are unknowable; a maker whose cjfee was smaller than its mining-fee contribution is misclassified as taker; the per-transaction mining fee of a CoinJoin cannot be split from maker fees.
Attributes
Classes
ClassifiedTransaction
dataclass
Result of classifying a single wallet transaction from chain data.
Source code in jmwallet/src/jmwallet/history_reconstruction.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | |
Attributes
change_address: str
instance-attribute
cj_amount: int
instance-attribute
destination_address: str
instance-attribute
fee_received: int
instance-attribute
mining_fee_paid: int
instance-attribute
net_fee: int
instance-attribute
peer_count: int | None
instance-attribute
role: HistoryRole
instance-attribute
source_addresses: str
instance-attribute
source_mixdepth: int
instance-attribute
total_maker_fees_paid: int
instance-attribute
utxos_used: str
instance-attribute
OwnedInput
dataclass
One of our coins spent by a transaction (prevout resolved locally).
Source code in jmwallet/src/jmwallet/history_reconstruction.py
60 61 62 63 64 65 66 67 68 | |
Attributes
address: str
instance-attribute
mixdepth: int
instance-attribute
txid: str
instance-attribute
value: int
instance-attribute
vout: int
instance-attribute
OwnedOutput
dataclass
One of our coins created by a transaction.
Source code in jmwallet/src/jmwallet/history_reconstruction.py
71 72 73 74 75 76 77 78 79 | |
Attributes
address: str
instance-attribute
is_external: bool
instance-attribute
mixdepth: int
instance-attribute
value: int
instance-attribute
vout: int
instance-attribute
ReconstructionResult
dataclass
Outcome of a reconstruction pass.
Source code in jmwallet/src/jmwallet/history_reconstruction.py
100 101 102 103 104 105 106 107 | |
Attributes
capped: bool = False
class-attribute
instance-attribute
created: int = 0
class-attribute
instance-attribute
scanned: int = 0
class-attribute
instance-attribute
skipped_existing: int = 0
class-attribute
instance-attribute
Functions:
classify_wallet_transaction(parsed: ParsedTransaction, owned_inputs: list[OwnedInput], owned_outputs: list[OwnedOutput], all_inputs_ours: bool, network: str = 'mainnet') -> ClassifiedTransaction | None
Classify a wallet transaction into a history role with inferred fees.
Args: parsed: The parsed transaction. owned_inputs: Our inputs (prevouts resolved to our addresses). owned_outputs: Our outputs. all_inputs_ours: True when every input of the transaction spends one of our coins (lets us compute the exact mining fee for sends). network: Network name (for foreign-script address rendering).
Returns:
A :class:ClassifiedTransaction, or None when the transaction
does not involve the wallet at all.
Source code in jmwallet/src/jmwallet/history_reconstruction.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
reconstruct_history_from_chain(backend: BlockchainBackend, *, address_paths: Mapping[str, tuple[int, int, int]], network: str, wallet_fingerprint: str, data_dir: Path, max_transactions: int = 1000) -> ReconstructionResult
async
Reconstruct history rows from chain data and persist them.
Enumerates every confirmed wallet transaction via the backend's
transaction enumeration (listsinceblock on Bitcoin Core; the watched
transaction log on neutrino-api 1.4.0+), classifies each with
:func:classify_wallet_transaction, and appends a source="onchain"
row per transaction that is not already recorded for this wallet.
Protocol-time rows always win: a txid already present in the wallet's
history (whatever its source) is never touched (issue #517 policy).
Args:
backend: Blockchain backend (must support transaction enumeration).
address_paths: Mapping of wallet address -> (mixdepth, change,
index) used to recognize our scripts (typically
WalletService.address_cache after a sync).
network: Network name recorded on the entries.
wallet_fingerprint: Wallet scoping fingerprint for the entries.
data_dir: Data directory holding history.csv.
max_transactions: Safety cap on transactions classified in one pass.
Returns:
A :class:ReconstructionResult with pass statistics.
Source code in jmwallet/src/jmwallet/history_reconstruction.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |