use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.
the class ImportLightTest method createBlockchain.
public static BlockChainImpl createBlockchain(Genesis genesis) {
RskSystemProperties config = new RskSystemProperties();
config.setBlockchainConfig(new GenesisConfig(new GenesisConfig.GenesisConstants() {
@Override
public BlockDifficulty getMinimumDifficulty() {
return new BlockDifficulty(BigInteger.ONE);
}
}));
IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
EthereumListenerAdapter listener = new EthereumListenerAdapter();
KeyValueDataSource ds = new HashMapDB();
ds.init();
ReceiptStore receiptStore = new ReceiptStoreImpl(ds);
BlockChainImpl blockchain = new BlockChainImpl(config, repository, blockStore, receiptStore, null, listener, new AdminInfo(), new DummyBlockValidator());
blockchain.setNoValidation(true);
TransactionPoolImpl transactionPool = new TransactionPoolImpl(config, repository, null, receiptStore, null, listener, 10, 100);
blockchain.setTransactionPool(transactionPool);
Repository track = repository.startTracking();
for (RskAddress addr : genesis.getPremine().keySet()) {
track.createAccount(addr);
track.addBalance(addr, genesis.getPremine().get(addr).getAccountState().getBalance());
}
track.commit();
genesis.setStateRoot(repository.getRoot());
genesis.flushRLP();
blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
blockchain.setBestBlock(genesis);
blockchain.setTotalDifficulty(genesis.getCumulativeDifficulty());
return blockchain;
}
use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.
the class ContractDetailsImplTest method usingSameExternalStorage.
@Test
public void usingSameExternalStorage() {
TrieStore store = new TrieStoreImpl(new HashMapDB());
Trie trie = new TrieImpl(store, false);
byte[] accountAddress = randomAddress();
ContractDetailsImpl details = new ContractDetailsImpl(config, accountAddress, trie, null);
int nkeys = IN_MEMORY_STORAGE_LIMIT;
for (int k = 1; k <= nkeys + 1; k++) details.put(new DataWord(k), new DataWord(k * 2));
Assert.assertTrue(details.hasExternalStorage());
details.syncStorage();
ContractDetailsImpl details1 = new ContractDetailsImpl(config, details.getEncoded());
ContractDetailsImpl details2 = new ContractDetailsImpl(config, details.getEncoded());
Assert.assertTrue(details1.hasExternalStorage());
Assert.assertTrue(details2.hasExternalStorage());
for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details1.get(new DataWord(k)));
for (int k = 1; k <= nkeys + 1; k++) Assert.assertNotNull(details2.get(new DataWord(k)));
details1.syncStorage();
details2.syncStorage();
}
use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.
the class ContractDetailsImplTest method externalStorageTransition.
@Test
public void externalStorageTransition() {
byte[] address = randomAddress();
byte[] code = randomBytes(512);
Map<DataWord, DataWord> elements = new HashMap<>();
HashMapDB externalStorage = new HashMapDB();
ContractDetailsImpl original = new ContractDetailsImpl(config, address, new TrieImpl(new TrieStoreImpl(externalStorage), true), code);
for (int i = 0; i < IN_MEMORY_STORAGE_LIMIT - 1; i++) {
DataWord key = randomDataWord();
DataWord value = randomDataWord();
elements.put(key, value);
original.put(key, value);
}
original.syncStorage();
ContractDetails deserialized = new ContractDetailsImpl(config, original.getEncoded());
// adds keys for in-memory storage limit overflow
for (int i = 0; i < 10; i++) {
DataWord key = randomDataWord();
DataWord value = randomDataWord();
elements.put(key, value);
deserialized.put(key, value);
}
deserialized.syncStorage();
deserialized = new ContractDetailsImpl(config, deserialized.getEncoded());
Map<DataWord, DataWord> storage = deserialized.getStorage();
Assert.assertEquals(elements.size(), storage.size());
for (DataWord key : elements.keySet()) {
Assert.assertEquals(elements.get(key), storage.get(key));
}
}
use of co.rsk.trie.TrieStoreImpl in project rskj by rsksmart.
the class RepositoryImplTest method flushNoReconnect.
@Test
public void flushNoReconnect() {
TrieStore store = new TrieStoreImpl(new HashMapDB());
RepositoryImpl repository = new RepositoryImpl(config, store);
RskAddress accAddress = randomAccountAddress();
byte[] initialRoot = repository.getRoot();
repository.createAccount(accAddress);
repository.flushNoReconnect();
Assert.assertTrue(repository.isExist(accAddress));
}
use of co.rsk.trie.TrieStoreImpl 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));
}
Aggregations