-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
186 lines (165 loc) · 4.7 KB
/
api.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (C) 2023 Wooyang2018
// Licensed under the GNU General Public License v3.0
package node
import (
"encoding/hex"
"fmt"
"io"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/wooyang2018/svp-blockchain/core"
"github.com/wooyang2018/svp-blockchain/execution/common"
"github.com/wooyang2018/svp-blockchain/logger"
)
type nodeAPI struct {
node *Node
}
func serveNodeAPI(node *Node) {
api := &nodeAPI{node}
gin.SetMode(gin.ReleaseMode)
gin.DefaultWriter = io.Discard
r := gin.New()
r.Use(gin.Recovery())
r.GET("/consensus", api.getConsensusStatus)
r.GET("/txpool", api.getTxPoolStatus)
r.POST("/transactions", api.submitTx)
r.POST("/transactions/batch", api.batchSubmitTxs)
r.GET("/transactions/:hash/status", api.getTxStatus)
r.GET("/transactions/:hash/commit", api.getTxCommit)
r.GET("/blocks/:hash", api.getBlock)
r.GET("/blocks/height/:height", api.getBlockByHeight)
r.POST("/querystate", api.queryState)
r.POST("/bincc", api.uploadBinChainCode)
r.POST("/contract", api.uploadContractCode)
r.Static("/bincc", node.config.ExecutionConfig.BinccDir)
r.Static("/contract", node.config.ExecutionConfig.ContractDir)
go func() {
if err := r.Run(fmt.Sprintf(":%d", node.config.APIPort)); err != nil {
logger.I().Fatalf("failed to start api %+v", err)
}
}()
}
func (api *nodeAPI) getConsensusStatus(c *gin.Context) {
c.JSON(http.StatusOK, api.node.consensus.GetStatus())
}
func (api *nodeAPI) getTxPoolStatus(c *gin.Context) {
c.JSON(http.StatusOK, api.node.txpool.GetStatus())
}
func (api *nodeAPI) submitTx(c *gin.Context) {
tx := core.NewTransaction()
if err := c.ShouldBind(tx); err != nil {
c.String(http.StatusBadRequest, "cannot parse tx")
return
}
if err := api.node.txpool.SubmitTx(tx); err != nil {
logger.I().Warnf("submit tx failed %+v", err)
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, "transaction accepted")
}
func (api *nodeAPI) batchSubmitTxs(c *gin.Context) {
txs := core.NewTxList()
if err := c.ShouldBind(txs); err != nil {
c.String(http.StatusBadRequest, "cannot parse txs")
return
}
if err := api.node.txpool.StoreTxs(txs); err != nil {
logger.I().Warnf("store txs failed %+v", err)
c.String(http.StatusInternalServerError, err.Error())
return
}
c.String(http.StatusOK, "transactions accepted")
}
func (api *nodeAPI) queryState(c *gin.Context) {
query := new(common.QueryData)
if err := c.ShouldBind(query); err != nil {
c.String(http.StatusBadRequest, "cannot parse request")
return
}
result, err := api.node.execution.Query(query)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, result)
}
func (api *nodeAPI) getTxStatus(c *gin.Context) {
hash, err := api.getHash(c)
if err != nil {
c.String(http.StatusBadRequest, "cannot parse request")
return
}
status := api.node.txpool.GetTxStatus(hash)
c.JSON(http.StatusOK, status)
}
func (api *nodeAPI) getTxCommit(c *gin.Context) {
hash, err := api.getHash(c)
if err != nil {
c.String(http.StatusBadRequest, "cannot parse hash")
return
}
txc, err := api.node.storage.GetTxCommit(hash)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, txc)
}
func (api *nodeAPI) getBlock(c *gin.Context) {
hash, err := api.getHash(c)
if err != nil {
c.String(http.StatusBadRequest, "cannot parse hash")
return
}
blk, err := api.node.GetBlock(hash)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, blk)
}
func (api *nodeAPI) getHash(c *gin.Context) ([]byte, error) {
hashstr := c.Param("hash")
return hex.DecodeString(hashstr)
}
func (api *nodeAPI) getBlockByHeight(c *gin.Context) {
hstr := c.Param("height")
height, err := strconv.ParseUint(hstr, 10, 64)
if err != nil {
c.String(http.StatusBadRequest, "cannot parse height")
return
}
blk, err := api.node.storage.GetBlockByHeight(height)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, blk)
}
func (api *nodeAPI) uploadBinChainCode(c *gin.Context) {
uploadCode(c, api.node.config.ExecutionConfig.BinccDir)
}
func (api *nodeAPI) uploadContractCode(c *gin.Context) {
uploadCode(c, api.node.config.ExecutionConfig.ContractDir)
}
func uploadCode(c *gin.Context, path string) {
fh, err := c.FormFile("file")
if err != nil {
c.String(http.StatusBadRequest, "cannot get uploaded file")
return
}
f, err := fh.Open()
if err != nil {
c.String(http.StatusBadRequest, "cannot open multipart file")
return
}
defer f.Close()
codeID, err := common.StoreCode(path, f)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.JSON(http.StatusOK, codeID)
}