-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathblocks.go
More file actions
75 lines (63 loc) · 1.92 KB
/
blocks.go
File metadata and controls
75 lines (63 loc) · 1.92 KB
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 api
import (
"context"
"errors"
"github.com/sourcegraph/jsonrpc2"
"github.com/CovenantSQL/CovenantSQL/api/models"
)
func init() {
rpc.RegisterMethod("bp_getBlockList", bpGetBlockList, bpGetBlockListParams{})
rpc.RegisterMethod("bp_getBlockByHeight", bpGetBlockByHeight, bpGetBlockByHeightParams{})
rpc.RegisterMethod("bp_getBlockByHash", bpGetBlockByHash, bpGetBlockByHashParams{})
}
type bpGetBlockListParams struct {
Since int `json:"since"`
Page int `json:"page"`
Size int `json:"size"`
}
func (params *bpGetBlockListParams) Validate() error {
if params.Size > 1000 {
return errors.New("max size is 1000")
}
return nil
}
// BPGetBlockListResponse is the response for method bp_getBlockList.
type BPGetBlockListResponse struct {
Blocks []*models.Block `json:"blocks"`
Pagination *models.Pagination `json:"pagination"`
}
func bpGetBlockList(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
result interface{}, err error,
) {
params := ctx.Value("_params").(*bpGetBlockListParams)
model := models.BlocksModel{}
blocks, pagination, err := model.GetBlockList(params.Since, params.Page, params.Size)
if err != nil {
return nil, err
}
result = &BPGetBlockListResponse{
Blocks: blocks,
Pagination: pagination,
}
return result, nil
}
type bpGetBlockByHeightParams struct {
Height int `json:"height"`
}
func bpGetBlockByHeight(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
result interface{}, err error,
) {
params := ctx.Value("_params").(*bpGetBlockByHeightParams)
model := models.BlocksModel{}
return model.GetBlockByHeight(params.Height)
}
type bpGetBlockByHashParams struct {
Hash string `json:"hash"`
}
func bpGetBlockByHash(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
result interface{}, err error,
) {
params := ctx.Value("_params").(*bpGetBlockByHashParams)
model := models.BlocksModel{}
return model.GetBlockByHash(params.Hash)
}