Skip to content

Commit

Permalink
Merge pull request #635 from aionnetwork/sync-enhancement
Browse files Browse the repository at this point in the history
Sync enhancement: first version of Lightning sync
  • Loading branch information
AionJayT committed Oct 2, 2018
2 parents cf733af + 4d4f3f6 commit 67e953e
Show file tree
Hide file tree
Showing 27 changed files with 3,718 additions and 334 deletions.
39 changes: 38 additions & 1 deletion modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java
Expand Up @@ -514,12 +514,21 @@ public boolean skipTryToConnect(long blockNumber) {
return blockNumber > current + 32 || blockNumber < current - 32;
}

/**
* If using TOP pruning we need to check the pruning restriction for the block.
* Otherwise, there is not prune restriction.
*/
public boolean hasPruneRestriction() {
// no restriction when not in TOP pruning mode
return repository.usesTopPruning();
}

/**
* Heuristic for skipping the call to tryToConnect with block number that was already pruned.
*/
public boolean isPruneRestricted(long blockNumber) {
// no restriction when not in TOP pruning mode
if (!repository.usesTopPruning()) {
if (!hasPruneRestriction()) {
return false;
}
return blockNumber < bestBlockNumber.get() - repository.getPruneBlockCount() + 1;
Expand Down Expand Up @@ -1183,6 +1192,34 @@ public synchronized void storeBlock(AionBlock block, List<AionTxReceipt> receipt
setBestBlock(block);
}

@Override
public boolean storePendingStatusBlock(AionBlock block) {
return repository.getPendingBlockStore().addStatusBlock(block);
}

@Override
public int storePendingBlockRange(List<AionBlock> blocks) {
return repository.getPendingBlockStore().addBlockRange(blocks);
}

@Override
public Map<ByteArrayWrapper, List<AionBlock>> loadPendingBlocksAtLevel(long level) {
return repository.getPendingBlockStore().loadBlockRange(level);
}

@Override
public long nextBase(long current, long knownStatus) {
return repository.getPendingBlockStore().nextBase(current, knownStatus);
}

@Override
public void dropImported(
long level,
List<ByteArrayWrapper> ranges,
Map<ByteArrayWrapper, List<AionBlock>> blocks) {
repository.getPendingBlockStore().dropPendingQueues(level, ranges, blocks);
}

public boolean hasParentOnTheChain(AionBlock block) {
return getParent(block.getHeader()) != null;
}
Expand Down
2 changes: 1 addition & 1 deletion modAionImpl/src/org/aion/zero/impl/AionHub.java
Expand Up @@ -122,7 +122,7 @@ public static AionHub inst() {
return Holder.INSTANCE;
}

private static final int INIT_ERROR_EXIT_CODE = -1;
public static final int INIT_ERROR_EXIT_CODE = -1;

public AionHub() {
initializeHub(CfgAion.inst(), AionBlockchainImpl.inst(), AionRepositoryImpl.inst(), false);
Expand Down
29 changes: 26 additions & 3 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Expand Up @@ -24,6 +24,7 @@

import static org.aion.base.util.ByteUtil.EMPTY_BYTE_ARRAY;
import static org.aion.crypto.HashUtil.EMPTY_TRIE_HASH;
import static org.aion.zero.impl.AionHub.INIT_ERROR_EXIT_CODE;

import java.io.File;
import java.math.BigInteger;
Expand Down Expand Up @@ -62,6 +63,9 @@ public class AionRepositoryImpl

private TransactionStore<AionTransaction, AionTxReceipt, AionTxInfo> transactionStore;

// pending block store
private PendingBlockStore pendingStore;

/**
* used by getSnapShotTo
*
Expand Down Expand Up @@ -108,13 +112,22 @@ private void init() {
// Setup block store.
this.blockStore = new AionBlockStore(indexDatabase, blockDatabase, checkIntegrity);

this.pendingStore = new PendingBlockStore(pendingStoreProperties);

// Setup world trie.
worldState = createStateTrie();
} catch (Exception e) { // TODO - If any of the connections failed.
LOG.error("Unable to initialize repository.", e);
} catch (Exception e) {
LOGGEN.error("Shutdown due to failure to initialize repository.");
// the above message does not get logged without the printStackTrace below
e.printStackTrace();
System.exit(INIT_ERROR_EXIT_CODE);
}
}

public PendingBlockStore getPendingBlockStore() {
return this.pendingStore;
}

/** @implNote The transaction store is not locked within the repository implementation. */
public TransactionStore<AionTransaction, AionTxReceipt, AionTxInfo> getTransactionStore() {
return this.transactionStore;
Expand Down Expand Up @@ -511,7 +524,7 @@ public void commitBlock(A0BlockHeader blockHeader) {
detailsDS.syncLargeStorage();

if (pruneEnabled) {
if (blockHeader.getNumber() % archiveRate == 0 && stateDSPrune.isArchiveEnabled()) {
if (stateDSPrune.isArchiveEnabled() && blockHeader.getNumber() % archiveRate == 0) {
// archive block
worldState.saveDiffStateToDatabase(
blockHeader.getStateRoot(), stateDSPrune.getArchiveSource());
Expand Down Expand Up @@ -677,6 +690,16 @@ public void close() {
LOGGEN.error("Exception occurred while closing the block store.", e);
}

try {
if (pendingStore != null) {
pendingStore.close();
LOGGEN.info("Pending block store closed.");
pendingStore = null;
}
} catch (Exception e) {
LOGGEN.error("Exception occurred while closing the pending block store.", e);
}

try {
if (txPoolDatabase != null) {
txPoolDatabase.close();
Expand Down

0 comments on commit 67e953e

Please sign in to comment.