[Experience Post] EOS_v2.0 version of the official tutorial

Getting Started

This experience is based on the Getting Started chapter of the EOSIO official tutorial. You have
experienced starting nodes, managing wallets, creating accounts, writing contracts, compiling and deploying contracts, calling contracts, etc. For
details, please refer to https://developers.eos.io/welcome/latest/getting- started/index/
deficiencies, please correct me.

What is EOS?

  1. Software: EOS can be understood as the abbreviation for eos.io software. The eos.io software is developed by block.one, which constructs the underlying technical architecture of the EOS blockchain; eos.io is similar to the operating system in the blockchain, and developers can quickly build DAPP based on this tool.
  2. Token: EOS can refer to a token, which can currently support exchange in EOS and BOS and has a certain value.
  3. Public chain: EOS can also be understood as an underlying public chain architecture that uses the DPOS consensus mechanism. It uses a voting mechanism to select 21 nodes as the master node to complete the transaction package.

The EOS we tried in this article refers to the software eos.io

One free installation trial version

If you think the local installation is more troublesome, you can directly visit:

https://gitpod.io/#https://github.com/EOSIO/eosio-web-ide

Tips: You need to configure the environment for the first visit, and opening the page may be slower.
Online IDE page

For more details about Web IDE, you can visit

https://github.com/EOSIO/eosio-web-ide

(1) Compile the contract
First look at the contract file talk.cpp

#include <eosio/eosio.hpp>

// Message table
struct [[eosio::table("message"), eosio::contract("talk")]] message {
    
    
    uint64_t    id       = {
    
    }; // Non-0
    uint64_t    reply_to = {
    
    }; // Non-0 if this is a reply
    eosio::name user     = {
    
    };
    std::string content  = {
    
    };

    uint64_t primary_key() const {
    
     return id; }
    uint64_t get_reply_to() const {
    
     return reply_to; }
};

using message_table = eosio::multi_index<
    "message"_n, message, eosio::indexed_by<"by.reply.to"_n, eosio::const_mem_fun<message, uint64_t, &message::get_reply_to>>>;

// The contract
class talk : eosio::contract {
    
    
  public:
    // Use contract's constructor
    using contract::contract;

