Exemple #1
0
def test_minimal_cryptocurrency():
    """Test basic process of a create a cryptocurrency"""

    # Generate first user
    wallet = Wallet()

    # Create a new currency with 10 units
    blockchain = BlockChain.new_cryptocurrency(wallet.account, 10, timestamp=datetime(2018, 1, 1, 0, 0, 0),
                                               difficulty=4, mining=True)

    # Get the wallets of Alice and Bob
    alice = blockchain.get_wallet(wallet.key)
    bob = blockchain.get_wallet()

    assert alice.get_balance() == 10
    assert bob.get_balance() == 0

    # Alice transfer to Bob 5 units and get 10 units because she mining the block
    assert blockchain.add_transaction(alice.key, bob.account, 5)

    blockchain.generate_candidate(alice.account, timestamp=datetime(2018, 1, 1, 0, 1, 0))
    blockchain.mining_candidate()

    assert alice.get_balance() == 15
    assert bob.get_balance() == 5

    # New users Carol, Dan and Eve
    carol = blockchain.get_wallet()
    dan = blockchain.get_wallet()
    eve = blockchain.get_wallet()

    # Alice transfer 3 to Carol and 4.5 to Dan
    assert blockchain.add_transaction(alice.key, carol.account, 3)
    assert blockchain.add_transaction(alice.key, dan.account, 4.5)

    # Bon transfer 2.2 to Dan
    assert blockchain.add_transaction(bob.key, dan.account, 2.2)

    # Eve mine the Block
    blockchain.generate_candidate(eve.account, timestamp=datetime(2018, 1, 1, 0, 2, 0))
    blockchain.mining_candidate()

    assert alice.get_balance() == 7.5
    assert bob.get_balance() == 2.8
    assert carol.get_balance() == 3
    assert dan.get_balance() == 6.7
    assert eve.get_balance() == 10
def test_minimal_cryptocurrency():
    """Test the operation with a minimal cryptocurrency"""

    wallet_1 = Wallet('aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518')

    # Launch a new currency
    blockchain = BlockChain.new_cryptocurrency(wallet_1.public,
                                               100,
                                               timestamp=datetime(
                                                   2000, 1, 1, 0, 0, 0),
                                               difficulty=4,
                                               mining=True)

    # Open the wallets
    wallet_1 = blockchain.get_wallet(
        'aedc3975fa118bec4a1d203cd2b996c4ceb5aa398b7f7518')
    wallet_2 = blockchain.get_wallet(
        '7d6433bcc63f973580dc7562d2ca79fcb12bb4e08c7e7333')
    wallet_3 = blockchain.get_wallet(
        '3d66f0ea52a2c5cf42893560d5522e82621790edeb7f609b')

    # Ask for the accounts
    assert wallet_1.get_balance() == 100
    assert wallet_2.get_balance() == 0
    assert wallet_3.get_balance() == 0

    # Generate an unspent transaction
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public,
                                      190) is False
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public, 90)
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public,
                                      90) is False

    # Generate a candidate to mining
    blockchain.generate_candidate(wallet_1.public,
                                  timestamp=datetime(2000, 1, 1, 0, 1, 0))
    blockchain.mining_candidate()

    # Ask for the accounts
    assert wallet_1.get_balance() == 110
    assert wallet_2.get_balance() == 90
    assert wallet_3.get_balance() == 0

    # Move more operations
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public, 10)
    assert blockchain.add_transaction(wallet_2.private, wallet_3.public, 20)

    # Generate a candidate to mining
    blockchain.generate_candidate(wallet_1.public,
                                  timestamp=datetime(2000, 1, 1, 0, 2, 0))
    blockchain.mining_candidate()

    # Ask for the accounts
    assert wallet_1.get_balance() == 200
    assert wallet_2.get_balance() == 80
    assert wallet_3.get_balance() == 20

    # Move more operations
    assert blockchain.add_transaction(wallet_1.private, wallet_2.public, 25)
    assert blockchain.add_transaction(wallet_1.private, wallet_3.public, 25)
    assert blockchain.add_transaction(wallet_2.private, wallet_3.public, 50)

    # Generate a candidate to mining
    blockchain.generate_candidate(wallet_2.public,
                                  timestamp=datetime(2000, 1, 1, 0, 3, 0))
    blockchain.mining_candidate()

    # Ask for the accounts
    assert wallet_1.get_balance() == 150
    assert wallet_2.get_balance() == 155
    assert wallet_3.get_balance() == 95