Skip to content

Commit

Permalink
move magic numbers and the property define to the Constant class
Browse files Browse the repository at this point in the history
  • Loading branch information
AionJayT committed Feb 25, 2020
1 parent 073177b commit 1830321
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.aion.txpool.Constant.TXPOOL_PROPERTY;
import org.aion.txpool.v1.TxPoolV1;
import org.aion.util.types.ByteArrayWrapper;
import org.aion.zero.impl.blockchain.AionImpl.NetworkBestBlockCallback;
Expand All @@ -36,7 +37,6 @@
import org.aion.zero.impl.types.TxResponse;
import org.aion.base.AccountState;
import org.aion.mcf.db.RepositoryCache;
import org.aion.txpool.Constant;
import org.aion.types.AionAddress;
import org.aion.util.bytes.ByteUtil;
import org.aion.util.conversions.Hex;
Expand Down Expand Up @@ -150,8 +150,8 @@ public AionPendingStateImpl(
} else {
Properties prop = new Properties();
// The BlockEnergyLimit will be updated when the best block found.
prop.put(Constant.PROP_BLOCK_NRG_LIMIT, String.valueOf(energyUpperBound));
prop.put(Constant.PROP_TX_TIMEOUT, String.valueOf(txPendingTimeout));
prop.put(TXPOOL_PROPERTY.PROP_BLOCK_NRG_LIMIT, String.valueOf(energyUpperBound));
prop.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, String.valueOf(txPendingTimeout));
this.txPool = new TxPoolV1(prop);
}

Expand Down
15 changes: 12 additions & 3 deletions modTxPool/src/org/aion/txpool/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

