Skip to content

Commit 339c15f

Browse files
authored
Merge pull request #31 from AntChainOpenLabs/feat/sdp-call-biz-failed-with-event-not-revert
Feat/sdp call biz failed with event not revert
2 parents 604c5b9 + 4f1f4e8 commit 339c15f

File tree

27 files changed

+374
-85
lines changed

27 files changed

+374
-85
lines changed

pluginset/eos/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*/.idea
2+
**/cmake**
3+
**/*.wasm
4+
**/*.abi

pluginset/eos/offchain-plugin/pom.xml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@
2929
</repositories>
3030

3131
<dependencies>
32+
<dependency>
33+
<groupId>one.block</groupId>
34+
<artifactId>eosiojava</artifactId>
35+
<version>${eosiojava.version}</version>
36+
<exclusions>
37+
<exclusion>
38+
<groupId>org.slf4j</groupId>
39+
<artifactId>slf4j-api</artifactId>
40+
</exclusion>
41+
</exclusions>
42+
</dependency>
3243
<dependency>
3344
<groupId>com.alipay.antchain.bridge</groupId>
3445
<artifactId>antchain-bridge-plugin-lib</artifactId>
@@ -40,11 +51,6 @@
4051
<version>0.2.1</version>
4152
<scope>provided</scope>
4253
</dependency>
43-
<dependency>
44-
<groupId>one.block</groupId>
45-
<artifactId>eosiojava</artifactId>
46-
<version>${eosiojava.version}</version>
47-
</dependency>
4854
<dependency>
4955
<groupId>one.block</groupId>
5056
<artifactId>eosiojavasoftkeysignatureprovider</artifactId>
@@ -65,12 +71,6 @@
6571
<artifactId>eos-java-rpc-wrapper</artifactId>
6672
<version>master</version>
6773
</dependency>
68-
<dependency>
69-
<groupId>org.slf4j</groupId>
70-
<artifactId>slf4j-api</artifactId>
71-
<version>1.7.30</version>
72-
<scope>test</scope>
73-
</dependency>
7474
<dependency>
7575
<groupId>junit</groupId>
7676
<artifactId>junit</artifactId>

pluginset/eos/offchain-plugin/src/main/java/com/alipay/antchain/bridge/plugins/eos/EosBBCService.java

