Skip to content

Commit

Permalink
Add tests and bugfix for getBlockByHashWithInfo
Browse files Browse the repository at this point in the history
The method was incorrectly returning all blocks flagged as main chain.
  • Loading branch information
AlexandraRoatis committed Mar 12, 2020
1 parent 6e049e4 commit d4fbdd4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
7 changes: 4 additions & 3 deletions modAionImpl/src/org/aion/zero/impl/db/AionBlockStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,10 @@ boolean isBlockStored(byte[] hash, long number) {
}

/**
* Retrieve block with unity protocol info
* @param hash given hash of the block
* @return the block data has matched hash with unity protocol info
* Retrieve a block with unity protocol info.
*
* @param hash the hash of the requested block
* @return the block data for the given hash with unity protocol info
*/
public Block getBlockByHashWithInfo(byte[] hash) {
if (hash == null) {
Expand Down
4 changes: 2 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/types/AbstractBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public abstract class AbstractBlock implements Block {

/** use for cli tooling */
Boolean mainChain;
boolean mainChain = false;

// set from BlockInfos in index database
protected BigInteger totalDifficulty;
Expand Down Expand Up @@ -93,7 +93,7 @@ public void setTotalDifficulty(BigInteger totalDifficulty) {

@Override
public boolean isMainChain() {
return mainChain == null ? true : mainChain;
return mainChain;
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions modAionImpl/src/org/aion/zero/impl/types/AionBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ public String toString() {
toStringBuff.append(" total difficulty=").append(totalDifficulty).append("\n");
}

if (mainChain != null) {
toStringBuff.append(" mainChain=").append(mainChain ? "yes" : "no").append("\n");
}
toStringBuff.append(" mainChain=").append(mainChain ? "yes" : "no").append("\n");

if (!getTransactionsList().isEmpty()) {
toStringBuff
Expand Down
4 changes: 1 addition & 3 deletions modAionImpl/src/org/aion/zero/impl/types/StakingBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,7 @@ public String toString() {
toStringBuff.append(" total difficulty=").append(totalDifficulty).append("\n");
}

if (mainChain != null) {
toStringBuff.append(" mainChain=").append(mainChain ? "yes" : "no").append("\n");
}
toStringBuff.append(" mainChain=").append(mainChain ? "yes" : "no").append("\n");

if (!getTransactionsList().isEmpty()) {
toStringBuff.append("Txs [\n");
Expand Down
58 changes: 58 additions & 0 deletions modAionImpl/test/org/aion/zero/impl/db/AionBlockStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.aion.util.TestResources;
import org.aion.util.types.AddressUtils;
import org.aion.zero.impl.types.AionBlock;
import org.aion.zero.impl.types.BlockUtil;
import org.apache.commons.lang3.RandomUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -229,6 +231,62 @@ public void testGetBlocksByRange_withAscendingOrderAndIncorrectHeight() {
assertThat(store.getBlocksByRange(first.getNumber(), last.getNumber())).isNull();
}

@Test
public void testGetBlockByHashWithInfo_withNullInput() {
AionBlockStore store = new AionBlockStore(index, blocks, false);
Block block = store.getBlockByHashWithInfo(null);
assertThat(block).isNull();
}

@Test
public void testGetBlockByHashWithInfo_withMissingBlock() {
byte[] blockHash = RandomUtils.nextBytes(32);

AionBlockStore store = new AionBlockStore(index, blocks, false);
assertThat(index.isEmpty()).isTrue();
assertThat(blocks.isEmpty()).isTrue();

Block block = store.getBlockByHashWithInfo(blockHash);
assertThat(block).isNull();
}

@Test
public void testGetBlockByHashWithInfo() {
Block givenBlock = consecutiveBlocks.get(0);
BigInteger totalDifficulty = BigInteger.TEN;

AionBlockStore store = new AionBlockStore(index, blocks, false);
// does not require accurate total difficulty
store.saveBlock(givenBlock, totalDifficulty, true);

Block block = store.getBlockByHashWithInfo(givenBlock.getHash());
assertThat(block).isEqualTo(givenBlock);
assertThat(block.getTotalDifficulty()).isEqualTo(totalDifficulty);
assertThat(block.isMainChain()).isTrue();
}

@Test
public void testGetBlockByHashWithInfo_withSideChain() {
Block givenBlock = consecutiveBlocks.get(0);
BigInteger totalDifficulty = BigInteger.TWO;

BigInteger sideTotalDifficulty = BigInteger.TEN;
Block sideBlock = BlockUtil.newBlockFromRlp(givenBlock.getEncoded());
sideBlock.updateHeaderDifficulty(sideTotalDifficulty.toByteArray());
assertThat(givenBlock.getHash()).isNotEqualTo(sideBlock.getHash());
assertThat(givenBlock.getEncoded()).isNotEqualTo(sideBlock.getEncoded());

AionBlockStore store = new AionBlockStore(index, blocks, false);
// does not require accurate total difficulty
store.saveBlock(givenBlock, totalDifficulty, false);
store.saveBlock(sideBlock, sideTotalDifficulty, true);

Block block = store.getBlockByHashWithInfo(givenBlock.getHash());
assertThat(block).isEqualTo(givenBlock);
assertThat(block.getTotalDifficulty()).isEqualTo(totalDifficulty);
assertThat(block.isMainChain()).isFalse();
}

@Test
public void testRollback() {

Expand Down

0 comments on commit d4fbdd4

Please sign in to comment.