Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package stest.tron.wallet.contract.linkage;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.tron.api.GrpcAPI.AccountResourceMessage;
import org.tron.api.WalletGrpc;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Utils;
import org.tron.core.Wallet;
import org.tron.protos.Protocol.Account;
import org.tron.protos.Protocol.TransactionInfo;
import stest.tron.wallet.common.client.Configuration;
import stest.tron.wallet.common.client.Parameter.CommonConstant;
import stest.tron.wallet.common.client.utils.PublicMethed;

@Slf4j
public class ContractLinkage005 {

//testng001、testng002、testng003、testng004
private final String testKey003 =
"FC8BF0238748587B9617EB6D15D47A66C0E07C1A1959033CF249C6532DC29FE6";
private final byte[] fromAddress = PublicMethed.getFinalAddress(testKey003);

private ManagedChannel channelFull = null;
private WalletGrpc.WalletBlockingStub blockingStubFull = null;
private String fullnode = Configuration.getByPath("testng.conf")
.getStringList("fullnode.ip.list").get(0);

String contractName;
String code;
String abi;
Long currentFee;
Long zeroForCycleCost;
Long firstForCycleCost;
Long secondForCycleCost;
Long thirdForCycleCost;
Long forthForCycleCost;
Long fifthForCycleCost;
Long zeroForCycleTimes = 498L;
Long firstForCycleTimes = 500L;
Long secondForCycleTimes = 502L;
Long thirdForCycleTimes = 504L;
Long forthForCycleTimes = 506L;
Long fifthForCycleTimes = 508L;
byte [] contractAddress;

ECKey ecKey1 = new ECKey(Utils.getRandom());
byte[] linkage005Address = ecKey1.getAddress();
String linkage005Key = ByteArray.toHexString(ecKey1.getPrivKeyBytes());

@BeforeSuite
public void beforeSuite() {
Wallet wallet = new Wallet();
Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET);
}

@BeforeClass(enabled = true)
public void beforeClass() {
PublicMethed.printAddress(linkage005Key);
channelFull = ManagedChannelBuilder.forTarget(fullnode)
.usePlaintext(true)
.build();
blockingStubFull = WalletGrpc.newBlockingStub(channelFull);

}

