use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class BlockchainImplementationTest method testGetListOfHeadersEndWith_wGenesisBlock.
@Test
public void testGetListOfHeadersEndWith_wGenesisBlock() {
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
StandaloneBlockchain chain = bundle.bc;
// populate chain at random
generateRandomChain(chain, 6, 2, accounts, MAX_TX_PER_BLOCK);
MiningBlock genesis = chain.getGenesis();
byte[] genesisHash = genesis.getHash();
int qty;
// get one block
qty = 1;
assertEndWith_expectedOne(chain, genesisHash, qty);
// get list of 10 from genesis
// larger than block height
qty = 10;
assertEndWith_expectedOne(chain, genesisHash, qty);
// get list of -10 from genesis
// negative value
qty = -10;
assertEndWith_expectedOne(chain, genesisHash, qty);
// get list of 0 from genesis
// zero blocks requested
qty = 0;
assertEndWith_expectedOne(chain, genesisHash, qty);
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class BlockchainImplementationTest method testGetListOfHeadersStartFromBlock_wGenesisBlock.
@Test
public void testGetListOfHeadersStartFromBlock_wGenesisBlock() {
StandaloneBlockchain.Builder builder = new StandaloneBlockchain.Builder();
StandaloneBlockchain.Bundle bundle = builder.withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
StandaloneBlockchain chain = bundle.bc;
// populate chain at random
generateRandomChain(chain, 12, 2, accounts, MAX_TX_PER_BLOCK);
MiningBlock genesis = chain.getGenesis();
Block best = chain.getBestBlock();
assertThat(best.getNumber()).isGreaterThan(0L);
byte[] genesisHash = genesis.getHash();
// expected result with qty >= chain height
List<ByteArrayWrapper> expectedAll = new ArrayList<>();
Block block = chain.getBlockByHash(best.getParentHash());
while (block.getNumber() > 0) {
expectedAll.add(0, ByteArrayWrapper.wrap(block.getHash()));
block = chain.getBlockByHash(block.getParentHash());
}
expectedAll.add(0, ByteArrayWrapper.wrap(genesis.getHash()));
int qty;
// get one block
qty = 1;
assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
// get list of chain height from genesis block
// chain height
qty = (int) best.getNumber();
assertStartFromBlock(chain, 0, qty, expectedAll);
// get list of half from genesis block
qty = (int) (best.getNumber() / 2);
List<ByteArrayWrapper> expectedHalf = new ArrayList<>();
block = chain.getBlockByNumber(qty - 1);
for (int i = 0; i < qty; i++) {
expectedHalf.add(0, ByteArrayWrapper.wrap(block.getHash()));
block = chain.getBlockByHash(block.getParentHash());
}
assertStartFromBlock(chain, 0, qty, expectedHalf);
// get list of 20 from genesis
// larger than block height
qty = 24;
expectedAll.add(ByteArrayWrapper.wrap(best.getHash()));
assertStartFromBlock(chain, 0, qty, expectedAll);
// get list of -10 from genesis block
// negative value
qty = -10;
assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
// get list of 0 from genesis block
// zero blocks requested
qty = 0;
assertStartFromBlock_expectedOne(chain, genesisHash, 0, qty);
}
use of org.aion.zero.impl.types.MiningBlock 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.MiningBlock in project aion by aionnetwork.
the class BlockchainIntegrationTest method testCreateNewEmptyBlock.
@Test
public void testCreateNewEmptyBlock() {
StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
MiningBlock block = bc.createNewMiningBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
assertThat(block.getParentHash()).isEqualTo(bc.getGenesis().getHash());
}
use of org.aion.zero.impl.types.MiningBlock in project aion by aionnetwork.
the class BlockchainIntegrationTest method testSimpleFailedTransactionInsufficientBalance.
@Test
public void testSimpleFailedTransactionInsufficientBalance() {
// generate a recipient
final AionAddress receiverAddress = AddressUtils.wrapAddress("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE");
StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withValidatorConfiguration("simple").withDefaultAccounts().build();
StandaloneBlockchain bc = bundle.bc;
// (byte[] nonce, byte[] from, byte[] to, byte[] value, byte[] data)
ECKey key = bundle.privateKeys.get(0);
AionTransaction tx = AionTransaction.create(key, BigInteger.valueOf(1).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 1L, energyPrice, TransactionTypes.DEFAULT, null);
MiningBlock block = bc.createNewMiningBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
assertThat(block.getTransactionsList()).isEmpty();
assertThat(block.getTxTrieRoot()).isEqualTo(ConstantUtil.EMPTY_TRIE_HASH);
ImportResult connection = bc.tryToConnect(block);
assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
}
Aggregations