Lines changed: 115 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,15 @@
3030
import cn.hutool.crypto.digest.DigestUtil;
3131
import com.alibaba.fastjson.JSON;
3232
import com.alibaba.fastjson.JSONArray;
33+
import com.alibaba.fastjson.JSONObject;
3334
import com.alibaba.fastjson.JSONPath;
3435
import com.alipay.antchain.bridge.commons.bbc.AbstractBBCContext;
3536
import com.alipay.antchain.bridge.commons.bbc.syscontract.AuthMessageContract;
3637
import com.alipay.antchain.bridge.commons.bbc.syscontract.ContractStatusEnum;
3738
import com.alipay.antchain.bridge.commons.bbc.syscontract.SDPContract;
3839
import com.alipay.antchain.bridge.commons.core.base.CrossChainMessage;
3940
import com.alipay.antchain.bridge.commons.core.base.CrossChainMessageReceipt;
40-
import com.alipay.antchain.bridge.plugins.eos.types.EosBlockInfo;
41-
import com.alipay.antchain.bridge.plugins.eos.types.EosTransactionStatusEnum;
42-
import com.alipay.antchain.bridge.plugins.eos.types.EosTxActions;
43-
import com.alipay.antchain.bridge.plugins.eos.types.EosTxInfo;
41+
import com.alipay.antchain.bridge.plugins.eos.types.*;
4442
import com.alipay.antchain.bridge.plugins.eos.utils.Utils;
4543
import com.alipay.antchain.bridge.plugins.lib.BBCService;
4644
import com.alipay.antchain.bridge.plugins.spi.bbc.AbstractBBCService;
@@ -61,6 +59,7 @@
6159
import one.block.eosiojava.session.TransactionProcessor;
6260
import one.block.eosiojava.session.TransactionSession;
6361
import one.block.eosiojavaabieosserializationprovider.AbiEosSerializationProviderImpl;
62+
import one.block.eosiojavarpcprovider.error.EosioJavaRpcProviderCallError;
6463
import one.block.eosiojavarpcprovider.error.EosioJavaRpcProviderInitializerError;
6564
import one.block.eosiojavarpcprovider.implementations.EosioJavaRpcProviderImpl;
6665
import one.block.eosiosoftkeysignatureprovider.SoftKeySignatureProviderImpl;
@@ -184,6 +183,16 @@ public class EosBBCService extends AbstractBBCService {
184183
*/
185184
private static final String AM_RECV_PKG_FROM_RELAYER_PARAMETER_FORMAT = "{\"invoker\": \"%s\", \"pkg_hex\": \"%s\"}";
186185

186+
/**
187+
* reject the message when calling {@code recvrelayerx} failed
188+
*/
189+
private static final String AM_REJECT_AM_FROM_RELAYER_ACTION = "rejectamx";
190+
191+
/**
192+
* - 参数格式
193+
*/
194+
private static final String AM_REJECT_AM_FROM_RELAYER_PARAMETER_FORMAT = "{\"invoker\": \"%s\", \"pkg_hex\": \"%s\"}";
195+
187196
// ============================== 插件基本变量 ==============================
188197
private EosConfig config;
189198

@@ -389,7 +398,16 @@ public CrossChainMessageReceipt readCrossChainMessageReceipt(String txHash) {
389398
crossChainMessageReceipt.setSuccessful(txInfo.isSuccess());
390399
crossChainMessageReceipt.setConfirmed(txInfo.isConfirmed());
391400
crossChainMessageReceipt.setTxhash(txHash);
392-
crossChainMessageReceipt.setErrorMsg(crossChainMessageReceipt.isSuccessful() ? "SUCCESS" : txInfo.getStatus().getStatus());
401+
if (txInfo.containsAction(config.getAmContractAddressDeployed(), AM_REJECT_AM_FROM_RELAYER_ACTION)) {
402+
crossChainMessageReceipt.setErrorMsg(
403+
"call biz failed but seq updated: " + (crossChainMessageReceipt.isSuccessful() ? "SUCCESS" : txInfo.getStatus().getStatus())
404+
);
405+
crossChainMessageReceipt.setSuccessful(false);
406+
} else {
407+
crossChainMessageReceipt.setErrorMsg(
408+
"call biz: " + (crossChainMessageReceipt.isSuccessful() ? "SUCCESS" : txInfo.getStatus().getStatus())
409+
);
410+
}
393411

394412
getBBCLogger().info("cross chain message receipt for tx {} : {}", crossChainMessageReceipt.getTxhash(), crossChainMessageReceipt.getErrorMsg());
395413

@@ -555,6 +573,9 @@ public void setProtocol(String protocolAddress, String protocolType) {
555573
},
556574
}
557575
);
576+
if (ObjectUtil.isNull(sendTransactionResponse)) {
577+
throw new RuntimeException("null transaction response means that calling tx failed");
578+
}
558579
EosTxInfo txInfo = bbcGetTxInfoByTransactionHashOnRpc(sendTransactionResponse.getTransactionId());
559580
if (ObjectUtil.isNull(txInfo)) {
560581
throw new RuntimeException(String.format("failed to get transaction info %s", sendTransactionResponse.getTransactionId()));
@@ -659,6 +680,9 @@ public void setAmContract(String contractAddress) {
659680
},
660681
}
661682
);
683+
if (ObjectUtil.isNull(sendTransactionResponse)) {
684+
throw new RuntimeException("null transaction response means that calling tx failed");
685+
}
662686
EosTxInfo txInfo = bbcGetTxInfoByTransactionHashOnRpc(sendTransactionResponse.getTransactionId());
663687
if (ObjectUtil.isNull(txInfo)) {
664688
throw new RuntimeException(String.format("failed to get transaction info %s", sendTransactionResponse.getTransactionId()));
@@ -716,6 +740,9 @@ public void setLocalDomain(String domain) {
716740
},
717741
}
718742
);
743+
if (ObjectUtil.isNull(sendTransactionResponse)) {
744+
throw new RuntimeException("null transaction response means that calling tx failed");
745+
}
719746
EosTxInfo txInfo = bbcGetTxInfoByTransactionHashOnRpc(sendTransactionResponse.getTransactionId());
720747
if (ObjectUtil.isNull(txInfo)) {
721748
throw new RuntimeException(String.format("failed to get transaction info %s", sendTransactionResponse.getTransactionId()));
@@ -792,39 +819,82 @@ public CrossChainMessageReceipt relayAuthMessage(byte[] rawMessage) {
792819
HexUtil.encodeHexStr(rawMessage), this.bbcContext.getAuthMessageContract().getContractAddress());
793820

794821
// 2. invoke am contract
795-
SendTransactionResponse sendTransactionResponse = bbcInvokeContractsOnRpc(
796-
new String[][]{
797-
{
798-
this.bbcContext.getAuthMessageContract().getContractAddress(),
799-
AM_RECV_PKG_FROM_RELAYER_ACTION,
800-
String.format(
801-
AM_RECV_PKG_FROM_RELAYER_PARAMETER_FORMAT,
802-
this.config.getUserName(),
803-
generateRandomPrefix() + HexUtil.encodeHexStr(rawMessage)
804-
)
805-
},
806-
}
807-
);
808-
EosTxInfo txInfo = bbcGetTxInfoByTransactionHashOnRpc(sendTransactionResponse.getTransactionId());
809-
if (ObjectUtil.isNull(txInfo)) {
810-
throw new RuntimeException(String.format("failed to get transaction info %s", sendTransactionResponse.getTransactionId()));
822+
String baseMsg = "call biz";
823+
EosTxInfo txInfo = null;
824+
SendTransactionResponse sendTransactionResponse = bbcInvokeContractsOnRpc(getRelayAuthMessageArgs(rawMessage));
825+
if (ObjectUtil.isNotNull(sendTransactionResponse)) {
826+
txInfo = bbcGetTxInfoByTransactionHashOnRpc(sendTransactionResponse.getTransactionId());
827+
if (ObjectUtil.isNull(txInfo)) {
828+
throw new RuntimeException(String.format("failed to get transaction info %s", sendTransactionResponse.getTransactionId()));
829+
}
830+
waitUntilTxIrreversible(txInfo);
811831
}
812832

813-
waitUntilTxIrreversible(txInfo);
833+
if (ObjectUtil.isNull(txInfo) || !txInfo.isSuccess()) {
834+
getBBCLogger().error("failed to call biz contract with tx {}", ObjectUtil.isNull(txInfo) ? "no tx sent" : txInfo.getTxId());
835+
836+
sendTransactionResponse = bbcInvokeContractsOnRpc(getRejectAuthMessageArgs(rawMessage));
837+
if (ObjectUtil.isNull(sendTransactionResponse)) {
838+
getBBCLogger().error("calling `rejectamx` failed");
839+
} else {
840+
txInfo = bbcGetTxInfoByTransactionHashOnRpc(sendTransactionResponse.getTransactionId());
841+
if (ObjectUtil.isNull(txInfo)) {
842+
throw new RuntimeException(String.format("failed to get transaction info %s for adding seq", sendTransactionResponse.getTransactionId()));
843+
}
844+
845+
baseMsg = "call biz failed but seq updated";
846+
getBBCLogger().info("send tx {} to add sequence by 1", txInfo.getTxId());
847+
}
848+
}
814849

815850
// 3. check transaction
816851
CrossChainMessageReceipt crossChainMessageReceipt = new CrossChainMessageReceipt();
817852

818-
crossChainMessageReceipt.setSuccessful(txInfo.isSuccess());
819-
crossChainMessageReceipt.setConfirmed(txInfo.isConfirmed());
820-
crossChainMessageReceipt.setTxhash(txInfo.getTxId());
821-
crossChainMessageReceipt.setErrorMsg(crossChainMessageReceipt.isSuccessful() ? "" : txInfo.getStatus().getStatus());
853+
crossChainMessageReceipt.setSuccessful(ObjectUtil.isNotNull(txInfo) && txInfo.isSuccess());
854+
crossChainMessageReceipt.setConfirmed(ObjectUtil.isNotNull(txInfo) && txInfo.isConfirmed());
855+
crossChainMessageReceipt.setTxhash(ObjectUtil.isNotNull(txInfo) ? txInfo.getTxId() : "");
856+
857+
String errorMsg = StrUtil.format(
858+
"{}: {}",
859+
baseMsg,
860+
crossChainMessageReceipt.isSuccessful() ? "SUCCESS"
861+
: ObjectUtil.isNull(txInfo) ? "FAILED TO CALL" : txInfo.getStatus().getStatus()
862+
);
863+
crossChainMessageReceipt.setErrorMsg(errorMsg);
822864

823-
getBBCLogger().info("relay auth message by tx {}", txInfo.getTxId());
865+
getBBCLogger().info("relay auth message by tx {} with msg {}", crossChainMessageReceipt.getTxhash(), errorMsg);
824866

825867
return crossChainMessageReceipt;
826868
}
827869

870+
private String[][] getRelayAuthMessageArgs(byte[] rawMessage) {
871+
return new String[][]{
872+
{
873+
this.bbcContext.getAuthMessageContract().getContractAddress(),
874+
AM_RECV_PKG_FROM_RELAYER_ACTION,
875+
String.format(
876+
AM_RECV_PKG_FROM_RELAYER_PARAMETER_FORMAT,
877+
this.config.getUserName(),
878+
generateRandomPrefix() + HexUtil.encodeHexStr(rawMessage)
879+
)
880+
},
881+
};
882+
}
883+
884+
private String[][] getRejectAuthMessageArgs(byte[] rawMessage) {
885+
return new String[][]{
886+
{
887+
this.bbcContext.getAuthMessageContract().getContractAddress(),
888+
AM_REJECT_AM_FROM_RELAYER_ACTION,
889+
String.format(
890+
AM_REJECT_AM_FROM_RELAYER_PARAMETER_FORMAT,
891+
this.config.getUserName(),
892+
generateRandomPrefix() + HexUtil.encodeHexStr(rawMessage)
893+
)
894+
},
895+
};
896+
}
897+
828898
private String generateRandomPrefix() {
829899
return StrUtil.sub(DigestUtil.sha256Hex(UUID.randomUUID().toString()), 0, 8);
830900
}
@@ -865,6 +935,18 @@ private SendTransactionResponse bbcInvokeContractsOnRpc(String[][] invokeParams)
865935
} catch (TransactionPrepareError e) {
866936
throw new RuntimeException("failed to prepare invoke contract action", e);
867937
} catch (TransactionSignAndBroadCastError e) {
938+
if (e.getCause().getCause().getCause() instanceof EosioJavaRpcProviderCallError) {
939+
EosioJavaRpcProviderCallError callError = (EosioJavaRpcProviderCallError) e.getCause().getCause().getCause();
940+
if (callError.getRpcResponseError().getCode().equals(BigInteger.valueOf(500))) {
941+
getBBCLogger().error(
942+
"failed to call biz contract: ( code: {}, msg: {}, detail: {} ) ",
943+
callError.getRpcResponseError().getCode(),
944+
callError.getRpcResponseError().getMessage(),
945+
JSON.toJSONString(callError.getRpcResponseError().getError())
946+
);
947+
return null;
948+
}
949+
}
868950
throw new RuntimeException("failed to sign and broadcast invoke contract action", e);
869951
}
870952
}
@@ -903,6 +985,12 @@ private EosTxInfo bbcGetTxInfoByTransactionHashOnRpc(String txHash) {
903985
(String) JSONPath.extract(response, "$.trx.receipt.status")
904986
)
905987
);
988+
989+
JSONArray actions = (JSONArray) JSONPath.extract(response, "$.trx.trx.actions");
990+
if (ObjectUtil.isNotNull(actions)) {
991+
txInfo.setActions(actions.toJavaList(EosTxAction.class));
992+
}
993+
906994
return txInfo;
907995
}
908996

