def finalise(host, peer, port): # default for peers is exactly one, but if started up with more, more are supported # the argument sets the time how often the checks are made in seconds to verify if the peer still replies useVis = m_cfg['canTrack'] try: if 'file' in m_debug: xh = host.replace(":", "").replace("/", "").replace("http", "").replace(".", "") m_debug['file'] = open( "aaLog_" + m_info['type'].replace("\*", "d") + "_" + xh + "_" + m_info['nodeId'] + "_log.dat", "w") except Exception: i = 0 # temporarily switch off any delay to allow peer initialisation to go ahead without delay m_cfg['canTrack'] = False if isBCNode(): c_blockchainNode.c_blockchainHandler.clearChainLoadGenesis() elif isWallet() or isFaucet(): m_db['db'] = sqlite3.connect(m_db['DATABASE']) elif isGenesis(): c_genesisInterface.initGenesis() holdOn = True while holdOn: try: sleep(1) requests.get("http://" + host + ":" + str(port)) holdOn = False except Exception: print("Holding on for flask...") c_peer.registerPotentialPeer(peer, port) thread = Thread(target=c_peer.checkEveryXSecs) thread.start() while m_cfg['checkingPeers'] == True: sleep(1) print("Still initialising peers..") print("Initialise peers completed") m_cfg['canTrack'] = useVis if isBCNode(): c_blockchainNode.c_blockchainHandler.getMissingBlocksFromPeer( "", -1, False, {}, 1) initPendingTX() elif isMiner(): initMiner(host)
def debug(): m_ret = {"cfg": deepcopy(m_cfg)} addCfg(m_ret) if isBCNode(): m_ret.update({"TX": deepcopy(m_pendingTX)}) m_ret.update({"minerCandidates": deepcopy(m_BufferMinerCandidates)}) m_ret.update({"balances": deepcopy(m_AllBalances)}) m_ret.update({"blocks": deepcopy(m_Blocks)}) return setOK(m_ret)
def nodeSpecificGET(self, url, linkInfo): ret = { 'NodeType': m_info['type'], 'info': "This URL/API is not available/broken" } try: if self.permittedURLGET(url) is False: return errMsg("This URL/API is invalid or not available. " + url) if "chainInit" not in m_cfg: m_cfg['chainInit'] = False # backward compatible maxWait = 12 while (len(m_isPOST) > 0) or (m_cfg['chainInit'] is True): if url == "/info": # this is needed for peers to cross reference each other break if url.startswith( "/blockBalances" ): # this is needed for peers to cross reference each other break if self.delay(url, 1) is False: break # for some reason we decide to ignore the lock maxWait = maxWait - 1 if maxWait < 0: print( "Console maxwait for chain update reached, just go ahead now ...." ) break m_simpleLock.append(url) if isBCNode(): ret = self.c_blockInterface.nodeSpecificGETNow(url, linkInfo) elif isWallet(): ret = self.c_walletInterface.nodeSpecificGETNow(url, linkInfo) elif isFaucet(): ret = self.c_faucetInterface.nodeSpecificGETNow(url, linkInfo) elif isGenesis(): ret = self.c_genesisInterface.nodeSpecificGETNow(url, linkInfo) except Exception: d("*********************************************") d("*********************************************") print("GET exception caught, isPoststack " + str(len(m_isPOST))) d("*********************************************") d("*********************************************") if url in m_simpleLock: m_simpleLock.remove( url ) # maybe need to check for being there, then need to add random to URL return ret
def suitablePeer(self, peer, fastTrack=False): try: s1 = self.doGET(peer + "/info", fastTrack) reply = json.loads(s1.text) m, l, f = checkRequiredFields(reply, m_info, ["chainId", "about"], False) if (len(f) > 0) or ( len(m) > 0): # we skip to check any additional fields, is that OK print("Peer " + peer + " reply not accepted, considered not alive") # as it is invalid peer, don't try again m_cfg['peers'][peer][ 'wrongType'] = m_cfg['peers'][peer]['wrongType'] + 1 del m_cfg['peers'][peer] m_info['peers'] = len(m_cfg['peers']) return {'wrongChain': True} if peer != reply['nodeUrl']: return {'Inconsistency in nodeURL': True} if not self.ensureBCNode(reply['type']): return {'wrongType': True} if isBCNode(): if m_cfg['chainLoaded'] is True: if (reply['blocksCount'] > len(m_Blocks)) or ( reply['blockHash'] != m_Blocks[-1]['blockHash']): # ignore the return data from the situational check as here is primarily peer # and informing the blockmanager about potential peers is secondary here d("info showed longer block or different hash:" + str(reply['blocksCount']) + " " + str(len(m_Blocks)) + " " + reply['blockHash'] + " " + m_Blocks[-1]['blockHash']) project.classes.c_blockchainNode.c_blockchainHandler.checkChainSituation( 'info', reply) if reply['blocksCount'] < len(m_Blocks): # we found a laggard, let him know we are further project.classes.c_blockchainNode.c_blockchainHandler.asynchNotifyPeers( ) return reply except Exception: return {'fail': True}
def index(): addOn = "" if m_cfg['debug'] == True: addOn = "_debug" if isBCNode(): return render_template("indexBC" + addOn + ".html") if isWallet(): return render_template("TabWallet" + addOn + ".html") if isFaucet(): return render_template("TabFaucet" + addOn + ".html") if isMiner(): return render_template("TabMiner" + addOn + ".html") if isGenesis(): project.classes.c_genesisInterface.initGenesis() return render_template("TabGenesis" + addOn + ".html") response = { 'NodeType': m_info['type'], 'info': "Requested URL/API is not available" } return errMsg(response)
def nodeSpecificPOST(self, url, linkInfo, json, request): ret = { 'NodeType': m_info['type'], 'info': "This URL/API is not available/broken" } try: for x in json: if not re.match("[0-9a-zA-Z]+", x): return errMsg("Invalid JSON key: " + str(x)) if isinstance(json[x], str): if not re.match("[0-9a-zA-Z \.%!@#$\-_+=;:,/?<>]*", json[x]): return errMsg("Invalid character in JSON data: " + str(x)) elif isinstance(json[x], list): for xx in json[x]: if isinstance(xx, str): if not re.match("[0-9a-zA-Z \.%!@#$\-_+=;:,/?<>]*", xx): return errMsg( "Invalid character in JSON data: " + str(xx)) elif not isinstance(json[x], int): return errMsg("Invalid character in JSON data: " + str(json[x])) # This is only applicable to POST, and is a shortcut to stop endless broadcast # of the same message for urlJson in m_peerSkip: if urlJson['url'] == url: m, l, f = checkRequiredFields(json, urlJson['json'], urlJson['json'], True) if len(m) + len(f) == 0: #TODO what text here? return setOK("Acknowledge... ") # for bigger network, make this bigger as well? if len(m_peerSkip) > 10: del m_peerSkip[0] if self.permittedURLPOST(url) is False: return errMsg("This URL/API is invalid or not available. " + url) d("Add delay url: '" + url + "' before we had " + str(len(m_isPOST))) m_isPOST.append(url) while (len(m_isPOST) > 1) or (m_cfg['chainInit'] is True): if self.delay(url, 2) is False: break # for some reason we decide to ignore the loop while len(m_simpleLock) > 0: if self.delay(url, 3) is False: break # for some reason we decide to ignore the loop self.release = False if isBCNode(): ret = self.c_blockInterface.nodeSpecificPOSTNow( url, linkInfo, json, request) elif isWallet(): ret = self.c_walletInterface.nodeSpecificPOSTNow( url, linkInfo, json, request) elif isFaucet(): ret = self.c_faucetInterface.nodeSpecificPOSTNow( url, linkInfo, json, request) elif isGenesis(): ret = self.c_genesisInterface.nodeSpecificPOSTNow( url, linkInfo, json, request) except Exception: d("*********************************************") d("*********************************************") print("POST exception caught, isPoststack " + str(len(m_isPOST))) d("*********************************************") d("*********************************************") if url in m_isPOST: m_isPOST.remove(url) d("Removed delay url: '" + url + "' back to " + str(len(m_isPOST))) self.release = True return ret