Skip to content

Commit 4ea7f9a

Browse files
committed
Merge remote-tracking branch 'origin/develop' into new_resource_calc
# Conflicts: # src/main/java/org/tron/common/runtime/Runtime.java # src/main/java/org/tron/common/runtime/utils/MUtil.java # src/main/java/org/tron/common/runtime/vm/VM.java # src/main/java/org/tron/common/runtime/vm/program/Program.java
2 parents d41a820 + dfb2a47 commit 4ea7f9a

17 files changed

Lines changed: 961 additions & 96 deletions

‎README.md‎

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 align="center">
22
<br>
3-
<img width=20% src="https://raw.githubusercontent.com/tronprotocol/wiki/master/images/java-tron.png">
3+
<img width=20% src="https://github.com/tronprotocol/wiki/blob/master/images/java-tron.jpg?raw=true">
44
<br>
55
java-tron
66
<br>
@@ -62,6 +62,11 @@ TRON Protocol and the Tron Virtual Machine (TVM) allow anyone to develop decentr
6262

6363
* JDK 1.8 (JDK 1.9+ are not supported yet)
6464
* On Linux Ubuntu system (e.g. Ubuntu 16.04.4 LTS), ensure that the machine has [__Oracle JDK 8__](https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-ubuntu-16-04), instead of having __Open JDK 8__ in the system. If you are building the source code by using __Open JDK 8__, you will get [__Build Failed__](https://github.com/tronprotocol/java-tron/issues/337) result.
65+
* Open UDP port for connection to the network
66+
67+
## Build and Deploy automatically using scripts
68+
69+
- Please take a look at the [Tron Deployment Scripts](https://github.com/tronprotocol/TronDeployment) repository.
6570

6671
## Getting the code with git
6772

@@ -126,7 +131,7 @@ cd java-tron
126131

127132
* Build in [IntelliJ IDEA](https://www.jetbrains.com/idea/) (community version is enough):
128133

129-
** Please run ./gradlew build once to build the protocol files **
134+
**Please run ./gradlew build once to build the protocol files**
130135

131136
1. Start IntelliJ. Select `File` -> `Open`, then locate to the java-tron folder which you have git cloned to your local drive. Then click `Open` button on the right bottom.
132137
2. Check on `Use auto-import` on the `Import Project from Gradle` dialog. Select JDK 1.8 in the `Gradle JVM` option. Then click `OK`.
@@ -142,10 +147,54 @@ https://github.com/tronprotocol/Documentation/blob/master/TRX/Solidity_and_Full_
142147

143148
## Running a local node and connecting to the public testnet
144149

145-
* Ensure that the version number is consistent with the version number of the test network. If it is not consistent, please modify the node.p2p.version in the config.conf file and delete the out-directory directory (if it exists).
146-
* The current p2p.version is **619**
150+
* Use the [Testnet Config](https://github.com/tronprotocol/TronDeployment/blob/master/test_net_config.conf) or use the [Tron Deployment Scripts](https://github.com/tronprotocol/TronDeployment)
151+
152+
153+
### Running a Super Representative Node for mainnet
154+
155+
* Use the executable JAR(Recommended way)
156+
157+
```bash
158+
java -jar FullNode.jar -p your private key --witness -c your config.conf(Example:/data/java-tron/config.conf)
159+
Example:
160+
java -jar FullNode.jar -p 650950B193DDDDB35B6E48912DD28F7AB0E7140C1BFDEFD493348F02295BD812 --witness -c /data/java-tron/config.conf
161+
162+
```
163+
164+
This is similar to running a private testnet, except that the IPs in the `config.conf` are officially declared by TRON.
165+
166+
<details>
167+
<summary>Correct output</summary>
168+
169+
```bash
170+
171+
20:43:18.138 INFO [main] [o.t.p.FullNode](FullNode.java:21) Full node running.
172+
20:43:18.486 INFO [main] [o.t.c.c.a.Args](Args.java:429) Bind address wasn't set, Punching to identify it...
173+
20:43:18.493 INFO [main] [o.t.c.c.a.Args](Args.java:433) UDP local bound to: 10.0.8.146
174+
20:43:18.495 INFO [main] [o.t.c.c.a.Args](Args.java:448) External IP wasn't set, using checkip.amazonaws.com to identify it...
175+
20:43:19.450 INFO [main] [o.t.c.c.a.Args](Args.java:461) External address identified: 47.74.147.87
176+
20:43:19.599 INFO [main] [o.s.c.a.AnnotationConfigApplicationContext](AbstractApplicationContext.java:573) Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@124c278f: startup date [Fri Apr 27 20:43:19 CST 2018]; root of context hierarchy
177+
20:43:19.972 INFO [main] [o.s.b.f.a.AutowiredAnnotationBeanPostProcessor](AutowiredAnnotationBeanPostProcessor.java:153) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
178+
20:43:20.380 INFO [main] [o.t.c.d.DynamicPropertiesStore](DynamicPropertiesStore.java:244) update latest block header timestamp = 0
179+
20:43:20.383 INFO [main] [o.t.c.d.DynamicPropertiesStore](DynamicPropertiesStore.java:252) update latest block header number = 0
180+
20:43:20.393 INFO [main] [o.t.c.d.DynamicPropertiesStore](DynamicPropertiesStore.java:260) update latest block header id = 00
181+
20:43:20.394 INFO [main] [o.t.c.d.DynamicPropertiesStore](DynamicPropertiesStore.java:265) update state flag = 0
182+
20:43:20.559 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
183+
20:43:20.567 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
184+
20:43:20.568 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
185+
20:43:20.568 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
186+
20:43:20.569 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
187+
20:43:20.596 INFO [main] [o.t.c.d.Manager](Manager.java:300) create genesis block
188+
20:43:20.607 INFO [main] [o.t.c.d.Manager](Manager.java:306) save block: BlockCapsule
189+
190+
```
191+
192+
Then observe whether block synchronization success,If synchronization successfully explains the success of the super node
193+
194+
</details>
195+
147196

148-
### How to run a Super Node
197+
### Running a Super Representative Node for private testnet
149198
* use master branch
150199
* You should modify the config.conf
151200
1. Replace existing entry in genesis.block.witnesses with your address.
@@ -157,17 +206,11 @@ https://github.com/tronprotocol/Documentation/blob/master/TRX/Solidity_and_Full_
157206

158207
```bash
159208
cd build/libs
160-
java -jar java-tron.jar -p yourself private key --witness -c yourself config.conf(Example:/data/java-tron/config.conf)
209+
java -jar FullNode.jar -p your private key --witness -c your config.conf (Example:/data/java-tron/config.conf)
161210
Example:
162-
java -jar java-tron.jar -p 650950B193DDDDB35B6E48912DD28F7AB0E7140C1BFDEFD493348F02295BD812 --witness -c /data/java-tron/config.conf
211+
java -jar FullNode.jar -p 650950B193DDDDB35B6E48912DD28F7AB0E7140C1BFDEFD493348F02295BD812 --witness -c /data/java-tron/config.conf
163212

164213
```
165-
166-
* In the Terminal
167-
Un the config.conf localwitness add your private key.
168-
```bash
169-
./gradlew run -Pwitness
170-
```
171214

172215
<details>
173216
<summary>Show Output</summary>
@@ -286,50 +329,6 @@ In the `Program arguments` option, fill in `--witness`:
286329
287330
Then, run `FullNode::main()` again.
288331
289-
### Running a Super Node
290-
291-
* Use the executable JAR(Recommended way)
292-
293-
```bash
294-
cd build/libs
295-
java -jar java-tron.jar -p yourself private key --witness -c yourself config.conf(Example:/data/java-tron/config.conf)
296-
Example:
297-
java -jar java-tron.jar -p 650950B193DDDDB35B6E48912DD28F7AB0E7140C1BFDEFD493348F02295BD812 --witness -c /data/java-tron/config.conf
298-
299-
```
300-
301-
This is similar to running a private testnet, except that the IPs in the `config.conf` are officially declared by TRON.
302-
303-
<details>
304-
<summary>Correct output</summary>
305-
306-
```bash
307-
308-
20:43:18.138 INFO [main] [o.t.p.FullNode](FullNode.java:21) Full node running.
309-
20:43:18.486 INFO [main] [o.t.c.c.a.Args](Args.java:429) Bind address wasn't set, Punching to identify it...
310-
20:43:18.493 INFO [main] [o.t.c.c.a.Args](Args.java:433) UDP local bound to: 10.0.8.146
311-
20:43:18.495 INFO [main] [o.t.c.c.a.Args](Args.java:448) External IP wasn't set, using checkip.amazonaws.com to identify it...
312-
20:43:19.450 INFO [main] [o.t.c.c.a.Args](Args.java:461) External address identified: 47.74.147.87
313-
20:43:19.599 INFO [main] [o.s.c.a.AnnotationConfigApplicationContext](AbstractApplicationContext.java:573) Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@124c278f: startup date [Fri Apr 27 20:43:19 CST 2018]; root of context hierarchy
314-
20:43:19.972 INFO [main] [o.s.b.f.a.AutowiredAnnotationBeanPostProcessor](AutowiredAnnotationBeanPostProcessor.java:153) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
315-
20:43:20.380 INFO [main] [o.t.c.d.DynamicPropertiesStore](DynamicPropertiesStore.java:244) update latest block header timestamp = 0
316-
20:43:20.383 INFO [main] [o.t.c.d.DynamicPropertiesStore](DynamicPropertiesStore.java:252) update latest block header number = 0
317-
20:43:20.393 INFO [main] [o.t.c.d.DynamicPropertiesStore](DynamicPropertiesStore.java:260) update latest block header id = 00
318-
20:43:20.394 INFO [main] [o.t.c.d.DynamicPropertiesStore](DynamicPropertiesStore.java:265) update state flag = 0
319-
20:43:20.559 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
320-
20:43:20.567 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
321-
20:43:20.568 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
322-
20:43:20.568 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
323-
20:43:20.569 INFO [main] [o.t.c.c.TransactionCapsule](TransactionCapsule.java:83) Transaction create succeeded!
324-
20:43:20.596 INFO [main] [o.t.c.d.Manager](Manager.java:300) create genesis block
325-
20:43:20.607 INFO [main] [o.t.c.d.Manager](Manager.java:306) save block: BlockCapsule
326-
327-
```
328-
329-
Then observe whether block synchronization success,If synchronization successfully explains the success of the super node
330-
331-
</details>
332-
333332
# Quick Start
334333
335334
Read the [Quick Start](http://wiki.tron.network/en/latest/The_TRON_Network.html#how-to-build).

‎src/main/java/org/tron/common/runtime/Runtime.java‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static com.google.common.primitives.Longs.max;
44
import static com.google.common.primitives.Longs.min;
55
import static org.apache.commons.lang3.ArrayUtils.isEmpty;
6+
import static org.tron.common.runtime.utils.MUtil.transfer;
67
import static org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType.ET_CONSTANT_TYPE;
78
import static org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType.ET_NORMAL_TYPE;
89
import static org.tron.common.runtime.vm.program.InternalTransaction.ExecutorType.ET_PRE_TYPE;
@@ -127,6 +128,7 @@ public Runtime(TransactionTrace trace, Block block, Deposit deosit,
127128
/**
128129
* For pre trx run
129130
*/
131+
@Deprecated
130132
public Runtime(Transaction tx, DepositImpl deposit, ProgramInvokeFactory programInvokeFactory) {
131133
this.trx = tx;
132134
this.deposit = deposit;
@@ -149,6 +151,7 @@ public Runtime(Transaction tx, DepositImpl deposit, ProgramInvokeFactory program
149151
/**
150152
* For constant trx
151153
*/
154+
@Deprecated
152155
public Runtime(Transaction tx, ProgramInvokeFactory programInvokeFactory, Deposit deposit) {
153156
trx = tx;
154157
this.deposit = deposit;
@@ -159,6 +162,29 @@ public Runtime(Transaction tx, ProgramInvokeFactory programInvokeFactory, Deposi
159162
}
160163

161164

165+
/**
166+
* For constant trx with latest block.
167+
*/
168+
public Runtime(Transaction tx, Block block, DepositImpl deposit, ProgramInvokeFactory programInvokeFactory) {
169+
this.trx = tx;
170+
this.deposit = deposit;
171+
this.programInvokeFactory = programInvokeFactory;
172+
this.executorType = ET_PRE_TYPE;
173+
this.block = block;
174+
Transaction.Contract.ContractType contractType = tx.getRawData().getContract(0).getType();
175+
switch (contractType.getNumber()) {
176+
case Transaction.Contract.ContractType.TriggerSmartContract_VALUE:
177+
trxType = TRX_CONTRACT_CALL_TYPE;
178+
break;
179+
case Transaction.Contract.ContractType.CreateSmartContract_VALUE:
180+
trxType = TRX_CONTRACT_CREATION_TYPE;
181+
break;
182+
default:
183+
trxType = TRX_PRECOMPILED_TYPE;
184+
}
185+
}
186+
187+
162188
public void precompiled() throws ContractValidateException, ContractExeException {
163189
TransactionCapsule trxCap = new TransactionCapsule(trx);
164190
final List<Actuator> actuatorList = ActuatorFactory
@@ -344,7 +370,7 @@ private long getGasLimit(AccountCapsule creator, AccountCapsule caller,
344370
/*
345371
**/
346372
private void create()
347-
throws ContractExeException {
373+
throws ContractExeException, ContractValidateException {
348374
CreateSmartContract contract = ContractCapsule.getSmartContractFromTransaction(trx);
349375
SmartContract newSmartContract = contract.getNewContract();
350376

@@ -419,16 +445,16 @@ private void create()
419445
byte[] callerAddress = contract.getOwnerAddress().toByteArray();
420446
long callValue = newSmartContract.getCallValue();
421447
if (callValue != 0) {
422-
this.deposit.addBalance(callerAddress, -callValue);
423-
this.deposit.addBalance(contractAddress, callValue);
448+
transfer(this.deposit,callerAddress,callerAddress,callValue);
424449
}
425450

426451
}
427452

428453
/**
429454
* **
430455
*/
431-
private void call() {
456+
private void call()
457+
throws ContractValidateException {
432458
Contract.TriggerSmartContract contract = ContractCapsule.getTriggerContractFromTransaction(trx);
433459
if (contract == null) {
434460
return;
@@ -469,8 +495,7 @@ private void call() {
469495
byte[] callerAddress = contract.getOwnerAddress().toByteArray();
470496
long callValue = contract.getCallValue();
471497
if (0 != callValue) {
472-
this.deposit.addBalance(callerAddress, -callValue);
473-
this.deposit.addBalance(contractAddress, callValue);
498+
transfer(this.deposit,callerAddress,contractAddress,callValue);
474499
}
475500

476501
}

‎src/main/java/org/tron/common/runtime/utils/MUtil.java‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
package org.tron.common.runtime.utils;
22

3+
import com.google.protobuf.ByteString;
34
import java.util.Arrays;
45
import org.spongycastle.util.encoders.Hex;
56
import org.tron.common.crypto.Hash;
67
import org.tron.common.storage.Deposit;
78
import org.tron.core.Wallet;
9+
import org.tron.core.actuator.Actuator;
10+
import org.tron.core.actuator.ActuatorFactory;
11+
import org.tron.core.capsule.TransactionCapsule;
12+
import org.tron.core.exception.ContractExeException;
13+
import org.tron.core.exception.ContractValidateException;
14+
import org.tron.protos.Contract;
15+
import org.tron.protos.Contract.TransferContract;
16+
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
817

918
/**
1019
* @author Guo Yonggang
1120
* @since 02.05.2018
1221
*/
1322
public class MUtil {
1423

15-
public static void transfer(Deposit deposit, byte[] fromAddress, byte[] toAddress, long amount) {
24+
public static void transfer(Deposit deposit, byte[] fromAddress, byte[] toAddress, long amount)
25+
throws ContractValidateException {
26+
transferValidate(deposit,fromAddress,toAddress,amount);
1627
if (deposit.getBalance(fromAddress) < amount) {
1728
throw new RuntimeException(
1829
Hex.toHexString(fromAddress).toUpperCase() + " not enough balance!");
@@ -24,6 +35,19 @@ public static void transfer(Deposit deposit, byte[] fromAddress, byte[] toAddres
2435
deposit.addBalance(fromAddress, -amount);
2536
}
2637

38+
public static void transferValidate(Deposit deposit, byte[] fromAddress, byte[] toAddress, long amount)
39+
throws ContractValidateException {
40+
Contract.TransferContract.Builder builder = Contract.TransferContract.newBuilder();
41+
builder.setAmount(amount);
42+
builder.setOwnerAddress(ByteString.copyFrom(fromAddress));
43+
builder.setToAddress(ByteString.copyFrom(toAddress));
44+
TransferContract contract = builder.build();
45+
TransactionCapsule trx = new TransactionCapsule(contract,
46+
ContractType.TransferContract);
47+
Actuator actuator = ActuatorFactory.createActuator(trx ,deposit.getDbManager()).get(0);
48+
actuator.validate();
49+
}
50+
2751
public static void burn(Deposit deposit, byte[] address, long amount) {
2852
if (deposit.getBalance(address) < amount) {
2953
throw new RuntimeException("Not enough balance!");

‎src/main/java/org/tron/common/runtime/vm/VM.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.tron.common.runtime.config.SystemProperties;
1717
import org.tron.common.runtime.vm.program.Program;
1818
import org.tron.common.runtime.vm.program.Stack;
19+
import org.tron.core.exception.ContractExeException;
20+
import org.tron.core.exception.ContractValidateException;
1921

2022
public class VM {
2123

@@ -90,7 +92,8 @@ private long calcMemGas(GasCost gasCosts, long oldMemSize, BigInteger newMemSize
9092
return gasCost;
9193
}
9294

93-
public void step(Program program) {
95+
public void step(Program program)
96+
throws ContractValidateException {
9497
if (vmTrace) {
9598
program.saveOpTrace();
9699
}
@@ -1332,7 +1335,8 @@ public void step(Program program) {
13321335
}
13331336
}
13341337

1335-
public void play(Program program) {
1338+
public void play(Program program)
1339+
throws ContractValidateException {
13361340
try {
13371341
if (program.byTestingSuite()) {
13381342
return;

0 commit comments

Comments
 (0)