EOSIO Source Code Analysis - Introduction to EOSIO

What is EOSIO

EOS can be understood as Enterprise Operation Systema blockchain operating system designed for commercial distributed applications. EOS is a new blockchain architecture introduced to achieve performance scaling of distributed applications. It is not a currency like bitcoin and ethereum, but a token issued on top of the EOS software project, known as blockchain 3.0.
EOS is a programmable blockchain that provides smart contract computing. It is developed by a company called block.one in the United States. It is implemented in C++ language throughout the process. Smart contracts are implemented in edited C++ language. Ethereum has a higher TPS, and EOS has no handling fee, which is charged indirectly according to the resources consumed by users.

Get EOSIO

binary install

  • MacOS:
brew tap eosio/eosio
brew install eosio
  • Ubuntu:
wget https://github.com/eosio/eos/releases/download/v2.1.0/eosio_2.1.0-1-ubuntu-20.04_amd64.deb
sudo apt install ./eosio_2.1.0-1-ubuntu-20.04_amd64.deb
  • CentOS:
wget https://github.com/eosio/eos/releases/download/v2.1.0/eosio-2.1.0-1.el8.x86_64.rpm
sudo yum install ./eosio-2.1.0-1.el8.x86_64.rpm

EOSIO code structure

get source code

git clone https://github.com/EOSIO/eos.git --recursive
--recursive parameters must be added, otherwise the fetched code is only the most basic framework code, and the third-party code referenced by EOSIO cannot be fetched

source code structure

The overall code structure of EOSIO is relatively simple, and the plug-in mode is used to organize the code as a whole (take V2.0.13 as an example), the core structure is as follows

EOSIO 
   |-----CMakeModules 相关第三库的自动编译脚本,如最重要的VM
   |-----libraries
        |-----appbase: 插件框架库,负责整个程序的,特别是nodeos程序的初始化,插件的初始化及启动
        |-----builtins: 虚拟机内建对象,特别是对于浮点结构的支持
        |-----chain: 整个链最核心的代码,负责链初始化,交易鉴权,交易执行,块打包等核心任务
        |-----fc: 基础库代码,完成结构打包,json反射,压缩,加密解密等最基础任务
        |-----chainbase: 高效eosio设计的内存映射数据库,实现对数据的快速访问与修改,最重要是实现了和eosio对应的数据回滚机制
        |-----eos-vm: 支持合约运行的WASM虚拟机
        |-----softfloat: 提供对于浮点运算的支持
        |-----wabt: 工具软件包,拓展wasm应用
        |-----yubihsm: 一种安全库,提供对钱包的支持
        |-----CMakeLists.txt 编译清单文件
   |-----plugins
        |-----chain_plugin: 核心库,实现链的初始化,控制与访问等业务
        |-----net_plugin: 核心库,实现P2P,完成交易广播,块分布式存储等业务
        |-----producer_plugin: 核心库,完成块生产,共识,交易执行调度等核心业务
        |-----chain_api_plugin: 提供对chain_plugin的访问,如发送交易,查询用户等
        |-----net_api_plugin: 提供对net_plugin的访问,如链接节点,获取链接状态等
        |-----producer_api_plugin: 提供对producer_plugin的访问,如控制生产,设置给白名单等
        |-----http_plugin: 封装http基础服务功能
        |-----wallet_plugin: 实现钱包相关功能,如钱包创建,公私钥存储等
        |-----wallet_api_plugin: 实现对wallet_plugin的访问,如最核心的交易签名
        |-----CMakeLists.txt 编译清单文件
   |-----programs
        |-----cleos: 客户端命令行程序,使用此程序可以实现对链的访问,如发送交易,查询账号,生产投票,链接其他节点
        |-----keosd: 钱包服务程序,钱包服务可以启动多个,可以根据业务需求灵活部署,也可以自己开发钱包服务
        |-----nodeos: 节点服务程序,也是最核心的程序,负责区块同步,交易生产等
        |-----CMakeLists.txt 编译清单文件
   |-----scripts
        |-----eosio_build.sh: 编译shell脚本,根据系统自己编译出目标程序
        |-----generate_package.sh: 可以将编译出来的二进制程序,按照各自系统打包成如deb,rpm等安装包
   |--CMakeLists.txt 编译清单文件

source code compilation

./scripts/eosio_build.sh

