Search in sources :

Example 26 with RepositoryImpl

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

the class BlockChainImplTest method addValidMGPBlock.

@Test
public void addValidMGPBlock() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), (DB) null);
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.blockStore(blockStore).addPrevMinGasPriceRule().addTxsMinGasPriceRule();
    BlockChainImpl blockChain = createBlockChain(repository, blockStore, validatorBuilder.build());
    Repository track = repository.startTracking();
    Account account = BlockExecutorTest.createAccount("acctest1", track, Coin.valueOf(100000));
    Assert.assertTrue(account.getEcKey().hasPrivKey());
    track.commit();
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000100", BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.valueOf(22000L));
    tx.sign(account.getEcKey().getPrivKeyBytes());
    txs.add(tx);
    Block genesis = getGenesisBlock(blockChain);
    genesis.setStateRoot(repository.getRoot());
    genesis.flushRLP();
    Block block = new BlockBuilder().minGasPrice(BigInteger.ZERO).transactions(txs).parent(genesis).build();
    BlockExecutor executor = new BlockExecutor(config, repository, null, null, null);
    executor.executeAndFill(block, genesis);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryImpl(co.rsk.db.RepositoryImpl) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 27 with RepositoryImpl

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

the class BlockChainImplTest method addInvalidMGPBlock.

@Test
public void addInvalidMGPBlock() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(repository).addPrevMinGasPriceRule().addTxsMinGasPriceRule();
    BlockChainImpl blockChain = createBlockChain(repository, blockStore, validatorBuilder.build());
    Block genesis = getGenesisBlock(blockChain);
    Block block = new BlockBuilder().minGasPrice(BigInteger.ONE).parent(genesis).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.INVALID_BLOCK, blockChain.tryToConnect(block));
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000006", BigInteger.ZERO, BigInteger.ZERO, BigInteger.valueOf(1L), BigInteger.TEN);
    tx.sign(new byte[] { 22, 11, 00 });
    txs.add(tx);
    block = new BlockBuilder().transactions(txs).minGasPrice(BigInteger.valueOf(11L)).parent(genesis).build();
    Assert.assertEquals(ImportResult.INVALID_BLOCK, blockChain.tryToConnect(block));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryImpl(co.rsk.db.RepositoryImpl) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 28 with RepositoryImpl

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

the class BlockExecutorTest method executeBlockWithTwoTransactions.

@Test
public void executeBlockWithTwoTransactions() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    Repository track = repository.startTracking();
    Account account = createAccount("acctest1", track, Coin.valueOf(60000));
    Account account2 = createAccount("acctest2", track, Coin.valueOf(10L));
    track.commit();
    Assert.assertFalse(Arrays.equals(EMPTY_TRIE_HASH, repository.getRoot()));
    BlockExecutor executor = new BlockExecutor(config, repository, null, null, null);
    Transaction tx1 = createTransaction(account, account2, BigInteger.TEN, repository.getNonce(account.getAddress()));
    Transaction tx2 = createTransaction(account, account2, BigInteger.TEN, repository.getNonce(account.getAddress()).add(BigInteger.ONE));
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx1);
    txs.add(tx2);
    List<BlockHeader> uncles = new ArrayList<>();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock(), txs, uncles, 1, null);
    BlockResult result = executor.execute(block, repository.getRoot(), false);
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.getTransactionReceipts());
    Assert.assertFalse(result.getTransactionReceipts().isEmpty());
    Assert.assertEquals(2, result.getTransactionReceipts().size());
    TransactionReceipt receipt = result.getTransactionReceipts().get(0);
    Assert.assertEquals(tx1, receipt.getTransaction());
    Assert.assertEquals(21000, new BigInteger(1, receipt.getGasUsed()).longValue());
    Assert.assertEquals(21000, BigIntegers.fromUnsignedByteArray(receipt.getCumulativeGas()).longValue());
    Assert.assertTrue(receipt.hasTxStatus() && receipt.isTxStatusOK() && receipt.isSuccessful());
    receipt = result.getTransactionReceipts().get(1);
    Assert.assertEquals(tx2, receipt.getTransaction());
    Assert.assertEquals(21000, new BigInteger(1, receipt.getGasUsed()).longValue());
    Assert.assertEquals(42000, BigIntegers.fromUnsignedByteArray(receipt.getCumulativeGas()).longValue());
    Assert.assertTrue(receipt.hasTxStatus() && receipt.isTxStatusOK() && receipt.isSuccessful());
    Assert.assertEquals(42000, result.getGasUsed());
    Assert.assertEquals(42000, result.getPaidFees().asBigInteger().intValueExact());
    Assert.assertNotNull(result.getReceiptsRoot());
    Assert.assertArrayEquals(BlockChainImpl.calcReceiptsTrie(result.getTransactionReceipts()), result.getReceiptsRoot());
    Assert.assertFalse(Arrays.equals(repository.getRoot(), result.getStateRoot()));
    Assert.assertNotNull(result.getLogsBloom());
    Assert.assertEquals(256, result.getLogsBloom().length);
    for (int k = 0; k < result.getLogsBloom().length; k++) Assert.assertEquals(0, result.getLogsBloom()[k]);
    AccountState accountState = repository.getAccountState(account.getAddress());
    Assert.assertNotNull(accountState);
    Assert.assertEquals(BigInteger.valueOf(60000), accountState.getBalance().asBigInteger());
    Repository finalRepository = repository.getSnapshotTo(result.getStateRoot());
    accountState = finalRepository.getAccountState(account.getAddress());
    Assert.assertNotNull(accountState);
    Assert.assertEquals(BigInteger.valueOf(60000 - 42000 - 20), accountState.getBalance().asBigInteger());
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RepositoryImpl(co.rsk.db.RepositoryImpl) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Example 29 with RepositoryImpl

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

