日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/9/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 EOS智能合约授权限制和数据存储 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

EOS智能合約授權(quán)限制和數(shù)據(jù)存儲(chǔ)

在EOS合約中,調(diào)用合約需要來(lái)自賬戶的授權(quán),同時(shí)還要指定需要調(diào)用的動(dòng)作。當(dāng)然,有的合約并不是所有賬戶都可以調(diào)用的,這就需要用到授權(quán)限制。接下來(lái)我們就來(lái)看看如何限制合約的授權(quán)賬戶。

合約案例

為了更好的演示,寫(xiě)了一個(gè)下課和吃飯的智能合約小例子。這個(gè)合約有兩個(gè)動(dòng)作,下課和吃飯。教師賬戶可以調(diào)用下課動(dòng)作,學(xué)生賬戶可以調(diào)用吃飯動(dòng)作。教師調(diào)用下課動(dòng)作后,學(xué)生才能調(diào)用吃飯動(dòng)作。接下來(lái)我們來(lái)看代碼:

teacher.hpp

頭文件teacher.hpp定義了兩個(gè)動(dòng)作,over是class over 下課動(dòng)作,eat是吃飯動(dòng)作。

#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 tablestruct 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動(dòng)作的實(shí)現(xiàn)。

#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;//存儲(chǔ)動(dòng)作調(diào)用狀態(tài)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 );}elseprint("teacher:Class time, you can't eat"); }

仔細(xì)觀察這段代碼就會(huì)發(fā)現(xiàn),over和eat動(dòng)作中都有個(gè)"require_auth()"語(yǔ)句。在over動(dòng)作中,"requir_auth(teacher)"的作用是限制只有"teacher"賬戶才可以調(diào)用over動(dòng)作。在eat動(dòng)作中則是限制只有"student"賬戶才可調(diào)用eat動(dòng)作。

合約數(shù)據(jù)存儲(chǔ)

此合約設(shè)計(jì)為下課后才可以吃飯,所以當(dāng)教師賬戶調(diào)用合約的over動(dòng)作后,需要存儲(chǔ)一個(gè)合約調(diào)用狀態(tài)值。EOS合約的數(shù)據(jù)存儲(chǔ)需要用數(shù)據(jù)庫(kù),把數(shù)據(jù)存到一張表中,這是令人很難受的。

teacher.hpp

在teacher.hpp中創(chuàng)建一個(gè)結(jié)構(gòu)體。下段代碼中的"//@abi table"注釋非常重要,必須要寫(xiě)的,如果不寫(xiě)數(shù)據(jù)將無(wú)法存儲(chǔ)。

static uint64_t id;// @abi tablestruct 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來(lái)插入數(shù)據(jù),使用modify來(lái)修改數(shù)據(jù),使用erase刪除數(shù)據(jù)。

teacher.cpp

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

在over動(dòng)作中創(chuàng)建數(shù)據(jù),學(xué)生調(diào)用eat動(dòng)作后再修改或刪除數(shù)據(jù)即可

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 );}

合約調(diào)用效果展示

  • 授權(quán)限制
$ 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
  • 學(xué)生沒(méi)下課就吃飯
$ 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!
  • 查看表中數(shù)據(jù)
$ cleos get table class class mshare {"rows": [{"id": 1,"start": 1}],"more": false }

整個(gè)合約寫(xiě)下來(lái)調(diào)通也是費(fèi)了我很多腦細(xì)胞,數(shù)據(jù)存儲(chǔ)也比較坑爹啊。現(xiàn)在把我的一點(diǎn)經(jīng)驗(yàn)分享出來(lái),希望大家在學(xué)習(xí)EOS的路上少踩一些坑。


歡迎添加微信(id:pdj107)

轉(zhuǎn)載于:https://www.cnblogs.com/tokenpai/p/9175959.html

總結(jié)

以上是生活随笔為你收集整理的EOS智能合约授权限制和数据存储的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。