blockchain: blockchains_lock needed to write/iterate global dict
This commit is contained in:
parent
7dc5665ab1
commit
4360a785ad
@ -74,6 +74,8 @@ def hash_header(header: dict) -> str:
|
|||||||
|
|
||||||
|
|
||||||
blockchains = {}
|
blockchains = {}
|
||||||
|
blockchains_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def read_blockchains(config):
|
def read_blockchains(config):
|
||||||
blockchains[0] = Blockchain(config, 0, None)
|
blockchains[0] = Blockchain(config, 0, None)
|
||||||
@ -118,7 +120,8 @@ class Blockchain(util.PrintError):
|
|||||||
return blockchains[self.parent_id]
|
return blockchains[self.parent_id]
|
||||||
|
|
||||||
def get_max_child(self) -> Optional[int]:
|
def get_max_child(self) -> Optional[int]:
|
||||||
children = list(filter(lambda y: y.parent_id==self.forkpoint, blockchains.values()))
|
with blockchains_lock: chains = list(blockchains.values())
|
||||||
|
children = list(filter(lambda y: y.parent_id==self.forkpoint, chains))
|
||||||
return max([x.forkpoint for x in children]) if children else None
|
return max([x.forkpoint for x in children]) if children else None
|
||||||
|
|
||||||
def get_forkpoint(self) -> int:
|
def get_forkpoint(self) -> int:
|
||||||
@ -237,21 +240,23 @@ class Blockchain(util.PrintError):
|
|||||||
self.write(parent_data, 0)
|
self.write(parent_data, 0)
|
||||||
parent.write(my_data, (forkpoint - parent.forkpoint)*HEADER_SIZE)
|
parent.write(my_data, (forkpoint - parent.forkpoint)*HEADER_SIZE)
|
||||||
# store file path
|
# store file path
|
||||||
for b in blockchains.values():
|
with blockchains_lock: chains = list(blockchains.values())
|
||||||
|
for b in chains:
|
||||||
b.old_path = b.path()
|
b.old_path = b.path()
|
||||||
# swap parameters
|
# swap parameters
|
||||||
self.parent_id = parent.parent_id; parent.parent_id = parent_id
|
self.parent_id = parent.parent_id; parent.parent_id = parent_id
|
||||||
self.forkpoint = parent.forkpoint; parent.forkpoint = forkpoint
|
self.forkpoint = parent.forkpoint; parent.forkpoint = forkpoint
|
||||||
self._size = parent._size; parent._size = parent_branch_size
|
self._size = parent._size; parent._size = parent_branch_size
|
||||||
# move files
|
# move files
|
||||||
for b in blockchains.values():
|
for b in chains:
|
||||||
if b in [self, parent]: continue
|
if b in [self, parent]: continue
|
||||||
if b.old_path != b.path():
|
if b.old_path != b.path():
|
||||||
self.print_error("renaming", b.old_path, b.path())
|
self.print_error("renaming", b.old_path, b.path())
|
||||||
os.rename(b.old_path, b.path())
|
os.rename(b.old_path, b.path())
|
||||||
# update pointers
|
# update pointers
|
||||||
blockchains[self.forkpoint] = self
|
with blockchains_lock:
|
||||||
blockchains[parent.forkpoint] = parent
|
blockchains[self.forkpoint] = self
|
||||||
|
blockchains[parent.forkpoint] = parent
|
||||||
|
|
||||||
def assert_headers_file_available(self, path):
|
def assert_headers_file_available(self, path):
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
@ -417,14 +422,16 @@ class Blockchain(util.PrintError):
|
|||||||
def check_header(header: dict) -> Optional[Blockchain]:
|
def check_header(header: dict) -> Optional[Blockchain]:
|
||||||
if type(header) is not dict:
|
if type(header) is not dict:
|
||||||
return None
|
return None
|
||||||
for b in blockchains.values():
|
with blockchains_lock: chains = list(blockchains.values())
|
||||||
|
for b in chains:
|
||||||
if b.check_header(header):
|
if b.check_header(header):
|
||||||
return b
|
return b
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def can_connect(header: dict) -> Optional[Blockchain]:
|
def can_connect(header: dict) -> Optional[Blockchain]:
|
||||||
for b in blockchains.values():
|
with blockchains_lock: chains = list(blockchains.values())
|
||||||
|
for b in chains:
|
||||||
if b.can_connect(header):
|
if b.can_connect(header):
|
||||||
return b
|
return b
|
||||||
return None
|
return None
|
||||||
|
|||||||
@ -510,8 +510,9 @@ class Interface(PrintError):
|
|||||||
chain = self.blockchain.check_header(bad_header)
|
chain = self.blockchain.check_header(bad_header)
|
||||||
if not chain:
|
if not chain:
|
||||||
b = forkfun(bad_header)
|
b = forkfun(bad_header)
|
||||||
assert bad not in blockchain.blockchains, (bad, list(blockchain.blockchains.keys()))
|
with blockchain.blockchains_lock:
|
||||||
blockchain.blockchains[bad] = b
|
assert bad not in blockchain.blockchains, (bad, list(blockchain.blockchains))
|
||||||
|
blockchain.blockchains[bad] = b
|
||||||
self.blockchain = b
|
self.blockchain = b
|
||||||
height = b.forkpoint + 1
|
height = b.forkpoint + 1
|
||||||
assert b.forkpoint == bad
|
assert b.forkpoint == bad
|
||||||
@ -540,7 +541,8 @@ class Interface(PrintError):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
bad, bad_header = height, header
|
bad, bad_header = height, header
|
||||||
local_max = max([0] + [x.height() for x in blockchain.blockchains.values()]) if 'mock' not in header else float('inf')
|
with blockchain.blockchains_lock: chains = list(blockchain.blockchains.values())
|
||||||
|
local_max = max([0] + [x.height() for x in chains]) if 'mock' not in header else float('inf')
|
||||||
height = min(local_max + 1, height - 1)
|
height = min(local_max + 1, height - 1)
|
||||||
while await iterate():
|
while await iterate():
|
||||||
bad, bad_header = height, header
|
bad, bad_header = height, header
|
||||||
|
|||||||
@ -177,7 +177,7 @@ class Network(PrintError):
|
|||||||
config = {} # Do not use mutables as default values!
|
config = {} # Do not use mutables as default values!
|
||||||
self.config = SimpleConfig(config) if isinstance(config, dict) else config
|
self.config = SimpleConfig(config) if isinstance(config, dict) else config
|
||||||
self.num_server = 10 if not self.config.get('oneserver') else 0
|
self.num_server = 10 if not self.config.get('oneserver') else 0
|
||||||
blockchain.blockchains = blockchain.read_blockchains(self.config) # note: needs self.blockchains_lock
|
blockchain.blockchains = blockchain.read_blockchains(self.config)
|
||||||
self.print_error("blockchains", list(blockchain.blockchains.keys()))
|
self.print_error("blockchains", list(blockchain.blockchains.keys()))
|
||||||
self.blockchain_index = config.get('blockchain_index', 0)
|
self.blockchain_index = config.get('blockchain_index', 0)
|
||||||
if self.blockchain_index not in blockchain.blockchains.keys():
|
if self.blockchain_index not in blockchain.blockchains.keys():
|
||||||
@ -199,7 +199,6 @@ class Network(PrintError):
|
|||||||
self.interface_lock = threading.RLock() # <- re-entrant
|
self.interface_lock = threading.RLock() # <- re-entrant
|
||||||
self.callback_lock = threading.Lock()
|
self.callback_lock = threading.Lock()
|
||||||
self.recent_servers_lock = threading.RLock() # <- re-entrant
|
self.recent_servers_lock = threading.RLock() # <- re-entrant
|
||||||
self.blockchains_lock = threading.Lock()
|
|
||||||
|
|
||||||
self.server_peers = {} # returned by interface (servers that the main interface knows about)
|
self.server_peers = {} # returned by interface (servers that the main interface knows about)
|
||||||
self.recent_servers = self.read_recent_servers() # note: needs self.recent_servers_lock
|
self.recent_servers = self.read_recent_servers() # note: needs self.recent_servers_lock
|
||||||
@ -711,8 +710,7 @@ class Network(PrintError):
|
|||||||
@with_interface_lock
|
@with_interface_lock
|
||||||
def get_blockchains(self):
|
def get_blockchains(self):
|
||||||
out = {}
|
out = {}
|
||||||
with self.blockchains_lock:
|
with blockchain.blockchains_lock: blockchain_items = list(blockchain.blockchains.items())
|
||||||
blockchain_items = list(blockchain.blockchains.items())
|
|
||||||
for k, b in blockchain_items:
|
for k, b in blockchain_items:
|
||||||
r = list(filter(lambda i: i.blockchain==b, list(self.interfaces.values())))
|
r = list(filter(lambda i: i.blockchain==b, list(self.interfaces.values())))
|
||||||
if r:
|
if r:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user