一起来读源码69-eosio.contracts eosio.system

摘要

eos的投票以及资产购买,抵押,竞拍,出租,等等;
购买机制使用的bancor算法; 执行action需要ram,cpu和net,cpu和net需要抵押获得,ram需要购买;用来抵押的物品为eos token;
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是否有一个值;
  • std::is_same 判断类型是否一致,位于头文件<type_traits>中,两个一样的类型会返回true;
  • 1000’000ll 表示把int型的1000000转为long long型;

投票

  1. 投票前需要抵押eos换取cpu和net,调用delegatebw抵押eos;
  2. 账号需要调用regproducer注册成为生产者节点候选人,别的账号才能给他投票;
  3. 账号可以调用voteproducer给其他账号投票;
  4. 如果不想投票,调用undelegatebw取消抵押,但是要3天才能到帐;
  5. 投票计分,账号的投票权重与抵押的eos数量正相关;具体的规则看代码;

购买/出售资源

  1. 内存无法抵押获得,只能获得;
  2. 购买(buyram)或出售(sellram)内存需要收取:0.5%的小费,也就是说完成一次交易需要买卖双方总共需要支付1%的小费;

名字竞拍

竞拍规则:

  1. 如果没人抢,直接给竞拍者;
  2. 如果有人抢, 出钱多的放到竞拍表,出钱少的放到竞拍退钱表里;

生产块

  1. 每分钟更新块生产者一次;
  2. 在生产区块时,出现冲突,先找到eosio_global_state全局变量表的last_producer_schedule_update(最后一个块生成时间)做比对;

领奖

  1. 出块有奖励,限制一天只能领取一次;
  2. 出块资金分成两部分:一部分给节点,另一部分给eosio.saving;
  3. 具体规则看代码

名字规则

  1. 账号格式:12个字符长,包含:".", 1~9, a~z;
  2. 如果名字中间含有"."",则".”后面必须为创建者名字;比如 abc.def 的创建者是 def

定义内联函数

//域是否存在, 如果标记F是无符号整型 且 域E是枚举无符号整型
template<typename ER, typename F>
static inline auto has_field( F flag, E field )
-> std::enable_if_t< std::is_integral_v<F> && std::is_unsigned_v<F> &&
					 std::is_enum_v<E> && std::is_same_v< F, std::underlying_type_t<E> >, bool >
{
	return ( (flags & static_cast<F>(field)) != 0 );//静态转换ok
}
//设置域(bool)
template<typename E, typename F>
static inline auto set_field( F flags, E field, bool value = true )
-> std::enable_if_t< std::is_integral_v<F> && std::is_unsigned_v<F> &&
					 std::is_enum_v<E> && std::is_same_v< F, std::underlying_type_t<E> >, F >
{
	if( value )
		return ( flags | static_cast<F>(field) ); 
	else
		return ( flags & ~static_cast<F>(field) );
}

定义全局变量

名字 类型 说明
seconds_per_year static constexpr uint32_t 52724*3600, 每一年的秒数
seconds_per_day static constexpr uint32_t 24*3600, 每天的秒数
seconds_per_hour static constexpr uint32_t 3600, 每小时的秒数
useconds_per_year static constexpr int64_t int64_t(seconds_per_year) * 1000’ 000ll, 每年的微秒数
useconds_per_day static constexpr int64_t int64_t(seconds_per_day) * 1000’ 000ll, 每天的微秒数
useconds_per_hour static constexpr int64_t int64_t(seconds_per_hour) * 1000’ 000ll, 每小时的微秒数
blocks_per_day static constexpr uint32_t 2*seconds_per_day, 1秒钟产生2个块
min_activated_stake static constexpr int64_t 150’000’000’0000, ?
ram_gift_bytes static constexpr int64_t 1400, 内存数据包大小
min_pervote_daily_pay static constexpr int64_t 100’0000, 每天投票?
refund_delay_sec static constexpr uint32_t 3 * seconds_per_day, 资金返还延时,3天
inflation_percision static constexpr int64_t 100, 通胀系数,2个小数点
default_annual_rate static constexpr int64_t 500, 默认年利率
pay_factor_percision static constexpr int64_t 10000, ?
default_inflation_pay_factor static constexpr int64_t 50000, ?
default_votepay_factor static constexpr int64_t 40000, ?

定义action

