From a4dea8dcdd5180954a5451c67356561def26231d Mon Sep 17 00:00:00 2001 From: Daniel Edgecumbe Date: Thu, 28 Sep 2017 00:03:58 +0100 Subject: [PATCH] [block] Update prevblock with nextblockhash on new bestblockhash --- block.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/block.py b/block.py index b8850cd..5d0d55c 100644 --- a/block.py +++ b/block.py @@ -94,7 +94,23 @@ class BlockStore(object): async def on_bestblockhash(self, blockhash): with await self._lock: self._bestblockhash = blockhash - # TODO: if the previous block exists, update its' nextblockhash + + # Pre-fetch it if necessary and update the previous block + # TODO: think about the locking here. + block = await self.get_block(blockhash) + with await self._lock: + try: + prevblock = self._blocks[block["previousblockhash"]] + except KeyError: + return + + if "nextblockhash" in prevblock: + if prevblock["nextblockhash"] == blockhash: + return + + raise Exception("BlockStore does not handle re-orgs") + + prevblock["nextblockhash"] = blockhash async def get_bestblockhash(self): with await self._lock: