/
get_blockchain.go
75 lines (62 loc) · 1.6 KB
/
get_blockchain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package http
import (
"fmt"
"io"
"net/http"
"go.uber.org/zap"
"github.com/ariden83/blockchain/internal/iterator"
)
/*
func (e *EndPoint) handlePrintBlockChain(w http.ResponseWriter, _ *http.Request) {
iterator := e.Iterator()
for {
block, err := iterator.Next()
if err != nil {
e.log.Error("fail to iterate next block", zap.Error(err))
return
}
if _, err := io.WriteString(w, fmt.Sprintf("Previous hash: %x\ndata: %+v\nhash: %x\n",
block.PrevHash,
block,
block.Hash)); err != nil {
e.log.Error("fail to write string", zap.Error(err))
return
}
// This works because the Genesis block has no PrevHash to point to.
if len(block.PrevHash) == 0 {
break
}
}
e.event.Push(event.Message{Type: event.BlockChain})
}
*/
// Iterator takes our BlockChain struct and returns it as a BlockCHainIterator struct
/*func (e *EndPoint) Iterator() *iterator.BlockChainIterator {
iterator := iterator.BlockChainIterator{
CurrentHash: e.persistence.LastHash(),
Persistence: e.persistence,
}
return &iterator
}*/
func (e *EndPoint) handleGetBlockChain(w http.ResponseWriter, _ *http.Request) {
iterator := iterator.New(e.persistence)
return
for {
block, err := iterator.Next()
if err != nil {
e.log.Error("fail to iterate next block", zap.Error(err))
return
}
if _, err = io.WriteString(w, fmt.Sprintf("Previous hash: %x\ndata: %+v\nhash: %x\n",
block.PrevHash,
block,
block.Hash)); err != nil {
e.log.Error("fail to WriteString", zap.Error(err))
return
}
if len(block.PrevHash) == 0 {
break
}
}
// e.event.Push(event.Message{Type: event.BlockChain})
}