接口 参数 说明
init unsigned_init:版本, const symbol&:系统货币 初始化
onblock ignore<block_header>:块头 更新块
setalimits const name& 账号, int64_t 内存字节大小, int64_t 网络资源, int64_t cpu资源 设置账号资源限制
setacctram const name& 账号, const std::optional<int64_t>& 内存限制 设置内存限制
setacctnet const name& 账号, const std::optional<int64_t>& 网络限制 设置网络限制
setacctcpu const name& 账号, const std::optional<int64_t>& cpu限制 设置cpu限制
activate const eosio::checksum256& 特征摘要 激活协议
delegatebw const name& 抵押者, const name&:token 接收者, const asset& 获得的网络, const asset& 获得的cpu, bool 是否发生交易 抵押token,获取net和cpu
setrex const asset& 数量 设置资源代币数量
deposit const name& 用户, const asset&:数量 交定金
withdraw const name& 资金拥有者, const asset& 资金数量 从资金池撤回资产
buyrex const name& 购买者, const asset& 资金数量 购买资源代币
unstaketorex const name& 退货人, const name& 接受者, const asset& 网络, const asset& cpu 取消抵押资源代币并退钱
sellrex const name& 出售者, const asset& 资金数量 出售资源代币
cnclrexorder const name& 调用人 取消资源代币订单
rentcpu const name& 出租者, const name& 接收者, const asset& 出租花费, const asset& 出租退费 出租cpu
rentnet const name& 出租者, const name& 接收者, const asset& 出租花费, const asset& 出租退费 出租网络
fundcpuloan const name& 出租者, uint64_t 出租数量, const asset& 花费 cpu出租打钱
fundnetloan const name& 出租者, uint64_t 出租数量, const asset& 花费 net出租打钱
defcpuloan const name& 出租者, uint64_t 出租数量, const asset& 价格 定义cpu出租价格
defnetloan const name& 出租者, uint64_t 出租数量, const asset& 价格 定义net出租价格
updaterex const name& 用户 更新用户资源代币
rexexec const name& 用户, uint16_t 最大订单数 处理cpu,net,rex出租订单
consolidate const name& 收款人 应付款到期合并
mvtosavings const name& 收款人, const asset& 金额 删除应付款
mvfrsavings const name& 收款人, const asset& 金额 添加应付款
closerex const name& 持有人 删除代币
undelegatebw const name& 押金持有人, const name& 押金抵押者, const asset& 抵押的net, const asset& 抵押的cpu 取消抵押
buyram const name& 买家, const name& 卖家, const asset& 内存数量 购买内存
buyrambytes const name& 买家, const name& 卖家, uint32_t 字节数 购买指定大小内存,使用的bancor算法,内存与资金的兑换比率浮动变化;收0.5%的小费
sellram const name& 卖家, int64_t 内存大小 出售内存,需要0.5%的小费
refund const name& 收款人 退款,有3天的延时
regproducer const name& 节点, const public_key& 节点公钥, const std::string& 节点地址, uint16_t 所属国家 注册节点
regproducer2 const name& 节点, const eosio::block_signing_authority& 权限, const std::string& 地址, uint16_t 所属国家 注册节点2
unregprod const name& 生产者名字 取消注册生产者
setram uint64_t 内存大小 设置提供的内存大小
setramrate uint16_t 每个块的内存大小 设置每个块内存增长速率
voteproducer const name& 投票人, const name& 账户代理人, const std::vector& 节点集合 投票生产者节点,可以投给多人
regproxy const name& 代理人, bool 是否使用了代理 注册代理人,已经使用了代理的投票者不能注册未投票代理
setparams const eosio::blockchain_parameters& 参数 设置系统参数
claimrewards const name& 节点 节点领奖
setpriv const name& 账号, uint8_t 是否开启特权 设置特权开启,0不开,>0开启
rmvproducer const name& 节点 让一节点失效
updtrevision uint8_t 新的版本 更新版本
bidname const name& 竞拍者, const name& 新名字, const asset& 竞拍资金 竞拍抢名字
bidrefund const name& 竞拍者, const name& 新名字 竞拍退款
newaccount const name& 创建者, const name& 要创建的名字, ignore owner权限, ignore active权限 新建账号;1.账号格式:12个字符长,不包含"."; 2.新账号创建时必须购买一个最小单位的内存
updateauth ignore 账号, ignore 权限, ignore 账号的父账号,调用次action的人, ignore 此action的调用权限 更新账号的权限
deleteauth ignore 账号, ignore 权限 删除权限
linkauth ignore 调用者账号, ignore 被授权人, ignore 被赋值的action, ignore权限 给一个action赋予权限,有5个action: updateauth, deleteauth, linkauth, unlinkauth, canceldelay;不能被赋权
unlinkauth ignore 调用者账号, ignore 被授权人, ignore 被取消的action 取消授权
canceldelay ignore<permission_level> 权限, ignore 传输id 取消延时传输
onerror ignore<uint128_t> 发送者, ignore<std::vector> 传输id 报错
setabi const name& 账号, const std::vector& abi 设置合约abi
setcode const name& 部署者, uint8_t 虚拟机类型, uint8_t 虚拟机版本, const std::vector& 合约内容 设置合约
setinflation int64_t 年率, int64_t 通胀系数, int64_t 投票系数 设置通胀率

定义table

name_bid 名字竞拍表

类型 名字 注释
name newname 新的名字
name high_bidder 最高出价者
int64_t high_bid 价格
time_point last_bid_time 最后一次出价时间
主键 primary_key
分索引 by_high_bid

