Skip to Content
Author's profile photo Nabheet Madan

ABAP Meets #Blockchain

Yesterday I was reading about Blockchain, its architecture and other technical detail. I being from ABAP background thought of coding a basic blockchain in ABAP.  Please note that intent here is to build a basic blockchain so that its understanding can be increased. Implementation of the blockchain may differ based on the requirements but basic concept remains the same.

The other features such as power of distributed data management and sync, validation of blocks etc. is out of scope of this blog.

Blockchain Basics

As the name suggests Blockchain is basically chain or collection of blocks. Typically a block comprises of a header which has different fields, a hash (cryptic value) of previous block and Transactions Data in hashed form (we will dig into this in our next blog). The first block in the blockchain is known as genesis block. N number of transactions together make a block. These all block are distributed over the network as local copy on each connected node.

Once the maximum number of transactions in a block is reached, the three parts of the block which are header, data(detail of transactions) & previous block hash are  combined and hashed(encrypted ) with a key known as proof of work or nonce.  The correct key or proof of work is determined based on the difficulty level.  For example we say a generated hash is valid only and only if it contains 5 leading zero’s. So this leading 5 zero’s is known as the difficulty level.

The process of determining the right key consumes the major time and is known as mining. Depending on which currency it is, rewards are given accordingly to the one who finds a valid Proof of Work.  The moment someone finds a valid proof of work all the connected nodes are informed and if 50% of the nodes agreed then this new hash is added as a new block to the chain.

What we are planning to build

  • A basic prototype of Blockchain, where a report will take input as difficulty level and number of blocks to be generated and then output will be hash and nonce for value for each hash. In the current blog our focus is to highlight how it works. In next blog we will focus on how the transactions are arranged in a block:)

ABAP Implementation of basic Blockchain

  • Created a basic report with below mentioned selection screen parameter

PARAMETERS: diffle  TYPE char5,
            noblock TYPE i.

TYPES:BEGIN OF block,
        index     TYPE string,
        data      TYPE string,
        phash     TYPE string,
        timestamp TYPE string,
        chash     TYPE string,
        nonce     TYPE string,
      END OF block,
      ttyblock TYPE STANDARD TABLE OF block.
DATA:blockdata TYPE ttyblock.
  • A Type for basic blockchain structure

  • Genesis block
    • First step is to add genesis block, we are using SHA1 algorithm for encryption.
  DATA:blockdataline LIKE LINE OF blockdata,
       timestamp     TYPE timestampl,
       combineddata  TYPE string,
       gethash       TYPE REF TO cl_abap_message_digest.
  blockdataline-index = 0.
  blockdataline-data = 'My first Genesis block'.
  blockdataline-phash = '000000'.
  GET TIME STAMP FIELD timestamp.
  blockdataline-timestamp = timestamp.
  blockdataline-nonce  = 0.
  CONCATENATE blockdataline-index blockdataline-data blockdataline-phash blockdataline-timestamp blockdataline-nonce INTO combineddata.
  CALL METHOD cl_abap_message_digest=>calculate_hash_for_char
    EXPORTING
      if_algorithm  = 'SHA1'
      if_data       = combineddata
    IMPORTING
      ef_hashstring = blockdataline-chash.
  APPEND blockdataline TO blockdata.
  • Determine Next Block Hash
    • An iterative loop to repeat number of blocked needed – 1 since genesis block is already added.
    • Inside block loop have another loop to concatenate current and previous hash data along with Nonce value and generate a hash. Validate the hash as per difficulty if valid exit and append the block
DATA:blockdataline LIKE LINE OF blockdata,
       prevblockdata LIKE LINE OF blockdata,
       nonce         TYPE i VALUE 1,
       noncestring   TYPE string,
       timestamp     TYPE timestampl,
       combineddata  TYPE string,
       flag          TYPE c,
       difflength    TYPE i.
  noblock = noblock - 1.
  difflength = strlen( diffle ).
**  // Do block-1 times as genesis block added previously
  DO noblock TIMES.
    blockdataline-index = sy-tabix.
    CONCATENATE 'Current Block ' blockdataline-index INTO blockdataline-data SEPARATED BY '-'.
    READ TABLE blockdata INTO prevblockdata INDEX blockdataline-index.
    IF sy-subrc EQ 0.
      blockdataline-phash = prevblockdata-chash.
    ENDIF.
    GET TIME STAMP FIELD timestamp.
    blockdataline-timestamp = timestamp.
*   Calculate valid hash
    WHILE flag EQ ''.
*     Nonce is the Proof of work
      noncestring = nonce.
      CONCATENATE blockdataline-index blockdataline-data blockdataline-phash blockdataline-timestamp noncestring INTO combineddata.
      CALL METHOD cl_abap_message_digest=>calculate_hash_for_char
        EXPORTING
          if_algorithm  = 'SHA1'
          if_data       = combineddata
        IMPORTING
          ef_hashstring = blockdataline-chash.
*     Valid hashfound
      IF blockdataline-chash(difflength) = diffle.
        flag = 'X'.
        blockdataline-nonce  = nonce.
        APPEND blockdataline TO blockdata.
        nonce = 1.
        CLEAR:blockdataline.
      ENDIF.
      nonce = nonce + 1.
    ENDWHILE.
    CLEAR flag.
  ENDDO.
  • Output Display

