Esempio n. 1
0
def mining():
    print("Into the mining process")
    # Initializing Election Class
    elec = Election()
    # Do not make a block if mempool is empty
    # if elec.redis_client.llen("mempool") == 0:
    #     print("No block made! Mempool is empty!")
    #     return
    # Transaction verification
    # Initializing Block Class
    blk = Block()
    # Create Coinbase Transaction
    blk.create_coinbase_transaction()
    # Connect to Previous Block
    blk.add_previous_block()
    # Scan Mempool
    vd = True
    for i in range(0, elec.redis_client.llen("mempool")):
        # Get Transaction
        tx = elec.redis_client.lindex('mempool', i).decode('utf-8')
        if tx == None:
            # Exit if tx is None
            break
        # Get tx verification verdict
        # verify_verdict = elec.verification.verify_tx(tx)
        # if verify_verdict == "verified":
        #     # Sending data to block
        #     blk.add_transaction(tx)
        # else:
        #     vd = False
        #     print("Some Transaction Verification Failed! Aborting Mining ...")
        #     break
    # If Tx Verification Fails
    if vd == False:
        print("Mining Aborted!")
        return
    # create block
    blk.compute_hash()
    blk.calculate_merkle_root()
    block = blk
    # add block
    blkChain = BlockChain()
    blkChain.add_block(block)
    print("Block added to this Node's blockchain!")
    # check
    # full Blockchain verify
    # full_verify_message = elec.verification.full_chain_verify()
    # if msg != "verified":
    #    return sync.chainsync()
    # if full_verify_message == "verified":
    # braodcast the block you made
    print("Broadcasting block made by this node ...")
    udphandler = UDPHandler()
    udphandler.sendblock(block.to_json())
Esempio n. 2
0
    def create_block(self):
        block = Block()
        block.create_coinbase_transaction()
        mempool = Mempool()
        blkc = BlockChain()

        mempool_size = mempool.get_len()
        if mempool_size == 0:
            return
        while mempool_size > 0:
            tx = mempool.get_transaction()
            block.add_transaction(tx)
            mempool_size -= 1
        
        block.calcalute_block_size()
        block.calculate_merkle_root()
        block.compute_hash()
        blkc.add_block(block)

        return block
Esempio n. 3
0
    def test_txs_by_address(self):
        blockchain = BlockChain()
        blockchain.flush_chain()
        block = Block()
        tx = Transaction()
        input1 = TransactionInput()
        input1.address = "test_address"
        tx.add_input(input1)
        tx.generate_hash()
        block.add_transaction(tx)

        tx1 = Transaction()
        output = TransactionOutput()
        output.address = "fake_address"
        output1 = TransactionOutput()
        output1.address = "test_address"
        tx1.add_output(output)
        tx1.add_output(output1)
        tx1.generate_hash()
        block.add_transaction(tx1)

        tx2 = Transaction()
        output2 = TransactionOutput()
        output2.address = "fake_address"
        tx2.add_output(output2)
        tx2.generate_hash()

        block.add_transaction(tx2)
        block.compute_hash()

        blockchain.add_block(block)
        txs = blockchain.get_txs_by_addr("test_address")
        getb = blockchain.get_block(-1)
        print(getb.to_json())
        for tx in txs:
            print(tx.hash, end='\n')

        # self.assertEqual(len(txs), 2)
        print(len(txs))
        blockchain.close()
Esempio n. 4
0
from trucoin.TransactionOutput import TransactionOutput
import time
import sys
from utils import getsize, getbytes
output = TransactionOutput()
output.address = "1d3f347aada53547142da8edea5e0019e6ef31bb15"
output.n = 0
output.value = 50
transaction = Transaction()
transaction.add_output(output)
transaction.is_coinbase = True
transaction.hash = "eef9fda50a6bf6c11c5078d8772d94jk"
block = Block()
block.add_transaction(transaction)
block.calculate_merkle_root()
block.compute_hash()
block.miner = "1d3f347aada53547142da8edea5e0019e6ef31bb15jk"
block.size = getbytes(block)
print(block.__dict__)
message = {
    "hash": block.hash,
    "timestamp": block.timestamp,
    "transaction": block.transactions[0].broadcast_transaction(),
    "previous_block_hash": block.previous_block_hash,
    "merkle_root": block.merkle_root,
    "height": block.height,
    "miner": block.miner,
    "version": block.version,
    "size": block.size
}
size = 0