bid_refund 竞拍退款表

类型 名字 注释
name bidder 竞拍者
asset amount 资产

eosio_global_state 全局变量表

类型 名字 注释
uint64_t max_ram_size 最大内存大小 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 上一个生产者调度任务大小
double total_producer_vote_weight 生产者的投票资产
block_timestamp last_name_close
主键

eosio_global_state2 全局变量表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

eosio_global_state3 全局变量表3

类型 名字 注释
time_point last_vpay_state_update 上一次投票状态更新时间
double total_vpay_share_change_rate = 0 上一次投票数更新时间

eosio_global_state4 全局变量表4

类型 名字 注释
double continuous_rate 持续率
int64_t inflation_pay_factor 通胀倍数
int64_t votepay_factor 投票倍数

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_extensioneosio::block_signing_authority producer_authority 生产者权限验证
主键 owner

producer_info2 生产者信息表2

类型 名字 注释
name owner 拥有者名字
double votepay_share = 0 投票数量
time_point last_votepay_share_update 上一次投票数量更新时间

voter_info 投票者信息表

类型 名字 注释
name owner 投票人
name proxy 代理人
std::vector producers 生产者集合
int64_t staked = 0 抵押的token数量
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

user_resources 用户资源表

类型 名字 注释
name owner 拥有者
asset net_weight 网络资产
asset cpu_weight cpu资产
int64_t ram_bytes 内存大小

delegated_bandwidth 委托绑定表

类型 名字 注释
name from 发起者
name to 接收者
asset net_weight net资产
asset cpu_weight cpu资产

refund_request 返还请求表

类型 名字 注释
name owner 拥有者名字
time_point_sec request_time 请求时间
eosio::asset net_amount 网络资产
eosio::asset cpu_amount cpu资产

rex_pool 代币池表

类型 名字 注释
uint8_t version 版本
asset total_lent 所有已借出资产
asset total_unlent 所有未借出资产
asset total_rent 租金
asset total_lendable 已借出资产+未借出资产
asset total_rex rex总额
asset namebid_proceeds 名字竞拍者出资
uint64_t loan_num 贷款数量

rex_return_pool 代币返还池表

类型 名字 注释
uint8_t version 版本
time_point_sec last_dist_time 最后一次返还资金时间
time_point_sec pending_bucket_time 0xffffffff 秒,12小时,资金返还桶装满时间
time_point_sec oldest_bucket_time 0, 12小时,最老的资金返还桶时间
int64_t pending_bucket_proceeds 0,?
int64_t current_rate_of_increase 0, ?
int64_t proceeds 0, ?
static constexpr uint32_t total_intervals
static constexpr uint32_t dist_interval 10 * 60, 10分钟
static constexpr uint8_t hours_per_bucket 12, 每个桶子的时长

rex_return_buckets 代币进项积累表

类型 名字 注释
uint8_t version 版本
std::map<time_point_sec, int64_t> return_buckets 12小时内进项

rex_func

类型 名字 注释
uint8_t version 版本
name owner 拥有者
asset balance 资金余额

rex_balance 代币现金表

类型 名字 注释
uint8_t version 0, 版本
name owner rex现金持有者
asset vote_stake 投票股份
asset rex_balance REX现金数量
int64_t matured_rex 可出售的rex
std::deque<std::pair<time_point_sec, int64_t>> rex_maturities

rex_loan 代币出租表

类型 名字 注释
uint8_t version 0, 版本
name from 出租老板
name receiver 租户
asset payment 支付的SYS tokens
asset balance 可用来续租的SYS tokens
asset total_staked 押金
uint64_t loan_num 出租的rex数量
eosio::time_point expiration 交易过期时间

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, 订单是否有效

abi_hash abi表

类型 名字 注释
name owner 拥有者
checksum256 hash hash

exchange_state 交易所

类型 名字 注释
asset supply 供应量
connector base 原价
connector quote 出价
struct connector {
    asset balance,      //储备金余额
    double weight = 5,  //小费
}

定义类

permission_level_weight 等级权限

类型 名字 注释
permission_level permission 等级
uint16_t weight 权重

key_weight 键权重

类型 名字 注释
eosio::public_key key
uint16_t weight 权重

wait_weight

类型 名字 注释
uint32_t wait_sec
uint16_t weight

authority 权限验证

类型 名字 注释
uint32_t threshold 0, 起点
std::vector<key_weight> keys key权重
std::vector<permission_level_weight> accounts 权限等级权重
std::vector<wait_weight> waits 等待权重

block_header 块头

类型 名字 注释
uint32_t timestamp 时间戳
name producer 块头生产者
checksum256 previous ?
checksum256 transaction_mroot ?
checksum256 action_mroot ?
uint32_t schedule_version 0, 调度器版本
std::optionaleosio::producer_schedule new_producers 新的生产者

rex.results合约

//购买结果
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 )

猜你喜欢

转载自blog.csdn.net/qq_27304213/article/details/107951495
今日推荐