github.com/decred/dcrlnd@v0.7.6/chanacceptor/merge_test.go (about) 1 package chanacceptor 2 3 import ( 4 "testing" 5 6 "github.com/decred/dcrlnd/lnwire" 7 "github.com/stretchr/testify/require" 8 ) 9 10 // TestMergeResponse tests merging of channel acceptor responses. 11 func TestMergeResponse(t *testing.T) { 12 var ( 13 addr1 = lnwire.DeliveryAddress{1} 14 addr2 = lnwire.DeliveryAddress{2} 15 16 populatedResp = ChannelAcceptResponse{ 17 UpfrontShutdown: addr1, 18 CSVDelay: 2, 19 Reserve: 3, 20 InFlightTotal: 4, 21 HtlcLimit: 5, 22 MinHtlcIn: 6, 23 MinAcceptDepth: 7, 24 } 25 ) 26 27 tests := []struct { 28 name string 29 current ChannelAcceptResponse 30 new ChannelAcceptResponse 31 merged ChannelAcceptResponse 32 err error 33 }{ 34 { 35 name: "same response", 36 current: populatedResp, 37 new: populatedResp, 38 merged: populatedResp, 39 err: nil, 40 }, 41 { 42 name: "different upfront", 43 current: ChannelAcceptResponse{ 44 UpfrontShutdown: addr1, 45 }, 46 new: ChannelAcceptResponse{ 47 UpfrontShutdown: addr2, 48 }, 49 err: fieldMismatchError(fieldUpfrontShutdown, addr1, addr2), 50 }, 51 { 52 name: "different csv", 53 current: ChannelAcceptResponse{ 54 CSVDelay: 1, 55 }, 56 new: ChannelAcceptResponse{ 57 CSVDelay: 2, 58 }, 59 err: fieldMismatchError(fieldCSV, 1, 2), 60 }, 61 { 62 name: "different reserve", 63 current: ChannelAcceptResponse{ 64 Reserve: 1, 65 }, 66 new: ChannelAcceptResponse{ 67 Reserve: 2, 68 }, 69 err: fieldMismatchError(fieldReserve, 1, 2), 70 }, 71 { 72 name: "different in flight", 73 current: ChannelAcceptResponse{ 74 InFlightTotal: 1, 75 }, 76 new: ChannelAcceptResponse{ 77 InFlightTotal: 2, 78 }, 79 err: fieldMismatchError( 80 fieldInFlightTotal, lnwire.MilliAtom(1), 81 lnwire.MilliAtom(2), 82 ), 83 }, 84 { 85 name: "different htlc limit", 86 current: ChannelAcceptResponse{ 87 HtlcLimit: 1, 88 }, 89 new: ChannelAcceptResponse{ 90 HtlcLimit: 2, 91 }, 92 err: fieldMismatchError(fieldHtlcLimit, 1, 2), 93 }, 94 { 95 name: "different min in", 96 current: ChannelAcceptResponse{ 97 MinHtlcIn: 1, 98 }, 99 new: ChannelAcceptResponse{ 100 MinHtlcIn: 2, 101 }, 102 err: fieldMismatchError( 103 fieldMinIn, lnwire.MilliAtom(1), 104 lnwire.MilliAtom(2), 105 ), 106 }, 107 { 108 name: "different depth", 109 current: ChannelAcceptResponse{ 110 MinAcceptDepth: 1, 111 }, 112 new: ChannelAcceptResponse{ 113 MinAcceptDepth: 2, 114 }, 115 err: fieldMismatchError(fieldMinDep, 1, 2), 116 }, 117 { 118 name: "merge all values", 119 current: ChannelAcceptResponse{ 120 UpfrontShutdown: lnwire.DeliveryAddress{1}, 121 CSVDelay: 1, 122 Reserve: 0, 123 InFlightTotal: 3, 124 HtlcLimit: 0, 125 MinHtlcIn: 5, 126 MinAcceptDepth: 0, 127 }, 128 new: ChannelAcceptResponse{ 129 UpfrontShutdown: nil, 130 CSVDelay: 0, 131 Reserve: 2, 132 InFlightTotal: 0, 133 HtlcLimit: 4, 134 MinHtlcIn: 0, 135 MinAcceptDepth: 6, 136 }, 137 merged: ChannelAcceptResponse{ 138 UpfrontShutdown: lnwire.DeliveryAddress{1}, 139 CSVDelay: 1, 140 Reserve: 2, 141 InFlightTotal: 3, 142 HtlcLimit: 4, 143 MinHtlcIn: 5, 144 MinAcceptDepth: 6, 145 }, 146 err: nil, 147 }, 148 { 149 // Test the case where fields have the same non-zero 150 // value, and the case where only response value is 151 // non-zero. 152 name: "empty and identical", 153 current: ChannelAcceptResponse{ 154 CSVDelay: 1, 155 Reserve: 2, 156 InFlightTotal: 0, 157 }, 158 new: ChannelAcceptResponse{ 159 CSVDelay: 0, 160 Reserve: 2, 161 InFlightTotal: 3, 162 }, 163 merged: ChannelAcceptResponse{ 164 CSVDelay: 1, 165 Reserve: 2, 166 InFlightTotal: 3, 167 }, 168 err: nil, 169 }, 170 } 171 172 for _, test := range tests { 173 test := test 174 175 t.Run(test.name, func(t *testing.T) { 176 resp, err := mergeResponse(test.current, test.new) 177 require.Equal(t, test.err, err) 178 179 // If we expect an error, exit early rather than compare 180 // our result. 181 if test.err != nil { 182 return 183 } 184 185 require.Equal(t, test.merged, resp) 186 }) 187 } 188 }