This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_api.go
133 lines (114 loc) · 4.31 KB
/
sync_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
package main
import (
"github.com/gofiber/fiber/v2"
)
type _ struct {
Success bool `json:"success" example:"true"`
Results int `json:"results" example:"1"`
SyncedBlock SyncedBlock `json:"synced_block"`
} // @name GetPeakSyncedBlockResponse
// GetPeakSyncedBlock godoc
// @Summary returns the peak synced block
// @Description Beta constantly syncs to the blockchain by fetching the latest blocks. This function returns information about the latest processed block (the one with the biggest 'height' value).
// @Tags Sync
// @Accept json
// @Produce json
// @Success 200 {object} GetPeakSyncedBlockResponse
// @Failure 401 {object} NoAPIKeyResponse
// @Failure 500 {object} ErrorResponse
// @Security ApiKeyAuth
// @Router /get_peak_synced_block [get]
// @Router /get_peak_synced_block [post]
func GetPeakSyncedBlock(c *fiber.Ctx) error {
var sb SyncedBlock
result := DB.Order("height desc").First(&sb)
if result.Error != nil {
return MakeErrorResponse(c, result.Error.Error())
}
return MakeSuccessResponseForSingleObject(c, "synced_block", SyncedBlockToJSON(sb))
}
type GetSyncedBlockArgs struct {
Height uint32 `json:"height" example:"2000000"`
} // @name GetSyncedBlockArgs
type _ struct {
Success bool `json:"success" example:"true"`
Results int `json:"results" example:"1"`
SyncedBlock SyncedBlock `json:"synced_block"`
} // @name GetSyncedBlockResponse
// GetSyncedBlock godoc
// @Summary returns block for given height
// @Description Beta constantly syncs to the blockchain by fetching the latest blocks. This function returns information about the block at the given height.
// @Tags Sync
// @Accept json
// @Produce json
// @Param Body body GetSyncedBlockArgs true "The height"
// @Success 200 {object} GetSyncedBlockResponse
// @Failure 401 {object} NoAPIKeyResponse
// @Failure 500 {object} ErrorResponse
// @Security ApiKeyAuth
// @Router /get_synced_block [post]
func GetSyncedBlock(c *fiber.Ctx) error {
args := new(GetSyncedBlockArgs)
if err := c.BodyParser(args); err != nil {
return MakeErrorResponse(c, err.Error())
}
var sb SyncedBlock
result := DB.First(&sb, "height = ?", args.Height)
if result.Error != nil {
return MakeErrorResponse(c, result.Error.Error())
}
return MakeSuccessResponseForSingleObject(c, "synced_block", SyncedBlockToJSON(sb))
}
type GetSyncedBlocksArgs struct {
Start_height uint32 `json:"start" example:"2000000"`
End_height uint32 `json:"end" example:"2000001"`
}// @name GetSyncedBlocksArgs
type _ struct {
Success bool `json:"success" example:"true"`
Results int `json:"results" example:"1"`
SyncedBlocks []SyncedBlock `json:"synced_blocks"`
} // @name GetSyncedBlocksResponse
// GetSyncedBlocks godoc
// @Summary returns blocks for given range
// @Description Beta constantly syncs to the blockchain by fetching the latest blocks. This function returns information about the blocks with height in [start, end).
// @Tags Sync
// @Accept json
// @Produce json
// @Param Body body GetSyncedBlocksArgs true "The start and end heights"
// @Success 200 {object} GetSyncedBlocksResponse
// @Failure 401 {object} NoAPIKeyResponse
// @Failure 500 {object} ErrorResponse
// @Security ApiKeyAuth
// @Router /get_synced_blocks [post]
func GetSyncedBlocks(c *fiber.Ctx) error {
args := new(GetSyncedBlocksArgs)
if err := c.BodyParser(args); err != nil {
return MakeErrorResponse(c, err.Error())
}
if args.End_height <= args.Start_height {
return MakeErrorResponse(c, "end_height less than or equal to start_height")
}
if args.End_height - args.Start_height > 100 {
return MakeErrorResponse(c, "if you really need more than 100 blocks at a time, mesage us directly")
}
var synced_blocks []SyncedBlock
result := DB.Order("height asc").Find(
&synced_blocks,
"height >= ? AND height < ?",
args.Start_height, args.End_height,
)
if result.Error != nil {
return MakeErrorResponse(c, result.Error.Error())
}
var synced_blocks_JSON []fiber.Map
for _, synced_block := range synced_blocks {
synced_blocks_JSON = append(synced_blocks_JSON, SyncedBlockToJSON(synced_block))
}
return MakeSuccessResponseForArray(c, "synced_blocks", synced_blocks_JSON)
}
func SetupSyncAPIRoutes(app *fiber.App) {
app.Get("/get_peak_synced_block", GetPeakSyncedBlock)
app.Post("/get_peak_synced_block", GetPeakSyncedBlock)
app.Post("/get_synced_block", GetSyncedBlock)
app.Post("/get_synced_blocks", GetSyncedBlocks)
}