/
models.go
237 lines (207 loc) · 7.12 KB
/
models.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
233
234
235
236
237
/*
* 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 hypercash
import (
"fmt"
"github.com/blocktree/openwallet/v2/crypto"
"github.com/blocktree/openwallet/v2/openwallet"
"github.com/ethereum/go-ethereum/common"
"github.com/tidwall/gjson"
)
//BlockchainInfo 本地节点区块链信息
type BlockchainInfo struct {
Chain string `json:"chain"`
Blocks uint64 `json:"blocks"`
Headers uint64 `json:"headers"`
Bestblockhash string `json:"bestblockhash"`
Difficulty string `json:"difficulty"`
Mediantime uint64 `json:"mediantime"`
Verificationprogress string `json:"verificationprogress"`
Chainwork string `json:"chainwork"`
Pruned bool `json:"pruned"`
}
func NewBlockchainInfo(json *gjson.Result) *BlockchainInfo {
b := &BlockchainInfo{}
//解析json
b.Chain = gjson.Get(json.Raw, "chain").String()
b.Blocks = gjson.Get(json.Raw, "blocks").Uint()
b.Headers = gjson.Get(json.Raw, "headers").Uint()
b.Bestblockhash = gjson.Get(json.Raw, "bestblockhash").String()
b.Difficulty = gjson.Get(json.Raw, "difficulty").String()
b.Mediantime = gjson.Get(json.Raw, "mediantime").Uint()
b.Verificationprogress = gjson.Get(json.Raw, "verificationprogress").String()
b.Chainwork = gjson.Get(json.Raw, "chainwork").String()
b.Pruned = gjson.Get(json.Raw, "pruned").Bool()
return b
}
//Unspent 未花记录
type Unspent struct {
/*
{
"txid" : "d54994ece1d11b19785c7248868696250ab195605b469632b7bd68130e880c9a",
"vout" : 1,
"address" : "mgnucj8nYqdrPFh2JfZSB1NmUThUGnmsqe",
"account" : "test label",
"scriptPubKey" : "76a9140dfc8bafc8419853b34d5e072ad37d1a5159f58488ac",
"amount" : 0.00010000,
"confirmations" : 6210,
"spendable" : true,
"solvable" : true
}
*/
Key string `storm:"id"`
TxID string `json:"txid"`
Vout uint64 `json:"vout"`
Address string `json:"address"`
AccountID string `json:"account" storm:"index"`
ScriptPubKey string `json:"scriptPubKey"`
Amount string `json:"amount"`
Confirmations uint64 `json:"confirmations"`
Spendable bool `json:"spendable"`
Solvable bool `json:"solvable"`
HDAddress openwallet.Address
}
func NewUnspent(json *gjson.Result) *Unspent {
obj := &Unspent{}
//解析json
obj.TxID = gjson.Get(json.Raw, "txid").String()
obj.Vout = gjson.Get(json.Raw, "vout").Uint()
obj.Address = gjson.Get(json.Raw, "address").String()
obj.AccountID = gjson.Get(json.Raw, "account").String()
obj.ScriptPubKey = gjson.Get(json.Raw, "scriptPubKey").String()
obj.Amount = gjson.Get(json.Raw, "amount").String()
obj.Confirmations = gjson.Get(json.Raw, "confirmations").Uint()
//obj.Spendable = gjson.Get(json.Raw, "spendable").Bool()
obj.Spendable = true
obj.Solvable = gjson.Get(json.Raw, "solvable").Bool()
return obj
}
type UnspentSort struct {
values []*Unspent
comparator func(a, b *Unspent) int
}
func (s UnspentSort) Len() int {
return len(s.values)
}
func (s UnspentSort) Swap(i, j int) {
s.values[i], s.values[j] = s.values[j], s.values[i]
}
func (s UnspentSort) Less(i, j int) bool {
return s.comparator(s.values[i], s.values[j]) < 0
}
//type Address struct {
// Address string `json:"address" storm:"id"`
// Account string `json:"account" storm:"index"`
// HDPath string `json:"hdpath"`
// CreatedAt time.Time
//}
type User struct {
UserKey string `storm:"id"` // primary key
Group string `storm:"index"` // this field will be indexed
Email string `storm:"unique"` // this field will be indexed with a unique constraint
Name string // this field will not be indexed
Age int `storm:"index"`
}
type Block struct {
/*
"hash": "000000000000000127454a8c91e74cf93ad76752cceb7eb3bcff0c398ba84b1f",
"confirmations": 2,
"strippedsize": 191875,
"size": 199561,
"weight": 775186,
"height": 1354760,
"version": 536870912,
"versionHex": "20000000",
"merkleroot": "48239e76f8b37d9c8824fef93d42ac3d7c433029c1e9fa23b6416dd0356f3e57",
"tx": ["c1e12febeb58aefb0b01c04360262138f4ee0faeb207276e79ea3866608ed84f"]
"time": 1532143012,
"mediantime": 1532140298,
"nonce": 3410287696,
"bits": "19499855",
"difficulty": 58358570.79038175,
"chainwork": "00000000000000000000000000000000000000000000006f68c43926cd6c2d1f",
"previousblockhash": "00000000000000292d142fcc1ddbd9dafd4518310009f152bdca2a66cc589f97",
"nextblockhash": "0000000000004a50ef5733ab333f718e6ef5c1995e2cfd5a7caa0875f118cd30"
*/
Hash string
Confirmations uint64
Merkleroot string
tx []string
stx []string
Previousblockhash string
Height uint64 `storm:"id"`
Version uint64
Time uint64
Fork bool
}
func NewBlock(json *gjson.Result) *Block {
obj := &Block{}
//解析json
obj.Hash = gjson.Get(json.Raw, "hash").String()
obj.Confirmations = gjson.Get(json.Raw, "confirmations").Uint()
obj.Merkleroot = gjson.Get(json.Raw, "merkleroot").String()
txs := make([]string, 0)
for _, tx := range gjson.Get(json.Raw, "tx").Array() {
txs = append(txs, tx.String())
}
stxs := make([]string, 0)
for _, tx := range gjson.Get(json.Raw, "stx").Array() {
stxs = append(txs, tx.String())
}
obj.tx = txs
obj.stx = stxs
obj.Previousblockhash = gjson.Get(json.Raw, "previousblockhash").String()
obj.Height = gjson.Get(json.Raw, "height").Uint()
obj.Version = gjson.Get(json.Raw, "version").Uint()
obj.Time = gjson.Get(json.Raw, "time").Uint()
return obj
}
//BlockHeader 区块链头
func (b *Block) BlockHeader() *openwallet.BlockHeader {
obj := openwallet.BlockHeader{}
//解析json
obj.Hash = b.Hash
obj.Confirmations = b.Confirmations
obj.Merkleroot = b.Merkleroot
obj.Previousblockhash = b.Previousblockhash
obj.Height = b.Height
obj.Version = b.Version
obj.Time = b.Time
obj.Symbol = Symbol
return &obj
}
//maxRescanCount 记录最大重扫次数
const maxRescanCount = 1000
//UnscanRecords 扫描失败的区块及交易
type UnscanRecord struct {
ID string `storm:"id"` // primary key
BlockHeight uint64
TxID string
Reason string
RescanCount int
}
func NewUnscanRecord(height uint64, txID, reason string) *UnscanRecord {
obj := UnscanRecord{}
obj.BlockHeight = height
obj.TxID = txID
obj.Reason = reason
obj.ID = common.Bytes2Hex(crypto.SHA256([]byte(fmt.Sprintf("%d_%s", height, txID))))
return &obj
}
type FloatStr string
func (n FloatStr) MarshalJSON() ([]byte, error) {
// There are probably better ways to do it. It is just an example
return []byte(fmt.Sprintf("%s", n)), nil
}