public class Constant {

public enum TXPOOL_PROPERTY {
PROP_TX_TIMEOUT,
PROP_BLOCK_NRG_LIMIT,
PROP_POOL_SIZE_MAX
}

public final static int MAX_BLK_SIZE = 2 * 1024 * 1024;
public final static long MIN_ENERGY_CONSUME = 21_000L;
public final static String PROP_TX_TIMEOUT = "tx-timeout";
public final static String PROP_BLOCK_NRG_LIMIT = "blk-nrg-limit";
public final static String PROP_POOL_SIZE_MAX = "pool-size-max";
public final static int TRANSACTION_TIMEOUT_DEFAULT = 3600;
public final static int TRANSACTION_TIMEOUT_MIN = 10;
public final static long BLOCK_ENERGY_LIMIT_MIN = 1_000_000L;
public final static long BLOCK_ENERGY_LIMIT_DEFAULT = 10_000_000L;
public final static int TXPOOL_SIZE_DEFAULT = 2048;
public final static int TXPOOL_SIZE_MIN = 1024;
}
19 changes: 10 additions & 9 deletions modTxPool/src/org/aion/txpool/v1/TxPoolV1.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.aion.log.AionLoggerFactory;
import org.aion.log.LogEnum;
import org.aion.txpool.Constant;
import org.aion.txpool.Constant.TXPOOL_PROPERTY;
import org.aion.types.AionAddress;
import org.aion.util.types.ByteArrayWrapper;
import org.slf4j.Logger;
Expand Down Expand Up @@ -67,23 +68,23 @@ public final class TxPoolV1 {
*/
public TxPoolV1(Properties config) {

if (Optional.ofNullable(config.get(Constant.PROP_TX_TIMEOUT)).isPresent()) {
transactionTimeout = Math.max(Integer.parseInt(config.get(Constant.PROP_TX_TIMEOUT).toString()), 10);
if (Optional.ofNullable(config.get(TXPOOL_PROPERTY.PROP_TX_TIMEOUT)).isPresent()) {
transactionTimeout = Math.max(Integer.parseInt(config.get(TXPOOL_PROPERTY.PROP_TX_TIMEOUT).toString()), Constant.TRANSACTION_TIMEOUT_MIN);
} else {
transactionTimeout = 3600;
transactionTimeout = Constant.TRANSACTION_TIMEOUT_DEFAULT;
}

if (Optional.ofNullable(config.get(Constant.PROP_BLOCK_NRG_LIMIT)).isPresent()) {
if (Optional.ofNullable(config.get(TXPOOL_PROPERTY.PROP_BLOCK_NRG_LIMIT)).isPresent()) {
blockEnergyLimit =new AtomicLong(
Math.max(Long.parseLong((String) config.get(Constant.PROP_BLOCK_NRG_LIMIT)), 1_000_000L));
Math.max(Long.parseLong((String) config.get(TXPOOL_PROPERTY.PROP_BLOCK_NRG_LIMIT)), Constant.BLOCK_ENERGY_LIMIT_MIN));
} else {
blockEnergyLimit = new AtomicLong(10_000_000L);
blockEnergyLimit = new AtomicLong(Constant.BLOCK_ENERGY_LIMIT_DEFAULT);
}

if (Optional.ofNullable(config.get(Constant.PROP_POOL_SIZE_MAX)).isPresent()) {
maxPoolSize = Math.max(Integer.parseInt((String) config.get(Constant.PROP_POOL_SIZE_MAX)), 1024);
if (Optional.ofNullable(config.get(TXPOOL_PROPERTY.PROP_POOL_SIZE_MAX)).isPresent()) {
maxPoolSize = Math.max(Integer.parseInt((String) config.get(TXPOOL_PROPERTY.PROP_POOL_SIZE_MAX)), Constant.TXPOOL_SIZE_MIN);
} else {
maxPoolSize = 8192;
maxPoolSize = Constant.TXPOOL_SIZE_DEFAULT;
}

poolTransactions = new HashMap<>();
Expand Down
58 changes: 29 additions & 29 deletions modTxPool/test/org/aion/txpool/TxnPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void getTxnPool() {
@Test
public void add1() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

ITxPool tp = new TxPoolA0(config);
List<PooledTransaction> txnl = getMockTransaction(0);
Expand All @@ -82,7 +82,7 @@ public void add1() {
@Test
public void add2() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

ITxPool tp = new TxPoolA0(config);
PooledTransaction pooledTx = genTransaction(new byte[0], 0);
Expand All @@ -95,7 +95,7 @@ public void add2() {
@Test
public void add3() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

ITxPool tp = new TxPoolA0(config);
List<PooledTransaction> txl = null;
Expand Down Expand Up @@ -124,7 +124,7 @@ private List<PooledTransaction> getMockTransaction(long energyConsumed) {
@Test
public void remove() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

ITxPool tp = new TxPoolA0(config);
List<PooledTransaction> txnl = getMockTransaction(0);
Expand All @@ -138,7 +138,7 @@ public void remove() {
@Test
public void remove2() {
Properties config = new Properties();
config.put("tx-timeout", "100"); // 100 sec
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100"); // 100 sec

ITxPool tp = new TxPoolA0(config);
List<PooledTransaction> txl = new ArrayList<>();
Expand Down Expand Up @@ -166,7 +166,7 @@ public void remove2() {
@Test
public void remove3() {
Properties config = new Properties();
config.put("tx-timeout", "100"); // 100 sec
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100"); // 100 sec

ITxPool txPool = new TxPoolA0(config);
List<PooledTransaction> txl = new ArrayList<>();
Expand Down Expand Up @@ -195,7 +195,7 @@ public void remove3() {
@Test
public void remove4() {
Properties config = new Properties();
config.put("tx-timeout", "100"); // 100 sec
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100"); // 100 sec

ITxPool txPool = new TxPoolA0(config);
List<PooledTransaction> txl = new ArrayList<>();
Expand Down Expand Up @@ -281,7 +281,7 @@ private PooledTransaction genTransactionRandomPrice(byte[] nonce, ECKey key, lon
@Test
public void timeout1() {
Properties config = new Properties();
config.put("tx-timeout", "10"); // 10 sec
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10"); // 10 sec

TxPoolA0 tp = new TxPoolA0(config);
List<PooledTransaction> txnl = getMockTransaction(30000L);
Expand All @@ -294,7 +294,7 @@ public void timeout1() {
@Test
public void timeout2() {
Properties config = new Properties();
config.put("tx-timeout", "1"); // still 10 sec
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "1"); // still 10 sec

TxPoolA0 tp = new TxPoolA0(config);
List<PooledTransaction> txnl = getMockTransaction(30000L);
Expand All @@ -307,7 +307,7 @@ public void timeout2() {
@Test
public void snapshot() {
Properties config = new Properties();
config.put("tx-timeout", "10"); // 10 sec
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10"); // 10 sec

ITxPool tp = new TxPoolA0(config);
List<PooledTransaction> txnl = getMockTransaction(0);
Expand All @@ -320,7 +320,7 @@ public void snapshot() {
@Test
public void snapshot2() {
Properties config = new Properties();
config.put("tx-timeout", "100"); // 100 sec
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100"); // 100 sec

ITxPool tp = new TxPoolA0(config);
List<PooledTransaction> txl = new ArrayList<>();
Expand All @@ -340,7 +340,7 @@ public void snapshot2() {
@Test
public void snapshot3() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -368,7 +368,7 @@ public void snapshot3() {
@Test
public void snapshot4() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -396,7 +396,7 @@ public void snapshot4() {
@Test
public void snapshot5() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -426,7 +426,7 @@ public void snapshot5() {
@Test
public void snapshot6() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -456,7 +456,7 @@ public void snapshot6() {
@Test
public void snapshot7() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -487,7 +487,7 @@ public void snapshot7() {
@Test
public void snapshot8() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -518,7 +518,7 @@ public void snapshot8() {
@Test
public void snapshot9() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -563,7 +563,7 @@ public void snapshot9() {
@Test
public void snapshot10() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -623,7 +623,7 @@ public void snapshot10() {
@Test
public void snapshotWithSameTransactionTimestamp() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -688,7 +688,7 @@ public void snapshotWithSameTransactionTimestamp() {
@Test
public void addRepeatedTxn() {
Properties config = new Properties();
config.put("tx-timeout", "10");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10");

ITxPool tp = new TxPoolA0(config);
AionTransaction txn =
Expand All @@ -715,7 +715,7 @@ public void addRepeatedTxn() {
@Test
public void addRepeatedTxn2() {
Properties config = new Properties();
config.put("tx-timeout", "10");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10");

ITxPool tp = new TxPoolA0(config);

Expand Down Expand Up @@ -746,7 +746,7 @@ public void addRepeatedTxn2() {
@Test
public void addRepeatedTxn3() {
Properties config = new Properties();
config.put("tx-timeout", "10");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10");

ITxPool tp = new TxPoolA0(config);

Expand Down Expand Up @@ -778,7 +778,7 @@ public void addRepeatedTxn3() {
@Test
public void addTxWithSameNonce() {
Properties config = new Properties();
config.put("tx-timeout", "10");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10");
ITxPool tp = new TxPoolA0(config);

PooledTransaction pooledTx = genTransaction(BigInteger.ONE.toByteArray(), MIN_ENERGY_CONSUME + 100);
Expand All @@ -799,7 +799,7 @@ public void addTxWithSameNonce() {
@Test
public void noncebyAccountTest() {
Properties config = new Properties();
config.put("tx-timeout", "10");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -841,7 +841,7 @@ public void noncebyAccountTest() {
@Test
public void noncebyAccountTest2() {
Properties config = new Properties();
config.put("tx-timeout", "10");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -885,7 +885,7 @@ public void noncebyAccountTest2() {
@Test
public void feemapTest() {
Properties config = new Properties();
config.put("tx-timeout", "10");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "10");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -927,7 +927,7 @@ public void feemapTest() {
@Test
public void TxnfeeCombineTest() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down Expand Up @@ -956,7 +956,7 @@ public void TxnfeeCombineTest() {
@Test
public void TxnfeeCombineTest2() {
Properties config = new Properties();
config.put("tx-timeout", "100");
config.put(TXPOOL_PROPERTY.PROP_TX_TIMEOUT, "100");

TxPoolA0 tp = new TxPoolA0(config);

Expand Down
Loading

0 comments on commit 1830321

Please sign in to comment.