/
subscribe.go
133 lines (105 loc) · 3.19 KB
/
subscribe.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
/*
* 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 openw
import (
"github.com/nbit99/openwallet/v2/log"
"github.com/nbit99/openwallet/v2/openwallet"
)
//blockScanNotify 区块扫描结果通知
func (wm *WalletManager) BlockScanNotify(header *openwallet.BlockHeader) error {
//log.Debug("NewBlock:", header)
if header.Fork {
//加载已存在所有app
appIDs, err := wm.loadAllAppIDs()
if err != nil {
return err
}
//分叉的区块,删除提出记录
for _, appID := range appIDs {
wrapper, err := wm.NewWalletWrapper(appID, "")
if err != nil {
return err
}
txWrapper := NewTransactionWrapper(wrapper)
err = txWrapper.DeleteBlockDataByHeight(header.Height)
if err != nil {
return err
}
}
return nil
}
//推送数据
for o, _ := range wm.observers {
o.BlockScanNotify(header)
}
//TODO:定时删除过时的记录,保证数据库不会无限增加
//可以由配置,自定义删除超过例如1000个块之前的记录
return nil
}
//BlockExtractDataNotify 区块提取结果通知
func (wm *WalletManager) BlockExtractDataNotify(sourceKey string, data *openwallet.TxExtractData) error {
//保存提取出来的数据
appID, accountID := wm.decodeSourceKey(sourceKey)
log.Debug("NewBlockExtractData:", appID, accountID)
wrapper, err := wm.NewWalletWrapper(appID, "")
if err != nil {
return err
}
txWrapper := NewTransactionWrapper(wrapper)
err = txWrapper.SaveBlockExtractData(accountID, data)
if err != nil {
return err
}
//更新账户余额
//err = wm.RefreshAssetsAccountBalance(appID, accountID)
//if err != nil {
// log.Error("RefreshAssetsAccountBalance error:", err)
//}
account, err := wrapper.GetAssetsAccountInfo(accountID)
if err != nil {
return err
}
for o, _ := range wm.observers {
o.BlockTxExtractDataNotify(account, data)
}
return nil
}
//BlockExtractSmartContractDataNotify 区块提取智能合约交易结果通知
//@param sourceKey: 为contractID
//@param data: 合约交易回执
//@required
func (wm *WalletManager) BlockExtractSmartContractDataNotify(sourceKey string, data *openwallet.SmartContractReceipt) error {
return nil
}
//DeleteRechargesByHeight 删除某区块高度的充值记录
func (wm *WalletManager) DeleteRechargesByHeight(height uint64) error {
//加载已存在所有app
appIDs, err := wm.loadAllAppIDs()
if err != nil {
return err
}
for _, appID := range appIDs {
wrapper, err := wm.NewWalletWrapper(appID, "")
if err != nil {
return err
}
txWrapper := NewTransactionWrapper(wrapper)
err = txWrapper.DeleteBlockDataByHeight(height)
if err != nil {
return err
}
}
return nil
}