예제 #1
0
    def test_transaction_add_signature(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)

        self.assertTrue(hasattr(transaction, "signature"))
예제 #2
0
    def test_wallet_sign_transaction_valid(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)

        self.assertIsInstance(signature, str)
        self.assertGreater(len(signature), 0)
예제 #3
0
    def test_transaction_verify_transaction_signature(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        verify = transaction.verify_transaction_signature()

        self.assertTrue(verify)
예제 #4
0
    def test_block_initialize(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        block = Block(0, [transaction], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0")

        self.assertIsInstance(block, Block)
예제 #5
0
    def test_blockchain_add_new_transaction_not_enough_balance(self):
        wallet = Wallet()
        blockchain = Blockchain(wallet)
        transaction = Transaction(wallet.pubkey, "CityU", 100)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        blockchain.add_new_transaction(transaction)

        self.assertEqual(len(blockchain.unconfirmed_transactions), 0)
예제 #6
0
    def test_block_to_dict(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0")
        content = block.to_dict()

        self.assertIsInstance(content, dict)
        self.assertEqual(list(content.keys()), ["index", "timestamp", "previous_hash", "merkle_root", "nonce", "difficulty"])
예제 #7
0
    def test_block_compute_hash(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0")
        hash_val = block.compute_hash()

        self.assertIsInstance(hash_val, str)
        self.assertGreater(len(hash_val), 0)
예제 #8
0
    def test_block_to_json(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0")
        content = block.to_json()

        self.assertIsInstance(content, str)
        self.assertGreater(len(content), 0)
예제 #9
0
 def test_blockchain_proof_of_work(self):
     wallet = Wallet()
     blockchain = Blockchain(wallet)
     transaction = Transaction(wallet.pubkey, "CityU", 1)
     signature = wallet.sign_transaction(transaction)
     transaction.add_signature(signature)
     previous_hash = blockchain.last_block["hash"]
     block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), previous_hash)
     computed_hash = blockchain.proof_of_work(block)
     
     self.assertGreater(len(computed_hash), 0)
예제 #10
0
    def test_blockchain_proof_of_work_bad_block(self):
        wallet = Wallet()
        blockchain = Blockchain(wallet)
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        previous_hash = blockchain.last_block["hash"]
        block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), previous_hash)
        delattr(block, "transaction")

        self.assertRaises(AttributeError, blockchain.proof_of_work, block)
예제 #11
0
    def test_blockchain_add_block_wrong_previous_hash(self):
        wallet = Wallet()
        blockchain = Blockchain(wallet)
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        signature = wallet.sign_transaction(transaction)
        transaction.add_signature(signature)
        block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "Wrong")
        computed_hash = blockchain.proof_of_work(block)
        result = blockchain.add_block(block, computed_hash)

        self.assertFalse(result)
예제 #12
0
 def test_blockchain_check_balance_no_chain(self):
     wallet = Wallet()
     blockchain = Blockchain(wallet)
     blockchain.chain = []
     balance = blockchain.check_balance(wallet.pubkey)
     
     self.assertFalse(balance)
예제 #13
0
    def test_transaction_verify_transaction_signature_too_large_signature(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        transaction.add_signature("3379cc2f08e4cde5d24af02611c32693b18f406d4b58fbcd2bbd0acc67b1d")
        verify = transaction.verify_transaction_signature()

        self.assertFalse(verify)
예제 #14
0
    def test_transaction_to_dict(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        content = transaction.to_dict()

        self.assertIsInstance(content, dict)
        self.assertEqual(list(content.keys()), ["sender", "recipient", "value", "fee"])
예제 #15
0
    def test_transaction_to_json(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        content = transaction.to_json()

        self.assertIsInstance(content, str)
        self.assertGreater(len(content), 0)
예제 #16
0
 def test_blockchain_register_node(self):
     wallet = Wallet()
     blockchain = Blockchain(wallet)
     blockchain.register_node("http://127.0.0.1:101")
     blockchain.register_node("127.0.0.1:102")
     
     self.assertTrue("127.0.0.1:101" in blockchain.nodes)
     self.assertTrue("127.0.0.1:102" in blockchain.nodes)
예제 #17
0
    def test_transaction_initialize(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)

        self.assertIsInstance(transaction, Transaction)
예제 #18
0
    def test_wallet_secretkey(self):
        wallet = Wallet()
        secret = wallet.secret

        self.assertIsInstance(secret, str)
        self.assertGreater(len(secret), 0)
예제 #19
0
    def test_wallet_pubkey(self):
        wallet = Wallet()
        pubkey = wallet.pubkey

        self.assertIsInstance(pubkey, str)
        self.assertGreater(len(pubkey), 0)
예제 #20
0
    def test_wallet_initialize(self):
        wallet = Wallet()

        self.assertIsInstance(wallet, Wallet)
예제 #21
0
 def test_blockchain_initialize_create_genesis_block(self):
     wallet = Wallet()
     blockchain = Blockchain(wallet)
     
     self.assertIsInstance(blockchain, Blockchain)
     self.assertEqual(len(blockchain.chain), 1)
예제 #22
0
    def test_blockchain_last_block(self):
        wallet = Wallet()
        blockchain = Blockchain(wallet)
        block = blockchain.last_block

        self.assertGreater(len(block), 0)
예제 #23
0
 def test_blockchain_register_node_wrong_address(self):
     wallet = Wallet()
     blockchain = Blockchain(wallet)
     
     self.assertRaises(ValueError, blockchain.register_node, "")
예제 #24
0
 def test_blockchain_check_balance(self):
     wallet = Wallet()
     blockchain = Blockchain(wallet)
     balance = blockchain.check_balance(wallet.pubkey)
     
     self.assertGreater(balance, 0)
예제 #25
0
    def test_transaction_verify_transaction_signature_no_signature(self):
        wallet = Wallet()
        transaction = Transaction(wallet.pubkey, "CityU", 1)
        verify = transaction.verify_transaction_signature()

        self.assertFalse(verify)
예제 #26
0
from flask import Flask, jsonify, request

from pycoin import Wallet
from pycoin import Transaction
from pycoin import Block
from pycoin import Blockchain

# EE4017 Lab 6

# Initializing Flask framework and the client
# Flask is a lightweight web application framework for Python
# use it to build APIs to interact with the blockchain client through http requests.
app = Flask(__name__)

# Initialize the wallet, the blockchain
myWallet = Wallet()
blockchain = Blockchain(myWallet)

# Flask uses the @app.route() decorator to define an API.
# All API return messages in JSON file format and a number (HTTP status code) behind it.
# All Flask API must be placed outside of all classes and the main method.

@app.route('/status', methods=['GET'])
def status():
    if myWallet and blockchain:
        return "alive", 200
    return "dead", 400


@app.route('/register_node', methods=['POST'])
def register_node():