eos 智能合约开发体验
eos编译安装
ERROR: Could not find a package configuration file provided by "LLVM" (requested version 4.0) with any of the following names
需要执行以下命令(检查一下你有没有这个目录,没有的话搜索一下)
export LLVM_DIR=/usr/local/Cellar/llvm@4/4.0.1/lib/cmake
cleos/localize.hpp:7:10: fatal error: 'libintl.h' file not found
brew reinstall gettext
brew unlink gettext && brew link gettext --force
eos/build/programs/nodeos/config.hpp:13:33: error: invalid suffix 'x' on integer constant
重新下载tag dawn4版本进行编译通过
EOS.IO has been successfully built. 0:0:10
To verify your installation run the following commands:
/usr/local/bin/mongod -f /usr/local/etc/mongod.conf &
cd /opt/eos/build; make test
For more information:
EOS.IO website: https://eos.io
EOS.IO Telegram channel @ https://t.me/EOSProject
EOS.IO resources: https://eos.io/resources/
EOS.IO wiki: https://github.com/EOSIO/eos/wiki
cd build
sudo make install
eos 特性
数据存储
eos数据库使用指南 参照 Boost Multi-Index Containers容器进行理解
【系列】EOS智能合约开发31 - 使用数据库的智能合约实例
【系列】EOS智能合约开发32 - 详解Multi-Index(上)
【系列】EOS智能合约开发33 - 详解Multi-Index(下)
【系列】EOS智能合约开发34 - EOS开发命令阶段性总结
系列文章
eos投票智能合约开发
创建合约eosiocpp -n ballot
// ballot.hpp
/**
* @file
* @copyright defined in eos/LICENSE.txt
*/
#include <eosiolib/eosio.hpp>
#include <string>
using namespace eosio;
using std::string;
class Ballot : public eosio::contract
{
public:
using contract::contract;
Ballot(account_name self) : contract(self) {}
// 投票发起人接口
void addposposal(string posposal_name); // 初始化投票提案
void addvoter(account_name voter_name, uint64_t weight = 1); // 初始化投票人
// 普通选民接口
void delegate(account_name voter, account_name delegate_to); // 委托投票(跟随投票)
void vote(account_name voter, string proposal); // 投票给某个提议
void winproposal(); // 返回投票数最多的提议
void allproposal(); // 返回全部提案投票状态
private:
// 定义投票人
struct voter
{
account_name account; // 投票人
uint64_t weight; // 权重
bool voted; // 如果值为true,代表这个投票人已经投过票
uint64_t delegate_account; // 委托投票人地址
string pos_name; // 投票提案名
uint64_t primary_key() const { return account; }
EOSLIB_SERIALIZE(voter, (account)(weight)(voted)(delegate_account)(pos_name));
};
// 提案的数据结构
struct posposal
{
string name; // 提案的名称
uint64_t voteCount; // 提议接受的投票数
uint64_t primary_key() const { return eosio::string_to_name(name.c_str()); }
EOSLIB_SERIALIZE(posposal, (name)(voteCount));
};
typedef eosio::multi_index<N(voter), voter> voter_index;
typedef eosio::multi_index<N(posposal), posposal> posposal_index;
};
// ballot.cpp
#include <eosiolib/eosio.hpp>
#include "ballot.hpp"
using namespace eosio;
// 投票发起人接口
void Ballot::addposposal(string posposal_name) // 初始化投票提案
{
require_auth(_self);
posposal_index posposals(_self, _self);
auto it = posposals.find(eosio::string_to_name(posposal_name.c_str()));
eosio_assert(it == posposals.end(), "posposal exist");
posposals.emplace(_self, [&](auto &a) {
a.name = posposal_name;
a.voteCount = 0;
});
}
void Ballot::addvoter(account_name voter_name, uint64_t weight /* =1 */) // 初始化投票人
{
require_auth(_self);
eosio_assert(weight > 0, "must positive weight");
voter_index voters(_self, _self);
auto it = voters.find(voter_name);
eosio_assert(it == voters.end(), "voter exist");
voters.emplace(_self, [&](auto &a) {
a.account = voter_name;
a.weight = weight;
a.voted = false;
a.delegate_account = 0;
a.pos_name = "";
});
}
// 普通选民接口
void Ballot::delegate(account_name voter, account_name delegate_to) // 委托投票(跟随投票)
{
require_auth(voter);
voter_index voters(_self, _self);
auto vit = voters.find(voter);
eosio_assert(vit->voted == false, "is voted");
eosio_assert(vit != voters.end(), "voter not exist");
auto it = voters.find(delegate_to);
eosio_assert(it != voters.end(), "delegate_to not exist");
while (it != voters.end() && it->delegate_account != 0 && it->delegate_account != voter)
it = voters.find(it->delegate_account);
eosio_assert(it != voters.end(), "delegate obj not exist");
eosio_assert(it->account != voter, "not delegate self");
if (it->voted)
{
vote(voter, it->pos_name);
}
else
{
voters.modify(it, _self, [&](auto &a) {
a.weight += vit->weight;
});
voters.modify(vit, _self, [&](auto &a) {
a.voted = true;
});
}
}
void Ballot::vote(account_name voter, string proposal) // 投票给某个提议
{
require_auth(voter);
posposal_index posposals(_self, _self);
auto it = posposals.find(eosio::string_to_name(proposal.c_str()));
eosio_assert(it != posposals.end(), "posposal not exist");
voter_index voters(_self, _self);
auto vit = voters.find(voter);
eosio_assert(vit->voted == false, "is voted");
eosio_assert(vit != voters.end(), "voter not exist");
voters.modify(vit, _self, [&](auto &a) {
a.voted = true;
a.pos_name = proposal;
});
posposals.modify(it, _self, [&](auto &a) {
a.voteCount += vit->weight;
});
}
void Ballot::winproposal() // 返回投票数最多的提议
{
posposal_index posposals(_self, _self);
auto win = posposal();
uint64_t max = 0;
for (auto it : posposals)
{
if (it.voteCount > max)
{
max = it.voteCount;
win = it;
}
}
if (max > 0)
{
eosio::print("win posposal is: ", win.name.c_str(), "vote count", win.voteCount, "\n");
}
else
{
eosio::print("not vote", "\n");
}
}
void Ballot::allproposal() // 返回全部提案投票状态
{
posposal_index posposals(_self, _self);
uint64_t idx = 0;
for (auto it : posposals)
{
eosio::print(" posposal ", idx, ":", it.name.c_str(), "vote count", it.voteCount, "\n");
}
}
EOSIO_ABI(Ballot, (addposposal)(addvoter)(delegate)(vote)(winproposal)(allproposal))
eosiocpp -o ballot.wast ballot.cpp
eosiocpp -g ballot.abi ballot.cpp
eos投票智能合约部署测试
1.创建
2.生成公私钥
cleos create key
Private key: 5JMiZ1VyByUsHuRR9homayUFKM6buMLzyBeJ6p5ToMEn5N27p1B
Public key: EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak
3.创建钱包
cleos wallet create -n gxk
Creating wallet: gxk
Save password to use in the future to unlock this wallet.
Without password imported keys will not be retrievable.
"PW5HsWtVxz3kBQKeTGvNL448p2cD1P9ghQAM6MYjWysfsLZApx3n2"
4.创建多个账户
cleos create account eosio acc1 EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak
cleos create account eosio acc2 EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak
cleos create account eosio acc3 EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak
cleos create account eosio acc4 EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak
cleos create account eosio acc5 EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak
cleos create account eosio accballot EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak
5.查看账户
# cleos get accounts EOS8PPs1yQ4bkBDMAzGGNigPWmKLNBV6FULFq8TmcNFP9Pv5Jgvak
{
"account_names": [
"acc1",
"acc2",
"acc3",
"acc4",
"acc5",
"accballot"
]
}
6.部署智能合约
# cleos set contract accballot /opt/eos/contracts/ballot
Reading WAST/WASM from /opt/eos/contracts/ballot/ballot.wast...
Using already assembled WASM...
Publishing contract...
executed transaction: c01fc59198803da7206a94751d6913500b1a06d2a36fb643bc136e0e94344898 7872 bytes 5781 us
# eosio <= eosio::setcode {"account":"accballot","vmtype":0,"vmversion":0,"code":"0061736d01000000017a1460027f7f0060037f7e7e00...
# eosio <= eosio::setabi {"account":"accballot","abi":{"types":[],"structs":[{"name":"addposposal","base":"","fields":[{"name...
warning: transaction executed locally, but may not be confirmed by the network yet
#cleos get code accballot
code hash: adaa3df4b876295a8eb185edf9668f110d83d8af03dce0ccdc6ae9503967fd00
7.调用合约,创建投票
#cleos push action accballot addposposal '["baidu"]' -p accballot
executed transaction: f0b54c41b0418d7785dd37f0db30099e50b6a6426ed10b3741ff22d7c143285a 104 bytes 11014 us accballot <= accballot::addposposal {"posposal_name":"baidu"}
warning: transaction executed locally, but may not be confirmed by the network yet
#cleos push action accballot addposposal '["alibaba"]' -p accballot
#cleos push action accballot addposposal '["163"]' -p accballot
#cleos push action accballot addposposal '["360"]' -p accballot
#cleos push action accballot addposposal '["qq"]' -p accballot
#cleos push action accballot addposposal '["yy"]' -p accballot
// 返回投票状态
#cleos push action accballot allproposal '[]' -p acc1
executed transaction: 672fc91ea96bd9509850162dd1866eb4a625afd844e488883129df648ea26202 96 bytes 1063 us
accballot <= accballot::allproposal {}
>> posposal 0:163vote count0
// 查询投票结果:当前未进行过投票
#cleos push action accballot winproposal '[]' -p acc1
executed transaction: e37d6ea6577055bfe33a053c29cf369b7c2702550078af99627f1fea9fa92620 96 bytes 1086 us
accballot <= accballot::winproposal {}
>> not vote
添加投票人
#cleos push action accballot addvoter '["acc1",1]' -p accballot
#cleos push action accballot addvoter '["acc2",1]' -p accballot
#cleos push action accballot addvoter '["acc3",1]' -p accballot
#cleos push action accballot addvoter '["acc4",1]' -p accballot
#cleos push action accballot addvoter '["acc5",1]' -p accballot
executed transaction: 954165ee5be714b7ef31a3183cbda179b69d8c2ea159c4447ee2ed0c4283c2d8 112 bytes 468 us
accballot <= accballot::addvoter {"voter_name":"acc5","weight":1}
warning: transaction executed locally, but may not be confirmed by the network yet
8.进行投票操作
#cleos push action accballot vote '["acc1","qq"]' -p acc1
#cleos push action accballot vote '["acc2","qq"]' -p acc2
#cleos push action accballot vote '["acc3","163"]' -p acc3
#cleos push action accballot vote '["acc4","qq"]' -p acc4
#cleos push action accballot delegate '["acc5","acc3"]' -p acc5
9.查看票数最多的获胜结果
cleos push action accballot winproposal '[]' -p acc1
executed transaction: a30ce81cfc3bb5eaf51e8dbbe8ff88723af3f46f1ba8c69c434aa95917aecf81 96 bytes 941 us
accballot <= accballot::winproposal {}
>> win posposal is: qqvote count3
10.全部投票状态
148655ms thread-0 apply_context.cpp:29 print_debug ]
[(accballot,allproposal)->accballot]: CONSOLE OUTPUT BEGIN =====================
posposal 0:163vote count2
posposal 0:360vote count0
posposal 0:alibabavote count0
posposal 0:baiduvote count0
posposal 0:qqvote count3
posposal 0:yyvote count0
[(accballot,allproposal)->accballot]: CONSOLE OUTPUT END =====================
注意避坑
- 函数名不支持大写字母,不支持下划线,不能超过12个字符长度
- 结构体名不要使用大写,持久化的数据表操作会受影响(类名大写暂未碰到问题)
eos 智能合约开发体验的更多相关文章
- EOS智能合约开发(四):智能合约部署及调试(附编程示例)
EOS智能合约开发(一):EOS环境搭建和创建节点 EOS智能合约开发(二):EOS创建和管理钱包 EOS智能合约开发(三):EOS创建和管理账号 部署智能合约的示例代码如下: $ cleos set ...
- EOS智能合约开发(三):EOS创建和管理账号
没有看前面文章的小伙伴可以看一下 EOS智能合约开发(一):EOS环境搭建和启动节点 EOS智能合约开发(二):EOS创建和管理钱包 创建好钱包.密钥之后,接下来你就可以创建账号了,账号是什么?账号保 ...
- EOS智能合约开发(二):EOS创建和管理钱包
上节介绍了EOS智能合约开发之EOS环境搭建及启动节点 那么,节点启动后我们要做的第一件事儿是什么呢?就是我们首先要有账号,但是有账号的前提是什么呢?倒不是先创建账号,而是先要有自己的一组私钥,有了私 ...
- EOS智能合约开发(一):EOS环境搭建和启动节点
EOS和以太坊很像,EOS很明确的说明它就是一个区块链的操作系统,BM在博客中也是说过的. 可以这样比喻,EOS就相当于内置激励系统的Windows/Linux/MacOS,这是它的一个定位. 包括以 ...
- eos智能合约开发最佳实践
安全问题 1.可能的错误 智能合约终止 限制转账限额 限制速率 有效途径来进行bug修复和提升 2.谨慎发布智能合约 对智能合约进行彻底的测试 并在任何新的攻击手法被发现后及时制止 赏金计划和审计合约 ...
- 智能合约开发环境搭建及Hello World合约
如果你对于以太坊智能合约开发还没有概念(本文会假设你已经知道这些概念),建议先阅读入门篇. 就先学习任何编程语言一样,入门的第一个程序都是Hello World.今天我们来一步一步从搭建以太坊智能合约 ...
- 【精解】EOS智能合约演练
EOS,智能合约,abi,wasm,cleos,eosiocpp,开发调试,钱包,账户,签名权限 热身 本文旨在针对EOS智能合约进行一个完整的实操演练,过程中深入熟悉掌握整个EOS智能合约的流程,过 ...
- EOS智能合约存储实例讲解
EOS智能合约存储实例 智能合约中的基础功能之一是token在某种规则下转移.以EOS提供的token.cpp为例,定义了eos token的数据结构:typedef eos::token<ui ...
- [转] 智能合约开发环境搭建及Hello World合约
[From] http://www.cnblogs.com/tinyxiong/p/7898599.html 如果你对于以太坊智能合约开发还没有概念(本文会假设你已经知道这些概念),建议先阅读入门篇. ...
随机推荐
- Git基本指令
Git学习笔记 git //检查git是否安装 sudo apt-get install git git config --global user.name "dzq" git c ...
- linux环境安装包方式
概述 安装有很多种,有时我们会混淆视听不知在什么场景或什么情况下用什么命令,下面讲解下几种安装命令的使用.希望对大家有帮助~ 详解 pip install kuming或 python -m pip ...
- 「JLOI2012」树
「JLOI2012」树 传送门 不得不说这题的数据是真的水... 我们可以想到很明确的一条思路:枚举每一个点向根节点跳,知道路径和不小于 \(s\),恰好等于 \(s\) 就直接加答案. 跳的过程可以 ...
- CVPR 2019 行人检测新思路:
CVPR 2019 行人检测新思路:高级语义特征检测取得精度新突破 原创: CV君 我爱计算机视觉 今天 点击我爱计算机视觉置顶或标星,更快获取CVML新技术 今天跟大家分享一篇昨天新出的CVPR 2 ...
- MyEclipse和Eclipse中常用的快捷键
##########################快捷键分类速查########################## *******常用类********[Ctrl+O] 显示类中方法和 ...
- 谷歌下,因为给外层大容器设置 overflow:visible; 而导致问题,IE、火狐正常
#selector { height: auto !important; width: 1024px!important; margin: auto; overflow:visible !import ...
- Java连载80-数字类格式、随机数、BigDecimal
一.数字类 1.关于数字格式化:java.text.DecimalFormat; 2.数字格式元素: # 任意数字 , 千分位 . 小数点 0 不够补零 package com.bjpowernode ...
- C#二维数组的初始化和存取
static void Main(string[] args) { ,]; ; j < ; j++) { strings[j, ] = $"{j}.0"; strings[j ...
- win7 X64 进程名称不一致,导致杀进程失效!
win7 x86, 或 win10 x64 环境下, x86的进程名称 ”aaa.exe“ 在win7 x64下面显示为 ”aaa.exe *32“
- mysql时出现:is not allowed to connect to this MySQL serverConnection closed by foreign host问题的解决
这个原因是因为索要链接的mysql数据库只允许其所在的服务器连接,需要在mysql服务器上设置一下允许的ip权限,如下: 1.连接mysql mysql -u root -p 1 如图: 2.授权 g ...