Python blockchain creates multiple transactions
Creating Multiple Transactions in Python Blockchain
Transactions from various clients are queued in the system; miners pick up transactions from this queue and add them to blocks. They then mine the block, and the winning miner is given the privilege of adding the block to the blockchain, earning some money.
We’ll describe this mining process later when we discuss blockchain creation. Before we write the code for multiple transactions, let’s add a small utility function to print the contents of a given transaction.
Displaying Transactions
The display_transaction function accepts a single argument of type transaction. The dictionary object within the received transaction is copied into a temporary variable called dict, and the various values are printed to the console using the dictionary’s keys.
def display_transaction(transaction):
#for transaction in transactions:
dict = transaction.to_dict()
print ("sender: " + dict['sender'])
print ('-----')
print ("recipient: " + dict['recipient'])
print ('-----')
print ("value: " + str(dict['value']))
print ('-----')
print ("time: " + str(dict['time']))
print ('-----')
Next, we define a transaction queue to store our transaction objects.
Transaction Queue
To create a queue, we declare a global list variable called transactions as shown below.
transactions = []
We will simply append each newly created transaction to this queue. Note that for the sake of brevity, we will not implement queue management logic in this tutorial.
Creating Multiple Clients
Now, we will begin creating transactions. First, we will create four clients who will send money to each other in exchange for various services or goods from others.
Dinesh = Client()
Ramesh = Client()
Seema = Client()
Vijay = Client()
At this point, we have four clients: Dinesh, Ramesh, Seema, and Vijay. We will currently assume that each of these clients holds some TPCoins in their wallets for transactions. The identities of these clients will be specified using the identity properties of these objects.
Creating Your First Transaction
Now, let’s initiate our first transaction as shown below
t1 = Transaction(
Dinesh,
Ramesh.identity,
15.0
)
In this transaction, Dinesh sends Ramesh 5 TPCoins. For the transaction to succeed, we must ensure that Dinesh has enough funds in his wallet to cover this amount. Note that we will need a genesis transaction to initiate the circulation of TPCoins in the system. As you read on, you will soon write the transaction code for this genesis transaction.
We’ll sign this transaction using Dinesh’s private key and add it to the transaction queue as shown below.
t1.sign_transaction()
transactions.append(t1)
Following Dinesh’s first transaction, we’ll create several more transactions between the different clients we created above.
Adding More Transactions
We’ll now create more transactions, each giving a certain amount of TPCoins to the other party. When someone spends a TPCoin, they don’t necessarily need to check if they have enough TPCoins in this wallet. Regardless, miners will validate each transaction to confirm the balance the sender had at the time the transaction was initiated.
If the balance is insufficient, the miner will mark the transaction as invalid and will not add it to the block.
The following code creates and adds nine transactions to our queue.
t2 = Transaction(
Dinesh,
Seema.identity,
6.0
)
t2.sign_transaction()
transactions.append(t2)
t3 = Transaction(
Ramesh,
Vijay.identity,
2.0
)
t3.sign_transaction()
transactions.append(t3)
t4 = Transaction(
Seema,
Ramesh.identity,
4.0
)
t4.sign_transaction()
transactions.append(t4)
t5 = Transaction(
Vijay,
Seema.identity,
7.0
)
t5.sign_transaction()
transactions.append(t5)
t6 = Transaction(
Ramesh,
Seema.identity, 3.0
)
t6.sign_transaction()
transactions.append(t6)
t7 = Transaction(
Seema,
Dinesh.identity,
8.0
)
t7.sign_transaction()
transactions.append(t7)
t8 = Transaction(
Seema,
Ramesh.identity,
1.0
)
t8.sign_transaction()
transactions.append(t8)
t9 = Transaction(
Vijay,
Dinesh.identity,
5.0
)
t9.sign_transaction()
transactions.append(t9)
t10 = Transaction(
Vijay,
Ramesh.identity, 3.0
)
t10.sign_transaction()
transactions.append(t10)
When you run the above code, you will have ten transactions queued for miners to create their blocks.
Dumping Transactions
As a blockchain administrator, you may want to periodically review the contents of the transaction queue. To do this, you can use the display_transaction function we developed earlier. To dump all transactions in the queue, simply iterate over the transaction list and, for each referenced transaction, call the display_transaction function, as shown.
for transaction in transactions:
display_transaction (transaction)
print ('--------------')
The transactions are separated by a dotted line. If you run the above code, you will see the following transaction list:
for transaction in transactions:
display_transaction (transaction)
print ('--------------')
The transactions are separated by a dotted line. If you run the above code, you will see the following transaction list:
for transaction in transactions:
display_transaction (transaction)
print ('--------------')
line-numbers”>sender:
30819f300d06092a864886f70d010101050003818d0030818902818100bb064c99c49214
4a9f463480273aba93ac1db1f0da3cb9f3c1f9d058cf499fd8e54d244da0a8dd6ddd329e
c86794b04 d773eb4841c9f935ea4d9ccc2821c7a1082d23b6c928d59863407f52fa05d8b
47e5157f8fe56c2ce3279c657f9c6a80500073b0be8093f748aef667c03e64f04f84d311
c4d866c12d79d3fc3034563dfb0203010001
— —
recipient:
30819f300d06092a864886f70d010101050003818d0030818902818100be93b516b28c6e
674abe7abdb11ce0fdf5bb728b75216b73f37a6432e4b402b3ad8139b8c0ba541a72c8ad
d126b6e1a1308fb98 b727beb63c6060356bb177bb7d54b54dbe87aee7353d0a6baa93977
04de625d1836d3f42c7ee5683f6703259592cc24b09699376807f28fe0e00ff882974484
d805f874260dfc2d1627473b910203010001
—–
value: 15.0
—–
time: 2019-01-14 16:18:01.859915
—–
—————
sender:
30819f300d06092a864886f70d010101050003818d0030818902818100bb064c99c49214
4a9f463480273aba93ac1db1f0da3cb9f3c1f9d058cf499fd8e54d244da0a8dd6 ddd329e
c86794b04d773eb4841c9f935ea4d9ccc2821c7a1082d23b6c928d59863407f52fa05d8b
47e5157f8fe56c2ce3279c657f9c6a80500073b0be8093f748aef667c03e64f04f84d311
c4d866c12d79d3fc3034563dfb020 3010001
—–
recipient:
30819f300d06092a864886f70d010101050003818d0030818902818100a070c82b34ae14
3cbe59b3a2afde7186e9d5bc274955d8112d87a00256a35369acc4d0edfe65e8f9dc93fb
d9ee74b9e7ea 12334da38c8c9900e6ced1c4ce93f86e06611e656521a1eab561892b7db0
961b4f212d1fd5b5e49ae09cf8c603a068f9b723aa8a651032ff6f24e5de00387e4d0623
75799742a359b8f22c5362e5650203010001
—–
value: 6.0
—–
time: 2019-01-14 16:18:01.860966
—–
—————
sender:
30819f300d06092a864886f70d010101050003818d0030818902818100be93b516b28c6e
674abe7abdb11ce0fdf5bb728b75216b73f37a6432e4b402b3ad8139b8c0ba541 a72c8ad
d126b6e1a1308fb98b727beb63c6060356bb177bb7d54b54dbe87aee7353d0a6baa93977
04de625d1836d3f42c7ee5683f6703259592cc24b09699376807f28fe0e00ff882974484
d805f874260dfc2d1627473b91020 3010001
—–
recipient:
30819f300d06092a864886f70d010101050003818d0030818902818100cba097c0854876
f41338c62598c658f545182cfa4acebce147aedf328181f9c4930f14498fd03c0af6b0cc
e25be99452a8 1df4fa30a53eddbb7bb7b203adf8764a0ccd9db6913a576d68d642d8fd47
452590137869c25d9ff83d68ebe6d616056a8425b85b52e69715b8b85ae807b84638d8f0
0e321b65e4c33acaf6469e18e30203010001
—–
value: 2.0
—–
time: 2019-01-14 16:18:01.861958
—–
————–
For brevity, I’ve only printed the first few transactions in the list. In the code above, we print all transactions starting with the first one, except for the genesis transaction, which has never been added to the list. Since transactions are added to blocks periodically, you’re usually only interested in viewing the list of transactions that haven’t been mined yet. In this case, you’ll need to create an appropriate for loop to iterate over the unmined transactions.
So far, you’ve learned how to create clients, allow them to trade with each other, and maintain a queue of pending transactions. Now comes the most important part of this tutorial: creating a blockchain itself. You’ll learn this in the next lesson.