Search in sources :

Example 1 with Account

use of org.ethereum.core.Account in project rskj by rsksmart.

the class TransactionPoolImplTest method rejectTransactionPoolTransaction.

@Test
public void rejectTransactionPoolTransaction() {
    BlockChainImpl blockchain = createBlockchain();
    Coin balance = Coin.valueOf(1000000);
    TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(2, balance, blockchain);
    transactionPool.processBest(blockchain.getBestBlock());
    Transaction tx = createSampleTransaction(1, 2, 1000, 0);
    tx.setGasLimit(BigInteger.valueOf(3000001).toByteArray());
    Account receiver = createAccount(2);
    transactionPool.addTransaction(tx);
    Repository repository = transactionPool.getRepository();
    Assert.assertEquals(BigInteger.valueOf(1000000), repository.getBalance(receiver.getAddress()).asBigInteger());
}
Also used : Coin(co.rsk.core.Coin) Account(org.ethereum.core.Account) Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) Test(org.junit.Test)

Example 2 with Account

use of org.ethereum.core.Account in project rskj by rsksmart.

the class TransactionPoolImplTest method usingAccountsWithInitialBalance.

@Test
public void usingAccountsWithInitialBalance() {
    TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(2, Coin.valueOf(10L), createBlockchain());
    Repository repository = transactionPool.getRepository();
    Assert.assertNotNull(repository);
    Account account1 = createAccount(1);
    Account account2 = createAccount(2);
    Assert.assertEquals(BigInteger.TEN, repository.getBalance(account1.getAddress()).asBigInteger());
    Assert.assertEquals(BigInteger.TEN, repository.getBalance(account2.getAddress()).asBigInteger());
}
Also used : Account(org.ethereum.core.Account) Repository(org.ethereum.core.Repository) Test(org.junit.Test)

Example 3 with Account

use of org.ethereum.core.Account in project rskj by rsksmart.

the class EthModuleWalletEnabled method sendTransaction.

@Override
public synchronized String sendTransaction(Web3.CallArguments args) {
    Account account = this.getAccount(args.from);
    String s = null;
    try {
        if (account == null) {
            throw new JsonRpcInvalidParamException("From address private key could not be found in this node");
        }
        String toAddress = args.to != null ? Hex.toHexString(stringHexToByteArray(args.to)) : null;
        BigInteger value = args.value != null ? TypeConverter.stringNumberAsBigInt(args.value) : BigInteger.ZERO;
        BigInteger gasPrice = args.gasPrice != null ? TypeConverter.stringNumberAsBigInt(args.gasPrice) : BigInteger.ZERO;
        BigInteger gasLimit = args.gas != null ? TypeConverter.stringNumberAsBigInt(args.gas) : BigInteger.valueOf(GasCost.TRANSACTION_DEFAULT);
        if (args.data != null && args.data.startsWith("0x")) {
            args.data = args.data.substring(2);
        }
        synchronized (transactionPool) {
            BigInteger accountNonce = args.nonce != null ? TypeConverter.stringNumberAsBigInt(args.nonce) : transactionPool.getRepository().getNonce(account.getAddress());
            Transaction tx = Transaction.create(config, toAddress, value, accountNonce, gasPrice, gasLimit, args.data);
            tx.sign(account.getEcKey().getPrivKeyBytes());
            eth.submitTransaction(tx.toImmutableTransaction());
            s = tx.getHash().toJsonString();
        }
        return s;
    } finally {
        LOGGER.debug("eth_sendTransaction({}): {}", args, s);
    }
}
Also used : Account(org.ethereum.core.Account) Transaction(org.ethereum.core.Transaction) BigInteger(java.math.BigInteger) JsonRpcInvalidParamException(org.ethereum.rpc.exception.JsonRpcInvalidParamException)

Example 4 with Account

use of org.ethereum.core.Account in project rskj by rsksmart.

the class AccountBuilder method build.

public Account build() {
    byte[] privateKeyBytes = HashUtil.keccak256(name.getBytes());
    ECKey key = ECKey.fromPrivate(privateKeyBytes);
    Account account = new Account(key);
    if (blockChain != null) {
        Block best = blockChain.getStatus().getBestBlock();
        BlockDifficulty td = blockChain.getStatus().getTotalDifficulty();
        RepositorySnapshot repository = repositoryLocator.snapshotAt(blockChain.getBestBlock().getHeader());
        Repository track = repository.startTracking();
        track.createAccount(account.getAddress());
        if (this.balance != null)
            track.addBalance(account.getAddress(), this.balance);
        if (this.code != null) {
            track.saveCode(account.getAddress(), this.code);
            track.getCode(account.getAddress());
        }
        track.commit();
        track.save();
        // Check that code is there...
        repository.getCode(account.getAddress());
        best.setStateRoot(repository.getRoot());
        best.flushRLP();
        blockStore.saveBlock(best, td, true);
    }
    return account;
}
Also used : Account(org.ethereum.core.Account) BlockDifficulty(co.rsk.core.BlockDifficulty) RepositorySnapshot(co.rsk.db.RepositorySnapshot) Repository(org.ethereum.core.Repository) Block(org.ethereum.core.Block) ECKey(org.ethereum.crypto.ECKey)

Example 5 with Account

use of org.ethereum.core.Account in project rskj by rsksmart.

the class AccountBuilderTest method createAccountWithBalanceAndCode.

@Test
public void createAccountWithBalanceAndCode() {
    World world = new World();
    byte[] code = new byte[] { 0x01, 0x02, 0x03 };
    Coin balance = Coin.valueOf(10);
    Account account = new AccountBuilder(world).name("acc1").balance(balance).code(code).build();
    Assert.assertNotNull(account);
    Assert.assertTrue(account.getEcKey().hasPrivKey());
    RepositorySnapshot repository = world.getRepositoryLocator().snapshotAt(world.getBlockChain().getBestBlock().getHeader());
    Assert.assertEquals(balance, repository.getBalance(account.getAddress()));
    Assert.assertArrayEquals(code, repository.getCode(account.getAddress()));
}
Also used : Coin(co.rsk.core.Coin) Account(org.ethereum.core.Account) RepositorySnapshot(co.rsk.db.RepositorySnapshot) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) Test(org.junit.Test)

Aggregations

Account (org.ethereum.core.Account)62 Test (org.junit.Test)34 Transaction (org.ethereum.core.Transaction)26 AccountBuilder (co.rsk.test.builders.AccountBuilder)22 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)16 World (co.rsk.test.World)15 DslParser (co.rsk.test.dsl.DslParser)11 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)11 BigInteger (java.math.BigInteger)10 RskAddress (co.rsk.core.RskAddress)5 RepositorySnapshot (co.rsk.db.RepositorySnapshot)5 Block (org.ethereum.core.Block)5 Repository (org.ethereum.core.Repository)5 ReceiptStore (org.ethereum.db.ReceiptStore)5 ECKey (org.ethereum.crypto.ECKey)4 Coin (co.rsk.core.Coin)3 Keccak256 (co.rsk.crypto.Keccak256)3 Trie (co.rsk.trie.Trie)3 ArrayList (java.util.ArrayList)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2