|
| 1 | +package stest.tron.wallet.dailybuild.tvmnewcommand.setTokenOwnerAddress; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.containsString; |
| 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.spongycastle.util.encoders.Hex; |
| 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.GrpcAPI.TransactionExtention; |
| 18 | +import org.tron.api.WalletGrpc; |
| 19 | +import org.tron.api.WalletSolidityGrpc; |
| 20 | +import org.tron.common.crypto.ECKey; |
| 21 | +import org.tron.common.utils.ByteArray; |
| 22 | +import org.tron.common.utils.Utils; |
| 23 | +import org.tron.core.Wallet; |
| 24 | +import org.tron.protos.Protocol.SmartContract; |
| 25 | +import org.tron.protos.Protocol.TransactionInfo; |
| 26 | +import stest.tron.wallet.common.client.Configuration; |
| 27 | +import stest.tron.wallet.common.client.Parameter.CommonConstant; |
| 28 | +import stest.tron.wallet.common.client.WalletClient; |
| 29 | +import stest.tron.wallet.common.client.utils.AbiUtil; |
| 30 | +import stest.tron.wallet.common.client.utils.Base58; |
| 31 | +import stest.tron.wallet.common.client.utils.PublicMethed; |
| 32 | +import stest.tron.wallet.common.client.utils.PublicMethedForDailybuild; |
| 33 | + |
| 34 | +@Slf4j |
| 35 | +public class setTokenOwnerAddressTess001 { |
| 36 | + |
| 37 | + |
| 38 | + final String mainGateWayAddress = Configuration.getByPath("testng.conf") |
| 39 | + .getString("gateway_address.key1"); |
| 40 | + final byte[] mainGateWayAddressKey = WalletClient.decodeFromBase58Check(mainGateWayAddress); |
| 41 | + final String sideGatewayAddress = Configuration.getByPath("testng.conf") |
| 42 | + .getString("gateway_address.key2"); |
| 43 | + final byte[] sideChainAddress = WalletClient.decodeFromBase58Check(sideGatewayAddress); |
| 44 | + final String chainIdAddress = Configuration.getByPath("testng.conf") |
| 45 | + .getString("gateway_address.chainIdAddress"); |
| 46 | + final byte[] chainIdAddressKey = WalletClient.decodeFromBase58Check(chainIdAddress); |
| 47 | + final String gateWayOwnerKey = Configuration.getByPath("testng.conf") |
| 48 | + .getString("gateWatOwnerAddressKey.key1"); |
| 49 | + final byte[] gateWayOwnerAddress = PublicMethed.getFinalAddress(gateWayOwnerKey); |
| 50 | + private final String fundationKey001 = Configuration.getByPath("testng.conf") |
| 51 | + .getString("foundationAccount.key2"); |
| 52 | + private final byte[] fundationAddress001 = PublicMethed.getFinalAddress(fundationKey001); |
| 53 | + private Long maxFeeLimit = Configuration.getByPath("testng.conf") |
| 54 | + .getLong("defaultParameter.maxFeeLimit"); |
| 55 | + private ManagedChannel channelSolidity = null; |
| 56 | + private ManagedChannel channelFull = null; |
| 57 | + private WalletGrpc.WalletBlockingStub blockingStubFull = null; |
| 58 | + private ManagedChannel channelFull1 = null; |
| 59 | + private WalletGrpc.WalletBlockingStub blockingSideStubFull = null; |
| 60 | + private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null; |
| 61 | + private String fullnode = Configuration.getByPath("testng.conf") |
| 62 | + .getStringList("mainfullnode.ip.list").get(0); |
| 63 | + private String fullnode1 = Configuration.getByPath("testng.conf") |
| 64 | + .getStringList("fullnode.ip.list").get(0); |
| 65 | + |
| 66 | + private ECKey ecKey1 = new ECKey(Utils.getRandom()); |
| 67 | + private byte[] dev001Address = ecKey1.getAddress(); |
| 68 | + private String dev001Key = ByteArray.toHexString(ecKey1.getPrivKeyBytes()); |
| 69 | + |
| 70 | + @BeforeSuite |
| 71 | + public void beforeSuite() { |
| 72 | + Wallet wallet = new Wallet(); |
| 73 | + Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * constructor. |
| 78 | + */ |
| 79 | + |
| 80 | + @BeforeClass(enabled = true) |
| 81 | + public void beforeClass() { |
| 82 | + channelFull = ManagedChannelBuilder.forTarget(fullnode) |
| 83 | + .usePlaintext(true) |
| 84 | + .build(); |
| 85 | + blockingStubFull = WalletGrpc.newBlockingStub(channelFull); |
| 86 | + channelFull1 = ManagedChannelBuilder.forTarget(fullnode1) |
| 87 | + .usePlaintext(true) |
| 88 | + .build(); |
| 89 | + blockingSideStubFull = WalletGrpc.newBlockingStub(channelFull1); |
| 90 | + |
| 91 | + Assert.assertTrue( |
| 92 | + PublicMethedForDailybuild.sendcoin(dev001Address, 10000_000_000L, fundationAddress001, |
| 93 | + fundationKey001, blockingSideStubFull)); |
| 94 | + Assert.assertTrue( |
| 95 | + PublicMethed.sendcoin(dev001Address, 10000_000_000L, fundationAddress001, |
| 96 | + fundationKey001, blockingStubFull)); |
| 97 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 98 | + } |
| 99 | + |
| 100 | + @Test(enabled = true, description = "normal Address trigger setTokenOwnerAddress Function") |
| 101 | + public void setTokenOwnerAddress001() { |
| 102 | + |
| 103 | + String parame2 = |
| 104 | + "\"" + sideGatewayAddress + "\",\"" + WalletClient.encode58Check(fundationAddress001) |
| 105 | + + "\""; |
| 106 | + |
| 107 | + byte[] input1 = Hex.decode(AbiUtil.parseMethod("setTokenOwner(address,address)", parame2, |
| 108 | + false)); |
| 109 | + String ownerTrx1 = PublicMethed |
| 110 | + .triggerContractSideChain(sideChainAddress, chainIdAddressKey, 0l, input1, |
| 111 | + 1000000000, |
| 112 | + 0l, "0", fundationAddress001, fundationKey001, blockingSideStubFull); |
| 113 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 114 | + Optional<TransactionInfo> infoById3 = PublicMethed |
| 115 | + .getTransactionInfoById(ownerTrx1, blockingSideStubFull); |
| 116 | + |
| 117 | + logger.info("infoById3:" + infoById3); |
| 118 | + |
| 119 | + String data = ByteArray |
| 120 | + .toHexString(infoById3.get().getContractResult(0).substring(68, 85).toByteArray()); |
| 121 | + logger.info("data:" + data); |
| 122 | + Assert.assertEquals("msg.sender != own", PublicMethed.hexStringToString(data)); |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + @Test(enabled = true, description = "normal Contract trigger 0x10002 ") |
| 127 | + public void setTokenOwnerAddress002() { |
| 128 | + |
| 129 | + String filePath = "./src/test/resources/soliditycode/setTokenOwnerAddress001.sol"; |
| 130 | + String contractName = "Ballot"; |
| 131 | + HashMap retMap = PublicMethedForDailybuild.getBycodeAbi(filePath, contractName); |
| 132 | + |
| 133 | + String code = retMap.get("byteCode").toString(); |
| 134 | + String abi = retMap.get("abI").toString(); |
| 135 | + |
| 136 | + final String transferTokenTxid = PublicMethedForDailybuild |
| 137 | + .deployContractAndGetTransactionInfoById(contractName, abi, code, "", |
| 138 | + maxFeeLimit, 0L, 0, 10000, |
| 139 | + "0", 0, null, dev001Key, |
| 140 | + dev001Address, blockingSideStubFull); |
| 141 | + PublicMethedForDailybuild.waitProduceNextBlock(blockingSideStubFull); |
| 142 | + |
| 143 | + Optional<TransactionInfo> infoById = PublicMethedForDailybuild |
| 144 | + .getTransactionInfoById(transferTokenTxid, blockingSideStubFull); |
| 145 | + |
| 146 | + if (infoById.get().getResultValue() != 0) { |
| 147 | + Assert.fail("deploy transaction failed with message: " + infoById.get().getResMessage()); |
| 148 | + } |
| 149 | + |
| 150 | + TransactionInfo transactionInfo = infoById.get(); |
| 151 | + logger.info("EnergyUsageTotal: " + transactionInfo.getReceipt().getEnergyUsageTotal()); |
| 152 | + logger.info("NetUsage: " + transactionInfo.getReceipt().getNetUsage()); |
| 153 | + |
| 154 | + byte[] factoryContractAddress = infoById.get().getContractAddress().toByteArray(); |
| 155 | + SmartContract smartContract = PublicMethedForDailybuild.getContract(factoryContractAddress, |
| 156 | + blockingSideStubFull); |
| 157 | + Assert.assertNotNull(smartContract.getAbi()); |
| 158 | + |
| 159 | + String parame2 = |
| 160 | + "\"" + sideGatewayAddress + "\",\"" + WalletClient.encode58Check(fundationAddress001) |
| 161 | + + "\""; |
| 162 | + |
| 163 | + byte[] input1 = Hex.decode(AbiUtil.parseMethod("setTokenOwner(address,address)", parame2, |
| 164 | + false)); |
| 165 | + String ownerTrx1 = PublicMethed |
| 166 | + .triggerContractSideChain(factoryContractAddress, chainIdAddressKey, 0l, input1, |
| 167 | + 1000000000, |
| 168 | + 0l, "0", fundationAddress001, fundationKey001, blockingSideStubFull); |
| 169 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 170 | + Optional<TransactionInfo> infoById3 = PublicMethed |
| 171 | + .getTransactionInfoById(ownerTrx1, blockingSideStubFull); |
| 172 | + |
| 173 | + logger.info("infoById3:" + infoById3); |
| 174 | + |
| 175 | + String data = infoById3.get().getResMessage().toStringUtf8(); |
| 176 | + Assert.assertThat(data, containsString("[updatecontractowner method]caller must be gateway")); |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + @Test(enabled = true, description = "Contract OriginAddress isno Gateway ") |
| 181 | + public void setTokenOwnerAddress003() { |
| 182 | + |
| 183 | + String contractName = "trc20Contract"; |
| 184 | + String code = Configuration.getByPath("testng.conf") |
| 185 | + .getString("code.code_ContractTRC20"); |
| 186 | + String abi = Configuration.getByPath("testng.conf") |
| 187 | + .getString("abi.abi_ContractTRC20"); |
| 188 | + String parame = "\"" + Base58.encode58Check(dev001Address) + "\""; |
| 189 | + |
| 190 | + String deployTxid = PublicMethed |
| 191 | + .deployContractWithConstantParame(contractName, abi, code, "TronToken(address)", |
| 192 | + parame, "", |
| 193 | + maxFeeLimit, |
| 194 | + 0L, 100, null, dev001Key, dev001Address |
| 195 | + , blockingStubFull); |
| 196 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 197 | + |
| 198 | + Optional<TransactionInfo> infoById = PublicMethed |
| 199 | + .getTransactionInfoById(deployTxid, blockingStubFull); |
| 200 | + byte[] trc20Contract = infoById.get().getContractAddress().toByteArray(); |
| 201 | + Assert.assertEquals(0, infoById.get().getResultValue()); |
| 202 | + Assert.assertNotNull(trc20Contract); |
| 203 | + |
| 204 | + String mapTxid5 = PublicMethed |
| 205 | + .mappingTrc20(mainGateWayAddressKey, deployTxid, 1000000000, |
| 206 | + dev001Address, dev001Key, blockingStubFull); |
| 207 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 208 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 209 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 210 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 211 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 212 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 213 | + |
| 214 | + Optional<TransactionInfo> infoById5 = PublicMethed |
| 215 | + .getTransactionInfoById(mapTxid5, blockingStubFull); |
| 216 | + Assert.assertEquals("SUCESS", infoById5.get().getResult().name()); |
| 217 | + Assert.assertEquals(0, infoById5.get().getResultValue()); |
| 218 | + |
| 219 | + String parame1 = "\"" + Base58.encode58Check(trc20Contract) + "\""; |
| 220 | + byte[] input2 = Hex |
| 221 | + .decode(AbiUtil.parseMethod("mainToSideContractMap(address)", parame1, false)); |
| 222 | + TransactionExtention return1 = PublicMethed |
| 223 | + .triggerContractForTransactionExtention(sideChainAddress, 0, input2, |
| 224 | + maxFeeLimit, |
| 225 | + 0, "0", |
| 226 | + dev001Address, dev001Key, blockingSideStubFull); |
| 227 | + logger.info(Hex.toHexString(return1.getConstantResult(0).toByteArray())); |
| 228 | + String ContractRestule = Hex.toHexString(return1.getConstantResult(0).toByteArray()); |
| 229 | + |
| 230 | + String tmpAddress = ContractRestule.substring(24); |
| 231 | + logger.info(tmpAddress); |
| 232 | + String addressHex = "41" + tmpAddress; |
| 233 | + logger.info("address_hex: " + addressHex); |
| 234 | + String addressFinal = Base58.encode58Check(ByteArray.fromHexString(addressHex)); |
| 235 | + logger.info("address_final: " + addressFinal); |
| 236 | + |
| 237 | + byte[] sideContractAddress = WalletClient.decodeFromBase58Check(addressFinal); |
| 238 | + Assert.assertEquals(0, infoById.get().getResultValue()); |
| 239 | + Assert.assertNotNull(sideContractAddress); |
| 240 | + |
| 241 | + SmartContract contract = PublicMethed.getContract(sideContractAddress, blockingSideStubFull); |
| 242 | + Assert.assertEquals(WalletClient.encode58Check(contract.getOriginAddress().toByteArray()), |
| 243 | + WalletClient.encode58Check(dev001Address)); |
| 244 | + |
| 245 | + String parame2 = |
| 246 | + "\"" + addressFinal + "\",\"" + WalletClient.encode58Check(fundationAddress001) |
| 247 | + + "\""; |
| 248 | + |
| 249 | + byte[] input1 = Hex.decode(AbiUtil.parseMethod("setTokenOwner(address,address)", parame2, |
| 250 | + false)); |
| 251 | + String ownerTrx1 = PublicMethed |
| 252 | + .triggerContractSideChain(sideChainAddress, chainIdAddressKey, 0l, input1, |
| 253 | + 1000000000, |
| 254 | + 0l, "0", gateWayOwnerAddress, gateWayOwnerKey, blockingSideStubFull); |
| 255 | + PublicMethed.waitProduceNextBlock(blockingSideStubFull); |
| 256 | + Optional<TransactionInfo> infoById3 = PublicMethed |
| 257 | + .getTransactionInfoById(ownerTrx1, blockingSideStubFull); |
| 258 | + |
| 259 | + logger.info("infoById3:" + infoById3); |
| 260 | + |
| 261 | + String data = infoById3.get().getResMessage().toStringUtf8(); |
| 262 | + Assert.assertThat(data, containsString( |
| 263 | + "[updatecontractowner method]target contract not exists or address not in gatewayList")); |
| 264 | + |
| 265 | + } |
| 266 | + |
| 267 | + /** |
| 268 | + * constructor. |
| 269 | + */ |
| 270 | + @AfterClass |
| 271 | + public void shutdown() throws InterruptedException { |
| 272 | + |
| 273 | + if (channelFull != null) { |
| 274 | + channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 275 | + } |
| 276 | + if (channelFull1 != null) { |
| 277 | + channelFull1.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 278 | + } |
| 279 | + } |
| 280 | +} |
0 commit comments