/
utxo_test.go
133 lines (123 loc) · 2.87 KB
/
utxo_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
package db
import (
"bytes"
"database/sql"
"encoding/hex"
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"strconv"
"sync"
"testing"
)
var uxdb UtxoDB
var utxo wallet.Utxo
func init() {
conn, _ := sql.Open("sqlite3", ":memory:")
initDatabaseTables(conn)
uxdb = UtxoDB{
db: conn,
lock: new(sync.RWMutex),
}
sh1, _ := chainhash.NewHashFromStr("e941e1c32b3dd1a68edc3af9f7fe711f35aaca60f758c2dd49561e45ca2c41c0")
outpoint := wire.NewOutPoint(sh1, 0)
utxo = wallet.Utxo{
Op: *outpoint,
AtHeight: 300000,
Value: 100000000,
ScriptPubkey: []byte("scriptpubkey"),
WatchOnly: false,
}
}
func TestUtxoPut(t *testing.T) {
err := uxdb.Put(utxo)
if err != nil {
t.Error(err)
}
stmt, _ := uxdb.db.Prepare("select outpoint, value, height, scriptPubKey from utxos where outpoint=?")
defer stmt.Close()
var outpoint string
var value int
var height int
var scriptPubkey string
o := utxo.Op.Hash.String() + ":" + strconv.Itoa(int(utxo.Op.Index))
err = stmt.QueryRow(o).Scan(&outpoint, &value, &height, &scriptPubkey)
if err != nil {
t.Error(err)
}
if outpoint != o {
t.Error("Utxo db returned wrong outpoint")
}
if value != int(utxo.Value) {
t.Error("Utxo db returned wrong value")
}
if height != int(utxo.AtHeight) {
t.Error("Utxo db returned wrong height")
}
if scriptPubkey != hex.EncodeToString(utxo.ScriptPubkey) {
t.Error("Utxo db returned wrong scriptPubKey")
}
}
func TestUtxoGetAll(t *testing.T) {
err := uxdb.Put(utxo)
if err != nil {
t.Error(err)
}
utxos, err := uxdb.GetAll()
if err != nil {
t.Error(err)
}
if utxos[0].Op.Hash.String() != utxo.Op.Hash.String() {
t.Error("Utxo db returned wrong outpoint hash")
}
if utxos[0].Op.Index != utxo.Op.Index {
t.Error("Utxo db returned wrong outpoint index")
}
if utxos[0].Value != utxo.Value {
t.Error("Utxo db returned wrong value")
}
if utxos[0].AtHeight != utxo.AtHeight {
t.Error("Utxo db returned wrong height")
}
if !bytes.Equal(utxos[0].ScriptPubkey, utxo.ScriptPubkey) {
t.Error("Utxo db returned wrong scriptPubKey")
}
}
func TestSetWatchOnlyUtxo(t *testing.T) {
err := uxdb.Put(utxo)
if err != nil {
t.Error(err)
}
err = uxdb.SetWatchOnly(utxo)
if err != nil {
t.Error(err)
}
stmt, _ := uxdb.db.Prepare("select watchOnly from utxos where outpoint=?")
defer stmt.Close()
var watchOnlyInt int
o := utxo.Op.Hash.String() + ":" + strconv.Itoa(int(utxo.Op.Index))
err = stmt.QueryRow(o).Scan(&watchOnlyInt)
if err != nil {
t.Error(err)
}
if watchOnlyInt != 1 {
t.Error("Utxo freeze failed")
}
}
func TestDeleteUtxo(t *testing.T) {
err := uxdb.Put(utxo)
if err != nil {
t.Error(err)
}
err = uxdb.Delete(utxo)
if err != nil {
t.Error(err)
}
utxos, err := uxdb.GetAll()
if err != nil {
t.Error(err)
}
if len(utxos) != 0 {
t.Error("Utxo db delete failed")
}
}