EOS智能合约授权限制和数据存储

EOS智能合约授权限制和数据存储

EOS智能合约授权限制和数据存储

image

在EOS合约中,调用合约需要来自账户的授权,同时还要指定需要调用的动作。当然,有的合约并不是所有账户都可以调用的,这就需要用到授权限制。接下来我们就来看看如何限制合约的授权账户。

合约案例

为了更好的演示,写了一个下课和吃饭的智能合约小例子。这个合约有两个动作,下课和吃饭。教师账户可以调用下课动作,学生账户可以调用吃饭动作。教师调用下课动作后,学生才能调用吃饭动作。接下来我们来看代码:

teacher.hpp

头文件teacher.hpp定义了两个动作,over是class over 下课动作,eat是吃饭动作。

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>

using namespace eosio;

class teacher_stu : public eosio::contract{

    using contract::contract;
  public:
    teacher_stu( account_name self ) :
        contract( self ) {}

    void over(account_name teacher);

    void eat(account_name student);

  private:
    static uint64_t id;

    // @abi table
    struct mshare {
      uint64_t id;
      uint64_t start = 0;

      uint64_t primary_key() const { return id; }
    };

    typedef multi_index<N(mshare), mshare> mshares;

};

EOSIO_ABI( teacher_stu, (over)(eat))

teacher.cpp

teacher.cpp中主要是over和eat动作的实现。

#include "teacher.hpp"

uint64_t teacher_stu::id = 1;
uint64_t start = 0;

void teacher_stu::over(account_name teacher) {
  require_auth(teacher);
  print("teache:Class over!");

  mshares mshare_table(_self, _self);

  start = 1;//存储动作调用状态
  mshare_table.emplace( teacher, [&]( auto& mshare ) {
    mshare.id = id;
    mshare.start = start;
  });
}

void teacher_stu::eat(account_name student) {
  require_auth(student);
  mshares mshare_table(_self, _self);
  auto it = mshare_table.find( id );
  if (it->start == 1) {
    print("student:Class over, go to eat!");
    mshare_table.erase( it );
  }
  else
    print("teacher:Class time, you can't eat");
}

仔细观察这段代码就会发现,over和eat动作中都有个”require_auth()”语句。在over动作中,”requir_auth(teacher)”的作用是限制只有”teacher”账户才可以调用over动作。在eat动作中则是限制只有”student”账户才可调用eat动作。

合约数据存储

此合约设计为下课后才可以吃饭,所以当教师账户调用合约的over动作后,需要存储一个合约调用状态值。EOS合约的数据存储需要用数据库,把数据存到一张表中,这是令人很难受的。

teacher.hpp

在teacher.hpp中创建一个结构体。下段代码中的”//@abi table”注释非常重要,必须要写的,如果不写数据将无法存储。

    static uint64_t id;

    // @abi table
    struct mshare {
      uint64_t id;
      uint64_t start = 0;

      uint64_t primary_key() const { return id; }
    };

    typedef multi_index<N(mshare), mshare> mshares;

DAWN 3.0 使用eosio::multi_index作为容器,我们可以使用emplace来插入数据,使用modify来修改数据,使用erase删除数据。

teacher.cpp

  mshares mshare_table(_self, _self);

  start = 1;
  mshare_table.emplace( teacher, [&]( auto& mshare ) {
    mshare.id = id;
    mshare.start = start;
  });

在over动作中创建数据,学生调用eat动作后再修改或删除数据即可

  mshares mshare_table(_self, _self);
  auto it = mshare_table.find( id );
  if (it->start == 1) {
    print("student:Class over, go to eat!");
    mshare_table.erase( it );
  }

合约调用效果展示

  • 授权限制
$ cleos push action class over '{"teacher":"teacher","student":"student"}' -p student
Error 3030001: missing required authority
Ensure that you have the related authority inside your transaction!;
If you are currently using 'cleos push action' command, try to add the relevant authority using -p option.
Error Details:
missing authority of teacher
  • 学生没下课就吃饭
$ cleos push action class eat '{"teacher":"teacher","student":"student"}' -p student
executed transaction: 02918b223230cb9ea1fd383e0499ea22d22ced8f2108db3233bdfd51c06f3b37  232 bytes  102400 cycles
#         class <= class::eat                   {"student":"student"}
>> teacher:Class time, you can't eat
  • 正常下课吃饭
