Skip to content
Permalink
Browse files

api: Add block related endpoints

Create the `/block/recent` and `/block/height` API endpoints.
  • Loading branch information
00-matt committed Feb 7, 2020
1 parent 750697e commit 9a04b6805c496d2ac116fb160da3a843f216eb57
@@ -0,0 +1,35 @@
package uk.offtopica.moneropool.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import uk.offtopica.moneropool.api.exception.BlockNotFoundException;
import uk.offtopica.moneropool.api.model.Block;
import uk.offtopica.moneropool.api.service.BlockService;

import java.util.List;

@RestController
@RequestMapping("/block")
public class BlockController {
@Autowired
private BlockService blockService;

@GetMapping("/recent")
public List<Block> getRecent() {
return blockService.findAllRecent(0).getContent();
}

@GetMapping("/{height}")
public Block getBlockByHeight(@PathVariable Integer height) {
try {
return blockService.findByHeight(height);
} catch (BlockNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Block not found", e);
}
}
}
@@ -1,5 +1,6 @@
package uk.offtopica.moneropool.api.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import javax.persistence.*;
@@ -24,11 +25,13 @@
private Boolean orphaned;

@Column(name = "expected_reward", nullable = false, updatable = false)
@JsonProperty("expected_reward")
private Long expectedReward;

@Column(name = "difficulty", nullable = false, updatable = false)
private Long difficulty;

@Column(name = "created_at", nullable = false, updatable = false)
@JsonProperty("created_at")
private LocalDateTime createdAt;
}
@@ -1,6 +1,9 @@
package uk.offtopica.moneropool.api.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import uk.offtopica.moneropool.api.exception.BlockNotFoundException;
import uk.offtopica.moneropool.api.model.Block;
@@ -11,6 +14,11 @@
@Autowired
private BlockRepository blockRepository;

public Page<Block> findAllRecent(Integer page) {
// TODO: Make count configurable?
return blockRepository.findAll(PageRequest.of(page, 100, Sort.by("id").descending()));
}

public Block findByHeight(Integer height) throws BlockNotFoundException {
return blockRepository.findByHeight(height).orElseThrow(BlockNotFoundException::new);
}
@@ -6,6 +6,53 @@ to the API server should be passed through a reverse proxy and cached.
This API is not yet stable and may change in backwords-incompatible
ways without warning in the future.

## `GET /block/recent`

Get the most recent blocks.

Example response:

[
{
"id": 13,
"height": 512182,
"paid": false,
"orphaned": false,
"difficulty": 183396,
"expected_reward": 13668202361319,
"created_at": "2020-02-07T15:20:42.211864"
},
{
"id": 12,
"height": 512181,
"paid": false,
"orphaned": false,
"difficulty": 183582,
"expected_reward": 13668228431396,
"created_at": "2020-02-07T15:17:26.245702"
},
...
]

Note that the expected reward is in atomic units.
1e12 atomic units represent 1 monero.

## `GET /block/{height}`

Get a specific block by its height.

Example response:

{
"id": 13,
"height": 512182,
"paid": false,
"orphaned": false,
"difficulty": 183396,
"expected_reward": 13668202361319,
"created_at": "2020-02-07T15:20:42.211864"
}

## `GET /stats/pool`

Get global pool statistics.

0 comments on commit 9a04b68

Please sign in to comment.
You can’t perform that action at this time.