如何使用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、微软Win11商店22107.1401.6.0更新内容有哪些?1975
  2. 2、Win7屏幕刷新率如何更改?Win7屏幕刷新率更改的方法1421
  3. 3、快压怎么添加文件注释?文件注释添加的方法256
  4. 4、Win10电脑怎么调整cf烟雾头?最新Win10调整cf烟雾头盔教程429
  5. 5、Excel中怎么圈选无效数据?Excel圈选无效数据的方法1858
  6. 6、迅雷已删除任务如何重新下载?迅雷X已删除任务重新下载方法简述4109
  7. 7、bandicam中怎么给视频添加水印?给视频添加水印的方法4460
  8. 8、Win10电脑一装系统就出现电脑蓝屏怎么办?1814
  9. 9、如何使用快手制作表白视频使用快手制作表白视频的方法479
  10. 10、千牛工作台如何设置防骚扰?千牛工作台设置防骚扰的方法教程3469
全部评论(0)
我也有话说
0
收藏
点赞
顶部