如何使用Solidity编写智能合约的异步交易模式

许多开发人员在开发Solidity之前实现了     Java   ,Go,     Python   感觉就像回到80年代后期的DeLorean一样。 但是Solidity的稳定性非常有限。

 如何使用Solidity编写智能合约的异步交易模式_设计制作_测量仪表

我正在使用名为#Scrip     tI   t的队长的NodeJS oracle用于以下用例:

1. 新用户获得256分

2. 每次新呼叫,用户的积分将减少log2

船长将直接从Docker容器中的Solidity运行NodeJS调用,并将结果返回给您的合约。

智能合约

异步联系将派生自usingCapt     ai   nJS,其中包括异步调用和回调功能。

要在回调发生时记住异步调用,您需要一个JobCoun     te   r以及作业ID和发件人地址的映射:

uint JobCounter = 0;

map     pi   ng (uint =》 address) JobToSenderMap;

事件

在以太坊中,当同步事务处于挂起状态,事务只有失败或成功两种状态。异步事务将要求发出事件时,通知用户事务是否挂起、成功或失败。

因此,您定义三个这些事件,并且每个事件至少应包含发件人地址:

event GetPoints_Success(address Sender, uint Points);

event GetPoints_Pending(address Sender);

event GetPoints_Fai     led   (address Sender, string ErrorMsg);

函数

以太坊的默认模式是每个用户调用一个合约函数,并支付在一个同步事务环境中执行代码所需的GAS。

但现在我们有了一个异步事务环境, 这意味着在同步函数调用终止后将需要额外的气体。

因此,您的函数必须是payable,您的首次检查必须是验证用户是否转移了足够的额外gas费用:

uint GasRequired = DEFAULT_GAS_UNITS * tx.gasprice + 70 szabo;

require(msg.value 》= GasRequired, “please send some extra gas.。.”);

在这个演示用例中,我们将要求使用usingCaptainJS中定义的默认gas单位乘以当前的交易gas价格加上70 Szabo的交易费。

一旦用户输送了足够gas,你可以根据船长在GitHub上的描述来调用mathjs的log2函数:

Run(

JobCounter,

concat(“math:log2(”,uintToString(PointsPerUser[msg.sender]), “)”),

“”, “”, 1, DEFAULT_GAS_UNITS, tx.gasprice

);

e     mi   t GetPoints_Pending(msg.sender);

在调用Run(。..)之后,您必须发出pending事件。如果调用Run(。..)失败,则同步调用将失败。

回调

一旦船长计算了用户积分的log2值,他就会通过调用CaptainsResult函数将结果发送回合约。通过仅添加CaptainsOrde     rs   Allowed确保只有队长调用此功能。

确保在函数结束时发出成功事件。

func  TI on CaptainsResult(uint JobCounter, string Log2Result)

external onlyCaptainsOrdersAllowed {

// the return of the async call

address sender = JobToSenderMap[JobCounter];

uint Points = StringToUint(Log2Result);

PointsPerUser[sender] = Points;

emit GetPoints_Success(sender, Points);

}

果队长无法调用您提交的代码(也许您的     JavaScript   代码中有拼写错误),他会通过调用合同的CaptainsError函数通知您。

确保在函数结束时发出失败的事件。

func  TI on CaptainsError(uint JobCounter, string ErrorMsg)

external onlyCaptainsOrdersAllowed {

// the return of the async call

address sender = JobToSenderMap[JobCounter];

emit GetPoints_Failed(sender, ErrorMsg);

}

这是完整的代码:

pragma solidity ^0.4.25;

import “。/usingCaptainJS_v2.sol”;

contract AsyncPattern is usingCaptainJS {

// to ident     if   y async calls

uint JobCounter = 0;

mapping (uint =》 address) JobToSenderMap;

// demo use case: points per sender

mapping (address =》 uint) PointsPerUser;

event GetPoints_Success(address Sender, uint Points);

event GetPoints_Pending(address Sender);

event GetPoints_Failed(address Sender, string ErrorMsg);

func  TI on GetPoints() public payable {

// make sure to have enough gas for the async callback

uint GasRequired = DEFAULT_GAS_UNITS * tx.gasprice + 70 szabo;

require(msg.value 》= GasRequired, “please send some extra gas.。.”);

// remember this call

JobToSenderMap[++JobCounter] = msg.sender;

// now do the math - but mix async + async.。.

// every user has 256 points at the beginning and with every next

// call it is log2 of his points

if(PointsPerUser[msg.sender] == 0) {

// first call!

PointsPerUser[msg.sender] = 256;

emit GetPoints_Success(msg.sender, 256);

}

else {

// every other call

Run(

JobCounter, concat(“math:log2(”, uintToString(PointsPerUser[msg.sender]), “)”),

“”, “”, 1, DEFAULT_GAS_UNITS, tx.gasprice

);

emit GetPoints_Pending(msg.sender);

}

}

func  TI on CaptainsResult(uint JobCounter, string Log2Result) external onlyCaptainsOrdersAllowed {

// the return of the async call

address sender = JobToSenderMap[JobCounter];

uint Points = StringToUint(Log2Result);

PointsPerUser[sender] = Points;

emit GetPoints_Success(sender, Points);

}

function CaptainsError(uint JobCounter, string ErrorMsg) external onlyCaptainsOrdersAllowed {

// the return of the async call

address sender = JobToSenderMap[JobCounter];

emit GetPoints_Failed(sender, ErrorMsg);

}

}

68
111
0
27

相关资讯

  1. 1、Win10系统太卡怎么办?Win10系统磁盘清理教程1542
  2. 2、Win7旗舰版无法识别的usb设备怎么解决?2807
  3. 3、美篇app中怎么设置去除作者名美篇APP设置去除作者名的具体步骤1083
  4. 4、怎么抢快手2020春晚红包?2020快手春晚10亿红包领取方法介绍3115
  5. 5、英雄联盟盒子怎么查看英雄的胜率?3490
  6. 6、Win10怎么设置鼠标光标的样式?2994
  7. 7、必应输入法皮肤编辑器怎么用?必应输入法皮肤编辑器使用方法4445
  8. 8、Win7系统如何使用360浏览器设置鼠标手势?3643
  9. 9、如何开通支付宝巨星卡支付宝巨星卡开通方法3375
  10. 10、VMware虚拟机安装微软Win11无TPM模块怎么安装?3774
全部评论(0)
我也有话说
0
收藏
点赞
顶部