Exemplo n.º 1
0
    def __init__(self, address, port):

        self.registered_peers = ContactList()
        self.numPeers = 0
        self.registrar = self.startRegistrar(address, port)
        self.cont = True
        self.blockchain = Blockchain()
Exemplo n.º 2
0
 def testSaveLoad(self):
     contactsList = ContactList()
     
     self.assertEqual({}, contactsList.contacts)
     contactsList.addContact(Contact('name1', "1234"))
     contactsList.addContact(Contact('name2', "2345"))
     contactsList.addContact(Contact('name3', "3456"))
     
     contactsList.save('contactsTest.csv')
     
     contactListLoaded = ContactList()
     contactListLoaded.load('contactsTest.csv')
     
     self.assertEqual(3, len(contactListLoaded.contacts))
     self.assertEqual('1234', contactListLoaded.contacts['name1'])
     self.assertEqual('2345', contactListLoaded.contacts['name2'])
     self.assertEqual('3456', contactListLoaded.contacts['name3'])
Exemplo n.º 3
0
    def __init__(self):

        # Define the data and variables that will be used by the Peer
        self.profile = {
            'id': None,
            'username': None,
            'addr': None,
            'port': None,
        }  # This Peer's Info
        self.contacts = ContactList()  # Contact List
        self.blockchain = Blockchain()  # Blockchain
        self.wallet = Wallet()  # Wallet
        self.battery = Wallet(currency='kW')  # Battery Bank
        self.listen_port = 0  # Where to contact this Peer

        self.notifications = []

        self.awaitingValidation = True
        self.validated = False
Exemplo n.º 4
0
class Registrar():
    def __init__(self, address, port):

        self.registered_peers = ContactList()
        self.numPeers = 0
        self.registrar = self.startRegistrar(address, port)
        self.cont = True
        self.blockchain = Blockchain()

    def startRegistrar(self, address, port):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((address, int(port)))
        sock.listen(100)
        return sock

    def getRegistrar(self):
        print("Successful creation of the Registrar")
        return self.registrar

    def addContact(self, username, uid, address, port):
        self.registered_peers.addContact(str(uid), str(username), str(address),
                                         port)
    def testMergeListsWithCollisionsResolving(self):
        contactList1 = ContactList()
        contactList1.addContact(Contact('name1', "1234"))
        contactList1.addContact(Contact('name2', "2345"))
        contactList1.addContact(Contact('name3', "3456"))

        contactList2 = ContactList()
        contactList2.addContact(Contact('name1', "6789"))
        contactList2.addContact(Contact('name2', "6789"))
        contactList2.addContact(Contact('name3', "6789"))

        resolvedCollisions = ContactList()
        resolvedCollisions.addContact(Contact('name1', "1234"))
        resolvedCollisions.addContact(Contact('name2', "6789"))
        resolvedCollisions.addContact(Contact('name3', "6789"))

        contactList1.mergeContacts(contactList2, resolvedCollisions)
        self.assertEqual(3, len(contactList1.contacts))
        self.assertEqual('1234', contactList1.contacts['name1'])
        self.assertEqual('6789', contactList1.contacts['name2'])
        self.assertEqual('6789', contactList1.contacts['name3'])
    def testMergeLists(self):
        contactList1 = ContactList()
        contactList1.addContact(Contact('name1', "1234"))
        contactList1.addContact(Contact('name2', "2345"))
        contactList1.addContact(Contact('name3', "3456"))

        contactList2 = ContactList()
        contactList2.addContact(Contact('name1', "6789"))
        contactList2.addContact(Contact('name2', "6789"))
        contactList2.addContact(Contact('name3', "6789"))

        contactList1.mergeContacts(contactList2)
        self.assertEqual(3, len(contactList1.contacts))
        self.assertEqual('1234', contactList1.contacts['name1'])
        self.assertEqual('2345', contactList1.contacts['name2'])
        self.assertEqual('3456', contactList1.contacts['name3'])

        contactList2.addContact(Contact('name4', "6789"))

        contactList1.mergeContacts(contactList2)
        self.assertEqual(4, len(contactList1.contacts))
        self.assertEqual('1234', contactList1.contacts['name1'])
        self.assertEqual('2345', contactList1.contacts['name2'])
        self.assertEqual('3456', contactList1.contacts['name3'])
        self.assertEqual('6789', contactList1.contacts['name4'])
    def testFindCollisions(self):
        contactList1 = ContactList()
        contactList1.addContact(Contact('name1', "1234"))
        contactList1.addContact(Contact('name2', "2345"))
        contactList1.addContact(Contact('name3', "3456"))

        contactList2 = ContactList()

        self.assertEqual(0, len(contactList1.findCollisions(contactList2)))

        contactList2.addContact(Contact('name1', "6789"))
        contactList2.addContact(Contact('name2', "2345"))
        contactList2.addContact(Contact('name4', "1234"))

        collisions = contactList1.findCollisions(contactList2)
        self.assertEqual(1, len(collisions))
        self.assertEqual("name1", collisions[0].name)
        self.assertEqual("1234", collisions[0].phoneNumber1)
        self.assertEqual("6789", collisions[0].phoneNumber2)
    def testSaveLoad(self):
        contactsList = ContactList()

        self.assertEqual({}, contactsList.contacts)
        contactsList.addContact(Contact('name1', "1234"))
        contactsList.addContact(Contact('name2', "2345"))
        contactsList.addContact(Contact('name3', "3456"))

        contactsList.save('contactsTest.csv')

        contactListLoaded = ContactList()
        contactListLoaded.load('contactsTest.csv')

        self.assertEqual(3, len(contactListLoaded.contacts))
        self.assertEqual('1234', contactListLoaded.contacts['name1'])
        self.assertEqual('2345', contactListLoaded.contacts['name2'])
        self.assertEqual('3456', contactListLoaded.contacts['name3'])
Exemplo n.º 9
0
    def testMergeListsWithCollisionsResolving(self):
        contactList1 = ContactList();
        contactList1.addContact(Contact('name1', "1234"))
        contactList1.addContact(Contact('name2', "2345"))
        contactList1.addContact(Contact('name3', "3456"))

        contactList2 = ContactList();
        contactList2.addContact(Contact('name1', "6789"))
        contactList2.addContact(Contact('name2', "6789"))
        contactList2.addContact(Contact('name3', "6789"))

        resolvedCollisions = ContactList();
        resolvedCollisions.addContact(Contact('name1', "1234"))
        resolvedCollisions.addContact(Contact('name2', "6789"))
        resolvedCollisions.addContact(Contact('name3', "6789"))

        contactList1.mergeContacts(contactList2, resolvedCollisions)
        self.assertEqual(3, len(contactList1.contacts))
        self.assertEqual('1234', contactList1.contacts['name1'])
        self.assertEqual('6789', contactList1.contacts['name2'])
        self.assertEqual('6789', contactList1.contacts['name3'])
Exemplo n.º 10
0
    def testMergeLists(self):
        contactList1 = ContactList();
        contactList1.addContact(Contact('name1', "1234"))
        contactList1.addContact(Contact('name2', "2345"))
        contactList1.addContact(Contact('name3', "3456"))

        contactList2 = ContactList();
        contactList2.addContact(Contact('name1', "6789"))
        contactList2.addContact(Contact('name2', "6789"))
        contactList2.addContact(Contact('name3', "6789"))

        contactList1.mergeContacts(contactList2)
        self.assertEqual(3, len(contactList1.contacts))
        self.assertEqual('1234', contactList1.contacts['name1'])
        self.assertEqual('2345', contactList1.contacts['name2'])
        self.assertEqual('3456', contactList1.contacts['name3'])

        contactList2.addContact(Contact('name4', "6789"))

        contactList1.mergeContacts(contactList2)
        self.assertEqual(4, len(contactList1.contacts))
        self.assertEqual('1234', contactList1.contacts['name1'])
        self.assertEqual('2345', contactList1.contacts['name2'])
        self.assertEqual('3456', contactList1.contacts['name3'])
        self.assertEqual('6789', contactList1.contacts['name4'])
