|
| 1 | +package stest.tron.wallet.contract.scenario; |
| 2 | + |
| 3 | +import io.grpc.ManagedChannel; |
| 4 | +import io.grpc.ManagedChannelBuilder; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.spongycastle.util.encoders.Hex; |
| 10 | +import org.testng.annotations.AfterClass; |
| 11 | +import org.testng.annotations.BeforeClass; |
| 12 | +import org.testng.annotations.BeforeSuite; |
| 13 | +import org.testng.annotations.Test; |
| 14 | +import org.tron.api.GrpcAPI.AccountResourceMessage; |
| 15 | +import org.tron.api.GrpcAPI.TransactionExtention; |
| 16 | +import org.tron.api.WalletGrpc; |
| 17 | +import org.tron.api.WalletSolidityGrpc; |
| 18 | +import org.tron.common.crypto.ECKey; |
| 19 | +import org.tron.common.utils.ByteArray; |
| 20 | +import org.tron.common.utils.Utils; |
| 21 | +import org.tron.core.Wallet; |
| 22 | +import org.tron.protos.Protocol.Account; |
| 23 | +import org.tron.protos.Protocol.Transaction; |
| 24 | +import stest.tron.wallet.common.client.Configuration; |
| 25 | +import stest.tron.wallet.common.client.Parameter.CommonConstant; |
| 26 | +import stest.tron.wallet.common.client.utils.PublicMethed; |
| 27 | + |
| 28 | +@Slf4j |
| 29 | +public class ContractScenario015 { |
| 30 | + |
| 31 | + private final String testNetAccountKey = Configuration.getByPath("testng.conf") |
| 32 | + .getString("foundationAccount.key2"); |
| 33 | + private final byte[] testNetAccountAddress = PublicMethed.getFinalAddress(testNetAccountKey); |
| 34 | + private Long maxFeeLimit = Configuration.getByPath("testng.conf") |
| 35 | + .getLong("defaultParameter.maxFeeLimit"); |
| 36 | + private ManagedChannel channelSolidity = null; |
| 37 | + |
| 38 | + private ManagedChannel channelFull = null; |
| 39 | + private WalletGrpc.WalletBlockingStub blockingStubFull = null; |
| 40 | + |
| 41 | + private ManagedChannel channelFull1 = null; |
| 42 | + private WalletGrpc.WalletBlockingStub blockingStubFull1 = null; |
| 43 | + |
| 44 | + |
| 45 | + private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null; |
| 46 | + |
| 47 | + private String fullnode = Configuration.getByPath("testng.conf") |
| 48 | + .getStringList("fullnode.ip.list").get(0); |
| 49 | + private String fullnode1 = Configuration.getByPath("testng.conf") |
| 50 | + .getStringList("fullnode.ip.list").get(1); |
| 51 | + |
| 52 | + private String soliditynode = Configuration.getByPath("testng.conf") |
| 53 | + .getStringList("solidityNode.ip.list").get(0); |
| 54 | + |
| 55 | + byte[] contractAddress = null; |
| 56 | + |
| 57 | + ECKey ecKey1 = new ECKey(Utils.getRandom()); |
| 58 | + byte[] contractExcAddress = ecKey1.getAddress(); |
| 59 | + String contractExcKey = ByteArray.toHexString(ecKey1.getPrivKeyBytes()); |
| 60 | + |
| 61 | + |
| 62 | + @BeforeSuite |
| 63 | + public void beforeSuite() { |
| 64 | + Wallet wallet = new Wallet(); |
| 65 | + Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * constructor. |
| 70 | + */ |
| 71 | + |
| 72 | + @BeforeClass(enabled = true) |
| 73 | + public void beforeClass() { |
| 74 | + PublicMethed.printAddress(contractExcKey); |
| 75 | + channelFull = ManagedChannelBuilder.forTarget(fullnode) |
| 76 | + .usePlaintext(true) |
| 77 | + .build(); |
| 78 | + blockingStubFull = WalletGrpc.newBlockingStub(channelFull); |
| 79 | + channelFull1 = ManagedChannelBuilder.forTarget(fullnode1) |
| 80 | + .usePlaintext(true) |
| 81 | + .build(); |
| 82 | + blockingStubFull1 = WalletGrpc.newBlockingStub(channelFull1); |
| 83 | + |
| 84 | + channelSolidity = ManagedChannelBuilder.forTarget(soliditynode) |
| 85 | + .usePlaintext(true) |
| 86 | + .build(); |
| 87 | + blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelSolidity); |
| 88 | + } |
| 89 | + |
| 90 | + @Test(enabled = true, description = "TriggerConstantContract a constant function ") |
| 91 | + public void testTriggerConstantContract() { |
| 92 | + Assert.assertTrue(PublicMethed |
| 93 | + .sendcoin(contractExcAddress, 10000000000L, testNetAccountAddress, testNetAccountKey, |
| 94 | + blockingStubFull)); |
| 95 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 96 | + String filePath = "src/test/resources/soliditycode/ClearAbi001.sol"; |
| 97 | + String contractName = "testConstantContract"; |
| 98 | + HashMap retMap = PublicMethed.getBycodeAbi(filePath, contractName); |
| 99 | + String code = retMap.get("byteCode").toString(); |
| 100 | + String abi = retMap.get("abI").toString(); |
| 101 | + |
| 102 | + contractAddress = PublicMethed.deployContract(contractName, abi, code, "", maxFeeLimit, |
| 103 | + 0L, 100, null, contractExcKey, |
| 104 | + contractExcAddress, blockingStubFull); |
| 105 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 106 | + Account info; |
| 107 | + |
| 108 | + AccountResourceMessage resourceInfo = PublicMethed.getAccountResource(contractExcAddress, |
| 109 | + blockingStubFull); |
| 110 | + info = PublicMethed.queryAccount(contractExcKey, blockingStubFull); |
| 111 | + Long beforeBalance = info.getBalance(); |
| 112 | + Long beforeEnergyUsed = resourceInfo.getEnergyUsed(); |
| 113 | + Long beforeNetUsed = resourceInfo.getNetUsed(); |
| 114 | + Long beforeFreeNetUsed = resourceInfo.getFreeNetUsed(); |
| 115 | + logger.info("beforeBalance:" + beforeBalance); |
| 116 | + logger.info("beforeEnergyUsed:" + beforeEnergyUsed); |
| 117 | + logger.info("beforeNetUsed:" + beforeNetUsed); |
| 118 | + logger.info("beforeFreeNetUsed:" + beforeFreeNetUsed); |
| 119 | + TransactionExtention transactionExtention = PublicMethed |
| 120 | + .triggerConstantContractForExtention(contractAddress, |
| 121 | + "testPayable()", "#", false, |
| 122 | + 0, 0, "0", 0, contractExcAddress, contractExcKey, blockingStubFull); |
| 123 | + Transaction transaction = transactionExtention.getTransaction(); |
| 124 | + |
| 125 | + byte[] result = transactionExtention.getConstantResult(0).toByteArray(); |
| 126 | + logger.info("message:" + transaction.getRet(0).getRet()); |
| 127 | + logger.info(":" + ByteArray |
| 128 | + .toStr(transactionExtention.getResult().getMessage().toByteArray())); |
| 129 | + logger.info("Result:" + Hex.toHexString(result)); |
| 130 | + logger.info("getCode"+transactionExtention.getResult().getCode().getNumber()); |
| 131 | + Assert.assertEquals("SUCESS",transaction.getRet(0).getRet().toString()); |
| 132 | + Assert.assertEquals(1, ByteArray.toLong(ByteArray |
| 133 | + .fromHexString(Hex |
| 134 | + .toHexString(result)))); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + /** |
| 139 | + * constructor. |
| 140 | + */ |
| 141 | + @AfterClass |
| 142 | + public void shutdown() throws InterruptedException { |
| 143 | + if (channelFull != null) { |
| 144 | + channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 145 | + } |
| 146 | + if (channelFull1 != null) { |
| 147 | + channelFull1.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 148 | + } |
| 149 | + if (channelSolidity != null) { |
| 150 | + channelSolidity.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | +} |
0 commit comments