-
Notifications
You must be signed in to change notification settings - Fork 4
/
block.go
108 lines (93 loc) · 3.06 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
package api
import (
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/gorilla/mux"
"github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/types"
"math/big"
"net/http"
"strconv"
"time"
)
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"`
Precommits []*types.Vote `json:"precommits"`
}
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"`
TxResult ResponseDeliverTx `json:"tx_result"`
}
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)
json.NewEncoder(w).Encode(Response{
Code: 0,
Result: err.Error(),
})
return
}
w.WriteHeader(http.StatusOK)
txs := make([]BlockTransactionResponse, len(block.Block.Data.Txs))
for i, rawTx := range block.Block.Data.Txs {
tx, _ := transaction.DecodeFromBytes(rawTx)
sender, _ := tx.Sender()
txs[i] = BlockTransactionResponse{
Hash: fmt.Sprintf("Mt%x", types.Tx(rawTx).Hash()),
RawTx: fmt.Sprintf("%x", 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(),
TxResult: ResponseDeliverTx{
Code: blockResults.Results.DeliverTx[i].Code,
Data: blockResults.Results.DeliverTx[i].Data,
Log: blockResults.Results.DeliverTx[i].Log,
Info: blockResults.Results.DeliverTx[i].Info,
GasWanted: blockResults.Results.DeliverTx[i].GasWanted,
GasUsed: blockResults.Results.DeliverTx[i].GasUsed,
Tags: blockResults.Results.DeliverTx[i].Tags,
},
}
}
response := BlockResponse{
Hash: block.Block.Hash(),
Height: block.Block.Height,
Time: block.Block.Time,
NumTxs: block.Block.NumTxs,
TotalTxs: block.Block.TotalTxs,
Precommits: block.Block.LastCommit.Precommits,
Transactions: txs,
}
err = json.NewEncoder(w).Encode(Response{
Code: 0,
Result: response,
})
if err != nil {
panic(err)
}
}