Search in sources :

Example 61 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class AvmBulkTransactionTest method importBlockWithContractDeploysAndSubsequentCalls.

/**
 * Ensures that AVM contracts can be deployed and called within the same block.
 */
@Test
public void importBlockWithContractDeploysAndSubsequentCalls() {
    BigInteger expectedNonce = getNonce(deployerKey);
    BigInteger initialBalance = getBalance(deployerKey);
    // note: 3 contract deployments would pass the block energy limit
    int nbCreateTransactions = 2;
    int nbCallTransactions = 2;
    int nbTransactions = nbCreateTransactions + nbCreateTransactions * nbCallTransactions;
    List<AionTransaction> transactions = new ArrayList<>();
    for (int j = 0; j < nbCreateTransactions; j++) {
        // create contract transaction
        AionTransaction deployTx = makeAvmContractCreateTransaction(deployerKey, expectedNonce);
        expectedNonce = expectedNonce.add(BigInteger.ONE);
        AionAddress deployedContract = TxUtil.calculateContractAddress(deployTx);
        transactions.add(deployTx);
        // subsequent call transactions
        for (int i = 0; i < nbCallTransactions; i++) {
            transactions.add(makeAvmContractCallTransaction(deployerKey, expectedNonce, deployedContract));
            expectedNonce = expectedNonce.add(BigInteger.ONE);
        }
    }
    // process the transactions in bulk
    AionBlockSummary blockSummary = sendTransactionsInBulkInSingleBlock(transactions);
    // verify all transactions were successful
    assertEquals(nbTransactions, blockSummary.getSummaries().size());
    for (AionTxExecSummary transactionSummary : blockSummary.getSummaries()) {
        System.out.println(transactionSummary.getReceipt());
        assertTrue(transactionSummary.getReceipt().isSuccessful());
    }
    BigInteger expectedBalance = initialBalance;
    for (int i = 0; i < nbTransactions; i++) {
        BigInteger energyUsed = BigInteger.valueOf(blockSummary.getSummaries().get(i).getReceipt().getEnergyUsed());
        expectedBalance = expectedBalance.subtract(energyUsed.multiply(BigInteger.valueOf(energyPrice)));
    }
    assertEquals(expectedBalance, getBalance(deployerKey));
    assertEquals(expectedNonce, getNonce(deployerKey));
}
Also used : AionAddress(org.aion.types.AionAddress) AionBlockSummary(org.aion.zero.impl.types.AionBlockSummary) AionTxExecSummary(org.aion.base.AionTxExecSummary) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) AionTransaction(org.aion.base.AionTransaction) Test(org.junit.Test)

Example 62 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class GenesisSpecificationTest method overrideGenesisBlockTest.

/**
 * Test that the genesis block can be overrode by certain configuration options that correspond
 * to the options provided by {@link AionGenesis.Builder}
 */
@Test
public void overrideGenesisBlockTest() {
    AionGenesis.Builder genesisBuilder = new AionGenesis.Builder();
    // values to override defaults with
    byte[] overrideHash = ByteUtil.hexStringToBytes("DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF");
    byte[] overrideAddress = ByteUtil.hexStringToBytes("DEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEFDEADBEEF");
    BigInteger overrideValue = BigInteger.valueOf(1337);
    AccountState defaultAccountState = new AccountState(overrideValue, overrideValue);
    HashSet<AionAddress> accountStateSet = new HashSet<>();
    accountStateSet.add(new AionAddress(overrideHash));
    genesisBuilder.withParentHash(overrideHash).withCoinbase(new AionAddress(overrideAddress)).withDifficulty(overrideValue.toByteArray()).withEnergyLimit(overrideValue.longValue()).withNonce(overrideHash).withNumber(overrideValue.longValue()).withTimestamp(overrideValue.longValue()).addPreminedAccount(new AionAddress(overrideAddress), defaultAccountState);
    AionGenesis genesis = genesisBuilder.buildForTest();
    assertThat(genesis.getParentHash()).isEqualTo(overrideHash);
    assertThat(genesis.getCoinbase().toByteArray()).isEqualTo(overrideAddress);
    assertThat(genesis.getDifficulty()).isEqualTo(overrideValue.toByteArray());
    assertThat(genesis.getDifficultyBI()).isEqualTo(overrideValue);
    assertThat(genesis.getTimestamp()).isEqualTo(overrideValue.longValue());
    assertThat(genesis.getNrgConsumed()).isEqualTo(0);
    assertThat(genesis.getNrgLimit()).isEqualTo(overrideValue.longValue());
    assertThat(genesis.getTxTrieRoot()).isEqualTo(ConstantUtil.EMPTY_TRIE_HASH);
    assertThat(genesis.getReceiptsRoot()).isEqualTo(ConstantUtil.EMPTY_TRIE_HASH);
    assertThat(genesis.getDifficultyBI()).isEqualTo(overrideValue);
    assertThat(genesis.getTransactionsList().isEmpty()).isEqualTo(true);
    assertThat(genesis.getPremine().keySet()).isEqualTo(accountStateSet);
}
Also used : AionAddress(org.aion.types.AionAddress) BigInteger(java.math.BigInteger) AccountState(org.aion.base.AccountState) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 63 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class StakingBlockHeaderTest method testBlockHeaderFromRLP.

