eosio.cdt发布带来的变化
change of version 1.3.x+,EOSIO.CDT
After eos version 1.3.x, generation of cdt tools, Smart Contracts has changed alot.
Both source code and compilation.
Compilation and application
1. download and install cdt
$ git clone --recursive https://github.com/eosio/eosio.cdt
$ cd eosio.cdt
$ ./build.sh
$ sudo ./install.sh
2. Building your first smart contract
This will generate two files:old===>new
eg:
eosiocpp -g hello.abi hello.hpp
eosiocpp -o heool.wast hello.cpp
===>
eosio-cpp -abigen hello.cpp -o hello.wasm
3. Original contract change to cdt contract need do somethings:
- name replace account_name
- symbol("EOS",4) replace S(4,EOS)
- add #define N(X) name("X") to .hpp
- ACTION replace void
- [[eosio::action]] replace @abi action
- struct [[eosio::table("testtabb")]] test_table2{} replace //@abi table testtabb
- EOSIO_DISPATCH() replace EOSIO_ABI()
Modules changes
1. console Defines C++ wrapper to log/print text messages. More...
e.g.
print( "hello world, this is a number: ", 5 );
print_f("Number of apples: %", 10);
const char *s = "Hello World!";
uint64_t unsigned_64_bit_int = 1e+18;
uint128_t unsigned_128_bit_int (87654323456);
uint64_t string_as_unsigned_64_bit = N(abcde);
std::out << s << " " << unsigned_64_bit_int << " " << unsigned_128_bit_int << " " << string_as_unsigned_64_bit;
2. multiindex Defines EOSIO Multi Index Table
e.g.
#include <eosiolib/eosio.hpp>
using namespace eosio;
using namespace std;
class addressbook: contract {
struct address {
uint64_t account_name;
string first_name;
string last_name;
string street;
string city;
string state;
uint64_t primary_key() const { return account_name; }
};
public:
addressbook(name receiver, name code, datastream<const char*> ds):contract(receiver, code, ds) {}
typedef eosio::multi_index< name("address"), address > address_index;
void myaction() {
address_index addresses(_self, _self.value); // code, scope
}
}
EOSIO_DISPATCH( addressbook, (myaction) )
3. inline actions delete currency.hpp
/**
* Send inline action
*
* @brief Send inline action
* @param CONTRACT - The account this action is intended for
* @param NAME - The name of the action
* @param ... - The member of the action specified as ("action_member1_name", action_member1_value)("action_member2_name", action_member2_value)
*/
#define SEND_INLINE_ACTION( CONTRACT, NAME, ... )\
INLINE_ACTION_SENDER(std::decay_t<decltype(CONTRACT)>, NAME)( (CONTRACT).get_self(),\
BOOST_PP_TUPLE_ENUM(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__), BOOST_PP_VARIADIC_TO_TUPLE(__VA_ARGS__)) );
e.g.1:owner call owner function ,create inline option
SEND_INLINE_ACTION(*this,transfer,{_self, N(active)},{_self,from,"1.0000 EOS","inline transfer"})
e.g.2:contract B call contract A's hi function
#include <eosiolib/eosio.hpp>
using namespace eosio;
CONTRACT A : public eosio::contract {
public:
using contract::contract;
ACTION hi( name user );
// accessor for external contracts to easily send inline actions to your contract
using hi_action = action_wrapper<"hi"_n, &A::hi>;
};
===>
#include <A.hpp>
using namespace eosio;
CONTRACT send_inline : public eosio::contract {
public:
using contract::contract;
ACTION test( name user, name inline_code ) {
print_f( "Hello % from send_inline", user );
// constructor takes two arguments (the code the contract is deployed on and the set of permissions)
A::hi_action hi(inline_code, {_self, "active"_n});
hi.send(user);
}
// accessor for external contracts to easily send inline actions to your contract
using test_action = action_wrapper<"test"_n, &send_inline::test>;
};
4. others
- delete eosio::vector,bytes replace to std::vector<>,std::vector
Headers changes
1.change eosiolib/types.hpp
- delete account_name and replace to name (details later)
- delete permission_name,table_name,scope_name,action_name
- delete time,weight_type
- change name public_key to capi_public_key,signature to capi_signature the same as capi_checksum256...
2.add eosiolib/name.hpp
- Added enum class eosio::name::raw which is implicitly converted from an eosio::name (used for template non-type parameters)
struct name
{
public:
enum class raw : uint64_t {};
uint64_t value = 0;
}
- delete macro N(X) replace to "X"_n or name("X")
#define N(X) name("X")
#define V(X) N(X).value
3.change eosiolib/contract.hpp
- change constract function
class contract {
public:
/**
* Construct a new contract given the contract name
* @brief Construct a new contract object.
* @param n - The name of this contract
*/
contract( account_name n ):_self(n){}
/**
* Get this contract name
* @brief Get this contract name.
* @return account_name - The name of this contract
*/
inline account_name get_self()const { return _self; }
protected:
/**
* The name of this contract
* @brief The name of this contract.
*/
account_name _self;
};
==========================================================================>
class contract {
public:
/**
* Construct a new contract given the contract name
* @brief Construct a new contract object.
* @param receiver - The name of this contract
* @param code - The code name of the action this contract is processing.
* @param ds - The datastream used
*/
contract( name receiver, name code, datastream<const char*> ds ):_self(receiver),_code(code),_ds(ds) {}
/**
* Get this contract name
* @brief Get this contract name.
* @return name - The name of this contract
*/
inline name get_self()const { return _self; }
/**
* The code name of the action this contract is processing.
* @brief The code name of the action this contract is processing.
* @return name - The code name of the action this contract is processing.
*/
inline name get_code()const { return _code; }
/**
* Get the datastream for this contract
* @brief Get the datastream for this contract
* @return datastream<const char*> - The datastream for this contract
*/
inline datastream<const char*> get_datastream()const { return _ds; }
protected:
/**
* The name of this contract
* @brief The name of this contract.
*/
name _self;
/**
* The code name of the action this contract is processing.
* @brief The code name of the action this contract is processing.
*/
name _code;
/**
* The datastream for this contract
*@ The datastream for this contract
*/
datastream<const char*> _ds = datastream<const char*>(nullptr, 0);
};
4.change eosiolib/eosio.hpp
Added ACTION macro which is simply a shortcut for [[eosio::action]] void.
Added TABLE macro which is simply a shortcut for struct [[eosio::table]].
Added CONTRACT macro which is simply a shortcut for class [[eosio::contract]].
e.g. abitest.cpp
#include <eosiolib/eosio.hpp>
#include <optional>
using namespace eosio;
typedef int type_def;
namespace test {
// mark this struct as an action
struct [[ eosio::action ]] testa {
void printddd() { print("testa"); }
int fielda;
float fieldb;
capi_name name;
};
struct testb {
float field;
void printll() { print("testb"); }
};
// mark this struct as an action and specify the name explicitly
struct [[ using eosio: action("testc"), contract("abitest") ]] test_c : testb {
uint64_t num;
};
}
CONTRACT abitest : public eosio::contract {
public:
using contract::contract;
// mark this method as an action and specify the name explicity
[[ eosio::action("testacta") ]]
void testact_a( name user, const std::string& s, std::vector<int>& c, std::vector<std::string> sv ) {
print( "Hello, ", name{user} );
symbol sym("TEST", 4);
}
// mark this method as an action
ACTION testactb( test::test_c input, type_def td, std::optional<int> cc, bool d ) {
print(input.num);
}
// mark this struct as a table and allow multi_index typedefs to define the tables
TABLE testtable {
uint64_t owner;
uint64_t third;
uint64_t primary_key() const { return owner; }
};
// mark this struct as a table and allow multi_index typedefs to define the tables, and specify a primitive table (non multi_index) with an explicit name
struct [[eosio::table("testtabb")]] test_table2 {
uint64_t owner;
uint64_t sec;
uint64_t third;
uint64_t primary_key() const { return owner; }
};
};
typedef eosio::multi_index< "testtab"_n, abitest::testtable > testtable_t;
typedef eosio::multi_index< "testtaba"_n, abitest::testtable > testtable_a_t;
typedef eosio::multi_index< "testtab2"_n, abitest::test_table2 > testtable2_t;
#define EOSIO_DISPATCH_EX( TYPE, MEMBERS ) \
extern "C"
void apply(uint64_t, uint64_t, uint64_t){
auto self = receiver; \
if( action == "onerror"_n.value) { \
/* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ \
eosio_assert(code == "eosio"_n.value, "onerror action's are only valid from the \"eosio\" system account"); \
} \
if( (code == self && action != "transfer"_n.value) || action == "onerror"_n.value || (code == "eosio.token"_n.value && action == "transfer"_n.value)) { \
switch( action ) { \
EOSIO_DISPATCH_HELPER( TYPE, MEMBERS ) \
} \
/* does not allow destructor of thiscontract to run: eosio_exit(0); */ \
} \
} \
}
EOSIO_DISPATCH_EX( abitest, (testactb) )
5.eosiolib/dispatcher.hpp
- Renamed the macro EOSIO_ABI to EOSIO_DISPATCH as this is more descriptive of what this macro actually does.
- Modified the definition of EOSIO_DISPATCH to work with the new constructor for eosio::contrac
- delete EOSIO_API and replace to EOSIO_DISPATCH_HELPER
e.g.
#define EOSIO_ABI_EX( TYPE, MEMBERS ) \
extern "C" { \
void apply( uint64_t receiver, uint64_t code, uint64_t action ) { \
auto self = receiver; \
if( action == N(onerror)) { \
/* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ \
eosio_assert(code == N(eosio), "onerror action's are only valid from the \"eosio\" system account"); \
} \
if( (code == self && action != N(transfer)) || action == N(onerror) || (code == N(eosio.token) && action == N(transfer))) { \
TYPE thiscontract( self ); \
switch( action ) { \
EOSIO_API( TYPE, MEMBERS ) \
} \
/* does not allow destructor of thiscontract to run: eosio_exit(0); */ \
} \
} \
}
EOSIO_ABI_EX( monster,(transfer))
==>
#define EOSIO_DISPATCH_EX( TYPE, MEMBERS ) \
extern "C" { \
void apply( uint64_t receiver, uint64_t code, uint64_t action ) { \
auto self = receiver; \
if( action == "onerror"_n.value) { \
/* onerror is only valid if it is for the "eosio" code account and authorized by "eosio"'s "active permission */ \
eosio_assert(code == "eosio"_n.value, "onerror action's are only valid from the \"eosio\" system account"); \
} \
if( (code == self && action != "transfer"_n.value) || action == "onerror"_n.value || (code == "eosio.token"_n.value && action == "transfer"_n.value)) { \
switch( action ) { \
EOSIO_DISPATCH_HELPER( TYPE, MEMBERS ) \
} \
/* does not allow destructor of thiscontract to run: eosio_exit(0); */ \
} \
} \
}
6.Removed eosiolib/core_symbol.hpp and change symbol.hpp
- change contract function.Removed eosio::symbol_type struct and replaced with eosio::symbol class
struct symbol_type {
symbol_name value;
};
==================================>
class symbol_code {
public:
constexpr symbol_code() : value(0) {}
constexpr explicit symbol_code( uint64_t raw )
:value(raw)
{}
private:
uint64_t value = 0;
};
- Removed the S macro. The symbol constructor should be used as a type safe replacement
S(4,SYS)
===>
symbol(symbol_code("SYS"), 4) = symbol("SYS", 4)
安来根-EOS 开发社区
---
![](https://img2018.cnblogs.com/blog/1015273/201812/1015273-20181218201530501-1531824304.jpg)
eosio.cdt发布带来的变化的更多相关文章
- eosio.cdt:EOS智能合约工具集
目前EOS已经迎来了1.5.x时代,很多内容都有了较大的改变.其中智能合约的工作流程发生了改变,EOSIO为智能合约提供了独立且功能完整的工具集eosio.cdt.该工具集基于WASM平台,可解耦于e ...
- 安装EOS合约工具eosio.cdt
安装: 安装完之后 会产生 eosio-cpp_, eosio-cc, eosio-ld, eosio-pp, and _eosio_abigen (These are the C++ compile ...
- 【重构前端知识体系之HTML】HTML5给网页音频带来的变化
[重构前端知识体系之HTML]HTML5给网页音频带来的变化 引言 音乐播放,相信大家都很熟悉,但是早在之前的音乐播放之前,你的浏览器会问你,是否下载flash插件.然而现在,估计一些年轻的开发者都不 ...
- 随着visual studio 2013 发布.带来的一些变化
1.asp.net a.在2013中, asp.net走向了统一.使用不同的asp.net 框架搭(web forms ,api, mvc )建混合应用 b.身份验证 无身份验证 个人用户账户 (窗体 ...
- ASP.NET 5 Beta5 对TagHelper带来的变化
最近做的TagHelper项目要从原来的ASP.NET 5 Beta 4升级到Beta 5,特地整理了升级后的变化: 新增ImageTagHelper <img asp-file-version ...
- 新抽象语法树(AST)给 PHP7 带来的变化
本文大部分内容参照 AST 的 RFC 文档而成:https://wiki.php.net/rfc/abstract_syntax_tree,为了易于理解从源文档中节选部分进行介绍. 我的官方群点击此 ...
- 【总结】设备树对platform平台设备驱动带来的变化(史上最强分析)【转】
本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/74375086 版权声明:本文为博主原创文章,转载请注明http://blog.c ...
- MyEclipse 2017 CI 9 发布(附下载)
挑战全年最低价!MyEclipse线上狂欢继续!火热开启中>> 在进入年底之时,2017 CI 9是我们最大的版本发布之一.在新版本中,我们添加了对Angular 5和TypeScript ...
- 腾讯优图联手Science发布主题报告:计算机视觉的研发和应用
近日,腾讯优图与<科学>(Science)杂志共同发布<Seeing is believing: R&D applications of computer vision> ...
- 重磅发布 | 黑镜调查:深渊背后的真相之「DDoS 威胁与黑灰产业调查报告」
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云鼎实验室发表于云+社区专栏 本文经授权转载自 FreeBuf 2018年世界杯硝烟散尽,但关于她的话题却远远没有结束.说起世界杯,就 ...
随机推荐
- springboot项目整合-注册功能模块开发
工程简介 准备工作:项目所用到的html界面以及sql文件链接如下:链接: https://pan.baidu.com/s/18loHJiKRC6FI6XkoANMSJg?pwd=nkz2 提取码: ...
- CSP-S游记
第三次考csp-s了,希望这次不要二等 Day ?(初赛) 之前校内模拟赛平均下来都在班级中游,所以不求高分但是觉得过没问题(事实好像确实如此 先开题,选择题很水秒了(devinNB猜到了考Linux ...
- mybatis的映射
//一对一 <resultMap id="给resultMap取的id名" type="类名"> <result property=" ...
- vulnhub靶场之Beelzebub
准备: 攻击机:虚拟机kali.本机win10. 靶机:Beelzebub: 1,网段地址我这里设置的桥接,所以与本机电脑在同一网段,下载地址:https://download.vulnhub.com ...
- 嵌入式-c语言基础:冒泡排序实现从大到小排列
#include<stdio.h> int main() { /*冒泡排序:从大到小*/ /*i=0 第1轮(i+1):需要比较9次(sizeArr-i-1)*/ /*i=1 第2轮(i+ ...
- .NET复习总纲
以下是自己学习遇到比较好的课程和学习网站,如果大家有更好的课程推荐,可以打在评论区或者私聊我,让我也进行学习和补充进文档 一..NET基础 官方文档:https://learn.microsoft.c ...
- elasticsearch bucket 之rare terms聚合
目录 1.背景 2.需求 3.前置准备 3.1 准备mapping 3.2 准备数据 4.实现需求 4.1 dsl 4.2 java代码 4.3 运行结果 5.max_doc_count 和 sear ...
- 社论 22.10.14 区间在线去重k小
浅谈区间在线去重k小 关于讨论 https://www.luogu.com.cn/discuss/509205 本文将描述一种分块做法以及讨论中提出的各种 \(O(n \ \text{polylog} ...
- JDK中内嵌JS引擎介绍及使用
原文: JDK中内嵌JS引擎介绍及使用 - Stars-One的杂货小窝 最近研究阅读这个APP,其主要功能就是通过一个个书源,从而实现移动端阅读的体验 比如说某些在线小说阅读网站,会加上相应的广告, ...
- 4.5:HDFS操作实验
〇.概述 1.拓扑结构 2.目标 进行HDFS的实验,了解HDFS的基本操作. 一.常用操作 1.启动 50070 2.查看及创建 3.上传文件