Commit Graph

54 Commits

Author SHA1 Message Date
SomberNight 3d39074294 verifier.py: fix CVE-2012-2459: reject left-sibling duplicates
Due to how the txid-commitment merkle tree used in the block headers is constructed, we need an extra check to be able to validate the *position* of a txid in a block.

I think this is low severity for us.

See https://bitcointalk.org/?topic=102395 :

> The Merkle hash implementation that Bitcoin uses to calculate the Merkle
> root in a block header is flawed in that one can easily construct multiple
> lists of hashes that map to the same Merkle root.
> For example, merkle_hash([a, b, c]) and merkle_hash([a, b, c, c]) yield
> the same result. This is because, at every iteration, the Merkle hash
> function pads its intermediate list of hashes with the last hash if the
> list is of odd length, in order to make it of even length.
>
> And so, the Merkle root function can be effectively preimaged by
> changing the input so that one of the intermediate lists is of even
> length with the last two elements equal (where originally it was
> of odd length with a last element equal to the earlier mentioned two).
> As was later noted, this extends to any input length that is
> not a power of two:
> merkle_hash([a, b, c, d, e, f]) == merkle_hash([a, b, c, d, e, f, e, f]).
> Note that to maintain the same root hash, the only flexibility that
> exists is duplication of elements.

Ported from https://github.com/Electron-Cash/Electron-Cash/commit/165146362b4cb0ad74770b36aca1f9acb2800195

Co-authored-by: bitcoincashautist <80100588+A60AB5450353F40E@users.noreply.github.com>
2026-04-03 02:22:01 +00:00
SomberNight b944371ffd adb: change API of util.TxMinedInfo: height() is now always SPV-ed 2025-09-24 13:46:24 +00:00
SomberNight 6a56fd756b interface: split request_chunk, based on "can_return_early" param 2025-07-15 22:53:35 +00:00
SomberNight 5fad4bff8f verifier: fix off-by-one for max_checkpoint
if a wallet had a tx mined in the max_checkpoint block, in certain cases
we would leave it forever in the "unverified" state and remain stuck in "synchronizing..."
2025-07-15 22:35:21 +00:00
SomberNight 27599ac537 interface: small clean-up. intro ChainResolutionMode.
- type hints
- minor API changes
- no functional changes
2025-06-06 16:42:15 +00:00
SomberNight fc093f8a93 verifier.py: fix typo 2025-02-28 16:46:29 +00:00
SomberNight 24980feab7 config: introduce ConfigVars
A new config API is introduced, and ~all of the codebase is adapted to it.
The old API is kept but mainly only for dynamic usage where its extra flexibility is needed.

Using examples, the old config API looked this:
```
>>> config.get("request_expiry", 86400)
604800
>>> config.set_key("request_expiry", 86400)
>>>
```

The new config API instead:
```
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS
604800
>>> config.WALLET_PAYREQ_EXPIRY_SECONDS = 86400
>>>
```

The old API operated on arbitrary string keys, the new one uses
a static ~enum-like list of variables.

With the new API:
- there is a single centralised list of config variables, as opposed to
  these being scattered all over
- no more duplication of default values (in the getters)
- there is now some (minimal for now) type-validation/conversion for
  the config values

closes https://github.com/spesmilo/electrum/pull/5640
closes https://github.com/spesmilo/electrum/pull/5649

Note: there is yet a third API added here, for certain niche/abstract use-cases,
where we need a reference to the config variable itself.
It should only be used when needed:
```
>>> var = config.cv.WALLET_PAYREQ_EXPIRY_SECONDS
>>> var
<ConfigVarWithConfig key='request_expiry'>
>>> var.get()
604800
>>> var.set(3600)
>>> var.get_default_value()
86400
>>> var.is_set()
True
>>> var.is_modifiable()
True
```
2023-05-25 17:39:48 +00:00
SomberNight 312f2641e7 don't use bare except
use "except Exception", or if really needed explicitly "except BaseException"
2023-04-24 12:58:01 +00:00
SomberNight 373db76ac9 util: kill bh2u
no longer useful, and the name is so confusing...
2023-02-17 11:43:11 +00:00
SomberNight 428dff90f8 address_sync: include verifier in sync_state progress indicator 2022-04-08 20:36:45 +02:00
SomberNight a05ef140d6 address_sync: split off unconfirmed_tx from unverified_tx 2022-04-08 20:34:45 +02:00
SomberNight 250795f137 verifier: small clean-up
this code runs in the interface's group, so it's confusing to call the network's methods
2022-04-04 20:55:47 +02:00
SomberNight 1dbff51fce synchronizer: fix rare race where synchronizer could get stuck 2021-03-05 20:46:41 +01:00
SomberNight bf7129d57e synchronizer/verifier: ensure fairness between wallets (follow-up)
follow-up to https://github.com/spesmilo/electrum/commit/4346d2fc765f40a9388108c06cf1ee132d663923

