electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit e8ee4250d9b46466478a186ee23af4383c29875e
parent 8d02c2027c8a63eeff6bc3a15f5a64ff0ec3f400
Author: ThomasV <thomasv@electrum.org>
Date:   Sun, 23 Feb 2020 20:35:03 +0100

Do not save new channels before they are added to lnworker

Diffstat:
Melectrum/json_db.py | 8+++++++-
Melectrum/lnpeer.py | 8+++-----
Melectrum/lnworker.py | 7++++++-
3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/electrum/json_db.py b/electrum/json_db.py @@ -86,9 +86,15 @@ class StoredDict(dict): # early return to prevent unnecessary disk writes if not is_new and self[key] == v: return + # recursively set db and path + if isinstance(v, StoredDict): + v.db = self.db + v.path = self.path + [key] + for k, vv in v.items(): + v[k] = vv # recursively convert dict to StoredDict. # _convert_dict is called breadth-first - if isinstance(v, dict): + elif isinstance(v, dict): if self.db: v = self.db._convert_dict(self.path, key, v) v = StoredDict(v, self.db, self.path + [key]) diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py @@ -48,6 +48,7 @@ from .lnmsg import encode_msg, decode_msg from .interface import GracefulDisconnect, NetworkException from .lnrouter import fee_for_edge_msat from .lnutil import ln_dummy_address +from .json_db import StoredDict if TYPE_CHECKING: from .lnworker import LNWorker, LNGossip, LNWallet @@ -620,10 +621,7 @@ class Peer(Logger): "revocation_store": {}, "static_remotekey_enabled": self.is_static_remotekey(), # stored because it cannot be "downgraded", per BOLT2 } - channel_id = chan_dict.get('channel_id') - channels = self.lnworker.db.get_dict('channels') - channels[channel_id] = chan_dict - return channels.get(channel_id) + return StoredDict(chan_dict, None, []) async def on_open_channel(self, payload): # payload['channel_flags'] @@ -695,7 +693,7 @@ class Peer(Logger): ) chan.open_with_first_pcp(payload['first_per_commitment_point'], remote_sig) chan.set_state(channel_states.OPENING) - self.lnworker.add_channel(chan) + self.lnworker.add_new_channel(chan) def validate_remote_reserve(self, payload_field: bytes, dust_limit: int, funding_sat: int) -> int: remote_reserve_sat = int.from_bytes(payload_field, 'big') diff --git a/electrum/lnworker.py b/electrum/lnworker.py @@ -760,7 +760,7 @@ class LNWallet(LNWorker): funding_sat=funding_sat, push_msat=push_sat * 1000, temp_channel_id=os.urandom(32)) - self.add_channel(chan) + self.add_new_channel(chan) self.network.trigger_callback('channels_updated', self.wallet) self.wallet.add_transaction(funding_tx) # save tx as local into the wallet self.wallet.set_label(funding_tx.txid(), _('Open channel')) @@ -774,6 +774,11 @@ class LNWallet(LNWorker): with self.lock: self.channels[chan.channel_id] = chan self.lnwatcher.add_channel(chan.funding_outpoint.to_str(), chan.get_funding_address()) + + def add_new_channel(self, chan): + self.add_channel(chan) + channels_db = self.db.get_dict('channels') + channels_db[chan.channel_id.hex()] = chan.storage self.wallet.save_backup() @log_exceptions