Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AionBlockchainImpl clean up #1138

Merged
merged 7 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions modAionImpl/src/org/aion/zero/impl/blockchain/A0BCConfig.java

This file was deleted.

157 changes: 77 additions & 80 deletions modAionImpl/src/org/aion/zero/impl/blockchain/AionBlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
*/
public class AionBlockchainImpl implements IAionBlockchain {

private static final Logger LOG = LoggerFactory.getLogger(LogEnum.CONS.name());
protected static final Logger LOG = LoggerFactory.getLogger(LogEnum.CONS.name());
private static final Logger GEN_LOG = LoggerFactory.getLogger(LogEnum.GEN.name());
private static final Logger SURVEY_LOG = LoggerFactory.getLogger(LogEnum.SURVEY.name());
private static final Logger SYNC_LOG = LoggerFactory.getLogger(LogEnum.SYNC.name());
Expand All @@ -161,7 +161,6 @@ public class AionBlockchainImpl implements IAionBlockchain {
*/
protected ChainConfiguration chainConfiguration;

private A0BCConfig config;
private AionRepositoryImpl repository;
private RepositoryCache<AccountState> track;
private TransactionStore transactionStore;
Expand Down Expand Up @@ -212,7 +211,14 @@ public class AionBlockchainImpl implements IAionBlockchain {
* The constructor for the blockchain initialization {@see AionHub}.
*/
public AionBlockchainImpl(CfgAion cfgAion, IEventMgr eventMgr, boolean forTest) {
this(generateBCConfig(cfgAion), AionRepositoryImpl.inst(),
this(
cfgAion.getConsensus().getExtraData().getBytes(),
AddressUtils.wrapAddress(cfgAion.getConsensus().getMinerAddress()),
getEnergyLimitStrategy(cfgAion),
cfgAion.getDb().isInternalTxStorageEnabled(),
cfgAion.getGenesis().getHashWrapper().equals(ByteArrayWrapper.fromHex("30793b4ea012c6d3a58c85c5b049962669369807a98e36807c1b02116417f823")),
new ForkUtility(cfgAion.getFork().getProperties(), LOG),
AionRepositoryImpl.inst(),
forTest ? new ChainConfiguration() {
/*
* Remove the equiHash solution for the simplified
Expand Down Expand Up @@ -259,19 +265,29 @@ public BlockHeaderValidator createBlockHeaderValidatorForImport() {
/**
* The constructor for the public constructor {@see AionBlockchainImpl(CfgAion, IEventMgr, boolean)}
* and the integrating test class {@see StandaloneBlockchain}
*
* @param extraData an (up to) 32-byte value representing the currently set extra data for this particular node, blocks mined with this node will use this as extra data
* @param minerCoinbase a 32-bytes address representing the currently set mining coinbase for this particular node, blocks mined with this node will use this as the coinbase
* @param energyLimitStrategy the selected energy strategy algorithm
* @param storeInternalTransactions flag indicating the desired behavior for storing internal transactions
* @param isMainnet flag indicating if the current network used is the Aion mainnet
* @param forkUtility utility for checking when fork updates occur
*/
protected AionBlockchainImpl(
final A0BCConfig config,
final byte[] extraData,
final AionAddress minerCoinbase,
final AbstractEnergyStrategyLimit energyLimitStrategy,
final boolean storeInternalTransactions,
final boolean isMainnet,
final ForkUtility forkUtility,
final AionRepositoryImpl repository,
final ChainConfiguration chainConfig,
final IEventMgr eventMgr) {

// TODO AKI-318: this specialized class is very cumbersome to maintain; could be replaced with CfgAion
this.config = config;
this.repository = repository;
this.storeInternalTransactions = config.isInternalTransactionStorageEnabled();
this.storeInternalTransactions = storeInternalTransactions;

isMainnet = CfgAion.inst().getGenesis().getHashWrapper().equals(ByteArrayWrapper.fromHex("30793b4ea012c6d3a58c85c5b049962669369807a98e36807c1b02116417f823"));
this.isMainnet = isMainnet;

/**
* Because we dont have any hardforks, later on chain configuration must be determined by
Expand All @@ -288,13 +304,13 @@ protected AionBlockchainImpl(

this.transactionStore = this.repository.getTransactionStore();

this.minerCoinbase = this.config.getMinerCoinbase();
this.minerCoinbase = minerCoinbase;
if (minerCoinbase == null) {
LOG.warn("No miner Coinbase!");
}

/** Save a copy of the miner extra data */
byte[] extraBytes = this.config.getExtraData();
byte[] extraBytes = extraData;
this.minerExtraData =
new byte[this.chainConfiguration.getConstants().getMaximumExtraDataSize()];
if (extraBytes.length < this.chainConfiguration.getConstants().getMaximumExtraDataSize()) {
Expand All @@ -307,9 +323,9 @@ protected AionBlockchainImpl(
0,
this.chainConfiguration.getConstants().getMaximumExtraDataSize());
}
this.energyLimitStrategy = config.getEnergyLimitStrategy();
this.energyLimitStrategy = energyLimitStrategy;

this.forkUtility = new ForkUtility(CfgAion.inst().getFork().getProperties(), LOG);
this.forkUtility = forkUtility;

// initialize beacon hash validator
this.beaconHashValidator = new BeaconHashValidator(this, this.forkUtility);
Expand All @@ -318,14 +334,9 @@ protected AionBlockchainImpl(
}

/**
* Helper method for generating the adapter between this class and {@link CfgAion}
*
* @param cfgAion
* @return {@code configuration} instance that directly references the singleton instance of
* cfgAion
* Helper method for retrieving the selected energy strategy algorithm
*/
private static A0BCConfig generateBCConfig(CfgAion cfgAion) {

private static AbstractEnergyStrategyLimit getEnergyLimitStrategy(CfgAion cfgAion) {
Long blkNum = monetaryUpdateBlkNum(cfgAion.getFork().getProperties());

BigInteger initialSupply = ZERO;
Expand All @@ -334,47 +345,10 @@ private static A0BCConfig generateBCConfig(CfgAion cfgAion) {
}

ChainConfiguration config = new ChainConfiguration(blkNum, initialSupply);
return new A0BCConfig() {
@Override
public AionAddress getCoinbase() {
return cfgAion.getGenesis().getCoinbase();
}

@Override
public byte[] getExtraData() {
return cfgAion.getConsensus().getExtraData().getBytes();
}

@Override
public boolean getExitOnBlockConflict() {
return true;
// return cfgAion.getSync().getExitOnBlockConflict();
}

@Override
public AionAddress getMinerCoinbase() {
return AddressUtils.wrapAddress(cfgAion.getConsensus().getMinerAddress());
}

// TODO: hook up to configuration file
@Override
public int getFlushInterval() {
return 1;
}

@Override
public AbstractEnergyStrategyLimit getEnergyLimitStrategy() {
return EnergyStrategies.getEnergyStrategy(
cfgAion.getConsensus().getEnergyStrategy().getStrategy(),
cfgAion.getConsensus().getEnergyStrategy(),
config);
}

@Override
public boolean isInternalTransactionStorageEnabled() {
return CfgAion.inst().getDb().isInternalTxStorageEnabled();
}
};
return EnergyStrategies.getEnergyStrategy(
cfgAion.getConsensus().getEnergyStrategy().getStrategy(),
cfgAion.getConsensus().getEnergyStrategy(),
config);
}

private static Long monetaryUpdateBlkNum(Properties properties) {
Expand Down Expand Up @@ -917,7 +891,7 @@ public Triple<Long, Set<ByteArrayWrapper>, ImportResult> tryToConnect(final List

// printing additional information when debug is enabled
SYNC_LOG.debug(
"<import-status: node = {}, hash = {}, number = {}, txs = {}, block time = {}, result = {}, time elapsed = {} ms, block td = {}, chain td = {}>",
"<import-status: node = {}, hash = {}, number = {}, txs = {}, block time = {}, result = {}, time elapsed = {} ns, block td = {}, chain td = {}>",
peerDisplayId,
block.getShortHash(),
block.getNumber(),
Expand Down Expand Up @@ -954,6 +928,9 @@ public Triple<Long, Set<ByteArrayWrapper>, ImportResult> tryToConnect(final List
private final static long TEN_SECOND_TO_NANO = TimeUnit.SECONDS.toNanos(10);
private final static long SIXTY_SECOND_TO_MILLI = TimeUnit.SECONDS.toMillis(60);

/**
* @return the import result and the duration of the import in {@link TimeUnit#NANOSECONDS}.
*/
private Pair<ImportResult, Long> tryToConnectWithTimedExecution(final BlockWrapper blockWrapper) {
long importTime = System.nanoTime();
ImportResult importResult =
Expand Down Expand Up @@ -1946,39 +1923,59 @@ private void storeBlock(Block block, List<AionTxReceipt> receipts, List<AionTxEx
setBestBlock(block);
}

@Override
public int storePendingBlockRange(List<Block> blocks) {
/**
* Attempts to store the given range of blocks in the pending block store, saving them to be
* imported later when the chain has reached the required height or has imported the needed
* parent block. The blocks from the range that are already stored with be skipped.
*
* @param blocks a range of blocks that cannot be imported due to height or lack of parent block
* @param log the logger used for messages
* @return an integer value (ranging from zero to the number of given blocks) representing the
* number of blocks that were stored from the given input.
* @apiNote Functionality used to store blocks coming from <b>range import requests</b>.
*/
public int storePendingBlockRange(List<Block> blocks, Logger log) {
try {
return repository.getPendingBlockStore().addBlockRange(blocks);
return repository.getPendingBlockStore().addBlockRange(blocks, log);
} catch (Exception e) {
LOG.error(
"Unable to store range of blocks in " + repository.toString() + " due to: ", e);
log.error("Unable to store range of blocks in " + repository.toString() + " due to: ", e);
return 0;
}
}

@Override
public Map<ByteArrayWrapper, List<Block>> loadPendingBlocksAtLevel(long level) {
/**
* Retrieves ranges of blocks from the pending block store for a specific blockchain height.
*
* @param level the blockchain height of interest
* @param log the logger used for messages
* @return a map containing all the block ranges that are stored in the pending block store at
* the given height. The map may be empty if there is no data stored for the given height.
* It may also contain several entries if there are multiple ranges starting at the given
* level due to the storage of different chains.
*/
public Map<ByteArrayWrapper, List<Block>> loadPendingBlocksAtLevel(long level, Logger log) {
try {
return repository.getPendingBlockStore().loadBlockRange(level);
return repository.getPendingBlockStore().loadBlockRange(level, log);
} catch (Exception e) {
LOG.error(
"Unable to retrieve stored blocks from " + repository.toString() + " due to: ",
e);
log.error("Unable to retrieve stored blocks from " + repository.toString() + " due to: ", e);
return Collections.emptyMap();
}
}

@Override
public void dropImported(
long level,
List<ByteArrayWrapper> ranges,
Map<ByteArrayWrapper, List<Block>> blocks) {
/**
* Deletes the given blocks from the pending block storage.
*
* @param level the block height of the range starting point
* @param ranges the identifiers for the ranges to be deleted
* @param blocks the range identifier to blocks mappings to me deleted (used to ensure that if
* the ranges have been expanded, only the relevant blocks get deleted)
* @param log the logger used for messages
*/
public void dropImported(long level, List<ByteArrayWrapper> ranges, Map<ByteArrayWrapper, List<Block>> blocks, Logger log) {
try {
repository.getPendingBlockStore().dropPendingQueues(level, ranges, blocks);
repository.getPendingBlockStore().dropPendingQueues(level, ranges, blocks, log);
} catch (Exception e) {
LOG.error(
"Unable to delete used blocks from " + repository.toString() + " due to: ", e);
log.error("Unable to delete used blocks from " + repository.toString() + " due to: ", e);
return;
}
}
Expand Down
35 changes: 0 additions & 35 deletions modAionImpl/src/org/aion/zero/impl/blockchain/IAionBlockchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.aion.base.AionTransaction;
import org.aion.mcf.blockchain.Block;
import org.aion.mcf.blockchain.BlockHeader;
import org.aion.mcf.db.Repository;
import org.aion.types.AionAddress;
import org.aion.util.types.ByteArrayWrapper;
import org.aion.zero.impl.types.AionBlock;
Expand Down Expand Up @@ -81,40 +80,6 @@ Map<ByteArrayWrapper, byte[]> getReferencedTrieNodes(

long getSize();

/**
* Attempts to store the given range of blocks in the pending block store, saving them to be
* imported later when the chain has reached the required height or has imported the needed
* parent block. The blocks from the range that are already stored with be skipped.
*
* @param blocks a range of blocks that cannot be imported due to height or lack of parent block
* @return an integer value (ranging from zero to the number of given blocks) representing the
* number of blocks that were stored from the given input.
* @apiNote Functionality used to store blocks coming from <b>range import requests</b>.
*/
int storePendingBlockRange(List<Block> blocks);

/**
* Retrieves ranges of blocks from the pending block store for a specific blockchain height.
*
* @param level the blockchain height of interest
* @return a map containing all the block ranges that are stored in the pending block store at
* the given height. The map may be empty if there is no data stored for the given height.
* It may also contain several entries if there are multiple ranges starting at the given
* level due to the storage of different chains.
*/
Map<ByteArrayWrapper, List<Block>> loadPendingBlocksAtLevel(long level);

/**
* Deletes the given blocks from the pending block storage.
*
* @param level the block height of the range starting point
* @param ranges the identifiers for the ranges to be deleted
* @param blocks the range identifier to blocks mappings to me deleted (used to ensure that if
* the ranges have been expanded, only the relevant blocks get deleted)
*/
void dropImported(
long level, List<ByteArrayWrapper> ranges, Map<ByteArrayWrapper, List<Block>> blocks);

void setBestBlock(Block block);

void close();
Expand Down
Loading