|
| 1 | +package org.tron.core.actuator; |
| 2 | + |
| 3 | +import static junit.framework.TestCase.fail; |
| 4 | +import static stest.tron.wallet.common.client.utils.PublicMethed.jsonStr2Abi; |
| 5 | + |
| 6 | +import com.google.protobuf.Any; |
| 7 | +import com.google.protobuf.ByteString; |
| 8 | +import java.io.File; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.junit.AfterClass; |
| 11 | +import org.junit.Assert; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.BeforeClass; |
| 14 | +import org.junit.Test; |
| 15 | +import org.tron.common.application.TronApplicationContext; |
| 16 | +import org.tron.common.utils.ByteArray; |
| 17 | +import org.tron.common.utils.FileUtil; |
| 18 | +import org.tron.common.utils.StringUtil; |
| 19 | +import org.tron.core.Constant; |
| 20 | +import org.tron.core.Wallet; |
| 21 | +import org.tron.core.capsule.AccountCapsule; |
| 22 | +import org.tron.core.capsule.ContractCapsule; |
| 23 | +import org.tron.core.capsule.TransactionResultCapsule; |
| 24 | +import org.tron.core.config.DefaultConfig; |
| 25 | +import org.tron.core.config.args.Args; |
| 26 | +import org.tron.core.db.Manager; |
| 27 | +import org.tron.core.exception.ContractExeException; |
| 28 | +import org.tron.core.exception.ContractValidateException; |
| 29 | +import org.tron.protos.Contract; |
| 30 | +import org.tron.protos.Protocol; |
| 31 | +import org.tron.protos.Protocol.SmartContract.ABI; |
| 32 | + |
| 33 | + |
| 34 | +@Slf4j |
| 35 | +public class ClearABIContractActuatorTest { |
| 36 | + |
| 37 | + private static TronApplicationContext context; |
| 38 | + private static Manager dbManager; |
| 39 | + private static final String dbPath = "output_clearabicontract_test"; |
| 40 | + private static final String OWNER_ADDRESS; |
| 41 | + private static final String OWNER_ADDRESS_ACCOUNT_NAME = "test_account"; |
| 42 | + private static final String SECOND_ACCOUNT_ADDRESS; |
| 43 | + private static final String OWNER_ADDRESS_NOTEXIST; |
| 44 | + private static final String OWNER_ADDRESS_INVALID = "aaaa"; |
| 45 | + private static final String SMART_CONTRACT_NAME = "smart_contarct"; |
| 46 | + private static final String CONTRACT_ADDRESS = "111111"; |
| 47 | + private static final String NO_EXIST_CONTRACT_ADDRESS = "2222222"; |
| 48 | + private static final ABI SOURCE_ABI = jsonStr2Abi("[{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]"); |
| 49 | + private static final ABI TARGET_ABI = ABI.getDefaultInstance(); |
| 50 | + |
| 51 | + static { |
| 52 | + Args.setParam(new String[]{"--output-directory", dbPath}, Constant.TEST_CONF); |
| 53 | + context = new TronApplicationContext(DefaultConfig.class); |
| 54 | + OWNER_ADDRESS = Wallet.getAddressPreFixString() + "abd4b9367799eaa3197fecb144eb71de1e049abc"; |
| 55 | + OWNER_ADDRESS_NOTEXIST = |
| 56 | + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d4271a1abc"; |
| 57 | + SECOND_ACCOUNT_ADDRESS = |
| 58 | + Wallet.getAddressPreFixString() + "548794500882809695a8a687866e76d427122222"; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Init data. |
| 63 | + */ |
| 64 | + @BeforeClass |
| 65 | + public static void init() { |
| 66 | + dbManager = context.getBean(Manager.class); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * create temp Capsule test need. |
| 71 | + */ |
| 72 | + @Before |
| 73 | + public void createCapsule() { |
| 74 | + // address in accountStore and the owner of contract |
| 75 | + AccountCapsule accountCapsule = |
| 76 | + new AccountCapsule( |
| 77 | + ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS)), |
| 78 | + ByteString.copyFromUtf8(OWNER_ADDRESS_ACCOUNT_NAME), |
| 79 | + Protocol.AccountType.Normal); |
| 80 | + dbManager.getAccountStore().put(ByteArray.fromHexString(OWNER_ADDRESS), accountCapsule); |
| 81 | + |
| 82 | + // smartContarct in contractStore |
| 83 | + Protocol.SmartContract.Builder builder = Protocol.SmartContract.newBuilder(); |
| 84 | + builder.setName(SMART_CONTRACT_NAME); |
| 85 | + builder.setOriginAddress(ByteString.copyFrom(ByteArray.fromHexString(OWNER_ADDRESS))); |
| 86 | + builder.setContractAddress(ByteString.copyFrom(ByteArray.fromHexString(CONTRACT_ADDRESS))); |
| 87 | + builder.setAbi(SOURCE_ABI); |
| 88 | + dbManager.getContractStore().put( |
| 89 | + ByteArray.fromHexString(CONTRACT_ADDRESS), |
| 90 | + new ContractCapsule(builder.build())); |
| 91 | + |
| 92 | + // address in accountStore not the owner of contract |
| 93 | + AccountCapsule secondAccount = |
| 94 | + new AccountCapsule( |
| 95 | + ByteString.copyFrom(ByteArray.fromHexString(SECOND_ACCOUNT_ADDRESS)), |
| 96 | + ByteString.copyFromUtf8(OWNER_ADDRESS_ACCOUNT_NAME), |
| 97 | + Protocol.AccountType.Normal); |
| 98 | + dbManager.getAccountStore().put(ByteArray.fromHexString(SECOND_ACCOUNT_ADDRESS), secondAccount); |
| 99 | + |
| 100 | + // address does not exist in accountStore |
| 101 | + dbManager.getAccountStore().delete(ByteArray.fromHexString(OWNER_ADDRESS_NOTEXIST)); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Release resources. |
| 106 | + */ |
| 107 | + @AfterClass |
| 108 | + public static void destroy() { |
| 109 | + Args.clearParam(); |
| 110 | + context.destroy(); |
| 111 | + if (FileUtil.deleteDir(new File(dbPath))) { |
| 112 | + logger.info("Release resources successful."); |
| 113 | + } else { |
| 114 | + logger.info("Release resources failure."); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private Any getContract(String accountAddress, String contractAddress) { |
| 119 | + return Any.pack( |
| 120 | + Contract.ClearABIContract.newBuilder() |
| 121 | + .setOwnerAddress(StringUtil.hexString2ByteString(accountAddress)) |
| 122 | + .setContractAddress(StringUtil.hexString2ByteString(contractAddress)) |
| 123 | + .build()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void successClearABIContract() { |
| 128 | + ClearABIContractActuator actuator = |
| 129 | + new ClearABIContractActuator( |
| 130 | + getContract(OWNER_ADDRESS, CONTRACT_ADDRESS), dbManager); |
| 131 | + |
| 132 | + TransactionResultCapsule ret = new TransactionResultCapsule(); |
| 133 | + try { |
| 134 | + actuator.validate(); |
| 135 | + actuator.execute(ret); |
| 136 | + |
| 137 | + // assert result state and consume_user_resource_percent |
| 138 | + Assert.assertEquals(ret.getInstance().getRet(), Protocol.Transaction.Result.code.SUCESS); |
| 139 | + Assert.assertEquals( |
| 140 | + dbManager.getContractStore().get(ByteArray.fromHexString(CONTRACT_ADDRESS)). |
| 141 | + getInstance().getAbi(), |
| 142 | + TARGET_ABI); |
| 143 | + } catch (ContractValidateException e) { |
| 144 | + Assert.assertFalse(e instanceof ContractValidateException); |
| 145 | + } catch (ContractExeException e) { |
| 146 | + Assert.assertFalse(e instanceof ContractExeException); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + public void invalidAddress() { |
| 152 | + ClearABIContractActuator actuator = |
| 153 | + new ClearABIContractActuator( |
| 154 | + getContract(OWNER_ADDRESS_INVALID, CONTRACT_ADDRESS), dbManager); |
| 155 | + |
| 156 | + TransactionResultCapsule ret = new TransactionResultCapsule(); |
| 157 | + try { |
| 158 | + actuator.validate(); |
| 159 | + actuator.execute(ret); |
| 160 | + |
| 161 | + fail("Invalid address"); |
| 162 | + } catch (ContractValidateException e) { |
| 163 | + Assert.assertTrue(e instanceof ContractValidateException); |
| 164 | + Assert.assertEquals("Invalid address", e.getMessage()); |
| 165 | + } catch (ContractExeException e) { |
| 166 | + Assert.assertFalse(e instanceof ContractExeException); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void noExistAccount() { |
| 172 | + ClearABIContractActuator actuator = |
| 173 | + new ClearABIContractActuator( |
| 174 | + getContract(OWNER_ADDRESS_NOTEXIST, CONTRACT_ADDRESS), dbManager); |
| 175 | + |
| 176 | + TransactionResultCapsule ret = new TransactionResultCapsule(); |
| 177 | + try { |
| 178 | + actuator.validate(); |
| 179 | + actuator.execute(ret); |
| 180 | + |
| 181 | + fail("Account[" + OWNER_ADDRESS_NOTEXIST + "] not exists"); |
| 182 | + } catch (ContractValidateException e) { |
| 183 | + Assert.assertTrue(e instanceof ContractValidateException); |
| 184 | + Assert.assertEquals("Account[" + OWNER_ADDRESS_NOTEXIST + "] not exists", e.getMessage()); |
| 185 | + } catch (ContractExeException e) { |
| 186 | + Assert.assertFalse(e instanceof ContractExeException); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void noExistContract() { |
| 192 | + ClearABIContractActuator actuator = |
| 193 | + new ClearABIContractActuator( |
| 194 | + getContract(OWNER_ADDRESS, NO_EXIST_CONTRACT_ADDRESS), dbManager); |
| 195 | + |
| 196 | + TransactionResultCapsule ret = new TransactionResultCapsule(); |
| 197 | + try { |
| 198 | + actuator.validate(); |
| 199 | + actuator.execute(ret); |
| 200 | + |
| 201 | + fail("Contract not exists"); |
| 202 | + } catch (ContractValidateException e) { |
| 203 | + Assert.assertTrue(e instanceof ContractValidateException); |
| 204 | + Assert.assertEquals("Contract not exists", e.getMessage()); |
| 205 | + } catch (ContractExeException e) { |
| 206 | + Assert.assertFalse(e instanceof ContractExeException); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void callerNotContractOwner() { |
| 212 | + ClearABIContractActuator actuator = |
| 213 | + new ClearABIContractActuator( |
| 214 | + getContract(SECOND_ACCOUNT_ADDRESS, CONTRACT_ADDRESS), dbManager); |
| 215 | + |
| 216 | + TransactionResultCapsule ret = new TransactionResultCapsule(); |
| 217 | + try { |
| 218 | + actuator.validate(); |
| 219 | + actuator.execute(ret); |
| 220 | + |
| 221 | + fail("Account[" + SECOND_ACCOUNT_ADDRESS + "] is not the owner of the contract"); |
| 222 | + } catch (ContractValidateException e) { |
| 223 | + Assert.assertTrue(e instanceof ContractValidateException); |
| 224 | + Assert.assertEquals( |
| 225 | + "Account[" + SECOND_ACCOUNT_ADDRESS + "] is not the owner of the contract", |
| 226 | + e.getMessage()); |
| 227 | + } catch (ContractExeException e) { |
| 228 | + Assert.assertFalse(e instanceof ContractExeException); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + @Test |
| 233 | + public void twiceUpdateSettingContract() { |
| 234 | + ClearABIContractActuator actuator = |
| 235 | + new ClearABIContractActuator( |
| 236 | + getContract(OWNER_ADDRESS, CONTRACT_ADDRESS), dbManager); |
| 237 | + |
| 238 | + ClearABIContractActuator secondActuator = |
| 239 | + new ClearABIContractActuator( |
| 240 | + getContract(OWNER_ADDRESS, CONTRACT_ADDRESS), dbManager); |
| 241 | + |
| 242 | + TransactionResultCapsule ret = new TransactionResultCapsule(); |
| 243 | + try { |
| 244 | + // first |
| 245 | + actuator.validate(); |
| 246 | + actuator.execute(ret); |
| 247 | + |
| 248 | + Assert.assertEquals(ret.getInstance().getRet(), Protocol.Transaction.Result.code.SUCESS); |
| 249 | + Assert.assertEquals( |
| 250 | + dbManager.getContractStore().get(ByteArray.fromHexString(CONTRACT_ADDRESS)). |
| 251 | + getInstance().getAbi(), |
| 252 | + TARGET_ABI); |
| 253 | + |
| 254 | + // second |
| 255 | + secondActuator.validate(); |
| 256 | + secondActuator.execute(ret); |
| 257 | + |
| 258 | + Assert.assertEquals(ret.getInstance().getRet(), Protocol.Transaction.Result.code.SUCESS); |
| 259 | + Assert.assertEquals( |
| 260 | + dbManager.getContractStore().get(ByteArray.fromHexString(CONTRACT_ADDRESS)). |
| 261 | + getInstance().getAbi(), |
| 262 | + TARGET_ABI); |
| 263 | + |
| 264 | + } catch (ContractValidateException e) { |
| 265 | + Assert.assertFalse(e instanceof ContractValidateException); |
| 266 | + } catch (ContractExeException e) { |
| 267 | + Assert.assertFalse(e instanceof ContractExeException); |
| 268 | + } |
| 269 | + } |
| 270 | +} |
0 commit comments