-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_server.go
162 lines (138 loc) · 3.34 KB
/
http_server.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package blc
import (
"fmt"
"github.com/gin-gonic/gin"
log "github.com/treeforest/logger"
"io/ioutil"
"net/http"
)
type HttpServer struct {
port int
chain *BlockChain
}
func NewHttpServer(port int, chain *BlockChain) *HttpServer {
return &HttpServer{port: port, chain: chain}
}
func (s *HttpServer) Run() {
r := gin.Default()
r.GET("/height", s.handleGetHeight)
r.GET("/blocks", s.handleGetBlocks)
r.GET("/balance", s.handleGetBalance)
r.GET("/utxo", s.handleGetUtxo)
r.GET("/txPool", s.handleGetTxPool)
r.POST("/tx", s.handlePostTx)
err := r.Run(fmt.Sprintf(":%d", s.port))
if err != nil {
log.Fatal("http server run failed:", err)
}
}
func (s *HttpServer) handleGetHeight(c *gin.Context) {
if !s.checkEnvironment(c) {
return
}
type Response struct {
Height uint64 `json:"height"`
}
c.JSON(http.StatusOK, Response{Height: s.chain.GetLatestBlock().Height})
}
func (s *HttpServer) handleGetBlocks(c *gin.Context) {
if !s.checkEnvironment(c) {
return
}
type Request struct {
Start uint64 `json:"start"`
End uint64 `json:"end"`
}
req := Request{}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "request obj error"})
return
}
type Response struct {
Blocks []Block `json:"blocks"`
}
resp := Response{Blocks: make([]Block, 0)}
for height := req.Start; height <= req.End; height++ {
b, err := s.chain.GetBlock(height)
if err != nil {
break
}
resp.Blocks = append(resp.Blocks, *b)
}
c.JSON(http.StatusOK, resp)
}
func (s *HttpServer) handleGetBalance(c *gin.Context) {
if !s.checkEnvironment(c) {
return
}
type Request struct {
Address string `json:"address"`
}
req := Request{}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "request obj error"})
return
}
type Response struct {
Balance uint64 `json:"balance"`
}
resp := Response{Balance: s.chain.GetBalance(req.Address)}
c.JSON(http.StatusOK, resp)
}
func (s *HttpServer) handleGetUtxo(c *gin.Context) {
if !s.checkEnvironment(c) {
return
}
type Request struct {
Address string `json:"address"`
}
req := Request{}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "request obj error"})
return
}
type Utxo struct {
TxHash [32]byte
Index int
TxOut TxOutput
}
utxoes := make([]Utxo, 0)
s.chain.utxoSet.Traverse(func(txHash [32]byte, index int, output TxOutput) {
utxoes = append(utxoes, Utxo{
TxHash: txHash,
Index: index,
TxOut: output,
})
})
c.JSON(http.StatusOK, utxoes)
}
func (s *HttpServer) handleGetTxPool(c *gin.Context) {
c.JSON(http.StatusOK, s.chain.txPool)
}
func (s *HttpServer) handlePostTx(c *gin.Context) {
if !s.checkEnvironment(c) {
return
}
var tx Transaction
data, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if err = tx.Unmarshal(data); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "unmarshal failed"})
return
}
if err = s.chain.AddToTxPool(tx); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.Status(http.StatusOK)
}
func (s *HttpServer) checkEnvironment(c *gin.Context) bool {
if s.chain.GetLatestBlock() == nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "not exist blockchain"})
return false
}
return true
}