the class CommonConfig method repository.

@Bean
public Repository repository(RskSystemProperties config) {
    String databaseDir = config.databaseDir();
    if (config.databaseReset()) {
        FileUtil.recursiveDelete(databaseDir);
        logger.info("Database reset done");
    }
    KeyValueDataSource ds = makeDataSource(config, "state");
    KeyValueDataSource detailsDS = makeDataSource(config, "details");
    return new RepositoryImpl(config, new TrieStoreImpl(ds), detailsDS);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) RepositoryImpl(co.rsk.db.RepositoryImpl) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Bean(org.springframework.context.annotation.Bean)

Example 30 with RepositoryImpl

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

the class BridgeStateTest method recreateFromEmptyStorageProvider.

@Test
public void recreateFromEmptyStorageProvider() throws IOException {
    RskSystemProperties config = new RskSystemProperties();
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    BridgeConstants bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
    BridgeState state = new BridgeState(42, provider);
    BridgeState clone = BridgeState.create(bridgeConstants, state.getEncoded());
    Assert.assertNotNull(clone);
    Assert.assertEquals(42, clone.getBtcBlockchainBestChainHeight());
    Assert.assertTrue(clone.getBtcTxHashesAlreadyProcessed().isEmpty());
    Assert.assertTrue(clone.getActiveFederationBtcUTXOs().isEmpty());
    Assert.assertTrue(clone.getRskTxsWaitingForSignatures().isEmpty());
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) HashMapDB(org.ethereum.datasource.HashMapDB) BridgeConstants(co.rsk.config.BridgeConstants) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Aggregations

RepositoryImpl (co.rsk.db.RepositoryImpl)62 Test (org.junit.Test)54 Repository (org.ethereum.core.Repository)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 RskAddress (co.rsk.core.RskAddress)19 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)16 HashMapDB (org.ethereum.datasource.HashMapDB)15 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)13 org.ethereum.core (org.ethereum.core)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)12 LogInfo (org.ethereum.vm.LogInfo)12 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)11 BridgeConstants (co.rsk.config.BridgeConstants)10 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)10 DataWord (org.ethereum.vm.DataWord)9 ArrayList (java.util.ArrayList)8 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)7 ECKey (org.ethereum.crypto.ECKey)7 Keccak256 (co.rsk.crypto.Keccak256)6 SimpleBlockChain (co.rsk.peg.simples.SimpleBlockChain)6