Ethereumj

EthereumJ 連接到私有節點

  • January 21, 2022

我需要使用沒有 ethereum.conf 文件的 ethereumj 連接到私有乙太坊節點。我需要使用 Spring Framework 從程式碼配置連接。

它對我有用。可能會對某些人有所幫助。

public class EthereumAppConfig implements BeanPostProcessor {

   private static final List<String> ipList = new ArrayList<>();

   private static ConfigValue value(Object value) {
       return ConfigValueFactory.fromAnyRef(value);
   }





   @Override
   public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
       if (o instanceof SystemProperties)
           ((SystemProperties) o).overrideParams(getConfig());

       return o;
   }

   @Override
   public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
       return o;
   }








   private static Config getConfig() {
       ipList.add("0.0.0.0:00000");
       List<Map<String, String>> peerActives = new ArrayList<>();


       Map<String, String> urls = new HashMap<>();

       urls.put("url", "enode://0");
       urls.put("url", "enode://1");
       urls.put("url", "enode://2");
       urls.put("url", "enode://4");

       peerActives.add(
               urls
       );
       return ConfigFactory.empty()
               .withValue("peer.discovery.enabled", value(true))
               .withValue("peer.discovery.external.ip", value("0.0.0.0"))
               .withValue("peer.discovery.bind.ip", value("0.0.0.0"))
               .withValue("peer.discovery.persist", value("false"))
               .withValue("peer.listen.port", value(00000))
               .withValue("peer.privateKey", value(Hex.toHexString(ECKey.fromPrivate(("0").getBytes()).getPrivKeyBytes())))
               .withValue("peer.networkId", value(76543))
               .withValue("sync.enabled", value(true))
               .withValue("genesis", value("genesis.json"))
               .withValue("database.dir", value("database"))
               .withValue("peer.discovery.ip.list", value(ipList))
               .withValue("peer.active", value(peerActives))
               .withValue("mine.start", value(true));
   }

}

請查看我上傳到 Github 的程式碼,它正是這樣做的 :) 它包含文件中的所有相關配置.conf

引用自:https://ethereum.stackexchange.com/questions/21642