// Test is a self referencing
@Test
public void testBlockHeaderFromRLP() {
    long time = System.currentTimeMillis() / 1000;
    StakingBlockHeader.Builder builder = StakingBlockHeader.Builder.newInstance();
    builder.fromUnsafeSource().withCoinbase(new AionAddress(COINBASE)).withTxTrieRoot(TRIE_ROOT).withExtraData(EXTRA_DATA).withReceiptTrieRoot(RECEIPT_ROOT).withTimestamp(time).withNumber(NUMBER_BYTES).withEnergyConsumed(ENERGY_CONSUMED_BYTES).withEnergyLimit(ENERGY_LIMIT_BYTES).withParentHash(PARENT_HASH).withSeed(SEED).withSigningPublicKey(SIGNINGPUBKEY).withSignature(SIGNATURE).withDefaultStateRoot().withDefaultLogsBloom().withDefaultDifficulty();
    StakingBlockHeader header = builder.build();
    byte[] encoded = header.getEncoded();
    StakingBlockHeader reconstructed = StakingBlockHeader.Builder.newInstance(true).withRlpEncodedData(encoded).build();
    assertThat(reconstructed.getCoinbase()).isEqualTo(header.getCoinbase());
    assertThat(reconstructed.getTxTrieRoot()).isEqualTo(header.getTxTrieRoot());
    assertThat(reconstructed.getExtraData()).isEqualTo(header.getExtraData());
    assertThat(reconstructed.getReceiptsRoot()).isEqualTo(header.getReceiptsRoot());
    assertThat(reconstructed.getTimestamp()).isEqualTo(header.getTimestamp());
    assertThat(reconstructed.getNumber()).isEqualTo(header.getNumber());
    assertThat(reconstructed.getEnergyConsumed()).isEqualTo(header.getEnergyConsumed());
    assertThat(reconstructed.getEnergyLimit()).isEqualTo(header.getEnergyLimit());
    assertThat(reconstructed.getParentHash()).isEqualTo(header.getParentHash());
    assertThat(reconstructed.getSeedOrProof()).isEqualTo(header.getSeedOrProof());
    assertThat(reconstructed.getSignature()).isEqualTo(header.getSignature());
    assertThat(reconstructed.getSigningPublicKey()).isEqualTo(header.getSigningPublicKey());
    assertThat(reconstructed.getStateRoot()).isEqualTo(header.getStateRoot());
    assertThat(reconstructed.getDifficulty()).isEqualTo(header.getDifficulty());
    assertThat(reconstructed.getSealType() == header.getSealType());
}
Also used : AionAddress(org.aion.types.AionAddress) Test(org.junit.Test)

Example 64 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class StakingBlockHeaderTest method testBlockHeaderFromRLPwithProof.

@Test
public void testBlockHeaderFromRLPwithProof() {
    long time = System.currentTimeMillis() / 1000;
    StakingBlockHeader.Builder builder = StakingBlockHeader.Builder.newInstance();
    builder.fromUnsafeSource().withCoinbase(new AionAddress(COINBASE)).withTxTrieRoot(TRIE_ROOT).withExtraData(EXTRA_DATA).withReceiptTrieRoot(RECEIPT_ROOT).withTimestamp(time).withNumber(NUMBER_BYTES).withEnergyConsumed(ENERGY_CONSUMED_BYTES).withEnergyLimit(ENERGY_LIMIT_BYTES).withParentHash(PARENT_HASH).withProof(StakingBlockHeader.DEFAULT_PROOF).withSigningPublicKey(SIGNINGPUBKEY).withSignature(SIGNATURE).withDefaultStateRoot().withDefaultLogsBloom().withDefaultDifficulty();
    StakingBlockHeader header = builder.build();
    byte[] encoded = header.getEncoded();
    StakingBlockHeader reconstructed = StakingBlockHeader.Builder.newInstance(true).withRlpEncodedData(encoded).build();
    assertThat(reconstructed.getCoinbase()).isEqualTo(header.getCoinbase());
    assertThat(reconstructed.getTxTrieRoot()).isEqualTo(header.getTxTrieRoot());
    assertThat(reconstructed.getExtraData()).isEqualTo(header.getExtraData());
    assertThat(reconstructed.getReceiptsRoot()).isEqualTo(header.getReceiptsRoot());
    assertThat(reconstructed.getTimestamp()).isEqualTo(header.getTimestamp());
    assertThat(reconstructed.getNumber()).isEqualTo(header.getNumber());
    assertThat(reconstructed.getEnergyConsumed()).isEqualTo(header.getEnergyConsumed());
    assertThat(reconstructed.getEnergyLimit()).isEqualTo(header.getEnergyLimit());
    assertThat(reconstructed.getParentHash()).isEqualTo(header.getParentHash());
    assertThat(reconstructed.getSeedOrProof()).isEqualTo(header.getSeedOrProof());
    assertThat(reconstructed.getSignature()).isEqualTo(header.getSignature());
    assertThat(reconstructed.getSigningPublicKey()).isEqualTo(header.getSigningPublicKey());
    assertThat(reconstructed.getStateRoot()).isEqualTo(header.getStateRoot());
    assertThat(reconstructed.getDifficulty()).isEqualTo(header.getDifficulty());
    assertThat(reconstructed.getSealType() == header.getSealType());
}
Also used : AionAddress(org.aion.types.AionAddress) Test(org.junit.Test)

