Skip to content

Commit df0d14c

Browse files
authored
Merge pull request #1849 from tronprotocol/hard_fork_in_maintenance
Hard fork in maintenance
2 parents 5adbe5f + e5ad6ca commit df0d14c

File tree

4 files changed

+59
-88
lines changed

4 files changed

+59
-88
lines changed

src/main/java/org/tron/common/utils/ForkController.java

Lines changed: 38 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
import com.google.common.collect.Streams;
55
import com.google.protobuf.ByteString;
66
import java.util.Arrays;
7-
import java.util.HashSet;
87
import java.util.List;
9-
import java.util.Set;
8+
import java.util.Objects;
109
import java.util.stream.Collectors;
1110
import java.util.stream.Stream;
1211
import lombok.AccessLevel;
@@ -18,77 +17,51 @@
1817
import org.tron.core.Wallet;
1918
import org.tron.core.capsule.BlockCapsule;
2019
import org.tron.core.config.Parameter.ForkBlockVersionConsts;
20+
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
2121
import org.tron.core.db.Manager;
2222

2323
@Slf4j
2424
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2525
public class ForkController {
2626

27+
private static final byte VERSION_DOWNGRADE = (byte) 0;
2728
private static final byte VERSION_UPGRADE = (byte) 1;
28-
private static final byte HARD_FORK_EFFECTIVE = (byte) 2;
2929
private static final byte[] check;
30-
private static final byte[] check2;
30+
3131
static {
3232
check = new byte[1024];
3333
Arrays.fill(check, VERSION_UPGRADE);
34-
check2 = new byte[1024];
35-
Arrays.fill(check2, HARD_FORK_EFFECTIVE);
3634
}
3735

3836
@Getter
3937
private Manager manager;
4038

41-
private Set<Integer> passSet = new HashSet<>();
42-
4339
public void init(Manager manager) {
4440
this.manager = manager;
45-
passSet.clear();
4641
}
4742

48-
public synchronized boolean pass(int version) {
49-
if (!checkEnergy(version)) {
50-
return false;
51-
}
52-
53-
if (passSet.contains(version)) {
54-
return true;
55-
}
43+
public boolean pass(ForkBlockVersionEnum forkBlockVersionEnum) {
44+
return pass(forkBlockVersionEnum.getValue());
45+
}
5646

57-
byte[] stats = manager.getDynamicPropertiesStore().statsByVersion(version);
58-
boolean pass;
47+
public synchronized boolean pass(int version) {
5948
if (version == ForkBlockVersionConsts.ENERGY_LIMIT) {
60-
pass = check(stats);
61-
} else {
62-
pass = check2(stats);
49+
return checkForEnergyLimit();
6350
}
6451

65-
if (pass) {
66-
passSet.add(version);
67-
}
68-
return pass;
52+
byte[] stats = manager.getDynamicPropertiesStore().statsByVersion(version);
53+
return check(stats);
6954
}
7055

7156
// when block.version = 5,
7257
// it make block use new energy to handle transaction when block number >= 4727890L.
7358
// version !=5, skip this.
74-
private boolean checkEnergy(int version) {
75-
if (version != ForkBlockVersionConsts.ENERGY_LIMIT) {
76-
return true;
77-
}
78-
59+
private boolean checkForEnergyLimit() {
7960
long blockNum = manager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
8061
return blockNum >= 4727890L;
8162
}
8263

8364
private boolean check(byte[] stats) {
84-
return check(check, stats);
85-
}
86-
87-
private boolean check2(byte[] stats) {
88-
return check(check2, stats);
89-
}
90-
91-
private boolean check(byte[] check, byte[] stats) {
9265
if (stats == null || stats.length == 0) {
9366
return false;
9467
}
@@ -102,6 +75,19 @@ private boolean check(byte[] check, byte[] stats) {
10275
return true;
10376
}
10477

78+
private void downgrade(int version, int slot) {
79+
for (ForkBlockVersionEnum versionEnum : ForkBlockVersionEnum.values()) {
80+
int versionValue = versionEnum.getValue();
81+
if (versionValue > version) {
82+
byte[] stats = manager.getDynamicPropertiesStore().statsByVersion(versionValue);
83+
if (!check(stats) && Objects.nonNull(stats)) {
84+
stats[slot] = VERSION_DOWNGRADE;
85+
manager.getDynamicPropertiesStore().statsByVersion(versionValue, stats);
86+
}
87+
}
88+
}
89+
}
90+
10591
public synchronized void update(BlockCapsule blockCapsule) {
10692
List<ByteString> witnesses = manager.getWitnessController().getActiveWitnesses();
10793
ByteString witness = blockCapsule.getWitnessAddress();
@@ -111,16 +97,18 @@ public synchronized void update(BlockCapsule blockCapsule) {
11197
}
11298

11399
int version = blockCapsule.getInstance().getBlockHeader().getRawData().getVersion();
114-
if (version < ForkBlockVersionConsts.ENERGY_LIMIT || passSet.contains(version)) {
100+
if (version < ForkBlockVersionConsts.ENERGY_LIMIT) {
115101
return;
116102
}
117103

104+
downgrade(version, slot);
105+
118106
byte[] stats = manager.getDynamicPropertiesStore().statsByVersion(version);
119-
if (check(stats) || check2(stats)) {
107+
if (check(stats)) {
120108
return;
121109
}
122110

123-
if (stats == null) {
111+
if (Objects.isNull(stats) || stats.length != witnesses.size()) {
124112
stats = new byte[witnesses.size()];
125113
}
126114

@@ -138,38 +126,14 @@ public synchronized void update(BlockCapsule blockCapsule) {
138126
version);
139127
}
140128

141-
public synchronized void updateWhenMaintenance(BlockCapsule blockCapsule) {
142-
int version = blockCapsule.getInstance().getBlockHeader().getRawData().getVersion();
143-
if (version < ForkBlockVersionConsts.VERSION_3_2_2 || passSet.contains(version)) {
144-
return;
145-
}
146-
147-
byte[] stats = manager.getDynamicPropertiesStore().statsByVersion(version);
148-
if (check2(stats)) {
149-
return;
150-
}
151-
152-
if (check(stats)) {
153-
Arrays.fill(stats, HARD_FORK_EFFECTIVE);
154-
manager.getDynamicPropertiesStore().statsByVersion(version, stats);
155-
logger.info("*******hard fork is effective in the maintenance, version is {}", version);
156-
}
157-
}
158-
159-
public synchronized void reset(BlockCapsule blockCapsule) {
160-
int version = blockCapsule.getInstance().getBlockHeader().getRawData().getVersion();
161-
if (version < ForkBlockVersionConsts.ENERGY_LIMIT || passSet.contains(version)) {
162-
return;
163-
}
164-
165-
byte[] stats = manager.getDynamicPropertiesStore().statsByVersion(version);
166-
if (check(stats) || check2(stats)) {
167-
return;
168-
}
169-
170-
if (stats != null) {
171-
Arrays.fill(stats, (byte) 0);
172-
manager.getDynamicPropertiesStore().statsByVersion(version, stats);
129+
public synchronized void reset() {
130+
for (ForkBlockVersionEnum versionEnum : ForkBlockVersionEnum.values()) {
131+
int versionValue = versionEnum.getValue();
132+
byte[] stats = manager.getDynamicPropertiesStore().statsByVersion(versionValue);
133+
if (!check(stats) && Objects.nonNull(stats)) {
134+
Arrays.fill(stats, VERSION_DOWNGRADE);
135+
manager.getDynamicPropertiesStore().statsByVersion(versionValue, stats);
136+
}
173137
}
174138
}
175139

src/main/java/org/tron/core/actuator/ProposalCreateActuator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.tron.core.capsule.TransactionResultCapsule;
1717
import org.tron.core.config.Parameter.ChainParameters;
1818
import org.tron.core.config.Parameter.ForkBlockVersionConsts;
19+
import org.tron.core.config.Parameter.ForkBlockVersionEnum;
1920
import org.tron.core.config.args.Args;
2021
import org.tron.core.db.Manager;
2122
import org.tron.core.exception.ContractExeException;
@@ -219,7 +220,7 @@ private void validateValue(Map.Entry<Long, Long> entry) throws ContractValidateE
219220
if (!dbManager.getForkController().pass(ForkBlockVersionConsts.ENERGY_LIMIT)) {
220221
throw new ContractValidateException("Bad chain parameter id");
221222
}
222-
if (dbManager.getForkController().pass(ForkBlockVersionConsts.VERSION_3_2_2)) {
223+
if (dbManager.getForkController().pass(ForkBlockVersionEnum.VERSION_3_2_2)) {
223224
throw new ContractValidateException("Bad chain parameter id");
224225
}
225226
if (entry.getValue() < 0 || entry.getValue() > 100_000_000_000_000_000L) {
@@ -243,7 +244,7 @@ private void validateValue(Map.Entry<Long, Long> entry) throws ContractValidateE
243244
break;
244245
}
245246
case (19): {
246-
if (!dbManager.getForkController().pass(ForkBlockVersionConsts.VERSION_3_2_2)) {
247+
if (!dbManager.getForkController().pass(ForkBlockVersionEnum.VERSION_3_2_2)) {
247248
throw new ContractValidateException("Bad chain parameter id");
248249
}
249250
if (entry.getValue() < 0 || entry.getValue() > 100_000_000_000_000_000L) {

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.tron.core.config;
22

3+
import lombok.Getter;
4+
35
public interface Parameter {
46

57
interface ChainConstant {
@@ -104,11 +106,22 @@ enum ChainParameters {
104106
// EXCHANGE_BALANCE_LIMIT,
105107
}
106108

109+
@Deprecated
107110
interface ForkBlockVersionConsts {
108111

109112
int START_NEW_TRANSACTION = 4;
110113
int ENERGY_LIMIT = 5;
111-
int VERSION_3_2_2 = 6;
114+
}
115+
116+
enum ForkBlockVersionEnum {
117+
VERSION_3_2_2(6);
118+
119+
@Getter
120+
private int value;
121+
122+
ForkBlockVersionEnum(int value) {
123+
this.value = value;
124+
}
112125
}
113126

114127
}

src/main/java/org/tron/core/db/Manager.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ private void applyBlock(BlockCapsule block) throws ContractValidateException,
678678
processBlock(block);
679679
this.blockStore.put(block.getBlockId().getBytes(), block);
680680
this.blockIndexStore.put(block.getBlockId());
681-
updateFork();
681+
updateFork(block);
682682
if (System.currentTimeMillis() - block.getTimeStamp() >= 60_000) {
683683
revokingStore.setMaxFlushCount(SnapshotManager.DEFAULT_MAX_FLUSH_COUNT);
684684
} else {
@@ -1363,14 +1363,8 @@ public void updateLatestSolidifiedBlock() {
13631363
logger.info("update solid block, num = {}", latestSolidifiedBlockNum);
13641364
}
13651365

1366-
public void updateFork() {
1367-
try {
1368-
long latestSolidifiedBlockNum = dynamicPropertiesStore.getLatestSolidifiedBlockNum();
1369-
BlockCapsule solidifiedBlock = getBlockByNum(latestSolidifiedBlockNum);
1370-
forkController.update(solidifiedBlock);
1371-
} catch (ItemNotFoundException | BadItemException e) {
1372-
logger.error("solidified block not found");
1373-
}
1366+
public void updateFork(BlockCapsule block) {
1367+
forkController.update(block);
13741368
}
13751369

13761370
public long getSyncBeginNumber() {
@@ -1405,8 +1399,7 @@ private void processMaintenance(BlockCapsule block) {
14051399
proposalController.processProposals();
14061400
witnessController.updateWitness();
14071401
this.dynamicPropertiesStore.updateNextMaintenanceTime(block.getTimeStamp());
1408-
forkController.updateWhenMaintenance(block);
1409-
forkController.reset(block);
1402+
forkController.reset();
14101403
}
14111404

14121405
/**

0 commit comments

Comments
 (0)