這篇文章主要介紹“怎么為EOSIO平臺(tái)開發(fā)智能合約”,在日常操作中,相信很多人在怎么為EOSIO平臺(tái)開發(fā)智能合約問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么為EOSIO平臺(tái)開發(fā)智能合約”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比新城網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式新城網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋新城地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。
示例智能合約的目的是模擬選舉。我創(chuàng)建了一個(gè)EOSIO用戶來(lái)托管智能合約。創(chuàng)建了兩個(gè)公民用戶來(lái)投票給候選人。投票記錄保存在EOSIO區(qū)塊鏈中。在此示例中,所有操作都在命令模式下運(yùn)行。讓我們開始吧。
EOSIO執(zhí)行以WebAssembly標(biāo)準(zhǔn)開發(fā)的智能合約。所以我用C++開發(fā)了選舉智能合約。以下是election.cpp
的完整源代碼:
#include <eosiolib/eosio.hpp> using namespace eosio; class election : public contract { private: // create the multi index tables to store the data /// @abi table struct candidate { uint64_t _key; // primary key std::string _name; // candidate name uint32_t _count = 0; // voted count uint64_t primary_key() const { return _key; } }; typedef eosio::multi_index<N(candidate), candidate> candidates; /// @abi table struct voter { uint64_t _key; uint64_t _candidate_key; // name of poll account_name _account; // this account has voted, avoid duplicate voter uint64_t primary_key() const { return _key; } uint64_t candidate_key() const { return _candidate_key; } }; typedef eosio::multi_index<N(voter), voter, indexed_by<N(_candidate_key), const_mem_fun<voter, uint64_t, &voter::candidate_key>>> voters; // local instances of the multi indexes candidates _candidates; voters _voters; uint64_t _candidates_count; public: election(account_name s) : contract(s), _candidates(s, s), _voters(s, s), _candidates_count(0) {} // public methods exposed via the ABI // on candidates /// @abi action void version() { print("Election Smart Contract version 0.0.1\n"); }; /// @abi action void addc(std::string name) { print("Adding candidate ", name, "\n"); uint64_t key = _candidates.available_primary_key(); // update the table to include a new candidate _candidates.emplace(get_self(), [&](auto &p) { p._key = key; p._name = name; p._count = 0; }); print("Candidate added successfully. candidate_key = ", key, "\n"); }; /// @abi action void reset() { // Get all keys of _candidates std::vector<uint64_t> keysForDeletion; for (auto &itr : _candidates) { keysForDeletion.push_back(itr.primary_key()); } // now delete each item for that poll for (uint64_t key : keysForDeletion) { auto itr = _candidates.find(key); if (itr != _candidates.end()) { _candidates.erase(itr); } } // Get all keys of _voters keysForDeletion.empty(); for (auto &itr : _voters) { keysForDeletion.push_back(itr.primary_key()); } // now delete each item for that poll for (uint64_t key : keysForDeletion) { auto itr = _voters.find(key); if (itr != _voters.end()) { _voters.erase(itr); } } print("candidates and voters reset successfully.\n"); }; /// @abi action void results() { print("Start listing voted results\n"); for (auto& item : _candidates) { print("Candidate ", item._name, " has voted count: ", item._count, "\n"); } }; /// @abi action void vote(account_name s, uint64_t candidate_key) { require_auth(s); bool found = false; // Did the voter vote before? for (auto& item : _voters) { if (item._account == s) { found = true; break; } } eosio_assert(!found, "You're voted already!"); // Findout the candidate by id std::vector<uint64_t> keysForModify; for (auto& item : _candidates) { if (item.primary_key() == candidate_key) { keysForModify.push_back(item.primary_key()); break; } } if (keysForModify.size() == 0) { eosio_assert(found, "Invalid candidate id!"); return; } // Update the voted count inside the candidate for (uint64_t key : keysForModify) { auto itr = _candidates.find(key); auto candidate = _candidates.get(key); if (itr != _candidates.end()) { _candidates.modify(itr, get_self(), [&](auto& p) { p._count++; }); print("Voted candidate: ", candidate._name, " successfully\n"); } } // Add this user to voters array _voters.emplace(get_self(), [&](auto& p) { p._key = _voters.available_primary_key(); p._candidate_key = candidate_key; p._account = s; }); }; }; EOSIO_ABI(election, (version)(reset)(addc)(results)(vote))
注意最后一行EOSIO_ABI()
是一個(gè)宏語(yǔ)句,用于自動(dòng)生成ABI文件而不是手動(dòng)編寫。ABI文件用于定義提交動(dòng)作處理程序。這告訴了EOSIO智能合約中處理程序的定義。
EOSIO為我們提供了多索引數(shù)據(jù)庫(kù)API,可以將數(shù)據(jù)保存到區(qū)塊鏈中。在上面的選舉智能合約中,我定義了兩個(gè)multi_index
(類似于SQL表):候選人和選民。實(shí)際上是兩個(gè)數(shù)組存儲(chǔ)兩個(gè)結(jié)構(gòu):候選者和選民。我使用C++ STL來(lái)操作multi_index
,例如add
,update
,delete
。
請(qǐng)注意,兩個(gè)結(jié)構(gòu)在開頭標(biāo)有/// @abi table
。這是告訴EOSIO abi生成器在election.abi
文件中生成ABI表。這很方便。
編譯選舉智能合約:
$ eosiocpp -o election.wast election.cpp
分別生成WAST和WASM文件。但這對(duì)EOSIO來(lái)說(shuō)還不夠。我們還需要生成ABI文件:
$ eosiocpp -g election.abi election.cpp
為了增強(qiáng)開發(fā)體驗(yàn),我為Visual Studio Code(VSCode)創(chuàng)建了一個(gè)屬性文件c_cpp_properties.json
,告訴它如何查找頭文件。該文件需要存儲(chǔ)在.vscode
目錄中,如下所示:
.vscode/c_cpp_properties
文件內(nèi)容如下:
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "~/eos/contracts", "~/opt/boost/include" ], "defines": [], "compilerPath": "/usr/bin/clang++-4.0", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 }
一直在使用配置良好的虛擬機(jī)(在第1部分中提到)。要啟動(dòng)單節(jié)點(diǎn)Testnet服務(wù)器:
$ nodeos -e -p eosio --plugin eosio::wallet_api_plugin --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin --access-control-allow-origin=* --contracts-console
單擊此處獲取nodeos參數(shù)的更多信息。
下一個(gè)任務(wù)是解鎖默認(rèn)錢包。EOSIO將密鑰對(duì)存儲(chǔ)在錢包中。每次服務(wù)器重啟或每15分鐘需要解鎖一次。解鎖錢包:
$ cleos wallet unlock --password ${wallet_password}
我們需要分別創(chuàng)建一個(gè)所有者密鑰對(duì)和活動(dòng)密鑰對(duì)。然后將該私鑰導(dǎo)入錢包。鍵入以下命令:
$ cleos create key # Create an owner key $ cleos create key # Create an active key $ cleos wallet import ${private_owner_key} $ cleos wallet import ${private_active_key}
不要忘記在某個(gè)地方記錄這些密鑰對(duì)。
接下來(lái)的任務(wù)是創(chuàng)建一個(gè)新的帳戶來(lái)保存選舉智能合約。 鍵入以下命令:
$ cleos create account eosio election ${public_owner_key} ${public_active_key}
此外,為投票模擬創(chuàng)建兩個(gè)公民:
$ cleos create account eosio voter1 ${public_owner_key} ${public_active_key} $ cleos create account eosio voter2 ${public_owner_key} ${public_active_key}
輸入以下命令上傳選舉智能合約:
$ cleos set contract election ../election -p election
結(jié)果類似下圖:
我們可以嘗試運(yùn)行合約。
1.運(yùn)行version
操作
$ cleos push action election version '' -p election
我們可以從nodeos檢查控制臺(tái)輸出:
2.增加選舉候選人
$ cleos push action election addc '["Hillary Clinton"]' -p election $ cleos push action election addc '["Donald J. Trump"]' -p election
3.顯示存儲(chǔ)在區(qū)塊鏈中的候選數(shù)據(jù)庫(kù)
$ cleos get table election election candidate
結(jié)果如圖所示:
4.模擬投票(兩位選民都被投票給唐納德·J·特朗普)
$ cleos push action election vote '["voter1", 1]' -p voter1 $ cleos push action election vote '["voter2", 1]' -p voter2
如果voter1再次投票:
$ cleos push action election vote '["voter1", 0]' -p voter1
EOSIO 將返回一個(gè)例外:
5.查看投票結(jié)果
$ cleos get table election election candidate
如你所見,候選人“Donald J. Trump”的投票數(shù)為2.這意味著選舉智能合約正在工作!
到此,關(guān)于“怎么為EOSIO平臺(tái)開發(fā)智能合約”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
分享名稱:怎么為EOSIO平臺(tái)開發(fā)智能合約
新聞來(lái)源:http://vcdvsql.cn/article8/gjeoip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、全網(wǎng)營(yíng)銷推廣、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站營(yíng)銷、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)