It's not just about the Synchronizer, the Verifier should not starve other jobs either...
(previously I thought the Verifier is not too important as it only makes
requests if there are new txs; however with LNWatcher its progress is not persisted)
2021-03-01 13:08:01 +01:00
SomberNight ed234d3444 rename all TaskGroup() fields to "taskgroup"
for consistency
2020-02-27 19:13:56 +01:00
SomberNight 4f5f949979 follow-up prev
Clean up tests a bit, and rm some of them.
It's overkill to test with SPV._raise_if_valid_tx mocked out.
2020-02-25 20:45:17 +01:00
zebra-lucky d4f7c207a7 fix SPV.hash_merkle_root, add tests/test_verifier.py 2020-02-25 20:06:11 +02:00
ThomasV 38f1436d78 post rebase fixes 2019-08-20 09:03:11 +02:00
SomberNight 17ccb79ca4 channel verifier: NetworkJobOnDefaultServer, and some error handling 2019-08-20 09:03:10 +02:00
SomberNight c91fe27e7d fix lnwatcher: network triggers were renamed 2019-08-20 09:03:10 +02:00
SomberNight f60f690ca9 change many str(e) to repr(e) as some exceptions were cryptic
it's often valuable to see the type of the exception
(especially as for some exceptions str(e) == '')
2019-07-17 20:12:52 +02:00
SomberNight 9b82321fc0 verifier: further sanity checks for SPV verification.
Thanks to @JeremyRand
2019-07-05 18:39:40 +02:00
SomberNight a7b13f4876 logging: make console log lines shorter 2019-05-02 15:19:11 +02:00
SomberNight 3385a94753 logging: basics 2019-05-02 15:19:03 +02:00
SomberNight ecefa47b40 verifier: trivial clean-up 2019-04-12 20:29:05 +02:00
SomberNight bca6ad5241 verifier: fix logic bug. after reorg, some verifs were not undone
after a reorg, in a many fork/orphan chains scenario,
we would sometimes not undo SPV for enough blocks

functions in blockchain.py somewhat based on kyuupichan/bitcoinX@5126bd15ef
2019-03-26 21:01:43 +01:00
ThomasV 791e680a96 abstract database away from wallet and address_synchronizer 2019-02-28 09:02:58 +01:00
SomberNight 38ab7ee554 network: catch untrusted exceptions from server in public methods
and re-raise a wrapper exception (that retains the original exc in a field)

closes #5111
2019-02-12 17:02:15 +01:00
chris-belcher d5c8a0e0d0 Add flag --skipmerklecheck (#4957)
The --skipmerklecheck optional flag makes Electrum tolerate invalid
merkle proofs from the server. This is useful for building Electrum
servers that need a minimum amount of storage, though of course users
should only enable it if they completely trust the connected server.
2019-01-16 18:48:10 +01:00
SomberNight c017f788ac wallet: TxMinedInfo (merged TxMinedStatus and VerifiedTxInfo) 2018-12-07 20:47:28 +01:00
SomberNight 082a83dd85 rename crypto.Hash to sha256d 2018-10-25 22:28:24 +02:00
SomberNight a88a2dea82 split bip32 from bitcoin.py 2018-10-25 22:20:33 +02:00
SomberNight 81cc20039e more type annotations in core lib 2018-10-22 16:41:25 +02:00
SomberNight e8bc025f5c verifier: fix race in __init__ 2018-10-19 18:10:04 +02:00
SomberNight 372921b423 mv NetworkJobOnDefaultServer to util
break ref cycles
2018-10-12 16:09:41 +02:00
SomberNight 37206ec08e network: auto-switch servers to preferred fork (or longest chain)
If auto_connect is enabled, allow jumping between forks too.
(Previously auto_connect was only switching servers on a given fork,
not across forks)
If there is a preferred fork set, jump to that (and stay);
if there isn't, always jump to the longest fork.
2018-10-11 20:07:19 +02:00
SomberNight 02f108d927 restructure synchronizer
fix CLI notify cmd. fix merchant websockets.
2018-10-03 17:13:46 +02:00
SomberNight 952e9b87e1 network: clean-up. make external API clear. rm interface_lock (mostly). 2018-09-25 16:44:39 +02:00
SomberNight 1b95cced5d verifier: perf optimisations
blockchain.read_header is expensive. do cheap tests first
on a wallet with 11k txns, that is synced except for spv proofs,
finishing sync now takes 80 sec instead of 180 sec
2018-09-17 18:31:25 +02:00
SomberNight aee2d8e120 verifier: fix a race during reorgs
related: 41e088693d
If our guess of a txn getting confirmed at the same height in the new chain
as it was at in the old chain is incorrect, there is a race between the
verifier and the synchronizer. If the verifier wins, the exception will cause
us to disconnect.
2018-09-17 03:35:25 +02:00
SomberNight dcab22dcc7 verifier: small clean-up 2018-09-16 22:21:49 +02:00
SomberNight 819044221b verifier: need to wait for reorg
fixes race between verifier and block header download.
scenario: client starts, connects to server. while client was offline,
there was a reorg. txn A was not mined in the old chain, but is mined
after reorg. client subscribes to addresses and starts downloading headers,
concurrently. server tells client txn A is mined at height H >= reorg height.
client sees it has block header at height H, asks for SPV proof for txn A.
but the header the client has is still the old one, the verifier was faster
than the block header download (race...). client receives proof. proof is
incorrect for old header. client disconnects.
2018-09-13 19:00:21 +02:00
SomberNight e829d6bbcf wallet: put Sync and Verifier in their own TaskGroup, and that into interface.group 2018-09-11 20:24:01 +02:00
SomberNight 77d86f074f verifier: don't try to request same chunk multiple times 2018-09-08 19:11:02 +02:00
SomberNight c49e563881 verifier: if we fail to verify SPV proof, disconnect from server 2018-09-08 18:10:21 +02:00
SomberNight 26172686b8 restructure synchronizer/verifier <--> interface coupling 2018-09-07 19:34:28 +02:00
SomberNight 2187615c08 verifier: request proofs in batches 2018-09-06 14:17:43 +02:00
Janus e9ceeb85af async block headers 2018-09-06 14:17:41 +02:00
Janus 8f36c9167d aiorpcx: remove callback based code, add session to Interface 2018-09-06 14:17:29 +02:00
SomberNight 531cdeffa9 blockchain.py: rename 'checkpoint' to 'forkpoint' 2018-08-03 18:25:53 +02:00