300 lines of ABAP code to implement one of the simplest blockchain prototypes

I don't know since when, the blockchain suddenly became popular on the Internet.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Here Jerry is out of his way. There are too many blockchain introduction articles on the Internet. This article of mine does not have any lofty terminology, it is 300 lines of ABAP code to implement a simplest blockchain prototype.

I personally think that compared with the implementation technology of the blockchain itself, the more difficult thing is how to find a suitable business scenario, integrate the blockchain into SAP products, and make it work.

This article contains three versions, each version adds some new features on the basis of the previous version.

Version 1: Implementation of two data structures, block and chain

Blockchain, as the name suggests, is a chain of blocks.

The figure below is very similar to the singly linked list we learned in the university computer course "Data Structure". In this version, each block contains the most basic fields: block index, block creation timestamp, current block hash and previous block hash. The pHash field of each block stores the hash value of the previous block, thus forming a linked list. The first node of the linked list, the block with the red head on the far left in the figure below is the genesis block, its index is 0, and the pHash field is empty.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

ABAP implementation of the block: ZCL_BLOCK. The fields shown in the figure above are all modeled in this class. For simplicity, most fields are set to public.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

The hash value of each block is calculated from the values ​​of all other fields of the block as input, calculated by the SHA1 algorithm, and stored in the field mv_hash.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Implementation of the chain: ZCL_BLOCKCHAIN

300 lines of ABAP code to implement one of the simplest blockchain prototypes

  • ADD_BLOCK: Accept an instance of a block as an input parameter, point the pHash of the instance to the block at the end of the current list, so that the instance becomes the new tail block of the list.
  • CONSTRUCTOR: Constructor that performs chain initialization and creates genesis block.
  • GET_BLOCK_BY_INDEX: Access the specified block by index.
  • GET_SIZE: Returns the number of blocks contained in the chain.
  • IS_VALID: Check if the blockchain is valid. The specific check logic will be introduced later.

All the code for the first version is on my github .

Execute the test program ZBLOCKCHAIN_V1, enter the number of blocks you want to create, and you will see the following output: (I chose 5)

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Because SAP GUI does not have UI controls for linked lists, I simulate it with tree controls.

In line 21 of the figure below, I tampered with the content of the block with index 1 in the blockchain to "Change by Jerry", and then executed the is_valid method on line 23 to check:

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Because the set_data method on line 21 modifies the content of the first block, it will trigger the recalculation of its hash value. In this way, the hash of the first block has changed (assuming it changed from YYY to CCC), and the pHash of the second block still points to the old hash value YYY before the first block change, so this blockchain was judged invalid.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

300 lines of ABAP code to implement one of the simplest blockchain prototypes

The output of the above figure comes from the verification method is_valid: traverse each block in the chain, and compare whether the field pHash in the block storing the hash value of the previous block is consistent with the hash value of the block located in the previous position of the block. .

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Version 2: Increased mining cost, added support for Proof of Work (POW)

The address of the second version code is on my github .

This version of the chain implementation class ZCL_BLOCKCHAIN_V2 has an additional input parameter to the constructor: iv_difficulty. What is the use of this parameter?

Looking closely at the tree output of the first version of the test program, you can see that there is no pattern in the hash value of each block. The input parameter of the second version is to increase the calculation cost of the hash value, that is, only when the calculated hash value meets certain rules, the hash value can be accepted by the blockchain. The parameter iv_difficulty defines the number of leading zeros that can be accepted for hash values.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

For example I specify the number of leading zeros to be 3:

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Execution result: You can see that the first three digits of all hash values ​​are zero.

There are two problems here:

  • What is the meaning of Nonce in the last column of the figure below?
  • How does the parameter iv_difficulty participate in the calculation of the hash value?

300 lines of ABAP code to implement one of the simplest blockchain prototypes

First, a new member field mv_nonce is added to the implementation class ZCL_BLOCK of the block:

300 lines of ABAP code to implement one of the simplest blockchain prototypes

In the method add_block which adds a block instance to the chain, a method mine is added.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Inside this method is a loop that computes a hash value within the body of the loop and checks to see if it contains the specified number of leading zeros. If not, increment mv_nonce by 1 and continue the loop. mv_nonce also participates in the hash calculation as part of the input. That is to say, the value of the final block field mv_nonce represents how many times the calculation has passed before obtaining a legal hash value that meets the requirements of leading zero digits. The process of obtaining a legal hash value through continuous attempts in the loop is commonly known as "mining" in the blockchain circle.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

On my test system, creating 10 blocks with 4 leading zeros took a total of 10 seconds.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

From the time spent, it can be seen that POW is actually a mechanism that avoids the block chain being filled with garbage or the block content being tampered by introducing a hash calculation that requires a certain amount of work.

Version 3: Use blockchain to record transaction details and increase mining rewards

Source code for this version:

https://github.com/i042416/KnowlegeRepository/tree/master/ABAP/blockchain/v3

Use ZCL_TRANSACTION to represent a transaction, which contains three fields: mv_from_address (payer), mv_to_address (payee) and mv_amount (transaction amount).

300 lines of ABAP code to implement one of the simplest blockchain prototypes

In this version, the first enhancement is ZCL_BLOCK3: the mv_index and mv_data fields used in the previous two versions are removed, and a field mt_transaction is added, which stores a collection of transactions.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

When calculating the hash value, each field of the transaction class also participates in the calculation:

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Class ZCL_BLOCKCHAIN_V3 adds a new member variable mt_pending_trans. Each time the method create_transaction is called, a new block is not created to record the transaction, but the transaction is simply added to the pending task queue mt_pending_trans.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

The field mv_mine_reward stores the mining reward, hardcoded to 100.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

This queue of pending tasks will only be processed when the method mine_pending_trans is called.

After calling the mine method of the block instance on line 6, a hash value that conforms to the leading zero specification is calculated. Then the queue of pending tasks is emptied, and a new transaction record is created on line 13 as a reward for mining. The account information of the rewarder is defined by the input parameter iv_award_address.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Now that transaction information is stored in each block, simply traversing these blocks can figure out what the final balance of an account is. The logic used is to traverse each transaction recorded in each block. If an account appears in the payer of the transaction record, the balance is subtracted from the transaction amount of the current transaction (line 5). Otherwise, if the account appears in the sender party, then the balance plus the transaction amount (line 9).

300 lines of ABAP code to implement one of the simplest blockchain prototypes

The test procedure is as follows. Because Tom transferred 100 yuan to Jerry, Jerry transferred 10 yuan to Tom, and then the reward account set in line 15 for mining is Jerry, so the final balance of Jerry is 100-10+100 = 190 yuan.

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Hopefully, after reading this article, you have a basic understanding of how blockchain works. Thanks for reading.

To get more original technical articles from Jerry, please follow the public account "Wang Zixi" or scan the QR code below:

300 lines of ABAP code to implement one of the simplest blockchain prototypes

300 lines of ABAP code to implement one of the simplest blockchain prototypes

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325332835&siteId=291194637