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
    .wast文件是wasm的代码文本格式,.wasm是汇编代码二级制格式

  • 生成abi文件
    eosiocpp -g hello.abi hello.cpp

部署智能合约

  • 创建新账号
    我们知道智能合约是附着在账号上的,因而需要为合约准备一个账号
    创建key并导入到钱包

    $ cleos create key
    Private key: 5JdchMrwMwD1PsZKCjpbaCQ4aJ3cFKzSWmCQfRzKCiGrDWds3PU
    Public key: EOS7KBTMkUq4VPakqsZUnZfBbMbS2U7cn9qSa3q6G5ZzEeUeNSVgv
    $ cleos wallet import 5JdchMrwMwD1PsZKCjpbaCQ4aJ3cFKzSWmCQfRzKCiGrDWds3PU

    以上面的key创建账号

    $ 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
    executed transaction: f694bfbc0c4d26751aa35a57bc1695fb24ff1a7606934dea64edd1e15f79664d  104 bytes  2444 us
    #    hello.code <= hello.code::hi               {"user":"args.user"}
    >> Hello, 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

    源码一键实践

    https://github.com/itleaks/eos-contract下载源码,即可一键实践执行该智能合约

转载自:http://blog.csdn.net/itleaks