use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.
the class RemascProcessMinerFeesTest method processMinersFeesFromTxThatIsNotTheLatestTx.
@Test
public void processMinersFeesFromTxThatIsNotTheLatestTx() {
Blockchain blockchain = blockchainBuilder.build();
BlockStore blockStore = blockchainBuilder.getBlockStore();
RepositoryLocator repositoryLocator = blockchainBuilder.getRepositoryLocator();
List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
Block blockWithOneTx = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, Collections.emptyList(), minerFee, 0, txValue, cowKey);
blocks.add(blockWithOneTx);
blocks.addAll(createSimpleBlocks(blockWithOneTx, 9));
BlockExecutor blockExecutor = buildBlockExecutor(repositoryLocator, blockStore);
executeBlocks(blockchain, blocks, blockExecutor);
RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
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(repository);
assertEquals(Coin.ZERO, remasceStorageProvider.getRewardBalance());
assertEquals(Coin.ZERO, remasceStorageProvider.getBurnedBalance());
// A hacker trying to screw the system creates a tx to remasc and a fool/accomplice miner includes that tx in a block
Transaction tx = Transaction.builder().nonce(Coin.valueOf(1)).gasPrice(Coin.valueOf(1)).gasLimit(Coin.valueOf(minerFee)).destination(PrecompiledContracts.REMASC_ADDR).chainId(config.getNetworkConstants().getChainId()).value(Coin.valueOf(txValue * 2)).build();
tx.sign(cowKey.getPrivKeyBytes());
Block newblock = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), Collections.emptyList(), null, tx);
blockExecutor.executeAndFillAll(newblock, blockchain.getBestBlock().getHeader());
blockchain.tryToConnect(newblock);
repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
// 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(minerFee + txValue + minerFee)), repository.getAccountState(new RskAddress(cowAddress)).getBalance());
long blockReward = minerFee / remascConfig.getSyntheticSpan();
long originalBlockReward = blockReward;
assertEquals(Coin.valueOf(minerFee + minerFee - blockReward), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
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(getRemascStorageProvider(repository), expectedRewardBalance, Coin.ZERO, 0L);
this.validateFederatorsBalanceIsCorrect(repository, federationReward, newblock);
}
use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.
the class RemascProcessMinerFeesTest method processMinersFeesWithoutMinimumSyntheticSpan.
@Test
public void processMinersFeesWithoutMinimumSyntheticSpan() {
Blockchain blockchain = blockchainBuilder.build();
BlockStore blockStore = blockchainBuilder.getBlockStore();
RepositoryLocator repositoryLocator = blockchainBuilder.getRepositoryLocator();
List<Block> blocks = createSimpleBlocks(genesisBlock, 2);
Block blockWithOneTx = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseA, Collections.emptyList(), minerFee, 0, txValue, cowKey);
blocks.add(blockWithOneTx);
blocks.addAll(createSimpleBlocks(blockWithOneTx, 9));
BlockExecutor blockExecutor = buildBlockExecutor(repositoryLocator, blockStore);
executeBlocks(blockchain, blocks, blockExecutor);
RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
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()));
Block newblock = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), Collections.emptyList(), null);
blockExecutor.executeAndFillAll(newblock, blockchain.getBestBlock().getHeader());
blockchain.tryToConnect(newblock);
repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
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 remascStorageProvider = getRemascStorageProvider(repository);
assertEquals(Coin.valueOf(minerFee), remascStorageProvider.getRewardBalance());
assertEquals(Coin.ZERO, remascStorageProvider.getBurnedBalance());
}
use of co.rsk.db.RepositorySnapshot 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 co.rsk.db.RepositorySnapshot in project rskj by rsksmart.
the class WorldDslProcessorTest method processAccountNewCommandWithBalanceAndCode.
@Test
public void processAccountNewCommandWithBalanceAndCode() throws DslProcessorException {
World world = new World();
WorldDslProcessor processor = new WorldDslProcessor(world);
DslParser parser = new DslParser("account_new acc1 1000000 01020304");
processor.processCommands(parser);
Account account = world.getAccountByName("acc1");
Assert.assertNotNull(account);
RepositorySnapshot repository = world.getRepositoryLocator().snapshotAt(world.getBlockChain().getBestBlock().getHeader());
Assert.assertEquals(new BigInteger("1000000"), repository.getBalance(account.getAddress()).asBigInteger());
byte[] code = repository.getCode(account.getAddress());
Assert.assertNotNull(code);
Assert.assertArrayEquals(new byte[] { 0x01, 0x02, 0x03, 0x04 }, code);
}
use of co.rsk.db.RepositorySnapshot 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