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

AKI-707 Block submit restriction from the RPC server #1155

Merged
merged 2 commits into from
May 13, 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
35 changes: 27 additions & 8 deletions modAionImpl/src/org/aion/zero/impl/blockchain/AionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,24 @@ public UnityChain getBlockchain() {
return aionHub.getBlockchain();
}

/**
* @implNote import a new block from the api server or the internal PoW miner, the kernel will
* reject to import a new block has the same or less than the kernel block height to reduce the orphan
* block happens (AKI-707)
*/
public ImportResult addNewBlock(Block block) {
lock.lock();
getLock().lock();
try {
ImportResult importResult =
this.aionHub
.getBlockchain()
.tryToConnect(new BlockWrapper(block, true, false, false, false));
Block bestBlock = getAionHub().getBlockchain().getBestBlock();
ImportResult importResult;
if (bestBlock.getNumber() >= block.getNumber()) {
importResult = ImportResult.INVALID_BLOCK;
} else {
importResult =
this.aionHub
.getBlockchain()
.tryToConnect(new BlockWrapper(block, true, false, false, false));
}

LOG_GEN.debug("ImportResult:{} new block:{}", importResult, block);

Expand All @@ -110,7 +121,7 @@ public ImportResult addNewBlock(Block block) {

return importResult;
} finally{
lock.unlock();
getLock().unlock();
}
}

Expand All @@ -121,11 +132,11 @@ public ImportResult addNewBlock(Block block) {
public BlockContext getNewMiningBlockTemplate(BlockContext oldBlockTemplate, long systemTime) {
lock.lock();
try {
if (getBlockchain().isUnityForkEnabledAtNextBlock() && getBlockchain().getBestBlock().getHeader().getSealType() == Seal.PROOF_OF_WORK) {
Block bestBlock = getBlockchain().getBestBlock();
if (getBlockchain().isUnityForkEnabledAtNextBlock() && bestBlock.getHeader().getSealType().equals(Seal.PROOF_OF_WORK)) {
return null;
} else {
BlockContext context;
Block bestBlock = getBlockchain().getBestBlock();
byte[] bestBlockHash = bestBlock.getHash();

if (oldBlockTemplate == null
Expand Down Expand Up @@ -478,4 +489,12 @@ public long getNetworkBestBlockNumber() {
return networkBest.isPresent() ? networkBest.get() : 0;
}
}

/**
* return the chainImpl lock for the testing purpose
* @return the chainImpl lock
*/
ReentrantLock getLock() {
return lock;
}
}
63 changes: 63 additions & 0 deletions modAionImpl/test/org/aion/zero/impl/blockchain/AionImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.aion.zero.impl.blockchain;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import java.util.concurrent.locks.ReentrantLock;
import org.aion.mcf.blockchain.Block;
import org.aion.zero.impl.core.ImportResult;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

public class AionImplTest {

@Mock AionImpl chainImpl;
@Mock AionHub hub;
@Mock AionBlockchainImpl blockchain;
@Mock Block newBlock;
@Mock Block bestBlock;
@Mock ReentrantLock lock;

long blockHeight = 10L;

@Before
public void setup() {
chainImpl = mock(AionImpl.class);
hub = mock(AionHub.class);
blockchain = mock(AionBlockchainImpl.class);
newBlock = mock(Block.class);
bestBlock = mock(Block.class);
lock = mock(ReentrantLock.class);

doReturn(hub).when(chainImpl).getAionHub();
doReturn(blockchain).when(hub).getBlockchain();
doReturn(bestBlock).when(blockchain).getBestBlock();
doReturn(blockHeight).when(bestBlock).getNumber();

doReturn(lock).when(chainImpl).getLock();
doNothing().when(lock).lock();
doNothing().when(lock).unlock();
}

@Test
public void testAddNewBlockSameAsTheBestBlock() {
doReturn(blockHeight).when(newBlock).getNumber();

doCallRealMethod().when(chainImpl).addNewBlock(newBlock);
ImportResult result = chainImpl.addNewBlock(newBlock);
assertThat(result.equals(ImportResult.INVALID_BLOCK)).isTrue();
}

@Test
public void testAddNewBlockLessThenTheBestBlock() {
doReturn(blockHeight - 1).when(newBlock).getNumber();

doCallRealMethod().when(chainImpl).addNewBlock(newBlock);
ImportResult result = chainImpl.addNewBlock(newBlock);
assertThat(result.equals(ImportResult.INVALID_BLOCK)).isTrue();
}
}