def genesis_block(self): """ Creates genesis block and appends it to chain. The block has index 0, previous_hash as 0, and a valid given hash. """ transactions = {} genesis_block = Block(0, [], datetime.datetime.now(), "0") genesis_block.hash = genesis_block.compute_hash() self.chain.append(genesis_block)
def getUserBlockThreads(db, latest = False): try: logging.info("getting block threads") uid = session['uid'] curTime = db.query("""select logout_time from user_info where uid = %s""", [uid]) logout_time = curTime.fetchone()[0] logging.info(logout_time) cur2 = db.query("""select block_id from user_info where uid = %s""", [uid]) bid = cur2.fetchone()[0] logging.info(bid) blockUsers = Block.getBlockUsersResidents(db, bid) logging.info(blockUsers) tids = [] for user in blockUsers: blockTids = getCommentTIDs(db, user, logout_time, latest) if blockTids: for b in blockTids: tids.append(b) tids.append(getLatestThreads(db, logout_time, user, 'b')) logging.info("tids") logging.info(tids) threads = [] for tid in tids: threads.append(tid) logging.info("threads") logging.info(threads) return threads except: logging.info("error fetching user threads") return None
def verify_and_add_block(): block_data = request.get_json() block = Block(block_data["index"], block_data["transactions"], block_data["timestamp"], block_data["previous_hash"]) proof = block_data['hash'] added = blockchain.add_block(block, proof) if not added: return "The block was discarded by the node", 400 return "Block added to the chain", 201
def create_chain_from_dump(chain_dump): blockchain = Blockchain() for idx, block_data in enumerate(chain_dump): block = Block(block_data["index"], block_data["transactions"], block_data["timestamp"], block_data["previous_hash"]) proof = block_data['hash'] if idx > 0: added = blockchain.add_block(block, proof) if not added: raise Exception("The chain dump is tampered!!") else: # the block is a genesis block, no verification needed blockchain.chain.append(block) return blockchain
def getHoodResidents(db, hid): logging.info("getting hood residents") error = None try: blocks = getAllBlocksInHood(db,hid) logging.info("blocks in hood") logging.info(blocks) hoodResidents = [] for block in blocks: blockResidents = Block.getBlockUsersResidents(db, block) for resident in blockResidents: hoodResidents.append(resident) logging.info(hoodResidents) return hoodResidents except: logging.error("error fetching residents of same block") error = "error" return error
def profile(): if 'uid' not in session: return redirect(url_for('login')) profile = [] if request.method == 'GET': logging.info("Get profile") profile_data = Users.view_profile(db.conn, request.form) if profile_data[10]: block_id = profile_data[10] block_name = Block.getBlockNameFromBid(db, block_id) logging.info(profile_data) else: block_name = "block approval pending" if profile_data: profile.append({"Fname": profile_data[1], "LName": profile_data[2], "email": profile_data[3], "Username": profile_data[5], "apt": profile_data[5],\ "street": profile_data[6], "city": profile_data[7], "state": profile_data[8], "zip": profile_data[9],"block_name" : block_name, "email_preference": profile_data[14]}) logging.info(profile) return render_template('show_profile.html', profileInfo=profile)
def interface(self): """Serves as an interface to add pending transactions to blockchain by adding them to block and verifying Proof of Work algorithm. """ if not self.unconfirmed_transactions: return False last_block = self.last_block new_block = Block(index=last_block.index + 1, transactions=self.unconfirmed_transactions, timestamp=datetime.datetime.now(), previous_hash=last_block.hash) proof = self.proof_of_work(new_block) self.add_block(new_block, proof) # reset unconfirmed transactions self.unconfirmed_transactions = [] return new_block.index
def getBlockListForHood(): hoodid = request.args.get('selectedHoodId') blocklist = Block.getBlockListForHood(db, hoodid) return blocklist