Search in sources :

Example 26 with ECKey

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

the class CliTest method testImportPrivateKey.

@Test
public void testImportPrivateKey() {
    ECKey key = ECKeyFac.inst().create();
    String[] args = { "-a", "import", Hex.toHexString(key.getPrivKeyBytes()) };
    assertEquals(0, cli.call(args, CfgAion.inst()));
}
Also used : ECKey(org.aion.crypto.ECKey) Test(org.junit.Test)

Example 27 with ECKey

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

the class BlockchainIntegrationTest method testSimpleBlockchainLoad.

// check that all accounts are loaded correctly
@Test
public void testSimpleBlockchainLoad() {
    StandaloneBlockchain.Bundle b = (new StandaloneBlockchain.Builder()).withDefaultAccounts().build();
    for (ECKey k : b.privateKeys) {
        assertThat(b.bc.getRepository().getBalance(Address.wrap(k.getAddress()))).isNotEqualTo(BigInteger.ZERO);
    }
    assertThat(b.privateKeys.size()).isEqualTo(10);
}
Also used : ECKey(org.aion.crypto.ECKey) Test(org.junit.Test)

Example 28 with ECKey

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

the class BlockPropagationTest method testIgnoreSameBlock.

@Test
public void testIgnoreSameBlock() {
    List<ECKey> accounts = generateDefaultAccounts();
    StandaloneBlockchain.Bundle bundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    AionBlock block = bundle.bc.createNewBlock(bundle.bc.getGenesis(), Collections.EMPTY_LIST, true);
    assertThat(block.getNumber()).isEqualTo(1);
    byte[] sender = HashUtil.h256("node1".getBytes());
    byte[] receiver = HashUtil.h256("receiver".getBytes());
    NodeMock senderMock = new NodeMock(sender, 1);
    NodeMock receiverMock = new NodeMock(receiver, 0);
    Map<Integer, INode> node = new HashMap<>();
    node.put(1, senderMock);
    node.put(2, receiverMock);
    AtomicInteger times = new AtomicInteger();
    P2pMock p2pMock = new P2pMock(node) {

        @Override
        public void send(int _nodeId, Msg _msg) {
            if (_nodeId != receiverMock.getIdHash())
                throw new RuntimeException("should only send to receiver");
            times.getAndIncrement();
        }
    };
    StandaloneBlockchain.Bundle anotherBundle = new StandaloneBlockchain.Builder().withValidatorConfiguration("simple").withDefaultAccounts(accounts).build();
    assertThat(bundle.bc.genesis.getHash()).isEqualTo(anotherBundle.bc.genesis.getHash());
    BlockPropagationHandler handler = new BlockPropagationHandler(1024, // NOTE: not the same blockchain that generated the block
    anotherBundle.bc, p2pMock, anotherBundle.bc.getBlockHeaderValidator());
    // block is processed
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.PROP_CONNECTED);
    assertThat(handler.processIncomingBlock(senderMock.getIdHash(), block)).isEqualTo(BlockPropagationHandler.PropStatus.DROPPED);
    assertThat(times.get()).isEqualTo(1);
}
Also used : BlockPropagationHandler(org.aion.zero.impl.sync.handler.BlockPropagationHandler) ECKey(org.aion.crypto.ECKey) StandaloneBlockchain(org.aion.zero.impl.StandaloneBlockchain) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AionBlock(org.aion.zero.impl.types.AionBlock) Test(org.junit.Test)

Example 29 with ECKey

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

the class ApiAion method sendTransaction.

public byte[] sendTransaction(ArgTxCall _params) {
    Address from = _params.getFrom();
    if (from == null || from.equals(Address.EMPTY_ADDRESS())) {
        LOG.error("<send-transaction msg=invalid-from-address>");
        return null;
    }
    ECKey key = this.getAccountKey(from.toString());
    if (key == null) {
        LOG.error("<send-transaction msg=account-not-found>");
        return null;
    }
    try {
        synchronized (pendingState) {
            // TODO : temp set nrg & price to 1
            byte[] nonce = (!_params.getNonce().equals(BigInteger.ZERO)) ? _params.getNonce().toByteArray() : pendingState.bestNonce(Address.wrap(key.getAddress())).toByteArray();
            AionTransaction tx = new AionTransaction(nonce, _params.getTo(), _params.getValue().toByteArray(), _params.getData(), _params.getNrg(), _params.getNrgPrice());
            tx.sign(key);
            pendingState.addPendingTransaction(tx);
            return tx.getHash();
        }
    } catch (Exception ex) {
        return null;
    }
}
Also used : Address(org.aion.base.type.Address) ECKey(org.aion.crypto.ECKey) AionTransaction(org.aion.zero.types.AionTransaction)

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