def test_multiple_retrieve(self): b = Blockchain() t = Contact("0", "test", "*****@*****.**") o = Contact("1", "other", "*****@*****.**") l = [t, o] m = Message(0, t, o, "This is a test.", '', False) n = Message(1, t, o, "This is another test.", '', False) b.submit(m) b.submit(n) m = b.retrieve(t, 1, l) self.assertEqual(m[0].text, "This is another test.")
def retrieve(self, user: Contact, last_message: int, contact_list: List[Contact]) -> List[Message]: """ :param user: The user to download new messages for. :param last_message: The last message ID that the local database was aware of. :param contact_list: The current database of known contacts. :return: The list of all new messages. """ abi = self.contract_interface['abi'] contract = self.w3.eth.contract(address=self.addr, abi=abi) result = contract.functions.retrieve(int(user.address), last_message).call() messages = list() res_list = result.split('♠') for x in res_list: y = x.split('♣') c: Contact = None if len(y) == 5: for z in contact_list: if z.uname == y[2]: c = z if c is None: c = Contact('', y[2], '') messages.append(Message(int(y[0]), user, c, y[3], y[4], False)) return messages
def insert(self, to: Contact, fr: Contact, text: str, sign: str, verified: bool) -> Message: """ Produces a message object and adds it to the internal database. :param to: The Contact the message is being sent to. :param fr: The Contact the message originated from. :param text: The text of the message itself. :param sign: The cryptographic signature of the unencrypted message text. :param verified: Whether or not the message's signature has been verified successfully. :return: The message object that has just been added to the database. """ m = Message(self.__max_msgid() + 1, to, fr, text, sign, verified) self.messages.append(m) self.__commit() return m
def test_submit(self): b = Blockchain() t = Contact("0", "test", "*****@*****.**") o = Contact("1", "other", "*****@*****.**") m = Message(0, t, o, "This is a test.", '', False) b.submit(m)