1919import org .tron .core .capsule .BlockCapsule ;
2020import org .tron .core .config .Parameter .ForkBlockVersionConsts ;
2121import org .tron .core .config .Parameter .ForkBlockVersionEnum ;
22+ import org .tron .core .store .DynamicPropertiesStore ;
2223
2324@ Slf4j (topic = "utils" )
2425public class ForkController {
@@ -87,7 +88,7 @@ private boolean passNew(int version) {
8788 }
8889 }
8990 return count >= Math
90- .ceil ((double ) versionEnum .getHardForkRate () * manager . getWitnesses (). size () / 100 );
91+ .ceil ((double ) versionEnum .getHardForkRate () * stats . length / 100 );
9192 }
9293
9394
@@ -116,9 +117,9 @@ private boolean check(byte[] stats) {
116117 private void downgrade (int version , int slot ) {
117118 for (ForkBlockVersionEnum versionEnum : ForkBlockVersionEnum .values ()) {
118119 int versionValue = versionEnum .getValue ();
119- if (versionValue > version ) {
120+ if (versionValue > version && ! pass ( versionValue ) ) {
120121 byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (versionValue );
121- if (! check ( stats ) && Objects .nonNull (stats )) {
122+ if (Objects .nonNull (stats )) {
122123 stats [slot ] = VERSION_DOWNGRADE ;
123124 manager .getDynamicPropertiesStore ().statsByVersion (versionValue , stats );
124125 }
@@ -129,15 +130,13 @@ private void downgrade(int version, int slot) {
129130 private void upgrade (int version , int slotSize ) {
130131 for (ForkBlockVersionEnum versionEnum : ForkBlockVersionEnum .values ()) {
131132 int versionValue = versionEnum .getValue ();
132- if (versionValue < version ) {
133+ if (versionValue < version && ! pass ( versionValue ) ) {
133134 byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (versionValue );
134- if (!check (stats )) {
135- if (stats == null || stats .length == 0 ) {
136- stats = new byte [slotSize ];
137- }
138- Arrays .fill (stats , VERSION_UPGRADE );
139- manager .getDynamicPropertiesStore ().statsByVersion (versionValue , stats );
135+ if (stats == null || stats .length == 0 ) {
136+ stats = new byte [slotSize ];
140137 }
138+ Arrays .fill (stats , VERSION_UPGRADE );
139+ manager .getDynamicPropertiesStore ().statsByVersion (versionValue , stats );
141140 }
142141 }
143142 }
@@ -155,15 +154,20 @@ public synchronized void update(BlockCapsule blockCapsule) {
155154 return ;
156155 }
157156
157+ if (manager .getDynamicPropertiesStore ().getLatestVersion () >= version ) {
158+ return ;
159+ }
160+
158161 downgrade (version , slot );
159162
160163 byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (version );
161164 if (Objects .isNull (stats ) || stats .length != witnesses .size ()) {
162165 stats = new byte [witnesses .size ()];
163166 }
164167
165- if (check ( stats )) {
168+ if (pass ( version )) {
166169 upgrade (version , stats .length );
170+ manager .getDynamicPropertiesStore ().saveLatestVersion (version );
167171 return ;
168172 }
169173
@@ -185,11 +189,12 @@ public synchronized void update(BlockCapsule blockCapsule) {
185189 }
186190
187191 public synchronized void reset () {
192+ int size = manager .getWitnessScheduleStore ().getActiveWitnesses ().size ();
188193 for (ForkBlockVersionEnum versionEnum : ForkBlockVersionEnum .values ()) {
189194 int versionValue = versionEnum .getValue ();
190195 byte [] stats = manager .getDynamicPropertiesStore ().statsByVersion (versionValue );
191196 if (Objects .nonNull (stats ) && !pass (versionValue )) {
192- Arrays . fill ( stats , VERSION_DOWNGRADE ) ;
197+ stats = new byte [ size ] ;
193198 manager .getDynamicPropertiesStore ().statsByVersion (versionValue , stats );
194199 }
195200 }
@@ -212,4 +217,33 @@ private ForkController getInstance() {
212217 return instance ;
213218 }
214219 }
220+
221+ public void checkLocalVersion () {
222+ DynamicPropertiesStore store = manager .getDynamicPropertiesStore ();
223+ int latestVersion = store .getLatestVersion ();
224+ if (latestVersion == 0 ) {
225+ for (ForkBlockVersionEnum version : ForkBlockVersionEnum .values ()) {
226+ int v = version .getValue ();
227+ if (pass (v ) && latestVersion < v ) {
228+ latestVersion = v ;
229+ }
230+ }
231+ store .saveLatestVersion (latestVersion );
232+ return ;
233+ }
234+
235+ if (!CommonParameter .getInstance ().isVersionCheckEnable ()) {
236+ return ;
237+ }
238+
239+ int systemVersion = 0 ;
240+ for (ForkBlockVersionEnum version : ForkBlockVersionEnum .values ()) {
241+ if (version .getValue () > systemVersion ) {
242+ systemVersion = version .getValue ();
243+ }
244+ }
245+ if (latestVersion > systemVersion ) {
246+ throw new RuntimeException ("Version check failed, please upgrade to the latest version" );
247+ }
248+ }
215249}
0 commit comments