Search in sources :

Example 6 with ECKey

use of org.aion.crypto.ECKey in project aion by aionnetwork.

the class TxnPoolTest method noncebyAccountTest2.

@Test
public void noncebyAccountTest2() {
    Properties config = new Properties();
    config.put("txn-timeout", "10");
    TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
    List<ITransaction> txnl = new ArrayList<>();
    int cnt = 100;
    for (ECKey aKey1 : key) {
        Address acc = Address.wrap(aKey1.getAddress());
        for (int i = 0; i < cnt; i++) {
            byte[] nonce = new byte[Long.BYTES];
            nonce[Long.BYTES - 1] = (byte) (i + 1);
            ITransaction txn = new AionTransaction(nonce, acc, Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
            ((AionTransaction) txn).sign(aKey1);
            txn.setNrgConsume(100L);
            txnl.add(txn);
        }
    }
    tp.add(txnl);
    assertTrue(tp.size() == cnt * key.size());
    // sort the inserted txs
    tp.snapshot();
    for (ECKey aKey : key) {
        List<BigInteger> nl = tp.getNonceList(Address.wrap(aKey.getAddress()));
        for (int i = 0; i < cnt; i++) {
            assertTrue(nl.get(i).equals(BigInteger.valueOf(i + 1)));
        }
    }
}
Also used : Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) TxPoolA0(org.aion.txpool.zero.TxPoolA0) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 7 with ECKey

use of org.aion.crypto.ECKey in project aion by aionnetwork.

the class TxnPoolTest method benchmarkSnapshot5.

@Test
public /* 100K new transactions in pool around 350ms (cold-call)

       the second time snapshot is around 35ms
     */
void benchmarkSnapshot5() {
    Properties config = new Properties();
    config.put("txn-timeout", "100");
    TxPoolA0<ITransaction> tp = new TxPoolA0<>(config);
    List<ITransaction> txnl = new ArrayList<>();
    int cnt = 10000;
    for (ECKey aKey1 : key) {
        Address acc = Address.wrap(aKey1.getAddress());
        for (int i = 0; i < cnt; i++) {
            ITransaction txn = new AionTransaction(BigInteger.valueOf(i).toByteArray(), acc, Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
            ((AionTransaction) txn).sign(aKey1);
            txn.setNrgConsume(100L);
            txnl.add(txn);
        }
    }
    tp.add(txnl);
    assertTrue(tp.size() == cnt * key.size());
    // sort the inserted txs
    System.out.println("1st time snapshot...");
    long start = System.currentTimeMillis();
    tp.snapshot();
    System.out.println("1st time spent: " + (System.currentTimeMillis() - start) + " ms.");
    System.out.println("2nd time snapshot...");
    start = System.currentTimeMillis();
    tp.snapshot();
    System.out.println("2nd time spent: " + (System.currentTimeMillis() - start) + " ms.");
    for (ECKey aKey : key) {
        List<BigInteger> nl = tp.getNonceList(Address.wrap(aKey.getAddress()));
        for (int i = 0; i < cnt; i++) {
            assertTrue(nl.get(i).equals(BigInteger.valueOf(i)));
        }
    }
}
Also used : Address(org.aion.base.type.Address) ITransaction(org.aion.base.type.ITransaction) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) TxPoolA0(org.aion.txpool.zero.TxPoolA0) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 8 with ECKey

use of org.aion.crypto.ECKey in project aion by aionnetwork.

the class CliTest method testImportPrivateKey2.

@Test
public void testImportPrivateKey2() {
    ECKey key = ECKeyFac.inst().create();
    System.out.println("Original address    : " + Hex.toHexString(key.getAddress()));
    System.out.println("Original public key : " + Hex.toHexString(key.getPubKey()));
    System.out.println("Original private key: " + Hex.toHexString(key.getPrivKeyBytes()));
    String[] args = { "-a", "import", Hex.toHexString(key.getPrivKeyBytes()) };
    assertEquals(0, cli.call(args, CfgAion.inst()));
    ECKey key2 = Keystore.getKey(Hex.toHexString(key.getAddress()), "password");
    System.out.println("Imported address    : " + Hex.toHexString(key2.getAddress()));
    System.out.println("Imported public key : " + Hex.toHexString(key2.getPubKey()));
    System.out.println("Imported private key: " + Hex.toHexString(key2.getPrivKeyBytes()));
}
Also used : ECKey(org.aion.crypto.ECKey) Test(org.junit.Test)

Example 9 with ECKey

use of org.aion.crypto.ECKey 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 10 with ECKey

use of org.aion.crypto.ECKey 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)

Aggregations

ECKey (org.aion.crypto.ECKey)29 Test (org.junit.Test)17 Address (org.aion.base.type.Address)13 AionTransaction (org.aion.zero.types.AionTransaction)12 BigInteger (java.math.BigInteger)10 TxPoolA0 (org.aion.txpool.zero.TxPoolA0)7 AionBlock (org.aion.zero.impl.types.AionBlock)6 ITransaction (org.aion.base.type.ITransaction)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 StandaloneBlockchain (org.aion.zero.impl.StandaloneBlockchain)4 BlockPropagationHandler (org.aion.zero.impl.sync.handler.BlockPropagationHandler)4 File (java.io.File)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ImportResult (org.aion.mcf.core.ImportResult)2 FileOutputStream (java.io.FileOutputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 FileAttribute (java.nio.file.attribute.FileAttribute)1