1. 程式人生 > >【系列】EOS開發3 EOS提供的程序和工具

【系列】EOS開發3 EOS提供的程序和工具

count 定義 iocp 聲明 keypair 服務 必須 public 反序列化

上一篇文章使用了nodeos命令來啟動eos服務,這一篇文章,就來介紹一下eos提供的相關程序和工具。

  • nodeos
    EOSIO的核心守護進程,它可通過插件配置來啟動一個節點。

  • cleos
    這是一個命令行工具,它跟nodeos開放的REST API接口進行交互。在cleos使用時需要帶上 nodeos實例的IP和端口。此外,cleos提供全面的命令行提示,如果不清楚說那個什麽參數,可直接輸 cleos 回車後會打印出 參數說明。如果需要查看子命令後的參數,也是如此,比如 輸入 cleos create
    
    ERROR: RequiredError: Subcommand required
    Create various items, on and off the blockchain
    Usage: ./cleos create SUBCOMMAND

Subcommands:
key Create a new keypair and print the public and private keys
account Create a new account on the blockchain


* **keosd**   
EOSIO錢包守護進程,它雲加載錢包相關插件,比如http接口和RPC API

* **launcher**   
launcher應用程序簡化了在LAN和WAN網絡上多個nodeos節點的分布

* **eosiocpp**    
 eosiocpp通過檢查合約源代碼中定義內容的類型,來生成ABI規範文件    
 為了指示一個類型需要導出到ABI文件,在類型聲明上必須加上@abi這個註解, 比如

@abi action [name name2 ... nameN]
@abi table [index_type name]

  要生成abi文件,esoiocpp必須加-g

eosiocpp -g abi.json types.hpp

Generated abi.json ...

  eosiocpp也可以生成 `helper function` 來序列化/反序列化 ABI 文件裏聲明的類型

  例如   
  #### 聲明一個`action`
#include <eosiolib/eosio.hpp>

class example : public eosio::contract {
   //@abi action
   void exampleaction( uint64_t param1, uint64_t param2, std::string param3 ) {
   }
};

{
  "types": [],
  "structs": [{
      "name": "exampleaction",
      "base": "",
      "fields": [{
           "name": "param1",
           "type": "uint64"
        },{
           "name": "param2",
           "type": "uint64"
        },{        
           "name": "param3",
           "type": "string"
        }
      ]
    }
  ],
  "actions": [{
      "name": "exampleaction",
      "type": "exampleaction",
      "ricardian_contract": ""
    }
  ],
  "tables": [],
  "ricardian_clauses": [],
  "abi_extensions": []
}

  #### 聲明一張 `table`
#include <eosiolib/eosio.hpp>

//@abi table my_table
struct my_record {
  uint64_t    ssn;
  std::string fullname;
  uint64_t primary_key() const { return key; }
};
{
  "types": [],
  "structs": [{
      "name": "my_record",
      "base": "",
      "fields": [{
        "name": "ssn",
        "type": "uint64"
      },{
        "name": "fullname",
        "type": "string"
      }
      ]
    }
  ],
  "actions": [],
  "tables": [{
      "name": "my_table",
      "index_type": "i64",
      "key_names": [
        "ssn"
      ],
      "key_types": [
        "uint64"
      ],
      "type": "my_record"
    }
  ],
  "ricardian_clauses": [],
  "abi_extensions": []
}

  #### typedef exporting
  #include <eosiolib/eosio.hpp>
struct simple {
  uint64_t u64;
};

typedef simple simple_alias;
typedef eosio::name name_alias;

class examplecontract : eosio::contract {
//@abi action
   void actionone( uint32_t param1, name_alias param2, simple_alias param3 ) {}
};
{
  "types": [{
      "new_type_name": "simple_alias",
      "type": "simple"
    },{
      "new_type_name": "name_alias",
      "type": "name"
    }
  ],
  "structs": [{
      "name": "simple",
      "base": "",
      "fields": [{
        "type": "uint64",
        "name": "u64"
      }]
    },{
      "name": "actionone",
      "base": "",
      "fields": [{
        "type": "uint32",
        "name": "param1"
      },{
        "type": "name_alias",
        "name": "param2"
      },{
        "type": "simple_alias",
        "name": "param3"
      }
     ]
    }
  ],
  "actions": [{
      "name": "actionone",
      "type": "actionone",
      "ricardian_contract": ""
    }
  ],
  "tables": [],
  "ricardian_clauses": [],
  "abi_extensions": []
}

  #### 使用生成的序列化/反序列化函數並明確用戶自定義的apply
#include <eosiolib/eosio.hpp>

struct simple {
  uint32_t u32;
};

struct my_complex_type {
  uint64_t u64;
  std::string str;
  simple simple;
  eosio::bytes bytes;
  public_key pub;
};

typedef my_complex_type complex;

//@abi action
struct test_action {
  uint32_t u32;
  complex cplx;
};

extern "C" {
   void apply( uint64_t code, uint64_t action, uint64_t receiver ) {
      if( code == N(mycontract) ) {
         if( action == N(testaction) ) {
            eosio::print("test_action content\n");
            test_action testact = eosio::unpack_action_data<test_action>();
            eosio::print_f( "Test action : % %", testact.u32, testact.cplx.u64 );
        }
     }
   }
}

「力場 lichang.io」公鏈挖礦第一社區!

【系列】EOS開發3 EOS提供的程序和工具