Ejemplo n.º 1
0
    def add_block(self, transactions, fee=0):  # change add "fee"  6.19
        last_block = self.get_last_block()
        prev_hash = last_block.get_header_hash()
        height = last_block.block_header.height + 1
        block_header = BlockHeader('', height, prev_hash)

        # reward to wallets[0]
        wallets = Wallets()
        keys = list(wallets.wallets.keys())
        w = wallets[keys[0]]
        coin_base_tx = self.coin_base_tx(w.address, fee)  # change 6.19
        transactions.insert(0, coin_base_tx)

        utxo_set = UTXOSet()
        # txs = transactions  # change 6.21
        txs = utxo_set.clear_transactions(
            transactions)  # change clear transactions(add sort)

        block = Block(block_header, txs)
        block.mine(self)
        block.set_header_hash()
        self.db.create(block.block_header.hash, block.serialize())
        last_hash = block.block_header.hash
        self.set_last_hash(last_hash)

        utxo_set.update(block)
Ejemplo n.º 2
0
 def deserialize(cls, data):
     block_header_dict = data['block_header']
     block_header = BlockHeader.deserialize(block_header_dict)
     transactions = data["transactions"]
     txs = []
     for transaction in transactions:
         txs.append(Transaction.deserialize(transaction))
     return cls(block_header, txs)
Ejemplo n.º 3
0
 def __init__(self, raw_data):
     self.header = BlockHeader(raw_data[:80])
     self.hash = hashlib.sha256(hashlib.sha256(
         raw_data[:80]).digest()).digest().hex()
     self.transactions = {}
     self.transactions = {
         tx.hash: tx
         for tx in self._get_transactions(raw_data[80:])
     }
Ejemplo n.º 4
0
def read_header(header):
    # Implement these functions to help you
    # Takes a dictionary as an input
    index = header['index']
    timestamp = header['timestamp']
    previous_hash = header['previous_hash']
    nonce = header['nonce']
    block_header = BlockHeader(index, previous_hash, timestamp, nonce)

    return block_header
Ejemplo n.º 5
0
    def add_block(self, transactions=[], fee=0):
        last_block = None
        while not last_block:
            last_block = self.get_last_block()
            time.sleep(5)
        prev_hash = last_block.get_header_hash()
        height = last_block.block_header.height + 1
        block_header = BlockHeader('', height, prev_hash)

        f = open('wallet.dat', 'rb')
        wallets = pickle.load(f)
        keys = list(wallets.keys())
        w = wallets[keys[0]]
        coin_base_tx = self.coin_base_tx(w.address, fee)
        if transactions:
            transactions.insert(0, coin_base_tx)
        else:
            transactions = [coin_base_tx]

        utxo_set = UTXOSet()
        txs = utxo_set.clear_transactions(transactions)

        block = Block(block_header, txs)
        block.mine(self)

        blo = None
        while not blo:
            blo = self.get_last_block()
            time.sleep(5)
        txs1 = blo._transactions
        # if len(transactions) > 1:
        # if txs1:
        # if len(txs1) > 1:
        # if txs1[1].txid == txs[1].txid:
        # return

        block.set_header_hash()
        self.db.create(block.block_header.hash, block.serialize())
        last_hash = block.block_header.hash
        self.set_last_hash(last_hash)

        utxo_set.update(block)
Ejemplo n.º 6
0
 def header(self):
     """Returns a BlockHeader object corresponding to this block"""
     if self._header is None:
         self._header = BlockHeader.from_hex(self.hex[:80])
     return self._header
Ejemplo n.º 7
0
 def new_genesis_block(cls, coin_base_tx):
     block_header = BlockHeader.new_genesis_block_header()
     return cls(block_header, coin_base_tx)
Ejemplo n.º 8
0
def read_header(header):
    # Implement these functions to help you
    # Takes a dictionary as an input
    return BlockHeader(header['index'], header['nonce'],
                       header['previous_hash'], header['timestamp'])
def read_header(header):
    # Implement these functions to help you
    # Takes a dictionary as an input
    return BlockHeader(header["index"], header["previous_hash"],
                       header["timestamp"], header["nonce"])