22
33import com .google .protobuf .ByteString ;
44import java .util .Optional ;
5+ import java .util .Arrays ;
6+ import java .util .stream .IntStream ;
57import lombok .extern .slf4j .Slf4j ;
68import org .joda .time .DateTime ;
79import org .tron .common .utils .ByteArray ;
@@ -23,8 +25,11 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
2325 private static final byte [] LATEST_SOLIDIFIED_BLOCK_NUM = "LATEST_SOLIDIFIED_BLOCK_NUM"
2426 .getBytes ();
2527
28+ private static final byte [] BLOCK_FILLED_SLOTS = "BLOCK_FILLED_SLOTS" .getBytes ();
2629
27- private BlockFilledSlots blockFilledSlots = new BlockFilledSlots ();
30+ private static final int BLOCK_FILLED_SLOTS_NUMBER = 128 ;
31+
32+ private int blockFilledSlotsIndex = 0 ;
2833
2934 private DateTime nextMaintenanceTime = new DateTime (
3035 Long .parseLong (Args .getInstance ().getGenesisBlock ().getTimestamp ()));
@@ -61,6 +66,14 @@ private DynamicPropertiesStore(String dbName) {
6166 this .saveLatestSolidifiedBlockNum (0 );
6267 }
6368
69+ try {
70+ this .getBlockFilledSlots ();
71+ } catch (IllegalArgumentException e ) {
72+ int [] blockFilledSlots = new int [BLOCK_FILLED_SLOTS_NUMBER ];
73+ Arrays .fill (blockFilledSlots , 1 );
74+ this .saveBlockFilledSlots (blockFilledSlots );
75+ }
76+
6477
6578 }
6679
@@ -103,9 +116,51 @@ public static DynamicPropertiesStore create(String dbName) {
103116 return instance ;
104117 }
105118
119+ public String intArrayToString (int [] a ) {
120+ StringBuilder sb = new StringBuilder ();
121+ for (int i : a ) {
122+ sb .append (i );
123+ }
124+ return sb .toString ();
125+ }
126+
127+ public int [] stringToIntArray (String s ) {
128+ int length = s .length ();
129+ int [] result = new int [length ];
130+ for (int i = 0 ; i < length ; ++i ) {
131+ result [i ] = Integer .parseInt (s .substring (i ,i +1 ));
132+ }
133+ return result ;
134+ }
135+
136+ public void saveBlockFilledSlots (int [] blockFilledSlots ) {
137+ logger .debug ("blockFilledSlots:" +intArrayToString (blockFilledSlots ));
138+ this .put (BLOCK_FILLED_SLOTS , new BytesCapsule (ByteArray .fromString (intArrayToString (blockFilledSlots ))));
139+ }
140+
141+ public int [] getBlockFilledSlots () {
142+ return Optional .ofNullable (this .dbSource .getData (BLOCK_FILLED_SLOTS ))
143+ .map (ByteArray ::toStr )
144+ .map (this ::stringToIntArray )
145+ .orElseThrow (
146+ () -> new IllegalArgumentException ("not found latest SOLIDIFIED_BLOCK_NUM timestamp" ));
147+ //return ByteArray.toLong(this.dbSource.getData(this.SOLIDIFIED_THRESHOLD));
148+ }
149+
150+ public void applyBlock (boolean fillBlock ) {
151+ int [] blockFilledSlots = getBlockFilledSlots ();
152+ blockFilledSlots [blockFilledSlotsIndex ] = fillBlock ? 1 : 0 ;
153+ blockFilledSlotsIndex = (blockFilledSlotsIndex + 1 ) % BLOCK_FILLED_SLOTS_NUMBER ;
154+ saveBlockFilledSlots (blockFilledSlots );
155+ }
156+
157+ public int calculateFilledSlotsCount () {
158+ int [] blockFilledSlots = getBlockFilledSlots ();
159+ return 100 * IntStream .of (blockFilledSlots ).sum () / BLOCK_FILLED_SLOTS_NUMBER ;
160+ }
106161
107162 public void saveLatestSolidifiedBlockNum (long number ) {
108- this .put (this . LATEST_SOLIDIFIED_BLOCK_NUM , new BytesCapsule (ByteArray .fromLong (number )));
163+ this .put (LATEST_SOLIDIFIED_BLOCK_NUM , new BytesCapsule (ByteArray .fromLong (number )));
109164 }
110165
111166 public long getLatestSolidifiedBlockNum () {
@@ -178,10 +233,6 @@ public void saveStateFlag(int n) {
178233 this .put (STATE_FLAG , new BytesCapsule (ByteArray .fromInt (n )));
179234 }
180235
181- public BlockFilledSlots getBlockFilledSlots () {
182- return blockFilledSlots ;
183- }
184-
185236
186237 public DateTime getNextMaintenanceTime () {
187238 return nextMaintenanceTime ;
0 commit comments