-
Notifications
You must be signed in to change notification settings - Fork 4
/
block.go
161 lines (135 loc) · 4.28 KB
/
block.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
package api
import (
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-node/core/rewards"
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/MinterTeam/minter-go-node/eventsdb"
"github.com/gorilla/mux"
"github.com/tendermint/tendermint/libs/common"
"math/big"
"net/http"
"strconv"
"time"
)
var edb *eventsdb.EventsDB
func init() {
edb = eventsdb.NewEventsDB(eventsdb.GetCurrentDB())
}
type BlockResponse struct {
Hash common.HexBytes `json:"hash"`
Height int64 `json:"height"`
Time time.Time `json:"time"`
NumTxs int64 `json:"num_txs"`
TotalTxs int64 `json:"total_txs"`
Transactions []BlockTransactionResponse `json:"transactions"`
Events json.RawMessage `json:"events,omitempty"`
Precommits json.RawMessage `json:"precommits"`
BlockReward string `json:"block_reward"`
Size int `json:"size"`
}
type BlockTransactionResponse struct {
Hash string `json:"hash"`
RawTx string `json:"raw_tx"`
From string `json:"from"`
Nonce uint64 `json:"nonce"`
GasPrice *big.Int `json:"gas_price"`
Type byte `json:"type"`
Data transaction.Data `json:"data"`
Payload []byte `json:"payload"`
ServiceData []byte `json:"service_data"`
Gas int64 `json:"gas"`
GasCoin types.CoinSymbol `json:"gas_coin"`
GasUsed int64 `json:"gas_used"`
Tags map[string]string `json:"tags"`
Code uint32 `json:"code,omitempty"`
Log string `json:"log,omitempty"`
}
func Block(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
height, _ := strconv.ParseInt(vars["height"], 10, 64)
block, err := client.Block(&height)
blockResults, err := client.BlockResults(&height)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err = json.NewEncoder(w).Encode(Response{
Code: 0,
Log: err.Error(),
})
if err != nil {
panic(err)
}
return
}
txs := make([]BlockTransactionResponse, len(block.Block.Data.Txs))
for i, rawTx := range block.Block.Data.Txs {
tx, _ := transaction.DecodeFromBytes(rawTx)
sender, _ := tx.Sender()
tags := make(map[string]string)
for _, tag := range blockResults.Results.DeliverTx[i].Tags {
switch string(tag.Key) {
case "tx.type":
tags[string(tag.Key)] = fmt.Sprintf("%X", tag.Value)
default:
tags[string(tag.Key)] = string(tag.Value)
}
}
txs[i] = BlockTransactionResponse{
Hash: fmt.Sprintf("Mt%x", rawTx.Hash()),
RawTx: fmt.Sprintf("%x", []byte(rawTx)),
From: sender.String(),
Nonce: tx.Nonce,
GasPrice: tx.GasPrice,
Type: tx.Type,
Data: tx.GetDecodedData(),
Payload: tx.Payload,
ServiceData: tx.ServiceData,
Gas: tx.Gas(),
GasCoin: tx.GasCoin,
GasUsed: blockResults.Results.DeliverTx[i].GasUsed,
Tags: tags,
Code: blockResults.Results.DeliverTx[i].Code,
Log: blockResults.Results.DeliverTx[i].Log,
}
}
precommits, _ := cdc.MarshalJSON(block.Block.LastCommit.Precommits)
size := len(cdc.MustMarshalBinaryLengthPrefixed(block))
var eventsRaw []byte
events := edb.LoadEvents(height)
if len(events) > 0 {
eventsRaw, err = cdc.MarshalJSON(events)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err = json.NewEncoder(w).Encode(Response{
Code: 0,
Log: err.Error(),
})
if err != nil {
panic(err)
}
return
}
}
response := BlockResponse{
Hash: block.Block.Hash(),
Height: block.Block.Height,
Time: block.Block.Time,
NumTxs: block.Block.NumTxs,
TotalTxs: block.Block.TotalTxs,
Transactions: txs,
Precommits: precommits,
BlockReward: rewards.GetRewardForBlock(uint64(height)).String(),
Size: size,
Events: eventsRaw,
}
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(Response{
Code: 0,
Result: response,
})
if err != nil {
panic(err)
}
}