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