pluginset/eos/offchain-plugin/src/main/java/com/alipay/antchain/bridge/plugins/eos/types/EosTxInfo.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.alipay.antchain.bridge.plugins.eos.types;
1818

19+
import java.util.List;
20+
21+
import cn.hutool.core.util.StrUtil;
1922
import com.alibaba.fastjson.annotation.JSONField;
2023
import lombok.AllArgsConstructor;
2124
import lombok.Getter;
@@ -37,11 +40,19 @@ public class EosTxInfo {
3740
@JSONField(name = "last_irreversible_block")
3841
private long irreversibleNum;
3942

43+
private List<EosTxAction> actions;
44+
4045
public boolean isSuccess() {
4146
return status == EosTransactionStatusEnum.EXECUTED;
4247
}
4348

4449
public boolean isConfirmed() {
4550
return irreversibleNum >= blockNum;
4651
}
52+
53+
public boolean containsAction(String account, String name) {
54+
return actions.stream().anyMatch(
55+
eosTxAction -> StrUtil.equals(eosTxAction.getAccount(), account) && StrUtil.equals(eosTxAction.getName(), name)
56+
);
57+
}
4758
}

pluginset/eos/offchain-plugin/src/test/java/com/alipay/antchain/bridge/plugins/eos/EosBBCServiceTest.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import com.alipay.antchain.bridge.plugins.eos.utils.Utils;
4646
import lombok.Getter;
4747
import lombok.Setter;
48+
import lombok.SneakyThrows;
4849
import okhttp3.RequestBody;
4950
import one.block.eosiojava.error.session.TransactionPrepareError;
5051
import one.block.eosiojava.error.session.TransactionSignAndBroadCastError;
@@ -67,7 +68,7 @@ public class EosBBCServiceTest {
6768
private static final String INVALID_URL = "127.0.0.1:9999";
6869

6970
// !!! replace to your test key
70-
private static final String EOS_DEFAULT_PRIVATE_KEY = "5JzGkEmpSQWb92DAS...hhY5aCWN4VZQc6uAxrQLAr";
71+
private static final String EOS_DEFAULT_PRIVATE_KEY = "5JzGkEmpSQWb92DASJiiBoNNfcbMmhhY5aCWN4VZQc6uAxrQLAr";
7172

7273
private static final String EOS_DEFAULT_USER_NAME = "relayer1";
7374

@@ -179,7 +180,7 @@ public void testReadCrossChainMessageReceipt() throws IOException, InterruptedEx
179180
relayAmPrepare();
180181

181182
CrossChainMessageReceipt crossChainMessageReceipt = eosBBCService.relayAuthMessage(
182-
getRawMsgFromRelayer(DigestUtil.sha256Hex(UUID.randomUUID().toString()), 0)
183+
getRawMsgFromRelayer(DigestUtil.sha256Hex(UUID.randomUUID().toString()), "awesome antchain-bridge", 0)
183184
);
184185
Assert.assertNotNull(crossChainMessageReceipt);
185186
Assert.assertTrue(crossChainMessageReceipt.isSuccessful());
@@ -198,7 +199,7 @@ public void testReadCrossChainMessageReceipt_sendUnordered() throws Exception {
198199
relayAmPrepare();
199200

200201
CrossChainMessageReceipt crossChainMessageReceipt = eosBBCService.relayAuthMessage(
201-
getRawMsgFromRelayer(DigestUtil.sha256Hex(UUID.randomUUID().toString()), -1)
202+
getRawMsgFromRelayer(DigestUtil.sha256Hex(UUID.randomUUID().toString()), "awesome antchain-bridge", -1)
202203
);
203204
Assert.assertNotNull(crossChainMessageReceipt);
204205
Assert.assertTrue(crossChainMessageReceipt.isSuccessful());
@@ -246,7 +247,7 @@ public void testRelayAuthMessage() throws Exception {
246247

247248
// relay am msg
248249
CrossChainMessageReceipt receipt = eosBBCService.relayAuthMessage(
249-
getRawMsgFromRelayer(DigestUtil.sha256Hex("senderID"), -1)
250+
getRawMsgFromRelayer(DigestUtil.sha256Hex("senderID"), "awesome antchain-bridge", -1)
250251
);
251252
Assert.assertTrue(receipt.isSuccessful());
252253

@@ -268,6 +269,24 @@ public void testRelayAuthMessage() throws Exception {
268269
);
269270
}
270271

272+
@Test
273+
public void testRelayFailedAuthMessage() throws Exception {
274+
relayAmPrepare();
275+
276+
// relay am msg
277+
CrossChainMessageReceipt receipt = eosBBCService.relayAuthMessage(
278+
getRawMsgFromRelayer(DigestUtil.sha256Hex(UUID.randomUUID().toString()), "12345678123456781234567812345678", 0)
279+
);
280+
Assert.assertTrue(receipt.isSuccessful());
281+
282+
Thread.sleep(WAIT_TIME);
283+
284+
receipt = eosBBCService.readCrossChainMessageReceipt(receipt.getTxhash());
285+
Assert.assertTrue(receipt.isSuccessful());
286+
Assert.assertTrue(receipt.isConfirmed());
287+
Assert.assertEquals("call biz failed but seq updated: SUCCESS", receipt.getErrorMsg());
288+
}
289+
271290
private SendTransactionResponse bbcInvokeContractsOnRpc(String[][] invokeParams) {
272291
List<Action> actionList = new ArrayList<>();
273292

@@ -449,7 +468,7 @@ private String generatePaddingZero(int nBytes) {
449468
return padding.toString();
450469
}
451470

452-
private byte[] getRawMsgFromRelayer(String senderHex, int seq) throws IOException {
471+
private byte[] getRawMsgFromRelayer(String senderHex, String msg, int seq) throws IOException {
453472
byte[] raw = Utils.convertEosBase32NameToNum(EOS_RECEIVER_CONTRACT_NAME).toByteArray();
454473

455474
ISDPMessage sdpMessage = SDPMessageFactory.createSDPMessage(
@@ -459,7 +478,7 @@ private byte[] getRawMsgFromRelayer(String senderHex, int seq) throws IOExceptio
459478
String.format(generatePaddingZero(32 - raw.length) + "%s", HexUtil.encodeHexStr(raw))
460479
),
461480
seq,
462-
"awesome antchain-bridge".getBytes()
481+
msg.getBytes()
463482
);
464483

465484
IAuthMessage am = AuthMessageFactory.createAuthMessage(

0 commit comments

Comments
 (0)