許多開發(fā)人員在開發(fā)Solidity之前實(shí)現(xiàn)了Java,Go,Python 感覺就像回到80年代后期的DeLorean一樣。 但是Solidity的穩(wěn)定性非常有限。
我正在使用名為#ScriptIt的隊(duì)長(zhǎng)的NodeJS oracle用于以下用例:
1. 新用戶獲得256分
2. 每次新呼叫,用戶的積分將減少log2
船長(zhǎng)將直接從Docker容器中的Solidity運(yùn)行NodeJS調(diào)用,并將結(jié)果返回給您的合約。
智能合約
異步聯(lián)系將派生自u(píng)singCaptainJS,其中包括異步調(diào)用和回調(diào)功能。
要在回調(diào)發(fā)生時(shí)記住異步調(diào)用,您需要一個(gè)JobCounter以及作業(yè)ID和發(fā)件人地址的映射:
uint JobCounter = 0;
mapping (uint =》 address) JobToSenderMap;
事件
在以太坊中,當(dāng)同步事務(wù)處于掛起狀態(tài),事務(wù)只有失敗或成功兩種狀態(tài)。異步事務(wù)將要求發(fā)出事件時(shí),通知用戶事務(wù)是否掛起、成功或失敗。
因此,您定義三個(gè)這些事件,并且每個(gè)事件至少應(yīng)包含發(fā)件人地址:
event GetPoints_Success(address Sender, uint Points);
event GetPoints_Pending(address Sender);
event GetPoints_Failed(address Sender, string ErrorMsg);
函數(shù)
以太坊的默認(rèn)模式是每個(gè)用戶調(diào)用一個(gè)合約函數(shù),并支付在一個(gè)同步事務(wù)環(huán)境中執(zhí)行代碼所需的GAS。
但現(xiàn)在我們有了一個(gè)異步事務(wù)環(huán)境, 這意味著在同步函數(shù)調(diào)用終止后將需要額外的氣體。
因此,您的函數(shù)必須是payable,您的首次檢查必須是驗(yàn)證用戶是否轉(zhuǎn)移了足夠的額外gas費(fèi)用:
uint GasRequired = DEFAULT_GAS_UNITS * tx.gasprice + 70 szabo;
require(msg.value 》= GasRequired, “please send some extra gas.。.”);
在這個(gè)演示用例中,我們將要求使用usingCaptainJS中定義的默認(rèn)gas單位乘以當(dāng)前的交易gas價(jià)格加上70 Szabo的交易費(fèi)。
一旦用戶輸送了足夠gas,你可以根據(jù)船長(zhǎng)在GitHub上的描述來(lái)調(diào)用mathjs的log2函數(shù):
Run(
JobCounter,
concat(“math:log2(”,uintToString(PointsPerUser[msg.sender]), “)”),
“”, “”, 1, DEFAULT_GAS_UNITS, tx.gasprice
);
emit GetPoints_Pending(msg.sender);
在調(diào)用Run(。..)之后,您必須發(fā)出pending事件。如果調(diào)用Run(。..)失敗,則同步調(diào)用將失敗。
回調(diào)
一旦船長(zhǎng)計(jì)算了用戶積分的log2值,他就會(huì)通過(guò)調(diào)用CaptainsResult函數(shù)將結(jié)果發(fā)送回合約。通過(guò)僅添加CaptainsOrdersAllowed確保只有隊(duì)長(zhǎng)調(diào)用此功能。
確保在函數(shù)結(jié)束時(shí)發(fā)出成功事件。
function 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);
}
果隊(duì)長(zhǎng)無(wú)法調(diào)用您提交的代碼(也許您的JavaScript代碼中有拼寫錯(cuò)誤),他會(huì)通過(guò)調(diào)用合同的CaptainsError函數(shù)通知您。
確保在函數(shù)結(jié)束時(shí)發(fā)出失敗的事件。
function 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 identify 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);
function 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);
}
}
function 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);
}
}
評(píng)論
查看更多