-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_wrap.go
172 lines (151 loc) · 5.36 KB
/
client_wrap.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
package bbrpc
/**
* 该文件对一些rpc进行封装的函数
*
*/
import (
"fmt"
)
// ListTransactionsSinceBlock 列出自某个区块以来的所有交易,不包含targetBlock的交易
// return: scan2BlockHash, tx, error
func (c *Client) ListTransactionsSinceBlock(targetBlockHash string, count int) (string, []TransactionDetail, error) {
var fork *string = nil
const defaultRecentHeight = 30 //如果提供的hash为空,则取最近的n个块的交易
topForkHeight, err := c.Getforkheight(fork)
if err != nil {
return "", nil, fmt.Errorf("failed to get fork height, %v", err)
}
if targetBlockHash == "" { //默认取最近30个块吧
defaultTargetBlockHeight := topForkHeight - defaultRecentHeight
if defaultTargetBlockHeight < 1 {
defaultTargetBlockHeight = 1
}
defaultTargetBlockHash, err := c.Getblockhash(int(defaultTargetBlockHeight), nil)
if err != nil || len(defaultTargetBlockHash) == 0 {
return "", nil, fmt.Errorf("failed to get default target block hash, %v, len(hash): %d", err, len(defaultTargetBlockHash))
}
targetBlockHash = defaultTargetBlockHash[0]
}
topBlockHash, err := c.Getblockhash(int(topForkHeight), nil)
if err != nil {
return "", nil, fmt.Errorf("failed to get top block hash, %v", err)
}
var all []TransactionDetail
prevBlockHash := targetBlockHash
block, err := c.Getblock(prevBlockHash)
if err != nil {
return "", nil, fmt.Errorf("failed to get block [%s]", prevBlockHash)
}
if block.Height == uint(topForkHeight) { //已经是最新高度了
return topBlockHash[0], []TransactionDetail{}, nil
}
prevBlockHeight := block.Height
scanBlockCount := 0
for {
prevBlockHeight++
blockHash, err := c.Getblockhash(int(prevBlockHeight), fork)
if err != nil {
return prevBlockHash, nil, fmt.Errorf("failed to get block hash @ [%d], %v", prevBlockHeight, err)
}
block, err := c.Getblock(blockHash[0])
if err != nil {
return prevBlockHash, nil, fmt.Errorf("failed to get block @ [%s], %v", blockHash, err)
}
if len(block.Tx) > 0 {
for _, txid := range block.Tx {
tx, err := c.Gettransaction(txid, pbool(false))
if err != nil {
return prevBlockHash, nil, fmt.Errorf("failed to get transaction [%s] at [%s(%d)]", txid, block.Hash, block.Height)
}
all = append(all, *tx)
}
}
prevBlockHash = block.Hash
scanBlockCount++
if block.Height == uint(topForkHeight) || scanBlockCount >= count {
break
}
}
return prevBlockHash, all, nil
}
// ListBlockDetailsSince 列出自某个区块以来的所有区块详情,不包含targetBlock的交易
// return: topBlockHeight, blockDetails, error
func (c *Client) ListBlockDetailsSince(fork *string, targetBlockHash string, count int) (int, []BlockDetail, error) {
// fmt.Println("[dbg]ListBlockDetailsSince", *fork, targetBlockHash, count)
const defaultRecentHeight = 30 //如果提供的hash为空,则取最近的n个块的交易
topForkHeight, err := c.Getforkheight(fork)
if err != nil {
return -1, nil, fmt.Errorf("failed to get fork height, %v", err)
}
if targetBlockHash == "" { //默认取最近30个块吧
defaultTargetBlockHeight := topForkHeight - defaultRecentHeight
if defaultTargetBlockHeight < 1 {
defaultTargetBlockHeight = 1
}
defaultTargetBlockHash, err := c.Getblockhash(int(defaultTargetBlockHeight), fork)
if err != nil || len(defaultTargetBlockHash) == 0 {
return topForkHeight, nil, fmt.Errorf("failed to get default target block hash, %v, len(hash): %d", err, len(defaultTargetBlockHash))
}
targetBlockHash = defaultTargetBlockHash[0]
}
var all []BlockDetail
var prevBlockHeight uint
{
block, err := c.Getblock(targetBlockHash)
if err != nil {
return topForkHeight, nil, fmt.Errorf("failed to get block [%s]", targetBlockHash)
}
if block.Height == uint(topForkHeight) { //已经是最新高度了
return topForkHeight, nil, nil
}
prevBlockHeight = block.Height
}
scannedBlockCount := 0
for {
prevBlockHeight++
blockHash, err := c.Getblockhash(int(prevBlockHeight), fork)
if err != nil || len(blockHash) == 0 {
return topForkHeight, nil, fmt.Errorf("failed to get block hash @ [%d], result len: [%d], %v", prevBlockHeight, len(blockHash), err)
}
var lastHeight uint
for _, hash := range blockHash {
detail, err := c.Getblockdetail(hash)
if err != nil {
return topForkHeight, nil, fmt.Errorf("failed to get block detail @ [%s], %v", blockHash, err)
}
all = append(all, *detail)
lastHeight = detail.Height
}
if lastHeight == uint(topForkHeight) || scannedBlockCount >= count {
break
}
scannedBlockCount++
}
return topForkHeight, all, nil
}
// ListBlockDetailsBetween 列出[from,to]内所有区块详情
// return: blockDetails, error
func (c *Client) ListBlockDetailsBetween(fork *string, fromHeight, toHeight int) ([]BlockDetail, error) {
var all []BlockDetail
cursorHeight := fromHeight
for {
blockHash, err := c.Getblockhash(cursorHeight, fork)
if err != nil || len(blockHash) == 0 {
return nil, fmt.Errorf("failed to get block hash @ [%d], result len: [%d], %v", cursorHeight, len(blockHash), err)
}
var lastHeight uint
for _, hash := range blockHash {
detail, err := c.Getblockdetail(hash)
if err != nil {
return nil, fmt.Errorf("failed to get block detail @ [%s], %v", blockHash, err)
}
all = append(all, *detail)
lastHeight = detail.Height
}
if int(lastHeight) == toHeight {
break
}
cursorHeight++
}
return all, nil
}