Exemplo n.º 11
0
 def testFindCollisions(self):
     contactList1 = ContactList();
     contactList1.addContact(Contact('name1', "1234"))
     contactList1.addContact(Contact('name2', "2345"))
     contactList1.addContact(Contact('name3', "3456"))
     
     contactList2 = ContactList();
     
     self.assertEqual(0, len(contactList1.findCollisions(contactList2)))
     
     contactList2.addContact(Contact('name1', "6789"))
     contactList2.addContact(Contact('name2', "2345"))
     contactList2.addContact(Contact('name4', "1234"))
     
     collisions = contactList1.findCollisions(contactList2)
     self.assertEqual(1, len(collisions))
     self.assertEqual("name1", collisions[0].name)
     self.assertEqual("1234", collisions[0].phoneNumber1)
     self.assertEqual("6789", collisions[0].phoneNumber2)
Exemplo n.º 12
0
class Peer():
    def __init__(self):

        # Define the data and variables that will be used by the Peer
        self.profile = {
            'id': None,
            'username': None,
            'addr': None,
            'port': None,
        }  # This Peer's Info
        self.contacts = ContactList()  # Contact List
        self.blockchain = Blockchain()  # Blockchain
        self.wallet = Wallet()  # Wallet
        self.battery = Wallet(currency='kW')  # Battery Bank
        self.listen_port = 0  # Where to contact this Peer

        self.notifications = []

        self.awaitingValidation = True
        self.validated = False

    def serverGo(self):
        self.server = PeerServer(
            kwargs={
                'contacts': self.contacts,
                'blockchain': self.blockchain,
                'wallet': self.wallet,
                'port': self.listen_port,
                'profile': self.profile,
                'notifications': self.notifications,
            })

    def clientGo(self):
        self.client = PeerClient(
            kwargs={
                'contacts': self.contacts,
                'blockchain': self.blockchain,
                'wallet': self.wallet,
                'port': self.listen_port,
                'profile': self.profile,
                'notifications': self.notifications,
                'awaitFlag': self.awaitingValidation,
                'validatedFlag': self.validated
            })

    def register(self):
        self.client.register("127.0.0.1", 10000)

    def updateContacts(self, contacts):
        self.contacts = contacts

    def getPort(self):
        self.listen_port = self.server.getPort()

    def checkWallet(self):
        return self.wallet.getBalance()

    def checkBattery(self):
        return self.battery.getBalance()

    def startMenu(self):
        try:

            res = ""
            # Clear the screen
            os.system('cls' if os.name == 'nt' else 'clear')

            while True:

                print("1. Contacts")
                print("2. Start Transaction")
                print("3. Check Wallet")
                print("4. Check Battery")
                print("5. Check Blockchain")
                print("6. Notifications", len(self.notifications))
                print("7. Exit\n")
                if res != "":
                    if 'hash' in res:
                        print("*** CURRENT BLOCKCHAIN ***\n")
                        pprint.pprint(res)
                    else:
                        print(res)
                    print()
                choice = input("Select: ")

                print()

                if choice == '1':

                    res = "*** CONTACTS ***\n"
                    res += str(self.contacts.all())

                elif choice == '2':

                    generating_transaction = False
                    while not generating_transaction:
                        # Clear the screen
                        os.system('cls' if os.name == 'nt' else 'clear')

                        print("Transaction Generator")
                        print("1. Buy Energy")
                        print("2. Sell Energy")
                        print("3. Cancel")
                        print()

                        t_choice = input("Select: ")

                        if t_choice == '1':
                            # Clear the screen
                            os.system('cls' if os.name == 'nt' else 'clear')

                            amount = int(
                                input(
                                    "How much energy (kW) do you wish to purchase: "
                                ))
                            transaction = {'type': 'buy', 'amount': amount}
                            if self.startTransaction(transaction):
                                res = 'Transaction Completed'
                            else:
                                res = 'Transaction could not be completed. Try again ...'

                            generating_transaction = True
                        elif t_choice == '2':
                            # Clear the screen
                            os.system('cls' if os.name == 'nt' else 'clear')

                            amount = int(
                                input(
                                    "How much energy (kW) do you wish to sell: "
                                ))
                            transaction = {'type': 'sell', 'amount': amount}
                            if self.startTransaction(transaction):
                                res = 'Transaction Completed'
                            else:
                                res = 'Transaction could not be completed. Try again ...'

                            generating_transaction = True
                        elif t_choice == '3':
                            print("Tansaction Cancelled")
                            generating_transaction = True

                        else:
                            print("Not a valid choice. Try again ...")
                            pass

                elif choice == '3':
                    res = "*** WALLET BALANCE ***\n"
                    res += "Balance\t" + str(self.checkWallet())

                elif choice == '4':
                    res = "*** BATTERY BALANCE ***\n"
                    res += "Balance\t" + str(self.checkBattery())

                elif choice == '5':

                    res = str(self.blockchain.chain)

                elif choice == '6':

                    res = "*** NOTIFICATIONS ***\n"
                    for n in self.notifications:
                        res += str(n) + "\n"

                    self.notifications = []

                elif choice == '7':
                    res = "BYE\n"
                    exit()

                # Clear the screen
                os.system('cls' if os.name == 'nt' else 'clear')

                pass
        except Exception as e:
            print(e)
            exit()

    def startTransaction(self, transaction):
        # check which type of transaction
        if transaction['type'] == 'buy':
            # Check that the Peer has the funding for the purchase
            # NOTE: Hardcoded price
            total_cost = transaction['amount'] * PRICE
            if total_cost > self.wallet.getBalance():
                self.notifications.append(
                    datetime.now().strftime("%H:%M:%S") +
                    "\tInsufficient funds for this purchase")
                return False
            else:
                # Create the block with the new transaction
                self.blockchain.add_transaction(str(self.profile['username']),
                                                'Smart Grid', total_cost,
                                                transaction['amount'])
                self.blockchain.add_block()

                # Propose the block with the new transaction
                self.client.propose()

                # Ensure we do not proceed until validation has been completed
                while self.client.validationStatus():
                    print("Client Awaiting Validation")
                    time.sleep(1)

                if self.client.isValidated():
                    # If all is in order, Carry out the transaction
                    self.wallet.withdrawal(total_cost)
                    self.battery.deposit(transaction['amount'])

                self.validated = False
                self.awaitingValidation = True

            return True
        elif transaction['type'] == 'sell':
            # Check that the Peer has the energy for the transaction
            # NOTE: Hardcoded price
            total_cost = transaction['amount'] * PRICE
            if transaction['amount'] > self.battery.getBalance():
                self.notifications.append(
                    datetime.now().strftime("%H:%M:%S") +
                    "\tInsufficient energy for this transaction")
                return False
            else:
                # Create the block with the new transaction
                self.blockchain.add_transaction(str(self.profile['username']),
                                                'Smart Grid', total_cost,
                                                transaction['amount'])
                self.blockchain.add_block()

                # Propose the block with the new transaction
                self.client.propose()

                # Ensure we do not proceed until validation has been completed
                while self.client.validationStatus():
                    print("Client Awaiting Validation")
                    time.sleep(1)

                if self.client.isValidated():
                    # If all is in order, Carry out the transaction
                    self.wallet.deposit(total_cost)
                    self.battery.withdrawal(transaction['amount'])

                self.validated = False
                self.awaitingValidation = True

            return True
        else:
            return False