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