Skip to content

Commit

Permalink
Merge pull request #55 from hunteryavitz/54
Browse files Browse the repository at this point in the history
stubbed out endpoint and service
  • Loading branch information
hunteryavitz authored Nov 8, 2023
2 parents a66d288 + 8bc8083 commit 1f567cb
Show file tree
Hide file tree
Showing 65 changed files with 1,201 additions and 259 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Blockchain API
Unreleased

*v0.0.15*
*v0.0.17*

**11/7/23**

- [ADD Nodes Health Check Service and Endpoint](#54)

**10/31/23**

Expand Down
52 changes: 34 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Blockchain API
**Last Updated:** 10/31/23
**Last Updated:** 11/2/23

This is the API and backend code for the blockchain project.

Expand Down Expand Up @@ -29,44 +29,60 @@ mvn javadoc:javadoc
## Endpoints
The current serviceable endpoints are as follows:

### Verify Blockchain
###
GET http://localhost:8080/api/v1/blockchain/verifyBlockchain

### Get Blockchain
###
GET http://localhost:8080/api/v1/blockchain/getBlockchain

### Get Block By Id
###
GET http://localhost:8080/api/v1/blockchain/getBlockById

### Add Block to Blockchain
###
POST http://localhost:8080/api/v1/block/addBlockToBlockchain
Content-Type: application/x-www-form-urlencoded

### Update Production
GET http://localhost:8080/api/v1/healthMetric/updateProduction
###
GET http://localhost:8080/api/v1/healthMetric/updateProductionHealth

### Get Production
###
GET http://localhost:8080/api/v1/healthMetric/getProductionHealth

### Get Health
GET http://localhost:8080/api/v1/healthMetric/health
###
GET http://localhost:8080/api/v1/healthMetric/getExceptionHealth

### Get Readiness
###
GET http://localhost:8080/api/v1/readiness

### Get Liveness
###
GET http://localhost:8080/api/v1/liveness

### Get Version
###
GET http://localhost:8080/api/v1/version

### Verify Blockchain
GET http://localhost:8080/api/v1/verifyBlockchain
###
GET http://localhost:8080/api/v1/node/getNodeStatus

### Submit Transaction
###
POST http://localhost:8080/api/v1/nodeManager/registerNode
Content-Type: application/json

{}

###
GET http://localhost:8080/api/v1/nodeManager/getNodeNetworkStatus

###
GET http://localhost:8080/api/v1/nodeManager/nodeNetworkRollCall

###
POST http://localhost:8080/api/v1/transaction/submitTransaction
Content-Type: application/json

{}

### Get Transaction Pool
GET /api/v1/transaction/getTransactionPool
###
GET http://localhost:8080/api/v1/transaction/getTransactionPool

---

Expand Down
11 changes: 0 additions & 11 deletions SCRATCH.md
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
### Get Production Payload

```json

{
"data": {
"dataset1": [5, 13, 23, 24, 12, 8, 5, 0, 0, 0, 0, 0],
"dataset2": [1, 1, 3, 2, 1, 1, 1, 0, 0, 0, 0, 0 ]
}
}
```
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.16
0.0.17
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.hunteryavitz.blockchainapi.constants;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;

/**
* Constants for the application.
*/
public class AppConstants {

/**
* The node manager registry address.
*/
@Value("${REGISTRY_ADDRESS}")
@Getter
private static String registryAddress;

/**
* The node certificate.
*/
@Value("${CERTIFICATE}")
@Getter
private static String certificate;

/**
* The node port.
*/
@Value("${PORT}")
@Getter
private static int port;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.hunteryavitz.blockchainapi.constants;

/**
* The NodeStatus enum represents the status of a node.
*/
public enum NodeStatus {

/**
* The node is inactive.
*/
INACTIVE,

/**
* The node is active.
*/
ACTIVE,

/**
* The node is unresponsive.
*/
UNRESPONSIVE,

/**
* The node is unregistered.
*/
UNREGISTERED,

/**
* The node failed registration.
*/
FAILED_REGISTRATION
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
/**
* The Status enum represents the status of an order.
*/
public enum Status {
public enum TransactionStatus {

/**
* The order has been created.
*/
CREATED,

/**
* The order is pending.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.hunteryavitz.blockchainapi.constants.ContaminationLevel;
import com.hunteryavitz.blockchainapi.services.BlockchainService;
import com.hunteryavitz.blockchainapi.services.HealthMetricService;
import jakarta.websocket.server.PathParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -28,39 +29,41 @@ public class BlockController {
private final HealthMetricService healthMetricService;

/**
* The BlockController constructor is responsible for initializing the
* The BlockController constructor is responsible for initializing the controller.
* @param blockchainService The blockchain service.
* @param healthMetricService The health metric service.
*/
public BlockController(BlockchainService blockchainService, HealthMetricService healthMetricService) {
this.blockchainService = blockchainService;
this.healthMetricService = healthMetricService;

try {
if (blockchainService.getBlockchain() == null) {
blockchainService.createInitialBlockchain();
}
if (healthMetricService.getProduction() == null) {
healthMetricService.createHealthMetricService();
}
} catch (Exception exception) {
assert healthMetricService != null;
healthMetricService.updateHealth(ContaminationLevel.CRITICAL, exception);
}
}

/**
* The addBlockToBlockchain method is responsible for adding a block to the blockchain.
* @param test The test query parameter.
* @return A ResponseEntity containing a boolean indicating whether the block was added to the blockchain.
*/
@PostMapping("/addBlockToBlockchain")
public ResponseEntity<Boolean> addBlockToBlockchain() {
public ResponseEntity<Boolean> addBlockToBlockchain(@PathParam("test") boolean test) {
try {
blockchainService.addBlockToBlockchain();
if (test) {
throw new Exception("Test exception");
}

return ResponseEntity.ok(true);
} catch (Exception exception) {
healthMetricService.updateHealth(ContaminationLevel.WARNING, exception);
return ResponseEntity.ok(false);
}

return ResponseEntity.ok(true);
return ResponseEntity.ok(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.hunteryavitz.blockchainapi.entities.Block;
import com.hunteryavitz.blockchainapi.services.BlockchainService;
import com.hunteryavitz.blockchainapi.services.HealthMetricService;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.websocket.server.PathParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -57,37 +57,50 @@ public ResponseEntity<Boolean> verifyBlockchain() {
} catch (Exception exception) {
healthMetricService.updateHealth(ContaminationLevel.CRITICAL, exception);
}

return ResponseEntity.ok(false);
}

/**
* The getBlockchain method is the endpoint for getting the blockchain.
* @param test The test query parameter.
* @return A ResponseEntity containing the blockchain.
*/
@GetMapping("/getBlockchain")
public ResponseEntity<Block[]> getBlockchain() {
public ResponseEntity<Block[]> getBlockchain(@PathParam("test") boolean test) {
try {
Block[] blockchain = blockchainService.getBlockchain();
if (test) {
throw new Exception("Empty blockchain");
}

return ResponseEntity.ok(blockchain);
} catch (Exception exception) {
healthMetricService.updateHealth(ContaminationLevel.WARNING, exception);
}
return ResponseEntity.ok(null);

return ResponseEntity.ok(new Block[0]);
}

/**
* The getBlockById method is the endpoint for getting a block by its id.
* @param id The id of the block.
* @param test The test query parameter.
* @return A ResponseEntity containing the block.
*/
@GetMapping("/getBlockById")
public ResponseEntity<Block> getBlockById(@RequestParam int id) {
public ResponseEntity<Block> getBlockById(@RequestParam int id, @PathParam("test") boolean test) {
try {
Block block = blockchainService.getBlockById(id);
if (test) {
throw new Exception("Empty block");
}

return ResponseEntity.ok(block);
} catch (Exception exception) {
healthMetricService.updateHealth(ContaminationLevel.WARNING, exception);
}
return ResponseEntity.ok(null);

return ResponseEntity.ok(new Block());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.hunteryavitz.blockchainapi.constants.ContaminationLevel;
import com.hunteryavitz.blockchainapi.services.HealthMetricService;
import com.hunteryavitz.blockchainapi.utils.structures.SlidingWindow;
import jakarta.websocket.server.PathParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -28,25 +29,21 @@ public class HealthMetricController {
*/
public HealthMetricController(HealthMetricService healthMetricService) {
this.healthMetricService = healthMetricService;

try {
if (healthMetricService.getProduction() == null) {
healthMetricService.createHealthMetricService();
}
} catch (Exception exception) {
assert healthMetricService != null;
healthMetricService.updateHealth(ContaminationLevel.CRITICAL, exception);
}
}

/**
* Updates the health metric.
* @param test The test query parameter.
* @return A ResponseEntity containing a boolean indicating whether the health metric was updated.
*/
@GetMapping("/updateProduction")
public ResponseEntity<Boolean> updateProduction() {
@GetMapping("/updateProductionHealth")
public ResponseEntity<Boolean> updateProductionHealth(@PathParam("test") boolean test) {
try {
healthMetricService.updateBlockchainProduction();
if (test) {
throw new Exception("Test exception");
}

return ResponseEntity.ok(true);
} catch (Exception exception) {
healthMetricService.updateHealth(ContaminationLevel.INFO, exception);
Expand All @@ -57,12 +54,17 @@ public ResponseEntity<Boolean> updateProduction() {

/**
* Gets the production health.
* @param test The test query parameter.
* @return The production health.
*/
@GetMapping("/getProductionHealth")
public ResponseEntity<String> getProductionHealth() {
public ResponseEntity<String> getProductionHealth(@PathParam("test") boolean test) {
try {
SlidingWindow slidingWindow = healthMetricService.getProduction();
if (test) {
throw new Exception("Test exception");
}

return ResponseEntity.ok(slidingWindow.asJson());
} catch (Exception exception) {
healthMetricService.updateHealth(ContaminationLevel.INFO, exception);
Expand All @@ -73,11 +75,16 @@ public ResponseEntity<String> getProductionHealth() {

/**
* Gets the production health.
* @param test The test query parameter.
* @return The production health.
*/
@GetMapping("/health")
public ResponseEntity<Integer[]> getHealth() {
@GetMapping("/getExceptionHealth")
public ResponseEntity<Integer[]> getExceptionHealth(@PathParam("test") boolean test) {
try {
if (test) {
throw new Exception("Test exception");
}

return ResponseEntity.ok(HealthMetricService.getHealth());
} catch (Exception exception) {
healthMetricService.updateHealth(ContaminationLevel.INFO, exception);
Expand Down
Loading

0 comments on commit 1f567cb

Please sign in to comment.