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;
}
}
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;
}
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());
}
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());
}
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)));
}
}
}
Aggregations