Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
extend explorer api with block.ID() and optional hexblock field
Browse files Browse the repository at this point in the history
  • Loading branch information
petabytestorage committed Oct 12, 2017
1 parent cce1680 commit 0bcd834
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions api/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/crypto"
"github.com/NebulousLabs/Sia/encoding"
"github.com/NebulousLabs/Sia/modules"
"github.com/NebulousLabs/Sia/types"

Expand All @@ -20,6 +21,8 @@ type (
MinerPayoutIDs []types.SiacoinOutputID `json:"minerpayoutids"`
Transactions []ExplorerTransaction `json:"transactions"`
RawBlock types.Block `json:"rawblock"`
HexBlock string `json:"hexblock"`
BlockId string `json:"blockid"`

modules.BlockFacts
}
Expand Down Expand Up @@ -171,8 +174,10 @@ func (api *API) buildExplorerTransaction(height types.BlockHeight, parent types.
}

// buildExplorerBlock takes a block and its height and uses it to construct an
// explorer block.
func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block) ExplorerBlock {
// explorer block. hex_block_enable is a required flag and indicates if the
// hex_block_field should be empty string (false) or
// should contain the hexadecimal binary encoded block (true).
func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block, hex_block_enable bool) ExplorerBlock {
var mpoids []types.SiacoinOutputID
for i := range block.MinerPayouts {
mpoids = append(mpoids, block.MinerPayoutID(uint64(i)))
Expand All @@ -188,10 +193,17 @@ func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block)
panic("incorrect request to buildExplorerBlock - block does not exist")
}

hex_block := ""
if hex_block_enable {
hex_block = fmt.Sprintf("%x", encoding.Marshal(block)[:])
}

return ExplorerBlock{
MinerPayoutIDs: mpoids,
Transactions: etxns,
RawBlock: block,
HexBlock: hex_block,
BlockId: fmt.Sprintf("%x", encoding.Marshal(block.ID())[:]),

BlockFacts: facts,
}
Expand All @@ -214,7 +226,8 @@ func (api *API) explorerBlocksHandler(w http.ResponseWriter, req *http.Request,
return
}
WriteJSON(w, ExplorerBlockGET{
Block: api.buildExplorerBlock(height, block),
Block: api.buildExplorerBlock(height, block,
req.FormValue("hexblock") == "true"),
})
}

Expand All @@ -231,7 +244,7 @@ func (api *API) buildTransactionSet(txids []types.TransactionID) (txns []Explore

// Check if the block is the transaction.
if types.TransactionID(block.ID()) == txid {
blocks = append(blocks, api.buildExplorerBlock(height, block))
blocks = append(blocks, api.buildExplorerBlock(height, block, false))
} else {
// Find the transaction within the block with the correct id.
for _, t := range block.Transactions {
Expand Down Expand Up @@ -271,7 +284,8 @@ func (api *API) explorerHashHandler(w http.ResponseWriter, req *http.Request, ps
if exists {
WriteJSON(w, ExplorerHashGET{
HashType: "blockid",
Block: api.buildExplorerBlock(height, block),
Block: api.buildExplorerBlock(height, block,
req.FormValue("hexblock") == "true"),
})
return
}
Expand Down

0 comments on commit 0bcd834

Please sign in to comment.