$ cleos push action class over '{"teacher":"teacher","student":"student"}' -p teacher
executed transaction: a96520fa28c8412e0998080126734337507811638ecf6b939e904818a4892e35  232 bytes  103424 cycles
#         class <= class::over                  {"teacher":"teacher"}
>> teache:Class over!
$ cleos push action class eat '{"teacher":"teacher","student":"student"}' -p student
executed transaction: 2955a693b626c539d20da9d4f5d41a6b53bb8ca2b2651b63cf4a67012fb3dd7e  232 bytes  103424 cycles
#         class <= class::eat                   {"student":"student"}
>> student:Class over, go to eat!
  • 查看表中数据
$ cleos get table class class mshare
{
  "rows": [{
      "id": 1,
      "start": 1
    }
  ],
  "more": false
}

整个合约写下来调通也是费了我很多脑细胞,数据存储也比较坑爹啊。现在把我的一点经验分享出来,希望大家在学习EOS的路上少踩一些坑。

知识星球二维码380.png
欢迎添加微信(id:pdj107)

转载于:https://www.cnblogs.com/tokenpai/p/9175959.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/101611.html原文链接:https://javaforall.net

(0)
上一篇 2021年6月6日 下午5:00
下一篇 2021年6月6日 下午6:00


相关推荐

  • Xilinx Vivado和SDK安装

    Xilinx Vivado和SDK安装参考方法以 vivado2015 2 1 为例 先安装 vivado2015 2 再安装 vivado2015 2 1 更新包 选下面两个都可以 看需求 选上 sdk 按需选择 添加 license 安装完后卸载烦人的 xic xilinxinform

    2026年3月18日
    2
  • 盘点|12款服务器监控工具「建议收藏」

    盘点|12款服务器监控工具「建议收藏」服务器监控工具功能相当强大,无论何时何地,我们都可以了解到服务器的功能以及性能。服务器监控工具的使用,可以让我们清楚的知道用户可以打开我们的网站,且确保网速不慢。只有这样做,才能留住宝贵的用户,以免因为系统停运的原因,导致用户丢失。基于此,我为大家收集了12款超实用的服务器监控工具。1、zabbixzabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。abbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问

    2022年6月12日
    83
  • PhpStorm 头部注释、类注释和函数注释的设置(稍微完善点)

    PhpStorm 头部注释、类注释和函数注释的设置(稍微完善点)

    2022年2月10日
    53
  • 智能优化算法改进算法 -附代码[通俗易懂]

    智能优化算法改进算法 -附代码[通俗易懂]智能优化算法改进算法摘要:为了方便大家对智能优化算法进行改进,复现多种智能优化改进算法供大家参考。所有代码均根据已经发表的文章,来复现方便大家参考别人的原理,代码会不定时更新。1.文献复现:基于变因子加权学习与邻代维度交叉策略的改进乌鸦算法Matlab代码[1]赵世杰,高雷阜,于冬梅,徒君.基于变因子加权学习与邻代维度交叉策略的改进CSA算法[J].电子学报,2019,47(01):40-48.2.文献复现:自适应t分布变异的缎蓝园丁鸟优化算法Matlab代码[1]韩斐斐,刘升.基于自适

    2022年5月23日
    43
  • Flash cookie — 本地共享对象(LOCAL SHARED OBJECTS)

    Flash cookie — 本地共享对象(LOCAL SHARED OBJECTS)写道本地共享对象(有时也称为“Flashcookie”)是一些可由您访问的站点在您的计算机上创建的数据文件。共享对象大多数情况下用来增强您浏览Web的体验。网站可以在您的计算机上编写cookie,当您下次访问该网站时,它将加载该cookie及其信息,从而使您拥有一种更加个性化的体验。例如,您可能让站点记住您的登录名。该信息存储在cookie中,并在您下次访问时被检索…

    2022年7月15日
    14
  • 【保姆级】OpenClaw 全网最细教学:安装→Skills实战→多Agent协作,1 小时全精通!

    【保姆级】OpenClaw 全网最细教学:安装→Skills实战→多Agent协作,1 小时全精通!

    2026年3月13日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号