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