Browse Source

reorganize with_lock decorator

patch-4
bitromortac 4 years ago
parent
commit
63308f94a0
No known key found for this signature in database GPG Key ID: 1965063FC13BEBE2
  1. 8
      electrum/address_synchronizer.py
  2. 8
      electrum/blockchain.py
  3. 8
      electrum/lnhtlc.py
  4. 12
      electrum/util.py

8
electrum/address_synchronizer.py

@ -32,7 +32,7 @@ from aiorpcx import TaskGroup
from . import bitcoin, util
from .bitcoin import COINBASE_MATURITY
from .util import profiler, bfh, TxMinedInfo, UnrelatedTransactionException
from .util import profiler, bfh, TxMinedInfo, UnrelatedTransactionException, with_lock
from .transaction import Transaction, TxOutput, TxInput, PartialTxInput, TxOutpoint, PartialTransaction
from .synchronizer import Synchronizer
from .verifier import SPV
@ -98,12 +98,6 @@ class AddressSynchronizer(Logger):
self.load_and_cleanup()
def with_lock(func):
def func_wrapper(self: 'AddressSynchronizer', *args, **kwargs):
with self.lock:
return func(self, *args, **kwargs)
return func_wrapper
def with_transaction_lock(func):
def func_wrapper(self: 'AddressSynchronizer', *args, **kwargs):
with self.transaction_lock:

8
electrum/blockchain.py

@ -29,7 +29,7 @@ from . import util
from .bitcoin import hash_encode, int_to_hex, rev_hex
from .crypto import sha256d
from . import constants
from .util import bfh, bh2u
from .util import bfh, bh2u, with_lock
from .simple_config import SimpleConfig
from .logging import get_logger, Logger
@ -195,12 +195,6 @@ class Blockchain(Logger):
self.lock = threading.RLock()
self.update_size()
def with_lock(func):
def func_wrapper(self, *args, **kwargs):
with self.lock:
return func(self, *args, **kwargs)
return func_wrapper
@property
def checkpoints(self):
return constants.net.CHECKPOINTS

8
electrum/lnhtlc.py

@ -3,7 +3,7 @@ from typing import Optional, Sequence, Tuple, List, Dict, TYPE_CHECKING, Set
import threading
from .lnutil import SENT, RECEIVED, LOCAL, REMOTE, HTLCOwner, UpdateAddHtlc, Direction, FeeUpdate
from .util import bh2u, bfh
from .util import bh2u, bfh, with_lock
if TYPE_CHECKING:
from .json_db import StoredDict
@ -50,12 +50,6 @@ class HTLCManager:
self._init_maybe_active_htlc_ids()
def with_lock(func):
def func_wrapper(self, *args, **kwargs):
with self.lock:
return func(self, *args, **kwargs)
return func_wrapper
@with_lock
def ctn_latest(self, sub: HTLCOwner) -> int:
"""Return the ctn for the latest (newest that has a valid sig) ctx of sub"""

12
electrum/util.py

@ -776,7 +776,7 @@ mainnet_block_explorers = {
'mempool.space': ('https://mempool.space/',
{'tx': 'tx/', 'addr': 'address/'}),
'mempool.emzy.de': ('https://mempool.emzy.de/',
{'tx': 'tx/', 'addr': 'address/'}),
{'tx': 'tx/', 'addr': 'address/'}),
'OXT.me': ('https://oxt.me/',
{'tx': 'transaction/', 'addr': 'address/'}),
'smartbit.com.au': ('https://www.smartbit.com.au/',
@ -797,7 +797,7 @@ testnet_block_explorers = {
'Blockstream.info': ('https://blockstream.info/testnet/',
{'tx': 'tx/', 'addr': 'address/'}),
'mempool.space': ('https://mempool.space/testnet/',
{'tx': 'tx/', 'addr': 'address/'}),
{'tx': 'tx/', 'addr': 'address/'}),
'smartbit.com.au': ('https://testnet.smartbit.com.au/',
{'tx': 'tx/', 'addr': 'address/'}),
'system default': ('blockchain://000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943/',
@ -1116,6 +1116,14 @@ def ignore_exceptions(func):
return wrapper
def with_lock(func):
"""Decorator to enforce a lock on a function call."""
def func_wrapper(self, *args, **kwargs):
with self.lock:
return func(self, *args, **kwargs)
return func_wrapper
class TxMinedInfo(NamedTuple):
height: int # height of block that mined tx
conf: Optional[int] = None # number of confirmations, SPV verified (None means unknown)

Loading…
Cancel
Save