forked from btcsuite/btcd
-
Notifications
You must be signed in to change notification settings - Fork 25
/
merge.go
152 lines (125 loc) · 3.49 KB
/
merge.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
package chanacceptor
import (
"bytes"
"github.com/pkt-cash/pktd/btcutil"
"github.com/pkt-cash/pktd/btcutil/er"
"github.com/pkt-cash/pktd/lnd/lnwire"
)
const (
// We use field names in our errors for more readable errors. Create
// consts for them here so that we can exactly match in our unit tests.
fieldCSV = "csv delay"
fieldHtlcLimit = "htlc limit"
fieldMinDep = "min depth"
fieldReserve = "reserve"
fieldMinIn = "min htlc in"
fieldInFlightTotal = "in flight total"
fieldUpfrontShutdown = "upfront shutdown"
)
// fieldMismatchError returns a merge error for a named field when we get two
// channel acceptor responses which have different values set.
func fieldMismatchError(name string, current, new interface{}) er.R {
return er.Errorf("multiple values set for: %v, %v and %v",
name, current, new)
}
// mergeInt64 merges two int64 values, failing if they have different non-zero
// values.
func mergeInt64(name string, current, new int64) (int64, er.R) {
switch {
case current == 0:
return new, nil
case new == 0:
return current, nil
case current != new:
return 0, fieldMismatchError(name, current, new)
default:
return new, nil
}
}
// mergeMillisatoshi merges two msat values, failing if they have different
// non-zero values.
func mergeMillisatoshi(name string, current,
new lnwire.MilliSatoshi) (lnwire.MilliSatoshi, er.R) {
switch {
case current == 0:
return new, nil
case new == 0:
return current, nil
case current != new:
return 0, fieldMismatchError(name, current, new)
default:
return new, nil
}
}
// mergeDeliveryAddress merges two delivery address values, failing if they have
// different non-zero values.
func mergeDeliveryAddress(name string, current,
new lnwire.DeliveryAddress) (lnwire.DeliveryAddress, er.R) {
switch {
case current == nil:
return new, nil
case new == nil:
return current, nil
case !bytes.Equal(current, new):
return nil, fieldMismatchError(name, current, new)
default:
return new, nil
}
}
// mergeResponse takes two channel accept responses, and attempts to merge their
// fields, failing if any fields conflict (are non-zero and not equal). It
// returns a new response that has all the merged fields in it.
func mergeResponse(current, new ChannelAcceptResponse) (ChannelAcceptResponse,
er.R) {
csv, err := mergeInt64(
fieldCSV, int64(current.CSVDelay), int64(new.CSVDelay),
)
if err != nil {
return current, err
}
current.CSVDelay = uint16(csv)
htlcLimit, err := mergeInt64(
fieldHtlcLimit, int64(current.HtlcLimit),
int64(new.HtlcLimit),
)
if err != nil {
return current, err
}
current.HtlcLimit = uint16(htlcLimit)
minDepth, err := mergeInt64(
fieldMinDep, int64(current.MinAcceptDepth),
int64(new.MinAcceptDepth),
)
if err != nil {
return current, err
}
current.MinAcceptDepth = uint16(minDepth)
reserve, err := mergeInt64(
fieldReserve, int64(current.Reserve), int64(new.Reserve),
)
if err != nil {
return current, err
}
current.Reserve = btcutil.Amount(reserve)
current.MinHtlcIn, err = mergeMillisatoshi(
fieldMinIn, current.MinHtlcIn, new.MinHtlcIn,
)
if err != nil {
return current, err
}
current.InFlightTotal, err = mergeMillisatoshi(
fieldInFlightTotal, current.InFlightTotal,
new.InFlightTotal,
)
if err != nil {
return current, err
}
current.UpfrontShutdown, err = mergeDeliveryAddress(
fieldUpfrontShutdown, current.UpfrontShutdown,
new.UpfrontShutdown,
)
if err != nil {
return current, err
}
return current, nil
}