EOS 学習メモ:永続データハンドリング編steemCreated with Sketch.

in #eos5 years ago (edited)

dg1uxga7sv.jpg

EOS 学習メモ:ABI 編 の続き。

検証用に実装した contract は コチラ。とても単純な addressbook である。なお、素人目ながら、チュートリアル のコードで「これは要らないんじゃない?」と思ったところなどはちょっと修正してみた。

実装した addressbook contract の概要は以下。

  • データ(person struct)を管理する people table を有する
  • action は upsert(登録・更新)と erase(削除)の 2 つ
  • 他人の account に関するデータついては upserterase もできない

表題の通り、今回は people table を用いた永続データ管理がテーマである。

なお、実装内容に関しての解説はコード内で丁寧にコメントを入れたつもりなのでここでは特に触れない。また、C++ に慣れていないので、とりあえずは呪文だと思って書いているコードもあるが、意味わからな過ぎて死ぬ!みたいなコードはないように思う。Solidity よりはちょっと難しいかなという感じがするけど、まあ慣れだと思う。

と、前置きはこの辺にして、実装した addressbook contract を動かしていく。

まず、実装した addressbook contract をコンテナ上に持っていく。

 $ docker cp contracts/addressbook eos:/tmp/contracts



コンテナに接続。

 $ docker exec -it eos bash



以降、コンテナ上で作業を進める。

早速 addressbook.cpp をコンパイルする。

 $ cd /tmp/contracts/addressbook
 $ eosio-cpp -o addressbook.wasm addressbook.cpp --abigen



addressbook account をつくる。

 $ cleos create account eosio addressbook EOS74MfAgXfFQX36paLQp5tuApRPoHxn4JCcBiwTFWorF5Cj1qMK7 -p eosio@active
 executed transaction: 6d538c460ed1d37d042a3570076adb886be26f4b13e3b1f8f99b24426ddb2fa5  200 bytes  37897 us
 #         eosio <= eosio::newaccount            {"creator":"eosio","name":"addressbook","owner":{"threshold":1,"keys":[{"key":"EOS74MfAgXfFQX36paLQp...



addressbook account に対して contract をデプロイする。この辺の流れには多少慣れてきた感じがする。

 $ cleos set contract addressbook . -p addressbook@active
 Reading WASM from /tmp/contracts/addressbook/addressbook.wasm...
 Publishing contract...
 executed transaction: dea81c1cff28ede28a34ced297a8684432d3bf9f8e6c6400261474bed6bf8f9d  5920 bytes  114314 us
 #         eosio <= eosio::setcode               {"account":"addressbook","vmtype":0,"vmversion":0,"code":"0061736d01000000019d011960027f7e0060077f7e...
 #         eosio <= eosio::setabi                {"account":"addressbook","abi":"0e656f73696f3a3a6162692f312e31000205657261736500010475736572046e616d...



デプロイできたら、alice のデータを登録してみる。

 $ cleos push action addressbook upsert '["alice", "alice", "liddell", "123 drink me way", "wonderland", "amsterdam"]' -p alice@active
 executed transaction: 01dc9afd489e4e38fe549e00edf624df602a697181dcfb4799289c0d171809c2  152 bytes  11596 us
 #   addressbook <= addressbook::upsert          {"user":"alice","first_name":"alice","last_name":"liddell","street":"123 drink me way","city":"wonde...



ちなみに、alicebob のデータを登録しようとするとエラーになる。これは、contract で第 1 引数に関して require_auth() をかけているから(EOS 学習メモ:コントラクト実行権限編 でやったのと同じ)。

 $ cleos push action addressbook upsert '["bob", "bob", "is a loser", "doesnt exist", "somewhere", "someplace"]' -p alice@active
 Error 3090004: 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 bob
 pending console output:



先ほど登録した alice のデータを取得してみる。

 $ cleos get table addressbook addressbook people --lower alice --limit 1
 {
   "rows": [{
       "key": "alice",
       "first_name": "alice",
       "last_name": "liddell",
       "street": "123 drink me way",
       "city": "wonderland",
       "state": "amsterdam"
     }
   ],
   "more": false
 }



問題なさそう。ということで、次は alice のデータを削除してみる。

 $ cleos push action addressbook erase '["alice"]' -p alice@active
 executed transaction: 378896f7ec305307f35804dc9429e4ac22be53332c6c70582f12156cd3a205bc  104 bytes  7742 us
 #   addressbook <= addressbook::erase           {"user":"alice"}



削除できたかどうか確認してみる。

 $ cleos get table addressbook addressbook people --lower alice --limit 1
 {
   "rows": [],
   "more": false
 }



問題なさそう。

一応、今回実装した addressbook contract の ABI も眺めてみる。

 {
     "____comment": "This file was generated with eosio-abigen. DO NOT EDIT Fri Feb  1 11:16:19 2019",
     "version": "eosio::abi/1.1",
     "structs": [
         {
             "name": "erase",
             "base": "",
             "fields": [
                 {
                     "name": "user",
                     "type": "name"
                 }
             ]
         },
         {
             "name": "person",
             "base": "",
             "fields": [
                 {
                     "name": "key",
                     "type": "name"
                 },
                 {
                     "name": "first_name",
                     "type": "string"
                 },
                 {
                     "name": "last_name",
                     "type": "string"
                 },
                 {
                     "name": "street",
                     "type": "string"
                 },
                 {
                     "name": "city",
                     "type": "string"
                 },
                 {
                     "name": "state",
                     "type": "string"
                 }
             ]
         },
         {
             "name": "upsert",
             "base": "",
             "fields": [
                 {
                     "name": "user",
                     "type": "name"
                 },
                 {
                     "name": "first_name",
                     "type": "string"
                 },
                 {
                     "name": "last_name",
                     "type": "string"
                 },
                 {
                     "name": "street",
                     "type": "string"
                 },
                 {
                     "name": "city",
                     "type": "string"
                 },
                 {
                     "name": "state",
                     "type": "string"
                 }
             ]
         }
     ],
     "types": [],
     "actions": [
         {
             "name": "erase",
             "type": "erase",
             "ricardian_contract": ""
         },
         {
             "name": "upsert",
             "type": "upsert",
             "ricardian_contract": ""
         }
     ],
     "tables": [
         {
             "name": "people",
             "type": "person",
             "index_type": "i64",
             "key_names": [],
             "key_types": []
         }
     ],
     "ricardian_clauses": [],
     "variants": [],
     "abi_extensions": []
 }



前回学習したことを逸脱するような要素はなさそう。

今回はここまで。

Sort:  

✅ Enjoy the vote! For more amazing content, please follow @themadcurator!

Congratulations @m0t0k1ch1! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 200 upvotes. Your next target is to reach 300 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.28
TRX 0.13
JST 0.032
BTC 65999.51
ETH 3019.75
USDT 1.00
SBD 3.71