-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_retrieve.go
239 lines (206 loc) · 6.49 KB
/
block_retrieve.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package api
import (
"gopkg.in/fatih/set.v0"
"github.com/bytom/bystack/blockchain/query"
chainjson "github.com/bytom/bystack/encoding/json"
"github.com/bytom/bystack/protocol/bc"
"github.com/bytom/bystack/protocol/bc/types"
)
// return best block hash
func (a *API) getBestBlockHash() Response {
blockHash := map[string]string{"block_hash": a.chain.BestBlockHash().String()}
return NewSuccessResponse(blockHash)
}
// return current block count
func (a *API) getBlockCount() Response {
blockHeight := map[string]uint64{"block_count": a.chain.BestBlockHeight()}
return NewSuccessResponse(blockHeight)
}
// BlockTx is the tx struct for getBlock func
type BlockTx struct {
ID bc.Hash `json:"id"`
Version uint64 `json:"version"`
Size uint64 `json:"size"`
TimeRange uint64 `json:"time_range"`
Inputs []*query.AnnotatedInput `json:"inputs"`
Outputs []*query.AnnotatedOutput `json:"outputs"`
MuxID bc.Hash `json:"mux_id"`
}
// BlockReq is used to handle getBlock req
type BlockReq struct {
BlockHeight uint64 `json:"block_height"`
BlockHash chainjson.HexBytes `json:"block_hash"`
}
// GetBlockResp is the resp for getBlock api
type GetBlockResp struct {
Hash *bc.Hash `json:"hash"`
Size uint64 `json:"size"`
Version uint64 `json:"version"`
Height uint64 `json:"height"`
Validator string `json:"validator"`
PreviousBlockHash *bc.Hash `json:"previous_block_hash"`
Timestamp uint64 `json:"timestamp"`
TransactionsMerkleRoot *bc.Hash `json:"transaction_merkle_root"`
Transactions []*BlockTx `json:"transactions"`
}
// return block by hash/height
func (a *API) getBlock(ins BlockReq) Response {
block, err := a.getBlockHelper(ins)
if err != nil {
return NewErrorResponse(err)
}
blockHash := block.Hash()
rawBlock, err := block.MarshalText()
if err != nil {
return NewErrorResponse(err)
}
var validatorPubKey string
if block.Height > 0 {
validator, err := a.chain.GetValidator(&block.PreviousBlockHash, block.Timestamp)
if err != nil {
return NewErrorResponse(err)
}
validatorPubKey = validator.PubKey
}
resp := &GetBlockResp{
Hash: &blockHash,
Size: uint64(len(rawBlock)),
Version: block.Version,
Height: block.Height,
Validator: validatorPubKey,
PreviousBlockHash: &block.PreviousBlockHash,
Timestamp: block.Timestamp,
TransactionsMerkleRoot: &block.TransactionsMerkleRoot,
Transactions: []*BlockTx{},
}
for _, orig := range block.Transactions {
tx := &BlockTx{
ID: orig.ID,
Version: orig.Version,
Size: orig.SerializedSize,
TimeRange: orig.TimeRange,
Inputs: []*query.AnnotatedInput{},
Outputs: []*query.AnnotatedOutput{},
}
resOutID := orig.ResultIds[0]
resOut := orig.Entries[*resOutID]
switch out := resOut.(type) {
case *bc.OriginalOutput:
tx.MuxID = *out.Source.Ref
case *bc.VoteOutput:
tx.MuxID = *out.Source.Ref
case *bc.Retirement:
tx.MuxID = *out.Source.Ref
}
for i := range orig.Inputs {
tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(orig, uint32(i)))
}
for i := range orig.Outputs {
tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(orig, i))
}
resp.Transactions = append(resp.Transactions, tx)
}
return NewSuccessResponse(resp)
}
// GetRawBlockResp is resp struct for getRawBlock API
type GetRawBlockResp struct {
RawBlock *types.Block `json:"raw_block"`
Validator string `json:"validator"`
}
func (a *API) getRawBlock(ins BlockReq) Response {
block, err := a.getBlockHelper(ins)
if err != nil {
return NewErrorResponse(err)
}
var validatorPubKey string
if block.Height > 0 {
validator, err := a.chain.GetValidator(&block.PreviousBlockHash, block.Timestamp)
if err != nil {
return NewErrorResponse(err)
}
validatorPubKey = validator.PubKey
}
resp := GetRawBlockResp{
RawBlock: block,
Validator: validatorPubKey,
}
return NewSuccessResponse(resp)
}
// GetBlockHeaderResp is resp struct for getBlockHeader API
type GetBlockHeaderResp struct {
BlockHeader *types.BlockHeader `json:"block_header"`
}
func (a *API) getBlockHeader(ins BlockReq) Response {
block, err := a.getBlockHelper(ins)
if err != nil {
return NewErrorResponse(err)
}
resp := &GetBlockHeaderResp{
BlockHeader: &block.BlockHeader,
}
return NewSuccessResponse(resp)
}
func (a *API) getBlockHelper(ins BlockReq) (*types.Block, error) {
if len(ins.BlockHash) == 32 {
hash := hexBytesToHash(ins.BlockHash)
return a.chain.GetBlockByHash(&hash)
} else {
return a.chain.GetBlockByHeight(ins.BlockHeight)
}
}
func hexBytesToHash(hexBytes chainjson.HexBytes) bc.Hash {
b32 := [32]byte{}
copy(b32[:], hexBytes)
return bc.NewHash(b32)
}
// MerkleBlockReq is used to handle getTxOutProof req
type MerkleBlockReq struct {
TxIDs []chainjson.HexBytes `json:"tx_ids"`
BlockHash chainjson.HexBytes `json:"block_hash"`
}
// GetMerkleBlockResp is resp struct for GetTxOutProof API
type GetMerkleBlockResp struct {
BlockHeader types.BlockHeader `json:"block_header"`
TxHashes []*bc.Hash `json:"tx_hashes"`
Flags []uint32 `json:"flags"`
MatchedTxIDs []*bc.Hash `json:"matched_tx_ids"`
}
func (a *API) getMerkleProof(ins MerkleBlockReq) Response {
blockReq := BlockReq{BlockHash: ins.BlockHash}
block, err := a.getBlockHelper(blockReq)
if err != nil {
return NewErrorResponse(err)
}
matchedTxs := getMatchedTx(block.Transactions, ins.TxIDs)
var matchedTxIDs []*bc.Hash
for _, tx := range matchedTxs {
matchedTxIDs = append(matchedTxIDs, &tx.ID)
}
hashes, compactFlags := types.GetTxMerkleTreeProof(block.Transactions, matchedTxs)
flags := make([]uint32, len(compactFlags))
for i, flag := range compactFlags {
flags[i] = uint32(flag)
}
resp := &GetMerkleBlockResp{
BlockHeader: block.BlockHeader,
TxHashes: hashes,
Flags: flags,
MatchedTxIDs: matchedTxIDs,
}
return NewSuccessResponse(resp)
}
func getMatchedTx(txs []*types.Tx, filterTxIDs []chainjson.HexBytes) []*types.Tx {
txIDSet := set.New()
for _, txID := range filterTxIDs {
hash := hexBytesToHash(txID)
txIDSet.Add(hash.String())
}
var matchedTxs []*types.Tx
for _, tx := range txs {
hashStr := tx.ID.String()
if txIDSet.Has(hashStr) {
matchedTxs = append(matchedTxs, tx)
}
}
return matchedTxs
}