When running this script, the system will automatically install the required third-party library. The author has tested it on Ubunut18.04, 20.04, and CentOS7. If you want to know the compilation process, you can check the relevant scripts under scripts in detail. Note that if you want to compile the Debug version, the
system It is better to have larger resources, especially if the compiled nodeos program exceeds 1G in size

EOSIO framework

insert image description here

From the frame diagram we can see:

  • For the chain, there is only one nodeos program, and nodeos actively connects to the main network
  • Users operate the wallet keosd and nodeos through the cleos command line, and these two programs can be developed by themselves
  • The wallet is the user's private property and has no interoperability with the chain. It needs to be carefully preserved to prevent loss. Users can create multiple wallets

Start and deploy EOSIO

keosd start

./keosd \
    --http-server-address=0.0.0.0:8900 \
    --http-validate-host=false >>  ./logs/keosd.log 2>&1 &

The wallet data files are stored in the /root/eos-wallet directory by default, and each wallet has a separate wallet file, defaulting to default.wallet

nodeos start

./nodeos -e -p eosio \
    --data-dir ./data/nodeos \
    --signature-provider=EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV=KEY:5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3 \
    --p2p-listen-endpoint=0.0.0.0:9878 \
    --plugin eosio::producer_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::net_api_plugin \
    --http-server-address=0.0.0.0:8888 \
    --http-validate-host=false
    --replay-blockchain >>  ./logs/nodeos.log 2>&1 &

For the startup of the founding node, the production name must be formulated as eosio, otherwise it will not be produced. EOSIO defaults to a block of 0.5s. To send a transaction on a certain node, you need to configure the plug-in chain_api_plugin. Special attention should be paid to configuring the mongo_dn_plugin plug-in. The nodeos program needs to enable the switch to support mongo, and then recompile the program.

EOSIO chain deployment

Regarding the deployment of EOSIO, because the entire EOSIO provides the infrastructure mechanism for the operation of smart contracts, so to complete a specific business logic, it is necessary to deploy the corresponding contract to complete.

The two most important contracts in EOSIO are eosio.system and eosio.token. The former provides logical services related to production voting, while the latter provides token services.

The eosio contract compilation requires the eosio.cdt tool to compile the developed contract file into a wasm program and generate an ABI file at the same time. After the contract file is generated, the deployment can be completed.

A basic contract deployment process is given below

wallet_url=0.0.0.0:8900
node_url=0.0.0.0:8888
# 创建钱包
WALLET_PASSWD=`./cleos --wallet-url $wallet_url -u $node_url wallet create --to-console`
export passeord=`echo $WALLET_PASSWD | awk -F"\"" '{print $2}'`
echo "$passeord" > key.txt
echo "get password : $passeord"
# 导入创世公私钥
./cleos --wallet-url $wallet_url -u $node_url wallet import --private-key 5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3
# 导入私钥,创建用户
ONE_ACCOUNT=eosio.token

echo "---- imports private ($ONE_ACCOUNT) keys ----"
key_OWNER=`./cleos  --wallet-url $wallet_url -u $node_url create key --to-console`
key_ACTIVE=`./cleos  --wallet-url $wallet_url -u $node_url create key --to-console`
a_owner=`echo $key_OWNER | awk -F" " '{print $3}'`
./cleos --wallet-url $wallet_url -u $node_url wallet import --private-key $a_owner
a_ative=`echo $key_ACTIVE | awk -F" " '{print $3}'`
./cleos --wallet-url $wallet_url -u $node_url wallet import --private-key $a_ative

a_private_owner=$a_owner
a_private_ative=$a_ative

a_owner=`echo $key_OWNER | awk -F" " '{print $6}'`
a_ative=`echo $key_ACTIVE | awk -F" " '{print $6}'`
./cleos --wallet-url $wallet_url -u $node_url create account eosio $ONE_ACCOUNT $a_owner $a_ative
# 部署合约, 发送交易
./cleos --wallet-url $wallet_url -u $node_url set contract eosio.token ./contracts/eosio.token/ -p eosio.token@active

./cleos --wallet-url $wallet_url -u $node_url push action eosio.token create '[ "eosio", "10000000000.0000 '$bioscurrencysymbol'" ]' -p eosio.token
./cleos --wallet-url $wallet_url -u $node_url push action eosio.token issue '[ "eosio", "1000000000.0000 '$bioscurrencysymbol'", "memo" ]' -p eosio

Guess you like

Origin blog.csdn.net/whg1016/article/details/126463787