Search in sources :

Example 1 with ECKey

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

the class AccountManager method lockAccount.

public boolean lockAccount(Address _address, String _password) {
    ECKey key = Keystore.getKey(_address.toString(), _password);
    if (Optional.ofNullable(key).isPresent()) {
        Account acc = inst.accounts.get(_address);
        if (Optional.ofNullable(acc).isPresent()) {
            acc.updateTimeout(Instant.now().getEpochSecond() - 1);
        }
        LOGGER.debug("<lock-success addr={}>", _address);
        return true;
    } else {
        LOGGER.debug("<lock-fail addr={}>", _address);
        return false;
    }
}
Also used : ECKey(org.aion.crypto.ECKey)

Example 2 with ECKey

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

the class Keystore method importAccount.

public static Set<String> importAccount(Map<String, String> importKey) {
    if (importKey == null) {
        throw new NullPointerException();
    }
    Set<String> rtn = new HashSet<>();
    int count = 0;
    for (Map.Entry<String, String> keySet : importKey.entrySet()) {
        if (count < 100) {
            byte[] raw = Hex.decode(keySet.getKey().startsWith("0x") ? keySet.getKey().substring(2) : keySet.getKey());
            ECKey key = KeystoreFormat.fromKeystore(raw, keySet.getValue());
            String address = Keystore.create(keySet.getValue(), key);
            if (!address.equals("0x")) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("The private key was imported, the address is: {}", address);
                }
            } else {
                if (LOG.isErrorEnabled()) {
                    LOG.error("Failed to import the private key. Already exists?");
                }
                // only return the failed import privateKey.
                rtn.add(keySet.getKey());
            }
        } else {
            rtn.add(keySet.getKey());
        }
        count++;
    }
    return rtn;
}
Also used : ECKey(org.aion.crypto.ECKey) HashMap(java.util.HashMap)

Example 3 with ECKey

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

the class TxnPoolTest method testSnapshotAll.

@Test
public void testSnapshotAll() {
    ECKeyFac.setType(ECKeyFac.ECKeyType.ED25519);
    ECKey key = ECKeyFac.inst().create();
    List<AionTransaction> txs = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        AionTransaction tx = new AionTransaction(BigInteger.valueOf(i).toByteArray(), Address.wrap(key.getAddress()), Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
        tx.sign(key);
        txs.add(tx);
    }
    Properties config = new Properties();
    ITxPool<AionTransaction> tp = new TxPoolA0<>(config);
    tp.add(txs.subList(0, 500));
    assertEquals(500, tp.snapshot().size());
    assertEquals(500, tp.snapshotAll().size());
    tp.remove(txs.subList(0, 100));
    assertEquals(400, tp.snapshot().size());
    assertEquals(400, tp.snapshotAll().size());
}
Also used : ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) TxPoolA0(org.aion.txpool.zero.TxPoolA0) Test(org.junit.Test)

Example 4 with ECKey

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

the class TxnPoolTest method testRemove2.

@Test
public void testRemove2() {
    ECKeyFac.setType(ECKeyFac.ECKeyType.ED25519);
    ECKey key = ECKeyFac.inst().create();
    List<AionTransaction> txs = new ArrayList<>();
    for (int i = 0; i < 95; i++) {
        AionTransaction tx = new AionTransaction(BigInteger.valueOf(i).toByteArray(), Address.wrap(key.getAddress()), Address.wrap("0000000000000000000000000000000000000000000000000000000000000001"), ByteUtils.fromHexString("1"), ByteUtils.fromHexString("1"), 10000L, 1L);
        tx.sign(key);
        tx.setNrgConsume(100L);
        txs.add(tx);
    }
    Properties config = new Properties();
    ITxPool<AionTransaction> tp = new TxPoolA0<>(config);
    tp.add(txs.subList(0, 26));
    assertEquals(26, tp.snapshot().size());
    assertEquals(26, tp.snapshotAll().size());
    tp.remove(txs.subList(0, 13));
    assertEquals(13, tp.snapshot().size());
    assertEquals(13, tp.snapshotAll().size());
    tp.add(txs.subList(26, 70));
    assertEquals(57, tp.snapshot().size());
    assertEquals(57, tp.snapshotAll().size());
    tp.remove(txs.subList(13, 40));
    assertEquals(30, tp.snapshot().size());
    assertEquals(30, tp.snapshotAll().size());
    // assume we don't remove tx 40
    tp.remove(txs.subList(41, 70));
    assertEquals(1, tp.snapshot().size());
    assertEquals(1, tp.snapshotAll().size());
}
Also used : ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction) TxPoolA0(org.aion.txpool.zero.TxPoolA0) Test(org.junit.Test)

Example 5 with ECKey

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

the class TxnPoolTest method benchmarkSnapshot2.

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

       1K new transactions insert to the pool later around 70ms to snap (including sort)
     */
void benchmarkSnapshot2() {
    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 aKey2 : key) {
        Address acc = Address.wrap(aKey2.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(aKey2);
            txn.setNrgConsume(100L);
            txnl.add(txn);
        }
    }
    tp.add(txnl);
    assertTrue(tp.size() == cnt * key.size());
    // sort the inserted txs
    long start = System.currentTimeMillis();
    tp.snapshot();
    System.out.println("time spent: " + (System.currentTimeMillis() - start) + " ms.");
    int cnt2 = 100;
    txnl.clear();
    for (ECKey aKey1 : key) {
        for (int i = 0; i < cnt2; i++) {
            ITransaction txn = new AionTransaction(BigInteger.valueOf(cnt + i).toByteArray(), Address.wrap(aKey1.getAddress()), 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 + cnt2) * key.size());
    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 + cnt2; 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)

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