Skip to content

Commit bf9fac6

Browse files
authored
Merge pull request tronprotocol#406 from tronprotocol/ls_debug
Ls debug
2 parents cbb44fb + 81c67d6 commit bf9fac6

4 files changed

Lines changed: 62 additions & 16 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Arrays;
44
import java.util.stream.IntStream;
55

6+
@Deprecated
67
public class BlockFilledSlots {
78

89
public static int SLOT_NUMBER = 128;

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

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.protobuf.ByteString;
44
import java.util.Optional;
5+
import java.util.Arrays;
6+
import java.util.stream.IntStream;
57
import lombok.extern.slf4j.Slf4j;
68
import org.joda.time.DateTime;
79
import 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;

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,11 @@ public void updateDynamicProperties(BlockCapsule block) {
518518
logger.info("{} miss a block. totalMissed = {}",
519519
w.createReadableString(), w.getTotalMissed());
520520
}
521+
this.dynamicPropertiesStore.applyBlock(false);
521522
}
523+
this.dynamicPropertiesStore.applyBlock(true);
522524

523-
if (slot >= 0) {
524-
while (slot-- > 0) {
525-
this.dynamicPropertiesStore.getBlockFilledSlots().applyBlock(false);
526-
}
527-
this.dynamicPropertiesStore.getBlockFilledSlots().applyBlock(true);
528-
} else {
525+
if (slot <= 0) {
529526
logger.warn("missedBlocks [" + slot + "] is illegal");
530527
}
531528

src/main/java/org/tron/core/witness/WitnessController.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.tron.core.capsule.BlockCapsule;
2525
import org.tron.core.capsule.WitnessCapsule;
2626
import org.tron.core.db.AccountStore;
27-
import org.tron.core.db.BlockFilledSlots;
2827
import org.tron.core.db.Manager;
2928
import org.tron.core.db.WitnessStore;
3029

@@ -336,9 +335,7 @@ public void updateWitness() {
336335
}
337336

338337
public int calculateParticipationRate() {
339-
return
340-
100 * manager.getDynamicPropertiesStore().getBlockFilledSlots().calculateFilledSlotsCount()
341-
/ BlockFilledSlots.SLOT_NUMBER;
338+
return manager.getDynamicPropertiesStore().calculateFilledSlotsCount();
342339
}
343340

344341
private static List<String> getWitnessStringList(List<WitnessCapsule> witnessStates) {

0 commit comments

Comments
 (0)