Do not save new channels before they are added to lnworker

This commit is contained in:
ThomasV 2020-02-23 20:35:03 +01:00
parent 8d02c2027c
commit e8ee4250d9
3 changed files with 16 additions and 7 deletions

View file

@ -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])

View file

@ -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')

View file

@ -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