Skip to content

Commit 9f8f90d

Browse files
committed
modify the hard fork
1 parent 742f321 commit 9f8f90d

File tree

3 files changed

+76
-17
lines changed

3 files changed

+76
-17
lines changed

chainbase/src/main/java/org/tron/common/utils/ForkUtils.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ public boolean pass(ForkBlockVersionEnum forkBlockVersionEnum) {
3737
}
3838

3939
public synchronized boolean pass(int version) {
40+
if (version > ForkBlockVersionEnum.VERSION_4_0.getValue()) {
41+
return passNew(version);
42+
} else {
43+
return passOld(version);
44+
}
45+
}
46+
47+
private boolean passOld(int version) {
4048
if (version == ForkBlockVersionConsts.ENERGY_LIMIT) {
4149
return checkForEnergyLimit();
4250
}
@@ -45,6 +53,40 @@ public synchronized boolean pass(int version) {
4553
return check(stats);
4654
}
4755

56+
private boolean passNew(int version) {
57+
ForkBlockVersionEnum versionEnum = ForkBlockVersionEnum.getForkBlockVersionEnum(version);
58+
if (versionEnum == null) {
59+
logger.error("not exist block version: {}", version);
60+
return false;
61+
}
62+
long latestBlockTime = dynamicPropertiesStore.getLatestBlockHeaderTimestamp();
63+
long maintenanceTimeInterval = dynamicPropertiesStore.getMaintenanceTimeInterval();
64+
long hardForkTime = ((versionEnum.getHardForkTime() - 1) / maintenanceTimeInterval + 1)
65+
* maintenanceTimeInterval;
66+
if (latestBlockTime < hardForkTime) {
67+
return false;
68+
}
69+
Boolean hardForkResult = dynamicPropertiesStore.getForked(version);
70+
if (hardForkResult != null) {
71+
return hardForkResult;
72+
}
73+
byte[] stats = dynamicPropertiesStore.statsByVersion(version);
74+
if (stats == null || stats.length == 0) {
75+
return false;
76+
}
77+
int count = 0;
78+
for (int i = 0; i < stats.length; i++) {
79+
if (check[i] == stats[i]) {
80+
++count;
81+
}
82+
}
83+
hardForkResult = count >= versionEnum.getHardForkCount();
84+
dynamicPropertiesStore.forked(version, hardForkResult);
85+
logger.info("******** update block version {} hard fork result:{} ********", version,
86+
hardForkResult);
87+
return hardForkResult;
88+
}
89+
4890
// when block.version = 5,
4991
// it make block use new energy to handle transaction when block number >= 4727890L.
5092
// version !=5, skip this.

chainbase/src/main/java/org/tron/core/config/args/Parameter.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,34 @@
55
public class Parameter {
66

77
public enum ForkBlockVersionEnum {
8-
ENERGY_LIMIT(5),
9-
VERSION_3_2_2(6),
10-
VERSION_3_5(7),
11-
VERSION_3_6(8),
12-
VERSION_3_6_5(9),
13-
VERSION_3_6_6(10),
14-
VERSION_4_0(16);
8+
ENERGY_LIMIT(5, 0L, 0),
9+
VERSION_3_2_2(6, 0L, 0),
10+
VERSION_3_5(7, 0L, 0),
11+
VERSION_3_6(8, 0L, 0),
12+
VERSION_3_6_5(9, 0L, 0),
13+
VERSION_3_6_6(10, 0L, 0),
14+
VERSION_4_0(16, 0L, 0);
1515

1616
@Getter
1717
private int value;
18+
@Getter
19+
private long hardForkTime;
20+
@Getter
21+
private int hardForkCount;
1822

19-
ForkBlockVersionEnum(int value) {
23+
ForkBlockVersionEnum(int value, long hardForkTime, int hardForkCount) {
2024
this.value = value;
25+
this.hardForkTime = hardForkTime;
26+
this.hardForkCount = hardForkCount;
27+
}
28+
29+
public static ForkBlockVersionEnum getForkBlockVersionEnum(int value) {
30+
for (ForkBlockVersionEnum versionEnum : values()) {
31+
if (versionEnum.getValue() == value) {
32+
return versionEnum;
33+
}
34+
}
35+
return null;
2136
}
2237
}
2338

chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
9191
private static final byte[] TOTAL_STORAGE_TAX = "TOTAL_STORAGE_TAX".getBytes();
9292
private static final byte[] TOTAL_STORAGE_RESERVED = "TOTAL_STORAGE_RESERVED".getBytes();
9393
private static final byte[] STORAGE_EXCHANGE_TAX_RATE = "STORAGE_EXCHANGE_TAX_RATE".getBytes();
94-
private static final byte[] FORK_CONTROLLER = "FORK_CONTROLLER".getBytes();
94+
private static final String FORK_CONTROLLER = "FORK_CONTROLLER";
9595
private static final String FORK_PREFIX = "FORK_VERSION_";
9696
//This value is only allowed to be 0, 1, -1
9797
private static final byte[] REMOVE_THE_POWER_OF_THE_GR = "REMOVE_THE_POWER_OF_THE_GR".getBytes();
@@ -121,7 +121,7 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
121121
private static final byte[] ALLOW_SHIELDED_TRANSACTION = "ALLOW_SHIELDED_TRANSACTION".getBytes();
122122
private static final byte[] ALLOW_SHIELDED_TRC20_TRANSACTION =
123123
"ALLOW_SHIELDED_TRC20_TRANSACTION"
124-
.getBytes();
124+
.getBytes();
125125
private static final byte[] ALLOW_TVM_CONSTANTINOPLE = "ALLOW_TVM_CONSTANTINOPLE".getBytes();
126126
private static final byte[] ALLOW_TVM_SOLIDITY_059 = "ALLOW_TVM_SOLIDITY_059".getBytes();
127127
private static final byte[] FORBID_TRANSFER_TO_CONTRACT = "FORBID_TRANSFER_TO_CONTRACT"
@@ -1767,8 +1767,9 @@ public void addTotalTransactionCost(long fee) {
17671767
saveTotalTransactionCost(newValue);
17681768
}
17691769

1770-
public void forked() {
1771-
put(FORK_CONTROLLER, new BytesCapsule(Boolean.toString(true).getBytes()));
1770+
public void forked(int version, boolean value) {
1771+
String forkKey = FORK_CONTROLLER + version;
1772+
put(forkKey.getBytes(), new BytesCapsule(Boolean.toString(value).getBytes()));
17721773
}
17731774

17741775
public void statsByVersion(int version, byte[] stats) {
@@ -1781,9 +1782,10 @@ public byte[] statsByVersion(int version) {
17811782
return revokingDB.getUnchecked(statsKey.getBytes());
17821783
}
17831784

1784-
public boolean getForked() {
1785-
byte[] value = revokingDB.getUnchecked(FORK_CONTROLLER);
1786-
return value == null ? Boolean.FALSE : Boolean.valueOf(new String(value));
1785+
public Boolean getForked(int version) {
1786+
String forkKey = FORK_CONTROLLER + version;
1787+
byte[] value = revokingDB.getUnchecked(forkKey.getBytes());
1788+
return value == null ? null : Boolean.valueOf(new String(value));
17871789
}
17881790

17891791
/**
@@ -1871,10 +1873,10 @@ private static class DynamicResourceProperties {
18711873
private static final byte[] BLOCK_ENERGY_USAGE = "BLOCK_ENERGY_USAGE".getBytes();
18721874
private static final byte[] ADAPTIVE_RESOURCE_LIMIT_MULTIPLIER =
18731875
"ADAPTIVE_RESOURCE_LIMIT_MULTIPLIER"
1874-
.getBytes();
1876+
.getBytes();
18751877
private static final byte[] ADAPTIVE_RESOURCE_LIMIT_TARGET_RATIO =
18761878
"ADAPTIVE_RESOURCE_LIMIT_TARGET_RATIO"
1877-
.getBytes();
1879+
.getBytes();
18781880
}
18791881

18801882
}

0 commit comments

Comments
 (0)