forked from lightningnetwork/lnd
-
Notifications
You must be signed in to change notification settings - Fork 24
/
dcrlnd_assertions.go
162 lines (140 loc) · 4.75 KB
/
dcrlnd_assertions.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
package itest
import (
"context"
"fmt"
"time"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrlnd/lnrpc"
"github.com/decred/dcrlnd/lntest"
"github.com/decred/dcrlnd/lntest/wait"
)
// assertCleanStateAliceBob ensures the state of the passed test nodes and the
// mempool are in a clean state (no open channels, no txs in the mempool, etc).
func assertCleanStateAliceBob(h *harnessTest, alice, bob *lntest.HarnessNode, net *lntest.NetworkHarness) {
_, minerHeight, err := net.Miner.Node.GetBestBlock(context.TODO())
if err != nil {
h.Fatalf("unable to get best height: %v", err)
}
net.EnsureConnected(h.t, alice, bob)
assertNodeBlockHeight(h, alice, int32(minerHeight))
assertNodeBlockHeight(h, bob, int32(minerHeight))
assertNodeNumChannels(h, alice, 0)
assertNumPendingChannels(h, alice, 0, 0, 0, 0)
assertNodeNumChannels(h, bob, 0)
assertNumPendingChannels(h, bob, 0, 0, 0, 0)
assertNumUnminedUnspent(h, alice, 0)
assertNumUnminedUnspent(h, bob, 0)
waitForNTxsInMempool(net.Miner.Node, 0, minerMempoolTimeout)
}
// assertCleanState ensures the state of the main test nodes and the mempool
// are in a clean state (no open channels, no txs in the mempool, etc).
func assertCleanState(h *harnessTest, net *lntest.NetworkHarness) {
assertCleanStateAliceBob(h, net.Alice, net.Bob, net)
}
func assertNodeBlockHeight(t *harnessTest, node *lntest.HarnessNode, height int32) {
t.t.Helper()
err := wait.NoError(func() error {
ctxt, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
getInfoReq := &lnrpc.GetInfoRequest{}
getInfoResp, err := node.GetInfo(ctxt, getInfoReq)
if err != nil {
return err
}
if int32(getInfoResp.BlockHeight) != height {
return fmt.Errorf("unexpected block height for node %s: "+
"want=%d got=%d", node.Name(),
height, getInfoResp.BlockHeight)
}
return nil
}, defaultTimeout)
if err != nil {
t.Fatalf("failed to assert node block height: %v", err)
}
}
// recordedTxFee returns the tx fee recorded in the transaction itself (that
// is, sum(TxOut[].Value) - sum(TxIn[].ValueIn)). While this is not currently
// enforced by consensus rules and cannot be relied upon for validation
// purposes, it's sufficient for testing purposes, assuming the procedure that
// generated the transaction correctly fills the ValueIn (which should be true
// for transactions produced by dcrlnd).
func recordedTxFee(tx *wire.MsgTx) int64 {
var amountIn, amountOut int64
for _, in := range tx.TxIn {
amountIn += in.ValueIn
}
for _, out := range tx.TxOut {
amountOut += out.Value
}
return amountIn - amountOut
}
// waitForPendingHtlcs waits for up to 15 seconds for the given channel in the
// given node to show the specified number of pending HTLCs.
func waitForPendingHtlcs(node *lntest.HarnessNode,
chanPoint *lnrpc.ChannelPoint, pendingHtlcs int) error {
fundingTxID, err := chainhash.NewHash(chanPoint.GetFundingTxidBytes())
if err != nil {
return fmt.Errorf("unable to convert funding txid into "+
"chainhash.Hash: %v", err)
}
outPoint := wire.OutPoint{
Hash: *fundingTxID,
Index: chanPoint.OutputIndex,
}
targetChan := outPoint.String()
req := &lnrpc.ListChannelsRequest{}
ctxb := context.Background()
var predErr error
wait.Predicate(func() bool {
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
channelInfo, err := node.ListChannels(ctxt, req)
if err != nil {
predErr = err
return false
}
for _, channel := range channelInfo.Channels {
if channel.ChannelPoint != targetChan {
continue
}
foundHtlcs := len(channel.PendingHtlcs)
if foundHtlcs == pendingHtlcs {
predErr = nil
return true
}
predErr = fmt.Errorf("found only %d htlcs (wanted %d)",
foundHtlcs, pendingHtlcs)
return false
}
predErr = fmt.Errorf("could not find channel %s", targetChan)
return false
}, time.Second*15)
return predErr
}
func assertNumUnminedUnspent(t *harnessTest, node *lntest.HarnessNode, expected int) {
err := wait.NoError(func() error {
ctxb := context.Background()
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
utxoReq := &lnrpc.ListUnspentRequest{}
utxoResp, err := node.ListUnspent(ctxt, utxoReq)
if err != nil {
return fmt.Errorf("unable to query utxos: %v", err)
}
actual := len(utxoResp.Utxos)
if actual != expected {
return fmt.Errorf("node %s has wrong number of unmined utxos ("+
"expected %d actual %d)", node.Name(), expected, actual)
}
return nil
}, defaultTimeout)
if err != nil {
t.Fatalf("failed asserting nb of unmined unspent: %v", err)
}
}
func chanPointFundingToOutpoint(cp *lnrpc.ChannelPoint) wire.OutPoint {
txId, err := lnrpc.GetChanPointFundingTxid(cp)
if err != nil {
panic(err)
}
return wire.OutPoint{Hash: *txId, Index: cp.OutputIndex}
}