Skip to content

Commit 791ee6c

Browse files
Merge pull request #6184 from zeusoo001/discard-inv-below-solid-block-1
feat(net): discard block inventory below the solidified block
2 parents fadc1a4 + 941959a commit 791ee6c

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

framework/src/main/java/org/tron/core/net/TronNetDelegate.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ public BlockId getKhaosDbHeadBlockId() {
156156
return chainBaseManager.getKhaosDbHead().getBlockId();
157157
}
158158

159+
public long getSolidifiedBlockNum() {
160+
return chainBaseManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
161+
}
162+
159163
public BlockId getSolidBlockId() {
160164
return chainBaseManager.getSolidBlockId();
161165
}

framework/src/main/java/org/tron/core/net/service/adv/AdvService.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,16 @@ public boolean addInv(Item item) {
121121
if (item.getType().equals(InventoryType.TRX) && trxCache.getIfPresent(item) != null) {
122122
return false;
123123
}
124-
if (item.getType().equals(InventoryType.BLOCK) && blockCache.getIfPresent(item) != null) {
125-
return false;
124+
125+
if (item.getType().equals(InventoryType.BLOCK)) {
126+
if (blockCache.getIfPresent(item) != null) {
127+
return false;
128+
}
129+
130+
long solidNum = tronNetDelegate.getSolidifiedBlockNum();
131+
if (new BlockId(item.getHash()).getNum() <= solidNum) {
132+
return false;
133+
}
126134
}
127135

128136
synchronized (this) {

framework/src/test/java/org/tron/core/net/services/AdvServiceTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ private void testAddInv() {
7474

7575
Item itemBlock = new Item(Sha256Hash.ZERO_HASH, InventoryType.BLOCK);
7676
flag = service.addInv(itemBlock);
77+
Assert.assertFalse(flag);
78+
79+
BlockCapsule.BlockId blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 1000L);
80+
itemBlock = new Item(blockId, InventoryType.BLOCK);
81+
flag = service.addInv(itemBlock);
7782
Assert.assertTrue(flag);
7883
flag = service.addInv(itemBlock);
7984
Assert.assertFalse(flag);
8085

86+
blockId = new BlockCapsule.BlockId(Sha256Hash.ZERO_HASH, 10000L);
87+
itemBlock = new Item(blockId, InventoryType.BLOCK);
8188
service.addInvToCache(itemBlock);
8289
flag = service.addInv(itemBlock);
8390
Assert.assertFalse(flag);

0 commit comments

Comments
 (0)