思路
本文这段ABAP代码是一个简单的区块链(Blockchain)模拟实现,主要用于演示和理解区块链的基本概念。下面将逐行解释该代码的主要功能和实现逻辑。
-
报表声明:
REPORT zblockchain.
这是ABAP报表的声明,用于创建一个独立的ABAP报表程序。
-
参数声明:
PARAMETERS: diffle TYPE char5 default '00000', noblock TYPE i DEFAULT 2.
这里声明了两个输入参数,
diffle
是一个长度为5的字符型参数,noblock
是一个整数参数,用于控制生成区块链的区块数量和工作量。 -
数据声明:
DATA: blockdata TYPE zcl_abap_blockchain_tool=>tt_block, blockdataline LIKE LINE OF blockdata, timestamp TYPE timestampl, combineddata TYPE string, prevblockdata LIKE LINE OF blockdata, nonce TYPE i VALUE 1, noncestring TYPE string, flag TYPE c, difflength TYPE i, gethash TYPE REF TO cl_abap_message_digest.
这些语句声明了用于存储区块链数据、时间戳、工作量证明(nonce)、以及其他辅助变量的数据结构。
-
初始区块的创建:
blockdataline-index = 0. blockdataline-data = 'Jerry first Genesis block'. blockdataline-phash = '000000'. GET TIME STAMP FIELD timestamp. blockdataline-timestamp = timestamp. blockdataline-nonce = 0.
这些语句用于创建区块链的初始区块,通常称为“创世区块”。它设置了区块的索引、数据、前一个区块的哈希、时间戳等信息,并初始化工作量证明的 nonce。
-
计算初始区块的哈希:
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.
这段代码将区块的各个属性合并为一个字符串,然后使用 SHA-1 哈希算法计算这个字符串的哈希值,并将哈希值存储在
blockdataline-chash
中。最后,创世区块被添加到blockdata
中。 -
生成后续区块:
noblock = noblock - 1. difflength = strlen( diffle ). DO noblock TIMES.
这部分代码使用一个循环来生成更多的区块。
noblock
参数控制要生成的区块数量。 -
设置新区块的属性:
blockdataline-index = sy-tabix. CONCATENATE `Jerry's Block ` blockdataline-index INTO blockdataline-data SEPARATED BY '-'. READ TABLE blockdata INTO prevblockdata INDEX blockdataline-index.
这段代码用于为新区块设置索引和数据,并尝试读取上一个区块的数据以获取前一个区块的哈希值。
-
计算工作量证明:
GET TIME STAMP FIELD timestamp. blockdataline-timestamp = timestamp. WHILE flag EQ abap_false.
接下来,使用一个循环来计算工作量证明,直到找到一个符合条件的哈希值。这个过程包括不断增加 nonce,重新计算哈希值,直到满足指定的条件(
blockdataline-chash
的前几位等于diffle
)为止。 -
更新哈希值:
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.
在每次循环迭代中,将 nonce 附加到区块数据中,重新计算哈希值,并将其存储在
blockdataline-chash
中。 -
检查工作量证明条件:
IF blockdataline-chash(difflength) = diffle. flag = 'X'. blockdataline-nonce = nonce. APPEND blockdataline TO blockdata. nonce = 1. CLEAR: blockdataline. ENDIF.
当找到符合条件的哈希值时,退出循环,将 nonce 的值和新区块添加到区块链数据中,并重置相关变量。
-
清除标志:
CLEAR flag.
清除循环中使用的标志
flag
。 -
数据表声明:
DATA: g_alv_tree TYPE REF TO cl_gui_alv_tree, gt_data TYPE STANDARD TABLE OF zcl_abap_blockchain_tool=>ty_displayed_node, ok_code LIKE sy-ucomm, save_ok LIKE sy-ucomm, ls_data LIKE LINE OF gt_data.
这部分声明用于创建数据表和 ALV 树的相关变量。
-
区块链工具的初始化:
DATA(lo_tool) = NEW zcl_abap_blockchain_tool( blockdata ).
创建了一个区块链工具对象,该对象将使用之前生成的区块链数据。
-
字段目录的生成:
DATA(lt_fieldcat) = lo_tool->get_fieldcat_by_data( ls_data ).
生成字段目录,用于定义 ALV 表格的列属性。
-
标签字段的定义:
PERFORM change_label.
调用
change_label
子程序来为各
个列定义标签。
-
屏幕调用:
CALL SCREEN 100.
调用屏幕编号 100,其中将显示 ALV 表格。
-
定义标签子程序:
DEFINE define_label. ... END-OF-DEFINITION.
这是一个子程序,用于为字段定义标签和其他属性。在
change_label
子程序中会调用该子程序来定义列的标签、宽度和其他属性。 -
初始化 ALV 树:
FORM init_tree. ... ENDFORM.
这个子程序初始化 ALV 树,包括设置 ALV 表格的标题和显示。
-
创建 ALV 树:
FORM create_tree. ... ENDFORM.
这个子程序用于创建 ALV 树,将数据显示在 ALV 表格中。
-
构建层次标题:
FORM build_hierarchy_header CHANGING p_hierarchy_header TYPE treev_hhdr. ... ENDFORM.
这个子程序用于构建 ALV 树的层次标题,指定树的标题和宽度。
-
退出程序:
FORM exit_program. ... ENDFORM.
这个子程序用于退出程序。
-
PBO 模块(Process Before Output):
MODULE pbo OUTPUT. ... ENDMODULE.
这个模块用于在输出数据到屏幕之前进行处理,包括设置屏幕的标题和菜单。
-
PAI 模块(Process After Input):
MODULE pai INPUT. ... ENDMODULE.
这个模块用于处理用户输入,包括响应用户的退出请求和其他输入操作。
总的来说,这段ABAP代码实现了一个简单的区块链模拟,包括初始区块的创建、工作量证明的计算以及区块链数据的显示。通过区块链工具对象和ALV树表格,用户可以查看和探索生成的区块链数据。这个示例主要用于教育和演示区块链的基本原理和工作方式。
完整源代码
*&---------------------------------------------------------------------*
*& Report ZBLOCKCHAIN
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zblockchain.
PARAMETERS: diffle TYPE char5 default '00000',
noblock TYPE i DEFAULT 2.
DATA:blockdata TYPE zcl_abap_blockchain_tool=>tt_block .
DATA:blockdataline LIKE LINE OF blockdata,
timestamp TYPE timestampl,
combineddata TYPE string,
prevblockdata LIKE LINE OF blockdata,
nonce TYPE i VALUE 1,
noncestring TYPE string,
flag TYPE c,
difflength TYPE i,
gethash TYPE REF TO cl_abap_message_digest.
blockdataline-index = 0.
blockdataline-data = 'Jerry 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.
noblock = noblock - 1.
difflength = strlen( diffle ).
DO noblock TIMES.
blockdataline-index = sy-tabix.
CONCATENATE `Jerry's 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.
WHILE flag EQ abap_false.
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.
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.
DATA: g_alv_tree TYPE REF TO cl_gui_alv_tree,
gt_data TYPE STANDARD TABLE OF zcl_abap_blockchain_tool=>ty_displayed_node,
ok_code LIKE sy-ucomm,
save_ok LIKE sy-ucomm,
ls_data LIKE LINE OF gt_data.
FIELD-SYMBOLS: <field> TYPE LINE OF LVC_T_FCAT.
DATA(lo_tool) = NEW zcl_abap_blockchain_tool( blockdata ).
DATA(lt_fieldcat) = lo_tool->get_fieldcat_by_data( ls_data ).
PERFORM change_label.
CALL SCREEN 100.
DEFINE define_label.
READ TABLE lt_fieldcat ASSIGNING <field> INDEX &1.
<field>-seltext = <field>-reptext = <field>-scrtext_m = <field>-scrtext_s = <field>-scrtext_l = &2.
<field>-outputlen = &3.
END-OF-DEFINITION.
FORM change_label.
define_label 1 'Block Data' 20.
define_label 2 'Hash' 40.
define_label 3 'Nonce' 10.
define_label 4 'Timestamp' 20.
ENDFORM.
FORM init_tree.
g_alv_tree = lo_tool->get_tree( ).
DATA l_hierarchy_header TYPE treev_hhdr.
PERFORM build_hierarchy_header CHANGING l_hierarchy_header.
CALL METHOD g_alv_tree->set_table_for_first_display
EXPORTING
is_hierarchy_header = l_hierarchy_header
CHANGING
it_fieldcatalog = lt_fieldcat
it_outtab = gt_data.
PERFORM create_tree.
g_alv_tree->frontend_update( ).
lo_tool->expand( ).
ENDFORM.
FORM create_tree.
lo_tool->render_tree( ).
ENDFORM.
FORM build_hierarchy_header CHANGING p_hierarchy_header TYPE treev_hhdr.
p_hierarchy_header-heading = 'BlockChain list'.
p_hierarchy_header-width = 30.
p_hierarchy_header-width_pix = ' '.
ENDFORM.
FORM exit_program.
LEAVE PROGRAM.
ENDFORM.
MODULE pbo OUTPUT.
SET PF-STATUS 'MAIN100'.
SET TITLEBAR 'MAINTITLE'.
IF g_alv_tree IS INITIAL.
PERFORM init_tree.
CALL METHOD cl_gui_cfw=>flush
EXCEPTIONS
cntl_system_error = 1
cntl_error = 2.
ASSERT sy-subrc = 0.
ENDIF.
ENDMODULE.
MODULE pai INPUT.
save_ok = ok_code.
CLEAR ok_code.
CASE save_ok.
WHEN 'EXIT' OR 'BACK' OR 'CANC'.
PERFORM exit_program.
WHEN OTHERS.
CALL METHOD cl_gui_cfw=>dispatch.
ENDCASE.
CALL METHOD cl_gui_cfw=>flush.
ENDMODULE.
标签:nonce,blockdataline,data,ABAP,timestamp,区块,TYPE,模拟
From: https://www.cnblogs.com/sap-jerry/p/17837350.html