forked from blocktree/tron-adapter
/
blockscanner_db.go
232 lines (195 loc) · 5.71 KB
/
blockscanner_db.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
/*
* Copyright 2018 The openwallet Authors
* This file is part of the openwallet library.
*
* The openwallet library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The openwallet library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package tron
import (
"fmt"
"github.com/blocktree/openwallet/v2/openwallet"
)
// //GetCurrentBlockHeader 获取当前区块高度
// func (bs *TronBlockScanner) GetCurrentBlockHeader() (*openwallet.BlockHeader, error) {
// var (
// block *core.Block
// blockHeight uint64 = 0
// hash string
// err error
// )
// blockHeight, hash = bs.GetLocalNewBlock()
// //如果本地没有记录,查询接口的高度
// if blockHeight == 0 {
// block, hash, err = bs.wm.GetNowBlock()
// if err != nil {
// return nil, err
// }
// blockHeight = uint64(block.GetBlockHeader().GetRawData().GetNumber())
// }
// return &openwallet.BlockHeader{Height: blockHeight, Hash: hash}, nil
// }
//GetLocalNewBlock 获取本地记录的区块高度和hash
func (bs *TronBlockScanner) GetLocalNewBlock() (uint64, string, error) {
//var (
// blockHeight uint64
// blockHash string
//)
//
////获取本地区块高度
//db, err := storm.Open(filepath.Join(bs.wm.Config.dbPath, bs.wm.Config.BlockchainFile))
//if err != nil {
// return 0, ""
//}
//defer db.Close()
//
//db.Get(blockchainBucket, "blockHeight", &blockHeight)
//db.Get(blockchainBucket, "blockHash", &blockHash)
//
//return blockHeight, blockHash
if bs.BlockchainDAI == nil {
return 0, "", fmt.Errorf("Blockchain DAI is not setup ")
}
header, err := bs.BlockchainDAI.GetCurrentBlockHead(bs.wm.Symbol())
if err != nil {
return 0, "", err
}
return header.Height, header.Hash, nil
}
//GetLocalBlock 获取本地区块数据
func (bs *TronBlockScanner) GetLocalBlock(height uint64) (*Block, error) {
//var (
// block Block
//)
//
//db, err := storm.Open(filepath.Join(bs.wm.Config.dbPath, bs.wm.Config.BlockchainFile))
//if err != nil {
// return nil, err
//}
//defer db.Close()
//
//err = db.One("Height", height, &block)
//if err != nil {
// return nil, err
//}
//
//return &block, nil
if bs.BlockchainDAI == nil {
return nil, fmt.Errorf("Blockchain DAI is not setup ")
}
header, err := bs.BlockchainDAI.GetLocalBlockHeadByHeight(height, bs.wm.Symbol())
if err != nil {
return nil, err
}
block := &Block{
Hash: header.Hash,
Height: header.Height,
}
return block, nil
}
//SaveUnscanRecord 保存交易记录到钱包数据库
func (bs *TronBlockScanner) SaveUnscanRecord(record *openwallet.UnscanRecord) error {
//if record == nil {
// return errors.New("the unscan record to save is nil")
//}
//
////if record.BlockHeight == 0 {
//// return errors.New("unconfirmed transaction do not rescan")
////}
//
////获取本地区块高度
//db, err := storm.Open(filepath.Join(bs.wm.Config.dbPath, bs.wm.Config.BlockchainFile))
//if err != nil {
// return err
//}
//defer db.Close()
//
//return db.Save(record)
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.SaveUnscanRecord(record)
}
//SaveLocalNewBlock 记录区块高度和hash到本地
func (bs *TronBlockScanner) SaveLocalNewBlock(blockHeight uint64, blockHash string) error {
//获取本地区块高度
//db, err := storm.Open(filepath.Join(bs.wm.Config.dbPath, bs.wm.Config.BlockchainFile))
//if err != nil {
// return
//}
//defer db.Close()
//
//db.Set(blockchainBucket, "blockHeight", &blockHeight)
//db.Set(blockchainBucket, "blockHash", &blockHash)
//
//if bs.BlockchainDAI == nil {
// return fmt.Errorf("Blockchain DAI is not setup ")
//}
header := &openwallet.BlockHeader{
Hash: blockHash,
Height: blockHeight,
Fork: false,
Symbol: bs.wm.Symbol(),
}
return bs.BlockchainDAI.SaveCurrentBlockHead(header)
}
//SaveLocalBlock 记录本地新区块
func (bs *TronBlockScanner) SaveLocalBlock(block *Block) error {
//db, err := storm.Open(filepath.Join(bs.wm.Config.dbPath, bs.wm.Config.BlockchainFile))
//if err != nil {
// return
//}
//defer db.Close()
//
//db.Save(block)
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
header := &openwallet.BlockHeader{
Hash: block.Hash,
Merkleroot: "",
Previousblockhash: block.Previousblockhash,
Height: block.Height,
Time: uint64(block.Time),
Symbol: bs.wm.Symbol(),
}
return bs.BlockchainDAI.SaveLocalBlockHead(header)
}
//DeleteUnscanRecord 删除指定高度的未扫记录
func (bs *TronBlockScanner) DeleteUnscanRecord(height uint64) error {
////获取本地区块高度
//db, err := storm.Open(filepath.Join(bs.wm.Config.dbPath, bs.wm.Config.BlockchainFile))
//if err != nil {
// return err
//}
//defer db.Close()
//
//var list []*UnscanRecord
//err = db.Find("BlockHeight", height, &list)
//if err != nil {
// return err
//}
//
//for _, r := range list {
// db.DeleteStruct(r)
//}
//
//return nil
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.DeleteUnscanRecordByHeight(height, bs.wm.Symbol())
}
func (bs *TronBlockScanner) GetUnscanRecords() ([]*openwallet.UnscanRecord, error) {
if bs.BlockchainDAI == nil {
return nil, fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.GetUnscanRecords(bs.wm.Symbol())
}