This is a block from the TSS project which consists in:
-
REST API: (Spring, JPA and Web3j)
- Login
- User Interface
- Blockchain / IoT
-
DATABASE: (mySQL)
- Users
- User Policies
- User Smart Policies
- User Smart Policy Alerts
- User Smart Policy Sensors
To install MySQL follow this.
Additional info for installation:
- It is not necessary to get everything, there may be some features that could not be possible to install depending on which software you already have (like MySQL for visual studio code)
- You may have to configure the server after the installation (not like the guide), just open the MySQL Installer again and click reconfigure in the server row. Now you can go and follow the guide.
- The root user & password are important for accessing to the command line client later for example, but it does not have to be related to the project.
Using the MySQL Command Line Client (once accessed with your root pass):
create database db_orchestrator;
create user 'tss'@'%' identified by 'tss1234';
grant all on db_orchestrator.* to 'tss'@'%';
- Creates the new database called "db_orchestrator".
- Creates the user "tss" with "tss1234" as its password.
- Gives all privileges to the new user on the newly created database.
All names and pass are customizable but please, make sure you change it in properties file.
In the src/main/resources/application.properties file:
spring.datasource.url = jdbc:mysql://localhost:3306/db_orchestrator
spring.datasource.username = tss
spring.datasource.password = tss1234
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
To recreate the database use:
drop database db_orchestrator;
create database db_orchestrator;
grant all on db_orchestrator.* to 'tss'@'%';
We will be using the Truffle Suite environment:
For the blockchain simulation install Ganache.
We are using this configuration:
GAS PRICE: 200000000
GAS LIMIT: 6721975
HOSTNAME: 127.0.0.1
PORT NUMBER: 7545
Once has Ganache started:
-
Install truffle:
$ npm install truffle -g
-
Deploy the token, go to /TSS-orchestrator/truffle/ directory and use:
$ truffle migrate
This step is not necessary if you just want to use our implementation. Otherwise, if you are willing to use a different smart contract:
-
Install web3j CLI:
$ curl -L get.web3j.io | sh && source ~/.web3j/source.sh
-
Create a truffle project or use the already created in /TSS-orchestrator/truffle/.
$ truffle init
-
Put your contract.sol in the /TSS-orchestrator/truffle/contracts/ and compile (you should be at the truffle project directory):
$ truffle compile
-
A yourContract.json should have been generated in the /TSS-orchestrator/truffle/build/contracts/, so now convert it into a Wrapper (.java)
$ web3j generate truffle -t path/to/yourContract.json -o path/to/src/main/java -p tss.orchestrator.models.contracts
-
Finally, modify the methods at the BlockChainServiceImpl class to use your contract.
You will have to create by default the users, this is made automatically adding in the src/main/resources/data.sql:
insert into user values (1,'{brokerAddress}','{clientAddress}','{insuranceAddress}', 'user1', '1234', '{privateKey}');
Change the {accountAddress} and {privateKey} to your Ganache's first account address and private key.
All the addresses are the same as {accountAddress}.
Change the smart contract token address in the src/main/java/tss/orchestrator/utils/constants/Constants.java
public static final String TOKEN_ADDRESS = "{tokenAddress}";
You can get the address from the ganache's second block mined.
Uncomment the last line in the src/main/resources/application.properties:
spring.datasource.initialization-mode=always
Comment this line again after the first execution.
To run the project use this command in your coding environment terminal:
mvnw spring-boot:run
The requests managers.
-
Interface:
@RequestMapping("/examplepath") public interface ExampleRestControllerApi { @GetMapping("/get") public List<E> getExample(); @PostMapping("/post") public ResponseEntity<Object> postExample(@RequestBody ModelDTO modelDTO); }
-
Class:
@RestController public class ExampleRestController implements ExampleRestControllerApi { @Override public List<E> getExample(){ //... } @Override public ResponseEntity<Object> postExample(@RequestBody ModelDTO modelDTO){ //... } }
-
Useful Code:
-
Get info from a request path /path/{id}
@PathVariable int id
-
Get info from a request parameter (int) key1=value1 & (string) key2=value2
@RequestParam int key1, @RequestParam String key2
-
Map json to an object from the body of a request
@RequestBody ModelDTO modelDTO
There are a lot of requests types but here we will mainly use get and post.
-
The data.
-
Data Transfer Object:
public class ModelDTO { //variables //setters & getters }
Used for direct parsing from json to Java operative class
-
Model:
public class Model { //variables public Model(){ } public Model (ModelDTO modelDTO){ super(); this.variable = modelDTO.getVariable(); } //setters & getters }
From the conversion of DTO
-
Model:
@Entity public class Model { @Id @GeneratedValue private Integer id; //variables public Model(){ } public Model (ModelDTO modelDTO){ super(); this.variable = modelDTO.getVariable(); } //setters & getters }
Attached to a database table.
The internal logic.
-
Table repository:
@Repository public interface ModelRepository extends JpaRepository<Model, Integer> { }
It will be autocompleted.
For the communication with the blockchain.
The path to send the sensor's data is /blockchain/api/sensors and the format of the json is:
{
"userId": "{userId}",
"smartPolicyId": "{smartPolicyId}",
"sensorData": {
"{sensorType}": "{sensorValue}",
"{sensorType}": "{sensorValue}",
...
},
"dataTimeStamp": "{dataTimeStamp}"
}
The sensorType should be in string type (Ex. temperature).
The dataTimeStamp should be epoch format in seconds.