Search in sources :

Example 1 with EthereumBackend

use of org.adridadou.ethereum.propeller.EthereumBackend in project eth-propeller-ethj by adridadou.

the class PrivateEthereumFacadeProvider method create.

public EthereumFacade create(PrivateNetworkConfig config, EthereumConfig ethereumConfig) {
    final boolean dagCached = new File("cachedDag/mine-dag.dat").exists();
    if (config.isResetPrivateBlockchain()) {
        deleteFolder(new File(config.getDbName()), true);
    }
    if (dagCached) {
        new File(config.getDbName()).mkdirs();
        try {
            FileUtils.copyFile(new File("cachedDag/mine-dag.dat"), new File(config.getDbName() + "/mine-dag.dat"));
            FileUtils.copyFile(new File("cachedDag/mine-dag-light.dat"), new File(config.getDbName() + "/mine-dag-light.dat"));
        } catch (IOException e) {
            throw new EthereumApiException("error while copying dag files", e);
        }
    }
    MinerConfig.dbName = config.getDbName();
    Ethereum ethereum = EthereumFactory.createEthereum(MinerConfig.class);
    EthereumBackend ethereumBackend = new EthereumReal(ethereum);
    ethereum.initSyncing();
    if (!dagCached) {
        try {
            new File("cachedDag").mkdirs();
            FileUtils.copyFile(new File(config.getDbName() + "/mine-dag.dat"), new File("cachedDag/mine-dag.dat"));
            FileUtils.copyFile(new File(config.getDbName() + "/mine-dag-light.dat"), new File("cachedDag/mine-dag-light.dat"));
        } catch (IOException e) {
            log.warn("couldn't copy files: " + e.getMessage());
        }
    }
    EthereumEventHandler ethereumListener = new EthereumEventHandler();
    final EthereumFacade facade = CoreEthereumFacadeProvider.create(ethereumBackend, ethereumConfig);
    // This event does not trigger when you are the miner
    ethereumListener.onReady();
    facade.events().ready().thenAccept((b) -> config.getInitialBalances().entrySet().stream().map(entry -> facade.sendEther(mainAccount, entry.getKey().getAddress(), entry.getValue())).forEach(result -> {
        try {
            result.get();
        } catch (InterruptedException | ExecutionException e) {
            throw new EthereumApiException("error while setting the initial balances");
        }
    }));
    return facade;
}
Also used : Ethash(org.ethereum.mine.Ethash) LoggerFactory(org.slf4j.LoggerFactory) AccountProvider(org.adridadou.ethereum.propeller.keystore.AccountProvider) EthereumEventHandler(org.adridadou.ethereum.propeller.event.EthereumEventHandler) Block(org.ethereum.core.Block) MinerListener(org.ethereum.mine.MinerListener) SystemProperties(org.ethereum.config.SystemProperties) Ethereum(org.ethereum.facade.Ethereum) ConfigFactory(com.typesafe.config.ConfigFactory) CoreEthereumFacadeProvider(org.adridadou.ethereum.propeller.CoreEthereumFacadeProvider) EthereumReal(org.adridadou.ethereum.ethj.EthereumReal) Logger(org.slf4j.Logger) EthereumBackend(org.adridadou.ethereum.propeller.EthereumBackend) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) EthereumFactory(org.ethereum.facade.EthereumFactory) DatabaseDirectory(org.adridadou.ethereum.values.config.DatabaseDirectory) File(java.io.File) EthAccount(org.adridadou.ethereum.propeller.values.EthAccount) EthereumFacade(org.adridadou.ethereum.propeller.EthereumFacade) ExecutionException(java.util.concurrent.ExecutionException) EthereumApiException(org.adridadou.ethereum.propeller.exception.EthereumApiException) EthereumConfig(org.adridadou.ethereum.propeller.EthereumConfig) BasicSample(org.ethereum.samples.BasicSample) Bean(org.springframework.context.annotation.Bean) EthereumJConfigs(org.adridadou.ethereum.ethj.EthereumJConfigs) EthereumReal(org.adridadou.ethereum.ethj.EthereumReal) EthereumEventHandler(org.adridadou.ethereum.propeller.event.EthereumEventHandler) Ethereum(org.ethereum.facade.Ethereum) EthereumFacade(org.adridadou.ethereum.propeller.EthereumFacade) IOException(java.io.IOException) File(java.io.File) EthereumApiException(org.adridadou.ethereum.propeller.exception.EthereumApiException) EthereumBackend(org.adridadou.ethereum.propeller.EthereumBackend)

Aggregations

ConfigFactory (com.typesafe.config.ConfigFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 EthereumJConfigs (org.adridadou.ethereum.ethj.EthereumJConfigs)1 EthereumReal (org.adridadou.ethereum.ethj.EthereumReal)1 CoreEthereumFacadeProvider (org.adridadou.ethereum.propeller.CoreEthereumFacadeProvider)1 EthereumBackend (org.adridadou.ethereum.propeller.EthereumBackend)1 EthereumConfig (org.adridadou.ethereum.propeller.EthereumConfig)1 EthereumFacade (org.adridadou.ethereum.propeller.EthereumFacade)1 EthereumEventHandler (org.adridadou.ethereum.propeller.event.EthereumEventHandler)1 EthereumApiException (org.adridadou.ethereum.propeller.exception.EthereumApiException)1 AccountProvider (org.adridadou.ethereum.propeller.keystore.AccountProvider)1 EthAccount (org.adridadou.ethereum.propeller.values.EthAccount)1 DatabaseDirectory (org.adridadou.ethereum.values.config.DatabaseDirectory)1 FileUtils (org.apache.commons.io.FileUtils)1 SystemProperties (org.ethereum.config.SystemProperties)1 Block (org.ethereum.core.Block)1 Ethereum (org.ethereum.facade.Ethereum)1 EthereumFactory (org.ethereum.facade.EthereumFactory)1