BAEL-1052: EthereumJ (#2614)

* Simple Boot REST application and example

* BAEL-509 - Removed extra duplicate ant matcher

* Example and Unit Tests

* documentation

* BAEL-1085 - changes per PR code review and document review - altered integration to unit test - all's good

* BAEL-1085 - Renamed unit tests and added both to pom.xml

* IntelliJ formatter

* REVERT - Had removed a duplicate ant matcher from BAEL-509 - this was the incorrect process - reverting now, article has been corrected, but will issue a seperate PR for this

* BAEL-509: Removed duplicate ant matcher - does not impact code at runtime

* BAEL-1085: Per editor's request, removed Angular client - using CURL POST commands now in article - as such moved the code into a more appropriate module: spring-rest

* BAEL-1052: EthereumJ

* BAEL-1052: .pom module renamed to EthereumJ

* Try catch clauses removed - tested and verified everything again
This commit is contained in:
Adam InTae Gerard 2017-09-17 02:43:49 -07:00 committed by Zeger Hendrikse
parent 9081c089f6
commit 1ced2a3b87
13 changed files with 393 additions and 0 deletions

6
ethereumj/.gitgnore Normal file
View File

@ -0,0 +1,6 @@
.idea
target
database
logs
target
*.iml

4
ethereumj/README.md Normal file
View File

@ -0,0 +1,4 @@
## EthereumJ
### Relevant Articles:
- [Introduction to EthereumJ](http://www.baeldung.com/intro-to-ethereumj)

110
ethereumj/pom.xml Normal file
View File

@ -0,0 +1,110 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.ethereumj</groupId>
<artifactId>ethereumj</artifactId>
<packaging>war</packaging>
<version>1.0.0</version>
<name>ethereumj</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<tomcat.version>8.5.4</tomcat.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<repositories>
<repository>
<id>Ethereum</id>
<name>Ethereum</name>
<url>https://dl.bintray.com/ethereum/maven/</url>
</repository>
</repositories>
<dependencies>
<!-- Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<!-- Unit Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.6.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- Ethereum -->
<dependency>
<groupId>org.ethereum</groupId>
<artifactId>ethereumj-core</artifactId>
<version>1.5.0-RELEASE</version>
</dependency>
<!-- JSTL/JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>ethereumj</finalName>
</build>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>none</exclude>
</excludes>
<includes>
<include>*/EthControllerTestOne.java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -0,0 +1,16 @@
package com.baeldung.ethereumj;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class ApplicationMain extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.ethereumj;
public class Constants {
public static final String ENDPOINT_ONE = "/api/get/bestblock/";
public static final String ENDPOINT_TWO = "/api/get/difficulty/";
public static final String RESPONSE_TYPE ="application/json/";
}

View File

@ -0,0 +1,25 @@
package com.baeldung.ethereumj.beans;
import com.baeldung.ethereumj.listeners.EthListener;
import org.ethereum.core.Block;
import org.ethereum.facade.Ethereum;
import org.ethereum.facade.EthereumFactory;
import java.math.BigInteger;
public class EthBean {
private Ethereum ethereum;
public void start() {
this.ethereum = EthereumFactory.createEthereum();
this.ethereum.addListener(new EthListener(ethereum));
}
public Block getBestBlock() {
return this.ethereum.getBlockchain().getBestBlock();
}
public BigInteger getTotalDifficulty() {
return this.ethereum.getBlockchain().getTotalDifficulty();
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.ethereumj.config;
import com.baeldung.ethereumj.beans.EthBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.Executors;
@Configuration
public class EthConfig {
@Bean
EthBean ethBeanConfig() throws Exception {
EthBean eBean = new EthBean();
Executors.newSingleThreadExecutor().submit(eBean::start);
return eBean;
}
}

View File

@ -0,0 +1,44 @@
package com.baeldung.ethereumj.controllers;
import com.baeldung.ethereumj.Constants;
import com.baeldung.ethereumj.beans.EthBean;
import com.baeldung.ethereumj.transfer.EthResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import javax.servlet.ServletContext;
@RestController
public class EthController {
@Autowired
EthBean ethBean;
@Autowired
private ServletContext servletContext;
private Logger l = LoggerFactory.getLogger(EthController.class);
@RequestMapping(Constants.ENDPOINT_ONE)
public EthResponse getBestBlock() {
l.debug("Request received - fetching best block.");
EthResponse r = new EthResponse();
r.setResponse(ethBean.getBestBlock().toString());
return r;
}
@RequestMapping(Constants.ENDPOINT_TWO)
public EthResponse getTotalDifficulty() {
l.debug("Request received - calculating total difficulty.");
EthResponse r = new EthResponse();
r.setResponse(ethBean.getTotalDifficulty().toString());
return r;
}
@PostConstruct
public void showIt() {
l.debug(servletContext.getContextPath());
}
}

View File

@ -0,0 +1,71 @@
package com.baeldung.ethereumj.listeners;
import org.ethereum.core.Block;
import org.ethereum.core.TransactionReceipt;
import org.ethereum.facade.Ethereum;
import org.ethereum.listener.EthereumListenerAdapter;
import org.ethereum.util.BIUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigInteger;
import java.util.List;
public class EthListener extends EthereumListenerAdapter {
private Logger l = LoggerFactory.getLogger(EthListener.class);
private Ethereum ethereum;
private boolean syncDone = false;
private static final int thou = 1000;
private void out(String t) {
l.info(t);
}
private String calcNetHashRate(Block block) {
String response = "Net hash rate not available";
if (block.getNumber() > thou) {
long timeDelta = 0;
for (int i = 0; i < thou; ++i) {
Block parent = ethereum
.getBlockchain()
.getBlockByHash(block.getParentHash());
timeDelta += Math.abs(block.getTimestamp() - parent.getTimestamp());
}
response = String.valueOf(block
.getDifficultyBI()
.divide(BIUtil.toBI(timeDelta / thou))
.divide(new BigInteger("1000000000"))
.doubleValue()) + " GH/s";
}
return response;
}
public EthListener(Ethereum ethereum) {
this.ethereum = ethereum;
}
@Override
public void onBlock(Block block, List<TransactionReceipt> receipts) {
if (syncDone) {
out("Net hash rate: " + calcNetHashRate(block));
out("Block difficulty: " + block.getDifficultyBI().toString());
out("Block transactions: " + block.getTransactionsList().toString());
out("Best block (last block): " + ethereum
.getBlockchain()
.getBestBlock().toString());
out("Total difficulty: " + ethereum
.getBlockchain()
.getTotalDifficulty().toString());
}
}
@Override
public void onSyncDone(SyncState state) {
out("onSyncDone " + state);
if (!syncDone) {
out(" ** SYNC DONE ** ");
syncDone = true;
}
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.ethereumj.transfer;
public class EthResponse {
private String response;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
}

View File

@ -0,0 +1,2 @@
server.servlet-path=/
server.port=8080

View File

@ -0,0 +1,72 @@
package com.baeldung.ethereumj.controllers;
import com.baeldung.ethereumj.ApplicationMain;
import com.baeldung.ethereumj.Constants;
import com.baeldung.ethereumj.transfer.EthResponse;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.*;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationMain.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@TestPropertySource(properties = "server.port=8080")
public class EthControllerTestOne {
@LocalServerPort
int port;
private RestTemplate restTemplate = new RestTemplate();
private String url(String uri) {
String s = "http://localhost:" + port + uri;
System.out.println(s);
return s;
}
@Before
public void setup() {
restTemplate = new RestTemplate();
}
@Test()
public void bestBlockTest() throws Exception {
Thread.sleep(20000);
EthResponse a = restTemplate.getForObject(url(Constants.ENDPOINT_ONE), EthResponse.class);
assertNotNull(a);
ResponseEntity<EthResponse> b = restTemplate.exchange(
url(Constants.ENDPOINT_ONE),
HttpMethod.GET, new HttpEntity<EthResponse>(null, new HttpHeaders()), EthResponse.class);
assertTrue("Status 200?", b.getStatusCode().equals(HttpStatus.OK));
System.out.println("Status 200?: " + b.getStatusCode().equals(HttpStatus.OK));
assertTrue("Dynamic data returned?", b.hasBody());
System.out.println("Dynamic data returned?: " + b.hasBody());
}
@Test()
public void difficultyTest() throws Exception {
Thread.sleep(20000);
ResponseEntity<EthResponse> a = restTemplate.exchange(
url(Constants.ENDPOINT_TWO),
HttpMethod.GET, new HttpEntity<EthResponse>(null, new HttpHeaders()), EthResponse.class);
assertTrue("Status 200?", a.getStatusCode().equals(HttpStatus.OK));
System.out.println("Status 200?: " + a.getStatusCode().equals(HttpStatus.OK));
assertTrue("Dynamic data returned?", a.hasBody());
System.out.println("Dynamic data returned?: " + a.hasBody());
}
}

View File

@ -50,6 +50,8 @@
<module>deltaspike</module>
<module>dozer</module>
<module>ethereumj</module>
<!--<module>ejb</module>-->
<module>feign</module>