Blockchain Programming

The blockchain is becoming a big part of transaction processing for example with the boom of digital currencies, so it is an exciting area of programming to explore. We can do that with GDScript to simulate a blockchain and transactions on it.

Disclaimer: this article represents the understanding of the topic by the author.

What is a blockchain?

In simple terms it is a digital record of historical transactions in a linked list. If any of the previous transaction records are altered then the integrity of the list is compromised and is easily detected via a verification process.

The transaction records contain a timestamp, a link to the previous transaction, and transaction details.

Multiple transaction details are usually combined before a new record is created for efficiency since they take time to resolve. The final record entry is called a token. These digital tokens are often digital coins, such as Bitcoin.

How to encode the data

To encode the data, we use a hash function. This creates a unique digital signature for a chunk of data that is hard to decode, but easy to encode and verify.

It’s similar to how secure login is implemented on websites where the site does not know your password, but it does know what signature your password entry produces when passed through a password verification function.

So, if you forget your password, you need to generate a new one which involves receiving a secure link to your email or phone. Then you enter the new password over an encrypted (HTTPS) link once. At this stage, the password could be copied by the website, so it’s better if they never know it.

They want to know it initially to check for dumb password entries that are not secure and easy to guess. Or they could provide you with an app to access their services that automatically handles the secure login for you.

We can use the SHA256 hash function to generate a signature of our password on our own local device and send this hash value to a website since they can use the same algorithm which is commonly used.

To guess the input to this hash function that produces the correct hash requires huge super-computing power, so it is not worth the effort. Yet, it is quick and easy to check that the input value produces a particular hash value for purposes of verification.

var hashed_data = "Text to encode".sha256_text()

In our blockchain, we may use the hash value as a reference to link the records together in the linked list. The hash value may be used as a hash map key in a Dictionary data object.

Transaction Encoding

A transaction needs an owner since it may indicate a purchase of an asset for example. So the owner needs a key to prove that they own the asset. So to enable this, we may make use of public and private keys. When somebody takes ownership of an asset, they should create a private key and submit the corresponding public key that will be recorded in the blockchain.

Decentralization

One of the key features of blockchain technology is Decentralization. This allows for many copies of the data to be preserved in diverse locations and this should help preserve its integrity. In turn, this requires synchronization to keep the copies up to date.

Where many transactions may take place, they need to be bundled up and periodically added to the blockchain rather than being added immediately. This allows for transactions to expire or be cancelled before they are locked in.

Verification

Integrity of the blockchain may be verified by working back through the records (tokens) by evaluating the hash of the previous token and comparing that to the previous hash value that is stored in the current token.

Transaction verification is where payment for work may be obtained such as charging a fee for transaction processing. Another idea is to grant ownership of a minted token to whoever does the verification work.

Smart contracts

A smart contract is a program with the terms of the agreement between buyer and seller being directly written in code.

GDScript code for blockchain

It seems like a pretty easy task to code a Blockchain, doesn’t it? Basically, it is a linked list of data records. Complexity creeps in when we want to handle large volumes of transactions, bundle them up, decide how create the new tokens, queue up the pending transactions, and handle the decentralization of the network. Then we need to be able to verify past transactions, and token ownership etc.

Example Blockchain Code

So I wrote some GDScript Code to simulate a Blockchain, and here it is:

Blockchain Code

More Articles