Search in sources :

Example 11 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class BlockchainEnergyTest method testEnergyUsageRecorded.

@Test
public void testEnergyUsageRecorded() {
    final int DEFAULT_TX_AMOUNT = 21000;
    final Address RECEIPT_ADDR = Address.wrap("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE");
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withDefaultAccounts().withValidatorConfiguration("simple").build();
    StandaloneBlockchain bc = bundle.bc;
    // TODO: where is the 21000 defined? bad to define magic variables
    int amount = (int) (bc.getGenesis().getNrgLimit() / DEFAULT_TX_AMOUNT);
    // (byte[] nonce, byte[] from, byte[] to, byte[] value, byte[] data, byte[] nrg, byte[] nrgPrice)
    List<AionTransaction> txs = new ArrayList<>();
    for (int i = 0; i < amount; i++) {
        // this transaction should send one (1) AION coin from acc[0] to RECEIPT_ADDR
        AionTransaction atx = new AionTransaction(ByteUtil.intToBytes(i), RECEIPT_ADDR, BigInteger.ONE.toByteArray(), ByteUtil.EMPTY_BYTE_ARRAY, 21000L, BigInteger.valueOf(5).multiply(BigInteger.TEN.pow(9)).longValue());
        atx.sign(bundle.privateKeys.get(0));
        txs.add(atx);
    }
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), txs, true);
    ImportResult result = bc.tryToConnect(block);
    assertThat(result).isEqualTo(ImportResult.IMPORTED_BEST);
    // proceed with connecting the next block, should observe an increase in energyLimit
    AionBlock secondBlock = bc.createNewBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
    assertThat(secondBlock.getNrgLimit()).isEqualTo(block.getNrgLimit());
    System.out.println(String.format("%d > %d", secondBlock.getNrgLimit(), block.getNrgLimit()));
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) Address(org.aion.base.type.Address) ArrayList(java.util.ArrayList) AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 12 with Address

use of org.aion.base.type.Address 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 13 with Address

use of org.aion.base.type.Address 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 14 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class BlockchainRewardTest method testBlockchainRewardMonotonicallyIncreasing.

/**
 * Test that blocks between the lower and upper bounds follow
 * a certain function [0, 259200]
 *
 * Note: this test is resource consuming!
 *
 * Check {@link org.aion.zero.impl.core.RewardsCalculator} for algorithm related
 * to the ramp-up block time
 */
@Ignore
@Test
public void testBlockchainRewardMonotonicallyIncreasing() {
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withDefaultAccounts().withValidatorConfiguration("simple").build();
    StandaloneBlockchain bc = bundle.bc;
    AionBlock block = bc.createNewBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
    ImportResult res = bc.tryToConnect(block);
    assertThat(res).isEqualTo(ImportResult.IMPORTED_BEST);
    Address coinbase = block.getCoinbase();
    BigInteger previousBalance = bc.getRepository().getBalance(coinbase);
    // first block already sealed
    for (int i = 2; i < 99999; i++) {
        AionBlock b = bc.createNewBlock(bc.getBestBlock(), Collections.EMPTY_LIST, true);
        ImportResult r = bc.tryToConnect(b);
        assertThat(r).isEqualTo(ImportResult.IMPORTED_BEST);
        // note the assumption here that blocks are mined by one coinbase
        BigInteger balance = bc.getRepository().getBalance(coinbase);
        assertThat(balance).isGreaterThan(previousBalance);
        previousBalance = balance;
        if (b.getNumber() % 1000 == 0)
            System.out.println("added block #: " + i);
    }
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 15 with Address

use of org.aion.base.type.Address in project aion by aionnetwork.

the class GenesisSpecificationTest method defaultGenesisBlockTest.

/**
 * Test that the default genesis block built from the builder produces the
 * correct genesis specs
 */
@Test
public void defaultGenesisBlockTest() {
    AionGenesis.Builder genesisBuilder = new AionGenesis.Builder();
    AionGenesis genesis = genesisBuilder.build();
    assertThat(genesis.getParentHash()).isEqualTo(AionGenesis.GENESIS_PARENT_HASH);
    assertThat(genesis.getCoinbase()).isEqualTo(AionGenesis.GENESIS_COINBASE);
    assertThat(genesis.getDifficulty()).isEqualTo(AionGenesis.GENESIS_DIFFICULTY);
    assertThat(genesis.getDifficultyBI()).isEqualTo(new BigInteger(1, AionGenesis.GENESIS_DIFFICULTY));
    assertThat(genesis.getLogBloom()).isEqualTo(AionGenesis.GENESIS_LOGSBLOOM);
    assertThat(genesis.getTimestamp()).isEqualTo(AionGenesis.GENESIS_TIMESTAMP);
    assertThat(genesis.getNrgConsumed()).isEqualTo(0);
    assertThat(genesis.getNrgLimit()).isEqualTo(AionGenesis.GENESIS_ENERGY_LIMIT);
    assertThat(genesis.getTxTrieRoot()).isEqualTo(HashUtil.EMPTY_TRIE_HASH);
    assertThat(genesis.getReceiptsRoot()).isEqualTo(HashUtil.EMPTY_TRIE_HASH);
    assertThat(genesis.getCumulativeDifficulty()).isEqualTo(new BigInteger(1, AionGenesis.GENESIS_DIFFICULTY));
    assertThat(genesis.getTransactionsList().isEmpty()).isEqualTo(true);
    Map<Address, AccountState> premined = genesis.getPremine();
    Set<Address> keySet = premined.keySet();
    // default set
    Set<Address> defaultKeySet = AionGenesis.GENESIS_PREMINE.keySet();
    assertThat(defaultKeySet.equals(keySet)).isEqualTo(true);
}
Also used : Address(org.aion.base.type.Address) BigInteger(java.math.BigInteger) AccountState(org.aion.mcf.core.AccountState) AionGenesis(org.aion.zero.impl.AionGenesis) Test(org.junit.Test)

Aggregations

Address (org.aion.base.type.Address)61 Test (org.junit.Test)34 BigInteger (java.math.BigInteger)29 AionTransaction (org.aion.zero.types.AionTransaction)23 ITransaction (org.aion.base.type.ITransaction)14 ECKey (org.aion.crypto.ECKey)13 DataWord (org.aion.mcf.vm.types.DataWord)11 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)10 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)9 IRepositoryCache (org.aion.base.db.IRepositoryCache)8 HashMap (java.util.HashMap)6 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)5 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)5 AccountState (org.aion.mcf.core.AccountState)5 AionBlock (org.aion.zero.impl.types.AionBlock)5 ArrayList (java.util.ArrayList)4 ImportResult (org.aion.mcf.core.ImportResult)4 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 IRepository (org.aion.base.db.IRepository)3 AionContractDetailsImpl (org.aion.zero.db.AionContractDetailsImpl)3