What is expected next?

  • Deep dive into Block transactions arrangement – Merkel tree
  • Implementing Blockchain using BOPF – This will be more like learning BOPF by implementing a blockchain using it
  • Adding flavor of CDS to Blockchains with annotations:)
  • Finally if time permits a HCP UI5 app to display and run this chain rather than traditional report

 

Feel free to provide your feedback,open to all ears:). Lets share and learn.

Assigned Tags

      27 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Thanks a lot for the explanations! Some code appears twice in your merged screenshot, around WHILE flag eq ''.

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks for the feedback, updated the screen shot.

       

      Author's profile photo Syambabu Allu
      Syambabu Allu

      Good to know the block chain concept implemented through ABAP style.

      Thanks,

      Syam

       

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks Syam Babu appreciate the feedback. Lets share and learn

      Author's profile photo Former Member
      Former Member

      Really Interesting, but difficult to understand :(.  Good job!

       

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks for the feedback. What part confuse you?? imagine it to be a linked list type of concept which we study in colleges where each node is a block.

       

      Author's profile photo Peter Inotai
      Peter Inotai

      Thanks for this blog. It's an interesting topic, I'm looking forward for the upcoming blogs.

      BTW is it possible to add the source code as code and not as screenshot? It would be more readable. Example here: https://blogs.sap.com/2017/09/20/abap-news-for-release-7.52-virtual-sorting-of-internal-tables/

      Thanks,

      Peter

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks for the feedback, updated the sample using code block( using first time it is good and easy to use).

      Author's profile photo Peter Inotai
      Peter Inotai

      Thanks, it looks much better now 🙂

      Author's profile photo Michelle Crapo
      Michelle Crapo

      Very nice - and I totally agree with  Peter Inotai. If you could use the code block it would make it even better!!!

      Michelle

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks Michelle Crapo , updated the code. Keep learning keep sharing.

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Nice blog, thanks for sharing! +1 from me on the suggestion to post the code as text (there is a code formatting option in the blog editor) instead of a screenshot. Screenshots are not searchable and it's customary in ABAP tag to post the code so that it can be copy-pasted.

       

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks Jelena Perfiljeva for the feedback, updated the blog and will follow the same in next blogs.

      Author's profile photo Hyung Jin Youn
      Hyung Jin Youn

      Interesting and thank you for sharing. Very nice

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks Hyung Jin Youn appreciate the feedback:)

      Author's profile photo Former Member
      Former Member

      Fantastic stuff very interesting. Always been hearing about block chain but always been a vague idea. Your blog now makes things more understandable.

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks Former Member for the feedback. Let's share and learn:)

      Author's profile photo Saumil Kadukkatt
      Saumil Kadukkatt

       Thank you nabheet madan for sharing  ..!!

      Author's profile photo Sebastian Nigl
      Sebastian Nigl

      Thank you very much for sharing your ideas!

      I also programmed a ABAP based Blockchain prototype. From my perspective the ABAP approach holds a lot of potential.

      The only thing which is not working in ABAP is the public key cryptography.

      Kind regards,

      Sebastian

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks for the feedback. I believe we can have a thirty party libraries installed and use them in our code call. Let me try it out

       

       

      Author's profile photo Sebastian Nigl
      Sebastian Nigl

      Would be really nice if you could share your experiences here. Looking forward for your response!

      Thank you very much!

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Sebastian Nigl meanwhile you can have a look at this link for further information

      Author's profile photo Sandro Scalco
      Sandro Scalco

      Thank you for your easy to read blog post!

      Author's profile photo Deepak Tewari
      Deepak Tewari

      Thank you very much for sharing this.

      I was really looking out for something that cold combine ABAP and  Blockchain, and your blog served the purpose.

       

      Regards,

      Deepak

      Author's profile photo Matthew Billingham
      Matthew Billingham

      An excellent explanation, but I do have a few reservations.

      Blockchain explanations often explore Proof of Work, but PoW does not have to be part of blockchain. It is one of several validation/authorisation methods being explored; Proof of Stake, for example.

      For private networks, where a public/democratic validation/authorisation process isn't required, such as the Citizen Id as found in Canton Zug in Switzerland, Proof of Authority is used. Here, most users of the chain have read access. Only the government can authorise new blocks. Or, to put it into blockchain parlance, only the government can "mine". There is no concept of rewards for mining with such a database.

      Second, blockhain has more applications than just currency. Zug's Citizen Id is one. Blockchain is an automatically audited distributed database, where if you can trust the block authorisation process, you can trust the contents of the blockchain.

      Author's profile photo Nabheet Madan
      Nabheet Madan
      Blog Post Author

      Thanks Matthew Billingham for the awesome feedback. I agree we have N number of validation methods anyone can use anything. Yes the concepts of rewards and all those depends on what you want to achieve not only currency:)

      I was reading other blogs people are doing crazy things with Blockchain. It is almost everywhere. Too many changes happening so too many things to learn, i feel the time ahead will be exciting:)

      Nabheet

      Author's profile photo Asaf Ali
      Asaf Ali

      Really interesting post. Can you explain the concept of nonce and diffle.