示例#1
0
def mining():
    elec = Election()
    if elec.redis_client.llen("mempool") == 0:
        return

    # Transaction verification 
    blk = Block()
    for i in range(0, elec.redis_client.llen("mempool")):
        tx = elec.redis_client.lindex('mempool', i).decode('utf-8')
        if tx == None:
            # check
            break
        verify_verdict = elec.verification.verify_tx(tx)
        if verify_verdict == "verified":
            # Sending data to block
            blk.add_transaction(tx)

    # create block
    blk.compute_hash()
    blk.calculate_merkle_root()
    block = blk.to_json()

    # add block
    blkChain = BlockChain()
    blkChain.add_block(block)

    # full blockchain verify
    full_verify_message = elec.verification.full_chain_verify()
    if full_verify_message == "verified":
        # braodcast the block you made
        UDPHandler.sendblock(block)
    else:
        return
示例#2
0
    def final_addr_balance(self, addr):
        """ This function scans the complete blockchain and returns
        the total balance of an Address """
        chain_length = self.redis_client.llen('chain')
        total = 0
        used_transactions = set()
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            for tx in block.transactions:
                for inp in tx.inputs:
                    if inp.address == addr:
                        used_transactions.add((inp.previous_tx, inp.index))
                for out_index, out in enumerate(tx.outputs):
                    if tx.hash not in used_transactions:
                        if out.address == addr:
                            total = total + out.value
                    else:
                        if out.address == addr and (
                                tx.hash, out_index) not in used_transactions:
                            total = total + out.value
            chain_length -= 1

        return total
示例#3
0
 def full_chain_verify(self):
     verify_message = "unverified"
     for i in range(0, self.chain_length):
         raw = json.loads(
             self.redis_client.lindex('chain', i).decode('utf-8'))
         block = Block.from_json(raw)
         block.calculate_merkle_root()
         v_merkl_root = block.merkle_root
         if v_merkl_root != raw["merkle_root"]:
             verify_message = "failed"
     verify_message = "verified"
     return verify_message
示例#4
0
    def test_merkle_root(self):
        block = Block()
        tx = Transaction()
        input = TransactionInput()
        input.address = "test_address"
        tx.add_input(input)
        tx.generate_hash()
        block.add_transaction(tx)
        block.calculate_merkle_root()
        initial_merkle_root = block.merkle_root

        tx1 = Transaction()
        input1 = TransactionInput()
        input1.address = "another_test_address"
        tx1.add_input(input1)
        tx1.generate_hash()
        block.add_transaction(tx1)
        block.calculate_merkle_root()
        final_merkle_root = block.merkle_root

        self.assertNotEqual(initial_merkle_root, final_merkle_root)
示例#5
0
    def get_tx_by_hash(self, hash: str):
        chain_length = self.redis_client.llen('chain')
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            for tx in block.transactions:
                if tx.hash == hash:
                    return tx
            chain_length -= 1

        return
示例#6
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)
        blockchain.close()
示例#7
0
    def get_txs_by_addr(self, addr) -> List[Transaction]:
        txs = []
        chain_length = self.redis_client.llen('chain')
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            print(len(block.transactions))
            for tx in block.transactions:
                inp_adresses = [inp.address for inp in tx.inputs]
                if addr in inp_adresses:
                    txs.append(tx)
                    continue
                out_adresses = [out.address for out in tx.outputs]
                if addr in out_adresses:
                    txs.append(tx)
                    continue
            chain_length -= 1

        return txs
示例#8
0
    def get_utxos_by_addr(self, addr) -> List[Tuple[str, int, float]]:
        utxos = []
        chain_length = self.redis_client.llen('chain')
        used_transactions = set()
        while chain_length > 0:
            block = Block.from_json(
                json.loads(
                    self.redis_client.lindex('chain', chain_length -
                                             1).decode('utf-8')))
            for tx in block.transactions:
                for inp in tx.inputs:
                    if inp.address == addr:
                        used_transactions.add((inp.previous_tx, inp.index))
                for out_index, out in enumerate(tx.outputs):
                    if tx.hash not in used_transactions:
                        if out.address == addr:
                            utxos.append((tx.hash, out_index, out.value))
                    else:
                        if out.address == addr and (
                                tx.hash, out_index) not in used_transactions:
                            utxos.append((tx.hash, out_index, out.value))
            chain_length -= 1

        return utxos
示例#9
0
 def get_block(self, index: int) -> Block:
     raw = self.redis_client.lindex("chain", index)
     if raw is not None:
         return Block.from_json(json.loads(raw.decode("utf-8")))
     else:
         return
示例#10
0
 def add_block(self, blk: Block):
     self.redis_client.rpush('chain', json.dumps(blk.to_json()))
示例#11
0
from dotscoin.Block import Block
from dotscoin.Transaction import Transaction
from dotscoin.TransactionInput import TransactionInput
from dotscoin.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,
示例#12
0
                    "n":
                    0,
                    "address":
                    "1d3f347aada53547142da8edea5e0019e6ef31bb15"
                }],
                "hash":
                "eef9fda50a6bf6c11c5078d8772d94df4f60ce54573c009f145eb047426ad0fb",
                "block":
                "16e8dab3a9185d5329fac9cfdc0a81c7817826f701e747cb3751878061e4dc8c"
            }],
            "previous_block_hash":
            "",
            "merkle_root":
            "78bc90dcc3fe2ae1eca8b9d4e09b63653cd18f0b583e002b2b4d43cc09fca9cd",
            "height":
            0,
            "version":
            "0.0.1",
            "size":
            1949
        }))

    for i in range(5):
        block = Block()
        block.add_transaction(
            create_fake_transaction(global_addresses[random.randint(0, 9)]))
        block.calculate_merkle_root()
        block.compute_hash()
        block.calcalute_block_size()

        redis_client.rpush("chain", json.dumps(block.to_json()))
示例#13
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)
示例#14
0
 def __init__(self):
     blockchain = BlockChain()
     self.block = Block()
     self.redis_client = redis.Redis(host='localhost', port=6379, db=0)
     self.chain_length = self.redis_client.llen('chain')