-
Notifications
You must be signed in to change notification settings - Fork 0
/
blocks.go
124 lines (108 loc) · 3.01 KB
/
blocks.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
// Copyright (c) 2018 The VeChainThor developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package blocks
import (
"net/http"
"strconv"
"github.com/ethereum/go-ethereum/common/math"
"github.com/gorilla/mux"
"github.com/miniBamboo/workshare/api/utils"
"github.com/miniBamboo/workshare/chain"
"github.com/miniBamboo/workshare/workshare"
"github.com/pkg/errors"
)
type Blocks struct {
repo *chain.Repository
}
func New(repo *chain.Repository) *Blocks {
return &Blocks{
repo,
}
}
func (b *Blocks) handleGetBlock(w http.ResponseWriter, req *http.Request) error {
revision, err := b.parseRevision(mux.Vars(req)["revision"])
if err != nil {
return utils.BadRequest(errors.WithMessage(err, "revision"))
}
expanded := req.URL.Query().Get("expanded")
if expanded != "" && expanded != "false" && expanded != "true" {
return utils.BadRequest(errors.WithMessage(errors.New("should be boolean"), "expanded"))
}
summary, err := b.getBlockSummary(revision)
if err != nil {
if b.repo.IsNotFound(err) {
return utils.WriteJSON(w, nil)
}
return err
}
isTrunk, err := b.isTrunk(summary.Header.ID(), summary.Header.Number())
if err != nil {
return err
}
jSummary := buildJSONBlockSummary(summary, isTrunk)
if expanded == "true" {
txs, err := b.repo.GetBlockTransactions(summary.Header.ID())
if err != nil {
return err
}
receipts, err := b.repo.GetBlockReceipts(summary.Header.ID())
if err != nil {
return err
}
return utils.WriteJSON(w, &JSONExpandedBlock{
jSummary,
buildJSONEmbeddedTxs(txs, receipts),
})
}
return utils.WriteJSON(w, &JSONCollapsedBlock{
jSummary,
summary.Txs,
})
}
func (b *Blocks) parseRevision(revision string) (interface{}, error) {
if revision == "" || revision == "best" {
return nil, nil
}
if len(revision) == 66 || len(revision) == 64 {
blockID, err := workshare.ParseBytes32(revision)
if err != nil {
return nil, err
}
return blockID, nil
}
n, err := strconv.ParseUint(revision, 0, 0)
if err != nil {
return nil, err
}
if n > math.MaxUint32 {
return nil, errors.New("block number out of max uint32")
}
return uint32(n), err
}
func (b *Blocks) getBlockSummary(revision interface{}) (s *chain.BlockSummary, err error) {
var id workshare.Bytes32
switch revision := revision.(type) {
case workshare.Bytes32:
id = revision
case uint32:
id, err = b.repo.NewBestChain().GetBlockID(revision)
if err != nil {
return
}
default:
id = b.repo.BestBlockSummary().Header.ID()
}
return b.repo.GetBlockSummary(id)
}
func (b *Blocks) isTrunk(blkID workshare.Bytes32, blkNum uint32) (bool, error) {
idByNum, err := b.repo.NewBestChain().GetBlockID(blkNum)
if err != nil {
return false, err
}
return blkID == idByNum, nil
}
func (b *Blocks) Mount(root *mux.Router, pathPrefix string) {
sub := root.PathPrefix(pathPrefix).Subrouter()
sub.Path("/{revision}").Methods("GET").HandlerFunc(utils.WrapHandlerFunc(b.handleGetBlock))
}