【精】EOS智能合约:system系统合约源码分析 (9)

创建账户的操作一直都是由system合约的newaccount动作承担的,下面仍旧通过源码分析研究其逻辑。

1. /** 2. * @brief 创建账户,包括资源管理以及名称竞拍的逻辑。 3. * 4. * @param creator 创建者 5. * @param newact 被创建的账户,如果包含点“.”,则其创建者也必须包含相同后缀。 6. * @param owner owner权限 7. * @param active active权限 8. */ 9. void native::newaccount( name creator, 10. name newact, 11. ignore<authority> owner, 12. ignore<authority> active ) { 13. if( creator != _self ) { // 创建者不能是当前合约账户。 14. uint64_t tmp = newact.value >> 4; // 将新账户名由字符转为无符号int 15. bool has_dot = false;// 定义标志位,是否包含点“.” 16. for( uint32_t i = 0; i < 12; ++i ) {// 遍历12次,因为名称最长12个字符 17. has_dot |= !(tmp & 0x1f); // 检查是否有点“.”存在,同时还可以检查账户的长度是否少于12位,有则更新has_dot标志位为true。 18. tmp >>= 5; // 移到下一位检查 19. } 20. if( has_dot ) { // 非常规账户 21. auto suffix = newact.suffix(); // 后缀 22. if( suffix == newact ) { // 创建者的后缀必须相同 23. // 在竞拍状态表中寻找创建者拥有的非常规账户,是否包含待创建账户 24. name_bid_table bids(_self, _self.value); 25. auto current = bids.find( newact.value ); 26. eosio_assert( current != bids.end(), "no active bid for name"); 27. // 校验当前待创建账户作为竞拍标的,其最高竞拍价是否是创建者报出的。 28. eosio_assert( current->high_bidder == creator, "only highest bidder can claim" ); 29. // 如果high_bid字段不是负数,说明竞拍未结束,该非常规账户还不属于创建者。 30. eosio_assert( current->high_bid < 0, "auction for name is not closed yet" ); 31. bids.erase( current ); // 通过以上校验,该竞拍标的属于创建者,创建者创建成功,删除标的历史对象。 32. } else { 33. eosio_assert( creator == suffix, "only suffix may create this account" ); 34. } 35. } 36. } 37. // 为新用户分配资源,初始化添加到用户资源状态表 38. user_resources_table userres( _self, newact.value); 39. userres.emplace( newact, [&]( auto& res ) { 40. res.owner = newact; 41. res.net_weight = asset( 0, system_contract::get_core_symbol() ); 42. res.cpu_weight = asset( 0, system_contract::get_core_symbol() ); 43. }); 44. set_resource_limits( newact.value, 0, 0, 0 ); 45. } 结束语

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsspgz.html