EOS新资源模的介绍
EOS主网即将在2个月后,迎来一次重大的升级:上线新的资源分配模。
我通过阅读源代码,去理解新资源模的工作原理。
其源代码地址在EOS代码仓库的1.8.x-rentbw分支,链接为:rentbw.cpp
新资源模,是更过更新系统合约,集成在eosio系统里的系统级资源租赁市场。
合约表结构
rent.state 数据库用于存储租赁市场的状态,类似市场的状态看板,租赁价格的计算,租借情况的计算,皆从这里来:
struct [[eosio::table("rent.state"),eosio::contract("eosio.system")]] rentbw_state {
static constexpr uint32_t DeFiault_rent_days = 30; // 30 day resource rentals
uint8_t version = 0;
rentbw_state_resource net = {}; // NET market state
rentbw_state_resource cpu = {}; // CPU market state
uint32_t rent_days = DeFiault_rent_days; // `rentbw` `days` argument must match this.
asset min_rent_fee = {}; // Rental fees below this amount are rejected
uint64_t primary_key()const { return 0; }
};
typeDeFi eosio::singletonlt;"rent.state"_n, rentbw_stategt; rentbw_state_singleton;
其中的rentbw_state_resource是一个子级的数据结构,存储了cpu和net各自的市场状态和参数配置:
struct rentbw_state_resource {
static constexpr double DeFiault_exponent = 2.0; // Exponent of 2.0 means that the price to rent a
// tiny amount of resources increases linear
// with utilization.
static constexpr uint32_t DeFiault_decay_secs = 1 * seconds_per_day; // 1 day; if * of bandwidth resources are in a
// single loan, then, assuming no further renting,
// 1 day after it expires the adjusted utilization
// will be at approximate 37% and after 3 days
// the adjusted utilization will be less than 5%.
uint8_t version = 0;
int64_t weight = 0; // resource market weight. calculated; varies over time.
// 1 represents the same amount of resources as 1
// satoshi of SYS staked.
int64_t weight_ratio = 0; // resource market weight ratio:
// assumed_stake_weight / (assumed_stake_weight + weight).
// calculated; varies over time. 1x = 10^15. 0.01x = 10^13.
int64_t assumed_stake_weight = 0; // Assumed stake weight for ratio calculations.
int64_t initial_weight_ratio = rentbw_frac; // Initial weight_ratio used for linear shrinkage.
int64_t target_weight_ratio = rentbw_frac / 100; // Linear shrink the weight_ratio to this amount.
time_point_sec initial_timestamp = {}; // When weight_ratio shrinkage started
time_point_sec target_timestamp = {}; // Stop automatic weight_ratio shrinkage at this time. Once this
// time hits, weight_ratio will be target_weight_ratio.
double exponent = DeFiault_exponent; // Exponent of resource price curve.
uint32_t decay_secs = DeFiault_decay_secs; // Number of seconds for the gap between adjusted resource
// utilization and instantaneous utilization to shrink by 63%.
asset min_price = {}; // Fee needed to rent the entire resource market weight at
// the minimum price (DeFiaults to 0).
asset max_price = {}; // Fee needed to rent the entire resource market weight at
// the maximum price.
int64_t utilization = 0; // Instantaneous resource utilization. This is the current
// amount SOLd. utilization lt;= weight.
int64_t adjusted_utilization = 0; // Adjusted resource utilization. This is gt;= utilization and
// lt;= weight. It grows instant but decays exponential.
time_point_sec utilization_timestamp = {}; // When adjusted_utilization was last updated
// for modeling - not to merged in code
int64_t fee = 0;
};
系统配置, 存储了资源模租赁算法的一些相关参数:
struct rentbw_config_resource {
std::optionallt;int64_t; current_weight_ratio; // Immediate set weight_ratio to this amount. 1x = 10^15. 0.01x = 10^13.
// Do not specify to preserve the existing setting or use the DeFiault;
// this avoids sudden price jumps. For new chains which don't need
// to gradual phase out staking and REX, 0.01x (10^13) is a good
// value for both current_weight_ratio and target_weight_ratio.
std::optionallt;int64_t; target_weight_ratio; // Linear shrink weight_ratio to this amount. 1x = 10^15. 0.01x = 10^13.
// Do not specify to preserve the existing setting or use the DeFiault.
std::optionallt;int64_t; assumed_stake_weight; // Assumed stake weight for ratio calculations. Use the sum of total
// staked and total rented by REX at the time the rentbw market
// is first activated. Do not specify to preserve the existing
// setting (no DeFiault exists); this avoids sudden price jumps.
// For new chains which don't need to phase out staking and REX,
// 10^12 is probab a good value.
std::optionallt;time_point_secgt; target_timestAMPL // Stop automatic weight_ratio shrinkage at this time. Once this
// time hits, weight_ratio will be target_weight_ratio. Ignored
// if current_weight_ratio == target_weight_ratio. Do not specify
// this to preserve the existing setting (no DeFiault exists).
std::optionallt;doublegt; exponent; // Exponent of resource price curve. Must be gt;= 1. Do not specify
// to preserve the existing setting or use the DeFiault.
std::optionallt;uint32_t; decay_secs; // Number of seconds for the gap between adjusted resource
// utilization and instantaneous resource utilization to shrink
// by 63%. Do not specify to preserve the existing setting or
// use the DeFiault.
std::optionallt;asset; min_price; // Fee needed to rent the entire resource market weight at the
// minimum price. For example, this could be set to 0.005% of
// total token supp. Do not specify to preserve the existing
// setting or use the DeFiault.
std::optionallt;asset; max_price; // Fee needed to rent the entire resource market weight at the
// maximum price. For example, this could be set to 10% of total
// token supp. Do not specify to preserve the existing
// setting (no DeFiault exists).
EOSLIB_SERIALIZE( rentbw_config_resource, (current_weight_ratio)(target_weight_ratio)(assumed_stake_weight)
(target_timestamp)(exponent)(decay_secs)(min_price)(max_price) )
};
struct rentbw_config {
rentbw_config_resource net; // NET market configuration
rentbw_config_resource cpu; // CPU market configuration
std::optionallt;uint32_t; rent_days; // `rentbw` `days` argument must match this. Do not specify to preserve the
// existing setting or use the DeFiault.
std::optionallt;asset; min_rent_fee; // Rental fees below this amount are rejected. Do not specify to preserve the
// existing setting (no DeFiault exists).
EOSLIB_SERIALIZE( rentbw_config, (net)(cpu)(rent_days)(min_rent_fee) )
};
rentbw.order租赁订单数据库,用于存储个人的租赁信息,比如租赁多少资源、租了多久,皆从这个表进行查询:
struct [[eosio::table("rentbw.order"),eosio::contract("eosio.system")]] rentbw_order {
uint8_t version = 0;
uint64_t id;
name owner;
int64_t net_weight;
int64_t cpu_weight;
time_point_sec expires;
uint64_t primary_key()const { return id; }
uint64_t by_owner()const { return owner.value; }
uint64_t by_expires()const { return expires.utc_seconds; }
};
typeDeFi eosio::multi_indexlt; "rentbw.order"_n, rentbw_order,
indexed_bylt;"byowner"_n, const_mem_funlt;rentbw_order, uint64_t, AMPLrentbw_order::by_ownergt;gt;,
indexed_bylt;"byexpires"_n, const_mem_funlt;rentbw_order, uint64_t, AMPLrentbw_order::by_expiresgt;gt;
gt; rentbw_order_table;
合约操作
相关Action一共有3:
第一个,是configrentbw,这是一个修改参数配置的动作,操作权限需要由21个超级节点多签通过才能执行。
/**
* Configure the `rentbw` market. The market becomes available the first time this
* action is invoked.
*/
[[eosio::action]]
void configrentbw( rentbw_configAMPL args );
第二个,是rentbwexec, 这是一个权限开放式的动作,任何人都可以调用它,去帮助完成租赁市场订单的处理和状态的更新。
/**
* Process rentbw queue and update state. Action does not execute anything related to a specific user.
*
* @param user - any account can execute this action
* @param max - number of queue items to process
*/
[[eosio::action]]
void rentbwexec( const nameAMPL user, uint16_t max );
第三个,是rentbw, 这是用户操作入口,通过这个入口,去执行租用的租赁,可以指定要租多少,租多久,以及**预算是多少:
/**
* Rent NET and CPU
*
* @param payer - the resource buyer
* @param receiver - the resource receiver
* @param days - number of days of resource availability. Must match market configuration.
* @param net_frac - fraction of net (* = 10^15) managed by this market
* @param cpu_frac - fraction of cpu (* = 10^15) managed by this market
* @param max_payment - the maximum amount `payer` is willing to pay. Tokens are withdrawn from
* `payer`'s token balance.
*/
[[eosio::action]]
void rentbw( const nameAMPL payer, const nameAMPL receiver, uint32_t days, int64_t net_frac, int64_t cpu_frac, const assetAMPL max_payment );
资源租赁市场是一个了不起的创新
EOS公链的了不起之处,是把一切可以 “市场化” 的资源,都进行了市场化。
将账号,抽象成可以拍卖转让的唯一id资源(类似NFT)。
将主网的存储资源抽象成 RAM,可以在RAM市场中买卖。
将主网的操作执行时间、数据传输量抽象成CPU、NET,可以在市场中租赁。
这样的创新,其实是对经济学深刻的理解和表达。
这比起那些还滞留在gas消耗模的公链里,是一种降维打击。
当然,这样精细化的设计,难免带来系统复杂度。普通用户要理解这些设计并不容易。
项目方要追求的,是能对普通用户隐藏这些公链底层的细节。
而本次的新资源模,将为EOS公链DAPP易用性的提升,带来一个质的飞跃。
新资源模对项目方和用户的意义
如果把主网的cpu、net可分配资源视为一张大饼。
那么,任何个人或者项目方,可以通过新的租赁市场。付费租下这块大饼的指定百分比。
比如DFS,是目前主网上一个较大的DApp开发者。服务于几千个EOS用户。
那么我们会预算去租下5%的主网资源,去为用户提供更好的服务。而用户,只要是在DFS应用里做任何操作,都可以无需担心cpu和net资源。
这是对DApp或DeFi应用的普及,有着重大意义的大利好。
他可以进一步降低普通用户的参与门槛。
对DApp用户而言
进一步降低了用户使用DApp的门槛。如果项目方愿意承担CPU成本的话,用户完全可以像使用互联网产品一样,顺畅的使用DApp。
这是一次公链资源模的巨大变革。
那些来不及学习的公链、还停留在所有操作都燃烧代币的gas模时代的公链,在易用性门槛这个条件上,将被EOS打得毫无还手之力。
对EOS持有者而言
EOS持有者,可以通过买入REX,去获得租赁市场的收入,如同地主,坐地收租,持有EOS买入REX,不仅可以参与主网节点投票,执行投票权,还能源源不断的享受公链上产生的经济收入。
总结
新资源模,不是一个简单的改动。不是为了把EOS主网设计复杂化。
我们需要理解它的设计原理和初衷:
它是从经济学上入手,对系统的各个角色的需求进行全面的分析后,而设计出来的一个闭环的经济模。
是从用户、项目方、EOS持有者、公链资源分配,多个维度的需求出发,作出的经济模。
它要复杂度有复杂度,是一个闭环的经济模,有订单、有队列、有价格算法、同时系统产生的支出和收益的分配得当。
要易用性有易用性,用户甚至都不需要关系细节,而dapp项目方也可以基于这个特性,提供基于vip的不同等级的服务。
文章标题:EOS新资源模型的介绍
文章链接:https://www.btchangqing.cn/150304.html
更新时间:2020年11月28日
本站大部分内容均收集于网络,若内容若侵犯到您的权益,请联系我们,我们将第一时间处理。
搞起来啊,棒棒棒