Search in sources :

Example 21 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class BlockTxsValidationRule method isValid.

@Override
public boolean isValid(Block block, Block parent) {
    if (block == null || parent == null) {
        logger.warn("BlockTxsValidationRule - block or parent are null");
        return false;
    }
    List<Transaction> txs = block.getTransactionsList();
    if (txs.isEmpty()) {
        return true;
    }
    RepositorySnapshot parentRepo = repositoryLocator.snapshotAt(parent.getHeader());
    Map<RskAddress, BigInteger> curNonce = new HashMap<>();
    for (Transaction tx : txs) {
        try {
            tx.verify();
        } catch (RuntimeException e) {
            logger.warn("Unable to verify transaction", e);
            return false;
        }
        RskAddress sender = tx.getSender();
        BigInteger expectedNonce = curNonce.get(sender);
        if (expectedNonce == null) {
            expectedNonce = parentRepo.getNonce(sender);
        }
        curNonce.put(sender, expectedNonce.add(ONE));
        BigInteger txNonce = new BigInteger(1, tx.getNonce());
        if (!expectedNonce.equals(txNonce)) {
            logger.warn("Invalid transaction: Tx nonce {} != expected nonce {} (parent nonce: {}): {}", txNonce, expectedNonce, parentRepo.getNonce(sender), tx);
            panicProcessor.panic("invalidtransaction", String.format("Invalid transaction: Tx nonce %s != expected nonce %s (parent nonce: %s): %s", txNonce, expectedNonce, parentRepo.getNonce(sender), tx.getHash()));
            return false;
        }
    }
    return true;
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) Transaction(org.ethereum.core.Transaction) HashMap(java.util.HashMap) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 22 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class GarbageCollectorTest method withSnapshotStateRootAtBlockNumber.

private void withSnapshotStateRootAtBlockNumber(int i, byte[] stateRoot) {
    Block block = block(i);
    when(blockStore.getChainBlockByNumber(i)).thenReturn(block);
    RepositorySnapshot snapshot = mock(RepositorySnapshot.class);
    when(repositoryLocator.snapshotAt(block.getHeader())).thenReturn(snapshot);
    when(snapshot.getRoot()).thenReturn(stateRoot);
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) Block(org.ethereum.core.Block)

Example 23 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class Web3InformationRetrieverTest method getState_latest.

@Test
public void getState_latest() {
    Block block = mock(Block.class);
    BlockHeader header = mock(BlockHeader.class);
    when(block.getHeader()).thenReturn(header);
    when(blockchain.getBestBlock()).thenReturn(block);
    RepositorySnapshot snapshot = mock(RepositorySnapshot.class);
    when(locator.findSnapshotAt(eq(header))).thenReturn(Optional.of(snapshot));
    AccountInformationProvider result = target.getInformationProvider("latest");
    assertEquals(snapshot, result);
}
Also used : RepositorySnapshot(co.rsk.db.RepositorySnapshot) AccountInformationProvider(co.rsk.core.bc.AccountInformationProvider) Test(org.junit.Test)

Example 24 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method processMinersFeesWithNoSiblings.

@Test
public void processMinersFeesWithNoSiblings() {
    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 remascStorageProvider = getRemascStorageProvider(repository);
    assertEquals(Coin.ZERO, remascStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, remascStorageProvider.getBurnedBalance());
    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());
    long blockReward = minerFee / remascConfig.getSyntheticSpan();
    assertEquals(Coin.valueOf(minerFee - blockReward), repository.getAccountState(PrecompiledContracts.REMASC_ADDR).getBalance());
    long rskReward = blockReward / remascConfig.getRskLabsDivisor();
    assertEquals(Coin.valueOf(rskReward), repository.getAccountState(remascConfig.getRskLabsAddress()).getBalance());
    long federationReward = (blockReward - rskReward) / remascConfig.getFederationDivisor();
    assertEquals(33, federationReward);
    assertEquals(Coin.valueOf(blockReward - rskReward - federationReward), repository.getAccountState(coinbaseA).getBalance());
    remascStorageProvider = getRemascStorageProvider(repository);
    assertEquals(Coin.valueOf(minerFee - blockReward), remascStorageProvider.getRewardBalance());
    assertEquals(Coin.ZERO, remascStorageProvider.getBurnedBalance());
    this.validateFederatorsBalanceIsCorrect(repository, federationReward, newblock);
}
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 25 with RepositorySnapshot