Example 65 with AionAddress

use of org.aion.types.AionAddress in project aion by aionnetwork.

the class StakingBlockHeaderTest method testBlockHeaderFromRLPwithInvalidSeedProofLength.

@Test(expected = IllegalArgumentException.class)
public void testBlockHeaderFromRLPwithInvalidSeedProofLength() {
    long time = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    byte[] number = RLP.encodeBigInteger(new BigInteger(1, NUMBER_BYTES));
    byte[] parentHash = RLP.encodeElement(PARENT_HASH);
    byte[] coinbase = RLP.encodeElement(new AionAddress(COINBASE).toByteArray());
    byte[] stateRoot = RLP.encodeElement(ConstantUtil.EMPTY_TRIE_HASH);
    byte[] txTrieRoot = RLP.encodeElement(TRIE_ROOT);
    byte[] receiptTrieRoot = RLP.encodeElement(RECEIPT_ROOT);
    byte[] logsBloom = RLP.encodeElement(EMPTY_BLOOM);
    byte[] difficulty = RLP.encodeElement(ByteUtil.EMPTY_HALFWORD);
    byte[] extraData = RLP.encodeElement(EXTRA_DATA);
    byte[] energyConsumed = RLP.encodeBigInteger(new BigInteger(1, ENERGY_CONSUMED_BYTES));
    byte[] energyLimit = RLP.encodeBigInteger(new BigInteger(1, ENERGY_LIMIT_BYTES));
    byte[] timestamp = RLP.encodeBigInteger(BigInteger.valueOf(time));
    byte[] seedOrProof = RLP.encodeElement(new byte[StakingBlockHeader.PROOF_LENGTH + 1]);
    byte[] signature = RLP.encodeElement(SIGNATURE);
    byte[] signingPublicKey = RLP.encodeElement(SIGNINGPUBKEY);
    byte[] sealType = RLP.encodeElement(new byte[] { Seal.PROOF_OF_STAKE.getSealId() });
    byte[] rlpEncoded = RLP.encodeList(sealType, number, parentHash, coinbase, stateRoot, txTrieRoot, receiptTrieRoot, logsBloom, difficulty, extraData, energyConsumed, energyLimit, timestamp, seedOrProof, signature, signingPublicKey);
    StakingBlockHeader reconstructed = StakingBlockHeader.Builder.newInstance(true).withRlpEncodedData(rlpEncoded).build();
}
Also used : AionAddress(org.aion.types.AionAddress) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Aggregations

AionAddress (org.aion.types.AionAddress)491 Test (org.junit.Test)364 AionTransaction (org.aion.base.AionTransaction)275 BigInteger (java.math.BigInteger)194 ImportResult (org.aion.zero.impl.core.ImportResult)110 AionTxExecSummary (org.aion.base.AionTxExecSummary)97 RepositoryCache (org.aion.base.db.RepositoryCache)90 MiningBlock (org.aion.zero.impl.types.MiningBlock)89 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)80 ArrayList (java.util.ArrayList)76 ECKey (org.aion.crypto.ECKey)74 AionTxReceipt (org.aion.base.AionTxReceipt)60 Block (org.aion.zero.impl.types.Block)60 AccountState (org.aion.base.AccountState)56 HashMap (java.util.HashMap)42 DataWord (org.aion.util.types.DataWord)39 PrecompiledTransactionResult (org.aion.precompiled.PrecompiledTransactionResult)36 InternalTransaction (org.aion.types.InternalTransaction)32 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)29 BlockContext (org.aion.zero.impl.types.BlockContext)29