Skip to content

Commit 1ced2a3

Browse files
Thoughtscriptzhendrikse
authored andcommitted
BAEL-1052: EthereumJ (eugenp#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
1 parent 9081c08 commit 1ced2a3

13 files changed

Lines changed: 393 additions & 0 deletions

File tree

‎ethereumj/.gitgnore‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
target
3+
database
4+
logs
5+
target
6+
*.iml

‎ethereumj/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## EthereumJ
2+
3+
### Relevant Articles:
4+
- [Introduction to EthereumJ](http://www.baeldung.com/intro-to-ethereumj)

‎ethereumj/pom.xml‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung.ethereumj</groupId>
6+
<artifactId>ethereumj</artifactId>
7+
<packaging>war</packaging>
8+
<version>1.0.0</version>
9+
<name>ethereumj</name>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<java.version>1.8</java.version>
14+
<tomcat.version>8.5.4</tomcat.version>
15+
</properties>
16+
17+
<parent>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-parent</artifactId>
20+
<version>1.5.6.RELEASE</version>
21+
</parent>
22+
23+
<repositories>
24+
<repository>
25+
<id>Ethereum</id>
26+
<name>Ethereum</name>
27+
<url>https://dl.bintray.com/ethereum/maven/</url>
28+
</repository>
29+
</repositories>
30+
31+
<dependencies>
32+
33+
<!-- Spring Boot Dependencies -->
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-tomcat</artifactId>
41+
<!--<scope>provided</scope>-->
42+
</dependency>
43+
44+
<!-- Unit Testing -->
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<version>1.5.6.RELEASE</version>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
<!-- Ethereum -->
53+
<dependency>
54+
<groupId>org.ethereum</groupId>
55+
<artifactId>ethereumj-core</artifactId>
56+
<version>1.5.0-RELEASE</version>
57+
</dependency>
58+
59+
<!-- JSTL/JSP -->
60+
<dependency>
61+
<groupId>javax.servlet</groupId>
62+
<artifactId>jstl</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.fasterxml.jackson.core</groupId>
66+
<artifactId>jackson-databind</artifactId>
67+
<version>2.5.0</version>
68+
</dependency>
69+
</dependencies>
70+
71+
<build>
72+
<plugins>
73+
<plugin>
74+
<groupId>org.springframework.boot</groupId>
75+
<artifactId>spring-boot-maven-plugin</artifactId>
76+
</plugin>
77+
</plugins>
78+
<finalName>ethereumj</finalName>
79+
</build>
80+
81+
<profiles>
82+
<profile>
83+
<id>integration</id>
84+
<build>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-surefire-plugin</artifactId>
89+
<executions>
90+
<execution>
91+
<phase>integration-test</phase>
92+
<goals>
93+
<goal>test</goal>
94+
</goals>
95+
<configuration>
96+
<excludes>
97+
<exclude>none</exclude>
98+
</excludes>
99+
<includes>
100+
<include>*/EthControllerTestOne.java</include>
101+
</includes>
102+
</configuration>
103+
</execution>
104+
</executions>
105+
</plugin>
106+
</plugins>
107+
</build>
108+
</profile>
109+
</profiles>
110+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.ethereumj;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
7+
import org.springframework.boot.web.support.SpringBootServletInitializer;
8+
9+
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
10+
@SpringBootApplication
11+
public class ApplicationMain extends SpringBootServletInitializer {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(ApplicationMain.class, args);
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.ethereumj;
2+
3+
public class Constants {
4+
5+
public static final String ENDPOINT_ONE = "/api/get/bestblock/";
6+
public static final String ENDPOINT_TWO = "/api/get/difficulty/";
7+
public static final String RESPONSE_TYPE ="application/json/";
8+
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.ethereumj.beans;
2+
3+
import com.baeldung.ethereumj.listeners.EthListener;
4+
import org.ethereum.core.Block;
5+
import org.ethereum.facade.Ethereum;
6+
import org.ethereum.facade.EthereumFactory;
7+
8+
import java.math.BigInteger;
9+
10+
public class EthBean {
11+
private Ethereum ethereum;
12+
13+
public void start() {
14+
this.ethereum = EthereumFactory.createEthereum();
15+
this.ethereum.addListener(new EthListener(ethereum));
16+
}
17+
18+
public Block getBestBlock() {
19+
return this.ethereum.getBlockchain().getBestBlock();
20+
}
21+
22+
public BigInteger getTotalDifficulty() {
23+
return this.ethereum.getBlockchain().getTotalDifficulty();
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.ethereumj.config;
2+
3+
import com.baeldung.ethereumj.beans.EthBean;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
import java.util.concurrent.Executors;
8+
9+
@Configuration
10+
public class EthConfig {
11+
12+
@Bean
13+
EthBean ethBeanConfig() throws Exception {
14+
EthBean eBean = new EthBean();
15+
Executors.newSingleThreadExecutor().submit(eBean::start);
16+
return eBean;
17+
}
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.ethereumj.controllers;
2+
3+
import com.baeldung.ethereumj.Constants;
4+
import com.baeldung.ethereumj.beans.EthBean;
5+
import com.baeldung.ethereumj.transfer.EthResponse;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import javax.annotation.PostConstruct;
13+
import javax.servlet.ServletContext;
14+
15+
@RestController
16+
public class EthController {
17+
18+
@Autowired
19+
EthBean ethBean;
20+
@Autowired
21+
private ServletContext servletContext;
22+
private Logger l = LoggerFactory.getLogger(EthController.class);
23+
24+
@RequestMapping(Constants.ENDPOINT_ONE)
25+
public EthResponse getBestBlock() {
26+
l.debug("Request received - fetching best block.");
27+
EthResponse r = new EthResponse();
28+
r.setResponse(ethBean.getBestBlock().toString());
29+
return r;
30+
}
31+
32+
@RequestMapping(Constants.ENDPOINT_TWO)
33+
public EthResponse getTotalDifficulty() {
34+
l.debug("Request received - calculating total difficulty.");
35+
EthResponse r = new EthResponse();
36+
r.setResponse(ethBean.getTotalDifficulty().toString());
37+
return r;
38+
}
39+
40+
@PostConstruct
41+
public void showIt() {
42+
l.debug(servletContext.getContextPath());
43+
}
44+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.ethereumj.listeners;
2+
3+
import org.ethereum.core.Block;
4+
import org.ethereum.core.TransactionReceipt;
5+
import org.ethereum.facade.Ethereum;
6+
import org.ethereum.listener.EthereumListenerAdapter;
7+
import org.ethereum.util.BIUtil;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.math.BigInteger;
12+
import java.util.List;
13+
14+
public class EthListener extends EthereumListenerAdapter {
15+
16+
private Logger l = LoggerFactory.getLogger(EthListener.class);
17+
private Ethereum ethereum;
18+
private boolean syncDone = false;
19+
private static final int thou = 1000;
20+
21+
private void out(String t) {
22+
l.info(t);
23+
}
24+
25+
private String calcNetHashRate(Block block) {
26+
String response = "Net hash rate not available";
27+
if (block.getNumber() > thou) {
28+
long timeDelta = 0;
29+
for (int i = 0; i < thou; ++i) {
30+
Block parent = ethereum
31+
.getBlockchain()
32+
.getBlockByHash(block.getParentHash());
33+
timeDelta += Math.abs(block.getTimestamp() - parent.getTimestamp());
34+
}
35+
response = String.valueOf(block
36+
.getDifficultyBI()
37+
.divide(BIUtil.toBI(timeDelta / thou))
38+
.divide(new BigInteger("1000000000"))
39+
.doubleValue()) + " GH/s";
40+
}
41+
return response;
42+
}
43+
44+
public EthListener(Ethereum ethereum) {
45+
this.ethereum = ethereum;
46+
}
47+
48+
@Override
49+
public void onBlock(Block block, List<TransactionReceipt> receipts) {
50+
if (syncDone) {
51+
out("Net hash rate: " + calcNetHashRate(block));
52+
out("Block difficulty: " + block.getDifficultyBI().toString());
53+
out("Block transactions: " + block.getTransactionsList().toString());
54+
out("Best block (last block): " + ethereum
55+
.getBlockchain()
56+
.getBestBlock().toString());
57+
out("Total difficulty: " + ethereum
58+
.getBlockchain()
59+
.getTotalDifficulty().toString());
60+
}
61+
}
62+
63+
@Override
64+
public void onSyncDone(SyncState state) {
65+
out("onSyncDone " + state);
66+
if (!syncDone) {
67+
out(" ** SYNC DONE ** ");
68+
syncDone = true;
69+
}
70+
}
71+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.ethereumj.transfer;
2+
3+
public class EthResponse {
4+
5+
private String response;
6+
7+
public String getResponse() {
8+
return response;
9+
}
10+
11+
public void setResponse(String response) {
12+
this.response = response;
13+
}
14+
}

0 commit comments

Comments
 (0)