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