/
block.go
168 lines (148 loc) · 5.18 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
162
163
164
165
166
167
168
package api
import (
"bytes"
"encoding/hex"
"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/rpc/lib/types"
core_types "github.com/tendermint/tendermint/rpc/core/types"
types2 "github.com/tendermint/tendermint/types"
"math/big"
"time"
)
type BlockResponse struct {
Hash string `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"`
BlockReward *big.Int `json:"block_reward"`
Size int `json:"size"`
Proposer types.Pubkey `json:"proposer,omitempty"`
Validators []BlockValidatorResponse `json:"validators,omitempty"`
Evidence types2.EvidenceData `json:"evidence,omitempty"`
}
type BlockTransactionResponse struct {
Hash string `json:"hash"`
RawTx string `json:"raw_tx"`
From string `json:"from"`
Nonce uint64 `json:"nonce"`
GasPrice uint32 `json:"gas_price"`
Type transaction.TxType `json:"type"`
Data json.RawMessage `json:"data"`
Payload []byte `json:"payload"`
ServiceData []byte `json:"service_data"`
Gas int64 `json:"gas"`
GasCoin types.CoinSymbol `json:"gas_coin"`
Tags map[string]string `json:"tags"`
Code uint32 `json:"code,omitempty"`
Log string `json:"log,omitempty"`
}
type BlockValidatorResponse struct {
Pubkey string `json:"pub_key"`
Signed bool `json:"signed"`
}
func Block(height int64) (*BlockResponse, error) {
block, err := client.Block(&height)
if err != nil {
return nil, rpctypes.RPCError{Code: 404, Message: "Block not found", Data: err.Error()}
}
blockResults, err := client.BlockResults(&height)
if err != nil {
return nil, rpctypes.RPCError{Code: 404, Message: "Block results not found", Data: err.Error()}
}
valHeight := height - 1
if valHeight < 1 {
valHeight = 1
}
tmValidators, err := client.Validators(&valHeight)
if err != nil {
return nil, rpctypes.RPCError{Code: 404, Message: "Validators for block not found", Data: err.Error()}
}
txs := make([]BlockTransactionResponse, len(block.Block.Data.Txs))
for i, rawTx := range block.Block.Data.Txs {
tx, _ := transaction.TxDecoder.DecodeFromBytes(rawTx)
sender, _ := tx.Sender()
tags := make(map[string]string)
for _, tag := range blockResults.Results.DeliverTx[i].Events[0].Attributes {
tags[string(tag.Key)] = string(tag.Value)
}
data, err := encodeTxData(tx)
if err != nil {
return nil, err
}
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: data,
Payload: tx.Payload,
ServiceData: tx.ServiceData,
Gas: tx.Gas(),
GasCoin: tx.GasCoin,
Tags: tags,
Code: blockResults.Results.DeliverTx[i].Code,
Log: blockResults.Results.DeliverTx[i].Log,
}
}
var validators []BlockValidatorResponse
proposer := types.Pubkey{}
if height > 1 {
proposer, err = getBlockProposer(block)
if err != nil {
return nil, err
}
validators = make([]BlockValidatorResponse, len(tmValidators.Validators))
for i, tmval := range tmValidators.Validators {
signed := false
for _, vote := range block.Block.LastCommit.Precommits {
if vote == nil {
continue
}
if bytes.Equal(vote.ValidatorAddress.Bytes(), tmval.Address.Bytes()) {
signed = true
break
}
}
validators[i] = BlockValidatorResponse{
Pubkey: fmt.Sprintf("Mp%x", tmval.PubKey.Bytes()[5:]),
Signed: signed,
}
if bytes.Equal(tmval.Address.Bytes(), block.Block.ProposerAddress.Bytes()) {
proposer = tmval.PubKey.Bytes()[5:]
}
}
}
return &BlockResponse{
Hash: hex.EncodeToString(block.Block.Hash()),
Height: block.Block.Height,
Time: block.Block.Time,
NumTxs: block.Block.NumTxs,
TotalTxs: block.Block.TotalTxs,
Transactions: txs,
BlockReward: rewards.GetRewardForBlock(uint64(height)),
Size: len(cdc.MustMarshalBinaryLengthPrefixed(block)),
Proposer: proposer,
Validators: validators,
Evidence: block.Block.Evidence,
}, nil
}
func getBlockProposer(block *core_types.ResultBlock) (types.Pubkey, error) {
vals, err := client.Validators(&block.Block.Height)
if err != nil {
return nil, rpctypes.RPCError{Code: 404, Message: "Validators for block not found", Data: err.Error()}
}
for _, tmval := range vals.Validators {
if bytes.Equal(tmval.Address.Bytes(), block.Block.ProposerAddress.Bytes()) {
return tmval.PubKey.Bytes()[5:], nil
}
}
return nil, rpctypes.RPCError{Code: 404, Message: "Block proposer not found"}
}