17
17
import org .apache .commons .lang3 .StringUtils ;
18
18
import org .tron .core .Wallet ;
19
19
import org .tron .core .capsule .BlockCapsule ;
20
- import org .tron .core .config .Parameter ;
21
20
import org .tron .core .config .Parameter .ForkBlockVersionConsts ;
22
21
import org .tron .core .db .Manager ;
23
22
24
23
@ Slf4j
25
24
@ NoArgsConstructor (access = AccessLevel .PRIVATE )
26
25
public class ForkController {
27
26
27
+ private static final byte VERSION_UPGRADE = (byte ) 1 ;
28
+ private static final byte HARD_FORK_EFFECTIVE = (byte ) 2 ;
28
29
private static final byte [] check ;
30
+ private static final byte [] check2 ;
29
31
static {
30
32
check = new byte [1024 ];
31
- Arrays .fill (check , (byte ) 1 );
33
+ Arrays .fill (check , VERSION_UPGRADE );
34
+ check2 = new byte [1024 ];
35
+ Arrays .fill (check2 , HARD_FORK_EFFECTIVE );
32
36
}
33
37
34
38
@ Getter
@@ -42,7 +46,7 @@ public void init(Manager manager) {
42
46
}
43
47
44
48
public synchronized boolean pass (int version ) {
45
- if (!check (version )) {
49
+ if (!checkEnergy (version )) {
46
50
return false ;
47
51
}
48
52
@@ -51,14 +55,23 @@ public synchronized boolean pass(int version) {
51
55
}
52
56
53
57
byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
54
- boolean pass = check (stats );
58
+ boolean pass ;
59
+ if (version == ForkBlockVersionConsts .ENERGY_LIMIT ) {
60
+ pass = check (stats );
61
+ } else {
62
+ pass = check2 (stats );
63
+ }
64
+
55
65
if (pass ) {
56
66
passSet .add (version );
57
67
}
58
68
return pass ;
59
69
}
60
70
61
- private boolean check (int version ) {
71
+ // when block.version = 5,
72
+ // it make block use new energy to handle transaction when block number >= 4727890L.
73
+ // version !=5, skip this.
74
+ private boolean checkEnergy (int version ) {
62
75
if (version != ForkBlockVersionConsts .ENERGY_LIMIT ) {
63
76
return true ;
64
77
}
@@ -68,6 +81,14 @@ private boolean check(int version) {
68
81
}
69
82
70
83
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 ) {
71
92
if (stats == null || stats .length == 0 ) {
72
93
return false ;
73
94
}
@@ -95,16 +116,15 @@ public synchronized void update(BlockCapsule blockCapsule) {
95
116
}
96
117
97
118
byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
98
- if (check (stats )) {
99
- passSet .add (version );
119
+ if (check (stats ) || check2 (stats )) {
100
120
return ;
101
121
}
102
122
103
123
if (stats == null ) {
104
124
stats = new byte [witnesses .size ()];
105
125
}
106
126
107
- stats [slot ] = ( byte ) 1 ;
127
+ stats [slot ] = VERSION_UPGRADE ;
108
128
manager .getDynamicPropertiesStore ().statsByVersion (version , stats );
109
129
logger .info (
110
130
"*******update hard fork:{}, witness size:{}, solt:{}, witness:{}, version:{}" ,
@@ -118,15 +138,32 @@ public synchronized void update(BlockCapsule blockCapsule) {
118
138
version );
119
139
}
120
140
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
+
121
159
public synchronized void reset (BlockCapsule blockCapsule ) {
122
160
int version = blockCapsule .getInstance ().getBlockHeader ().getRawData ().getVersion ();
123
161
if (version < ForkBlockVersionConsts .ENERGY_LIMIT || passSet .contains (version )) {
124
162
return ;
125
163
}
126
164
127
165
byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
128
- if (check (stats )) {
129
- passSet .add (version );
166
+ if (check (stats ) || check2 (stats )) {
130
167
return ;
131
168
}
132
169
0 commit comments