use of co.rsk.core.Coin in project rskj by rsksmart.
the class TransactionPoolImplTest method updateTransactionPool.
@Test
public void updateTransactionPool() {
BlockChainImpl blockchain = createBlockchain();
Coin balance = Coin.valueOf(1000000);
TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(2, balance, blockchain);
transactionPool.processBest(blockchain.getBestBlock());
Transaction tx1 = createSampleTransaction(1, 2, 1000, 0);
Transaction tx2 = createSampleTransaction(1, 2, 3000, 1);
Account receiver = createAccount(2);
transactionPool.addTransaction(tx1);
transactionPool.addTransaction(tx2);
transactionPool.updateState();
Repository repository = transactionPool.getRepository();
Assert.assertEquals(BigInteger.valueOf(1004000), repository.getBalance(receiver.getAddress()).asBigInteger());
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class TransactionPoolImplTest method retractBlockAddsTransactionsAsPending.
@Test
public void retractBlockAddsTransactionsAsPending() {
BlockChainImpl blockchain = createBlockchain();
Coin balance = Coin.valueOf(1000000);
TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(3, balance, blockchain);
transactionPool.processBest(blockchain.getBestBlock());
Transaction tx1 = createSampleTransaction(1, 2, 1000, 0);
Transaction tx2 = createSampleTransaction(1, 2, 3000, 1);
Transaction tx3 = createSampleTransaction(2, 3, 1000, 0);
Transaction tx4 = createSampleTransaction(2, 3, 3000, 1);
transactionPool.addTransaction(tx1);
transactionPool.addTransaction(tx2);
List<Transaction> txs = new ArrayList<>();
txs.add(tx3);
txs.add(tx4);
Block block = new BlockBuilder().parent(new BlockGenerator().getGenesisBlock()).transactions(txs).build();
transactionPool.retractBlock(block);
List<Transaction> alltxs = transactionPool.getPendingTransactions();
Assert.assertNotNull(alltxs);
Assert.assertFalse(alltxs.isEmpty());
Assert.assertEquals(4, alltxs.size());
Assert.assertTrue(alltxs.contains(tx1));
Assert.assertTrue(alltxs.contains(tx2));
Assert.assertTrue(alltxs.contains(tx3));
Assert.assertTrue(alltxs.contains(tx4));
List<Transaction> ptxs = transactionPool.getPendingTransactions();
Assert.assertNotNull(ptxs);
Assert.assertFalse(ptxs.isEmpty());
Assert.assertEquals(4, ptxs.size());
Assert.assertTrue(ptxs.contains(tx1));
Assert.assertTrue(ptxs.contains(tx2));
Assert.assertTrue(ptxs.contains(tx3));
Assert.assertTrue(ptxs.contains(tx4));
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class TransactionPoolImplTest method addAndExecutePendingTransaction.
@Test
public void addAndExecutePendingTransaction() {
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);
Account receiver = createAccount(2);
transactionPool.addTransaction(tx);
Repository repository = transactionPool.getRepository();
Assert.assertEquals(BigInteger.valueOf(1001000), repository.getBalance(receiver.getAddress()).asBigInteger());
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class RemascProcessMinerFeesTest method validateFederatorsBalanceIsCorrect.
private void validateFederatorsBalanceIsCorrect(Repository repository, long federationReward) throws IOException, BlockStoreException {
BridgeSupport bridgeSupport = new BridgeSupport(config, repository, null, PrecompiledContracts.BRIDGE_ADDR, null);
RemascFederationProvider provider = new RemascFederationProvider(bridgeSupport);
int nfederators = provider.getFederationSize();
Coin federatorBalance = Coin.valueOf(federationReward / nfederators);
for (int k = 0; k < nfederators; k++) {
assertEquals(federatorBalance, repository.getBalance(provider.getFederatorAddress(k)));
}
}
use of co.rsk.core.Coin in project rskj by rsksmart.
the class RemascProcessMinerFeesTest method processMinersFeesFromTxInvokedByAnotherContract.
@Test
public void processMinersFeesFromTxInvokedByAnotherContract() throws IOException, BlockStoreException {
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
Block blockWithOneTx = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, null, minerFee, 0, txValue, cowKey);
blocks.add(blockWithOneTx);
blocks.addAll(createSimpleBlocks(blockWithOneTx, 9));
BlockExecutor blockExecutor = new BlockExecutor(config, blockchain.getRepository(), null, blockchain.getBlockStore(), null);
for (Block b : blocks) {
blockExecutor.executeAndFillAll(b, blockchain.getBestBlock());
b.seal();
blockchain.tryToConnect(b);
}
Repository repository = blockchain.getRepository();
assertEquals(cowInitialBalance.subtract(Coin.valueOf(minerFee + txValue)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
assertEquals(Coin.valueOf(minerFee), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
assertNull(repository.getAccountState(coinbaseA));
assertNull(repository.getAccountState(remascConfig.getRskLabsAddress()));
RemascStorageProvider remasceStorageProvider = getRemascStorageProvider(blockchain);
assertEquals(Coin.ZERO, remasceStorageProvider.getRewardBalance());
assertEquals(Coin.ZERO, remasceStorageProvider.getBurnedBalance());
assertEquals(0, remasceStorageProvider.getSiblings().size());
// A hacker trying to screw the system creates a contracts that calls remasc and a fool/accomplice miner includes that tx in a block
// Contract code
// pragma solidity ^0.4.3;
// contract RemascHacker {
//
// function()
// {
// address remasc = 0x0000000000000000000000000000000001000008;
// remasc.call();
// }
// }
long txCreateContractGasLimit = 53755 + 32000;
Transaction txCreateContract = new Transaction(Coin.valueOf(1).getBytes(), Coin.valueOf(1).getBytes(), Coin.valueOf(txCreateContractGasLimit).getBytes(), null, Coin.ZERO.getBytes(), Hex.decode("6060604052346000575b6077806100176000396000f30060606040525b3460005760495b6000600890508073ffffffffffffffffffffffffffffffffffffffff166040518090506000604051808303816000866161da5a03f1915050505b50565b0000a165627a7a7230582036692fbb1395da1688af0189be5b0ac18df3d93a2402f4fc8f927b31c1baa2460029"), config.getBlockchainConfig().getCommonConstants().getChainId());
txCreateContract.sign(cowKey.getPrivKeyBytes());
long txCallRemascGasLimit = 21828;
Transaction txCallRemasc = new Transaction(Coin.valueOf(2).getBytes(), Coin.valueOf(1).getBytes(), Coin.valueOf(txCallRemascGasLimit).getBytes(), Hex.decode("da7ce79725418f4f6e13bf5f520c89cec5f6a974"), Coin.ZERO.getBytes(), null, config.getBlockchainConfig().getCommonConstants().getChainId());
txCallRemasc.sign(cowKey.getPrivKeyBytes());
Block newblock = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), null, null, txCreateContract, txCallRemasc);
blockExecutor.executeAndFillAll(newblock, blockchain.getBestBlock());
newblock.seal();
blockchain.tryToConnect(newblock);
repository = blockchain.getRepository();
// Check "hack" tx makes no changes to the remasc state, sender pays fees, and value is added to remasc account balance
assertEquals(cowInitialBalance.subtract(Coin.valueOf(txCreateContractGasLimit + txCallRemascGasLimit + txValue + minerFee)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
long blockReward = minerFee / remascConfig.getSyntheticSpan();
long originalBlockReward = blockReward;
long rskReward = blockReward / remascConfig.getRskLabsDivisor();
assertEquals(Coin.valueOf(rskReward), repository.getAccountState(remascConfig.getRskLabsAddress()).getBalance());
blockReward -= rskReward;
long federationReward = blockReward / remascConfig.getFederationDivisor();
assertEquals(33, federationReward);
blockReward -= federationReward;
assertEquals(Coin.valueOf(blockReward), repository.getAccountState(coinbaseA).getBalance());
Coin expectedRewardBalance = Coin.valueOf(minerFee - originalBlockReward);
this.validateRemascsStorageIsCorrect(this.getRemascStorageProvider(blockchain), expectedRewardBalance, Coin.ZERO, 0L);
this.validateFederatorsBalanceIsCorrect(blockchain.getRepository(), federationReward);
}
Aggregations