def __init__(self, slaveNodes: list): super().__init__() self.slavesNodes = slaveNodes self.cryptor = NooCrypto() #------------------------------------------------ self.cryptor.generateKeys() self.redis_host = os.getenv("REDIS_PORT_6379_TCP_ADDR") or 'localhost' self.redis_port = os.getenv("REDIS_PORT_6379_TCP_PORT") or '6379' self.redis = redis.StrictRedis(self.redis_host, self.redis_port, db=1) self.secondKeys = [] self.secondPKeys = [] self.secondKeys.append( "125466fda87a6c88df7222eec063a720958985e39d9e6eff5c76047e68d32c67") self.secondPKeys.append( "0323f264fd64a684db1e36a2c97b58867e0625f797008206216576fea2114bdbca" ) self.secondKeys.append( "a376bdbcca0cda0ed27c7f5a434444276b2379b25f504c3ba9c3f4f52a6a5d1c") self.secondPKeys.append( "027426df275f98bb0f66bb4f1a92352296be233b115415f709540e8caa80d820f2" ) cryptor = NooCrypto() for i in range(1, 100): cryptor.generateKeys() self.secondKeys.append(cryptor.getPrivateKey()) self.secondPKeys.append(cryptor.getPublicKey()) self.url = "http://explorer.vinci.id:5000"
def badAnswer(cryptor: NooCrypto): temp = json.dumps({ 'TT': 'BAN', 'SENDER': cryptor.getPublicKey() }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def giveAllSizeBlock(cryptor: NooCrypto, NBH: int): temp = json.dumps( { 'TT': 'ASB', 'SENDER': cryptor.getPublicKey(), "NBH": NBH }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def createCommitBlockForResend(cryptor: NooCrypto, block: str): block = json.dumps( { 'TT': 'CBR', 'SENDER': cryptor.getPublicKey(), "BLOCK": json.loads(block) }, separators=(',', ':')) sign = cryptor.signMessage(block) jblock = json.loads(block) jblock.update({'SIGNATURE': sign}) return json.dumps(jblock, separators=(',', ':'))
def universalPacket(cryptor: NooCrypto, type: str, value): temp = json.dumps( { 'TT': type, 'DATA': value, 'SENDER': cryptor.getPublicKey() }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def benchmarkTesting(cryptor: NooCrypto, uuid: str): temp = json.dumps( { 'TT': 'BT', 'SENDER': cryptor.getPublicKey(), 'START': uuid }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def myBenchmarkResult(cryptor: NooCrypto, result: str, start: str): temp = json.dumps( { 'TT': 'MBR', 'SENDER': cryptor.getPublicKey(), 'RESULT': result, 'START': start }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def createSignaturePrecommitedBlock(cryptor: NooCrypto, pblock: str, hash: str): temp = json.dumps( { 'TT': 'SG', 'SENDER': cryptor.getPublicKey(), 'HASH': hash, 'SIGNPB': cryptor.signMessage(pblock) }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def createCommitBlock(cryptor: NooCrypto, preCommitedBlock: str, signs: list): block = json.dumps( { 'TT': 'CB', 'SENDER': cryptor.getPublicKey(), "BLOCK": json.loads(preCommitedBlock), "SIGNATURES": signs }, separators=(',', ':')) sign = cryptor.signMessage(block) jblock = json.loads(block) jblock.update({'SIGNATURE': sign}) return json.dumps(jblock, separators=(',', ':'))
def createСomplaint(cryptor: NooCrypto, evidence_block: str, evidence_transaction: str): temp = json.dumps( { 'TT': 'COMPLAINT', 'SENDER': cryptor.getPublicKey(), 'EVIDENCE_BLOCK': evidence_block, 'EVIDENCE_TRANSACTION': evidence_transaction }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def startFastSendTransactions(self): cryptor = NooCrypto() cryptor.setPrivateKey( "125466fda87a6c88df7222eec063a720958985e39d9e6eff5c76047e68d32c67") sender = cryptor.getPublicKey() receiver = sender.replace("1", "2") while (True): tcount = randint(1, 9999) / 10000000 packet = JsonPackets.standartTransacton2(cryptor, receiver, tcount, "VNC") self.redis.zadd("RAW TRANSACTIONS", 1, packet) return True
def applicantTransaction( cryptor: NooCrypto, ip: str ): # Транзакция заявка на участие в следующем туре валидации temp = json.dumps( { 'TT': 'AT', 'SENDER': cryptor.getPublicKey(), 'IPADDR': ip, "TST": str(int(time.time())) }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def chooserTransaction( cryptor: NooCrypto, blockchain_height): # Транзакция выбора ноды для работы temp = json.dumps( { 'TT': 'CT', 'SENDER': cryptor.getPublicKey(), "NBH": blockchain_height, "TST": str(int(time.time())) }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def createCommitBlockForResendHash(cryptor: NooCrypto, block: str, bheight: int): block = json.dumps( { 'TT': 'CBRH', 'SENDER': cryptor.getPublicKey(), "HASH": NooTree.hash(block), "STEM_SIGNATURE": cryptor.signMessage(block), "BHEIGHT": bheight }, separators=(',', ':')) sign = cryptor.signMessage(block) jblock = json.loads(block) jblock.update({'SIGNATURE': sign}) return json.dumps(jblock, separators=(',', ':'))
def createPurifierBlock(cryptor: NooCrypto, addressList: list, keyList: list, bheight: int): temp = json.dumps( { 'TT': 'PURIFIER', 'SENDER': cryptor.getPublicKey(), 'CLEAN_LIST': addressList, 'CLEAN_KEY_LIST': keyList, "BHEIGHT": bheight }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def voteTransaction( cryptor: NooCrypto, reciever: str, vote_count: int ): # Транзакция заявка на участие в следующем туре валидации temp = json.dumps( { 'TT': 'AT', 'SENDER': cryptor.getPublicKey(), 'RECEIVER': reciever, 'VOTES': vote_count, "TST": str(int(time.time())) }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def standartTransaction( cryptor: NooCrypto, reciever: str, token_count: int, token_type: str): # Стандартная транзакция перевода средств temp = json.dumps( { 'TT': 'ST', 'SENDER': cryptor.getPublicKey(), 'RECEIVER': reciever, 'TTOKEN': token_type, 'CTOKEN': str(token_count), "TST": str(int(time.time())) }, separators=(',', ':')) sign = cryptor.signMessage(temp) jtemp = json.loads(temp) jtemp.update({'SIGNATURE': sign}) return json.dumps(jtemp, separators=(',', ':'))
def createPrecommitBlock(cryptor: NooCrypto, ver: str, transactions: set, bheight: int, fee: float): jtransactions = [] for tran in transactions: jtransactions.append(json.loads(tran)) block = json.dumps( { 'TT': 'BL', 'SENDER': cryptor.getPublicKey(), 'VERSION': ver, 'TCOUNT': len(jtransactions), "BHEIGHT": bheight, 'TRANSACTIONS': jtransactions, 'FEE': fee }, separators=(',', ':')) sign = cryptor.signMessage(block) jblock = json.loads(block) jblock.update({'SIGNATURE': sign}) return json.dumps(jblock, separators=(',', ':'))
class slaveWorker(QThread): floodPacket = pyqtSignal(str, str, int) sendPacket = pyqtSignal(str, str, int) setPurifyList = pyqtSignal(list) def __init__(self, myAddress: str, privateKey: str): super().__init__() self.cryptor = NooCrypto() self.transactionMemory = set() self.precommitedBlockHash = None self.signatureMemory = [] self.transactionsInPBlock = set() self.stemAddress = None self.stemPublicKey = "0320ab99dee836df538e5e09a7c692c0aef02d91a11ce711992b95835f28243242" self.nodesCount = 0 self.myAddress = myAddress self.packetStack = list() self.priorPacketStack = list() self.stackMutex = QMutex() self.signChecker = SignChecker() self.version = "0.1.0" self.redis_host = os.getenv("REDIS_PORT_6379_TCP_ADDR") or 'localhost' self.redis_port = os.getenv("REDIS_PORT_6379_TCP_PORT") or '6379' self.mongo_host = os.getenv("MONGO_PORT_27017_TCP_ADDR") or 'localhost' self.mongo_port = 27017 self.mongo_conn = MongoClient(self.mongo_host, self.mongo_port) self.redis = redis.StrictRedis(self.redis_host, self.redis_port, db=1) self.mongo = self.mongo_conn.noosphere self.mongo_conn.drop_database( self.mongo) # CLEAR MONGODB NOOSPHERE DATABASE self.lastCheckBlockBalance = 0 self.currentTokenTypes = ["NZT"] self.blockReward = 10 self.blocks_in_genesis = 10000000 self.timeToGarbageCollector = 300 * 1000 #5 minutes self.MAX_TRAN_COUNT_IN_BLOCK = 9000 self.MAX_TRAN_FOR_USER_IN_BLOCK = 9000 self.tempMoneySum = 0 self.updateBalanceStep = 1000 #----------------------------------------- TOKENOMIKS BASIC self.voteFeeArray = {"VT": 10, "UFT": 100, "DFT": 100} self.allTokens = 23 * 1000 * 1000 #23 mln tokens self.tokensToReward = self.allTokens * 0.26 #reward tokens for 4 years #self.freezeToAT = 16*1000 self.freezeToAT = 16 self.freezeToVT = 1 self.fee = 0 self.freeZoneHeight = 12 * 60 * 24 * 365 * 4 #self.freeZoneHeight = 10 #----------------------------------------- #self.cryptor.generateKeys() print("PRIVATE KEY!", privateKey) self.cryptor.setPrivateKey(privateKey) print("PUBLIC KEY!", self.cryptor.getPublicKey()) self.redis.flushdb() print(self.redis.set("VERSION", self.version)) ##################### TEST #self.redis.zadd("BALANCE:NZT",10000, "0323f264fd64a684db1e36a2c97b58867e0625f797008206216576fea2114bdbca") #self.redis.zadd("BALANCE:NBL",10000, "027426df275f98bb0f66bb4f1a92352296be233b115415f709540e8caa80d820f2") #self.redis.zadd("RAW TRANSACTIONS", 1541777858, '{"TT":"ET","SENDER":"0323f264fd64a684db1e36a2c97b58867e0625f797008206216576fea2114bdbca","RECEIVER":"027426df275f98bb0f66bb4f1a92352296be233b115415f709540e8caa80d820f2","STT":"NZT","STC":"100","RTT":"NBL","RTC":"10","TST":"1541777858","SIGNATURE":"3b229fe53c21b472e82d4eec2a9bbde9c340c243b80fbd7ee1897b065708603352df740f1493d4d7215802065be5fde34fd8e0cae65afa11b5d69dc0cfd0a01a00"}') @staticmethod def isfloat(value): try: float(value) return True except: return False @pyqtSlot(str, str) def appendPacketToStack(self, address: str, packet: str): # if not self.signChecker.checkTran(packet): # return jpacket = json.loads(packet) packetType = jpacket["TT"] self.stackMutex.lock() if packetType == "SG" or packetType == "CT" or packetType == "BL" or packetType == "PURIFIER": self.packetStack.insert(0, (address, packet)) else: self.packetStack.append((address, packet)) self.stackMutex.unlock() def getAnyFee(self, token_type): if token_type == "NZT": return self.fee else: return 0 def getAnyVoteFee(self, voteType): if self.voteFeeArray.get(voteType) is None: return 0 else: return self.voteFeeArray.get(voteType) def handler(self, address: str, packet: str): jpacket = json.loads(packet) print("RECIVE PACKET: ", jpacket["TT"]) if jpacket["TT"] == "SG": if self.precommitedBlockHash == jpacket.get("HASH"): self.signatureMemory.append(jpacket.get("SIGNPB")) else: print("Recive old block SG - failed and return") return signNeedCount = len(set(self.signatureMemory)) if signNeedCount >= int((2 / 3) * self.nodesCount): # CLEAR TRAN LIST # self.transactionMemory = self.transactionMemory - set( self.transactionsInPBlock) # CLEAR TRAN LIST # self.cttime = 0 print(self.signatureMemory) self.sendPacket.emit( self.stemAddress, JsonPackets.createCommitBlock(self.cryptor, self.precommitedBlock, self.signatureMemory), 1) self.transactionsInPBlock.clear() self.signatureMemory.clear() return if jpacket["TT"] == "CBRH": print("CBRH KEEP!") if jpacket[ "SENDER"] == self.stemPublicKey: #IF SENDER IS MASTER NODE hash = jpacket["HASH"] JTPCB = json.loads(self.precommitedBlock) JTPCB.pop("SIGNATURE") if NooTree.hash(json.dumps(JTPCB, separators=(',', ':'))) != hash: print("BAD HASH!!! FROM CBRH:", NooTree.hash(self.precommitedBlock), "AND", hash) # GIVE ME ALL SIZE BLOCK! self.sendPacket.emit( self.stemAddress, JsonPackets.giveAllSizeBlock(self.cryptor, jpacket["BHEIGHT"]), 1) return block = json.loads(self.precommitedBlock) block.update({"STEM_SIGNATURE": jpacket["STEM_SIGNATURE"]}) block.update({"NODE_SIGNATURES": jpacket["SIGNS"]}) self.mongo.noochain.save(block) #self.redis.zadd("NOOCHAIN", block["BHEIGHT"], json.dumps(block, separators=(',', ':'))) self.precommitedBlock = "" # CLEAR MEMMORY AFTER SAVE BLOCK IN CHAIN self.checkBalance() # # DELETE AFTER ENDING MAIN BLOCKCHAIN EXPLORER # money = self.redis.zscore("MONEY MOVE", datetime.now().strftime("%Y-%m-%d")) # # if money is None: # money = 0 # else: # if slaveWorker.isfloat(money): # money = float(money) # else: # print("CLEAR MONEY BUF-BUF") # money = 0 # # DELETE AFTER ENDING MAIN BLOCKCHAIN EXPLORER for tran in block["TRANSACTIONS"]: stran = json.dumps(tran, separators=(',', ':')) print("ZREEEEEEEM:", stran, self.redis.zrem("RAW TRANSACTIONS", stran)) self.redis.zrem("RAW TRANSACTIONS", stran) if tran["TT"] == "ET": sender = tran["SENDER"] receiver = tran["RECEIVER"] sign = tran["SIGNATURE"] self.redis.zadd("COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), NooTree.hash(sign)) self.redis.set("TRANSACTIONS:" + NooTree.hash(sign), stran) if tran["TT"] == "ST": sender = tran["SENDER"] receiver = tran["RECEIVER"] sign = tran["SIGNATURE"] self.redis.zadd("COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), NooTree.hash(sign)) self.redis.set("TRANSACTIONS:" + NooTree.hash(sign), stran) if tran["TT"] == "AT": sender = tran["SENDER"] sign = tran["SIGNATURE"] self.redis.zadd("COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), NooTree.hash(sign)) self.redis.zadd("APPLICANTS", int(block["BHEIGHT"]), sender) self.redis.set("TRANSACTIONS:" + NooTree.hash(sign), stran) if tran["TT"] == "UAT": sender = tran["SENDER"] sign = tran["SIGNATURE"] self.redis.zadd("COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), NooTree.hash(sign)) self.redis.zrem("APPLICANTS", sender) self.redis.set("TRANSACTIONS:" + NooTree.hash(sign), stran) if tran["TT"] == "VT": sender = tran["SENDER"] receiver = tran["RECEIVER"] votes = int(tran["VOTES"]) sign = tran["SIGNATURE"] self.redis.zadd("COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), NooTree.hash(sign)) self.redis.set("TRANSACTIONS:" + NooTree.hash(sign), stran) tempVotes = self.redis.zscore("VOTES:" + sender, receiver) if tempVotes == None: tempVotes = 0 self.redis.zadd("VOTES:" + receiver, tempVotes + votes, sender) tempUntVotes = self.redis.zscore("UNTVOTES", sender) if tempUntVotes == None: tempUntVotes = 0 self.redis.zadd("UNTVOTES", tempUntVotes + votes, sender) if tran["TT"] == "UFT": #UPFEETRAN sender = tran["SENDER"] receiver = tran["RECEIVER"] votes = int(tran["VOTES"]) sign = tran["SIGNATURE"] self.redis.zadd("COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), NooTree.hash(sign)) self.redis.set("TRANSACTIONS:" + NooTree.hash(sign), stran) tempVotes = self.redis.zscore("UFT-VOTES:" + sender, receiver) if tempVotes == None: tempVotes = 0 self.redis.zadd("UFT-VOTES:" + sender, tempVotes + votes, receiver) tempUntVotes = self.redis.zscore( "UFT-UNTVOTES", sender) if tempUntVotes == None: tempUntVotes = 0 self.redis.zadd("UFT-UNTVOTES", tempUntVotes + votes, sender) if tran["TT"] == "DFT": #DOWNFEETRAN sender = tran["SENDER"] receiver = tran["RECEIVER"] votes = int(tran["VOTES"]) sign = tran["SIGNATURE"] self.redis.zadd("COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), NooTree.hash(sign)) self.redis.set("TRANSACTIONS:" + NooTree.hash(sign), stran) tempVotes = self.redis.zscore("DFT-VOTES:" + sender, receiver) if tempVotes == None: tempVotes = 0 self.redis.zadd("DFT-VOTES:" + sender, tempVotes + votes, receiver) tempUntVotes = self.redis.zscore( "DFT-UNTVOTES", sender) if tempUntVotes == None: tempUntVotes = 0 self.redis.zadd("DFT-UNTVOTES", tempUntVotes + votes, sender) if tran["TT"] == "UVT": sender = tran["SENDER"] receiver = tran["RECEIVER"] sign = tran["SIGNATURE"] self.redis.zadd("COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), NooTree.hash(sign)) self.redis.zrem("VOTES:" + sender, receiver) self.redis.set("TRANSACTIONS:" + NooTree.hash(sign), stran) #self.redis.zadd("MONEY MOVE", money, datetime.now().strftime("%Y-%m-%d")) if jpacket["TT"] == "CBR": print("CBR KEEP!") if jpacket[ "SENDER"] == self.stemPublicKey: #IF SENDER IS MASTER NODE block = jpacket["BLOCK"] block.update({"STEM_SIGNATURE": jpacket["SIGNATURE"]}) self.mongo.noochain.save(block) #self.redis.zadd("NOOCHAIN", block["BHEIGHT"], json.dumps(block, separators=(',', ':'))) self.checkBalance() for tran in block["TRANSACTIONS"]: self.redis.zrem("RAW TRANSACTIONS", json.dumps(tran, separators=(',', ':'))) if tran["TT"] == "ST": sender = tran["SENDER"] receiver = tran["RECEIVER"] sign = tran["SIGNATURE"] #money += float(tran["CTOKEN"]) self.redis.zadd( "COMPLETE TRANSACTIONS", int(block["BHEIGHT"]), str(sender) + str(receiver) + str(sign)) if tran["TT"] == "AT": sender = tran["SENDER"] self.redis.zadd("APPLICANTS", int(block["BHEIGHT"]), sender) if tran["TT"] == "UAT": sender = tran["SENDER"] self.redis.zrem("APPLICANTS", sender) if tran["TT"] == "VT": sender = tran["SENDER"] receiver = tran["RECEIVER"] votes = tran["VOTES"] self.redis.zadd("VOTES:" + sender, votes, receiver) temp_votes = self.redis.zscore("UNTVOTES", sender) if temp_votes is None: temp_votes = 0 self.redis.zadd("UNTVOTES", temp_votes + votes, sender) if tran["TT"] == "UVT": sender = tran["SENDER"] receiver = tran["RECEIVER"] self.redis.zrem("VOTES:" + sender, receiver) del_votes = self.redis.zscore("VOTES:" + sender, receiver) if del_votes is None: del_votes = 0 self.redis.zrem("VOTES:" + sender, receiver) temp_votes = self.redis.zscore("UNTVOTES", sender) if temp_votes is None: temp_votes = 0 self.redis.zadd("UNTVOTES", temp_votes - del_votes, sender) #self.redis.zadd("MONEY MOVE", money, datetime.now().strftime("%Y-%m-%d")) # IF BLOCK FROM STEM NODE if jpacket["TT"] == "BL": # IF NEED SIGNATURE WORK transactions = jpacket["TRANSACTIONS"] trans = [] for tran in transactions: trans.append(json.dumps(tran, separators=(',', ':'))) unionTran = set(trans) - self.transactionMemory okSign = True if len(unionTran) != 0: # Build block with another transactions for newTran in unionTran: jnewTran = json.loads(newTran) signature = jnewTran.pop("SIGNATURE") sender = jnewTran.get("SENDER") if not self.cryptor.verifyMessage( signature, sender, json.dumps(jpacket, separators=(',', ':'))): self.sendPacket.emit( address, JsonPackets.createСomplaint( self.cryptor, packet, newTran), 1) okSign = False if okSign: # Good block! precommitedBlock = json.dumps(jpacket, separators=(',', ':')) self.sendPacket.emit( address, JsonPackets.createSignaturePrecommitedBlock( self.cryptor, precommitedBlock, NooTree.hash(precommitedBlock)), 1) return if jpacket["TT"] == "CT": self.updateFee() print("RECEIVE PACKET:", packet) #blockchain_height = self.redis.zcard("NOOCHAIN") blockchain_height = self.mongo.noochain.find().count() # pymongo_cursor = self.mongo.noochain.find().sort("BHEIGHT", pymongo.ASCENDING).limit(1) # blockchain_height = dict(pymongo_cursor).get("BHEIGHT") if blockchain_height is None: blockchain_height = 0 if blockchain_height != jpacket["NBH"]: print("NBH not Supported in my version BlockChain!") #print(blockchain_height, jpacket["NBH"]) self.sendPacket.emit(self.stemAddress, JsonPackets.badAnswer(self.cryptor), 1) return False txmas = [] txcount = self.redis.zcard("RAW TRANSACTIONS") if txcount < self.MAX_TRAN_COUNT_IN_BLOCK: txmas = self.redis.zrange("RAW TRANSACTIONS", 0, -1) else: txmas = self.redis.zrange("RAW TRANSACTIONS", 0, self.MAX_TRAN_COUNT_IN_BLOCK) tranUserCount = {} tempBalanceMemmory = {} tempDictBalanceMemmory = {} decodeTxmas = [] print("TXMAS_LEN:", len(txmas)) for tran in txmas: try: jtran = json.loads(tran) except Exception: print("000. GO TO HELL!", tran) continue tokenPrefix = "NZT" if jtran["TT"] == "ET": print("STEP 1") sender = jtran["SENDER"] # money sender receiver = jtran["RECEIVER"] # money receiver stt = jtran["STT"] # money sender token type rtt = jtran["RTT"] # money receiver token type stc = jtran["STC"] # money sender token count rtc = jtran["RTC"] # money receiver token count # if jtran.get("INIT_SIGNATURE") is not None: # init_sign = jtran.pop("INIT_SIGNATURE") # else: # init_sign = None # # sign = jtran.pop("SIGNATURE") # # if self.cryptor.verifyMessage(sign, sender, json.dumps(tran, separators=(',', ':'))): # tempBalance = tempBalanceMemmory.get(currentSender) # CHECK DOUBLE MONEY SEND tempSBalanceMemmory = tempDictBalanceMemmory.get(stt) if tempSBalanceMemmory is None: tempSBalanceMemmory = {} tempSBalance = self.redis.zscore( "BALANCE:" + stt, sender) else: tempSBalance = tempSBalanceMemmory.get(sender) if tempSBalance is None: tempSBalance = self.redis.zscore( "BALANCE:" + stt, sender) tempRBalanceMemmory = tempDictBalanceMemmory.get(rtt) if tempRBalanceMemmory is None: tempRBalanceMemmory = {} tempRBalance = self.redis.zscore( "BALANCE:" + rtt, receiver) else: tempRBalance = tempRBalanceMemmory.get(currentSender) if tempRBalance is None: tempRBalance = self.redis.zscore( "BALANCE:" + rtt, receiver) if tempSBalance is None: print("1. GO TO HELL ET!") self.redis.zadd("FAILED TRANSACTIONS", 1, NooTree.hash(jtran["SIGNATURE"])) self.redis.zrem("RAW TRANSACTIONS", tran) continue if tempRBalance is None: print("1. GO TO HELL ET!") self.redis.zadd("FAILED TRANSACTIONS", 1, NooTree.hash(jtran["SIGNATURE"])) self.redis.zrem("RAW TRANSACTIONS", tran) continue if stt == "NZT": totalSBalance = tempSBalance - self.getFreezeTokens( sender) else: totalSBalance = tempSBalance if totalSBalance < (float(stc) + self.getAnyFee(stt)): self.redis.zrem("RAW TRANSACTIONS", tran) self.redis.zadd("FAILED TRANSACTIONS", 2, NooTree.hash(jtran["SIGNATURE"])) print("2. GO TO HELL ET!") continue else: tempSBalance = tempSBalance - (float(stc) + self.getAnyFee(stt)) if stt == "NZT": totalRBalance = tempRBalance - self.getFreezeTokens( receiver) else: totalRBalance = tempRBalance if totalRBalance < (float(stc) + self.getAnyFee(rtt)): self.redis.zrem("RAW TRANSACTIONS", tran) self.redis.zadd("FAILED TRANSACTIONS", 2, NooTree.hash(jtran["SIGNATURE"])) print("2. GO TO HELL ET!") continue else: tempRBalance = tempRBalance - (float(rtc) + self.getAnyFee(rtt)) tempSBalanceMemmory.update({sender: tempSBalance}) tempRBalanceMemmory.update({receiver: tempRBalance}) tempDictBalanceMemmory.update({stt: tempSBalanceMemmory}) tempDictBalanceMemmory.update({rtt: tempRBalanceMemmory}) decodeTxmas.append(tran) if jtran["TT"] == "AT": currentSender = jtran["SENDER"] # CHECK DOUBLE MONEY SEND tempBalanceMemmory = tempDictBalanceMemmory.get("NZT") if tempBalanceMemmory is None: tempBalanceMemmory = {} tempBalance = self.redis.zscore( "BALANCE:NZT", currentSender) else: tempBalance = tempBalanceMemmory.get(currentSender) if tempBalance is None: tempBalance = self.redis.zscore( "BALANCE:NZT", currentSender) if tempBalance is None: print("1. GO TO HELL AT!") self.redis.zadd("FAILED TRANSACTIONS", 1, NooTree.hash(jtran["SIGNATURE"])) self.redis.zrem("RAW TRANSACTIONS", tran) continue if (tempBalance - self.getFreezeTokens(currentSender) ) < self.freezeToAT: self.redis.zrem("RAW TRANSACTIONS", tran) self.redis.zadd("FAILED TRANSACTIONS", 2, NooTree.hash(jtran["SIGNATURE"])) print("2. GO TO HELL AT!") continue else: tempBalance = tempBalance - self.freezeToAT tempBalanceMemmory.update({currentSender: tempBalance}) tempDictBalanceMemmory.update({"NZT": tempBalanceMemmory}) decodeTxmas.append(tran) if jtran["TT"] == "VT" or jtran["TT"] == "UFT" or jtran[ "TT"] == "DFT": # VOTE, UPFEE, DOWNFEE currentSender = jtran["SENDER"] # CHECK DOUBLE MONEY SEND tempBalanceMemmory = tempDictBalanceMemmory.get("NZT") if tempBalanceMemmory is None: tempBalanceMemmory = {} tempBalance = self.redis.zscore( "BALANCE:NZT", currentSender) else: tempBalance = tempBalanceMemmory.get(currentSender) if tempBalance is None: tempBalance = self.redis.zscore( "BALANCE:NZT", currentSender) if tempBalance is None: print("1. GO TO HELL VT!") self.redis.zadd("FAILED TRANSACTIONS", 1, NooTree.hash(jtran["SIGNATURE"])) self.redis.zrem("RAW TRANSACTIONS", tran) continue if (tempBalance - self.getFreezeTokens(currentSender) ) < self.freezeToVT * int(jtran["VOTES"]): self.redis.zrem("RAW TRANSACTIONS", tran) self.redis.zadd("FAILED TRANSACTIONS", 2, NooTree.hash(jtran["SIGNATURE"])) print("2. GO TO HELL VT!") continue else: if jtran["TT"] == "VT": tempBalance = tempBalance - self.getAnyVoteFee( jtran["TT"]) * int(jtran["VOTES"]) if jtran["TT"] == "UFT": tempBalance = tempBalance - self.getAnyVoteFee( jtran["TT"]) * int(jtran["VOTES"]) if jtran["TT"] == "DFT": tempBalance = tempBalance - self.getAnyVoteFee( jtran["TT"]) * int(jtran["VOTES"]) tempBalanceMemmory.update({currentSender: tempBalance}) tempDictBalanceMemmory.update({"NZT": tempBalanceMemmory}) decodeTxmas.append(tran) if jtran["TT"] == "ST": currentSender = jtran["SENDER"] # CHECK 3 TRAN FROM ONE USER count = tranUserCount.get(currentSender) if count is None: tranUserCount.update({currentSender: 1}) else: if count >= self.MAX_TRAN_FOR_USER_IN_BLOCK: continue else: tranUserCount.update({currentSender: (count + 1)}) # CHECK DOUBLE MONEY SEND tempBalanceMemmory = tempDictBalanceMemmory.get( jtran["TTOKEN"]) if tempBalanceMemmory is None: tempBalanceMemmory = {} tempBalance = self.redis.zscore( "BALANCE:" + jtran["TTOKEN"], currentSender) else: tempBalance = tempBalanceMemmory.get(currentSender) if tempBalance is None: tempBalance = self.redis.zscore( "BALANCE:" + jtran["TTOKEN"], currentSender) if tempBalance is None: print("1. GO TO HELL!") self.redis.zadd("FAILED TRANSACTIONS", 1, NooTree.hash(jtran["SIGNATURE"])) self.redis.zrem("RAW TRANSACTIONS", tran) continue if (tempBalance - self.getFreezeTokens(currentSender)) < ( float(jtran["CTOKEN"]) + self.fee): self.redis.zrem("RAW TRANSACTIONS", tran) self.redis.zadd("FAILED TRANSACTIONS", 2, NooTree.hash(jtran["SIGNATURE"])) print("2. GO TO HELL!") continue else: tempBalance = tempBalance - (float(jtran["CTOKEN"]) + self.fee) tempBalanceMemmory.update({currentSender: tempBalance}) tempDictBalanceMemmory.update( {jtran["TTOKEN"]: tempBalanceMemmory}) decodeTxmas.append(tran) self.transactionsInPBlock = decodeTxmas precommitBlock = JsonPackets.createPrecommitBlock( self.cryptor, self.version, self.transactionsInPBlock, blockchain_height, self.fee) # CLEAR ZONE # self.signatureMemory.clear() # CLEAR ZONE # self.precommitedBlock = precommitBlock self.precommitedBlockHash = NooTree.hash(precommitBlock) signature = json.loads(precommitBlock).get("SIGNATURE") print("APPEND MY SIGNATURE!", signature) self.signatureMemory.append(signature) signNeedCount = len(set(self.signatureMemory)) if signNeedCount >= math.ceil( (2 / 3) * self.nodesCount ): # !!!ONLY WORK IN TESTNET WITH ONE NODE!!! print("SIGN COMPLETE") #self.transactionMemory = self.transactionMemory - self.transactionsInPBlock # ДОБАВИТЬ ПОИСК И УДАЛЕНИЕ # CLEAR TRAN LIST # self.sendPacket.emit( self.stemAddress, JsonPackets.createCommitBlock(self.cryptor, self.precommitedBlock, self.signatureMemory), 1) self.transactionsInPBlock.clear() self.signatureMemory.clear() else: print("SIGN NOT COMPLETE") #print ("ZONE 2") self.floodPacket.emit(precommitBlock, str(), 1) return if jpacket["TT"] == "PURIFIER": #print("RECEIVE PACKET:", packet) self.updateFee() self.stemAddress = address pymongo_cursor = self.mongo.noochain.find().sort( "BHEIGHT", pymongo.ASCENDING).limit(1) blockchain_height = dict(pymongo_cursor).get("BHEIGHT") if blockchain_height is None: blockchain_height = 0 #blockchain_height = self.redis.zcard("NOOCHAIN") tempList = jpacket["CLEAN_LIST"] tempKeyList = jpacket["CLEAN_KEY_LIST"] if len(tempList) != len(tempKeyList): print("BAD PURIFIER BLOCK! STOP WORK!") return if blockchain_height == 0 or blockchain_height % self.blocks_in_genesis == 0: self.mongo.noochain.save(jpacket) #self.redis.zadd("NOOCHAIN", jpacket["BHEIGHT"], json.dumps(jpacket, separators=(',', ':'))) NLIST = [] for temp in zip(tempList, tempKeyList): NLIST.append({ "ADDRESS": temp[0], "TYPE": "1", "PUBLICKEY": temp[1] }) self.redis.set("NODES LIST", json.dumps({"NLIST": NLIST}, separators=(',', ':'))) self.nodesCount = len(tempList) self.setPurifyList.emit(tempList) return # if jpacket["TT"] == "BT": # #print("RECEIVE BT") # result = self.benchmark.benchmarkStart(jpacket["START"]) # packet = JsonPackets.myBenchmarkResult(self.cryptor, result, jpacket["START"]) # #print("SEND MBR") # self.sendPacket.emit(address, packet, 1) # return # if jpacket["TT"] == "AT": # print("RECEIVE PACKET:", packet) # self.transactionMemory.add(packet) # self.floodPacket.emit(packet, address, 0) # return # if jpacket["TT"] == "ST": # #if not self.checkTranForBalance(jpacket): # #return # self.transactionMemory.add(packet) # self.floodPacket.emit(packet, address, 0) # return def getFreezeTokens(self, wallet, token_type="NZT"): freezeForVotes = self.redis.zscore("UNTVOTES", wallet) if freezeForVotes is None: freezeForVotes = 0 freezeForVotes = freezeForVotes * self.freezeToVT if self.redis.zscore("APPLICANTS", wallet) is None: freezeForApplicant = 0 else: freezeForApplicant = self.freezeToAT return freezeForApplicant + freezeForVotes def garbageCollector(self): bheight = self.mongo.noochain.find().count() if bheight is None: bheight = 0 endBlock = bheight - 12 * 60 if endBlock < 0: endBlock = 0 oldTxs = self.redis.zrangebyscore("COMPLETE TRANSACTIONS", '0', str(endBlock)) for tran in oldTxs: print("SEARCH:", "TRANSACTIONS:" + tran.decode()) tranbody = self.redis.get("TRANSACTIONS:" + tran.decode()) if tranbody is None: continue jtranbody = json.loads(tranbody) jtranbody.update({"_id": tran.decode()}) self.mongo.complete.save(jtranbody) self.redis.delete("TRANSACTIONS:" + tran.decode()) self.redis.zrem("COMPLETE TRANSACTIONS", tran) oldFTxs = self.redis.zrange("FAILED TRANSACTIONS", 0, -1) for ftran in oldFTxs[0:int(len(oldFTxs) / 2)]: self.redis.zrem("FAILED TRANSACTIONS", ftran) return True def updateFee(self): #bheight = self.redis.zcard("NOOCHAIN") bheight = self.mongo.noochain.find().count() if bheight is None: bheight = 0 TrXcount = len( self.redis.zrange("COMPLETE TRANSACTIONS", bheight - 1000, bheight)) / 1000 if bheight > self.freeZoneHeight: if TrXcount >= 10000: self.fee = 0.0001 if TrXcount >= 5000 or TrXcount < 10000: self.fee = 0.001 if TrXcount >= 3000 or TrXcount < 5000: self.fee = 0.003 if TrXcount >= 100 or TrXcount < 3000: self.fee = 0.005 if TrXcount < 100: self.fee = 0.01 print("CURRENT FEE IN THIS TIME:", self.fee) return def beApplicant(self): if self.stemAddress is None: return False packet = JsonPackets.wantBeApplicant(self.cryptor) #print ("SEND BEAPP", self.stemAddress) self.sendPacket.emit(self.stemAddress, packet, 1) def checkTranForBalance(self, jtran: dict): sender = jtran["SENDER"] type = jtran["TTOKEN"] count = jtran["CTOKEN"] walletBalance = self.BALANCE.get(sender) if count < 0: return False if type != self.currentTokenType: return False if walletBalance is None: return False if walletBalance < count: return False return True def checkTranForBalanceAttrs(self, sender, type, count): walletBalance = self.BALANCE.get(sender) if count < 0: return False if type != self.currentTokenType: return False if walletBalance is None: return False if walletBalance < count: return False return True def sliceFloat(self, money: float): return "{0:.8f}".format(money) def balanceMainWorkRedis(self, bchain: list): for jblock in bchain: #jblock = json.loads(block) if jblock['TT'] == "BL": trans = jblock['TRANSACTIONS'] if self.redis.zscore("BALANCE:NZT", jblock['SENDER']) is None: self.redis.zadd("BALANCE:NZT", 0, jblock['SENDER']) blockSenderBalance = self.redis.zscore("BALANCE:NZT", jblock['SENDER']) blockSenderBalance += self.blockReward print("BALANCE_BHEIGHT", jblock['BHEIGHT']) print("FREEZZE:", self.getFreezeTokens(jblock['SENDER'])) print( "ADDING BALANCE NZT TO VALIDATOR:", blockSenderBalance - self.getFreezeTokens(jblock['SENDER']), jblock['SENDER']) self.redis.zadd("BALANCE:NZT", blockSenderBalance, jblock['SENDER']) for tran in trans: if tran["TT"] == "ST" and self.currentTokenTypes.count( tran["TTOKEN"]) and float(tran["CTOKEN"]) > 0: tokenPrefix = tran["TTOKEN"] if tran['SENDER'] == tran['RECEIVER']: continue if self.redis.zscore("BALANCE:" + tokenPrefix, tran['SENDER']) is None: self.redis.zadd("BALANCE:" + tokenPrefix, 0, tran['SENDER']) if self.redis.zscore("BALANCE:" + tokenPrefix, tran['RECEIVER']) is None: self.redis.zadd("BALANCE:" + tokenPrefix, 0, tran['RECEIVER']) senderBalance = self.redis.zscore( "BALANCE:" + tokenPrefix, tran['SENDER']) receiverBalance = self.redis.zscore( "BALANCE:" + tokenPrefix, tran["RECEIVER"]) senderBalance = senderBalance - float(tran["CTOKEN"]) receiverBalance = receiverBalance + float( tran["CTOKEN"]) self.redis.zadd("BALANCE:" + tokenPrefix, self.sliceFloat(senderBalance), tran["SENDER"]) self.redis.zadd("BALANCE:" + tokenPrefix, self.sliceFloat(receiverBalance), tran["RECEIVER"]) if tran["TT"] == "ET" and self.currentTokenTypes.count( tran["STT"]) and self.currentTokenTypes.count( tran["RTT"]) and float( tran["STC"]) > 0 and float( tran["RTC"]) > 0: tokenSenderPrefix = tran["STT"] tokenReceiverPrefix = tran["RTT"] if tran['SENDER'] == tran['RECEIVER']: continue #MONEY SEND if self.redis.zscore("BALANCE:" + tokenSenderPrefix, tran['SENDER']) is None: self.redis.zadd("BALANCE:" + tokenSenderPrefix, 0, tran['SENDER']) if self.redis.zscore("BALANCE:" + tokenSenderPrefix, tran['RECEIVER']) is None: self.redis.zadd("BALANCE:" + tokenSenderPrefix, 0, tran['RECEIVER']) senderBalance = self.redis.zscore( "BALANCE:" + tokenSenderPrefix, tran['SENDER']) receiverBalance = self.redis.zscore( "BALANCE:" + tokenSenderPrefix, tran["RECEIVER"]) senderBalance = senderBalance - float(tran["STC"]) receiverBalance = receiverBalance + float(tran["STC"]) self.redis.zadd("BALANCE:" + tokenSenderPrefix, self.sliceFloat(senderBalance), tran["SENDER"]) self.redis.zadd("BALANCE:" + tokenSenderPrefix, self.sliceFloat(receiverBalance), tran["RECEIVER"]) #GOODS SEND if self.redis.zscore("BALANCE:" + tokenReceiverPrefix, tran['RECEIVER']) is None: self.redis.zadd("BALANCE:" + tokenReceiverPrefix, 0, tran['RECEIVER']) if self.redis.zscore("BALANCE:" + tokenReceiverPrefix, tran['SENDER']) is None: self.redis.zadd("BALANCE:" + tokenReceiverPrefix, 0, tran['SENDER']) receiverBalance = self.redis.zscore( "BALANCE:" + tokenReceiverPrefix, tran['RECEIVER']) senderBalance = self.redis.zscore( "BALANCE:" + tokenReceiverPrefix, tran["SENDER"]) receiverBalance = receiverBalance - float(tran["RTC"]) senderBalance = senderBalance + float(tran["RTC"]) self.redis.zadd("BALANCE:" + tokenReceiverPrefix, self.sliceFloat(receiverBalance), tran["RECEIVER"]) self.redis.zadd("BALANCE:" + tokenReceiverPrefix, self.sliceFloat(senderBalance), tran["SENDER"]) def checkBalance(self, updateLen=-1): #bheight = self.redis.zcard("NOOCHAIN") #pymongo_cursor = self.mongo.noochain.find().sort("BHEIGHT", pymongo.ASCENDING).limit(1) #bheight = dict(pymongo_cursor).get("BHEIGHT") bheight = self.mongo.noochain.find().count() if updateLen == -1: print("START:", self.lastCheckBlockBalance, "STOP:", bheight) bchain = list( self.mongo.noochain.find( {"BHEIGHT": { "$gte": int(self.lastCheckBlockBalance) }})) print("BCH", bchain) #bchain = self.redis.zrange("NOOCHAIN", self.lastCheckBlockBalance, bheight) self.lastCheckBlockBalance = bheight self.balanceMainWorkRedis(bchain) else: ### STEP MODE step = self.lastCheckBlockBalance while step < bheight: nextStep = step + self.updateBalanceStep if nextStep > bheight: nextStep = bheight #bchain = self.redis.zrange("NOOCHAIN", step, nextStep) bchain = list( self.mongo.noochain.find([{ "BHEIGHT": { "$gt": step } }, { "BHEIGHT": { "$lt": nextStep } }])) step = nextStep self.balanceMainWorkRedis(bchain) self.lastCheckBlockBalance = nextStep return def run(self): while True: self.stackMutex.lock() if len(self.packetStack): packet = self.packetStack.pop(0) else: self.stackMutex.unlock() continue self.stackMutex.unlock() address = packet[0] packet = packet[1] #if self.signChecker.checkTran(packet): // Проверка подписи! self.handler(address, packet)