-
Notifications
You must be signed in to change notification settings - Fork 80
/
sortsignsend_test.go
78 lines (72 loc) · 2 KB
/
sortsignsend_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
package spvwallet
import (
"bytes"
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"os"
"testing"
)
func MockWallet() *SPVWallet {
txstore, _ := createTxStore()
peerCfg := &PeerManagerConfig{
UserAgentVersion: WALLET_VERSION,
Params: &chaincfg.TestNet3Params,
}
bc, _ := NewBlockchain("", MockCreationTime, &chaincfg.TestNet3Params)
createBlockChain(bc)
peerManager, _ := NewPeerManager(peerCfg)
return &SPVWallet{txstore: txstore, peerManager: peerManager, blockchain: bc, keyManager: txstore.keyManager, params: &chaincfg.TestNet3Params}
}
func Test_gatherCoins(t *testing.T) {
w := MockWallet()
h1, err := chainhash.NewHashFromStr("6f7a58ad92702601fcbaac0e039943a384f5274a205c16bb8bbab54f9ea2fbad")
if err != nil {
t.Error(err)
}
key1, err := w.keyManager.GetFreshKey(wallet.EXTERNAL)
if err != nil {
t.Error(err)
}
addr1, err := key1.Address(&chaincfg.TestNet3Params)
if err != nil {
t.Error(err)
}
script1, err := w.AddressToScript(addr1)
if err != nil {
t.Error(err)
}
op := wire.NewOutPoint(h1, 0)
err = w.txstore.Utxos().Put(wallet.Utxo{Op: *op, ScriptPubkey: script1, AtHeight: 5, Value: 10000})
if err != nil {
t.Error(err)
}
coinmap := w.gatherCoins()
for coin, key := range coinmap {
if !bytes.Equal(coin.PkScript(), script1) {
t.Error("Pubkey script in coin is incorrect")
}
if coin.Index() != 0 {
t.Error("Returned incorrect index")
}
if !coin.Hash().IsEqual(h1) {
t.Error("Returned incorrect hash")
}
height, _ := w.blockchain.db.Height()
if coin.NumConfs() != int64(height-5) {
t.Error("Returned incorrect number of confirmations")
}
if coin.Value() != 10000 {
t.Error("Returned incorrect coin value")
}
addr2, err := key.Address(&chaincfg.TestNet3Params)
if err != nil {
t.Error(err)
}
if addr2.EncodeAddress() != addr1.EncodeAddress() {
t.Error("Returned incorrect key")
}
}
os.Remove("headers.bin")
}