/
blockscanner_db.go
138 lines (106 loc) · 3.72 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
/*
* 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 conflux
import (
"fmt"
"github.com/blocktree/openwallet/v2/openwallet"
cfxtypes "github.com/Conflux-Chain/go-conflux-sdk/types"
)
//SaveLocalBlockHead 记录区块高度和hash到本地
func (bs *BlockScanner) SaveLocalBlockHead(blockHeight uint64, blockHash string) error {
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)
}
//GetLocalBlockHead 获取本地记录的区块高度和hash
func (bs *BlockScanner) GetLocalBlockHead() (uint64, string, error) {
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
}
//SaveLocalBlock 记录本地新区块
func (bs *BlockScanner) SaveLocalBlock(blockHeader *cfxtypes.Block) error {
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
header := &openwallet.BlockHeader{
Hash: blockHeader.Hash.String(),
Previousblockhash: blockHeader.ParentHash.String(),
Height: blockHeader.Height.ToInt().Uint64(),
Symbol: bs.wm.Symbol(),
}
return bs.BlockchainDAI.SaveLocalBlockHead(header)
}
//GetLocalBlock 获取本地区块数据
func (bs *BlockScanner) GetLocalBlock(height uint64) (*cfxtypes.Block, error) {
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 := &cfxtypes.Block{
BlockHeader: cfxtypes.BlockHeader{
Hash: cfxtypes.Hash( header.Hash),
Height: cfxtypes.NewBigInt(header.Height),
},
}
return block, nil
}
//SaveUnscanRecord 保存交易记录到钱包数据库
func (bs *BlockScanner) SaveUnscanRecord(record *openwallet.UnscanRecord) error {
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.SaveUnscanRecord(record)
}
//DeleteUnscanRecord 删除指定高度的未扫记录
func (bs *BlockScanner) DeleteUnscanRecord(height uint64) error {
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.DeleteUnscanRecordByHeight(height, bs.wm.Symbol())
}
//DeleteUnscanRecordByID 删除指定的未扫记录
func (bs *BlockScanner) DeleteUnscanRecordByID(id string) error {
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.DeleteUnscanRecordByID(id, bs.wm.Symbol())
}
func (bs *BlockScanner) GetUnscanRecords() ([]*openwallet.UnscanRecord, error) {
if bs.BlockchainDAI == nil {
return nil, fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.GetUnscanRecords(bs.wm.Symbol())
}
//SupportBlockchainDAI 支持外部设置区块链数据访问接口
//@optional
func (bs *BlockScanner) SupportBlockchainDAI() bool {
return true
}