@Test(enabled = true)
public void testEnergyCostDetail() {
Assert.assertTrue(PublicMethed.sendcoin(linkage005Address,2000000000000L,fromAddress,
testKey003,blockingStubFull));
Assert.assertTrue(PublicMethed.freezeBalance(linkage005Address,1000000L,
3,linkage005Key,blockingStubFull));
Assert.assertTrue(PublicMethed.freezeBalanceGetEnergy(linkage005Address,1000000L,
3,1,linkage005Key,blockingStubFull));

final Long maxFeeLimit = 150000000L;
contractName = "EnergyCost";
code = "6080604052600060035534801561001557600080fd5b5061027b806100256000396000f3006080604052600436106100825763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633755cd3c81146100875780637d965688146100b1578063a05b2577146100c9578063b0d6304d146100e1578063bbe1d75b14610115578063f8a8fd6d1461012a578063fe75faab14610141575b600080fd5b34801561009357600080fd5b5061009f600435610159565b60408051918252519081900360200190f35b3480156100bd57600080fd5b5061009f600435610178565b3480156100d557600080fd5b5061009f600435610198565b3480156100ed57600080fd5b5061009f73ffffffffffffffffffffffffffffffffffffffff600435811690602435166101e2565b34801561012157600080fd5b5061009f6101ff565b34801561013657600080fd5b5061013f610205565b005b34801561014d57600080fd5b5061009f600435610218565b600080548290811061016757fe5b600091825260209091200154905081565b600080805b83811015610191576001918201910161017d565b5092915050565b600080805b838110156101915760008054600181810183559180527f290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e56301829055918201910161019d565b600260209081526000928352604080842090915290825290205481565b60015481565b600380546001019055610216610205565b565b60006102238261022e565b600181905592915050565b600061023c6002830361022e565b6102486001840361022e565b01929150505600a165627a7a72305820bc44fd5f3a0e48cc057752b52e3abf50cd7dc75b3874ea7d049893cf1a2e345f0029";
abi = "[{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"iarray\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"testUseCpu\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"a\",\"type\":\"uint256\"}],\"name\":\"testUseStorage\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"},{\"name\":\"\",\"type\":\"address\"}],\"name\":\"m\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"calculatedFibNumber\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"test\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"n\",\"type\":\"uint256\"}],\"name\":\"setFibonacci\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";



contractAddress = PublicMethed.deployContract(contractName,abi,code,
"",maxFeeLimit, 0L, 100,null,linkage005Key,
linkage005Address,blockingStubFull);

firstForCycleTimes = 1000L;
secondForCycleTimes = 1002L;
thirdForCycleTimes = 1004L;

String txid = PublicMethed.triggerContract(contractAddress,
"testUseCpu(uint256)",firstForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
Optional<TransactionInfo> infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
firstForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();

txid = PublicMethed.triggerContract(contractAddress,
"testUseCpu(uint256)",secondForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
secondForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();

txid = PublicMethed.triggerContract(contractAddress,
"testUseCpu(uint256)",thirdForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
thirdForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();

logger.info(firstForCycleCost.toString());
logger.info(secondForCycleCost.toString());
logger.info(thirdForCycleCost.toString());

Assert.assertTrue(thirdForCycleCost - secondForCycleCost
== secondForCycleCost - firstForCycleCost);


zeroForCycleTimes = 498L;
firstForCycleTimes = 500L;
secondForCycleTimes = 502L;
thirdForCycleTimes = 504L;
forthForCycleTimes = 506L;
fifthForCycleTimes = 508L;

txid = PublicMethed.triggerContract(contractAddress,
"testUseStorage(uint256)",zeroForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
zeroForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();

txid = PublicMethed.triggerContract(contractAddress,
"testUseStorage(uint256)",firstForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
firstForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();

txid = PublicMethed.triggerContract(contractAddress,
"testUseStorage(uint256)",secondForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
secondForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();

txid = PublicMethed.triggerContract(contractAddress,
"testUseStorage(uint256)",thirdForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
thirdForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();

txid = PublicMethed.triggerContract(contractAddress,
"testUseStorage(uint256)",forthForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
forthForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();

txid = PublicMethed.triggerContract(contractAddress,
"testUseStorage(uint256)",fifthForCycleTimes.toString(), false,
0, 100000000L, linkage005Address, linkage005Key, blockingStubFull);
infoById = PublicMethed.getTransactionInfoById(txid,blockingStubFull);
fifthForCycleCost = infoById.get().getReceipt().getEnergyUsageTotal();



/* logger.info("Zero cost is " + zeroForCycleCost);
logger.info("First cost is " + firstForCycleCost);
logger.info("Second cost is " + secondForCycleCost);
logger.info("Third cost is " + thirdForCycleCost);
logger.info("Forth cost is " + forthForCycleCost);
logger.info("Fifth cost is " + fifthForCycleCost);
logger.info(Long.toString(firstForCycleCost - zeroForCycleCost));
logger.info(Long.toString(secondForCycleCost - firstForCycleCost));
logger.info(Long.toString(thirdForCycleCost - secondForCycleCost));
logger.info(Long.toString(forthForCycleCost - thirdForCycleCost));
logger.info(Long.toString(fifthForCycleCost - forthForCycleCost));*/

Assert.assertTrue(thirdForCycleCost - secondForCycleCost
== secondForCycleCost - firstForCycleCost);
Assert.assertTrue(fifthForCycleCost - forthForCycleCost
== forthForCycleCost - thirdForCycleCost);



}

@AfterClass
public void shutdown() throws InterruptedException {
if (channelFull != null) {
channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
}


}


Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package stest.tron.wallet.contract.scenario;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.tron.api.GrpcAPI.AccountResourceMessage;
import org.tron.api.WalletGrpc;
import org.tron.common.crypto.ECKey;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.Utils;
import org.tron.core.Wallet;
import org.tron.protos.Protocol.SmartContract;
import org.tron.protos.Protocol.TransactionInfo;
import stest.tron.wallet.common.client.Configuration;
import stest.tron.wallet.common.client.Parameter.CommonConstant;
import stest.tron.wallet.common.client.utils.Base58;
import stest.tron.wallet.common.client.utils.PublicMethed;

@Slf4j
public class ContractScenario013 {

//testng001、testng002、testng003、testng004
private final String testKey002 =
"FC8BF0238748587B9617EB6D15D47A66C0E07C1A1959033CF249C6532DC29FE6";
private final byte[] fromAddress = PublicMethed.getFinalAddress(testKey002);

private ManagedChannel channelFull = null;
private WalletGrpc.WalletBlockingStub blockingStubFull = null;
private String fullnode = Configuration.getByPath("testng.conf")
.getStringList("fullnode.ip.list").get(0);

byte[] contractAddress = null;
String txid = "";
Optional<TransactionInfo> infoById = null;

ECKey ecKey1 = new ECKey(Utils.getRandom());
byte[] contract013Address = ecKey1.getAddress();
String contract013Key = ByteArray.toHexString(ecKey1.getPrivKeyBytes());

@BeforeSuite
public void beforeSuite() {
Wallet wallet = new Wallet();
Wallet.setAddressPreFixByte(CommonConstant.ADD_PRE_FIX_BYTE_MAINNET);
}

@BeforeClass(enabled = true)
public void beforeClass() {
PublicMethed.printAddress(contract013Key);
channelFull = ManagedChannelBuilder.forTarget(fullnode)
.usePlaintext(true)
.build();
blockingStubFull = WalletGrpc.newBlockingStub(channelFull);
Assert.assertTrue(PublicMethed.sendcoin(contract013Address,2000000000L,fromAddress,
testKey002,blockingStubFull));
}

@Test(enabled = true)
public void deployTronTrxAndSunContract() {
AccountResourceMessage accountResource = PublicMethed.getAccountResource(contract013Address,
blockingStubFull);
Long energyLimit = accountResource.getEnergyLimit();
Long energyUsage = accountResource.getEnergyUsed();

logger.info("before energy limit is " + Long.toString(energyLimit));
logger.info("before energy usage is " + Long.toString(energyUsage));
Long maxFeeLimit = 5000000L;
String contractName = "TronTrxAndSunContract";
String code = "6080604052348015600f57600080fd5b50600180141515601e57600080fd5b603c80141515602c57600080fd5b610e1080141515603b57600080fd5b6201518080141515604b57600080fd5b62093a8080141515605b57600080fd5b6301e1338080141515606c57600080fd5b620f424080141515607c57600080fd5b60358060896000396000f3006080604052600080fd00a165627a7a723058206a36395ee2292959a89e7956d6826a7107c490331e4505fc319010873c26392b0029";
String abi = "[{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]";
txid = PublicMethed.deployContractAndGetTransactionInfoById(contractName,abi,code,"",
maxFeeLimit, 0L, 100,null,contract013Key,contract013Address,blockingStubFull);
logger.info(txid);
infoById = PublicMethed.getTransactionInfoById(txid, blockingStubFull);
//logger.info("result is " + infoById.get().getResultValue());
Assert.assertTrue(infoById.get().getResultValue() == 0);
Assert.assertTrue(infoById.get().getReceipt().getEnergyUsageTotal() > 0);
Assert.assertFalse(infoById.get().getContractAddress().isEmpty());
}

@Test(enabled = true)
public void triggerTronTrxAndSunContract() {
AccountResourceMessage accountResource = PublicMethed.getAccountResource(contract013Address,
blockingStubFull);
Long energyLimit = accountResource.getEnergyLimit();
Long energyUsage = accountResource.getEnergyUsed();

logger.info("before energy limit is " + Long.toString(energyLimit));
logger.info("before energy usage is " + Long.toString(energyUsage));
Long maxFeeLimit = 5000000L;
String contractName = "TronTrxAndSunContract";
String code = "608060405234801561001057600080fd5b5061011a806100206000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806316ada547146044575b600080fd5b348015604f57600080fd5b506056606c565b6040518082815260200191505060405180910390f35b6000600180141515607c57600080fd5b603c80141515608a57600080fd5b610e1080141515609957600080fd5b620151808014151560a957600080fd5b62093a808014151560b957600080fd5b6301e133808014151560ca57600080fd5b620f42408014151560da57600080fd5b60018014151560e857600080fd5b429050905600a165627a7a72305820eacfee595582d9244a2fb5f052905bd240f87864fb8f602f85fd31fe3b89cda80029";
String abi = "[{\"constant\":false,\"inputs\":[],\"name\":\"time\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]";
contractAddress = PublicMethed.deployContract(contractName,abi,code,"",maxFeeLimit,
0L, 100,null,contract013Key,contract013Address,blockingStubFull);

txid = PublicMethed.triggerContract(contractAddress,
"time()", "#", false,
0, 100000000L, contract013Address, contract013Key, blockingStubFull);
logger.info(txid);
infoById = PublicMethed.getTransactionInfoById(txid, blockingStubFull);
logger.info("result is " + infoById.get().getResultValue());
Assert.assertTrue(infoById.get().getResultValue() == 0);
Assert.assertTrue(infoById.get().getReceipt().getEnergyUsageTotal() > 0);
Assert.assertTrue(infoById.get().getFee() == infoById.get().getReceipt().getEnergyFee());
Assert.assertFalse(infoById.get().getContractAddress().isEmpty());
}

@AfterClass
public void shutdown() throws InterruptedException {
if (channelFull != null) {
channelFull.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
}
}


Loading