Class BlockchainRestController
Demonstrates the blockchain-spring autoconfiguration pattern (AC-10):
the BlockchainNode is injected as a Spring bean with no boilerplate — it
is created and started by BlockchainAutoConfiguration purely from
application.yml properties.
Endpoints
| Method | Path | Description | Status |
|---|---|---|---|
| GET | /api/status | Node status snapshot | 200 |
| GET | /api/chain | All blocks as a JSON array | 200 |
| GET | /api/chain/{index} | Single block by index | 200 / 404 |
| POST | /api/transactions | Submit and sign a payment | 202 / 422 |
| POST | /api/mine | Mine a block from the mempool | 200 / 204 |
Thread safety
All fields are final and injected via constructor. BlockchainNode
is thread-safe internally (design.md §7.4). Spring MVC's default servlet thread pool
may invoke these endpoints concurrently without additional synchronization.
- Since:
- 1.0.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classSimple payment transaction used by the REST demo.static final recordImmutable request body forsubmitTransaction(com.privatechain.examples.spring.BlockchainRestController.TransactionRequest). -
Constructor Summary
ConstructorsConstructorDescriptionBlockchainRestController(BlockchainNode node, Wallet demoWallet) Constructs the controller via Spring constructor injection. -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<Block> getBlock(int index) Returns a single block by its zero-based index.getChain()Returns the full blockchain as an ordered JSON array.org.springframework.http.ResponseEntity<BlockchainNode.NodeStatus> Returns a live snapshot of the node's operational status.Triggers a manual block mining cycle, selecting up to 100 transactions from the mempool and appending the resulting block to the chain.Accepts a payment transaction request, signs it with the demo wallet, and submits it to the mempool.
-
Constructor Details
-
BlockchainRestController
Constructs the controller via Spring constructor injection.- Parameters:
node- the autoconfigured blockchain node (non-null)demoWallet- the demo signing wallet (non-null)- Throws:
NullPointerException- if either argument is null
-
-
Method Details
-
getStatus
@GetMapping("/status") public org.springframework.http.ResponseEntity<BlockchainNode.NodeStatus> getStatus()Returns a live snapshot of the node's operational status.Sample response:
{ "chainHeight": 3, "mempoolSize": 0, "peerCount": 0, "lastBlockTime": "2026-06-06T10:00:00Z", "consensusEngine": "NoOp" }- Returns:
- HTTP 200 with the
BlockchainNode.NodeStatusrecord as JSON
-
getChain
Returns the full blockchain as an ordered JSON array.Each element is a
Blockserialized with its complete header, transaction list, and hash fields. For large chains, consider adding pagination viagetBlock(int)instead.- Returns:
- HTTP 200 with a JSON array of all blocks in ascending index order
-
getBlock
@GetMapping("/chain/{index}") public org.springframework.http.ResponseEntity<Block> getBlock(@PathVariable int index) Returns a single block by its zero-based index.- Parameters:
index- the block index (≥ 0)- Returns:
- HTTP 200 with the block, or HTTP 404 if no block exists at that index
-
submitTransaction
@PostMapping("/transactions") public org.springframework.http.ResponseEntity<Map<String,Object>> submitTransaction(@RequestBody BlockchainRestController.TransactionRequest request) Accepts a payment transaction request, signs it with the demo wallet, and submits it to the mempool.Request body:
{ "senderAddress": "1AbcDef...", "receiverAddress": "1XyzWvu...", "amount": 42.00, "currency": "USD" }Response (202 ACCEPTED):
{ "txId": "3f4a6b2c-...", "status": "PENDING", "mempoolSize": 1 }- Parameters:
request- the payment request body (non-null)- Returns:
- HTTP 202 ACCEPTED with the transaction ID on success, HTTP 422 UNPROCESSABLE_CONTENT if validation fails
-
mineBlock
@PostMapping("/mine") public org.springframework.http.ResponseEntity<Map<String,Object>> mineBlock()Triggers a manual block mining cycle, selecting up to 100 transactions from the mempool and appending the resulting block to the chain.Returns HTTP 204 NO_CONTENT if the mempool is empty (no block produced). Returns HTTP 200 with block metadata on success.
This endpoint exists for demo convenience — in a real node, mining runs in a background thread or is triggered by a dedicated mining service.
- Returns:
- HTTP 200 with block index, hash, and tx count on success; HTTP 204 if the mempool is empty; HTTP 500 if consensus or validation fails
-