예제 #1
0
class TestBlockChain(TestCase):
    def setUp(self):
        self.block_chain = BlockChain()

    def test_get_size(self):
        self.assertEquals(len(self.block_chain.chain),
                          self.block_chain.get_size(),
                          "# Chain Size does not match!!")

    def test_get_last_block(self):
        last_block = self.block_chain.get_last_block()
        self.assertIsNotNone(last_block.Hash)
        self.assertIsNotNone(last_block.Index)
        self.assertIsNotNone(last_block.PreviousHash)
        self.assertIsNotNone(last_block.Transactions)
        self.assertIsNotNone(last_block.Timestamp)

    def test_create_transaction(self):
        transaction = self.block_chain.create_transaction("A", "B", 100)
        self.assertEqual(
            transaction.From, "A",
            "Sender and From in Transaction created not matching ")
        self.assertEqual(
            transaction.To, "B",
            "Recipient and To in Transaction created not matching ")
        self.assertEqual(transaction.Amount, 100,
                         "Amount and Amount in Transaction not matching")

    def test_create_block(self):
        block = self.block_chain.create_block(is_genesis=False)
        self.assertEqual(block.Index, len(self.block_chain.chain))
        self.assertIsNotNone(block.Hash)
        self.assertIsNotNone(block.PreviousHash)
        self.assertIsNotNone(block.Transactions)
        self.assertIsNotNone(block.Timestamp)
        self.assertEqual(block, self.block_chain.get_last_block())
        self.assertEqual(self.block_chain.current, [])

    def test_get_chain(self):
        chain = self.block_chain.get_chain()
        self.assertEqual(chain, self.block_chain.chain)

    def test_validate_proof(self):
        block1 = self.block_chain.create_block(is_genesis=False)
        block2 = self.block_chain.create_block(is_genesis=False)
        self.assertTrue(
            BlockChain.validate_proof(last_proof=block1.Hash,
                                      proof=block2.Hash,
                                      last_hash=BlockChain.hash_block(block1)),
            "Proof of Work Hash in the last block does not align with Second Last block. Chain Corrupt!!!"
        )
예제 #2
0
            print("help  - - - - - List possible commands")
            print("address - - - - Return account address")
            print("mine  - - - - - Start mining")
            print("balance - - - - Account balance")
            print("exit  - - - - - Exit wallet")

        elif command.strip() == 'mine':

            # Setup our blockchain
            leading_zeros = 10
            blockchain = BlockChain(100, (2**(256 - leading_zeros)) - 1,
                                    public_key)

            # Add some blocks
            transaction = generate_transaction(0, public_key, 100, private_key)
            blockchain.create_block(transaction)
            transaction = generate_transaction(0, public_key, 100, private_key)
            blockchain.create_block(transaction)
            print(blockchain.blocks[0].get_json())
            print(blockchain.blocks[1].get_json())
            print(blockchain.blocks[2].get_json())

        elif command.strip() == 'balance':
            print(blockchain.account_balances)

        else:
            print(
                "Error: Not supported command, type 'help' for list of supported commands"
            )
        command = input("Enter Command: ")
