Пример #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():