Skip to content

Commit

Permalink
Merge pull request #1 from AustEcon/features/api-for-headers-by-height
Browse files Browse the repository at this point in the history
  • Loading branch information
Jad Wahab authored Dec 9, 2021
2 parents d00e37f + 32938d3 commit 3ed8002
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ Retrieves the header with the given hash
```
The content type: ```application/octet-stream``` can be provided in the requst header to return the headers raw bytes.

#### Query Headers By Height (batched)
Retrieves the header with the given hash
```
/api/v1/chain/header/byHeight?height=<start_height>&count=<count>
```
The Accept type: ```application/octet-stream``` can be provided in the requst header to return the headers as a
stream of raw bytes.

The query parameter: height is required. The count of headers requested is optional (default = 1)

#### Query Headers State
Retrieves the header with the given hash along with it's state relative to the chain
```
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/io/bitcoinsv/headerSV/api/HSVFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ public BlockHeaderDTO getBlockHeader(String hash){
return BlockHeaderDTO.of(blockHeader.get());
}

public List<BlockHeaderDTO> getHeadersByHeight(Integer height, Integer count){
List<BlockHeaderDTO> listBlockHeaders = new ArrayList<>();
ChainInfo longestChainInfo = blockChainStore.getLongestChain().get();
HeaderReadOnly chainTip = longestChainInfo.getHeader();

// validation of inputs
int lastHeaderHeight = height + count - 1;
if (height > longestChainInfo.getHeight()) {
throw new IllegalArgumentException(String.format("Header at height %s exceeds the chain tip: %s", height, longestChainInfo.getHeight()));
} else if (lastHeaderHeight > longestChainInfo.getHeight()) {
lastHeaderHeight = longestChainInfo.getHeight();
}

for(int curHeight = height; curHeight<=lastHeaderHeight; curHeight++)
{
Optional<ChainInfo> header = blockChainStore.getAncestorByHeight(chainTip.getHash(), curHeight);
if (header.isEmpty()) {
throw new IllegalStateException(String.format("Header at height %s not found", curHeight));
}
listBlockHeaders.add(BlockHeaderDTO.of(header.get().getHeader()));
}
return listBlockHeaders;
}

public List<BlockHeaderDTO> getAncestors(String hash, String ancestorHash){
Optional<ChainInfo> requestedBlock = blockChainStore.getBlockChainInfo(Sha256Hash.wrap(hash));
Optional<ChainInfo> ancestorBlock = blockChainStore.getBlockChainInfo(Sha256Hash.wrap(ancestorHash));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.bitcoinsv.headerSV.domain.dto.BlockHeaderDTO;
import io.bitcoinsv.headerSV.domain.dto.ChainStateDTO;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
Expand Down Expand Up @@ -63,6 +65,31 @@ public ResponseEntity<?> getAncestors(@PathVariable String hash, @RequestBody St
return new ResponseEntity<>(headerHistory, HttpStatus.OK);
}

@RequestMapping("/byHeight")
public ResponseEntity<?> getHeadersByHeight(@RequestParam String height, @RequestParam(defaultValue = "1") String count,
@RequestHeader(value = "Accept", required = false, defaultValue = "application/json") MediaType acceptContentType){
try {
if (Integer.parseInt(count) > 2000) {
throw new IllegalArgumentException("Count exceeds max value of 2000 headers");
}

List<BlockHeaderDTO> headers = hsvFacade.getHeadersByHeight(Integer.parseInt(height), Integer.parseInt(count));
if (acceptContentType.toString().equals(MediaType.APPLICATION_OCTET_STREAM_VALUE)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for (BlockHeaderDTO header : headers) {
baos.write(header.getHeaderReadOnly().serialize());
}
return new ResponseEntity<>(baos.toByteArray(), HttpStatus.OK);
} else {
// MediaType.APPLICATION_JSON_VALUE
return new ResponseEntity<>(headers, HttpStatus.OK);
}
}
catch (IllegalArgumentException | IllegalStateException | IOException exception) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, exception.getMessage());
}
}

@RequestMapping("/commonAncestor")
public ResponseEntity<?> getCommonAncestor(@RequestBody List<String> blockHashes){
BlockHeaderDTO headerHistory = hsvFacade.findCommonAncestor(blockHashes);
Expand Down

0 comments on commit 3ed8002

Please sign in to comment.