Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the simplest Block chain prototype with ABAP Code

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to implement the simplest blockchain prototype with ABAP code. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.

ABAP code implements one of the simplest blockchain prototypes.

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

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

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

The following image is similar to the single-linked list we learned in our college computer science class Data Structure. In this version, each block contains the most basic fields: the block index, the block creation timestamp, the hash of the current block, and the hash of the previous block. 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 leftmost red header block in the figure below, is the genesis block, its index is 0, and the pHash field is empty.

ABAP implementation of block: ZCL_BLOCK. The fields shown above are modeled in this class, and most of them are set to public for simplicity.

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

Chain implementation: ZCL_BLOCKCHAIN

ADD_BLOCK: Accepts an instance of a block as an input parameter, and points the pHash of that 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 operations to create genesis block blocks.

GET_BLOCK_BY_INDEX: Access the specified block based on the index.

GET_SIZE: Returns the number of blocks contained in the chain.

IS_VALID: Check if the blockchain is valid. Specific check logic is described later.

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

Run the test program ZBLOCKSHAIN_V1 and enter the number of blocks you want to create. You will see the following output: (I chose 5)

Because SAP GUI doesn't have UI controls for linked lists, I use tree controls to simulate it.

In line 21 of the following figure, I tamper with the block content cited as 1 in the blockchain to "Change by Jerry", and then execute the is_valid method in line 23 to check:

Because the set_data method on line 21 modifies the contents of the first block, it triggers a recalculation of its hash value. Thus the hash of the first block changes (say from YYY to CCC), while the pHash of the second block still points to the old hash value YYY before the change of the first block, so the block chain is judged invalid.

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

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

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

This version of the chain implementation class ZCL_BLOCKCHAIN_V2 constructor adds an input parameter: iv_difficult. What is the use of this parameter?

A closer look at the tree-like output of the first version of the test program shows that there is no pattern in the hash value of each block. The second version of this input parameter is to increase the calculation cost of the hash value, that is, only if the calculated hash value satisfies certain rules, the hash value can be accepted by the blockchain. The iv_difficulty parameter defines the number of leading zeros that can be accepted as hash values.

For example, if I specify the number of leading zeros as 3:

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

Here are two questions:

What is the meaning of Nonce in the last column of the picture below?

How does the iv_difficulty parameter participate in the hash calculation?

First add a new member field mv_nonce to the block implementation class ZCL_BLOCK:

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

This method consists of a loop that computes a hash value inside the loop and checks whether it contains a specified number of leading zeros. If not, increment mv_nonce by 1 and continue the loop. mv_nonce also participates in hashing as part of the input. That is, the value of mv_nonce in the final block field represents how many calculations have been performed before a valid hash value that meets the leading zero number requirement is obtained. The process of trying to get a legal hash value in a loop is commonly known as "mining" in blockchain circles.

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

From the time spent, POW is actually a mechanism to avoid garbage filling or tampering with block contents by introducing hash calculations that require a certain amount of work.

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

This version of the source code:

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).

In this version, the first enhancement is ZCL_BLOCK3: the mv_index and mv_data fields used in the previous two versions have been removed and a field mt_transaction has been added to store the set of transactions.

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

Class ZCL_BLOCKSHAIN_V3 adds a new member variable mt_pending_trans. Each call to create_transaction does not create a new block to record the transaction, but simply adds the transaction to mt_pending_trans.

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

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

After the mine method call on the block instance in line 6, a hash value that meets the leading zero specification is computed. The queue of pending tasks is then cleared, and a new transaction record is created in line 13 as a reward for mining. The account information of the reward party is defined by the input parameter iv_award_address.

Now that the transaction information is stored in each block, simply traversing these blocks will tell you what the final balance of an account is. The logic used is to traverse each transaction recorded in each block, subtracting the current transaction amount from the balance if an account number appears on the payer of the transaction record (line 5), and adding the transaction amount to the balance if the account number appears on the sender (line 9).

The test procedure is as follows. Because Tom transferred $100 to Jerry, Jerry transferred $10 to Tom, and then the reward account set in line 15 was Jerry, Jerry's final balance was 100-10+100 =$190.

After reading the above, do you have any further understanding of how to implement the simplest blockchain prototype with ABAP code? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report