use of org.ethereum.vm.PrecompiledContracts in project rskj by rsksmart.
the class SyncProcessorTest method processBodyResponseWithTransactionAddsToBlockchain.
@Test
public void processBodyResponseWithTransactionAddsToBlockchain() {
Account senderAccount = createAccount("sender");
Account receiverAccount = createAccount("receiver");
Map<RskAddress, AccountState> accounts = new HashMap<>();
accounts.put(senderAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.valueOf(20000000)));
accounts.put(receiverAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.ZERO));
final NetBlockStore store = new NetBlockStore();
BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
Blockchain blockchain = blockChainBuilder.ofSize(0, false, accounts);
Block genesis = blockchain.getBestBlock();
SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
List<Transaction> txs = Collections.singletonList(createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO));
Block block = new BlockGenerator().createChildBlock(genesis, txs, blockChainBuilder.getRepository().getRoot());
StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(blockChainBuilder.getTrieStore(), stateRootHandler), new TransactionExecutorFactory(config, blockChainBuilder.getBlockStore(), null, blockFactory, new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
Assert.assertEquals(1, block.getTransactionsList().size());
blockExecutor.executeAndFillAll(block, genesis.getHeader());
Assert.assertEquals(21000, block.getFeesPaidToMiner().asBigInteger().intValueExact());
Assert.assertEquals(1, block.getTransactionsList().size());
Assert.assertEquals(1, block.getNumber());
Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
BlockNodeInformation nodeInformation = new BlockNodeInformation();
TestSystemProperties config = new TestSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
List<Transaction> transactions = block.getTransactionsList();
List<BlockHeader> uncles = block.getUncleList();
long lastRequestId = new Random().nextLong();
BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
processor.registerExpectedMessage(response);
Deque<BlockHeader> headerStack = new ArrayDeque<>();
headerStack.add(block.getHeader());
List<Deque<BlockHeader>> headers = new ArrayList<>();
headers.add(headerStack);
List<BlockIdentifier> bids = new ArrayList<>();
bids.add(new BlockIdentifier(blockchain.getBlockByNumber(0).getHash().getBytes(), 0));
bids.add(new BlockIdentifier(block.getHash().getBytes(), 1));
processor.startDownloadingBodies(headers, Collections.singletonMap(sender, bids), sender);
((DownloadingBodiesSyncState) processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
processor.processBodyResponse(sender, response);
Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
use of org.ethereum.vm.PrecompiledContracts in project rskj by rsksmart.
the class RemascTestRunner method start.
public void start() {
this.blockchain = this.builder.build();
((BlockChainImpl) this.blockchain).setNoValidation(true);
this.addedSiblings = new ArrayList<>();
List<Block> mainChainBlocks = new ArrayList<>();
this.blockchain.tryToConnect(this.genesis);
BlockFactory blockFactory = new BlockFactory(builder.getConfig().getActivationConfig());
final ProgramInvokeFactoryImpl programInvokeFactory = new ProgramInvokeFactoryImpl();
BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(builder.getConfig().getNetworkConstants().getBridgeConstants().getBtcParams()), builder.getConfig().getNetworkConstants().getBridgeConstants(), builder.getConfig().getActivationConfig());
PrecompiledContracts precompiledContracts = new PrecompiledContracts(builder.getConfig(), bridgeSupportFactory);
BlockExecutor blockExecutor = new BlockExecutor(builder.getConfig().getActivationConfig(), builder.getRepositoryLocator(), new TransactionExecutorFactory(builder.getConfig(), builder.getBlockStore(), null, blockFactory, programInvokeFactory, precompiledContracts, blockTxSignatureCache));
for (int i = 0; i <= this.initialHeight; i++) {
int finalI = i;
List<SiblingElement> siblingsForCurrentHeight = this.siblingElements.stream().filter(siblingElement -> siblingElement.getHeightToBeIncluded() == finalI).collect(Collectors.toList());
List<BlockHeader> blockSiblings = new ArrayList<>();
// Going to add siblings
BlockDifficulty cummDifficulty = BlockDifficulty.ZERO;
if (siblingsForCurrentHeight.size() > 0) {
cummDifficulty = blockchain.getTotalDifficulty();
}
for (SiblingElement sibling : siblingsForCurrentHeight) {
RskAddress siblingCoinbase = TestUtils.randomAddress();
Block mainchainSiblingParent = mainChainBlocks.get(sibling.getHeight() - 1);
Block siblingBlock = createBlock(this.genesis, mainchainSiblingParent, PegTestUtils.createHash3(), siblingCoinbase, Collections.emptyList(), minerFee, this.gasPrice, (long) i, this.txValue, this.txSigningKey, null);
blockSiblings.add(siblingBlock.getHeader());
builder.getBlockStore().saveBlock(siblingBlock, cummDifficulty.add(siblingBlock.getCumulativeDifficulty()), false);
this.addedSiblings.add(siblingBlock);
}
long txNonce = i;
RskAddress coinbase = fixedCoinbase != null ? fixedCoinbase : TestUtils.randomAddress();
Block block = createBlock(this.genesis, this.blockchain.getBestBlock(), PegTestUtils.createHash3(), coinbase, blockSiblings, minerFee, this.gasPrice, txNonce, this.txValue, this.txSigningKey, null);
mainChainBlocks.add(block);
blockExecutor.executeAndFillAll(block, this.blockchain.getBestBlock().getHeader());
block.seal();
ImportResult result = this.blockchain.tryToConnect(block);
System.out.println(result);
}
}
use of org.ethereum.vm.PrecompiledContracts in project rskj by rsksmart.
the class World method getBlockExecutor.
public BlockExecutor getBlockExecutor() {
final ProgramInvokeFactoryImpl programInvokeFactory = new ProgramInvokeFactoryImpl();
Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams());
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(btcBlockStoreFactory, config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
if (this.blockExecutor == null) {
this.blockExecutor = new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(getTrieStore(), stateRootHandler), new TransactionExecutorFactory(config, blockStore, null, new BlockFactory(config.getActivationConfig()), programInvokeFactory, new PrecompiledContracts(config, bridgeSupportFactory), blockTxSignatureCache));
}
return this.blockExecutor;
}
use of org.ethereum.vm.PrecompiledContracts in project rskj by rsksmart.
the class BlockChainBuilder method build.
public BlockChainImpl build() {
BlocksIndex blocksIndex = new HashMapBlocksIndex();
if (config == null) {
config = new TestSystemProperties();
}
if (trieStore == null) {
trieStore = new TrieStoreImpl(new HashMapDB().setClearOnClose(false));
}
if (repository == null) {
repository = new MutableRepository(trieStore, new Trie(trieStore));
}
if (stateRootHandler == null) {
stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
}
if (genesis == null) {
genesis = new BlockGenerator().getGenesisBlock();
}
GenesisLoaderImpl.loadGenesisInitalState(repository, genesis);
repository.commit();
genesis.setStateRoot(repository.getRoot());
genesis.flushRLP();
BlockFactory blockFactory = new BlockFactory(config.getActivationConfig());
if (blockStore == null) {
blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), blocksIndex);
}
if (receiptStore == null) {
KeyValueDataSource ds = new HashMapDB();
ds.init();
receiptStore = new ReceiptStoreImpl(ds);
}
if (listener == null) {
listener = new BlockExecutorTest.SimpleEthereumListener();
}
if (bridgeSupportFactory == null) {
bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
}
BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(trieStore).blockStore(blockStore);
BlockValidator blockValidator = validatorBuilder.build();
ReceivedTxSignatureCache receivedTxSignatureCache = new ReceivedTxSignatureCache();
BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(receivedTxSignatureCache);
TransactionExecutorFactory transactionExecutorFactory = new TransactionExecutorFactory(config, blockStore, receiptStore, blockFactory, new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), blockTxSignatureCache);
repositoryLocator = new RepositoryLocator(trieStore, stateRootHandler);
transactionPool = new TransactionPoolImpl(config, repositoryLocator, this.blockStore, blockFactory, new TestCompositeEthereumListener(), transactionExecutorFactory, new ReceivedTxSignatureCache(), 10, 100);
BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), repositoryLocator, transactionExecutorFactory);
BlockChainImpl blockChain = new BlockChainLoader(blockStore, receiptStore, transactionPool, listener, blockValidator, blockExecutor, genesis, stateRootHandler, repositoryLocator).loadBlockchain();
if (this.testing) {
blockChain.setBlockValidator(new DummyBlockValidator());
blockChain.setNoValidation(true);
}
blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
if (this.blocks != null) {
for (Block b : this.blocks) {
blockExecutor.executeAndFillAll(b, blockChain.getBestBlock().getHeader());
blockChain.tryToConnect(b);
}
}
return blockChain;
}
use of org.ethereum.vm.PrecompiledContracts in project rskj by rsksmart.
the class BlockBuilder method build.
public Block build() {
Block block = blockGenerator.createChildBlock(parent, txs, uncles, difficulty, this.minGasPrice, gasLimit);
if (blockChain != null) {
final TestSystemProperties config = new TestSystemProperties();
StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
BlockExecutor executor = new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(trieStore, stateRootHandler), new TransactionExecutorFactory(config, blockStore, null, new BlockFactory(config.getActivationConfig()), new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
executor.executeAndFill(block, parent.getHeader());
}
return block;
}
Aggregations