EOS智能合约授权限制和数据存储
在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