cosme12 / SimpleCoin

Just a really simple, insecure and incomplete implementation of a blockchain for a cryptocurrency made in Python as educational material. In other words, a simple Bitcoin clone.
http://copitosystem.com
MIT License
1.78k stars 397 forks source link

Improve proof of work algorithm #3

Closed cosme12 closed 6 years ago

cosme12 commented 6 years ago

The current proof of work algorithm is really simple to solve. This make it easy for an attacker to destroy the actual blockchain.

adanielpincab commented 6 years ago

What time more or less do you want the proof of work to be solved? I've made one which prove is solved in an average time of 4.395 seconds, but I don´t know if it´s enough, or too much.

adanielpincab commented 6 years ago

Well, nevermind. I'll make a pull request adding a module with the proof of work, so it can be implemented later.

adanielpincab commented 6 years ago

Done! I've implemented my algorythm to the miner.py file. I think it works good, now the computational effort is harder. It's in my pull request, take a look to it 😄

cosme12 commented 6 years ago

I will close this for now. Changing the proof of work algorithm will add unnecessary complexity to the code destroying its purpose.

HourGlss commented 5 years ago
def proof_of_work(last_proof, blockchain):
    def random_str():
        # Generate a random size string from 3 - 27 characters long
        rand_str = ''
        for i in range(0, 3 + secrets.randbelow(25)):
            rand_str += string.ascii_lowercase[secrets.randbelow(26)]  # each char is a random downcase letter [a-z]
        return rand_str

    def genhash():
        #Generate random string
        r = random_str()
        #Create hash object
        m = hashlib.sha3_256()
        #update that hash object with the string
        m.update(r.encode("utf-8"))

        #return the string format of the hash
        return m.hexdigest()
    pow_hash = genhash()

    start_time = time.time()

    #check to see if the first <work> characters of the string are 0
    work = 6
    while not (pow_hash[0:work] == ("0"*work)):

        # Check if any node found the solution every 60 seconds
        if int((time.time()-start_time) % 60) == 0:
            # If any other node got the proof, stop searching
            new_blockchain = consensus(blockchain)
            if new_blockchain:
                # (False: another node got proof first, new blockchain)
                return False, new_blockchain
        # generate new hash for next time
        pow_hash = genhash()
    # Once that hash is found, we can return it as a proof of our work
    return pow_hash, blockchain

Wouldn't this be better?