EOS柚子区块链平台构建HelloWorld智能合约
蓝天天使2017
发表于 2022-11-12 13:43:43
84
0
0
一般环境设置通过上一篇文章已经说明,这方面的问题大家可以看本博客上一篇文章,本文引用了官方EOS在Git上的示例。- u* |; H7 ]" f) b$ `7 M
运行nodeos4 _& O7 M7 ^% |" W0 ]
要通过Hello World日志进行验证,请使用contracts-console选项运行它。
nodeos --contracts-console
生成智能合约: G# Z/ a$ Y$ i/ m& {
让我们尝试通过一个不同的新的终端上(除了nodeos的终端之外)访问EOS目录,创建一个名为Hello的文件夹。命令如下:
$ cd eos
$ cd mkdir hello1 k1 [' L7 c/ |7 l2 L9 n3 B
$ cd hello* v+ N- n5 b- d7 x, B
然后,创建hello.cpp文件并复制下面的示例。Hello合约继承了EOS.IO提供的合约,并具有打印出用户名Hello的功能。0 z: P) g" y5 V( K8 w
编译代码
对于第一个命令,让我们用WebAssembly编译代码。编译时,可能会提示警告(但我们可以忽略它)。对于第二个命令,让我们创建ABI。
0 S+ X v* s% O4 e$ R* o' r
# Compile webaassembly
eosiocpp -o hello.wast hello.cpp
# Creating abi
eosiocpp -g hello.abi hello.cpp% q" I- r' w, D, l
帐户创建和合约发布- S/ s0 ~- `- d8 \, }
创建一个名为Hello.Code的帐户。我们将进一步使用这个帐户发布Hello合约。+ n* z% z+ w3 ?) F* ~" O
用cleos set contract ${account} ${path} -p ${permmition}命令发布合约。' ]& b. M0 o' ~% u/ t" B- }' h
$ cleos create account eosio hello.code EOS8QMGRoRPZ4uf3w8WACcrg3wKzLtXpCk5Gpia6pdFzSuftLigWT EOS8QMGRoRPZ4uf3w8WACcrg3wKzLtXpCk5Gpia6pdFzSuftLigWT
executed transaction: e6847fc85c7733dd70a9ff27c2cad98ea0b50fb6c80c2b0c7ea1bf64f9917916 200 bytes 225 us
# eosio
调用函数
让我们调用hello类的hi函数。它可以用下面的命令来完成:1 q4 C# z5 S, k, u1 m. n
cleos push action ${contract_name} ${function} ${[argument]} -p ${permission}: T% _8 Q( i" a' W8 z+ B: V
在下面的示例中,hello.code合约中的用户帐户调用hi函数。
$ cleos push action hello.code hi '["user"]' -p user; \* c. [, C& n5 i! }- H f
executed transaction: d7932d1ee61ab6b0fed1f9e20d4a2e2607b029763aeaf1daea4ed718d2885797 104 bytes 500 us( t/ A) O9 g: R% I3 L
# hello.code
结果
在nodeos终端中执行交易的块中添加了以下输出。hello是名为hi函数的用户的名字。' _4 h4 G5 L/ g
2703777ms thread-0 apply_context.cpp:28 print_debug ] M$ g! Z, k" O' ^3 c. F
[(hello.code,hi)->hello.code]: CONSOLE OUTPUT BEGIN =====================7 G# W* D; D) u% P
Hello, user
[(hello.code,hi)->hello.code]: CONSOLE OUTPUT END =====================: P' b; w' u0 S }" \/ I! N3 x
授权运行请求
替换为包含授权请求调用hi函数。* T; x3 g6 ^1 R+ g9 O' U+ W
/ N! K w" v2 U5 j8 y9 n
结果
当我们试图用一个未经授权的帐户调用它时,会出现一个错误(如下)。使用授权帐户,它正常输出无错误。
# Authorized. {$ Q1 l, W9 s- ~
$ cleos push action hello.code hi '["tester"]' -p user# h9 ?; B+ x$ X2 n
Error 3090004: missing required authority
# Unauthorized# z( Z% s4 B6 O6 A7 w1 G
$ cleos push action hello.code hi '["tester"]' -p tester0 \' ~7 H2 y& a( J" ?
executed transaction: 16a34c27c7d162dc3940358197306df619911fb930cbddd6d208125a770886f4 104 bytes 243 us! G; u! y6 X+ d( t
# hello.code
可以在nodeos的块中按如下方式打印日志:+ T8 r, {9 \0 S, e# ?
2525788ms thread-0 http_plugin.cpp:405 handle_exception ] FC Exception encountered while processing chain.push_transaction
2525788ms thread-0 http_plugin.cpp:406 handle_exception ] Exception Details: 3090004 missing_auth_exception: missing required authority2 T$ z( L1 K+ j
missing authority of tester {"account":"tester"}0 u# K6 _, u4 V1 S4 g& q
thread-0 apply_context.cpp:132 require_authorization {"_pending_console_output.str()":""}/ B+ k& V2 ~. K" b6 w5 ~" N+ J: v
thread-0 apply_context.cpp:62 exec_one
成为第一个吐槽的人