    // Post a message
    [[eosio::action]] void post(uint64_t id, uint64_t reply_to, eosio::name user, const std::string& content) {
    
    
        message_table table{
    
    get_self(), 0};

        // Check user
        require_auth(user);

        // Check reply_to exists
        if (reply_to)
            table.get(reply_to);

        // Create an ID if user didn't specify one
        eosio::check(id < 1'000'000'000ull, "user-specified id is too big");
        if (!id)
            id = std::max(table.available_primary_key(), 1'000'000'000ull);

        // Record the message
        table.emplace(get_self(), [&](auto& message) {
    
    
            message.id       = id;
            message.reply_to = reply_to;
            message.user     = user;
            message.content  = content;
        });
    }
};

Create a new terminal in the IDE and run

eosio-cpp contract/talk.dpp

After the contract is compiled, talk.abi and talk.wasm will be generated in the contract file directory

#talk.abi内容
{
    
    
    "____comment": "This file was generated with eosio-abigen. DO NOT EDIT ",
    "version": "eosio::abi/1.1",
    "types": [],
    "structs": [
        {
    
    
            "name": "message",
            "base": "",
            "fields": [
                {
    
    
                    "name": "id",
                    "type": "uint64"
                },
                {
    
    
                    "name": "reply_to",
                    "type": "uint64"
                },
                {
    
    
                    "name": "user",
                    "type": "name"
                },
                {
    
    
                    "name": "content",
                    "type": "string"
                }
            ]
        },
        {
    
    
            "name": "post",
            "base": "",
            "fields": [
                {
    
    
                    "name": "id",
                    "type": "uint64"
                },
                {
    
    
                    "name": "reply_to",
                    "type": "uint64"
                },
                {
    
    
                    "name": "user",
                    "type": "name"
                },
                {
    
    
                    "name": "content",
                    "type": "string"
                }
            ]
        }
    ],
    "actions": [
        {
    
    
            "name": "post",
            "type": "post",
            "ricardian_contract": ""
        }
    ],
    "tables": [
        {
    
    
            "name": "message",
            "type": "message",
            "index_type": "i64",
            "key_names": [],
            "key_types": []
        }
    ],
    "ricardian_clauses": [],
    "variants": []

talk.wasm is a compiled binary file and will be distributed to each node when the contract is deployed.

(2) Installation contract

cleos create account eosio talk EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
cleos set code talk talk.wasm
cleos set abi talk talk.abi

***There is no response after the command is executed, so I gave up! ***

Just kidding, if there is no response when creating an account, please restart the workspace. Because you need to save and call the system contract to create an account, please note that you cannot close the block generation process (the page below) : the
Block out information page
output is as follows:

$ cleos create account eosio talk EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
executed transaction: f08fb469311c57c280e6ebcc0f038572ed230e28c070d48a60711b4183aabb92  200 bytes  282 us
#         eosio <= eosio::newaccount            {"creator":"eosio","name":"talk","owner":{"threshold":1,"keys":[{"key":"EOS6MRyAjQq8ud7hVNYcfnVPJqcV...
warning: transaction executed locally, but may not be confirmed by the network yet         ] 

$ cleos set code talk talk.wasm
Reading WASM from /workspace/eosio-web-ide/contract/talk.wasm...
Setting Code...
executed transaction: 4afac605a367ccf58dcf093463e881221bf910b29ad65b6ece4dfc85d695cc80  4024 bytes  736 us
#         eosio <= eosio::setcode               {"account":"talk","vmtype":0,"vmversion":0,"code":"0061736d0100000001a2011a6000006000017f60027f7f006...
warn  2020-09-01T04:32:52.185 cleos     main.cpp:495            warning: transaction executed locally, but may not be confirmed by the network yet

$ cleos set abi talk talk.abi
Setting ABI...
executed transaction: 941ddd90e63716f21f2568668dd967574b696b58c3ed189ede86b8e28d80da82  200 bytes  139 us
#         eosio <= eosio::setabi                {"account":"talk","abi":"0e656f73696f3a3a6162692f312e310002076d65737361676500040269640675696e7436340...
warn  2020-09-01T04:33:03.198 cleos     main.cpp:495            warning: transaction executed locally, but may not be confirmed by the network yet

warn: The reason is that the transaction has just been uploaded to the chain and has not been confirmed by other nodes.

(3) Create users and call contracts

#创建用户bob、alice
cleos create account eosio bob EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
cleos create account eosio jane EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV
#调用talk合约中post动作
cleos push action talk post '[1000, 0, bob, "This is a new post"]' -p bob
cleos push action talk post '[2000, 0, jane, "This is my first post"]' -p jane
cleos push action talk post '[1001, 2000, bob, "Replying to your post"]' -p bob

(4) Monitoring information on the chain

cleos get table talk '' message

Will feed back the chain information of the talk contract:

{
    
    
  "rows": [{
    
    
      "id": 1000,
      "reply_to": 0,
      "user": "bob",
      "content": "This is a new post"
    },{
    
    
      "id": 1001,
      "reply_to": 2000,
      "user": "bob",
      "content": "Replying to your post"
    },{
    
    
      "id": 2000,
      "reply_to": 0,
      "user": "jane",
      "content": "This is my first post"
    }
  ],
  "more": false
}

(5) The
data display function has been implemented with React in the front-end page display IDE, enter:

gp preview $(gp url 8000)

The effect of the pop-up front-end page is as follows:
Insert picture description here
(6) Build and run the unit test
Return to the upper directory and run in the eosio-web-ide directory:

./build-tests

This command will call all the .cpp files in the test folder for the build operation. The process takes about 40 seconds, wait patiently.

Copy (or move) the talk.wasm and talk.abi files generated in the contract directory to the directory where the tester is located.

run:

./tester

During normal operation, it displays

***No errors detected

If you are interested, you can check talk_tests.cpp. This file implements contract loading, creating users, calling actions, and exception handling at one time;

#include <boost/test/unit_test.hpp>
#include <eosio/chain/abi_serializer.hpp>
#include <eosio/chain/permission_object.hpp>
#include <eosio/chain/resource_limits.hpp>
#include <eosio/testing/tester.hpp>

using namespace eosio;
using namespace eosio::chain;
using namespace eosio::testing;
using namespace fc;

BOOST_AUTO_TEST_SUITE(talk_tests)

BOOST_AUTO_TEST_CASE(post) try {
    
    
    tester t{
    
    setup_policy::none};

    // Load contract
    t.create_account(N(talk));
    t.set_code(N(talk), read_wasm("talk.wasm"));
    t.set_abi(N(talk), read_abi("talk.abi").data());
    t.produce_block();

    // Create users
    t.create_account(N(john));
    t.create_account(N(jane));

    // Test "post" action
    t.push_action(
        N(talk), N(post), N(john),
        mutable_variant_object //
        ("id", 1)              //
        ("reply_to", 0)        //
        ("user", "john")       //
        ("content", "post 1")  //
    );
    t.push_action(
        N(talk), N(post), N(jane),
        mutable_variant_object //
        ("id", 2)              //
        ("reply_to", 0)        //
        ("user", "jane")       //
        ("content", "post 2")  //
    );
    t.push_action(
        N(talk), N(post), N(john),
        mutable_variant_object       //
        ("id", 3)                    //
        ("reply_to", 2)              //
        ("user", "john")             //
        ("content", "post 3: reply") //
    );

    // Can't reply to non-existing message
    BOOST_CHECK_THROW(
        [&] {
    
    
            t.push_action(
                N(talk), N(post), N(john),
                mutable_variant_object       //
                ("id", 4)                    //
                ("reply_to", 99)             //
                ("user", "john")             //
                ("content", "post 3: reply") //
            );
        }(),
        fc::exception);
}
FC_LOG_AND_RETHROW()

BOOST_AUTO_TEST_SUITE_END()

(7) Reset chain
Delete the existing chain and create another one. First enter the terminal used to produce blocks and press "ctrl+c" to stop.
Then run the following code:

#删除现有链数据
rm -rf ~/eosio/chain
#初始化并启动新链
nodeos --config-dir ~/eosio/chain/config --data-dir ~/eosio/chain/data -e -p eosio --plugin eosio::chain_api_plugin --contracts-console

At this point, you will find that blocks will be quickly produced starting from 0.

Two local installation

Environmental preparation

1. Install eosio

  • Binary installation
#Mac OS
brew tap eosio/eosio
brew install eosio
#Ubuntu18.04
wget https://github.com/EOSIO/eos/releases/download/v2.0.0/eosio_2.0.0-1-ubuntu-18.04_amd64.deb
sudo apt install ./eosio_2.0.0-1-ubuntu-18.04_amd64.deb
#Ubuntu16.04
wget https://github.com/EOSIO/eos/releases/download/v2.0.0/eosio_2.0.0-1-ubuntu-16.04_amd64.deb
sudo apt install ./eosio_2.0.0-1-ubuntu-16.04_amd64.deb
#CentOS
wget https://github.com/EOSIO/eos/releases/download/v2.0.0/eosio-2.0.0-1.el7.x86_64.rpm
sudo yum install ./eosio-2.0.0-1.el7.x86_64.rpm
  • Source Installation
    Because Ubuntu 20.04 is used, there is currently no binary file that can be supported. Try to use the source installation method (the final proof is not good, it is not supported during the build ).
    (1) Obtain the EOSIO source code from github [download is slow, it is recommended to download during break] The
    official website recommends a new eosio folder
mkdir -p ~/eosio && cd ~/eosio
git clone --recursive https://github.com/EOSIO/eos

(2) Update sub-module

cd eosio/eos
git submodule update --init --recursive

(3) Obtain change information

[git checkout <branch>]  (optional)
git pull --recurse-submodules

2. Install CDT

CDT: Contract Dev. Toolkit contract development kit
ubuntu18.08 installation method:

wget https://github.com/EOSIO/eosio.cdt/releases/download/v1.6.3/eosio.cdt_1.6.3-1-ubuntu-18.04_amd64.deb
sudo apt install ./eosio.cdt_1.6.3-1-ubuntu-18.04_amd64.deb

3. Use the wallet

Wallet: A repository of public and private key pairs

(1) Create a wallet

The -file option can prevent the wallet password from appearing in the terminal (actual production)
-to-console creates a default wallet, and the page displays the password (test phase)

$cleos wallet create --to-console

(2) Open the wallet The wallet
created by default is closed

#打开钱包
cleos wallet open
#返回钱包列表
cleos wallet list

If it runs correctly, the output:

Wallets:
[
  "default"
]

(3) Unlock wallet

cleos wallet unlock

After unlocking successfully, it will output:

Wallets:
[
  "default *"
]

(4) Import the private key to the wallet

#生成私钥
cleos wallet creat_key

(5) Import the development key

Each EOSIO chain has a default system user "eosio". This account starts the chain by loading a system contract for designated management and chain consensus. Each new EOS chain has the same development key.

cleos wallet import
#提示输入私钥时输入:
5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3

4. Start keosd & nodeos

#启动keosd
keosd &
#启动nodeos
nodeos -e -p eosio \
--plugin eosio::producer_plugin \
--plugin eosio::producer_api_plugin \
--plugin eosio::chain_api_plugin \
--plugin eosio::http_plugin \
--plugin eosio::history_plugin \
--plugin eosio::history_api_plugin \
--filter-on="*" \
--access-control-allow-origin='*' \
--contracts-console \
--http-validate-host=false \
--verbose-http-errors >> nodeos.log 2>&1 &

After successful startup, perform the necessary checks:

#检查节点是否正常出块
tail -f nodeos.log
#检查电子钱包,输出为Wallets:[]
cleos wallet list
#检查Nodeos端
#可以直接浏览器打开http://localhost:8888/v1/chain/get_info
#或者:
curl http://localhost:8888/v1/chain/get_info

5. Create a test account The
test process includes:

  • User account: bob, alice
  • Default account: eosio
  • Contract account
#创建用户账号
cleos create account eosio bob YOUR_PUBLIC_KEY
cleos create account eosio alice YOUR_PUBLIC_KEY
#检查公钥--用户的关联
#注意:用户名(alice)是所有权的唯一标识,绑定的公钥变更 不会改变账户的所有权
#eosio授权机制比较特殊:owner密钥保持冷存储,当active被盗用时可以通过owner重获控制权。
cleos get account alice

Contract development

1. Contract deployment
(1) Create a contract
, create a hello directory in the contract directory, and create a new hello.cpp

#include <eosio/eosio.hpp>

using namespace eosio;

class [[eosio::contract]] hello : public contract {
    
    
  public:
      using contract::contract;

      [[eosio::action]]
      void hi( name user ) {
    
    
         print( "Hello, ", user);
      }
};

(2) Compile the contract

eosio-cpp hello.cpp -o hello.wasm

(3) Deployment contract

#查看钱包内密钥
cleos wallet keys
#创建合约账户
cleos create account eosio hello YOUR_PUBLIC_KEY -p eosio@active
#部署合约
cleos set contract hello **CONTRACTS_DIR**/hello -p hello@active

(4) Execution of the contract

#调用action
cleos push action hello hi '["bob"]' -p bob@active
#切换用户调用
cleos push action hello hi '["bob"]' -p alice@active

2. Other

Guess you like

Origin blog.csdn.net/weixin_43347204/article/details/108334280