-
Notifications
You must be signed in to change notification settings - Fork 440
extend explorer api with block.ID() and optional hexblock field #2418
base: master
Are you sure you want to change the base?
Changes from 5 commits
d8e7ae0
7b6e39c
8384a40
0b81134
88a7a5c
954c25b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"net/http" | ||
|
||
"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" | ||
|
||
|
@@ -20,6 +22,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 | ||
} | ||
|
@@ -171,8 +175,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. hexBlockEnable is a required flag and indicates if the | ||
// hexBlock 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, hexBlockEnable bool) ExplorerBlock { | ||
var mpoids []types.SiacoinOutputID | ||
for i := range block.MinerPayouts { | ||
mpoids = append(mpoids, block.MinerPayoutID(uint64(i))) | ||
|
@@ -188,13 +194,18 @@ func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block) | |
panic("incorrect request to buildExplorerBlock - block does not exist") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion we shouldn't crash if a user supplies an invalid height but return an error we write to the http response. What do you think @lukechampine @DavidVorick |
||
} | ||
|
||
return ExplorerBlock{ | ||
b := ExplorerBlock{ | ||
MinerPayoutIDs: mpoids, | ||
Transactions: etxns, | ||
RawBlock: block, | ||
BlockID: hex.EncodeToString(encoding.Marshal(block.ID())), | ||
|
||
BlockFacts: facts, | ||
} | ||
if hexBlockEnable { | ||
b.HexBlock = hex.EncodeToString(encoding.Marshal(block)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you implement something like the following in
That way you can simply do |
||
} | ||
return b | ||
} | ||
|
||
// explorerHandler handles API calls to /explorer/blocks/:height. | ||
|
@@ -214,7 +225,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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I didn't comment on this in the first pass. Wasn't sure what we wanted here, but after discussing with Luke, we agree that the call should return an error if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense to me and I will do it. I copied this pattern from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you give me an example of the new pattern you and luke want in the pre-existing source code please? I'm looking through all the boolean flags in the system and not finding one "good" one according to your description. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as per our discord discussion, we'll punt this to a follow-up PR |
||
}) | ||
} | ||
|
||
|
@@ -231,7 +243,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 { | ||
|
@@ -271,7 +283,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"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
}) | ||
return | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, changing to
BlockID
actually caused tests to start failing, becausemodules.BlockFacts
has an embeddedBlockID
field.Is it necessary to have a
BlockID string
field if we already have it as a[32]byte
? If so, maybe call itHexBlockID
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lukechampine when i remove the field i get:
therefore i renamed it to HexBlockID however i left the json named the standard way because to me it seems cleaner. please feel free to adjust again if you think there is a cleaner way to handle this.