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 BlockPropagationTest method testIgnoreSameBlock.
@Test
public void testIgnoreSameBlock() {
List<ECKey> accounts = generateDefaultAccounts();
StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
AionBlock block = bundle.bc.createNewBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
assertThat(block.getNumber()).isEqualTo(1);
byte[] sender = HashUtil.h256("node1".getBytes());
byte[] receiver = HashUtil.h256("receiver".getBytes());
NodeMock senderMock = new NodeMock(sender, 1);
NodeMock receiverMock = new NodeMock(receiver, 0);
Map<Integer, INode> node = new HashMap<>();
node.put(1, senderMock);
node.put(2, receiverMock);
AtomicInteger times = new AtomicInteger();
P2pMock p2pMock = new P2pMock(node) {
@Override
public void send(int _nodeId, Msg _msg) {
if (_nodeId != receiverMock.getIdHash())
throw new RuntimeException("should only send to receiver");
times.getAndIncrement();
}
};
StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
anotherBundle.bc, p2pMock, anotherBundle.bc.getBlockHeaderValidator());
// block is processed
assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
assertThat(times.get()).isEqualTo(1);
}
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);
}
Aggregations