예제 #1
0
#!/usr/bin/env python3

from blockchain import BlockChain, public_key_to_address, ripemd160_to_address

from collections import defaultdict

import sys

BALANCES = defaultdict(int)
OUTPUTS = {}

filename = sys.argv[1]
with open(filename, "rb") as f:
    data = f.read()
    block_chain = BlockChain(data)
    for block in block_chain.blocks():
        for transaction in block["transactions"]:
            for inp in transaction["inputs"]:
                if inp["transaction_hash"] == b"0000000000000000000000000000000000000000000000000000000000000000":
                    pass  # generated
                else:
                    address, value = OUTPUTS[(inp["transaction_hash"], inp["transaction_index"])]
                    BALANCES[address] -= value
            for output_num, output in enumerate(transaction["outputs"]):
                transaction_hash = transaction["hash"]
                index = output_num
                value = output["value"]
                script = output["script"]
                if len(script) == 2 and script[1] == "OP_CHECKSIG":
                    address = public_key_to_address(script[0])
                elif len(script) == 5 and (
예제 #2
0
 countercurrent = 0
 synchronized = 0
 if receivedblockchain.length() == block_chain.length():
     for i in range(receivedblockchain.length() - 1, -1, -1):
         counterreceived += 1
         for j in range(block_chain.length() - 1, -1, -1):
             countercurrent += 1
             #if match is found, take the longer blockchain as the current one
             if receivedblockchain.blocks[
                     i].hash == block_chain.blocks[j].hash:
                 if counterreceived > countercurrent:
                     block_chain.replacechain(
                         receivedblockchain)
                     print("Replacing blockchain")
                 elif counterreceived == countercurrent:
                     block_chain.blocks = receivedblockchain.blocks
                 else:
                     updateothers = pickle.dumps(block_chain)
                     sock.sendall(b"update chain")
                     sock.sendall(updateothers)
                 synchronized = 1
                 break
 elif receivedblockchain.length() > block_chain.length():
     block_chain.blocks = receivedblockchain.blocks
     synchronized = 1
 else:
     data_to_send = pickle.dumps(block_chain)
     sock.sendall(b"update chain")
     sock.sendall(data_to_send)
     synchronized = 1
 #if no match is found, print error message.
예제 #3
0
from blockchain import BlockChain

if __name__ == '__main__':
    my_block_chain = BlockChain(debug=True)
    my_block_chain.new_block('Second Block')
    my_block_chain.new_block('Third Block')
    for block in range(0, 10):
        my_block_chain.new_block('Block %d' % block)
    print(my_block_chain.blocks())
예제 #4
0
#!/usr/bin/env python3

from blockchain import BlockChain, public_key_to_address, ripemd160_to_address

from collections import defaultdict

import sys

BALANCES = defaultdict(int)
OUTPUTS = {}

filename = sys.argv[1]
with open(filename, "rb") as f:
    data = f.read()
    block_chain = BlockChain(data)
    for block in block_chain.blocks():
        for transaction in block["transactions"]:
            for inp in transaction["inputs"]:
                if inp["transaction_hash"] == b"0000000000000000000000000000000000000000000000000000000000000000":
                    pass  # generated
                else:
                    address, value = OUTPUTS[(inp["transaction_hash"],
                                              inp["transaction_index"])]
                    BALANCES[address] -= value
            for output_num, output in enumerate(transaction["outputs"]):
                transaction_hash = transaction["hash"]
                index = output_num
                value = output["value"]
                script = output["script"]
                if len(script) == 2 and script[1] == "OP_CHECKSIG":
                    address = public_key_to_address(script[0])