摘要
eos的共识机制,投票以及资产购买; 购买机制使用的bancor算法; 执行action需要ram,cpu和net,
而这些都需要购买;
详情
eosio.system.hpp
exchange_state.hpp
native.hpp
rex.results.hpp
delegate_bandwidth.cpp
eosio.system.cpp
exchange_state.cpp
name_bindding.cpp
native.cpp
producer_pay.cpp
rex.cpp
rex.results.cpp
voting.cpp
知识点
- std::enable_if是c++15支持的写法,c++11为std::enable_if;当enable_if的条件为true时,优先匹配enable_if后面的模板;enable_if_t是enable_if的type;
- std::optional类似于智能指针接口,可以显示转化为bool类标识std::optional是否有一个值;
- 购买or出售内存需要收取:0.5%的小费,也就是说完成一次交易需要买卖双方总共需要支付1%的小费
- 退款有3天的延时,申请后至少3天才到账
- 货币转换: 具体见bancor算法
- 名字竞拍: 如果没人抢,直接给竞拍者;如果有人抢,出钱多的放到竞拍表,出钱少的放到竞拍退钱表里;
- 生产块: 每分钟更新块生产者一次,找到eosio_global_state全局变量表的last_producer_schedule_update(最后一个块生成时间)做比对;
- 领奖: 一天领一次;
定义action
//初始化 version:版本 core:系统货币
void init( unsigned_init version, const symbol& core )
//生产者产生块的时候调用 header:块头
void onblock( ignore<block_header> header )
//设置账号资源限制 account:账号 ram_bytes:内存字节大小 net_weight:网络资源 cpu_weight:cpu资源
void setalimits( const name& account, int64_t ram_bytes, int64_t net_weight, int64_t cpu_weight )
//设置内存限制 account:账号 ram_bytes:内存限制
void setacctram( const name& account, const std::optional<int64_t>& ram_bytes )
//设置网络限制 account:账号 net_weight:网络限制
void setacctnet( const name& account, const std::optional<int64_t>& net_weight )
//设置cpu限制 account:账号 cpu_weight:cpu限制
void setacctcpu( const name& account, const std::optional<int64_t>& cpu_weight )
//激活协议 feature_digest:特征摘要
void activate( const eosio::checksum256& feature_digest )
//授权带宽 from:授予者 receiver:接收者 stake_net_quantity:网络带宽 stake_cpu_quantity:cpu带宽 transfer:是否发生交易
void delegatebw( const name& from, const name& receiver, const asset& stake_net_quantity,
const asset& stake_cpu_quantity, bool transfer )
//设置抵押池可出租数量
void setrex( const asset& balance )
//交定金 owner:用户, amount:数量
void deposit( const name& owner, const asset& amount )
//从资金池撤回资产 owner:资金拥有者 amount:资金数量
void withdraw( const name& owner, const asset& amount )
//购买资金 from:购买者 amount:资金数量
void buyrex( const name& from, const asset& amount )
//退货 owner:退货人, receiver:接受者, from_net:网络, from_cpu:cpu
void unstaketorex( const name& owner, const name& receiver, const asset& from_net, const asset& from_cpu )
//出售资金 from:出售者 rex:资金数量
void sellrex( const name& from, const asset& rex )
//取消 出售/购买rex 订单 owner:调用人
void cnclrexorder( const name& owner )
//出租cpu from:出租者 receiver:接收者 loan_payment:出租花费 loan_fund:出租退费
void rentcpu( const name& from, const name& receiver, const asset& loan_payment, const asset& loan_fund )
//出租网络 from:出租者 receiver:接收者 loan_payment:出租花费 loan_fund:出租退费
void rentnet( const name& from, const name& receiver, const asset& loan_payment, const asset& loan_fund )
//cpu出租打钱 from:出租者 loan_num:出租数量 payment:花费
void fundcpuloan( const name& from, uint64_t loan_num, const asset& payment )
//net出租打钱 from:出租者 loan_num:出租数量 payment:花费
void fundnetloan( const name& from, uint64_t loan_num, const asset& payment )
//定义cpu出租价格 from:出租者 loan_num:出租数量 amount:价格
void defcpuloan( const name& from, uint64_t loan_num, const asset& amount )
//定义net出租价格 from:出租者 loan_num:出租数量 amount:价格
void defnetloan( const name& from, uint64_t loan_num, const asset& amount )
//更新用户资金 owner:用户
void updaterex( const name& owner )
//更新资金,出租过期
void rexexec( const name& user, uint16_t max )
//应付款到期合并
void consolidate( const name& owner )
//删除应付款
void mvtosavings( const name& owner, const asset& rex )
//添加应付款
void mvfrsavings( const name& owner, const asset& rex )
//删除代币
void closerex( const name& owner )
//
void undelegatebw( const name& from, const name& receiver, const asset& unstake_net_quantity,
const asset& unstake_cpu_quantity )
//购买内存 payer:买家 receiver:卖家 quant:内存数量
void buyram( const name& payer, const name& receiver, const asset& quant )
//购买内存字节 payer:买家 receiver:卖家 bytes:字节数
使用的bancor算法,内存与资金的兑换比率浮动变化;收0.5%的小费
void buyrambytes( const name& payer, const name& receiver, uint32_t bytes )
//出售内存 account:卖家 bytes:内存大小(单位:字节); 需要0.5%的小费
void sellram( const name& account, int64_t bytes )
//退款 owner:收款人 有3天的延时
void refund( const name& owner )
//注册生产者 producer:生产者 producer_key:公钥 url:生产者地址 location:国家码
void regproducer( const name& producer, const public_key& producer_key, const std::string& url, uint16_t location )
//注册生产者2
void regproducer2( const name& producer, const eosio::block_signing_authority& producer_authority,
const std::string& url, uint16_t location )
//取消注册生产者 producer:生产者名字
void unregprod( const name& producer )
//设置提供的内存大小 max_ram_size:内存大小
void setram( uint64_t max_ram_size )
//设置每个块内存增长速率
void setramrate( uint16_t bytes_per_block )
//投票生产者节点
void voteproducer( const name& voter, const name& proxy, const std::vector<name>& producers )
//注册代理人
void regproxy( const name& proxy, bool isproxy )
//设置参数
void setparams( const eosio::blockchain_parameters& parms )
//领奖 owner:领奖人
void claimrewards( const name& owner )
//设置特权开启 account:账号 is_priv:0不开,>0开启
void setpriv( const name& account, uint8_t is_priv )
//让一个生产者失效 producer:生产者
void rmvproducer( const name& producer )
//更新修订后版本
void updtrevision( uint8_t revision )
//竞拍抢名字 bidder:竞拍者, newname:新名字, bid:资金
void bidname( const name& bidder, const name& newname, const asset& bid )
//竞拍退款 bidder:竞拍者 newname:要竞拍的名字
void bidrefund( const name& bidder, const name& newname )
//新建账号,1.账号格式:12个字符长,不包含"."; 2.新账号创建时必须购买一个最小单位的内存;
creator:创建者,name:要创建的名字,owner:owner权限,active:active权限
void newaccount( const name& creator, const name& name, ignore<authority> owner, ignore<authority> active )
//更新账号的权限 account:账号, permission:权限, parent:账号的父账号,调用次action的人, auth:此action的调用权限
void updateauth( ignore<name> account, ignore<name> permission, ignore<name> parent, ignore<authorith> auth )
//删除权限 account:账号, permission:权限
void deleteauth( ignore<name> account, ignore<name> permission )
//给一个action赋予权限,有5个action: updateauth, deleteauth, linkauth, unlinkauth, canceldelay;不能被赋权
account:调用者账号, code:被授权人, type:被赋值的action, requirement:权限
void linkauth( ignore<name> account, ignore<name> code, ignore<name> type, ignore<name>requirement )
//取消授权 account:授权人, code:被授权人, type:被取消的action
void unlinkauth( ignore<name> account, ignore<name> code, ignore<name> type )
//取消延时传输 canceling_auth:权限, trx_id:传输id
void canceldelay( ignore<permission_level> canceling_auth, ignore<checksum256> trx_id )
//报错 sender_id:发送者, sent_trx:传输id
void onerror( ignore<uint128_t> sender_id, ignore<std::vector<char>> sent_trx )
//设置合约abi account:账号, abi:abi
void setabi( const name& account, const std::vector<char>& abi )
//
void setcode( const name& account, uint8_t vmtype, uint8_t vmversion, const std::vector<char>& code )
// 设置通胀率 annual_rate:年率, inflation_pay_factor:通胀系数 votepay_factor:投票系数
void setinflation( int64_t annual_rate, int64_t inflation_pay_factor, int64_t votepay_factor )
定义table
struct [[eosio::table, eosio::contract("eosio.system")]] name_bid { //名字竞拍表
name newname; //新的名字
name high_bidder; //最高出价者
int64_t high_bid = 0; //高出价
time_point last_bid_time; //最后一次出价时间
}
struct [[eosio::table, eosio::contract("eosio.system")]] bid_refund { //竞拍退款表
name bidder; //竞拍者
asset amount; //资产
}
struct [[eosio::table("global"), eosio::contract("eosio.system")]] eosio_global_state : eosio::blockchain_parameters
{ //eosio全局变量定义1
uint64_t free_ram()const { return max_ram_size - total_ram_bytes_reserved; }
uint64_t max_ram_size = 64ll*1024*1024*1024; //最大内存大小 64GB
uint64_t total_ram_bytes_reserved = 0; //可用内存大小
int64_t total_ram_stake = 0; //???
block_timestamp last_producer_schedule_update; //最后一个块生成时间
time_point last_pervote_bucket_fill; //最后一次投入桶时间
int64_t pervote_bucket = 0; //投入桶
int64_t perblock_bucket = 0; //???
uint32_t total_unpaid_blocks = 0; //总未支付的块
int64_t total_activated_stake = 0; //总活跃的块
time_point thresh_activated_stake_time; //???
uint16_t last_producer_schedule_size = 0; //上一个生产者调度任务大小
double total_producer_vote_weight = 0; //生产者的投票资产
block_timestamp last_name_close; //上一个名字关闭时间
}
struct [[eosio::table("global2"), eosio::contract("eosio.system")]] eosio_global_state2 { //eosio全局变量定义2
uint16_t new_ram_per_block = 0; //每个块分配内存
block_timestamp last_ram_increase; //上一次内存增长时间
block_timestamp last_block_num; //???
double total_producer_votepay_share = 0; //生产者可投票数
uint8_t revision = 0; //修订后的版本 最大256
}
struct [[eosio::table("global3"), eosio::contract("eosio.system")]] eosio_global_state3 {//eosio全局变量定义3
time_point last_vpay_state_update; //上一次投票状态更新时间
double total_vpay_share_change_rate = 0; //上一次投票数更新时间
}
struct [[eosio::table("global4"), eosio::contract("eosio.system")]] eosio_global_state4 { //eosio全局变量定义4
double continuous_rate; //持续率
int64_t inflation_pay_factor; //通胀倍数
int64_t votepay_factor; //投票倍数
}
struct [[eosio::table, eosio::contract("eosio.system")]] producer_info { //生产者信息
name owner; //拥有者名字
double total_votes = 0; //总投票数
eosio::public_key producer_key; //生产者key
bool is_active = true; //是否活跃
std::string url; //网址
uint32_t unpaid_blocks = 0; //未支付的块数量
time_point last_claim_time; //上一次领取时间
uint16_t location = 0; //所属地区
eosio::binary_extension<eosio::block_signing_authority> producer_authority; //上产者权限验证
}
struct [[eosio::table, eosio::contract("eosio.system")]] producer_info2 { //生产者信息2
name owner; //拥有者名字
double votepay_share = 0; //投票数量
time_point last_votepay_share_update; //上一次投票数量更新时间
}
struct [[eosio::table, eosio::contract("eosio.system")]] voter_info { //投票者信息
name owner; //投票人
name proxy; //代理人
std::vector<name> producers; //生产者集合
int64_t staked = 0; //???
double last_vote_weight = 0; //上一次投票资产
double proxied_vote_weight = 0; //代理人投票区资产
bool is_proxy = 0; //是否是代理人
uint32_t flags1 = 0; //标记 ram:1 net:2 cpu:4
uint32_t reserved2 = 0; //???
eosio::asset reserved3; //???
}
struct [[eosio::table, eosio::contract("eosio.system")]] user_resources { //用户资源
name owner; //拥有者
asset net_weight; //网络资产
asset cpu_weight; //cpu资产
int64_t ram_bytes = 0; //内存大小
}
struct [[eosio::table, eosio::contract("eosio.system")]] delegated_bandwidth { //委托绑定
name from; //发起者
name to; //接收者
asset net_weight; //网络资产
asset cpu_weight; //cpu资产
}
struct [[eosio::table, eosio::contract("eosio.system")]] refund_request { //返还请求
name owner; //拥有者名字
time_point_sec request_time; //请求时间
eosio::asset net_amount; //网络资产
eosio::asset cpu_amount; //cpu资产
}
struct [[eosio::table, eosio::contract("eosio.system")]] rex_pool { //资金池
uint8_t version = 0; //版本
asset total_lent; //已借出资产
asset total_unlent; //未借出资产
asset total_rent; //租金
asset total_lendable; //已借出资产+未借出资产
asset total_rex; //???
asset namebid_proceeds; //绑定者传过来的核心资产
uint64_t loan_num = 0; //每笔贷款数值
}
struct [[eosio::table, eosio::contract("eosio.system")]] rex_return_pool { //资金返还池
uint8_t version = 0; //版本
time_point_sec last_dist_time; //
time_point_sec pending_bucket_time = time_point_sec::maximum(); //
time_point_sec oldest_bucket_time = time_point_sec::min(); //
int64_t pending_bucket_proceeds = 0; //
int64_t current_rate_of_increase = 0; //
int64_t proceeds = 0; //
static constexpr uint32_t total_intervals = 30 * 144; //30天
static constexpr uint32_t dist_interval = 10 * 60; //10分钟
static constexpr uint8_t hours_per_bucket = 12; //
static_assert(total_intervals * dist_interval == 30 * seconds_per_day ); //
}
struct [[eosio::table, eosio::contract("eosio.system")]] rex_return_buckets { //进项积累
uint8_t version = 0; //版本
std::map<time_point_sec, int64_t> return_buckets; //12小时内进项积累
}
struct [[eosio::table, eosio::contract("eosio.system")]] rex_fund {
uint8_t version = 0; //版本
name owner; //拥有者
asset balance; //资金余额
}
struct [[eosio::table, eosio::contract("eosio.system")]] rex_balance {
uint8_t version = 0; //版本
name owner; //拥有者
asset vote_stake; //
asset rex_balance; //
int64_t matured_rex = 0; //
std::deque<std::pair<time_point_sec, int64_t>> rex_maturities; //
}
struct [[eosio::table, eosio::contract("eosio.system")]] rex_loan { //
uint8_t version = 0; //
name from; //
name receiver; //
asset payment; //
asset balance; //
asset total_staked; //
uint64_t loan_num; //
eosio::time_point expiration; //
}
struct [[eosio::table, eosio::contract("eosio.system")]] rex_order {
uint8_t version = 0;
name owner;
asset rex_requested;
asset proceeds;
asset stake_change;
eosio::time_point order_time;
bool is_open = true;
}
struct [[eosio::table("abihash"), eosio::contract("eosio.system")]] abi_hash {//abi_hash
name owner; //拥有者
checksum256 hash; //hash
}
struct [[eosio::table("abihash"), eosio::contract("eosio.system")]] abi_hash { //abihash表
name owner; //拥有者名字
checksum256 hash; //hash
}
struct [[eosio::table, eosio::contract("eosio.system")]] exchange_state { //交易所
asset supply; //供应量
struct connector {
asset balance; //储备金余额
double weight = .5; //小费
}
connector base; //原价
connector quote; //出价
}
定义类
struct permission_level_weight { //等级权重
permission_level permission; //等级
uint16_t weight; //权重
}
struct key_weight { //键权重
eosio::public_key key; //键
uint16_t weight; //权重
}
struct wait_weight {
uint32_t wait_sec
uint16_t weight
}
struct authority { //权限验证
uint32_t threshold = 0; //起点
std::vector<key_weight> keys; //key权重
std::vector<permission_level_weight> accounts; //权限等级权重
std::vector<wait_weight> waits; //等待权重
}
struct block_header { //块头
uint32_t timestamp; //时间戳
name producer; //块头生产者
uint16_t confirmed = 0; //确认标记
checksum256 previous; //
checksum256 transaction_mroot; //
checksum256 action_mroot; //
uint32_t schedule_version = 0; //调度器版本
std::optional<eosio::producer_schedule> new_producers; //新的生产者
}
rex.results合约
定义action
//购买结果
void buyresult( const asset& rex_received )
//出售结果
void sellresult( const asset& proceeds )
//预订结果
void orderresult( const name& owner, const asset& proceeds )
//出租结果
void rentresult( const asset& rented_tokens )