EOS開發教程(2.4.3)數據持久化

in #eos5 years ago

Step 10準備李嘉圖合約[可選]
要為這個智能合約定義壹個李嘉圖合約,需要創建壹個文件,文件名為addressbook.contracts.md,然後打開這個文件。註意,李嘉圖合約的名字必須與智能合約的名字壹致,在本例中,就是:addressbook。
touch addressbook.contracts.md
把李嘉圖合約定義添加到這個文件中:

upsert

--- spec-version: 0.0.2 title: Upsert summary: This action will either insert or update an entry in the address book. If an entry exists with the same name as the specified user parameter, the record is updated with the first_name, last_name, street, city, and state parameters. If a record does not exist, a new record is created. The data is stored in the multi index table. The ram costs are paid by the smart contract. icon:

erase

--- spec-version: 0.0.2 title: Erase summary: This action will remove an entry from the address book if an entry in the multi index table exists with the specified name. icon:
沒有李嘉圖合約的合約在編譯時,會產生壹個編譯器警告,因為每個action在李嘉圖合約中都缺乏入口。
Warning, action <action_name> does not have a ricardian contract
Step 11準備李嘉圖條款[可選]
要為壹個智能合約定義李嘉圖條款,需要創建壹個文件,文件名為addressbook.clauses.md,並打開它。再次註意,李嘉圖條款的名字必須與這個智能合約的名字壹致。
touch addressbook.clauses.md
添加李嘉圖條款定義到這個文件:

Data Storage

--- spec-version: 0.0.1 title: General Data Storage summary: This smart contract will store data added by the user. The user consents to the storage of this data by signing the transaction. icon:

Data Usage

--- spec-version: 0.0.1 title: General Data Use summary: This smart contract will store user data. The smart contract will not use the stored data for any purpose outside store and delete. icon:

Data Ownership

--- spec-version: 0.0.1 title: Data Ownership summary: The user of this smart contract verifies that the data is owned by the smart contract, and that the smart contract can use the data in accordance to the terms defined in the Ricardian Contract. icon:

Data Distirbution

--- spec-version: 0.0.1 title: Data Ownership summary: The smart contract promises to not actively share or distribute the address data. The user of the smart contract understands that data stored in a multi index table is not private data and can be accessed by any user of the blockchain. icon:

Data Future

--- spec-version: 0.0.1 title: Data Ownership summary: The smart contract promises to only use the data in accordance of the terms defined in the Ricardian Contract, now and at all future dates. icon:
沒有李嘉圖條款的合約編譯時會產生壹個編譯器警告,因為每個action在李嘉圖條款中都沒有入口。
Warning, empty ricardian clause file
Step 12: 編譯合約
在妳的終端上執行下面的命令。
eosio-cpp addressbook.cpp -o addressbook.wasm
如果妳創建了壹個李嘉圖合約和李嘉圖條款,那麽.abi文件中會包含他們的定義。比如addressbook.cpp,它包含了李嘉圖合約和條款的定義,如下所示:
{ "____comment": "This file was generated with eosio-abigen. DO NOT EDIT ", "version": "eosio::abi/1.1", "types": [], "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" } ] } ], "actions": [ { "name": "erase", "type": "erase", "ricardian_contract": "---\nspec-version: 0.0.2\ntitle: Erase\nsummary: his action will remove an entry from the address book if an entry exists with the same name \nicon:" }, { "name": "upsert", "type": "upsert", "ricardian_contract": "---\nspec-version: 0.0.2\ntitle: Upsert\nsummary: This action will either insert or update an entry in the address book. If an entry exists with the same name as the user parameter the record is updated with the first_name, last_name, street, city and state parameters. If a record does not exist a new record is created. The data is stored in the multi index table. The ram costs are paid by the smart contract.\nicon:" } ], "tables": [ { "name": "people", "type": "person", "index_type": "i64", "key_names": [], "key_types": [] } ], "ricardian_clauses": [ { "id": "Data Storage", "body": "---\nspec-version: 0.0.1\ntitle: General data Storage\nsummary: This smart contract will store data added by the user. The user verifies they are happy for this data to be stored.\nicon:" }, { "id": "Data Usage", "body": "---\nspec-version: 0.0.1\ntitle: General data Use\nsummary: This smart contract will store user data. The smart contract will not use the stored data for any purpose outside store and delete \nicon:" }, { "id": "Data Ownership", "body": "---\nspec-version: 0.0.1\ntitle: Data Ownership\nsummary: The user of this smart contract verifies that the data is owned by the smart contract, and that the smart contract can use the data in accordance to the terms defined in the Ricardian Contract \nicon:" }, { "id": "Data Distirbution", "body": "---\nspec-version: 0.0.1\ntitle: Data Ownership\nsummary: The smart contract promises to not actively share or distribute the address data. The user of the smart contract understands that data stored in a multi index table is not private data and can be accessed by any user of the blockchain. \nicon:" }, { "id": "Data Future", "body": "---\nspec-version: 0.0.1\ntitle: Data Ownership\nsummary: The smart contract promises to only use the data in accordance to the terms defined in the Ricardian Contract, now and at all future dates. \nicon:" } ], "variants": [] }
Step 13: 部署合約
為合約創建壹個帳號,執行下面的shell命令:
cleos create account eosio addressbook YOUR_PUBLIC_KEY YOUR_PUBLIC_KEY -p eosio@active
部署addressbook 合約
cleos set contract addressbook CONTRACTS_DIR/addressbook -p addressbook@active
5f78f9aea400783342b41a989b1b4821ffca006cd76ead38ebdf97428559daa0 5152 bytes 727 us # eosio <= eosio::setcode {"account":"addressbook","vmtype":0,"vmversion":0,"code":"0061736d010000000191011760077f7e7f7f7f7f7f... # eosio <= eosio::setabi {"account":"addressbook","abi":"0e656f73696f3a3a6162692f312e30010c6163636f756e745f6e616d65046e616d65... warning: transaction executed locally, but may not be confirmed by the network yet }
Step 14: 測試
向表中添加壹條記錄
cleos push action addressbook upsert '["alice", "alice", "liddell", "123 drink me way", "wonderland", "amsterdam"]' -p alice@active
executed transaction: 003f787824c7823b2cc8210f34daed592c2cfa66cbbfd4b904308b0dfeb0c811 152 bytes 692 us # addressbook <= addressbook::upsert {"user":"alice","first_name":"alice","last_name":"liddell","street":"123 drink me way","city":"wonde...
檢查看alice能不能向其它用戶添加記錄。
cleos push action addressbook upsert '["bob", "bob", "is a loser", "doesnt exist", "somewhere", "someplace"]' -p alice@active
正如我們期望的那樣,我們合約中的require_auth阻止了alice添加/修改其它用戶的內容。
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.
回到alice的記錄這邊來。
cleos get table addressbook addressbook people --lower alice --limit 1
结果
{ "rows": [{ "key": "3773036822876127232", "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: 0a690e21f259bb4e37242cdb57d768a49a95e39a83749a02bced652ac4b3f4ed 104 bytes 1623 us # addressbook <= addressbook::erase {"user":"alice"} warning: transaction executed locally, but may not be confirmed by the network yet ]
檢查那條記錄是不是被刪除了:
cleos get table addressbook addressbook people --lower alice --limit 1
结果
{ "rows": [], "more": false }
不錯,壹切正常!

Coin Marketplace

STEEM 0.18
TRX 0.15
JST 0.028
BTC 63006.79
ETH 2456.26
USDT 1.00
SBD 2.59