交易所开发系统如何重新编译并部署合约(国王小组)

简介: 交易所开发系统如何重新编译并部署合约(国王小组)

交易所开发系统如何重新编译并部署合约(国王小组)
动作的授权
EOSIO 区块链使用非对称密码学来验证推送交易的账户是否已使用匹配的私钥签署了交易。使用账户权限表来检查账户是否具有执行操作所需的权限。使用授权是保护智能合约的第一步。

本文档提供了四种方式在合约代码中进行授权检查。

1、has_auth(name n) 函数
验证指定账户是否与调用动作的账户相符,返回bool值。

以hi动作为例,如果我们希望动作只能向调用账户进行问好,而不相符的账户名进行消息提示,可以将动作部分的代码做如下调整:

  [[eosio::action]] void hi( eosio::name user ) {
     if(has_auth( user )){
        print("Hello, ", eosio::name{user} );
     }else{
        print("This is not ",eosio::name{user} );
     }
  }

重新编译并部署合约:

eosio-cpp -abigen -o hello.wasm hello.cpp
cleos set contract hello /home/xxx/biosboot/genesis/hello -p hello@active
使用alice账户对bob账户和alice账户分别调用hi动作。结果分别如下:

cleos push action hello hi '["bob"]' -p alice@active

executed transaction: 20a297df95fe19840893305a8f18e6380ee3482a3773885224f6381487559105 104 bytes 601 us

hello <= hello::hi {"user":"bob"}

This is not bob
cleos push action hello hi '["alice"]' -p alice@active

executed transaction: 3f12347d3458204be643a2c4cef5ef3542994ed3bb320466aac423e1a983e8d7 104 bytes 204 us

hello <= hello::hi {"user":"alice"}

Hello, alice
2、require_auth(name n) 函数
验证指定账户是否与调用动作的账户相符,不相符则直接报错失败。

与has_auth函数不同,此函数会在验证失败时直接停止运行。同样以hi动作为例,如果我们希望动作只能向调用账户进行问好,而对不相符的账户直接显示失败,可以将动作部分的代码做如下调整:

  [[eosio::action]] void hi( eosio::name user ) {
     require_auth( user );
     print("Hello, ", eosio::name{user} );
  }

重新编译并部署合约:

eosio-cpp -abigen -o hello.wasm hello.cpp
cleos set contract hello /home/xxx/biosboot/genesis/hello -p hello@active
使用alice账户对bob账户和alice账户分别调用hi动作。结果分别如下:

cleos push action hello hi '["bob"]' -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:
cleos push action hello hi '["alice"]' -p alice@active

executed transaction: 495b0e8d6fa3610f5b91ffd28eb4013d1a53a1e1e13046b6a3069566bfeaf65f 104 bytes 168 us

hello <= hello::hi {"user":"alice"}

Hello, alice
3、require_auth2(capi_name name, capi_name permission) 函数
验证指定账户和权限是否与调用动作的账户和权限相符,不相符则直接报错失败。

此函数比之前增加了对账户权限的限制。同样以hi动作为例,如果我们希望动作只能由账户的active权限进行调用,而对不相符的账户直接显示失败,可以将动作部分的代码做如下调整:

  [[eosio::action]] void hi( eosio::name user ) {
     require_auth2(user.value, "active"_n.value);
     print("Hello, ", eosio::name{user} );
  }

重新编译并部署合约:

eosio-cpp -abigen -o hello.wasm hello.cpp
cleos set contract hello /home/xxx/biosboot/genesis/hello -p hello@active
使用alice账户的family和active权限分别对alice账户调用hi动作。结果分别如下:

cleos push action hello hi '["alice"]' -p alice@family

Error 3090003: Provided keys, permissions, and delays do not satisfy declared authorizations
Ensure that you have the related private keys inside your wallet and your wallet is unlocked.
Error Details:
transaction declares authority '{"actor":"alice","permission":"family"}', but does not have signatures for it.
cleos push action hello hi '["alice"]' -p alice@active

executed transaction: 055228767c6f23c283 910ebc348d8a76d444fa1165454c9886dd58b940023ef5 104 bytes 395 us

hello <= hello::hi {"user":"alice"}

Hello, alice
4、check(bool pred, ...) 函数
断言,如果pred为假,则使用提供的消息进行反馈。例如:

eosio::check(a == b, "a does not equal b");

因此,之前我们实现的在参数与账户不相符时打印错误信息的代码可以优化为:

  [[eosio::action]] void hi( eosio::name user ) {
     eosio::check(has_auth(user), "User is not authorized to perform this action.");
     print("Hello, ", eosio::name{user} );
  }

重新编译并部署合约:

eosio-cpp -abigen -o hello.wasm hello.cpp
cleos set contract hello /home/xxx/biosboot/genesis/hello -p hello@active
使用alice账户对bob账户和alice账户分别调用hi动作。结果分别如下:

cleos push action hello hi '["bob"]' -p alice@active

Error 3050003: eosio_assert_message assertion failure
Error Details:
assertion failure with message: User is not authorized to perform this action.
pending console output:
cleos push action hello hi '["alice"]' -p alice@active

executed transaction: 86887c76f45d9c0f9abc017f2cfc49132bd5d0c1d6bbd7f41aeb7bc1675ab42c 104 bytes 172 us

hello <= hello::hi {"user":"alice"}

Hello, alice
关于上链
在之后的代码实践中,开发人员可以通过以上这四种授权检查代码的搭配来实现合约中动作的授权管理。

