Search in sources :

Example 6 with RepositorySnapshot

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);
}
Also used : RepositoryLocator(co.rsk.db.RepositoryLocator) Coin(co.rsk.core.Coin) RepositorySnapshot(co.rsk.db.RepositorySnapshot) BlockStore(org.ethereum.db.BlockStore) BlockExecutor(co.rsk.core.bc.BlockExecutor) RskAddress(co.rsk.core.RskAddress) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 7 with RepositorySnapshot

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());
}
Also used : RepositoryLocator(co.rsk.db.RepositoryLocator) RepositorySnapshot(co.rsk.db.RepositorySnapshot) BlockStore(org.ethereum.db.BlockStore) BlockExecutor(co.rsk.core.bc.BlockExecutor) RskAddress(co.rsk.core.RskAddress) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 8 with RepositorySnapshot

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;
}
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 9 with RepositorySnapshot

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);
}
Also used : WorldDslProcessor(co.rsk.test.dsl.WorldDslProcessor) Account(org.ethereum.core.Account) RepositorySnapshot(co.rsk.db.RepositorySnapshot) DslParser(co.rsk.test.dsl.DslParser) BigInteger(java.math.BigInteger) World(co.rsk.test.World) Test(org.junit.Test)

Example 10 with RepositorySnapshot

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

RepositorySnapshot (co.rsk.db.RepositorySnapshot)37 Test (org.junit.Test)25 RepositoryLocator (co.rsk.db.RepositoryLocator)18 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)13 RskAddress (co.rsk.core.RskAddress)12 BlockStore (org.ethereum.db.BlockStore)11 BigInteger (java.math.BigInteger)10 Coin (co.rsk.core.Coin)9 BlockExecutor (co.rsk.core.bc.BlockExecutor)8 World (co.rsk.test.World)7 TestSystemProperties (co.rsk.config.TestSystemProperties)6 RskSystemProperties (co.rsk.config.RskSystemProperties)5 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)5 Account (org.ethereum.core.Account)5 AccountInformationProvider (co.rsk.core.bc.AccountInformationProvider)3 TransactionGateway (co.rsk.net.TransactionGateway)3 DslParser (co.rsk.test.dsl.DslParser)3 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)3 TrieStore (co.rsk.trie.TrieStore)3 Block (org.ethereum.core.Block)3