use of org.aion.zero.impl.types.MiningBlockHeader in project aion by aionnetwork.
the class BlockchainTestUtils method generateNextBlock.
/**
* @param chain blockchain implementation to be populated
* @param accounts existing accounts
* @param txCount maximum number of transactions per block
*/
public static Block generateNextBlock(StandaloneBlockchain chain, List<ECKey> accounts, int txCount) {
Block block, parent = chain.getBestBlock();
AionRepositoryImpl repo = chain.getRepository();
List<AionTransaction> txs = generateTransactions(txCount, accounts, repo);
long time = System.currentTimeMillis();
block = chain.createNewMiningBlockInternal(parent, txs, true, time / TEN_THOUSAND_MS).block;
MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader((MiningBlockHeader) block.getHeader()).withExtraData(String.valueOf(time).getBytes()).build();
block.updateHeader(newBlockHeader);
return block;
}
use of org.aion.zero.impl.types.MiningBlockHeader in project aion by aionnetwork.
the class BlockchainIntegrationTest method testDisconnection.
/**
* This is a special case for testing that the blockchain stores disconnected blocks if we
* violate the contract of changing the block contents after importing best
*
* <p>This behaviour was originally observed when running a pooled miner
*/
@Test
public void testDisconnection() {
StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
// store this as the best block,
MiningBlock block = bundle.bc.createNewMiningBlock(bc.getGenesis(), Collections.emptyList(), true);
byte[] block1Hash = block.getHash();
// now modify this block to have a different value after connecting
ImportResult result = bundle.bc.tryToConnect(block);
assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
// now modify the block in some way
MiningBlockHeader newBlockHeader = MiningBlockHeader.Builder.newInstance().withHeader(block.getHeader()).withExtraData("other block".getBytes()).build();
block.updateHeader(newBlockHeader);
// now try submitting the block again
result = bundle.bc.tryToConnect(block);
assertThat(result).isEqualTo(ImportResult.IMPORTED_NOT_BEST);
// now try creating a new block
MiningBlock newBlock = bundle.bc.createNewMiningBlock(bundle.bc.getBestBlock(), Collections.emptyList(), true);
// gets the first main-chain block
Block storedBlock1 = bundle.bc.getBlockByNumber(1L);
System.out.println(ByteUtil.toHexString(storedBlock1.getHash()));
System.out.println(ByteUtil.toHexString(newBlock.getParentHash()));
assertThat(newBlock.getParentHash()).isNotEqualTo(storedBlock1.getHash());
assertThat(newBlock.getParentHash()).isEqualTo(block.getHash());
}
use of org.aion.zero.impl.types.MiningBlockHeader in project aion by aionnetwork.
the class PendingBlockStoreTest method testDropPendingQueues_wException.
@Test
public void testDropPendingQueues_wException() {
Properties props = new Properties();
props.setProperty(Props.DB_TYPE, DBVendor.MOCKDB.toValue());
PendingBlockStore pb = null;
try {
pb = new PendingBlockStore(props);
} catch (IOException e) {
e.printStackTrace();
}
assertThat(pb.isOpen()).isTrue();
// add first queue
List<Block> blocks = TestResources.consecutiveBlocks(6);
Block first = blocks.get(0);
pb.addBlockRange(blocks);
// add second queue
MiningBlock altBlock = (MiningBlock) BlockUtil.newBlockFromRlp(first.getEncoded());
MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(altBlock.getHeader()).withExtraData("random".getBytes()).build();
altBlock.updateHeader(newHeader);
List<Block> sideChain = new ArrayList<>();
sideChain.add(altBlock);
pb.addBlockRange(sideChain);
// check storage updates
assertThat(pb.getIndexSize()).isEqualTo(7);
assertThat(pb.getLevelSize()).isEqualTo(1);
assertThat(pb.getQueueSize()).isEqualTo(2);
// closing the pending block store to cause exception
pb.close();
// test drop functionality
Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
pb.dropPendingQueues(first.getNumber(), actual.keySet(), actual);
}
use of org.aion.zero.impl.types.MiningBlockHeader in project aion by aionnetwork.
the class SyncMgrTest method testValidateAndAddHeaders_withInvalidHeader.
@Test
public void testValidateAndAddHeaders_withInvalidHeader() {
int nodeId = 1;
String displayId = "peer1";
List<BlockHeader> sequentialHeaders = new ArrayList<>();
sequentialHeaders.add(consecutiveHeaders.get(0));
sequentialHeaders.add(consecutiveHeaders.get(1));
// Break energy consumed rule.
List<BlockHeader> invalidHeaderList = new ArrayList<>(sequentialHeaders);
assertThat(consecutiveHeaders.get(2).getEnergyConsumed()).isGreaterThan(0L);
invalidHeaderList.add(MiningBlockHeader.Builder.newInstance().withHeader((MiningBlockHeader) consecutiveHeaders.get(2)).withEnergyConsumed(0L).build());
syncMgr.validateAndAddHeaders(nodeId, displayId, invalidHeaderList);
verify(p2pMgr, never()).errCheck(nodeId, displayId);
// Check that the sequential subset of headers was stored.
assertThat(syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, invalidHeaderList.size(), invalidHeaderList.get(0).getTxTrieRootWrapper())).isNull();
List<BlockHeader> stored = syncMgr.syncHeaderRequestManager.matchAndDropHeaders(nodeId, sequentialHeaders.size(), sequentialHeaders.get(0).getTxTrieRootWrapper());
assertThat(stored.size()).isEqualTo(sequentialHeaders.size());
assertThat(stored).containsAllIn(sequentialHeaders);
}
use of org.aion.zero.impl.types.MiningBlockHeader in project aion by aionnetwork.
the class PendingBlockStoreTest method testLoadBlockRange.
@Test
public void testLoadBlockRange() {
Properties props = new Properties();
props.setProperty(Props.DB_TYPE, DBVendor.MOCKDB.toValue());
PendingBlockStore pb = null;
try {
pb = new PendingBlockStore(props);
} catch (IOException e) {
e.printStackTrace();
}
assertThat(pb.isOpen()).isTrue();
List<Block> allBlocks = TestResources.consecutiveBlocks(8);
// 1. test with empty storage
assertThat(pb.loadBlockRange(100)).isEmpty();
// 2. test with valid range
List<Block> blocks = allBlocks.subList(0, 6);
Block first = blocks.get(0);
assertThat(blocks.size()).isEqualTo(6);
assertThat(pb.addBlockRange(blocks)).isEqualTo(6);
Map<ByteArrayWrapper, List<Block>> actual = pb.loadBlockRange(first.getNumber());
assertThat(actual.size()).isEqualTo(1);
assertThat(actual.get(ByteArrayWrapper.wrap(first.getHash()))).isEqualTo(blocks);
// 3. test with multiple queues
// create side chain
MiningBlock altBlock = (MiningBlock) BlockUtil.newBlockFromRlp(first.getEncoded());
MiningBlockHeader newHeader = MiningBlockHeader.Builder.newInstance().withHeader(altBlock.getHeader()).withExtraData("random".getBytes()).build();
altBlock.updateHeader(newHeader);
assertThat(altBlock.equals(first)).isFalse();
List<Block> sideChain = new ArrayList<>();
sideChain.add(altBlock);
assertThat(pb.addBlockRange(sideChain)).isEqualTo(1);
// check functionality
actual = pb.loadBlockRange(first.getNumber());
assertThat(actual.size()).isEqualTo(2);
assertThat(actual.get(ByteArrayWrapper.wrap(first.getHash()))).isEqualTo(blocks);
assertThat(actual.get(ByteArrayWrapper.wrap(altBlock.getHash()))).isEqualTo(sideChain);
// 4. test with empty level
long level = first.getNumber();
assertThat(pb.loadBlockRange(level - 1)).isEmpty();
assertThat(pb.loadBlockRange(level + 1)).isEmpty();
}
Aggregations