|
| 1 | +package stest.tron.wallet.contract.trctoken; |
| 2 | + |
| 3 | +import io.grpc.ManagedChannel; |
| 4 | +import io.grpc.ManagedChannelBuilder; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.concurrent.TimeUnit; |
| 7 | +import lombok.extern.slf4j.Slf4j; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.testng.annotations.AfterClass; |
| 10 | +import org.testng.annotations.BeforeClass; |
| 11 | +import org.testng.annotations.BeforeSuite; |
| 12 | +import org.testng.annotations.Test; |
| 13 | +import org.tron.api.WalletGrpc; |
| 14 | +import org.tron.api.WalletSolidityGrpc; |
| 15 | +import org.tron.common.crypto.ECKey; |
| 16 | +import org.tron.common.utils.ByteArray; |
| 17 | +import org.tron.common.utils.Utils; |
| 18 | +import org.tron.core.Wallet; |
| 19 | +import org.tron.protos.Protocol.TransactionInfo; |
| 20 | +import stest.tron.wallet.common.client.Configuration; |
| 21 | +import stest.tron.wallet.common.client.Parameter.CommonConstant; |
| 22 | +import stest.tron.wallet.common.client.utils.PublicMethed; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +public class ContractTrcToken077 { |
| 26 | + |
| 27 | + |
| 28 | + private final String testNetAccountKey = Configuration.getByPath("testng.conf") |
| 29 | + .getString("foundationAccount.key1"); |
| 30 | + private final byte[] testNetAccountAddress = PublicMethed.getFinalAddress(testNetAccountKey); |
| 31 | + private Long maxFeeLimit = Configuration.getByPath("testng.conf") |
| 32 | + .getLong("defaultParameter.maxFeeLimit"); |
| 33 | + private ManagedChannel channelSolidity = null; |
| 34 | + |
| 35 | + private ManagedChannel channelFull = null; |
| 36 | + private WalletGrpc.WalletBlockingStub blockingStubFull = null; |
| 37 | + |
| 38 | + private ManagedChannel channelFull1 = null; |
| 39 | + private WalletGrpc.WalletBlockingStub blockingStubFull1 = null; |
| 40 | + |
| 41 | + |
| 42 | + private WalletSolidityGrpc.WalletSolidityBlockingStub blockingStubSolidity = null; |
| 43 | + |
| 44 | + private String fullnode = Configuration.getByPath("testng.conf") |
| 45 | + .getStringList("fullnode.ip.list").get(0); |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + byte[] contractAddress = null; |
| 50 | + |
| 51 | + ECKey ecKey1 = new ECKey(Utils.getRandom()); |
| 52 | + byte[] grammarAddress = ecKey1.getAddress(); |
| 53 | + String testKeyForGrammarAddress = ByteArray.toHexString(ecKey1.getPrivKeyBytes()); |
| 54 | + |
| 55 | + |
| 56 | + @BeforeSuite |
| 57 | + public void beforeSuite() { |
| 58 | + Wallet wallet = new Wallet(); |
| 59 | + Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * constructor. |
| 64 | + */ |
| 65 | + |
| 66 | + @BeforeClass(enabled = true) |
| 67 | + public void beforeClass() { |
| 68 | + PublicMethed.printAddress(testKeyForGrammarAddress); |
| 69 | + channelFull = ManagedChannelBuilder.forTarget(fullnode) |
| 70 | + .usePlaintext(true) |
| 71 | + .build(); |
| 72 | + blockingStubFull = WalletGrpc.newBlockingStub(channelFull); |
| 73 | + logger.info(Long.toString(PublicMethed.queryAccount(testNetAccountKey, blockingStubFull) |
| 74 | + .getBalance())); |
| 75 | + } |
| 76 | + |
| 77 | + @Test(enabled = false) |
| 78 | + public void testAddress001() { |
| 79 | + PublicMethed |
| 80 | + .sendcoin(grammarAddress, 100000000000L, testNetAccountAddress, testNetAccountKey, |
| 81 | + blockingStubFull); |
| 82 | + String contractName = "AddressTest"; |
| 83 | + String code = Configuration.getByPath("testng.conf") |
| 84 | + .getString("code.code_ContractTrcToken077_AddressTest"); |
| 85 | + String abi = Configuration.getByPath("testng.conf") |
| 86 | + .getString("abi.abi_ContractTrcToken077_AddressTest"); |
| 87 | + contractAddress = PublicMethed.deployContract(contractName, abi, code, "", maxFeeLimit, |
| 88 | + 0L, 100, null, testKeyForGrammarAddress, |
| 89 | + grammarAddress, blockingStubFull); |
| 90 | + String txid = ""; |
| 91 | + txid = PublicMethed.triggerContract(contractAddress, |
| 92 | + "addressTest()", "#", false, |
| 93 | + 0, maxFeeLimit, grammarAddress, testKeyForGrammarAddress, blockingStubFull); |
| 94 | + Optional<TransactionInfo> infoById = null; |
| 95 | + infoById = PublicMethed.getTransactionInfoById(txid, blockingStubFull); |
| 96 | + logger.info("infoById:" + infoById); |
| 97 | + |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + @Test(enabled = true, description = "The value of address is not at the beginning of 41") |
| 102 | + public void testAddress002() { |
| 103 | + PublicMethed |
| 104 | + .sendcoin(grammarAddress, 100000000000L, testNetAccountAddress, testNetAccountKey, |
| 105 | + blockingStubFull); |
| 106 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 107 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 108 | + |
| 109 | + String contractName = "AddressTest"; |
| 110 | + String code = Configuration.getByPath("testng.conf") |
| 111 | + .getString("code.code_ContractTrcToken077_AddressTest1"); |
| 112 | + String abi = Configuration.getByPath("testng.conf") |
| 113 | + .getString("abi.abi_ContractTrcToken077_AddressTest1"); |
| 114 | + contractAddress = PublicMethed.deployContract(contractName, abi, code, "", maxFeeLimit, |
| 115 | + 0L, 100, null, testKeyForGrammarAddress, |
| 116 | + grammarAddress, blockingStubFull); |
| 117 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 118 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 119 | + |
| 120 | + String txid = ""; |
| 121 | + txid = PublicMethed.triggerContract(contractAddress, |
| 122 | + "addressTest()", "#", false, |
| 123 | + 0, maxFeeLimit, grammarAddress, testKeyForGrammarAddress, blockingStubFull); |
| 124 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 125 | + PublicMethed.waitProduceNextBlock(blockingStubFull); |
| 126 | + |
| 127 | + Optional<TransactionInfo> infoById = null; |
| 128 | + infoById = PublicMethed.getTransactionInfoById(txid, blockingStubFull); |
| 129 | + logger.info("infoById:" + infoById); |
| 130 | + |
| 131 | + byte[] a = infoById.get().getContractResult(0).toByteArray(); |
| 132 | + byte[] b = subByte(a, 11, 1); |
| 133 | + byte[] c = subByte(a, 0, 11); |
| 134 | + byte[] e = "41".getBytes(); |
| 135 | + byte[] d = subByte(a, 12, 20); |
| 136 | + |
| 137 | + logger.info("a:" + ByteArray.toHexString(a)); |
| 138 | + |
| 139 | + logger.info("b:" + ByteArray.toHexString(b)); |
| 140 | + logger.info("c:" + ByteArray.toHexString(c)); |
| 141 | + |
| 142 | + logger.info("d:" + ByteArray.toHexString(d)); |
| 143 | + |
| 144 | + logger.info("41" + ByteArray.toHexString(d)); |
| 145 | + String exceptedResult = "41" + ByteArray.toHexString(d); |
| 146 | + String realResult = ByteArray.toHexString(b); |
| 147 | + Assert.assertEquals(realResult, "00"); |
| 148 | + Assert.assertNotEquals(realResult, "41"); |
| 149 | + |
| 150 | + Assert.assertEquals(exceptedResult, ByteArray.toHexString(contractAddress)); |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + |
| 155 | + /** |
| 156 | + * constructor. |
| 157 | + */ |
| 158 | + |
| 159 | + public byte[] subByte(byte[] b, int off, int length) { |
| 160 | + byte[] b1 = new byte[length]; |
| 161 | + System.arraycopy(b, off, b1, 0, length); |
| 162 | + return b1; |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * constructor. |
| 168 | + */ |
| 169 | + |
| 170 | + @AfterClass |
| 171 | + public void shutdown() throws InterruptedException { |
| 172 | + if (channelFull != null) { |
| 173 | + channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments