Exemplo n.º 1
0
def difficulty_test():
    from block import Block, genesis
    from db import clear_db
    clear_db()
    BLOCKCHAIN = []
    first = True
    wallet = Wallet()
    wallet.gen_keypair()
    set_mining_addr(wallet.address)
    while True:
        if (first == False):
            log.info("creating new block")
            pb = BLOCKCHAIN[-1]
            diff = 1
            if (len(BLOCKCHAIN) > 1):
                diff = compute_difficulty(pb)
            new_block = Block(height=pb.height + 1,
                              hash='',
                              diff_bits=diff,
                              timestamp=millis(),
                              transactions=[],
                              nonce=0,
                              version=100,
                              prev_hash=pb.hash)
            log.info("Created block, mining. diff={}".format(
                new_block.diff_bits))
            new_block.mine(diff)
            log.info("Mined block. hash={} time={} (sec) difficulty={}".format(
                new_block.hash, (millis() - new_block.timestamp) / 1000,
                new_block.diff_bits))
            BLOCKCHAIN.append(new_block)
        else:
            log.info("creating genesis block")
            diff = 1
            new_block = genesis()
            log.info("Created genesis block, mining")
            new_block.mine(1)
            log.info("Mined genesis block. hash={} time={}(sec) difficulty={}".
                     format(new_block.hash,
                            (millis() - new_block.timestamp) / 1000,
                            new_block.diff_bits))
            BLOCKCHAIN.append(new_block)
            first = False
        new_block.save()
        sleep(1.5)  # to prevent diff adjust issues
 def __init__(self):
     self.chain = [genesis()]
Exemplo n.º 3
0
'''
 (c) 2020 Coinfection Project
 This code is licensed under the GNU General Public License v3.0 (see LICENSE.txt for details)
'''
from db import set, get
from block import Block, genesis

TOP_BLOCK = None

read_block_height = get('height', 'coofchainstatus')
if read_block_height == None:
    # save the genesis block
    genesis = genesis()
    print("No blocks found, saving geneis block to db")
    genesis.save()
    set('height', '0', 'coofchainstatus')
    TOP_BLOCK = genesis


def get_block_height():
    return TOP_BLOCK.height


def set_top_block(new):
    TOP_BLOCK = new
    set('height', str(new.height), 'coofchainstatus')
    return True


def top_block():
    return TOP_BLOCK