Skip to content

Commit f7bd20f

Browse files
committed
refactor(config): move storage clamp/validation into bean postProcess
Address PR review from lxcmyf (2026-04-22, Args.java:255) — architectural consistency with NodeConfig / VmConfig / CommitteeConfig where clamps and validation live in the bean's own postProcess(). Before: Args.applyStorageConfig held an inline clamp for estimatedTransactions ([100, 10000]) and an inline range check for maxFlushCount ((0, 500]). This was carried over from develop's static Storage helpers during the bean-binding refactor. Now: - SnapshotConfig.postProcess() enforces maxFlushCount in (0, 500], throwing IllegalArgumentException on violation (same messages as develop). - TxCacheConfig.postProcess() clamps estimatedTransactions to [100, 10000]. - StorageConfig.fromConfig() calls both alongside the existing dbSettings.postProcess(). - Args.applyStorageConfig now simply reads already-validated values. This also lets StorageConfig enforce its invariants when loaded standalone (e.g. in tests) without depending on the Args bridge. Tests (StorageConfigTest): - testSnapshotMaxFlushCountZeroRejected - testSnapshotMaxFlushCountNegativeRejected - testSnapshotMaxFlushCountOver500Rejected - testTxCacheEstimatedClampedBelowMin - testTxCacheEstimatedClampedAboveMax - testTxCacheEstimatedWithinRangePreserved
1 parent 434d917 commit f7bd20f

3 files changed

Lines changed: 61 additions & 18 deletions

File tree

common/src/main/java/org/tron/core/config/args/StorageConfig.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,32 @@ public static class CheckpointConfig {
162162
@Setter
163163
public static class SnapshotConfig {
164164
private int maxFlushCount = 1;
165+
166+
// Reject out-of-range values. Mirrors develop Storage.getSnapshotMaxFlushCountFromConfig.
167+
void postProcess() {
168+
if (maxFlushCount <= 0) {
169+
throw new IllegalArgumentException("MaxFlushCount value can not be negative or zero!");
170+
}
171+
if (maxFlushCount > 500) {
172+
throw new IllegalArgumentException("MaxFlushCount value must not exceed 500!");
173+
}
174+
}
165175
}
166176

167177
@Getter
168178
@Setter
169179
public static class TxCacheConfig {
170180
private int estimatedTransactions = 1000;
171181
private boolean initOptimization = false;
182+
183+
// Clamp to [100, 10000]. Mirrors develop Storage.getEstimatedTransactionsFromConfig.
184+
void postProcess() {
185+
if (estimatedTransactions > 10000) {
186+
estimatedTransactions = 10000;
187+
} else if (estimatedTransactions < 100) {
188+
estimatedTransactions = 100;
189+
}
190+
}
172191
}
173192

174193
@Getter
@@ -200,6 +219,8 @@ public static StorageConfig fromConfig(Config config) {
200219
sc.defaultLDbOption = readDbOption(section, "defaultL");
201220

202221
sc.dbSettings.postProcess();
222+
sc.snapshot.postProcess();
223+
sc.txCache.postProcess();
203224
return sc;
204225
}
205226

common/src/test/java/org/tron/core/config/args/StorageConfigTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,40 @@ public void testBalanceHistoryLookup() {
103103
StorageConfig sc = StorageConfig.fromConfig(config);
104104
assertTrue(sc.getBalance().getHistory().isLookup());
105105
}
106+
107+
@Test(expected = IllegalArgumentException.class)
108+
public void testSnapshotMaxFlushCountZeroRejected() {
109+
StorageConfig.fromConfig(withRef("storage.snapshot.maxFlushCount = 0"));
110+
}
111+
112+
@Test(expected = IllegalArgumentException.class)
113+
public void testSnapshotMaxFlushCountNegativeRejected() {
114+
StorageConfig.fromConfig(withRef("storage.snapshot.maxFlushCount = -1"));
115+
}
116+
117+
@Test(expected = IllegalArgumentException.class)
118+
public void testSnapshotMaxFlushCountOver500Rejected() {
119+
StorageConfig.fromConfig(withRef("storage.snapshot.maxFlushCount = 501"));
120+
}
121+
122+
@Test
123+
public void testTxCacheEstimatedClampedBelowMin() {
124+
StorageConfig sc = StorageConfig.fromConfig(
125+
withRef("storage.txCache.estimatedTransactions = 50"));
126+
assertEquals(100, sc.getTxCache().getEstimatedTransactions());
127+
}
128+
129+
@Test
130+
public void testTxCacheEstimatedClampedAboveMax() {
131+
StorageConfig sc = StorageConfig.fromConfig(
132+
withRef("storage.txCache.estimatedTransactions = 99999"));
133+
assertEquals(10000, sc.getTxCache().getEstimatedTransactions());
134+
}
135+
136+
@Test
137+
public void testTxCacheEstimatedWithinRangePreserved() {
138+
StorageConfig sc = StorageConfig.fromConfig(
139+
withRef("storage.txCache.estimatedTransactions = 5000"));
140+
assertEquals(5000, sc.getTxCache().getEstimatedTransactions());
141+
}
106142
}

framework/src/main/java/org/tron/core/config/args/Args.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -235,25 +235,11 @@ private static void applyStorageConfig(StorageConfig sc) {
235235
PARAMETER.storage.setCheckpointVersion(sc.getCheckpoint().getVersion());
236236
PARAMETER.storage.setCheckpointSync(sc.getCheckpoint().isSync());
237237

238-
// estimatedTransactions with range clamping [100, 10000]
239-
int estimated = sc.getTxCache().getEstimatedTransactions();
240-
if (estimated > 10000) {
241-
estimated = 10000;
242-
} else if (estimated < 100) {
243-
estimated = 100;
244-
}
245-
PARAMETER.storage.setEstimatedBlockTransactions(estimated);
238+
// estimatedTransactions / maxFlushCount clamping & validation run inside
239+
// TxCacheConfig.postProcess / SnapshotConfig.postProcess during bean load.
240+
PARAMETER.storage.setEstimatedBlockTransactions(sc.getTxCache().getEstimatedTransactions());
246241
PARAMETER.storage.setTxCacheInitOptimization(sc.getTxCache().isInitOptimization());
247-
248-
// snapshotMaxFlushCount with range validation
249-
int maxFlush = sc.getSnapshot().getMaxFlushCount();
250-
if (maxFlush <= 0) {
251-
throw new IllegalArgumentException("MaxFlushCount value can not be negative or zero!");
252-
}
253-
if (maxFlush > 500) {
254-
throw new IllegalArgumentException("MaxFlushCount value must not exceed 500!");
255-
}
256-
PARAMETER.storage.setMaxFlushCount(maxFlush);
242+
PARAMETER.storage.setMaxFlushCount(sc.getSnapshot().getMaxFlushCount());
257243

258244
// backup
259245
StorageConfig.BackupConfig backup = sc.getBackup();

0 commit comments

Comments
 (0)