1寸痢,摘要
本文假設(shè)你已經(jīng)完成了鉑鏈本地節(jié)點(diǎn)部署和賬戶創(chuàng)建,沒有完成的話,參考《鉑鏈第3課 BOTTOS賬戶體系(密鑰對(duì)/賬號(hào)/錢包)的創(chuàng)建和管理》完成相關(guān)準(zhǔn)備工作迟隅。
本文的主要內(nèi)容包括:
(1)用C++創(chuàng)建"Hello World"合約
(2)編譯wasm和ABI文件
(3)部署和運(yùn)行合約
2,內(nèi)容
2.1 前置條件-本地節(jié)點(diǎn)啟動(dòng)和賬戶創(chuàng)建
1)啟動(dòng)本地節(jié)點(diǎn)
參考《鉑鏈第2課 如何部署節(jié)點(diǎn)并加入BOTTOS測(cè)試網(wǎng)絡(luò)励七?》 完成本地節(jié)點(diǎn)部署智袭。
啟動(dòng)本地節(jié)點(diǎn)
./bottos --delegate=bottos --enable-wallet
2)創(chuàng)建賬戶,質(zhì)押BTO
請(qǐng)參考《鉑鏈第3課 BOTTOS賬戶體系(密鑰對(duì)/賬號(hào)/錢包)的創(chuàng)建和管理》 完成用戶賬號(hào)的創(chuàng)建掠抬,從系統(tǒng)賬號(hào)轉(zhuǎn)賬一定的BTO并質(zhì)押200個(gè)BTO吼野。
賬戶信息:
賬號(hào)名稱: wangdenghui
密鑰對(duì):
public_key: 049b98b5f5eea7fd5145842d08f8cd25052f69e731f9f550ac8a2e37792e165cf13fbc52ad7dad32eaa192799601b4cc35eab1923e007048f9d47c80aa4bf9cb8d
private_key: 1d2533b83d5c811b53b7503aad9488310631b705e9df12bbce8c03149be559fd
賬戶余額:800BTO
質(zhì)押情況:100 BTO for time; 100 BTO for space;
2.2 創(chuàng)建合約
C++和C語言的語法两波,輝哥就不展開分析了瞳步。假設(shè)我們創(chuàng)建了相關(guān)的文件。
1) testHelloWorld.cpp
三個(gè)函數(shù)腰奋,打印“hello world...”出來谚攒。
#include "contractcomm.hpp"
extern "C" {
int start();
int add();
int del();
}
int start()
{
myprints("hello world in start");
return 0;
}
int add()
{
myprints("hello world in add");
return 0;
}
int del()
{
myprints("hello world in del");
return 0;
}
2) testHelloWorld.h
頭文件定義
//@abi action start
struct NullStruct {
};
2.3 編譯產(chǎn)生wasm和ABI文件
(1)制定合約工作目錄和下載編譯版本
假設(shè)我們創(chuàng)建/home/duncanwang/go/work目錄作為BOTOS工作目錄。
在工作目錄下氛堕,下載編譯工具:
git clone https://github.com/bottos-project/contract-tool-cpp.git
【成功執(zhí)行結(jié)果】
duncanwang@ubuntu64bit-server:~/go/work$ git clone https://github.com/bottos-project/contract-tool-cpp.git
Cloning into 'contract-tool-cpp'...
remote: Enumerating objects: 31, done.
remote: Counting objects: 100% (31/31), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 237 (delta 16), reused 23 (delta 10), pack-reused 206
Receiving objects: 100% (237/237), 26.89 MiB | 2.02 MiB/s, done.
Resolving deltas: 100% (128/128), done.
Checking out files: 100% (41/41), done.
(2) 編譯合約產(chǎn)生WASM文件
上面合約代碼其實(shí)鉑鏈的編譯工具已經(jīng)自帶了馏臭,輝哥就不重復(fù)上傳了。只是提示下讼稚,如果發(fā)生Access Denied的問題括儒,記得chmod 777修改文件讀寫屬性即可。
進(jìn)入合約目錄testHelloWorld,然后運(yùn)行下面命令編譯合約
python ../gentool.py --type wasm --file testHelloWorld.cpp
運(yùn)行后锐想,新的testHelloWorld.wasm和testHelloWorld.wast已經(jīng)產(chǎn)生了帮寻。
【成功結(jié)果】
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ python ../gentool.py --type wasm --file testHelloWorld.cpp
../bin/clang -emit-llvm -O3 --std=c++14 --target=wasm32 -ffreestanding -nostdlib -fno-threadsafe-statics -fno-rtti -fno-exceptions -I ../lib -I . -c testHelloWorld.cpp -o ./tmpdir/build/testHelloWorld.cpp
In file included from testHelloWorld.cpp:1:
../lib/contractcomm.hpp:181:22: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
myprints("ERROR: Get my contract name failed.");
^
../lib/contractcomm.hpp:196:18: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
myprints("getBinValue failed!");
^
testHelloWorld.cpp:11:14: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
myprints("hello world in start");
^
testHelloWorld.cpp:18:14: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
myprints("hello world in add");
^
testHelloWorld.cpp:25:14: warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]
myprints("hello world in del");
^
5 warnings generated.
../bin/llvm-link -o ./tmpdir/linked.bc ./tmpdir/build/*
../bin/llc --asm-verbose=false -o ./tmpdir/assembly.s ./tmpdir/linked.bc
../bin/s2wasm -o testHelloWorld.wast -s 16384 ./tmpdir/assembly.s
常見問題及解決方案
1)【失敗結(jié)果】- python未安裝
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp$ python
Command 'python' not found, but can be installed with:
sudo apt install python3
sudo apt install python
sudo apt install python-minimal
You also have python3 installed, you can run 'python3' instead.
【安裝出錯(cuò)】
duncanwang@ubuntu64bit-server:~/go/work$ sudo apt install python
[sudo] password for duncanwang:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal python2.7 python2.7-minimal
Suggested packages:
python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-minimal python2.7 python2.7-minimal
0 upgraded, 7 newly installed, 0 to remove and 75 not upgraded.
Need to get 3,949 kB of archives.
After this operation, 16.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:3 http://archive.ubuntu.com/ubuntu bionic/main amd64 python-minimal amd64 2.7.15~rc1-1 [28.1 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic/main amd64 libpython2.7-stdlib amd64 2.7.15~rc1-1 [1,910 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic/main amd64 python amd64 2.7.15~rc1-1 [140 kB]
Err:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 libpython2.7-minimal amd64 2.7.15~rc1-1
502 Bad Gateway [IP: 117.143.109.143 80]
Err:2 http://archive.ubuntu.com/ubuntu bionic/main amd64 python2.7-minimal amd64 2.7.15~rc1-1
502 Bad Gateway [IP: 117.143.109.142 80]
Get:5 http://archive.ubuntu.com/ubuntu bionic/main amd64 python2.7 amd64 2.7.15~rc1-1 [238 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic/main amd64 libpython-stdlib amd64 2.7.15~rc1-1 [7,620 B]
Fetched 2,324 kB in 17s (136 kB/s)
E: Failed to fetch http://117.143.109.143/cache/archive.ubuntu.com/ubuntu/pool/main/p/python2.7/libpython2.7-minimal_2.7.15~rc1-1_amd64.deb?ich_args2=144-13213206052547_3259d609842731f004706c767b1bb39a_10001002_9c89602ed6c2f4d8903c518939a83798_5a2e3e24305ad5ad963c61d5902f3d1f 502 Bad Gateway [IP: 117.143.109.143 80]
E: Failed to fetch http://117.143.109.142/cache/archive.ubuntu.com/ubuntu/pool/main/p/python2.7/python2.7-minimal_2.7.15~rc1-1_amd64.deb?ich_args2=144-13213210052808_ca0ba91cda916deada222929a3eb7ae4_10001002_9c89602ed6c2f4d8903c518939a83798_6d64072715e5c504a601fe11b7a54dbe 502 Bad Gateway [IP: 117.143.109.142 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
【翻墻-安裝成功有輸出】
duncanwang@ubuntu64bit-server:~/go/work$ sudo apt install python
[sudo] password for duncanwang:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal python2.7 python2.7-minimal
Suggested packages:
python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-minimal python2.7 python2.7-minimal
0 upgraded, 7 newly installed, 0 to remove and 75 not upgraded.
Need to get 334 kB/3,949 kB of archives.
After this operation, 16.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu bionic/main amd64 libpython2.7-minimal amd64 2.7.15~rc1-1 [334 kB]
Fetched 334 kB in 3s (124 kB/s)
Selecting previously unselected package libpython2.7-minimal:amd64.
(Reading database ... 102201 files and directories currently installed.)
Preparing to unpack .../0-libpython2.7-minimal_2.7.15~rc1-1_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.15~rc1-1) .......................................................................................................................................]
Selecting previously unselected package python2.7-minimal..................................................................................................................................]
...
Processing triggers for mime-support (3.60ubuntu1) ...##########################################################################################...........................................]
Processing triggers for man-db (2.8.3-2) ...
Setting up libpython2.7-stdlib:amd64 (2.7.15~rc1-1) ...
Setting up python2.7 (2.7.15~rc1-1) ...###################################################################################################################.................................]
Setting up libpython-stdlib:amd64 (2.7.15~rc1-1) ...###############################################################################################################........................]
Setting up python (2.7.15~rc1-1) ...########################################################################################################################################...............]
2)文件目錄不正確
【錯(cuò)誤輸出】
duncanwang@ubuntu64bit-server:~/go/work/RegUser$ python ~/go/work/contract-tool-cpp/gentool.py wasm testRegUser.cpp
Error! Please keep these files [clang | llc | llvm-link | s2wasm] under current directory.
【解決方法】
工作目錄要建立在~/go/work/contract-tool-cpp下,不要建立在其他地方赠摇。
(3) 編譯合約產(chǎn)生ABI文件
運(yùn)行以下命令產(chǎn)生ABI文件固逗。
python ../gentool.py --file testHelloWorld.hpp
【成功結(jié)果】
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ python ../gentool.py --file testHelloWorld.hpp
[ testHelloWorld.abi ] is generated. Please have a check.
下載已產(chǎn)生的ABI,可以看到testHelloWorld.abi內(nèi)容如下:
{
"types": [],
"structs": [
{
"name": "NullStruct",
"base": "",
"fields": {
}
}
],
"actions": [
{
"action_name": "start",
"type": "NullStruct"
}
],
"tables": [
]
}
簡(jiǎn)單介紹一下abi文件的組成:
structs :掃描出來的結(jié)構(gòu)體的定義描述藕帜,這兒是"NullStruct"烫罩,供后面使用;
actions:合約提供的方法描述洽故,其中action_name 是方法名贝攒,例如 "start"函數(shù), type 是調(diào)用合約所要的參數(shù)时甚;
tables :合約持久化數(shù)據(jù)訪問接口描述隘弊,即合約保存的數(shù)據(jù)描述哈踱,其中table_name 是表名,index_type 是索引的類型梨熙, key_names 和key_types 分別是鍵值的名稱和類型开镣,type 是保存的數(shù)據(jù)的結(jié)構(gòu)定義。
abi文件是通過掃描hpp文件生成咽扇, 在hpp文件里通過注釋來告訴掃描器具體的定義:
"http://@abi action start":
? 本合約定義了兩個(gè)方法邪财,其中start 對(duì)應(yīng)的入?yún)⒍x為NullStruct;
”//@abi table testTableName:[index_type:string, key_names:keyName, key_types:string] “
? 定義了一個(gè)表肌割, 表內(nèi)容的結(jié)構(gòu)體定義為testTableName 。本案例不存在帐要。
2.3 部署和運(yùn)行合約
(1)部署智能合約與ABI文件
部署合約d的命令如下把敞,該命令成功后將返回BCLI成功發(fā)送的Transaction信息。
./bcli contract deploy
參數(shù)描述如下:
--contract 合約名
--code 合約文件(.WASM)所在路徑
--filetype 合約文件類型:wasm/js
--abi 合約文件(.abi)
--account 部署合約的賬戶
在操作前榨惠,需要把bcli 命令從BOTTOS安裝環(huán)境復(fù)制過來奋早。
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ cp /home/duncanwang/go/src/github.com/bottos/bcli .
然后,把wangdenghui賬號(hào)unlock赠橙,把部署的合約取名為babycry,樣例命令如下:
./bcli contract deploy --contract babycry --account wangdenghui --code testHelloWorld.wasm --abi testHelloWorld.abi
【成功交易結(jié)果】
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ ./bcli contract deploy --contract babycry --account wangdenghui --code testHelloWorld.wasm --abi testHelloWorld.abi
TrxHash: fc5527e0600b353c0c38c87b64ec29c8b8856fc36ec83575f916b1cc556def7f
This transaction is sent. Please check its result by command : bcli transaction get --trxhash <hash>
【查詢交易結(jié)果】
./bcli transaction get --trxhash fc5527e0600b353c0c38c87b64ec29c8b8856fc36ec83575f916b1cc556def7f
【結(jié)果】
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ ./bcli transaction get --trxhash fc5527e0600b353c0c38c87b64ec29c8b8856fc36ec83575f916b1cc556def7f
{
"ResourceReceipt": {
"account_name": "wangdenghui",
"space_token_cost": 2138,
"time_token_cost": 100
},
"Transaction": {
"contract": "bottos",
"cursor_label": 1543494136,
"cursor_num": 5304,
"lifetime": 1546681090,
"method": "deploycontract",
"param": {
"contract": "babycry",
"contract_abi": "7b0a09227479706573223a205b5d2c0a092273747275637473223a205b0a20202020202020202020202020207b0a202020202020202020202020202009226e616d65223a20224e756c6c537472756374222c0a2020202020202020202020202020092262617365223a2022222c0a202020202020202020202020202009226669656c6473223a207b0a20202020202020202020202020202020202020207d0a2020202020202020202020202020097d0a202020202020205d2c0a0922616374696f6e73223a205b0a20202020202020202020202020207b0a20202020202020202020202020200922616374696f6e5f6e616d65223a20227374617274222c0a2020202020202020202020202020092274797065223a20224e756c6c537472756374220a20202020202020202020202020207d0a202020202020205d2c0a09227461626c6573223a205b0a202020202020205d0a7d0a",
"contract_code": "0061736d01000000010d0360027f7f006000006000017f020e0103656e76067072696e74730000030504010202020404017000000503010001074205066d656d6f7279020005737461727400020361646400030364656c0004215f474c4f42414c5f5f7375625f495f7465737448656c6c6f576f726c642e63707000010a9c0304f202004100420037028c4041004200370294404100420037029c40410042003702a440410042003702ac40410041003602b440410041003602b840410041003602bc40410041003602c040410041003602c440410041003602c840410041003602cc40410041003602d040410041003602d440410041003602d840410041003602dc40410041003602e040410041003602e440410041003602e840410041003602ec40410041003602f040410041003602f440410041003602f840410041003602fc404100410036028041410041003602844141004100360288414100410036028c414100410036029041410041003602944141004100360298414100410036029c41410041003602a041410041003602a441410041003602a841410041003602ac41410041003602b041410041003602b441410041003602b841410041003602bc41410041003602c041410041003602c441410041003602c841410041003602cc41410041003602d041410041003602d4410b0c004180c5004114100041000b0c0041a0c5004112100041000b0c0041c0c5004112100041000b0b5a040041040b04e0620000004180c5000b1568656c6c6f20776f726c6420696e207374617274000041a0c5000b1368656c6c6f20776f726c6420696e20616464000041c0c5000b1368656c6c6f20776f726c6420696e2064656c00",
"vm_type": 1,
"vm_version": 1
},
"sender": "wangdenghui",
"sig_alg": 1,
"signature": "65b8fe0a75949e3250ae1074234989fc3850af7179ef184d233e0c15d7e5a3025e7c343b557756af3e516b16cdda126c7f9728a65788a73ed6e8f4ba49890a9c",
"version": 65536
},
"TrxHash": "fc5527e0600b353c0c38c87b64ec29c8b8856fc36ec83575f916b1cc556def7f"
}
<<<Transaction Status>>> : commited
【提醒】
如果已把bcli復(fù)制到系統(tǒng)bin文件夾耽装,則直接執(zhí)行bcli ...,如果是復(fù)制到工作目錄期揪,則需要輸入./bcli ...格式掉奄。
復(fù)制bcli命令到本地工程目錄,命令調(diào)用格式:./bcli ...
cp /home/duncanwang/go/src/github.com/bottos/bcli .
復(fù)制到可執(zhí)行文件夾凤薛,命令調(diào)用格式:bcli ...
cp /home/duncanwang/go/src/github.com/bottos/bcli /usr/bin/.
【常見錯(cuò)誤提示】
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ ./bcli contract deploy --contract babyContract --account wangdenghui --code testHelloWorld.wasm --abi testHelloWorld.abi
Error: Please input the correct contract's name, [a-z,0-9], 3-10 character, begin with a-z
【分析說明】
合約名稱不超過10個(gè)字符姓建,只能是小寫字母和數(shù)字[a-z,0-9],這個(gè)太戲劇了缤苫,10個(gè)字符能表達(dá)一個(gè)好的合約名字嗎速兔?建議鉑鏈采用不超過32個(gè)字符的名字均可。
(2)運(yùn)行合約
運(yùn)行合約的命令如下活玲,該命令成功后將返回指定Trxhash對(duì)應(yīng)的Transaction信息涣狗。
./bcli transaction push
參數(shù)說明:
--sender 簽名發(fā)起者(缺省為內(nèi)置bottos用戶)
--contract 合約名,格式為合約名@部署賬戶
--method 合約方法名
--param 參數(shù)鍵值對(duì)
--sign 用戶自定義公鑰(缺省為內(nèi)置缺省值)
【運(yùn)行合約樣例】
./bcli transaction push --sender wangdenghui --method start --contract babycry@wangdenghui
【執(zhí)行成功結(jié)果】
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ ./bcli transaction push --sender wangdenghui --method start --contract babycry@wangdenghui
TrxHash: 50226dc38a102b4dd848ecebbf3cb3668886f7b0f10158acd435e315efcb930a
This transaction is sent. Please check its result by command : bcli transaction get --trxhash <hash>
【查看合約是否部署成功】
./bcli transaction get --trxhash "50226dc38a102b4dd848ecebbf3cb3668886f7b0f10158acd435e315efcb930a"
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ ./bcli transaction get --trxhash "50226dc38a102b4dd848ecebbf3cb3668886f7b0f10158acd435e315efcb930a"
<<<Transaction Status>>> : commited
./bcli gettable --account wangdenghui --table testTableName --key testKeyName
./bcli transaction get --trxhash "c0780dd99c1d38b613cc3c644309a897b022dfbcf45c029ee7e1b701aa6385db"
【重大疑問】-輸出并沒有"hello world"的打印
BOTTOS的命令行輸出沒有Info打印信息,所有信息都在~/go/src/github.com/bottos/datadir/log的bottos.log文件顯示舒憾。
但是默認(rèn)情況下不顯示info信息镀钓,需要執(zhí)行命令改變。
修改LOG輸出等級(jí)的命令:
./bcli log setconfigitem
參數(shù)說明:
--key 日志參數(shù)镀迂,選其一設(shè)置:minlevel maxlevel
--value 按日志參數(shù)掸宛,填寫對(duì)應(yīng)項(xiàng)值
例如:
./bcli log setconfigitem --key minlevel --value debug
【調(diào)整LOG等級(jí)輸出結(jié)果】
duncanwang@ubuntu64bit-server:~/go/work/contract-tool-cpp/testHelloWorld$ ./bcli log setconfigitem --key minlevel --value debug
{
"errcode": 0
}
setconfigitem successfully.
再執(zhí)行合約。
然后再執(zhí)行./bcli transaction push命令招拙,就可以在bottos.log文件搜索"hello world in start"關(guān)鍵字可以看到對(duì)應(yīng)的"hello world ..."打印唧瘾。
如下:
2019-01-05 10:39:21.173 [INF] res-processor.go:444 GetUserFreeTimeLimit(): RESOURCE:GetUserFreeTime account:wangdenghui, limit:{Available:202 Used:198 Max:400}
2019-01-05 10:39:21.173 [INF] res-processor.go:420 GetUserTimeLimit(): RESOURCE:GetUserTimeLimit account:wangdenghui, limit:{Available:28799999709 Used:291 Max:28800000000}
2019-01-05 10:39:21.174 [INF] env_func.go:442 prints(): VM prints value hello world in start
至此措译,BOTTOS最簡(jiǎn)合約的編寫及運(yùn)行講解完畢。
3饰序,參考
1) 鉑鏈官網(wǎng)
http://www.bottos.org/
2)鉑鏈開發(fā)手冊(cè)
https://docs.botfans.org/en/
3)鉑鏈GITHUB
https://github.com/bottos-project
4)研發(fā)幫助文檔
https://github.com/bottos-project/Documentation/tree/master/resource_cn
5)BCLI使用說明
https://github.com/bottos-project/Documentation/blob/master/resource_cn/BCLI%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E.md
本篇文章得到鉑鏈技術(shù)專家張偉的指導(dǎo)领虹,謹(jǐn)表感謝。