def addtransaction(self, data): tx = Transaction.from_json({"block": "Mempool", **data}) mempool = Mempool() print("adding to mempool") mempool.add_transaction(tx) UDPHandler.broadcastmessage( json.dumps({ "command": "sendtransaction", "body": tx.to_json() })) return json.dumps({"status": "ok"})
def sendtransaction(self, request=None, response=None): """ Send transaction """ if request is None and response is None: # If no data is being passed on by command handler pass if response is not None: # Processing reaponse if "command" in response.keys(): mm = Mempool() tx = Transaction() mm.add_transaction(tx.from_json(response["body"]))
def from_json(data): """ Mapping data from JSON to object """ tmp = Block() tmp.hash = data['hash'] tmp.timestamp = data['timestamp'] tmp.transactions = [Transaction.from_json( tx) for tx in data['transactions']] tmp.previous_block_hash = data['previous_block_hash'] tmp.merkle_root = data['merkle_root'] tmp.height = data['height'] tmp.version = data['version'] tmp.size = data['size'] return tmp
def gettxbyhash(self, request=None, response=None): """ Get transaction by hash """ if request is None and response is None: # If no data is being passed on by command handler pass if request is not None: # Processing requests UDPHandler.sendmessage(json.dumps({"hash": request["hash"]}), request["ip_addr"], request["receiver_port"]) elif response is not None: # Processing reaponse mempool = Mempool() mempool.add_transaction( Transaction.from_json(json.loads(response)["tx"]))
def getallmtxhash(self, request=None, response=None): """ Get all mempool transactiona by hash """ if request is None and response is None: # If no data is being passed on by command handler pass if request is not None: # Processing requests UDPHandler.broadcastmessage( json.dumps({"command": "getallmtxhash"})) if response is not None: # Processing reaponse redis_client = redis.Redis(host='localhost', port=6379, db=0) local_tx_hashes: set = set() remote_tx_hashes: set = set() for tx in redis_client.lrange("mempool", 0, -1): local_tx_hashes.add( Transaction.from_json(tx.decode("utf-8")).hash) response_data = json.loads(response) for tx in response_data["hashes"]: remote_tx_hashes.add(tx) remote_tx_hashes.difference_update(local_tx_hashes) receiver_port = settings.UDP_RECEIVER_PORT for raw in redis_client.lrange("nodes", 0, -1): info = json.loads(raw.decode("utf-8")) if info["ip_addr"] == response_data["ip_addr"]: receiver_port = info["receiver_port"] for tx_hash in remote_tx_hashes: self.gettxbyhash({ "hash": tx_hash, "ip_addr": response_data["ip_addr"], "receiver_port": receiver_port })
if __name__ == '__main__': tx = { 'timestamp': time.time(), 'version': '0.0.2', 'inputs': [{ "previous_tx": "oihrsgiohsioj9ih05i0yu9u59y8o4yu54h", "index": 3, "address": "iuyfvuyfguyifguyff687", "scriptSig": ["segbikldrih95euy9u4509uyh90e9p4ujy"], "verifying_key": ["jlbuigfuiga89y89egyg8w4oig8gw"] }], 'outputs': [{ 'value': 50, 'n': 0, 'address': 'bb4c10de221933bb0aa1ab1897f5640592fc1fa4' }], 'hash': 'eef9fda50a6besdrhyaeeeeeeeeee5072d94df4f6sfrhdztrhewrhyf145eb047426ad0fb', 'block': 'testchain', 'is_coinbase': False } mm = Mempool() t = Transaction() ob = t.from_json(tx) print("Adding tx to own mempool only!!") mm.add_transaction(ob)
def get_transaction(self) -> Transaction: return Transaction.from_json(json.loads(self.redis_client.rpop("mempool").decode("utf-8")))