예제 #3
0
class ServerCore:
    def __init__(self,
                 my_port=50082,
                 core_node_host=None,
                 core_node_port=None):
        self.server_state = STATE_INIT
        print('Initializing server...')
        self.my_ip = utils.get_host()
        print('Server IP address is set to ... ', self.my_ip)
        self.my_port = my_port
        self.cm = ConnectionManager(self.my_ip, self.my_port,
                                    self.__handle_message)
        self.core_node_host = core_node_host
        self.core_node_port = core_node_port
        self.miners_wallet = Wallet()
        self.blockchain = BlockChain(self.miners_wallet.blockchain_address)
        self.mining_semaphore = threading.Semaphore(1)
        self.__print_info()

    def __print_info(self):
        print({
            'private_key': self.miners_wallet.private_key,
            'public_key': self.miners_wallet.public_key,
            'blockchain_address': self.miners_wallet.blockchain_address
        })

    def start(self):
        """
            Coreノードとしての待受を開始
        """
        self.server_state = STATE_STANDBY
        self.cm.start()
        self.start_mining()

    def join_network(self):
        """
            事前に取得した情報に従い拠り所となる他のCoreノードに接続
        """
        if self.core_node_host is not None:
            self.server_state = STATE_CONNECTED_TO_NETWORK
            self.cm.join_network(self.core_node_host, self.core_node_port)
        else:
            print('This server is runnning as Genesis Core Node...')

    def shutdown(self):
        """
            待ち受け状態のServer Socketを閉じて終了
        """
        self.server_state = STATE_SHUTTING_DOWN
        print('Shutdown server...')
        self.cm.connection_close()

    def start_mining(self):
        print('start mining')
        is_acquire = self.mining_semaphore.acquire(blocking=False)
        if is_acquire:
            with contextlib.ExitStack() as stack:
                stack.callback(self.mining_semaphore.release)
                self.__mining()
                mining_interval = self.blockchain.mining_speed + random.uniform(
                    9.8, 10.3)
                loop = threading.Timer(round(mining_interval),
                                       self.start_mining)
                loop.start()

    def __mining(self):
        start = time.time()
        self.blockchain.add_transaction(
            sender_blockchain_address=MINING_SENDER,
            recipient_blockchain_address=self.blockchain.blockchain_address,
            value=MINING_REWARD)
        nonce = self.blockchain.proof_of_work()
        if nonce == -1:
            return False
        previous_hash = self.blockchain.hash(self.blockchain.chain[-1])
        is_created, block = self.blockchain.create_block(nonce, previous_hash)
        if not is_created:
            return False
        self.delete_transaction_for_all_peer()

        print({'action': 'mining', 'status': 'success'})
        utils.pprint(self.blockchain.chain)

        self.send_all_chain_for_consensus()

        elapse = round(time.time() - start, 4)
        self.blockchain.difficulty_adjustment(elapse)

        # print('mining speed : ', str(round(self.mining_speed, 3)))
        # print('difficult : ', str(self.difficulty))

        return True

    def delete_transaction_for_all_peer(self):
        """
            ノードのトランザクションプールをからにするメッセージの送信
        """
        new_message = self.cm.get_message_text(MSG_DELETE_TRANSACTION)
        self.cm.send_msg_to_all_peer(new_message)

    def send_all_chain_for_consensus(self):
        """
            チェーンを全てのノードに送信してコンセンサス
        """
        my_chain = self.blockchain.chain
        chain_data = json.dumps(my_chain)
        new_message = self.cm.get_message_text(RSP_FULL_CHAIN, chain_data)
        self.cm.send_msg_to_all_peer(new_message)

    def __handle_message(self, msg, is_core, peer=None):
        """
            ConnectionManagerに引き渡すコールバックの中身。
        """
        print('message_type : ', msg[2])
        if peer is not None:
            if msg[2] == MSG_REQUEST_FULL_CHAIN:
                # walletのチェーンを同期
                print('Send our latest blockchain for reply to : ', peer)
                my_chain = self.blockchain.chain
                chain_data = json.dumps(my_chain)
                new_message = self.cm.get_message_text(RSP_FULL_CHAIN,
                                                       chain_data)
                self.cm.send_msg(peer, new_message)
            elif msg[2] == MSG_REQUEST_KEY_INFO:
                # walletにminerの鍵情報を渡す
                key_info = pickle.dumps(self.miners_wallet, 0).decode()
                m_type = MSG_KEY_INFO
                message = self.cm.get_message_text(m_type, key_info)
                self.cm.send_msg(peer, message)
            elif msg[2] == MSG_DELETE_TRANSACTION:
                # transaction poolを空に
                print('DELETE_TRANSACTION is called')
                self.blockchain.transaction_pool = []
        else:
            if msg[2] == MSG_NEW_TRANSACTION:
                print('NEW_TRANSACTION command is called')
                payload = json.loads(msg[4])
                new_transaction = utils.sorted_dict_by_key({
                    'sender_blockchain_address':
                    payload['sender_blockchain_address'],
                    'recipient_blockchain_address':
                    payload['recipient_blockchain_address'],
                    'value':
                    float(payload['value'])
                })
                current_transactions = self.blockchain.transaction_pool.copy()
                print('new transaction : ', new_transaction)
                if new_transaction in current_transactions:
                    print('transaction is already in pool')
                    return
                else:
                    signature = pickle.loads(
                        payload['signature'].encode('utf8'))
                    print('signature', signature)
                    is_transacted = self.blockchain.add_transaction(
                        payload['sender_blockchain_address'],
                        payload['recipient_blockchain_address'],
                        payload['value'], payload['sender_public_key'],
                        signature)
                    if is_transacted:
                        print('new transaction is generated')
                        print('\n\ntransaction_pool',
                              self.blockchain.transaction_pool)
                    if not is_core:
                        # ウォレットからのトランザクションはブロードキャスト
                        print('transaction bloadcasted')
                        m_type = MSG_NEW_TRANSACTION
                        new_message = self.cm.get_message_text(m_type, msg[4])
                        self.cm.send_msg_to_all_peer(new_message)
            elif msg[2] == RSP_FULL_CHAIN:
                print('RSP_FULL_CHAIN command is called')
                if not is_core:
                    return

                new_block_chain = json.loads(msg[4])
                self.blockchain.resolve_conflicts(new_block_chain)
