4
4
import com .google .common .collect .Streams ;
5
5
import com .google .protobuf .ByteString ;
6
6
import java .util .Arrays ;
7
- import java .util .HashSet ;
8
7
import java .util .List ;
9
- import java .util .Set ;
8
+ import java .util .Objects ;
10
9
import java .util .stream .Collectors ;
11
10
import java .util .stream .Stream ;
12
11
import lombok .AccessLevel ;
@@ -27,75 +26,42 @@ public class ForkController {
27
26
28
27
private static final byte VERSION_DOWNGRADE = (byte ) 0 ;
29
28
private static final byte VERSION_UPGRADE = (byte ) 1 ;
30
- private static final byte HARD_FORK_EFFECTIVE = (byte ) 2 ;
31
29
private static final byte [] check ;
32
- private static final byte [] check2 ;
33
30
34
31
static {
35
32
check = new byte [1024 ];
36
33
Arrays .fill (check , VERSION_UPGRADE );
37
- check2 = new byte [1024 ];
38
- Arrays .fill (check2 , HARD_FORK_EFFECTIVE );
39
34
}
40
35
41
36
@ Getter
42
37
private Manager manager ;
43
38
44
- private Set <Integer > passSet = new HashSet <>();
45
-
46
39
public void init (Manager manager ) {
47
40
this .manager = manager ;
48
- passSet .clear ();
49
41
}
50
42
51
43
public boolean pass (ForkBlockVersionEnum forkBlockVersionEnum ) {
52
44
return pass (forkBlockVersionEnum .getValue ());
53
45
}
54
46
55
47
public synchronized boolean pass (int version ) {
56
- if (!checkEnergy (version )) {
57
- return false ;
58
- }
59
-
60
- if (passSet .contains (version )) {
61
- return true ;
62
- }
63
-
64
- byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
65
- boolean pass ;
66
48
if (version == ForkBlockVersionConsts .ENERGY_LIMIT ) {
67
- pass = check (stats );
68
- } else {
69
- pass = check2 (stats );
49
+ return checkForEnergyLimit ();
70
50
}
71
51
72
- if (pass ) {
73
- passSet .add (version );
74
- }
75
- return pass ;
52
+ byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
53
+ return check (stats );
76
54
}
77
55
78
56
// when block.version = 5,
79
57
// it make block use new energy to handle transaction when block number >= 4727890L.
80
58
// version !=5, skip this.
81
- private boolean checkEnergy (int version ) {
82
- if (version != ForkBlockVersionConsts .ENERGY_LIMIT ) {
83
- return true ;
84
- }
85
-
59
+ private boolean checkForEnergyLimit () {
86
60
long blockNum = manager .getDynamicPropertiesStore ().getLatestBlockHeaderNumber ();
87
61
return blockNum >= 4727890L ;
88
62
}
89
63
90
64
private boolean check (byte [] stats ) {
91
- return check (check , stats );
92
- }
93
-
94
- private boolean check2 (byte [] stats ) {
95
- return check (check2 , stats );
96
- }
97
-
98
- private boolean check (byte [] check , byte [] stats ) {
99
65
if (stats == null || stats .length == 0 ) {
100
66
return false ;
101
67
}
@@ -113,7 +79,7 @@ private void downgrade(int version, int slot) {
113
79
for (ForkBlockVersionEnum versionEnum : ForkBlockVersionEnum .values ()) {
114
80
if (versionEnum .getValue () > version ) {
115
81
byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (versionEnum .getValue ());
116
- if (!check2 (stats ) && stats != null ) {
82
+ if (!check (stats ) && Objects . nonNull ( stats ) ) {
117
83
stats [slot ] = VERSION_DOWNGRADE ;
118
84
manager .getDynamicPropertiesStore ().statsByVersion (versionEnum .getValue (), stats );
119
85
}
@@ -130,14 +96,12 @@ public synchronized void update(BlockCapsule blockCapsule) {
130
96
}
131
97
132
98
int version = blockCapsule .getInstance ().getBlockHeader ().getRawData ().getVersion ();
133
- if (version < ForkBlockVersionConsts . ENERGY_LIMIT || passSet . contains ( version )) {
99
+ if (version < ForkBlockVersionEnum . VERSION_3_2_2 . getValue ( )) {
134
100
return ;
135
101
}
136
102
137
- downgrade (version , slot );
138
-
139
103
byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
140
- if (check (stats ) || check2 ( stats ) ) {
104
+ if (check (stats )) {
141
105
return ;
142
106
}
143
107
@@ -147,6 +111,7 @@ public synchronized void update(BlockCapsule blockCapsule) {
147
111
148
112
stats [slot ] = VERSION_UPGRADE ;
149
113
manager .getDynamicPropertiesStore ().statsByVersion (version , stats );
114
+ downgrade (version , slot );
150
115
logger .info (
151
116
"*******update hard fork:{}, witness size:{}, solt:{}, witness:{}, version:{}" ,
152
117
Streams .zip (witnesses .stream (), Stream .of (ArrayUtils .toObject (stats )), Maps ::immutableEntry )
@@ -159,53 +124,13 @@ public synchronized void update(BlockCapsule blockCapsule) {
159
124
version );
160
125
}
161
126
162
- private void setSolidNumWithVersion5BeEffective (int version , int witnessSize ) {
163
- if (version != ForkBlockVersionConsts .ENERGY_LIMIT ) {
164
- return ;
165
- }
166
-
167
- long num = manager .getDynamicPropertiesStore ().getSolidNumWithVersion5 ();
168
- if (num == -1 ) {
169
- long blockNum = manager .getDynamicPropertiesStore ().getLatestSolidifiedBlockNum ();
170
- manager .getDynamicPropertiesStore ().setSolidNumWithVersion5 (blockNum + witnessSize * 2 / 3 );
171
- }
172
- }
173
-
174
- public synchronized void updateWhenMaintenance (BlockCapsule blockCapsule ) {
175
- int version = blockCapsule .getInstance ().getBlockHeader ().getRawData ().getVersion ();
176
- if (version < ForkBlockVersionEnum .VERSION_3_2_2 .getValue () || passSet .contains (version )) {
177
- return ;
178
- }
179
-
180
- byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
181
- if (check2 (stats )) {
182
- return ;
183
- }
184
-
185
- if (check (stats )) {
186
- Arrays .fill (stats , HARD_FORK_EFFECTIVE );
187
- manager .getDynamicPropertiesStore ().statsByVersion (version , stats );
188
- logger .info (
189
- "*******hard fork is effective in the maintenance:{}, version:{}" ,
190
- ArrayUtils .toObject (stats ),
191
- version );
192
- }
193
- }
194
-
195
- public synchronized void reset (BlockCapsule blockCapsule ) {
196
- int version = blockCapsule .getInstance ().getBlockHeader ().getRawData ().getVersion ();
197
- if (version < ForkBlockVersionConsts .ENERGY_LIMIT || passSet .contains (version )) {
198
- return ;
199
- }
200
-
201
- byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
202
- if (check (stats ) || check2 (stats )) {
203
- return ;
204
- }
205
-
206
- if (stats != null ) {
207
- Arrays .fill (stats , (byte ) 0 );
208
- manager .getDynamicPropertiesStore ().statsByVersion (version , stats );
127
+ public synchronized void reset () {
128
+ for (ForkBlockVersionEnum versionEnum : ForkBlockVersionEnum .values ()) {
129
+ byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (versionEnum .getValue ());
130
+ if (!check (stats ) && Objects .nonNull (stats )) {
131
+ Arrays .fill (stats , VERSION_DOWNGRADE );
132
+ manager .getDynamicPropertiesStore ().statsByVersion (versionEnum .getValue (), stats );
133
+ }
209
134
}
210
135
}
211
136
0 commit comments