From d8e7ae0025fdd48057e85c4e11ba677f3c7e85c4 Mon Sep 17 00:00:00 2001 From: Petabyte Storage Date: Thu, 12 Oct 2017 09:50:49 -0700 Subject: [PATCH 1/6] extend explorer api with block.ID() and optional hexblock field --- api/explorer.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/api/explorer.go b/api/explorer.go index d31cf2f961..6f1611db03 100644 --- a/api/explorer.go +++ b/api/explorer.go @@ -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" @@ -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 } @@ -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))) @@ -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, } @@ -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"), }) } @@ -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 { @@ -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 } From 7b6e39c2827e37e73791f71f2d784b621928f63e Mon Sep 17 00:00:00 2001 From: Petabyte Storage Date: Fri, 13 Oct 2017 14:42:20 -0700 Subject: [PATCH 2/6] fix variable names to camelCase --- api/explorer.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api/explorer.go b/api/explorer.go index 6f1611db03..ea5e6914e1 100644 --- a/api/explorer.go +++ b/api/explorer.go @@ -174,10 +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. hex_block_enable is a required flag and indicates if the -// hex_block field should be empty string (false) or +// 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, hex_block_enable bool) ExplorerBlock { +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))) @@ -193,16 +193,16 @@ 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)[:]) + hexBlock := "" + if hexBlockEnable { + hexBlock = fmt.Sprintf("%x", encoding.Marshal(block)[:]) } return ExplorerBlock{ MinerPayoutIDs: mpoids, Transactions: etxns, RawBlock: block, - HexBlock: hex_block, + HexBlock: hexBlock, BlockId: fmt.Sprintf("%x", encoding.Marshal(block.ID())[:]), BlockFacts: facts, From 8384a40c460e3fcbac05a65ddd453ff35fd540ef Mon Sep 17 00:00:00 2001 From: Petabyte Storage Date: Fri, 13 Oct 2017 15:26:49 -0700 Subject: [PATCH 3/6] switch to 'var hexBlock string' convention --- api/explorer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/explorer.go b/api/explorer.go index ea5e6914e1..566a3cdc3b 100644 --- a/api/explorer.go +++ b/api/explorer.go @@ -193,7 +193,7 @@ func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block, panic("incorrect request to buildExplorerBlock - block does not exist") } - hexBlock := "" + var hexBlock string if hexBlockEnable { hexBlock = fmt.Sprintf("%x", encoding.Marshal(block)[:]) } From 0b81134071bb7d8d2de1a546e1829d01137de3ec Mon Sep 17 00:00:00 2001 From: Petabyte Storage Date: Sun, 15 Oct 2017 09:02:07 -0700 Subject: [PATCH 4/6] do luke comments --- api/explorer.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/api/explorer.go b/api/explorer.go index 566a3cdc3b..4066b5237d 100644 --- a/api/explorer.go +++ b/api/explorer.go @@ -1,6 +1,7 @@ package api import ( + "encoding/hex" "fmt" "net/http" @@ -195,7 +196,7 @@ func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block, var hexBlock string if hexBlockEnable { - hexBlock = fmt.Sprintf("%x", encoding.Marshal(block)[:]) + hexBlock = hex.EncodeToString(encoding.Marshal(block)) } return ExplorerBlock{ @@ -203,7 +204,7 @@ func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block, Transactions: etxns, RawBlock: block, HexBlock: hexBlock, - BlockId: fmt.Sprintf("%x", encoding.Marshal(block.ID())[:]), + BlockId: hex.EncodeToString(encoding.Marshal(block.ID())), BlockFacts: facts, } From 88a7a5cb86c7b71a75aa01dbbce015becf85d5ed Mon Sep 17 00:00:00 2001 From: Petabyte Storage Date: Sun, 15 Oct 2017 13:55:09 -0700 Subject: [PATCH 5/6] get rid of extra variable --- api/explorer.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/api/explorer.go b/api/explorer.go index 4066b5237d..617d0ba309 100644 --- a/api/explorer.go +++ b/api/explorer.go @@ -23,7 +23,7 @@ type ( Transactions []ExplorerTransaction `json:"transactions"` RawBlock types.Block `json:"rawblock"` HexBlock string `json:"hexblock"` - BlockId string `json:"blockid"` + BlockID string `json:"blockid"` modules.BlockFacts } @@ -194,20 +194,18 @@ func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block, panic("incorrect request to buildExplorerBlock - block does not exist") } - var hexBlock string - if hexBlockEnable { - hexBlock = hex.EncodeToString(encoding.Marshal(block)) - } - - return ExplorerBlock{ + b := ExplorerBlock{ MinerPayoutIDs: mpoids, Transactions: etxns, RawBlock: block, - HexBlock: hexBlock, - BlockId: hex.EncodeToString(encoding.Marshal(block.ID())), + BlockID: hex.EncodeToString(encoding.Marshal(block.ID())), BlockFacts: facts, } + if hexBlockEnable { + b.HexBlock = hex.EncodeToString(encoding.Marshal(block)) + } + return b } // explorerHandler handles API calls to /explorer/blocks/:height. From 954c25bd33fd019c8025fbd68059a0a054472863 Mon Sep 17 00:00:00 2001 From: Petabyte Storage Date: Mon, 16 Oct 2017 00:05:14 -0700 Subject: [PATCH 6/6] rename to HexBlockID --- api/explorer.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/explorer.go b/api/explorer.go index 617d0ba309..8aba46c69f 100644 --- a/api/explorer.go +++ b/api/explorer.go @@ -23,7 +23,7 @@ type ( Transactions []ExplorerTransaction `json:"transactions"` RawBlock types.Block `json:"rawblock"` HexBlock string `json:"hexblock"` - BlockID string `json:"blockid"` + HexBlockID string `json:"blockid"` modules.BlockFacts } @@ -198,7 +198,7 @@ func (api *API) buildExplorerBlock(height types.BlockHeight, block types.Block, MinerPayoutIDs: mpoids, Transactions: etxns, RawBlock: block, - BlockID: hex.EncodeToString(encoding.Marshal(block.ID())), + HexBlockID: hex.EncodeToString(encoding.Marshal(block.ID())), BlockFacts: facts, }