[EOS源码分析]5.EOS编写HelloWorld智能合约及各种坑

 

本文所有实践都是基于EOS dawn-v4.1.0,请切到该分支然后实践

切换命令:git checkout dawn-v4.1.0

HelloWorld源码

#include <eosiolib/eosio.hpp>

#include <eosiolib/print.hpp>

using namespace eosio;

class hello : public eosio::contract {

  public:

      using contract::contract;

      /// @abi action

      void hi( account_name user ) {

         print( "Hello, ", name{user} );

      }

};

EOSIO_ABI( hello, (hi) )

 

'EOSIO_ABI'是一个生成智能合约初始化函数apply的宏,生成的apply函数是智能合约的入口,它采用switch case的方式调用具体action对应的函数

#define EOSIO_ABI( TYPE, MEMBERS ) \

extern "C" { \

   void apply( uint64_t receiver, uint64_t code, uint64_t action ) { \

      auto self = receiver; \

      if( code == self ) { \

         TYPE thiscontract( self ); \

         switch( action ) { \

            EOSIO_API( TYPE, MEMBERS ) \

         } \

         /* does not allow destructor of thiscontract to run: eosio_exit(0); */ \

      } \

   } \

} \

 

编译

 

生成wast文件

 

$ eosiocpp -o hello.wast hello.cpp

     编译完成后,会生成两个新文件,hello.wast, hello.wasm

$ ls

hello.cpp hello.wasm hello.wast

      .wast文件是wasm的代码文本格式,.wasm是汇编代码二级制格式

 

生成abi文件 

$ eosiocpp -g hello.abi hello.cpp

764454ms thread-0   abi_generator.hpp:68          ricardian_contracts  ] Warning, no ricardian clauses found for hello

764454ms thread-0   abi_generator.hpp:75          ricardian_contracts  ] Warning, no ricardian contract found for hi

 

部署智能合约

创建新账号

    我们知道智能合约是附着在账号上的,因而需要为合约准备一个账号

    创建key并导入到钱包

$ cleos create key

Private key: 5JdchMrwMwD1PsZKCjpbaCQ4aJ3cFKzSWmCQfRzKCiGrDWds3PU

Public key: EOS7KBTMkUq4VPakqsZUnZfBbMbS2U7cn9qSa3q6G5ZzEeUeNSVgv

$ cleos wallet import 5JdchMrwMwD1PsZKCjpbaCQ4aJ3cFKzSWmCQfRzKCiGrDWds3PU

   以上面的key创建账号

 

//cleos create account只适合本地私有链,其他网络需要使用cleos system newaccount命令

$ cleos create account eosio hello.code EOS7KBTMkUq4VPakqsZUnZfBbMbS2U7cn9qSa3q6G5ZzEeUeNSVgv EOS7KBTMkUq4VPakqsZUnZfBbMbS2U7cn9qSa3q6G5ZzEeUeNSVgv

executed transaction: 9cdfd59700f8fb13b9a3b330d75f9e1d6a014d54f5bec21cee9afe35c8c49b99  200 bytes  5304 us

#         eosio <= eosio::newaccount            {"creator":"eosio","name":"hello.code","owner":{"threshold":1,"keys":[{"key":"EOS7KBTMkUq4VPakqsZUnZ...

warning: transaction executed locally, but may not be confirmed by the network yet

    

将合约代码绑定到账号

    这里需要注意的是,xxx.wasm文件的目录必须是xxx,这里的hello.cpp, hello.wasm的目录就必须是hello,因为该命令会搜索xx/xx.wasm文件,这里会搜索hello/hello.wasm文件

$ cleos set contract hello.code ./hello -p hello.code

Reading WAST/WASM from ./hello/hello.wasm...

Using already assembled WASM...

Publishing contract...

executed transaction: b26872e44b439ce289f7c449f959a7c9ad574045f69c53039ff365e79e1b8494  1800 bytes  5647 us

#         eosio <= eosio::setcode               {"account":"hello.code","vmtype":0,"vmversion":0,"code":"0061736d01000000013b0c60027f7e006000017e600...

#         eosio <= eosio::setabi                {"account":"hello.code","abi":"00010c6163636f756e745f6e616d650675696e74363401026869000104757365720c6...

warning: transaction executed locally, but may not be confirmed by the network yet

 

执行智能合约函数

$cleos push action hello.code hi '["args.user"]' -p args.user

    

智能合约权限检测实践

    前面的智能合约没有执行任何权限检测,因而在hi函数里新增一个权限检测

class hello : public eosio::contract {

  public:

      using contract::contract;

      /// @abi action

      void hi( account_name user) {

         require_auth(user);

         print( "Hello, ", name{user} );

      }

};

    红色字体的意思是执行"hi" action的权限必须是作为参数传进来的user

    如果使用hello.code账号执行则会报错

$ cleos push action hello.code hi '["args.user"]' -p hello.code

Error 3090004: missing required authority

Ensure that you have the related authority inside your transaction!;

If you are currently using 'cleos push action' command, try to add the relevant authority using -p option.

Error Details:

missing authority of args.user

    将参数换成hello.code执行成功

$ cleos push action hello.code hi '["hello.code"]' -p hello.code

executed transaction: 49db2db96c3719992af6e31d378385ac99ff611453ef81fd03cbcb822d223bd5  104 bytes  1752 us

#    hello.code <= hello.code::hi               {"user":"hello.code"}

>> Hello, hello.code

warning: transaction executed locally, but may not be confirmed by the network yet

源码一键实践

    从以下github网页下载源码,即可一键实践执行该智能合约

        https://github.com/itleaks/eos-contract

/********************************

* 本文来自CSDN博主"爱踢门"

* 转载请标明出处:http://blog.csdn.net/itleaks

******************************************/

如果你对EOS,ETH技术及开发感兴趣,请入QQ群讨论: 829789117


如需实时查看最新文章,请关注公众号"区块链斜杠青年",一起探索区块链未来

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值