From 8a5a3c4983b76d5c3dd64d846cbc7f51de3f76db Mon Sep 17 00:00:00 2001 From: garritfra Date: Sat, 22 Dec 2018 16:58:27 +0100 Subject: [PATCH] Optimize blockchain overview --- core/Blockchain.go | 2 ++ server/webserver.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/core/Blockchain.go b/core/Blockchain.go index 761a875..5b3dcc8 100644 --- a/core/Blockchain.go +++ b/core/Blockchain.go @@ -76,6 +76,7 @@ func (bc *Blockchain) IsValid() bool { // JSONBlockchain is needed, because the hash of each block is calculated dynamically, and therefore is not stored in the `Block` struct type JSONBlockchain struct { Blocks []JSONBlock + Blockcount int PendingTransactions []Transaction } @@ -86,6 +87,7 @@ func (bc *Blockchain) AsJSON() JSONBlockchain { for _, block := range bc.Blocks { jsonChain.Blocks = append(jsonChain.Blocks, block.AsJSON()) } + jsonChain.Blockcount = len(jsonChain.Blocks) return jsonChain } diff --git a/server/webserver.go b/server/webserver.go index 42b4d41..dda7d78 100644 --- a/server/webserver.go +++ b/server/webserver.go @@ -41,7 +41,7 @@ func handleError(err error, w http.ResponseWriter, r *http.Request) { } func handleListBlocks(w http.ResponseWriter, r *http.Request) { - json.NewEncoder(w).Encode(blockchain.AsJSON().Blocks) + json.NewEncoder(w).Encode(blockchain.AsJSON()) } func handleAddTransaction(w http.ResponseWriter, r *http.Request) {