Graphene 源码阅读 ~ 番外篇 ~ 默认 Genesis 创世信息生成

in bitshares •  7 years ago  (edited)

Genesis 生成

egenesis 最前面的 e 代表 embedded, 意思是说创世信息嵌入代码中, 它有两个 full 和 brief 两个版本, full 版本包含所有创世信息, brief 只包含这些创世信息的 hash (创世信息的 hash 也是 chain_id).

full 和 brief 的生成靠的都是下面这段 make 脚本.

// libraries/egenesis/CMakeLists.txt

 22 MESSAGE( STATUS "egenesis: " ${GRAPHENE_EGENESIS_JSON} )
 23
 24 if( GRAPHENE_EGENESIS_JSON )
 25    list( APPEND embed_genesis_args --genesis-json "${GRAPHENE_EGENESIS_JSON}" )
 26 endif( GRAPHENE_EGENESIS_JSON )
 27
 28 MESSAGE( STATUS "embed_genesis_args: " ${embed_genesis_args} )
 29
 30 add_custom_command(
 31    OUTPUT
 32       "${CMAKE_CURRENT_BINARY_DIR}/egenesis_brief.cpp"
 33       "${CMAKE_CURRENT_BINARY_DIR}/egenesis_full.cpp"
 34    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
 35    COMMAND embed_genesis ${embed_genesis_args}
 36    DEPENDS
 37       "${GRAPHENE_EGENESIS_JSON}"
 38       "${CMAKE_CURRENT_SOURCE_DIR}/egenesis_brief.cpp.tmpl"
 39       "${CMAKE_CURRENT_SOURCE_DIR}/egenesis_full.cpp.tmpl"
 40       embed_genesis )

其中 GRAPHENE_EGENESIS_JSON 在 bitshares-core 根目录被设置为 genesis.json:

// ./CMakeLists.txt

26:set(GRAPHENE_EGENESIS_JSON "${CMAKE_CURRENT_SOURCE_DIR}/genesis.json" )

没错, 就是 bitshares-core 源码根目录的那个 genesis.json 文件.

egenesis 相关的代码都位于 libraries/egenesis/ 目录. 上面 libraries/egenesis/CMakeLists.txt 里的 embed_genesis 这个命令的源码实际就是 libraries/egenesis/embed_genesis.cpp; embed_genesis_args 参数展开就是 —genesis-json genesis.json; embed_genesis 程序会用 egenesis_full.cpp.tmpl, egenesis_brief.cpp.tmpl 这两个模板 egenesis_full.cpp, egenesis_brief.cpp 两个文件.

引用

上面说了 full 和 brief 两个 genesis 源码, 他们在构建时会分别生成 libgraphene_egenesis_full.alibgraphene_egenesis_brief.a, 这俩库里面实现的方法都是 <egenesis/egenesis.hpp> 中定义的方法, 如果同时链接这两个库势必会造成符号重定义错误. 所以这俩只会链接一个.

仍然以 witness 为例, 在 witness_node/CMakeLists.txt 中我们可以看到, 链接时实际上链的是 full 版本:

// witness_node/CMakeLists.txt

 15 PRIVATE graphene_app graphene_delayed_node graphene_account_history graphene_elasticsearch graphene_market_history graphene_witness graphene_chain graphene_debug_witness graphen    e_egenesis_full graphene_snapshot fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} )

参考

https://github.com/cryptonomex/graphene/wiki/egenesis

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

这篇相对前几篇来说,水分有点多呀。

  ·  7 years ago (edited)

哈哈批评的是~ 这篇不属于主要篇章里的, 只是代码中碰到的一些零碎点梳理一下 :P

Thumps up! Wish i could do coding.

Very interesting friends