|
| 1 | +package stest.tron.wallet.contract.scenario; |
| 2 | + |
| 3 | +import static org.tron.protos.Protocol.Transaction.Result.contractResult.SUCCESS_VALUE; |
| 4 | + |
| 5 | +import io.grpc.ManagedChannel; |
| 6 | +import io.grpc.ManagedChannelBuilder; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | +import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.testng.annotations.AfterClass; |
| 13 | +import org.testng.annotations.BeforeClass; |
| 14 | +import org.testng.annotations.BeforeSuite; |
| 15 | +import org.testng.annotations.Test; |
| 16 | +import org.tron.api.GrpcAPI.AccountResourceMessage; |
| 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.Account; |
| 24 | +import org.tron.protos.Protocol.SmartContract; |
| 25 | +import org.tron.protos.Protocol.Transaction; |
| 26 | +import org.tron.protos.Protocol.Transaction.Result.contractResult; |
| 27 | +import org.tron.protos.Protocol.TransactionInfo; |
| 28 | +import stest.tron.wallet.common.client.Configuration; |
| 29 | +import stest.tron.wallet.common.client.Parameter.CommonConstant; |
| 30 | +import stest.tron.wallet.common.client.utils.PublicMethed; |
| 31 | + |
| 32 | +@Slf4j |
| 33 | +public class ContractScenario014 { |
| 34 | + |
| 35 | + private final String testNetAccountKey = Configuration.getByPath("testng.conf") |
| 36 | + .getString("foundationAccount.key2"); |
| 37 | + private final byte[] testNetAccountAddress = PublicMethed.getFinalAddress(testNetAccountKey); |
| 38 | + private Long maxFeeLimit = Configuration.getByPath("testng.conf") |
| 39 | + .getLong("defaultParameter.maxFeeLimit"); |
| 40 | + private ManagedChannel channelSolidity = null; |
| 41 | + |
| 42 | + private ManagedChannel channelFull = null; |
| 43 | + private WalletGrpc.WalletBlockingStub blockingStubFull = null; |
| 44 | + |
| 45 | + private ManagedChannel channelFull1 = null; |
| 46 | + private WalletGrpc.WalletBlockingStub blockingStubFull1 = null; |
| 47 | + |
| 48 | + |
| 49 | + private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null; |
| 50 | + |
| 51 | + private String fullnode = Configuration.getByPath("testng.conf") |
| 52 | + .getStringList("fullnode.ip.list").get(0); |
| 53 | + private String fullnode1 = Configuration.getByPath("testng.conf") |
| 54 | + .getStringList("fullnode.ip.list").get(1); |
| 55 | + |
| 56 | + private String soliditynode = Configuration.getByPath("testng.conf") |
| 57 | + .getStringList("solidityNode.ip.list").get(0); |
| 58 | + |
| 59 | + byte[] contractAddress = null; |
| 60 | + |
| 61 | + ECKey ecKey1 = new ECKey(Utils.getRandom()); |
| 62 | + byte[] contractExcAddress = ecKey1.getAddress(); |
| 63 | + String contractExcKey = ByteArray.toHexString(ecKey1.getPrivKeyBytes()); |
| 64 | + |
| 65 | + |
| 66 | + @BeforeSuite |
| 67 | + public void beforeSuite() { |
| 68 | + Wallet wallet = new Wallet(); |
| 69 | + Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * constructor. |
| 74 | + */ |
| 75 | + |
| 76 | + @BeforeClass(enabled = true) |
| 77 | + public void beforeClass() { |
| 78 | + PublicMethed.printAddress(contractExcKey); |
| 79 | + channelFull = ManagedChannelBuilder.forTarget(fullnode) |
| 80 | + .usePlaintext(true) |
| 81 | + .build(); |
| 82 | + blockingStubFull = WalletGrpc.newBlockingStub(channelFull); |
| 83 | + channelFull1 = ManagedChannelBuilder.forTarget(fullnode1) |
| 84 | + .usePlaintext(true) |
| 85 | + .build(); |
| 86 | + blockingStubFull1 = WalletGrpc.newBlockingStub(channelFull1); |
| 87 | + |
| 88 | + channelSolidity = ManagedChannelBuilder.forTarget(soliditynode) |
| 89 | + .usePlaintext(true) |
| 90 | + .build(); |
| 91 | + blockingStubSolidity = WalletSolidityGrpc.newBlockingStub(channelSolidity); |
| 92 | + } |
| 93 | + |
| 94 | + @Test(enabled = true, description = "Clear a contract with ABI created by itself") |
| 95 | + public void testClearAbi() { |
| 96 | + Assert.assertTrue(PublicMethed |
| 97 | + .sendcoin(contractExcAddress, 10000000000L, testNetAccountAddress, testNetAccountKey, |
| 98 | + blockingStubFull)); |
| 99 | + |
| 100 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 101 | + String filePath = "src/test/resources/soliditycode/ClearAbi001.sol"; |
| 102 | + String contractName = "testConstantContract"; |
| 103 | + HashMap retMap = PublicMethed.getBycodeAbi(filePath, contractName); |
| 104 | + String code = retMap.get("byteCode").toString(); |
| 105 | + String abi = retMap.get("abI").toString(); |
| 106 | + |
| 107 | + contractAddress = PublicMethed.deployContract(contractName, abi, code, "", maxFeeLimit, |
| 108 | + 0L, 100, null, contractExcKey, |
| 109 | + contractExcAddress, blockingStubFull); |
| 110 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 111 | + SmartContract smartContract = PublicMethed.getContract(contractAddress, blockingStubFull); |
| 112 | + Assert.assertFalse(smartContract.getAbi().toString().isEmpty()); |
| 113 | + Assert.assertTrue(smartContract.getName().equalsIgnoreCase(contractName)); |
| 114 | + Assert.assertFalse(smartContract.getBytecode().toString().isEmpty()); |
| 115 | + Account info; |
| 116 | + |
| 117 | + AccountResourceMessage resourceInfo = PublicMethed.getAccountResource(contractExcAddress, |
| 118 | + blockingStubFull); |
| 119 | + info = PublicMethed.queryAccount(contractExcKey, blockingStubFull); |
| 120 | + Long beforeBalance = info.getBalance(); |
| 121 | + Long beforeEnergyUsed = resourceInfo.getEnergyUsed(); |
| 122 | + Long beforeNetUsed = resourceInfo.getNetUsed(); |
| 123 | + Long beforeFreeNetUsed = resourceInfo.getFreeNetUsed(); |
| 124 | + logger.info("beforeBalance:" + beforeBalance); |
| 125 | + logger.info("beforeEnergyUsed:" + beforeEnergyUsed); |
| 126 | + logger.info("beforeNetUsed:" + beforeNetUsed); |
| 127 | + logger.info("beforeFreeNetUsed:" + beforeFreeNetUsed); |
| 128 | + |
| 129 | + String txid = PublicMethed |
| 130 | + .clearContractAbi(contractAddress, contractExcAddress, contractExcKey, |
| 131 | + blockingStubFull); |
| 132 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 133 | + Optional<TransactionInfo> infoById = null; |
| 134 | + infoById = PublicMethed.getTransactionInfoById(txid, blockingStubFull); |
| 135 | + Assert.assertEquals(0,infoById.get().getResultValue()); |
| 136 | + Assert.assertEquals("", |
| 137 | + ByteArray.toHexString(infoById.get().getResMessage().toByteArray())); |
| 138 | + |
| 139 | + Optional<Transaction> byId = PublicMethed.getTransactionById(txid, blockingStubFull); |
| 140 | + Assert.assertEquals(byId.get().getRet(0).getContractRet().getNumber(), |
| 141 | + SUCCESS_VALUE); |
| 142 | + Assert.assertEquals(byId.get().getRet(0).getContractRetValue(), SUCCESS_VALUE); |
| 143 | + Assert.assertEquals(byId.get().getRet(0).getContractRet(), contractResult.SUCCESS); |
| 144 | + |
| 145 | + smartContract = PublicMethed.getContract(contractAddress, blockingStubFull); |
| 146 | + Assert.assertTrue(smartContract.getAbi().toString().isEmpty()); |
| 147 | + Assert.assertTrue(smartContract.getName().equalsIgnoreCase(contractName)); |
| 148 | + Assert.assertFalse(smartContract.getBytecode().toString().isEmpty()); |
| 149 | + |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + /** |
| 155 | + * constructor. |
| 156 | + */ |
| 157 | + @AfterClass |
| 158 | + public void shutdown() throws InterruptedException { |
| 159 | + if (channelFull != null) { |
| 160 | + channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 161 | + } |
| 162 | + if (channelFull1 != null) { |
| 163 | + channelFull1.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 164 | + } |
| 165 | + if (channelSolidity != null) { |
| 166 | + channelSolidity.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | +} |
0 commit comments