/
blockscanner_db.go
121 lines (93 loc) · 3.22 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
/*
* 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 metaverse
import (
"fmt"
"github.com/blocktree/openwallet/v2/openwallet"
)
//SaveLocalBlockHead 记录区块高度和hash到本地
func (bs *ETPBlockScanner) 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 *ETPBlockScanner) 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 *ETPBlockScanner) SaveLocalBlock(blockHeader *Block) error {
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
header := &openwallet.BlockHeader{
Hash: blockHeader.Hash,
Merkleroot: blockHeader.Merkleroot,
Previousblockhash: blockHeader.Previousblockhash,
Height: blockHeader.Height,
Time: uint64(blockHeader.Time),
Symbol: bs.wm.Symbol(),
}
return bs.BlockchainDAI.SaveLocalBlockHead(header)
}
//GetLocalBlock 获取本地区块数据
func (bs *ETPBlockScanner) GetLocalBlock(height uint64) (*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 := &Block{
Hash: header.Hash,
Height: header.Height,
}
return block, nil
}
//SaveUnscanRecord 保存交易记录到钱包数据库
func (bs *ETPBlockScanner) 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 *ETPBlockScanner) DeleteUnscanRecord(height uint64) error {
if bs.BlockchainDAI == nil {
return fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.DeleteUnscanRecordByHeight(height, bs.wm.Symbol())
}
func (bs *ETPBlockScanner) GetUnscanRecords() ([]*openwallet.UnscanRecord, error) {
if bs.BlockchainDAI == nil {
return nil, fmt.Errorf("Blockchain DAI is not setup ")
}
return bs.BlockchainDAI.GetUnscanRecords(bs.wm.Symbol())
}