Skip to content

Commit 9c2acc4

Browse files
add ContractScenario016
1 parent 1a75739 commit 9c2acc4

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package stest.tron.wallet.contract.scenario;
2+
3+
import static org.tron.protos.Protocol.Transaction.Result.contractResult.BAD_JUMP_DESTINATION_VALUE;
4+
import static org.tron.protos.Protocol.Transaction.Result.contractResult.OUT_OF_ENERGY_VALUE;
5+
6+
import io.grpc.ManagedChannel;
7+
import io.grpc.ManagedChannelBuilder;
8+
import java.util.HashMap;
9+
import java.util.Optional;
10+
import java.util.concurrent.TimeUnit;
11+
import lombok.extern.slf4j.Slf4j;
12+
import org.junit.Assert;
13+
import org.testng.annotations.AfterClass;
14+
import org.testng.annotations.BeforeClass;
15+
import org.testng.annotations.BeforeSuite;
16+
import org.testng.annotations.Test;
17+
import org.tron.api.WalletGrpc;
18+
import org.tron.api.WalletSolidityGrpc;
19+
import org.tron.common.crypto.ECKey;
20+
import org.tron.common.utils.ByteArray;
21+
import org.tron.common.utils.Utils;
22+
import org.tron.core.Wallet;
23+
import org.tron.protos.Protocol.SmartContract;
24+
import org.tron.protos.Protocol.Transaction;
25+
import org.tron.protos.Protocol.Transaction.Result.contractResult;
26+
import org.tron.protos.Protocol.TransactionInfo;
27+
import stest.tron.wallet.common.client.Configuration;
28+
import stest.tron.wallet.common.client.Parameter.CommonConstant;
29+
import stest.tron.wallet.common.client.utils.PublicMethed;
30+
31+
@Slf4j
32+
public class ContractScenario016 {
33+
private final String testNetAccountKey = Configuration.getByPath("testng.conf")
34+
.getString("foundationAccount.key2");
35+
private final byte[] testNetAccountAddress = PublicMethed.getFinalAddress(testNetAccountKey);
36+
private Long maxFeeLimit = Configuration.getByPath("testng.conf")
37+
.getLong("defaultParameter.maxFeeLimit");
38+
private ManagedChannel channelSolidity = null;
39+
40+
private ManagedChannel channelFull = null;
41+
private WalletGrpc.WalletBlockingStub blockingStubFull = null;
42+
43+
private ManagedChannel channelFull1 = null;
44+
private WalletGrpc.WalletBlockingStub blockingStubFull1 = null;
45+
46+
47+
private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null;
48+
49+
private String fullnode = Configuration.getByPath("testng.conf")
50+
.getStringList("fullnode.ip.list").get(1);
51+
private String fullnode1 = Configuration.getByPath("testng.conf")
52+
.getStringList("fullnode.ip.list").get(0);
53+
private String compilerVersion = Configuration.getByPath("testng.conf")
54+
.getString("defaultParameter.solidityCompilerVersion");
55+
56+
57+
byte[] contractAddress = null;
58+
59+
ECKey ecKey1 = new ECKey(Utils.getRandom());
60+
byte[] grammarAddress = ecKey1.getAddress();
61+
String testKeyForGrammarAddress = ByteArray.toHexString(ecKey1.getPrivKeyBytes());
62+
63+
64+
@BeforeSuite
65+
public void beforeSuite() {
66+
Wallet wallet = new Wallet();
67+
Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET);
68+
}
69+
70+
/**
71+
* constructor.
72+
*/
73+
74+
@BeforeClass(enabled = true)
75+
public void beforeClass() {
76+
PublicMethed.printAddress(testKeyForGrammarAddress);
77+
channelFull = ManagedChannelBuilder.forTarget(fullnode)
78+
.usePlaintext(true)
79+
.build();
80+
blockingStubFull = WalletGrpc.newBlockingStub(channelFull);
81+
channelFull1 = ManagedChannelBuilder.forTarget(fullnode1)
82+
.usePlaintext(true)
83+
.build();
84+
blockingStubFull1 = WalletGrpc.newBlockingStub(channelFull1);
85+
}
86+
87+
@Test(enabled = true, description = "ContractResult is BAD_JUMP_DESTINATION")
88+
public void test1Grammar001() {
89+
Assert.assertTrue(PublicMethed
90+
.sendcoin(grammarAddress, 100000000000L, testNetAccountAddress, testNetAccountKey,
91+
blockingStubFull));
92+
PublicMethed.waitProduceNextBlock(blockingStubFull);
93+
String contractName = "Test";
94+
95+
String code = "608060405234801561001057600080fd5b50d3801561001d57600080fd5b50d2801561002a57600"
96+
+ "080fd5b5061011f8061003a6000396000f30060806040526004361060485763ffffffff7c01000000000000"
97+
+ "000000000000000000000000000000000000000000006000350416634ef5a0088114604d5780639093b95b1"
98+
+ "4608c575b600080fd5b348015605857600080fd5b50d38015606457600080fd5b50d28015607057600080fd"
99+
+ "5b50607a60043560b8565b60408051918252519081900360200190f35b348015609757600080fd5b50d3801"
100+
+ "560a357600080fd5b50d2801560af57600080fd5b5060b660ee565b005b6000606082604051908082528060"
101+
+ "20026020018201604052801560e5578160200160208202803883390190505b50905050919050565b6001805"
102+
+ "600a165627a7a7230582092ba162087e13f41c6d6c00ba493edc5a5a6250a3840ece5f99aa38b66366a7000"
103+
+ "29";
104+
String abi = "[{\"constant\":false,\"inputs\":[{\"name\":\"x\",\"type\":\"uint256\"}],\"name\""
105+
+ ":\"testOutOfMem\",\"outputs\":[{\"name\":\"r\",\"type\":\"bytes32\"}],\"payable\":false"
106+
+ ",\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs"
107+
+ "\":[],\"name\":\"testBadJumpDestination\",\"outputs\":[],\"payable\":false,\"stateMutab"
108+
+ "ility\":\"nonpayable\",\"type\":\"function\"}]";
109+
110+
byte[] contractAddress = PublicMethed.deployContract(contractName, abi, code,
111+
"", maxFeeLimit,
112+
0L, 100, null, testKeyForGrammarAddress, grammarAddress, blockingStubFull);
113+
114+
PublicMethed.waitProduceNextBlock(blockingStubFull);
115+
SmartContract smartContract = PublicMethed.getContract(contractAddress, blockingStubFull);
116+
org.testng.Assert.assertTrue(smartContract.getAbi().toString() != null);
117+
String txid = null;
118+
Optional<TransactionInfo> infoById = null;
119+
txid = PublicMethed.triggerContract(contractAddress,
120+
"testBadJumpDestination()", "#", false,
121+
0, maxFeeLimit, grammarAddress, testKeyForGrammarAddress, blockingStubFull);
122+
PublicMethed.waitProduceNextBlock(blockingStubFull);
123+
infoById = PublicMethed.getTransactionInfoById(txid, blockingStubFull);
124+
logger.info("Txid is " + txid);
125+
logger.info("Trigger energytotal is " + infoById.get().getReceipt().getEnergyUsageTotal());
126+
127+
Optional<Transaction> byId = PublicMethed.getTransactionById(txid, blockingStubFull);
128+
logger.info("getRet:" + byId.get().getRet(0));
129+
logger.info("getNumber:" + byId.get().getRet(0).getContractRet().getNumber());
130+
logger.info("getContractRetValue:" + byId.get().getRet(0).getContractRetValue());
131+
logger.info("getContractRet:" + byId.get().getRet(0).getContractRet());
132+
logger.info("ById:" + byId);
133+
134+
logger.info("infoById:" + infoById);
135+
136+
Assert.assertEquals(byId.get().getRet(0).getContractRetValue(), BAD_JUMP_DESTINATION_VALUE);
137+
Assert.assertEquals(byId.get().getRet(0).getContractRet(), contractResult.BAD_JUMP_DESTINATION);
138+
139+
Assert
140+
.assertEquals(ByteArray.toHexString(infoById.get().getContractResult(0).toByteArray()), "");
141+
Assert
142+
.assertEquals(contractResult.BAD_JUMP_DESTINATION, infoById.get().getReceipt().getResult());
143+
144+
Assert.assertEquals(byId.get().getRet(0).getRet().getNumber(), 0);
145+
Assert.assertEquals(byId.get().getRet(0).getRetValue(), 0);
146+
147+
148+
}
149+
150+
151+
@Test(enabled = true, description = "ContractResult is OUT_OF_ENERGY")
152+
public void test2Grammar002() {
153+
154+
String filePath = "src/test/resources/soliditycode/contractUnknownException.sol";
155+
String contractName = "testC";
156+
HashMap retMap = PublicMethed.getBycodeAbi(filePath, contractName);
157+
String code = retMap.get("byteCode").toString();
158+
String abi = retMap.get("abI").toString();
159+
String txid = PublicMethed
160+
.deployContractAndGetTransactionInfoById(contractName, abi, code, "", maxFeeLimit,
161+
20L, 100, null, testKeyForGrammarAddress,
162+
grammarAddress, blockingStubFull);
163+
Optional<TransactionInfo> infoById = null;
164+
PublicMethed.waitProduceNextBlock(blockingStubFull);
165+
infoById = PublicMethed.getTransactionInfoById(txid, blockingStubFull);
166+
logger.info("Txid is " + txid);
167+
logger.info("Trigger energytotal is " + infoById.get().getReceipt().getEnergyUsageTotal());
168+
169+
Optional<Transaction> byId = PublicMethed.getTransactionById(txid, blockingStubFull);
170+
logger.info("getRet:" + byId.get().getRet(0));
171+
logger.info("getNumber:" + byId.get().getRet(0).getContractRet().getNumber());
172+
logger.info("getContractRetValue:" + byId.get().getRet(0).getContractRetValue());
173+
logger.info("getContractRet:" + byId.get().getRet(0).getContractRet());
174+
logger.info("ById:" + byId);
175+
176+
logger.info("infoById:" + infoById);
177+
178+
Assert.assertEquals(byId.get().getRet(0).getContractRetValue(), OUT_OF_ENERGY_VALUE);
179+
Assert.assertEquals(byId.get().getRet(0).getContractRet(), contractResult.OUT_OF_ENERGY);
180+
181+
Assert
182+
.assertEquals(ByteArray.toHexString(infoById.get().getContractResult(0).toByteArray()), "");
183+
Assert
184+
.assertEquals(contractResult.OUT_OF_ENERGY, infoById.get().getReceipt().getResult());
185+
186+
Assert.assertEquals(byId.get().getRet(0).getRet().getNumber(), 0);
187+
Assert.assertEquals(byId.get().getRet(0).getRetValue(), 0);
188+
189+
}
190+
191+
192+
193+
/**
194+
* constructor.
195+
*/
196+
@AfterClass
197+
public void shutdown() throws InterruptedException {
198+
if (channelFull != null) {
199+
channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS);
200+
}
201+
if (channelFull1 != null) {
202+
channelFull1.shutdown().awaitTermination(5, TimeUnit.SECONDS);
203+
}
204+
}
205+
206+
}

0 commit comments

Comments
 (0)