예제 #4
0
#!/usr/bin/env python3.6
# -*- encoding: utf-8 -*-
from blockchain import BlockChain

# Create the blockchain and add the genesis block
blockchain = BlockChain()

# How many blocks should we add to the chain
# after the genesis block
num_of_blocks_to_add = 20

# Add blocks to the chain
for i in range(0, num_of_blocks_to_add):
    new_block = blockchain.create_block("Hey! I'm block " + str(i + 1))
    last_block = blockchain.append_block(new_block)
    # Tell everyone about it!
    print("Block #{} has been added to the blockchain!".format(
        last_block.get_index()))
    print("Hash: {}".format(last_block.get_hash()))
    print("Data: {}\n".format(last_block.get_data()))
예제 #5
0
w1 = Wallet("x")
p_key = w1.public_key.exportKey()
print(p_key)
w = Wallet.findWallet(p_key)
print(vars(w))

w2 = Wallet("asdf")
w3 = Wallet("a")
w4 = Wallet("b")

w1.amount = 100

t1 = Transaction(w1, w2, 5)
t2 = Transaction(w1, w3, 5)
t3 = Transaction(w1, w4, 5)


block_chain = BlockChain()
block_chain.create_block([t1, t2, t3])

t4 = Transaction(w1, w2, 5)
t5 = Transaction(w1, w3, 5)
t6 = Transaction(w1, w4, 5)

block_chain.create_block([t4, t5, t6])




block_chain.print_blocks()
예제 #6
0
#!/usr/bin/env python3.6
# -*- encoding: utf-8 -*-
from blockchain import BlockChain
from tclearn_core import *
import json

if __name__ == '__main__':
    # Create the blockchain and add the genesis block
    blockchain = BlockChain()

    # How many blocks should we add to the chain
    # after the genesis block
    num_of_blocks_to_add = 20

    # Add blocks to the chain
    for i in range(0, num_of_blocks_to_add):
        block_data = TCLearnData("XXX-%d" % (i + 1), 0, [0, 1, 2])
        new_block = blockchain.create_block(block_data)
        print("Here is my new block:\n%s\n" % new_block)
        last_block = blockchain.append_block(new_block)
        # Tell everyone about it!
        print("Block #{} has been added to the blockchain!".format(
            last_block.get_index()))
        print("Hash: {}".format(last_block.get_hash()))
        print("Data:")
        data = json.loads(last_block.get_data())
        for k in data.keys():
            print("\t%s: %s" % (k, data[k]))
        print()