forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
255 lines (226 loc) · 5.4 KB
/
example_test.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) 2015 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wtxmgr
import (
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcwallet/walletdb"
)
var (
// Spends: bogus
// Outputs: 10 BTC
exampleTxRecordA *TxRecord
// Spends: A:0
// Outputs: 5 BTC, 5 BTC
exampleTxRecordB *TxRecord
)
func init() {
tx := spendOutput(&chainhash.Hash{}, 0, 10e8)
rec, err := NewTxRecordFromMsgTx(tx, timeNow())
if err != nil {
panic(err)
}
exampleTxRecordA = rec
tx = spendOutput(&exampleTxRecordA.Hash, 0, 5e8, 5e8)
rec, err = NewTxRecordFromMsgTx(tx, timeNow())
if err != nil {
panic(err)
}
exampleTxRecordB = rec
}
var exampleBlock100 = makeBlockMeta(100)
// This example demonstrates reporting the Store balance given an unmined and
// mined transaction given 0, 1, and 6 block confirmations.
func ExampleStore_Balance() {
s, db, teardown, err := testStore()
defer teardown()
if err != nil {
fmt.Println(err)
return
}
// Prints balances for 0 block confirmations, 1 confirmation, and 6
// confirmations.
printBalances := func(syncHeight int32) {
dbtx, err := db.BeginReadTx()
if err != nil {
fmt.Println(err)
return
}
defer dbtx.Rollback()
ns := dbtx.ReadBucket(namespaceKey)
zeroConfBal, err := s.Balance(ns, 0, syncHeight)
if err != nil {
fmt.Println(err)
return
}
oneConfBal, err := s.Balance(ns, 1, syncHeight)
if err != nil {
fmt.Println(err)
return
}
sixConfBal, err := s.Balance(ns, 6, syncHeight)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%v, %v, %v\n", zeroConfBal, oneConfBal, sixConfBal)
}
// Insert a transaction which outputs 10 BTC unmined and mark the output
// as a credit.
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(namespaceKey)
err := s.InsertTx(ns, exampleTxRecordA, nil)
if err != nil {
return err
}
return s.AddCredit(ns, exampleTxRecordA, nil, 0, false)
})
if err != nil {
fmt.Println(err)
return
}
printBalances(100)
// Mine the transaction in block 100 and print balances again with a
// sync height of 100 and 105 blocks.
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(namespaceKey)
return s.InsertTx(ns, exampleTxRecordA, &exampleBlock100)
})
if err != nil {
fmt.Println(err)
return
}
printBalances(100)
printBalances(105)
// Output:
// 10 BTC, 0 BTC, 0 BTC
// 10 BTC, 10 BTC, 0 BTC
// 10 BTC, 10 BTC, 10 BTC
}
func ExampleStore_Rollback() {
s, db, teardown, err := testStore()
defer teardown()
if err != nil {
fmt.Println(err)
return
}
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
ns := tx.ReadWriteBucket(namespaceKey)
// Insert a transaction which outputs 10 BTC in a block at height 100.
err := s.InsertTx(ns, exampleTxRecordA, &exampleBlock100)
if err != nil {
return err
}
// Rollback everything from block 100 onwards.
err = s.Rollback(ns, 100)
if err != nil {
return err
}
// Assert that the transaction is now unmined.
details, err := s.TxDetails(ns, &exampleTxRecordA.Hash)
if err != nil {
return err
}
if details == nil {
return fmt.Errorf("no details found")
}
fmt.Println(details.Block.Height)
return nil
})
if err != nil {
fmt.Println(err)
return
}
// Output:
// -1
}
func Example_basicUsage() {
// Open the database.
db, dbTeardown, err := testDB()
defer dbTeardown()
if err != nil {
fmt.Println(err)
return
}
// Open a read-write transaction to operate on the database.
dbtx, err := db.BeginReadWriteTx()
if err != nil {
fmt.Println(err)
return
}
defer dbtx.Commit()
// Create a bucket for the transaction store.
b, err := dbtx.CreateTopLevelBucket([]byte("txstore"))
if err != nil {
fmt.Println(err)
return
}
// Create and open the transaction store in the provided namespace.
err = Create(b)
if err != nil {
fmt.Println(err)
return
}
s, err := Open(b, &chaincfg.TestNet3Params)
if err != nil {
fmt.Println(err)
return
}
// Insert an unmined transaction that outputs 10 BTC to a wallet address
// at output 0.
err = s.InsertTx(b, exampleTxRecordA, nil)
if err != nil {
fmt.Println(err)
return
}
err = s.AddCredit(b, exampleTxRecordA, nil, 0, false)
if err != nil {
fmt.Println(err)
return
}
// Insert a second transaction which spends the output, and creates two
// outputs. Mark the second one (5 BTC) as wallet change.
err = s.InsertTx(b, exampleTxRecordB, nil)
if err != nil {
fmt.Println(err)
return
}
err = s.AddCredit(b, exampleTxRecordB, nil, 1, true)
if err != nil {
fmt.Println(err)
return
}
// Mine each transaction in a block at height 100.
err = s.InsertTx(b, exampleTxRecordA, &exampleBlock100)
if err != nil {
fmt.Println(err)
return
}
err = s.InsertTx(b, exampleTxRecordB, &exampleBlock100)
if err != nil {
fmt.Println(err)
return
}
// Print the one confirmation balance.
bal, err := s.Balance(b, 1, 100)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(bal)
// Fetch unspent outputs.
utxos, err := s.UnspentOutputs(b)
if err != nil {
fmt.Println(err)
}
expectedOutPoint := wire.OutPoint{Hash: exampleTxRecordB.Hash, Index: 1}
for _, utxo := range utxos {
fmt.Println(utxo.OutPoint == expectedOutPoint)
}
// Output:
// 5 BTC
// true
}