对于触发Error导致方法运行中断的情况,交易记录不会上链。只有当交易ID产生,动作正常运行完成,此次记录才会记录在链上。

对于上文的举例来说,第一种代码的错误案例完成了上链。因为交易正常结束,并打印出了错误信息。而后面三种分别触发了Error 3090003、Error 3090004、Error 3050003,导致动作中断,数据不会上链。

五、常见问题
(一)部署合约时遇到错误
<3>error 2022-08-08T08:44:27.913 cleos main.cpp:4371 operator() ] Failed with error: unspecified (0)
Unable to resolve path './hello'
遇到Unable to resolve path错误,可以将合约地址改为绝对路径,避免因相对路径产生的报错。

(二)require_auth2编译错误
/home/xxx/biosboot/genesis/hello/hello.cpp:6:10: error: use of undeclared identifier 'require_auth2'; did you mean 'eosio::internal_use_do_not_use::require_auth2'?
require_auth2(user.value, "active"_n.value);
^~~~~
eosio::internal_use_do_not_use::require_auth2
常规情况中可依据报错提示信息将require_auth2补充为eosio::internal_use_do_not_use::require_auth2解决。但本例中该前缀已经表明这是一个不推荐使用的方法。

故推荐的解决方法为引入action头文件。

include <../capi/eosio/action.h>

相关文章
|
4月前
|
存储 监控 关系型数据库
BRC20铭文合约NFT铸造交易平台系统开发稳定版/方案详细/步骤逻辑/源码指南
网络拓扑结构:设计一个稳定且高性能的网络拓扑结构对于BRC20铭文智能合约跨链系统的成功运行至关重要。
|
11月前
|
安全 区块链 数据安全/隐私保护
迪斯卡(Disca)众筹质押挖矿系统项目开发/Solidity编写
迪斯卡(Disca)众筹质押挖矿系统项目开发/Solidity编写
|
12月前
|
安全 前端开发 JavaScript
Swap薄饼去中心化交易所发行代币合约兑换底池项目系统开发(稳定版)丨详细步骤丨需求方案丨功能设计丨逻辑项目丨案例设计丨指南流程
Requirement analysis: Clarify project objectives and functional requirements. Understand the basic principles and mechanisms of Swap exchanges, such as liquidity provision, transaction matching, and fee allocation.
|
数据管理 区块链
DAPP智能合约/泰山众筹/互助公排模式系统开发合约源代码详情
def tfs(contract, token_name, user_address, amount): # 检查用户是否有足够的代币 balance = contract.functions.balanceOf(token_name, user_address).call()
|
开发框架 监控 前端开发
dapp/PancakeSwap薄饼交易所发行代币合约系统开发稳定版丨成熟技术丨案例详细丨规则玩法丨源码程序
 DApp是“去中心化应用程序”的缩写,它是使用区块链技术和智能合约构建的应用程序。与传统的应用程序不同,DApp不依赖于单一实体,而是由区块链网络中的多个节点共同维护和运行。这使得DApp具有去中心化、开放、透明和安全的特点。
|
应用服务中间件 网络安全 开发工具
永续合约/交割合约/币币合约交易所系统开发成熟稳定版/案例项目/方案策略/源码程序
永续合约是一种特殊的期货合约,与传统期货不同,永续合约没有到期日。因此在永续合约的交易中,用户可以一直持有合约直到平仓。另外,永续合约引入了现货价格指数的概念,并通过相应机制,使永续合约的价格回归现货指数价格,Therefore,unlike traditional futures,the price of perpetual contracts will not deviate too much from the spot
币币合约丨永续合约丨交割合约丨秒合约丨交易所系统开发(案例开发)/实现方案/成熟技术/稳定版及源码
 合约交易是一种金融衍生品,它是相对于现货市场的交易,用户可以在期货合约交易中通过判断涨跌,选择买入做多或者卖出做空合约,To gain the benefits of price increases or decreases.
|
安全 分布式数据库 区块链
Disrupt DEX迪斯克众筹质押挖矿系统开发(稳定版)丨迪斯克Disrupt DEX质押众筹挖矿系统开发(详情逻辑)/源码案例
 区块链技术是一种去中心化的分布式数据库技术,其本质是一种基于密码学原理、点对点网络和共识机制的数据管理方式。区块链可以记录所有参与者的交易,每个交易被记录在一个数据块(Block)中,并且每个数据块都包含了前一个数据块的信息,这样就形成了一个不可篡改的数据链(Chain),也就是所谓的“区块链”。
|
消息中间件 SQL 网络协议
国王小组:开发数字货币交易所源码搭建数据反馈处理器如何设置
DAPP交易所系统开发(开发案例)丨DAPP交易所系统开发(源码及方案) 交易所开发成品丨交易所系统开发(演示版)丨交易所APP源码设计 区块链交易所开发详细丨区块链交易所系统开发(开发方案)丨区块链交易所源码案例部署 Uniswap交易所开发稳定版丨Uniswap交易所系统开发(开发模板)丨Uniswap交易所系统源码案例部署 数字货币交易所开发详情版丨数字货币交易所系统开发(web3.0技术开发)丨数字货币交易所开发源码成品 交易所APP开发功能丨交易所系统开发(成熟及案例)丨交易所系统源码平台 DAPP交易所系统开发(开发案例)丨DAPP交易所系统开发(源码及方案)
国王小组:开发数字货币交易所源码搭建数据反馈处理器如何设置
|
运维 监控 安全
秒合约交易所系统开发搭建,需要注意以下几点(国王小组)
秒合约交易所系统开发搭建,需要注意以下几点(国王小组)