|
| 1 | +package stest.tron.wallet.dailybuild.tvmnewcommand.newGrammar; |
| 2 | + |
| 3 | +import io.grpc.ManagedChannel; |
| 4 | +import io.grpc.ManagedChannelBuilder; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.Optional; |
| 7 | +import java.util.concurrent.TimeUnit; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.junit.Assert; |
| 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.WalletGrpc; |
| 16 | +import org.tron.common.crypto.ECKey; |
| 17 | +import org.tron.common.utils.ByteArray; |
| 18 | +import org.tron.common.utils.Utils; |
| 19 | +import org.tron.core.Wallet; |
| 20 | +import org.tron.protos.Protocol; |
| 21 | +import org.tron.protos.Protocol.TransactionInfo; |
| 22 | +import org.tron.protos.contract.SmartContractOuterClass.SmartContract; |
| 23 | +import stest.tron.wallet.common.client.Configuration; |
| 24 | +import stest.tron.wallet.common.client.Parameter.CommonConstant; |
| 25 | +import stest.tron.wallet.common.client.utils.PublicMethed; |
| 26 | + |
| 27 | +@Slf4j |
| 28 | +public class AssignToExternalTest { |
| 29 | + |
| 30 | + private final String testKey002 = Configuration.getByPath("testng.conf") |
| 31 | + .getString("foundationAccount.key2"); |
| 32 | + private final byte[] fromAddress = PublicMethed.getFinalAddress(testKey002); |
| 33 | + |
| 34 | + private ManagedChannel channelFull = null; |
| 35 | + private WalletGrpc.WalletBlockingStub blockingStubFull = null; |
| 36 | + private String fullnode = Configuration.getByPath("testng.conf") |
| 37 | + .getStringList("fullnode.ip.list").get(1); |
| 38 | + private long maxFeeLimit = Configuration.getByPath("testng.conf") |
| 39 | + .getLong("defaultParameter.maxFeeLimit"); |
| 40 | + |
| 41 | + private byte[] contractAddress = null; |
| 42 | + |
| 43 | + private ECKey ecKey1 = new ECKey(Utils.getRandom()); |
| 44 | + private byte[] dev001Address = ecKey1.getAddress(); |
| 45 | + private String dev001Key = ByteArray.toHexString(ecKey1.getPrivKeyBytes()); |
| 46 | + |
| 47 | + @BeforeSuite |
| 48 | + public void beforeSuite() { |
| 49 | + Wallet wallet = new Wallet(); |
| 50 | + Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * constructor. |
| 55 | + */ |
| 56 | + @BeforeClass(enabled = true) |
| 57 | + public void beforeClass() { |
| 58 | + |
| 59 | + channelFull = ManagedChannelBuilder.forTarget(fullnode) |
| 60 | + .usePlaintext(true) |
| 61 | + .build(); |
| 62 | + blockingStubFull = WalletGrpc.newBlockingStub(channelFull); |
| 63 | + |
| 64 | + PublicMethed.printAddress(dev001Key); |
| 65 | + } |
| 66 | + |
| 67 | + @Test(enabled = true, description = "Deploy contract") |
| 68 | + public void test01DeployContract() { |
| 69 | + Assert.assertTrue(PublicMethed.sendcoin(dev001Address, 1000_000_000L, fromAddress, |
| 70 | + testKey002, blockingStubFull)); |
| 71 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 72 | + |
| 73 | + //before deploy, check account resource |
| 74 | + AccountResourceMessage accountResource = PublicMethed.getAccountResource(dev001Address, |
| 75 | + blockingStubFull); |
| 76 | + Protocol.Account info = PublicMethed.queryAccount(dev001Key, blockingStubFull); |
| 77 | + Long beforeBalance = info.getBalance(); |
| 78 | + Long beforeEnergyUsed = accountResource.getEnergyUsed(); |
| 79 | + Long beforeNetUsed = accountResource.getNetUsed(); |
| 80 | + Long beforeFreeNetUsed = accountResource.getFreeNetUsed(); |
| 81 | + logger.info("beforeBalance:" + beforeBalance); |
| 82 | + logger.info("beforeEnergyUsed:" + beforeEnergyUsed); |
| 83 | + logger.info("beforeNetUsed:" + beforeNetUsed); |
| 84 | + logger.info("beforeFreeNetUsed:" + beforeFreeNetUsed); |
| 85 | + |
| 86 | + String filePath = "./src/test/resources/soliditycode/AssignToExternal.sol"; |
| 87 | + String contractName = "AssignToExternal"; |
| 88 | + HashMap retMap = PublicMethed.getBycodeAbi(filePath, contractName); |
| 89 | + String code = retMap.get("byteCode").toString(); |
| 90 | + String abi = retMap.get("abI").toString(); |
| 91 | + |
| 92 | + final String txid = PublicMethed |
| 93 | + .deployContractAndGetTransactionInfoById(contractName, abi, code, "", |
| 94 | + maxFeeLimit, 0L, 0, 10000, |
| 95 | + "0", 0, null, dev001Key, |
| 96 | + dev001Address, blockingStubFull); |
| 97 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 98 | + |
| 99 | + Optional<TransactionInfo> infoById = null; |
| 100 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 101 | + infoById = PublicMethed.getTransactionInfoById(txid, blockingStubFull); |
| 102 | + if (infoById.get().getResultValue() != 0) { |
| 103 | + Assert.fail("deploy transaction failed with message: " + infoById.get().getResMessage()); |
| 104 | + } |
| 105 | + |
| 106 | + TransactionInfo transactionInfo = infoById.get(); |
| 107 | + logger.info("EnergyUsageTotal: " + transactionInfo.getReceipt().getEnergyUsageTotal()); |
| 108 | + logger.info("NetUsage: " + transactionInfo.getReceipt().getNetUsage()); |
| 109 | + |
| 110 | + contractAddress = infoById.get().getContractAddress().toByteArray(); |
| 111 | + SmartContract smartContract = PublicMethed.getContract(contractAddress, |
| 112 | + blockingStubFull); |
| 113 | + Assert.assertNotNull(smartContract.getAbi()); |
| 114 | + |
| 115 | + Long fee = infoById.get().getFee(); |
| 116 | + Long netUsed = infoById.get().getReceipt().getNetUsage(); |
| 117 | + Long energyUsed = infoById.get().getReceipt().getEnergyUsage(); |
| 118 | + Long netFee = infoById.get().getReceipt().getNetFee(); |
| 119 | + long energyUsageTotal = infoById.get().getReceipt().getEnergyUsageTotal(); |
| 120 | + logger.info("fee:" + fee); |
| 121 | + logger.info("netUsed:" + netUsed); |
| 122 | + logger.info("energyUsed:" + energyUsed); |
| 123 | + logger.info("netFee:" + netFee); |
| 124 | + logger.info("energyUsageTotal:" + energyUsageTotal); |
| 125 | + |
| 126 | + Protocol.Account infoafter = PublicMethed.queryAccount(dev001Key, blockingStubFull); |
| 127 | + AccountResourceMessage resourceInfoafter = PublicMethed |
| 128 | + .getAccountResource(dev001Address, |
| 129 | + blockingStubFull); |
| 130 | + Long afterBalance = infoafter.getBalance(); |
| 131 | + Long afterEnergyUsed = resourceInfoafter.getEnergyUsed(); |
| 132 | + Long afterNetUsed = resourceInfoafter.getNetUsed(); |
| 133 | + Long afterFreeNetUsed = resourceInfoafter.getFreeNetUsed(); |
| 134 | + logger.info("afterBalance:" + afterBalance); |
| 135 | + logger.info("afterEnergyUsed:" + afterEnergyUsed); |
| 136 | + logger.info("afterNetUsed:" + afterNetUsed); |
| 137 | + logger.info("afterFreeNetUsed:" + afterFreeNetUsed); |
| 138 | + |
| 139 | + Assert.assertTrue(afterBalance + fee == beforeBalance); |
| 140 | + Assert.assertTrue(beforeEnergyUsed + energyUsed >= afterEnergyUsed); |
| 141 | + Assert.assertTrue(beforeFreeNetUsed + netUsed >= afterFreeNetUsed); |
| 142 | + Assert.assertTrue(beforeNetUsed + netUsed >= afterNetUsed); |
| 143 | + } |
| 144 | + |
| 145 | + @Test(enabled = true, description = "Trigger contract with ") |
| 146 | + public void test02TriggerContract() { |
| 147 | + String methodStr = "f(uint256)"; |
| 148 | + String argStr = "2"; |
| 149 | + String txid = PublicMethed.triggerContract(contractAddress, methodStr, argStr, false, |
| 150 | + 0, maxFeeLimit, dev001Address, dev001Key, blockingStubFull); |
| 151 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 152 | + Optional<TransactionInfo> infoById = PublicMethed |
| 153 | + .getTransactionInfoById(txid, blockingStubFull); |
| 154 | + if (infoById.get().getResultValue() != 0) { |
| 155 | + Assert.fail("trigger contract failed with message: " + infoById.get().getResMessage()); |
| 156 | + } |
| 157 | + logger.info("infoById" + infoById); |
| 158 | + int contractResult = |
| 159 | + ByteArray.toInt(infoById.get().getContractResult(0).toByteArray()); |
| 160 | + Assert.assertEquals(3,contractResult); |
| 161 | + } |
| 162 | + |
| 163 | + @Test(enabled = true, description = "Trigger contract with ") |
| 164 | + public void test03TriggerContract() { |
| 165 | + String methodStr = "StringSet(string)"; |
| 166 | + String argStr = "\"test\""; |
| 167 | + String txid = PublicMethed.triggerContract(contractAddress, methodStr, argStr, false, |
| 168 | + 0, maxFeeLimit, dev001Address, dev001Key, blockingStubFull); |
| 169 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 170 | + Optional<TransactionInfo> infoById = PublicMethed |
| 171 | + .getTransactionInfoById(txid, blockingStubFull); |
| 172 | + if (infoById.get().getResultValue() != 0) { |
| 173 | + Assert.fail("trigger contract failed with message: " + infoById.get().getResMessage()); |
| 174 | + } |
| 175 | + logger.info("infoById" + infoById); |
| 176 | + String contractResult = |
| 177 | + ByteArray.toHexString(infoById.get().getContractResult(0).toByteArray()); |
| 178 | + Assert.assertEquals("0000000000000000000000000000000000000000000000000000000000000020" |
| 179 | + + "0000000000000000000000000000000000000000000000000000000000000004" |
| 180 | + + "7465737400000000000000000000000000000000000000000000000000000000",contractResult); |
| 181 | + } |
| 182 | + |
| 183 | + @Test(enabled = true, description = "Trigger contract with ") |
| 184 | + public void test04TriggerContract() { |
| 185 | + String methodStr = "ByteSet(bytes32)"; |
| 186 | + String argStr = "00000000000000000000000000000000000000000000000000000000000003e9"; |
| 187 | + String txid = PublicMethed.triggerContract(contractAddress, methodStr, argStr, true, |
| 188 | + 0, maxFeeLimit, dev001Address, dev001Key, blockingStubFull); |
| 189 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 190 | + Optional<TransactionInfo> infoById = PublicMethed |
| 191 | + .getTransactionInfoById(txid, blockingStubFull); |
| 192 | + if (infoById.get().getResultValue() != 0) { |
| 193 | + Assert.fail("trigger contract failed with message: " + infoById.get().getResMessage()); |
| 194 | + } |
| 195 | + logger.info("infoById" + infoById); |
| 196 | + int contractResult = |
| 197 | + ByteArray.toInt(infoById.get().getContractResult(0).toByteArray()); |
| 198 | + Assert.assertEquals(1001,contractResult); |
| 199 | + } |
| 200 | + |
| 201 | + @Test(enabled = true, description = "Trigger contract with ") |
| 202 | + public void test05TriggerContract() { |
| 203 | + String methodStr = "UintArraySet(uint256[2])"; |
| 204 | + String argStr = "00000000000000000000000000000000000000000000000000000000000003e9" |
| 205 | + + "00000000000000000000000000000000000000000000000000000000000003e9"; |
| 206 | + String txid = PublicMethed.triggerContract(contractAddress, methodStr, argStr, true, |
| 207 | + 0, maxFeeLimit, dev001Address, dev001Key, blockingStubFull); |
| 208 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 209 | + Optional<TransactionInfo> infoById = PublicMethed |
| 210 | + .getTransactionInfoById(txid, blockingStubFull); |
| 211 | + if (infoById.get().getResultValue() != 0) { |
| 212 | + Assert.fail("trigger contract failed with message: " + infoById.get().getResMessage()); |
| 213 | + } |
| 214 | + logger.info("infoById" + infoById); |
| 215 | + String contractResult = |
| 216 | + ByteArray.toHexString(infoById.get().getContractResult(0).toByteArray()); |
| 217 | + Assert.assertEquals("00000000000000000000000000000000000000000000000000000000000003e9" |
| 218 | + + "00000000000000000000000000000000000000000000000000000000000003e9",contractResult); |
| 219 | + } |
| 220 | + |
| 221 | + @Test(enabled = true, description = "Trigger contract with ") |
| 222 | + public void test06TriggerContract() { |
| 223 | + String methodStr = "AddSet(address)"; |
| 224 | + String argStr = "\"TYVT8YJYis13NdrzdE7yVuwVxjsaRy2UsM\""; |
| 225 | + String txid = PublicMethed.triggerContract(contractAddress, methodStr, argStr, false, |
| 226 | + 0, maxFeeLimit, dev001Address, dev001Key, blockingStubFull); |
| 227 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 228 | + Optional<TransactionInfo> infoById = PublicMethed |
| 229 | + .getTransactionInfoById(txid, blockingStubFull); |
| 230 | + if (infoById.get().getResultValue() != 0) { |
| 231 | + Assert.fail("trigger contract failed with message: " + infoById.get().getResMessage()); |
| 232 | + } |
| 233 | + logger.info("infoById" + infoById); |
| 234 | + String contractResult = |
| 235 | + ByteArray.toHexString(infoById.get().getContractResult(0).toByteArray()); |
| 236 | + Assert.assertEquals("000000000000000000000000f70b0a56acf4b0af44723c329ff113a677b5f589", |
| 237 | + contractResult); |
| 238 | + } |
| 239 | + /** |
| 240 | + * constructor. |
| 241 | + */ |
| 242 | + |
| 243 | + @AfterClass |
| 244 | + public void shutdown() throws InterruptedException { |
| 245 | + long balance = PublicMethed.queryAccount(dev001Key, blockingStubFull).getBalance(); |
| 246 | + PublicMethed.sendcoin(fromAddress, balance, dev001Address, dev001Key, |
| 247 | + blockingStubFull); |
| 248 | + if (channelFull != null) { |
| 249 | + channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 250 | + } |
| 251 | + } |
| 252 | +} |
0 commit comments