use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class BlockchainIntegrationTest method testSimpleFailedTransactionInsufficientBalance.
@Test
public void testSimpleFailedTransactionInsufficientBalance() {
// generate a recipient
final Address receiverAddress = Address.wrap(ByteUtil.hexStringToBytes("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)
AionTransaction tx = new AionTransaction(BigInteger.valueOf(1).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 1L, 1L);
tx.sign(bundle.privateKeys.get(0));
AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
assertThat(block.getTransactionsList()).isEmpty();
assertThat(block.getTxTrieRoot()).isEqualTo(HashUtil.EMPTY_TRIE_HASH);
ImportResult connection = bc.tryToConnect(block);
assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
}
use of org.aion.zero.impl.types.AionBlock 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;
AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
assertThat(block.getParentHash()).isEqualTo(bc.getGenesis().getHash());
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiAion method getTransactionCount.
public long getTransactionCount(Address addr, long blkNr) {
AionBlock pBlk = this.getBlock(blkNr);
if (pBlk == null) {
LOG.error("ApiAion.getTransactionByBlockNumberAndIndex - can't find the block by the block number");
return -1;
}
long cnt = 0;
List<AionTransaction> txList = pBlk.getTransactionsList();
for (AionTransaction tx : txList) {
if (addr.equals(tx.getFrom())) {
cnt++;
}
}
return cnt;
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class ApiAion method getBlockTemplate.
public AionBlock getBlockTemplate() {
// TODO: Change to follow onBlockTemplate event mode defined in internal
// miner
// TODO: Track multiple block templates
AionBlock bestPendingState = ((AionPendingStateImpl) ac.getAionHub().getPendingState()).getBestBlock();
AionPendingStateImpl.TransactionSortedSet ret = new AionPendingStateImpl.TransactionSortedSet();
ret.addAll(ac.getAionHub().getPendingState().getPendingTransactions());
return ac.getAionHub().getBlockchain().createNewBlock(bestPendingState, new ArrayList<>(ret), false);
}
use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.
the class AionBlockchainImpl method createNewBlock.
public synchronized AionBlock createNewBlock(AionBlock parent, List<AionTransaction> txs, boolean waitUntilBlockTime) {
long time = System.currentTimeMillis() / THOUSAND_MS;
if (parent.getTimestamp() >= time) {
time = parent.getTimestamp() + 1;
while (waitUntilBlockTime && System.currentTimeMillis() / THOUSAND_MS <= time) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
break;
}
}
}
long energyLimit = this.chainConfiguration.calcEnergyLimit(parent.getHeader());
A0BlockHeader.Builder headerBuilder = new A0BlockHeader.Builder();
headerBuilder.withParentHash(parent.getHash()).withCoinbase(minerCoinbase).withNumber(parent.getNumber() + 1).withTimestamp(time).withExtraData(minerExtraData).withTxTrieRoot(calcTxTrie(txs)).withEnergyLimit(energyLimit);
AionBlock block = new AionBlock(headerBuilder.build(), txs);
block.getHeader().setDifficulty(ByteUtil.bigIntegerToBytes(this.chainConfiguration.getDifficultyCalculator().calculateDifficulty(block.getHeader(), parent.getHeader()), DIFFICULTY_BYTES));
/*
* Begin execution phase
*/
pushState(parent.getHash());
track = repository.startTracking();
track.rollback();
RetValidPreBlock preBlock = generatePreBlock(block);
/*
* Calculate the gas used for the included transactions
*/
long totalEnergyUsed = 0;
for (AionTxExecSummary summary : preBlock.summaries) {
totalEnergyUsed = totalEnergyUsed + summary.getNrgUsed().longValueExact();
}
byte[] stateRoot = getRepository().getRoot();
popState();
/*
* End execution phase
*/
Bloom logBloom = new Bloom();
for (AionTxReceipt receipt : preBlock.receipts) {
logBloom.or(receipt.getBloomFilter());
}
block.seal(preBlock.txs, calcTxTrie(preBlock.txs), stateRoot, logBloom.getData(), calcReceiptsTrie(preBlock.receipts), totalEnergyUsed);
return block;
}
Aggregations