use of co.rsk.db.RepositorySnapshot in project rskj by rsksmart.

the class RemascProcessMinerFeesTest method noPublisherFeeIsPaidWhenThePublisherHasNoSiblings.

@Test
public void noPublisherFeeIsPaidWhenThePublisherHasNoSiblings() {
    Blockchain blockchain = blockchainBuilder.build();
    BlockStore blockStore = blockchainBuilder.getBlockStore();
    RepositoryLocator repositoryLocator = blockchainBuilder.getRepositoryLocator();
    final long NUMBER_OF_TXS_WITH_FEES = 3;
    List<Block> blocks = createSimpleBlocks(genesisBlock, 4);
    Block blockWithOneTxD = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbaseD, Collections.emptyList(), minerFee, 0, txValue, cowKey);
    blocks.add(blockWithOneTxD);
    Block blockWithOneTxA = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxD, PegTestUtils.createHash3(), coinbaseA, Collections.emptyList(), minerFee, 1, txValue, cowKey);
    Block blockWithOneTxB = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxD, PegTestUtils.createHash3(), coinbaseB, Collections.emptyList(), minerFee * 3, 1, txValue, cowKey);
    blocks.add(blockWithOneTxA);
    Block blockThatIncludesUncleC = RemascTestRunner.createBlock(this.genesisBlock, blockWithOneTxA, PegTestUtils.createHash3(), coinbaseC, Collections.singletonList(blockWithOneTxB.getHeader()), minerFee, 2, txValue, cowKey);
    blocks.add(blockThatIncludesUncleC);
    blocks.addAll(createSimpleBlocks(blockThatIncludesUncleC, 7));
    BlockExecutor blockExecutor = buildBlockExecutor(repositoryLocator, blockStore);
    executeBlocks(blockchain, blocks, blockExecutor);
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    // validate that the blockchain's and REMASC's initial states are correct
    Coin cowRemainingBalance = cowInitialBalance.subtract(Coin.valueOf(minerFee * NUMBER_OF_TXS_WITH_FEES + txValue * NUMBER_OF_TXS_WITH_FEES));
    List<Long> otherAccountsBalance = new ArrayList<>(Arrays.asList(null, null, null, null));
    this.validateAccountsCurrentBalanceIsCorrect(repository, cowRemainingBalance, minerFee * NUMBER_OF_TXS_WITH_FEES, null, this.getAccountsWithExpectedBalance(otherAccountsBalance));
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), Coin.ZERO, Coin.ZERO, 1L);
    // add block to pay fees of blocks on blockchain's height 4
    Block blockToPayFeesOnHeightFour = RemascTestRunner.createBlock(this.genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), TestUtils.randomAddress(), Collections.emptyList(), minerFee, 0, txValue, cowKey);
    blockExecutor.executeAndFillAll(blockToPayFeesOnHeightFour, blockchain.getBestBlock().getHeader());
    blockchain.tryToConnect(blockToPayFeesOnHeightFour);
    repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    // -- After executing REMASC's contract for paying height 4 block
    // validate that account's balances are correct
    long blockRewardOnHeightFour = minerFee / remascConfig.getSyntheticSpan();
    long remascCurrentBalance = minerFee * 3 - blockRewardOnHeightFour;
    long rskCurrentBalance = blockRewardOnHeightFour / remascConfig.getRskLabsDivisor();
    blockRewardOnHeightFour -= rskCurrentBalance;
    long federationReward = blockRewardOnHeightFour / remascConfig.getFederationDivisor();
    assertEquals(33, federationReward);
    blockRewardOnHeightFour -= federationReward;
    List<Long> otherAccountsBalanceOnHeightFour = new ArrayList<>(Arrays.asList(null, null, null, blockRewardOnHeightFour));
    this.validateAccountsCurrentBalanceIsCorrect(repository, cowRemainingBalance, remascCurrentBalance, rskCurrentBalance, this.getAccountsWithExpectedBalance(otherAccountsBalanceOnHeightFour));
    // validate that REMASC's state is correct
    blockRewardOnHeightFour = minerFee / remascConfig.getSyntheticSpan();
    Coin expectedRewardBalance = Coin.valueOf(minerFee - blockRewardOnHeightFour);
    this.validateRemascsStorageIsCorrect(getRemascStorageProvider(repository), expectedRewardBalance, Coin.ZERO, 1L);
    this.validateFederatorsBalanceIsCorrect(repository, federationReward, blockToPayFeesOnHeightFour);
}
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) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

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