Search in sources :

Example 16 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class BlockchainIntegrationTest method testSimpleOneTokenBalanceTransfer.

@Test
public void testSimpleOneTokenBalanceTransfer() {
    // 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;
    final ECKey sender = bundle.privateKeys.get(0);
    final BigInteger senderInitialBalance = bc.getRepository().getBalance(Address.wrap(sender.getAddress()));
    AionTransaction tx = new AionTransaction(BigInteger.valueOf(0).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 21000L, 1L);
    tx.sign(sender);
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
    assertThat(block.getTransactionsList().size()).isEqualTo(1);
    assertThat(block.getTransactionsList().get(0)).isEqualTo(tx);
    ImportResult connection = bc.tryToConnect(block);
    assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
    // to be sure, perform some DB tests
    IRepository repo = bc.getRepository();
    assertThat(repo.getBalance(receiverAddress)).isEqualTo(BigInteger.valueOf(100));
    assertThat(repo.getBalance(Address.wrap(sender.getAddress()))).isEqualTo(senderInitialBalance.subtract(BigInteger.valueOf(21000)).subtract(BigInteger.valueOf(100)));
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) IRepository(org.aion.base.db.IRepository) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 17 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class BlockchainIntegrationTest method testPruningEnabledBalanceTransfer.

@Ignore
@Test
public void testPruningEnabledBalanceTransfer() {
    // generate a recipient
    final Address receiverAddress = Address.wrap(ByteUtil.hexStringToBytes("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE"));
    // generate bc bundle with pruning enabled
    StandaloneBlockchain.Bundle bundle = (new StandaloneBlockchain.Builder()).withBlockPruningEnabled().withValidatorConfiguration("simple").withDefaultAccounts().build();
    StandaloneBlockchain bc = bundle.bc;
    // desginate the first account in our list of private keys as the sender
    // (each key in the bundle is preloaded with balance)
    final ECKey sender = bundle.privateKeys.get(0);
    // generate transaction that transfers 100 tokens from sender to receiver
    // pk[0] -> receiverAddress
    AionTransaction tx = new AionTransaction(BigInteger.valueOf(0).toByteArray(), receiverAddress, BigInteger.valueOf(100).toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 21000L, 1L);
    tx.sign(sender);
    // create a new block containing a single transaction (tx)
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.singletonList(tx), true);
    // import the block to our blockchain
    ImportResult connection = bc.tryToConnect(block);
    assertThat(connection).isEqualTo(ImportResult.IMPORTED_BEST);
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) Address(org.aion.base.type.Address) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class PendingTxCacheTest method flushTest1.

@Test
public void flushTest1() {
    PendingTxCache cache = new PendingTxCache(1);
    List<AionTransaction> txn = getMockTransaction(0, 10, 0);
    List<AionTransaction> newCache = new ArrayList<>();
    for (ITransaction tx : txn) {
        newCache.add(cache.addCacheTx((AionTransaction) tx).get(0));
    }
    assertTrue(newCache.size() == 10);
    Map<Address, BigInteger> map = new HashMap<>();
    map.put(Address.wrap(key.get(0).getAddress()), BigInteger.TWO);
    newCache = cache.flush(map);
    assertTrue(newCache.size() == 1);
    Map<BigInteger, AionTransaction> cacheMap = cache.geCacheTx(Address.wrap(key.get(0).getAddress()));
    assertTrue(cacheMap.size() == 8);
}
Also used : Address(org.aion.base.type.Address) HashMap(java.util.HashMap) ITransaction(org.aion.base.type.ITransaction) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.zero.types.AionTransaction) Test(org.junit.Test)

Example 19 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class PendingTxCacheTest method getMockBigTransaction.

private List<AionTransaction> getMockBigTransaction(int startNonce, int num, int keyIndex, int size) {
    List<AionTransaction> txn = new ArrayList<>();
    String data = "";
    for (int i = 0; i < size; i++) {
        data += "1";
    }
    for (int i = startNonce; i < startNonce + num; i++) {
        AionTransaction tx = new AionTransaction(BigInteger.valueOf(i).toByteArray(), Address.wrap(key.get(keyIndex).getAddress()), Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString(data), 10000L, 1L);
        tx.sign(key.get(keyIndex));
        txn.add(tx);
    }
    return txn;
}
Also used : ArrayList(java.util.ArrayList) AionTransaction(org.aion.zero.types.AionTransaction)

Example 20 with AionTransaction

use of org.aion.zero.types.AionTransaction in project aion by aionnetwork.

the class PendingTxCacheTest method addCacheTxTest4.

@Test
public void addCacheTxTest4() {
    PendingTxCache cache = new PendingTxCache(1);
    List<AionTransaction> txn = getMockTransaction(0, 10, 0);
    txn.addAll(getMockTransaction(5, 10, 0));
    List<AionTransaction> newCache = new ArrayList<>();
    for (ITransaction tx : txn) {
        newCache.add(cache.addCacheTx((AionTransaction) tx).get(0));
    }
    assertTrue(newCache.size() == 20);
    Map<BigInteger, AionTransaction> cacheMap = cache.geCacheTx(Address.wrap(key.get(0).getAddress()));
    assertTrue(cacheMap.size() == 15);
}
Also used : ITransaction(org.aion.base.type.ITransaction) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.zero.types.AionTransaction) Test(org.junit.Test)

Aggregations

AionTransaction (org.aion.zero.types.AionTransaction)75 Test (org.junit.Test)44 BigInteger (java.math.BigInteger)30 ITransaction (org.aion.base.type.ITransaction)26 Address (org.aion.base.type.Address)23 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)21 AionBlock (org.aion.zero.impl.types.AionBlock)17 ArrayList (java.util.ArrayList)16 ECKey (org.aion.crypto.ECKey)12 AionTxReceipt (org.aion.zero.types.AionTxReceipt)7 TxRecpt (org.aion.api.server.types.TxRecpt)4 ImportResult (org.aion.mcf.core.ImportResult)4 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)4 ByteString (com.google.protobuf.ByteString)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ByteBuffer (java.nio.ByteBuffer)2 java.util (java.util)2 Entry (java.util.Map.Entry)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Collectors (java.util.stream.Collectors)2