Skip to content

Commit

Permalink
Remove getBlockStore from the blockchain
Browse files Browse the repository at this point in the history
Access to the block store must go though the repository.
  • Loading branch information
AlexandraRoatis committed Mar 6, 2020
1 parent 44a7108 commit 50ea22d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,6 @@ public void setEventManager(IEventMgr eventManager) {
this.evtMgr = eventManager;
}

public AionBlockStore getBlockStore() {
return repository.getBlockStore();
}

/**
* Referenced only by external
*
Expand Down Expand Up @@ -591,17 +587,17 @@ private long getSizeInternal() {

@Override
public Block getBlockByNumber(long blockNr) {
return getBlockStore().getChainBlockByNumber(blockNr);
return repository.getBlockStore().getChainBlockByNumber(blockNr);
}

@Override
public List<Block> getBlocksByNumber(long blockNr) {
return getBlockStore().getBlocksByNumber(blockNr);
return repository.getBlockStore().getBlocksByNumber(blockNr);
}

@Override
public List<Block> getBlocksByRange(long first, long last) {
return getBlockStore().getBlocksByRange(first, last);
return repository.getBlockStore().getBlocksByRange(first, last);
}

@Override
Expand Down Expand Up @@ -666,12 +662,12 @@ private Map<ByteArrayWrapper, AionTxInfo> getTransactionInfoByAlias(byte[] inner

@Override
public Block getBlockByHash(byte[] hash) {
return getBlockStore().getBlockByHash(hash);
return repository.getBlockStore().getBlockByHash(hash);
}

@Override
public List<byte[]> getListOfHashesEndWith(byte[] hash, int qty) {
return getBlockStore().getListHashesEndWith(hash, qty < 1 ? 1 : qty);
return repository.getBlockStore().getListHashesEndWith(hash, qty < 1 ? 1 : qty);
}

@Override
Expand All @@ -693,7 +689,7 @@ public List<byte[]> getListOfHashesStartFromBlock(long blockNumber, int qty) {

Block block = getBlockByNumber(endNumber);

List<byte[]> hashes = getBlockStore().getListHashesEndWith(block.getHash(), qty);
List<byte[]> hashes = repository.getBlockStore().getListHashesEndWith(block.getHash(), qty);

// asc order of hashes is required in the response
Collections.reverse(hashes);
Expand All @@ -711,18 +707,18 @@ public void setRepository(AionRepositoryImpl repository) {

private State pushState(byte[] bestBlockHash) {
State push = stateStack.push(new State());
this.bestBlock = getBlockStore().getBlockByHashWithInfo(bestBlockHash);
this.bestBlock = repository.getBlockStore().getBlockByHashWithInfo(bestBlockHash);

if (bestBlock.getHeader().getSealType() == BlockSealType.SEAL_POW_BLOCK) {
bestMiningBlock = (AionBlock) bestBlock;
if (forkUtility.isUnityForkActive(bestBlock.getNumber())) {
bestStakingBlock = (StakingBlock) getBlockStore().getBlockByHash(bestBlock.getParentHash());
bestStakingBlock = (StakingBlock) getBlockByHash(bestBlock.getParentHash());
} else {
bestStakingBlock = null;
}
} else if (bestBlock.getHeader().getSealType() == BlockSealType.SEAL_POS_BLOCK) {
bestStakingBlock = (StakingBlock) bestBlock;
bestMiningBlock = (AionBlock) getBlockStore().getBlockByHash(bestBlock.getParentHash());
bestMiningBlock = (AionBlock) getBlockByHash(bestBlock.getParentHash());
} else {
throw new IllegalStateException("Invalid best block data!");
}
Expand Down Expand Up @@ -780,7 +776,7 @@ private AionBlockSummary tryConnectAndFork(final Block block) {
}

// main branch become this branch cause we proved that total difficulty is greater
forkLevel = getBlockStore().reBranch(block);
forkLevel = repository.getBlockStore().reBranch(block);

// The main repository rebranch
this.repository = savedState.savedRepo;
Expand Down Expand Up @@ -834,7 +830,7 @@ private void loadBestMiningBlock() {
if (bestBlock.getHeader().getSealType() == BlockSealType.SEAL_POW_BLOCK) {
bestMiningBlock = (AionBlock) bestBlock;
} else if (bestBlock.getHeader().getSealType() == BlockSealType.SEAL_POS_BLOCK) {
bestMiningBlock = (AionBlock) getBlockStore().getBlockByHash(bestBlock.getParentHash());
bestMiningBlock = (AionBlock) getBlockByHash(bestBlock.getParentHash());
} else {
throw new IllegalStateException("Invalid block type");
}
Expand All @@ -847,7 +843,7 @@ private void loadBestStakingBlock() {
if (bestBlock.getHeader().getSealType() == BlockSealType.SEAL_POS_BLOCK) {
bestStakingBlock = (StakingBlock) bestBlock;
} else {
bestStakingBlock = (StakingBlock) getBlockStore().getBlockByHash(bestBlock.getParentHash());
bestStakingBlock = (StakingBlock) getBlockByHash(bestBlock.getParentHash());
}
}
}
Expand Down Expand Up @@ -912,18 +908,18 @@ public synchronized FastImportResult tryFastImport(final Block block) {
}

// check that the block is not already known
Block known = getBlockStore().getBlockByHash(block.getHash());
Block known = getBlockByHash(block.getHash());
if (known != null && known.getNumber() == block.getNumber()) {
return FastImportResult.KNOWN;
}

// a child must be present to import the parent
Block child = getBlockStore().getChainBlockByNumber(block.getNumber() + 1);
Block child = getBlockByNumber(block.getNumber() + 1);
if (child == null || !Arrays.equals(child.getParentHash(), block.getHash())) {
return FastImportResult.NO_CHILD;
} else {
// the total difficulty will be updated after the chain is complete
getBlockStore().saveBlock(block, ZERO, true);
repository.getBlockStore().saveBlock(block, ZERO, true);

if (LOG.isDebugEnabled()) {
LOG.debug(
Expand Down Expand Up @@ -953,12 +949,12 @@ public Pair<ByteArrayWrapper, Long> findMissingAncestor(Block block) {
byte[] currentHash = block.getHash();
long currentNumber = block.getNumber();

Block known = getBlockStore().getBlockByHash(currentHash);
Block known = getBlockByHash(currentHash);

while (known != null && known.getNumber() > 0) {
currentHash = known.getParentHash();
currentNumber--;
known = getBlockStore().getBlockByHash(currentHash);
known = getBlockByHash(currentHash);
}

if (known == null) {
Expand Down Expand Up @@ -1093,7 +1089,7 @@ public Pair<ImportResult, Long> tryToConnectWithTimedExecution(Block block) {
public Pair<ImportResult, AionBlockSummary> tryToConnectAndFetchSummary(Block block, boolean doExistCheck) {
// Check block exists before processing more rules
if (doExistCheck // skipped when redoing imports
&& getBlockStore().getMaxNumber() >= block.getNumber()
&& repository.getBlockStore().getMaxNumber() >= block.getNumber()
&& isBlockStored(block.getHash(), block.getNumber())) {

if (LOG.isDebugEnabled()) {
Expand Down Expand Up @@ -1156,7 +1152,7 @@ && isBlockStored(block.getHash(), block.getNumber())) {

// determine if the block parent is main chain or side chain
long parentHeight = block.getNumber() - 1; // inferred parent number
if (getBlockStore().isMainChain(block.getParentHash(), parentHeight)) {
if (repository.getBlockStore().isMainChain(block.getParentHash(), parentHeight)) {
// main chain parent, therefore can use its number for getting the cache
executionTypeForAVM = BlockCachingContext.SIDECHAIN;
cachedBlockNumberForAVM = parentHeight;
Expand Down Expand Up @@ -1337,7 +1333,7 @@ BlockContext createNewMiningBlockInternal(
LOG.warn("Tried to create 2 PoW blocks in a row");
return null;
} else {
Block[] blockFamily = getBlockStore().getTwoGenerationBlocksByHashWithInfo(parentHdr.getParentHash());
Block[] blockFamily = repository.getBlockStore().getTwoGenerationBlocksByHashWithInfo(parentHdr.getParentHash());
parentMiningBlock = blockFamily[0].getHeader();
parentMiningBlocksParent = blockFamily[1].getHeader();
diffCalculator = chainConfiguration.getUnityDifficultyCalculator();
Expand Down Expand Up @@ -1400,7 +1396,7 @@ private StakingBlock createNewStakingBlock(
newDiff = calculateFirstPoSDifficultyAtBlock(parent);
forkUtility.setNonceForkResetDiff(newDiff);
} else {
Block[] blockFamily = getBlockStore().getTwoGenerationBlocksByHashWithInfo(parentHdr.getParentHash());
Block[] blockFamily = repository.getBlockStore().getTwoGenerationBlocksByHashWithInfo(parentHdr.getParentHash());

BlockHeader parentStakingBlock = blockFamily[0].getHeader();
BlockHeader parentStakingBlocksParent = blockFamily[1].getHeader();
Expand Down Expand Up @@ -1715,7 +1711,7 @@ public Pair<AionBlockSummary, RepositoryCache> add(
public void flush() {
repository.flush();
try {
getBlockStore().flush();
repository.getBlockStore().flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Expand All @@ -1729,7 +1725,7 @@ private boolean needFlushByMemory(double maxMemoryPercents) {
}

private Block getParent(BlockHeader header) {
return getBlockStore().getBlockByHashWithInfo(header.getParentHash());
return repository.getBlockStore().getBlockByHashWithInfo(header.getParentHash());
}

public boolean isValid(BlockHeader header) {
Expand All @@ -1741,7 +1737,7 @@ public boolean isValid(BlockHeader header) {
return false;
}

Block[] threeGenParents = getBlockStore().getThreeGenerationBlocksByHashWithInfo(header.getParentHash());
Block[] threeGenParents = repository.getBlockStore().getThreeGenerationBlocksByHashWithInfo(header.getParentHash());

if (threeGenParents == null) {
return false;
Expand Down Expand Up @@ -2066,7 +2062,7 @@ private void storeBlock(Block block, List<AionTxReceipt> receipts, List<AionTxEx

BigInteger td = totalDifficulty.get();

getBlockStore().saveBlock(block, td, !fork);
repository.getBlockStore().saveBlock(block, td, !fork);

for (int i = 0; i < receipts.size(); i++) {
AionTxInfo infoWithInternalTxs = AionTxInfo.newInstanceWithInternalTransactions(receipts.get(i), block.getHashWrapper(), i, summaries.get(i).getInternalTransactions());
Expand Down Expand Up @@ -2286,7 +2282,7 @@ private List<BlockHeader> getContinuousHeaders(long bestNumber, long blockNumber
return emptyList();
}

List<BlockHeader> headers = getBlockStore().getListHeadersEndWith(startHash, qty);
List<BlockHeader> headers = repository.getBlockStore().getListHeadersEndWith(startHash, qty);

// blocks come with decreasing numbers
Collections.reverse(headers);
Expand Down Expand Up @@ -2525,7 +2521,7 @@ public synchronized boolean recoverWorldState(Repository repository, Block block

// find all the blocks missing a world state
do {
other = repo.getBlockStore().getBlockByHash(other.getParentHash());
other = getBlockByHash(other.getParentHash());

// cannot recover if no valid states exist (must build from genesis)
if (other == null) {
Expand Down Expand Up @@ -2562,7 +2558,7 @@ public synchronized boolean recoverWorldState(Repository repository, Block block

// Load bestblock for executing the CLI command.
if (bestBlock == null) {
bestBlock = getBlockStore().getBestBlock();
bestBlock = repo.getBlockStore().getBestBlock();

if (bestBlock instanceof AionBlock) {
bestMiningBlock = (AionBlock) bestBlock;
Expand Down Expand Up @@ -2648,12 +2644,12 @@ void resetPubBestBlock(Block blk) {

@Override
public boolean isMainChain(byte[] hash, long level) {
return getBlockStore().isMainChain(hash, level);
return repository.getBlockStore().isMainChain(hash, level);
}

@Override
public boolean isMainChain(byte[] hash) {
return getBlockStore().isMainChain(hash);
return repository.getBlockStore().isMainChain(hash);
}

@Override
Expand Down Expand Up @@ -2738,12 +2734,12 @@ public AionBlock getCachingMiningBlockTemplate(byte[] hash) {

@Override
public Block getBlockWithInfoByHash(byte[] hash) {
return getBlockStore().getBlockByHashWithInfo(hash);
return repository.getBlockStore().getBlockByHashWithInfo(hash);
}

@Override
public Block getBestBlockWithInfo() {
return getBlockStore().getBestBlockWithInfo();
return repository.getBlockStore().getBestBlockWithInfo();
}

void setNodeStatusCallback(SelfNodeStatusCallback callback) {
Expand Down Expand Up @@ -2883,7 +2879,7 @@ public void load(AionGenesis genesis, Logger genLOG) {
throw new IllegalStateException();
}

BigInteger totalDifficulty = getBlockStore().getBestBlockWithInfo().getTotalDifficulty();
BigInteger totalDifficulty = repository.getBlockStore().getBestBlockWithInfo().getTotalDifficulty();
setTotalDifficulty(totalDifficulty);
if (bestBlock.getTotalDifficulty().equals(BigInteger.ZERO)) {
// setting the object runtime value
Expand Down
2 changes: 0 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/blockchain/UnityChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ public interface UnityChain {

Block getBlockByHash(byte[] hash);

AionBlockStore getBlockStore();

Block getBestBlock();

void flush();
Expand Down
14 changes: 7 additions & 7 deletions modAionImpl/src/org/aion/zero/impl/db/DBUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ public static void dumpTestData(long blockNumber, String[] otherParameters) {
}

/** Used by internal world state recovery method. */
public static Status revertTo(IAionBlockchain blockchain, long nbBlock, Logger log) {
AionBlockStore store = blockchain.getBlockStore();
public static Status revertTo(AionBlockchainImpl blockchain, long nbBlock, Logger log) {
AionBlockStore store = blockchain.getRepository().getBlockStore();

Block bestBlock = store.getBestBlock();
if (bestBlock == null) {
Expand Down Expand Up @@ -692,7 +692,7 @@ public static Status queryTransaction(byte[] txHash) {

for (Map.Entry<ByteArrayWrapper, AionTxInfo> entry : txInfoList.entrySet()) {

Block block = blockchain.getBlockStore().getBlockByHash(entry.getKey().toBytes());
Block block = blockchain.getBlockByHash(entry.getKey().toBytes());
if (block == null) {
log.error("Cannot find the block data for the block hash from the transaction info. The database might be corrupted. Please consider reimporting the database by running ./aion.sh -n <network> --redo-import");
return Status.FAILURE;
Expand Down Expand Up @@ -732,7 +732,7 @@ public static Status queryBlock(long nbBlock) {
AionBlockchainImpl blockchain = new AionBlockchainImpl(cfg, false);

try {
List<Block> blocks = blockchain.getBlockStore().getAllChainBlockByNumber(nbBlock);
List<Block> blocks = blockchain.getRepository().getBlockStore().getAllChainBlockByNumber(nbBlock);

if (blocks == null || blocks.isEmpty()) {
log.error("Cannot find the block with given block height.");
Expand All @@ -748,7 +748,7 @@ public static Status queryBlock(long nbBlock) {
// TODO: the worldstate can not read the data after the stateroot has been setup, need
// to fix the issue first then the tooling can print the states between the block.

Block mainChainBlock = blockchain.getBlockStore().getChainBlockByNumber(nbBlock);
Block mainChainBlock = blockchain.getBlockByNumber(nbBlock);
if (mainChainBlock == null) {
log.error("Cannot find the main chain block with given block height.");
return Status.FAILURE;
Expand Down Expand Up @@ -797,12 +797,12 @@ public static Status queryAccount(AionAddress address) {
AionBlockchainImpl blockchain = new AionBlockchainImpl(cfg, false);

try {
Block bestBlock = blockchain.getBlockStore().getBestBlock();
Block bestBlock = blockchain.getRepository().getBlockStore().getBestBlock();

Repository<AccountState> repository =
blockchain
.getRepository()
.getSnapshotTo(((AionBlock) bestBlock).getStateRoot())
.getSnapshotTo((bestBlock).getStateRoot())
.startTracking();

AccountState account = repository.getAccountState(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void addThread_tryToConnect(
// checking total difficulty
if (result == ImportResult.IMPORTED_BEST
|| result == ImportResult.IMPORTED_NOT_BEST) {
AionBlockStore store = _chain.getBlockStore();
AionBlockStore store = _chain.getRepository().getBlockStore();

BigInteger tdFromStore = _chain.getTotalDifficultyForHash(_block.getHash());
BigInteger tdCalculated =
Expand Down Expand Up @@ -225,7 +225,7 @@ private void addThread_tryToConnect(
// checking total difficulty
if (result == ImportResult.IMPORTED_BEST
|| result == ImportResult.IMPORTED_NOT_BEST) {
AionBlockStore store = _chain.getBlockStore();
AionBlockStore store = _chain.getRepository().getBlockStore();

BigInteger tdFromStore =
_chain.getTotalDifficultyForHash(_block.getHash());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void testSimpleBlockchainNullPointer() {
// after instantiation, check that the following elements are set correctly
// note that this is pre-best block setting
assertThat(bc.getBestBlock()).isNotEqualTo(null);
assertThat(bc.getBlockStore()).isNotEqualTo(null);
assertThat(bc.getRepository()).isNotEqualTo(null);
assertThat(bc.getTotalDifficulty()).isEqualTo(bc.getBestBlock().getTotalDifficulty());
assertThat(bc.getTransactionStore()).isNotEqualTo(null);
Expand Down Expand Up @@ -315,7 +314,7 @@ public void testDisconnection() {
bundle.bc.createNewMiningBlock(bundle.bc.getBestBlock(), Collections.emptyList(), true);

// gets the first main-chain block
Block storedBlock1 = bundle.bc.getBlockStore().getChainBlockByNumber(1L);
Block storedBlock1 = bundle.bc.getBlockByNumber(1L);
System.out.println(ByteUtil.toHexString(storedBlock1.getHash()));
System.out.println(ByteUtil.toHexString(newBlock.getParentHash()));

Expand Down
2 changes: 1 addition & 1 deletion modApiServer/src/org/aion/api/server/ApiAion.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public Block getBlock(long blkNr) {

protected Map.Entry<Block, BigInteger> getBlockWithTotalDifficulty(long blkNr) {
if (blkNr > 0) {
Block block = this.ac.getBlockchain().getBlockStore().getChainBlockByNumber(blkNr);
Block block = this.ac.getBlockchain().getBlockByNumber(blkNr);
return (Map.entry(block, block.getTotalDifficulty()));
} else if (blkNr == 0) {
AionGenesis genBlk = CfgAion.inst().getGenesis();
Expand Down

0 comments on commit 50ea22d

Please sign in to comment.