def filterTX(self, typep, l): tx = [] if len(l) < 3: return setOK(tx) query = [l[i:i + 3] for i in range(0, len(l), 3)] for addrx in query: resps = c_peer.sendGETToPeers("address/" + addrx[0] + "/transactions") # to keep it simple, just take first one valid reply code = 500 text = {} for rsp in resps: if len(rsp) != 0: (text, code) = rsp break if code == 200: trxn = {} if typep < 2: trxn = {'address': addrx[0]} elif typep < 4: trxn = {'keyName': addrx[1] + "/" + addrx[0]} elif typep < 6: trxn = {'publicKey': addrx[2] + "/" + addrx[0]} trxn['transactions'] = [] for trx in text['transactions']: if "minedInBlockIndex" in trx: if (typep == 0) or (typep == 2) or (typep == 4): trxn['transactions'].append(trx) else: if (typep == 1) or (typep == 3) or (typep == 5): trxn['transactions'].append(trx) if len(trxn['transactions']) > 0: tx.append(trxn) return setOK(tx)
def collectKeyBalance(self, addr): resps = c_peer.sendGETToPeers("address/" + addr + "/balance") # to keep it simple, just return first valid reply for rsp in resps: if len(rsp) > 0: return rsp return resps[0] # should not happen
def initChain(self, onePeer="", fetchAll=False, hashFetch=False): self.clearChainLoadGenesis() if fetchAll is False: # get the blocks one by one, due to size or whatever (later maybe even only getting the hashes! threadx = Thread(target=self.getMissingBlocksFromPeer,args=(onePeer, -1, False, {})) threadx.start() return else: if hashFetch is True: allPeersInfo = c_peer.sendGETToPeers("blocks/hash/0/100/"+str(len(defHash))) #TODO how to detemine? use info first? else: self.getAllBlocksOneReadDirect() m_cfg['chainLoaded'] = True
def getAllBlocksOneReadDirect(self): # coming here we are sure that we have back-up or nothing to loose! allPeersInfo = c_peer.sendGETToPeers("info") ret = -1 while (ret != 200) and (len(allPeersInfo)>0): #some peers exists and may have blocks, so we follow them maxIdx = -1 maxDiff = -1 cnt = -1 for detail, detail200 in allPeersInfo: cnt = cnt + 1 if detail200 != 200: #TODO actually should remove!!!! continue m, l, f = checkRequiredFields(detail, m_info, [], False) if (isSameChain(detail) is True) and (len(m) == 0): if maxDiff < detail['cumulativeDifficulty']: maxDiff = detail['cumulativeDifficulty'] maxIdx = cnt if maxIdx >= 0: bestPeer = allPeersInfo[maxIdx][0]['nodeUrl'] #bestPeer = bestPeer[0] #bestPeer = bestPeer['nodeUrl'] blockList, ret = c_peer.sendGETToPeer(bestPeer+"/blocks") if ret == 200: isFirst = True for block in blockList: if isFirst is True: #TODO compare block with my genesis # if any verification fails, set ret to -1 isFirst = False m_info['cumulativeDifficulty'] = block['difficulty'] continue if ret == 200: if len(self.verifyThenAddBlock(block)) != 0: self.clearChainLoadGenesis() break else: self.clearChainLoadGenesis() break m_info['blocksCount'] = len(m_Blocks) continue # with ret being 200, this ends the loop!!! del allPeersInfo[maxIdx] continue
def miner_get(url): try: response = c_peer.sendGETToPeers(url) return response except Exception: return {'peerError': "No data received from peer"}