Exemple #1
0
    def add_transaction(self,
                        recipient,
                        sender,
                        signature,
                        amount=1.0,
                        is_receiving=False):
        '''if self.public_key == None:
			return None'''

        transaction = Transaction(sender, recipient, amount, signature)
        """if not Wallet.verify_signature(transaction):
			return False"""
        if Verification.verify_transaction(transaction, self.get_balance):
            self.open_transactions.append(transaction)
            self.save_data()
            if not is_receiving:
                for node in self.peer_nodes:
                    url = 'http://{}/broadcast_transaction'.format(node)
                    try:
                        response = requests.post(url,
                                                 json={
                                                     'sender': sender,
                                                     'recipient': recipient,
                                                     'amount': amount,
                                                     'signature': signature
                                                 })
                        if response.status_code == 400 or response.status_code == 500:
                            print('Transaction declined, needs resolving')
                            return False
                    except requests.exceptions.ConnectionError:
                        continue
            return True
        return False
Exemple #2
0
 def proof_of_work(self):
     last_block = self.chain[-1]
     last_hash = hash_block(last_block)
     proof = 0
     while not Verification.valid_proof(self.open_transactions, last_hash,
                                        proof):
         proof += 1
     return proof
Exemple #3
0
    def add_transaction(self, recipient, sender, signature, amount=1.0):

        if self.public_key == None:
            return None

        transaction = Transaction(sender, recipient, amount, signature)
        """if not Wallet.verify_signature(transaction):
			return False"""
        if Verification.verify_transaction(transaction, self.get_balance):
            self.open_transactions.append(transaction)
            self.save_data()
            return True
        else:
            print("Solde insuffisant")
            return False
Exemple #4
0
 def mine_block(self):
     if self.public_key == None:
         return None
         #return False
     if not Verification.verify_chain(self.chain):
         return None
     last_block = self.chain[-1]
     hashed_blocks = hash_block(last_block)
     proof = self.proof_of_work()
     reward_transaction = Transaction('MINING', self.public_key, '',
                                      MINING_REWARD)
     copied_transactions = self.open_transactions[:]
     for tx in copied_transactions:
         if not Wallet.verify_signature(tx):
             return None
             #return False
     copied_transactions.append(reward_transaction)
     block = Block(hashed_blocks, len(self.chain), copied_transactions,
                   proof)
     self.chain.append(block)
     self.open_transactions = []
     self.save_data()
     return block
Exemple #5
0
 def mine_block(self):
     if self.public_key == None:
         return None
         #return False
     if not Verification.verify_chain(self.chain):
         return None
     last_block = self.chain[-1]
     hashed_blocks = hash_block(last_block)
     proof = self.proof_of_work()
     reward_transaction = Transaction('MINING', self.public_key, '',
                                      MINING_REWARD)
     copied_transactions = self.open_transactions[:]
     for tx in copied_transactions:
         if not Wallet.verify_signature(tx):
             return None
             #return False
     copied_transactions.append(reward_transaction)
     block = Block(hashed_blocks, len(self.chain), copied_transactions,
                   proof)
     self.chain.append(block)
     self.open_transactions = []
     self.save_data()
     for node in self.peer_nodes:
         url = 'http://{}/broadcast_block'.format(node)
         block_converted = block.__dict__.copy()
         block_converted['transactions'] = [
             tx.__dict__ for tx in block_converted['transactions']
         ]
         try:
             response = requests.post(url, json={'block': block_converted})
             if response.status_code == 400:
                 print('Block declined, needs resolving')
             if response.status_code == 500:
                 print('Block declined')
         except requests.exceptions.ConnectionError:
             continue
     return block
Exemple #6
0
 def add_block(self, block):
     transactions = [
         Transaction(tx['sender'], tx['recipient'], tx['signature'],
                     tx['amount']) for tx in block['transactions']
     ]
     proof_is_valid = Verification.valid_proof(transactions[:-1],
                                               block['hash_previous_block'],
                                               block['proof'])
     hashes_match = hash_block(
         self.chain[-1]) == block['hash_previous_block']
     print("hash block ", hash_block(self.chain[-1]))
     print("block previous hash ", block['hash_previous_block'])
     print(proof_is_valid)
     print(hashes_match)
     if not proof_is_valid or not hashes_match:
         return False
     converted_block = Block(block['hash_previous_block'], block['index'],
                             transactions, block['proof'],
                             block['timestamp'])
     self.chain.append(converted_block)
     stored_transactions = self.open_transactions[:]
     for incomingtx in block['transactions']:
         for opentx in stored_transactions:
             print("incoming tx ", incomingtx)
             print("opentx ", opentx)
             if incomingtx['sender'] == opentx.sender and incomingtx[
                     'recipient'] == opentx.recipient and incomingtx[
                         'amount'] == opentx.amount and incomingtx[
                             'signature'] == opentx.signature:
                 #Only transactions that are the same everywhere should be mined
                 try:
                     self.open_transactions.remove(opentx)
                 except ValueError:
                     print('Item is already removed')
     self.save_data()
     return True
Exemple #7
0
from time import time
import sys
sys.path.append("/Users/Guillaume/Documents/Informatique/Projets-git/psc")
from carac import *
from classes import Analyseur, Verification
from Verification.similarite import Similarity
from Verification.unmasking import Unmasking

verificateur = Unmasking()

taille_morceaux = 1000
analyseur = Analyseur([freq_gram, freq_stopwords])

liste_id_oeuvres_base = [("corneille",k) for k in range(1,16)]

liste_id_oeuvres_calibrage = [("racine",k) for k in range(1,11)]

liste_id_oeuvres_disputees = [("moliere",k) for k in [1,11]]

V = Verification(liste_id_oeuvres_base, liste_id_oeuvres_calibrage, liste_id_oeuvres_disputees, taille_morceaux, analyseur, verificateur)
V.creer_textes()
V.analyser(normalisation = False)
V.appliquer_verificateur()