routing: rename mock structs to make them obsolete

This commit renames the mock structs by appending Old in their names. In
doing so the old tests stay unchanged and new mock structs can be added
in the following commit.
This commit is contained in:
yyforyongyu 2021-05-24 18:29:27 +08:00
parent 54ed6c271b
commit 289d97fbfb
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
3 changed files with 69 additions and 66 deletions

View File

@ -13,17 +13,17 @@ import (
"github.com/lightningnetwork/lnd/routing/route"
)
type mockPaymentAttemptDispatcher struct {
type mockPaymentAttemptDispatcherOld struct {
onPayment func(firstHop lnwire.ShortChannelID) ([32]byte, error)
results map[uint64]*htlcswitch.PaymentResult
sync.Mutex
}
var _ PaymentAttemptDispatcher = (*mockPaymentAttemptDispatcher)(nil)
var _ PaymentAttemptDispatcher = (*mockPaymentAttemptDispatcherOld)(nil)
func (m *mockPaymentAttemptDispatcher) SendHTLC(firstHop lnwire.ShortChannelID,
pid uint64,
func (m *mockPaymentAttemptDispatcherOld) SendHTLC(
firstHop lnwire.ShortChannelID, pid uint64,
_ *lnwire.UpdateAddHTLC) error {
if m.onPayment == nil {
@ -55,7 +55,7 @@ func (m *mockPaymentAttemptDispatcher) SendHTLC(firstHop lnwire.ShortChannelID,
return nil
}
func (m *mockPaymentAttemptDispatcher) GetPaymentResult(paymentID uint64,
func (m *mockPaymentAttemptDispatcherOld) GetPaymentResult(paymentID uint64,
_ lntypes.Hash, _ htlcswitch.ErrorDecrypter) (
<-chan *htlcswitch.PaymentResult, error) {
@ -73,48 +73,51 @@ func (m *mockPaymentAttemptDispatcher) GetPaymentResult(paymentID uint64,
return c, nil
}
func (m *mockPaymentAttemptDispatcher) CleanStore(map[uint64]struct{}) error {
func (m *mockPaymentAttemptDispatcherOld) CleanStore(
map[uint64]struct{}) error {
return nil
}
func (m *mockPaymentAttemptDispatcher) setPaymentResult(
func (m *mockPaymentAttemptDispatcherOld) setPaymentResult(
f func(firstHop lnwire.ShortChannelID) ([32]byte, error)) {
m.onPayment = f
}
type mockPaymentSessionSource struct {
type mockPaymentSessionSourceOld struct {
routes []*route.Route
routeRelease chan struct{}
}
var _ PaymentSessionSource = (*mockPaymentSessionSource)(nil)
var _ PaymentSessionSource = (*mockPaymentSessionSourceOld)(nil)
func (m *mockPaymentSessionSource) NewPaymentSession(
func (m *mockPaymentSessionSourceOld) NewPaymentSession(
_ *LightningPayment) (PaymentSession, error) {
return &mockPaymentSession{
return &mockPaymentSessionOld{
routes: m.routes,
release: m.routeRelease,
}, nil
}
func (m *mockPaymentSessionSource) NewPaymentSessionForRoute(
func (m *mockPaymentSessionSourceOld) NewPaymentSessionForRoute(
preBuiltRoute *route.Route) PaymentSession {
return nil
}
func (m *mockPaymentSessionSource) NewPaymentSessionEmpty() PaymentSession {
return &mockPaymentSession{}
func (m *mockPaymentSessionSourceOld) NewPaymentSessionEmpty() PaymentSession {
return &mockPaymentSessionOld{}
}
type mockMissionControl struct {
type mockMissionControlOld struct {
MissionControl
}
var _ MissionController = (*mockMissionControl)(nil)
var _ MissionController = (*mockMissionControlOld)(nil)
func (m *mockMissionControl) ReportPaymentFail(paymentID uint64, rt *route.Route,
func (m *mockMissionControlOld) ReportPaymentFail(
paymentID uint64, rt *route.Route,
failureSourceIdx *int, failure lnwire.FailureMessage) (
*channeldb.FailureReason, error) {
@ -128,19 +131,19 @@ func (m *mockMissionControl) ReportPaymentFail(paymentID uint64, rt *route.Route
return nil, nil
}
func (m *mockMissionControl) ReportPaymentSuccess(paymentID uint64,
func (m *mockMissionControlOld) ReportPaymentSuccess(paymentID uint64,
rt *route.Route) error {
return nil
}
func (m *mockMissionControl) GetProbability(fromNode, toNode route.Vertex,
func (m *mockMissionControlOld) GetProbability(fromNode, toNode route.Vertex,
amt lnwire.MilliSatoshi) float64 {
return 0
}
type mockPaymentSession struct {
type mockPaymentSessionOld struct {
routes []*route.Route
// release is a channel that optionally blocks requesting a route
@ -149,9 +152,9 @@ type mockPaymentSession struct {
release chan struct{}
}
var _ PaymentSession = (*mockPaymentSession)(nil)
var _ PaymentSession = (*mockPaymentSessionOld)(nil)
func (m *mockPaymentSession) RequestRoute(_, _ lnwire.MilliSatoshi,
func (m *mockPaymentSessionOld) RequestRoute(_, _ lnwire.MilliSatoshi,
_, height uint32) (*route.Route, error) {
if m.release != nil {
@ -168,27 +171,27 @@ func (m *mockPaymentSession) RequestRoute(_, _ lnwire.MilliSatoshi,
return r, nil
}
func (m *mockPaymentSession) UpdateAdditionalEdge(_ *lnwire.ChannelUpdate,
func (m *mockPaymentSessionOld) UpdateAdditionalEdge(_ *lnwire.ChannelUpdate,
_ *btcec.PublicKey, _ *channeldb.ChannelEdgePolicy) bool {
return false
}
func (m *mockPaymentSession) GetAdditionalEdgePolicy(_ *btcec.PublicKey,
func (m *mockPaymentSessionOld) GetAdditionalEdgePolicy(_ *btcec.PublicKey,
_ uint64) *channeldb.ChannelEdgePolicy {
return nil
}
type mockPayer struct {
type mockPayerOld struct {
sendResult chan error
paymentResult chan *htlcswitch.PaymentResult
quit chan struct{}
}
var _ PaymentAttemptDispatcher = (*mockPayer)(nil)
var _ PaymentAttemptDispatcher = (*mockPayerOld)(nil)
func (m *mockPayer) SendHTLC(_ lnwire.ShortChannelID,
func (m *mockPayerOld) SendHTLC(_ lnwire.ShortChannelID,
paymentID uint64,
_ *lnwire.UpdateAddHTLC) error {
@ -201,7 +204,7 @@ func (m *mockPayer) SendHTLC(_ lnwire.ShortChannelID,
}
func (m *mockPayer) GetPaymentResult(paymentID uint64, _ lntypes.Hash,
func (m *mockPayerOld) GetPaymentResult(paymentID uint64, _ lntypes.Hash,
_ htlcswitch.ErrorDecrypter) (<-chan *htlcswitch.PaymentResult, error) {
select {
@ -220,7 +223,7 @@ func (m *mockPayer) GetPaymentResult(paymentID uint64, _ lntypes.Hash,
}
}
func (m *mockPayer) CleanStore(pids map[uint64]struct{}) error {
func (m *mockPayerOld) CleanStore(pids map[uint64]struct{}) error {
return nil
}
@ -249,7 +252,7 @@ type testPayment struct {
attempts []channeldb.HTLCAttempt
}
type mockControlTower struct {
type mockControlTowerOld struct {
payments map[lntypes.Hash]*testPayment
successful map[lntypes.Hash]struct{}
failed map[lntypes.Hash]channeldb.FailureReason
@ -264,17 +267,17 @@ type mockControlTower struct {
sync.Mutex
}
var _ ControlTower = (*mockControlTower)(nil)
var _ ControlTower = (*mockControlTowerOld)(nil)
func makeMockControlTower() *mockControlTower {
return &mockControlTower{
func makeMockControlTower() *mockControlTowerOld {
return &mockControlTowerOld{
payments: make(map[lntypes.Hash]*testPayment),
successful: make(map[lntypes.Hash]struct{}),
failed: make(map[lntypes.Hash]channeldb.FailureReason),
}
}
func (m *mockControlTower) InitPayment(phash lntypes.Hash,
func (m *mockControlTowerOld) InitPayment(phash lntypes.Hash,
c *channeldb.PaymentCreationInfo) error {
if m.init != nil {
@ -305,7 +308,7 @@ func (m *mockControlTower) InitPayment(phash lntypes.Hash,
return nil
}
func (m *mockControlTower) RegisterAttempt(phash lntypes.Hash,
func (m *mockControlTowerOld) RegisterAttempt(phash lntypes.Hash,
a *channeldb.HTLCAttemptInfo) error {
if m.registerAttempt != nil {
@ -359,7 +362,7 @@ func (m *mockControlTower) RegisterAttempt(phash lntypes.Hash,
return nil
}
func (m *mockControlTower) SettleAttempt(phash lntypes.Hash,
func (m *mockControlTowerOld) SettleAttempt(phash lntypes.Hash,
pid uint64, settleInfo *channeldb.HTLCSettleInfo) (
*channeldb.HTLCAttempt, error) {
@ -401,7 +404,7 @@ func (m *mockControlTower) SettleAttempt(phash lntypes.Hash,
return nil, fmt.Errorf("pid not found")
}
func (m *mockControlTower) FailAttempt(phash lntypes.Hash, pid uint64,
func (m *mockControlTowerOld) FailAttempt(phash lntypes.Hash, pid uint64,
failInfo *channeldb.HTLCFailInfo) (*channeldb.HTLCAttempt, error) {
if m.failAttempt != nil {
@ -439,7 +442,7 @@ func (m *mockControlTower) FailAttempt(phash lntypes.Hash, pid uint64,
return nil, fmt.Errorf("pid not found")
}
func (m *mockControlTower) Fail(phash lntypes.Hash,
func (m *mockControlTowerOld) Fail(phash lntypes.Hash,
reason channeldb.FailureReason) error {
m.Lock()
@ -459,7 +462,7 @@ func (m *mockControlTower) Fail(phash lntypes.Hash,
return nil
}
func (m *mockControlTower) FetchPayment(phash lntypes.Hash) (
func (m *mockControlTowerOld) FetchPayment(phash lntypes.Hash) (
*channeldb.MPPayment, error) {
m.Lock()
@ -468,7 +471,7 @@ func (m *mockControlTower) FetchPayment(phash lntypes.Hash) (
return m.fetchPayment(phash)
}
func (m *mockControlTower) fetchPayment(phash lntypes.Hash) (
func (m *mockControlTowerOld) fetchPayment(phash lntypes.Hash) (
*channeldb.MPPayment, error) {
p, ok := m.payments[phash]
@ -490,7 +493,7 @@ func (m *mockControlTower) fetchPayment(phash lntypes.Hash) (
return mp, nil
}
func (m *mockControlTower) FetchInFlightPayments() (
func (m *mockControlTowerOld) FetchInFlightPayments() (
[]*channeldb.MPPayment, error) {
if m.fetchInFlight != nil {
@ -521,7 +524,7 @@ func (m *mockControlTower) FetchInFlightPayments() (
return fl, nil
}
func (m *mockControlTower) SubscribePayment(paymentHash lntypes.Hash) (
func (m *mockControlTowerOld) SubscribePayment(paymentHash lntypes.Hash) (
*ControlTowerSubscriber, error) {
return nil, errors.New("not implemented")

View File

@ -739,7 +739,7 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
sendResult := make(chan error)
paymentResult := make(chan *htlcswitch.PaymentResult)
payer := &mockPayer{
payer := &mockPayerOld{
sendResult: sendResult,
paymentResult: paymentResult,
}
@ -749,8 +749,8 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
Chain: chain,
ChainView: chainView,
Control: control,
SessionSource: &mockPaymentSessionSource{},
MissionControl: &mockMissionControl{},
SessionSource: &mockPaymentSessionSourceOld{},
MissionControl: &mockMissionControlOld{},
Payer: payer,
ChannelPruneExpiry: time.Hour * 24,
GraphPruneInterval: time.Hour * 2,
@ -822,12 +822,12 @@ func testPaymentLifecycle(t *testing.T, test paymentLifecycleTestCase,
// Setup our payment session source to block on release of
// routes.
routeChan := make(chan struct{})
router.cfg.SessionSource = &mockPaymentSessionSource{
router.cfg.SessionSource = &mockPaymentSessionSourceOld{
routes: test.routes,
routeRelease: routeChan,
}
router.cfg.MissionControl = &mockMissionControl{}
router.cfg.MissionControl = &mockMissionControlOld{}
// Send the payment. Since this is new payment hash, the
// information should be registered with the ControlTower.

View File

@ -72,7 +72,7 @@ func (c *testCtx) RestartRouter(t *testing.T) {
Graph: c.graph,
Chain: c.chain,
ChainView: c.chainView,
Payer: &mockPaymentAttemptDispatcher{},
Payer: &mockPaymentAttemptDispatcherOld{},
Control: makeMockControlTower(),
ChannelPruneExpiry: time.Hour * 24,
GraphPruneInterval: time.Hour * 2,
@ -139,7 +139,7 @@ func createTestCtxFromGraphInstanceAssumeValid(t *testing.T,
Graph: graphInstance.graph,
Chain: chain,
ChainView: chainView,
Payer: &mockPaymentAttemptDispatcher{},
Payer: &mockPaymentAttemptDispatcherOld{},
Control: makeMockControlTower(),
MissionControl: mc,
SessionSource: sessionSource,
@ -324,7 +324,7 @@ func TestSendPaymentRouteFailureFallback(t *testing.T) {
// router's configuration to ignore the path that has son goku as the
// first hop. This should force the router to instead take the
// the more costly path (through pham nuwen).
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
if firstHop == roasbeefSongoku {
@ -442,7 +442,7 @@ func TestChannelUpdateValidation(t *testing.T) {
// We'll modify the SendToSwitch method so that it simulates a failed
// payment with an error originating from the first hop of the route.
// The unsigned channel update is attached to the failure message.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
return [32]byte{}, htlcswitch.NewForwardingError(
&lnwire.FailFeeInsufficient{
@ -553,7 +553,7 @@ func TestSendPaymentErrorRepeatedFeeInsufficient(t *testing.T) {
// We'll now modify the SendToSwitch method to return an error for the
// outgoing channel to Son goku. This will be a fee related error, so
// it should only cause the edge to be pruned after the second attempt.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
roasbeefSongoku := lnwire.NewShortChanIDFromInt(
@ -668,7 +668,7 @@ func TestSendPaymentErrorFeeInsufficientPrivateEdge(t *testing.T) {
// outgoing channel to songoku.
errorReturned := false
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
if firstHop != roasbeefSongoku || errorReturned {
@ -800,7 +800,7 @@ func TestSendPaymentPrivateEdgeUpdateFeeExceedsLimit(t *testing.T) {
// outgoing channel to songoku.
errorReturned := false
copy(preImage[:], bytes.Repeat([]byte{9}, 32))
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
if firstHop != roasbeefSongoku || errorReturned {
@ -909,7 +909,7 @@ func TestSendPaymentErrorNonFinalTimeLockErrors(t *testing.T) {
// outgoing channel to son goku. Since this is a time lock related
// error, we should fail the payment flow all together, as Goku is the
// only channel to Sophon.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
if firstHop == roasbeefSongoku {
@ -957,7 +957,7 @@ func TestSendPaymentErrorNonFinalTimeLockErrors(t *testing.T) {
// We'll now modify the error return an IncorrectCltvExpiry error
// instead, this should result in the same behavior of roasbeef routing
// around the faulty Son Goku node.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
if firstHop == roasbeefSongoku {
@ -1017,7 +1017,7 @@ func TestSendPaymentErrorPathPruning(t *testing.T) {
// First, we'll modify the SendToSwitch method to return an error
// indicating that the channel from roasbeef to son goku is not operable
// with an UnknownNextPeer.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
if firstHop == roasbeefSongoku {
@ -1073,7 +1073,7 @@ func TestSendPaymentErrorPathPruning(t *testing.T) {
// Next, we'll modify the SendToSwitch method to indicate that the
// connection between songoku and isn't up.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
if firstHop == roasbeefSongoku {
@ -1107,7 +1107,7 @@ func TestSendPaymentErrorPathPruning(t *testing.T) {
// Finally, we'll modify the SendToSwitch function to indicate that the
// roasbeef -> luoji channel has insufficient capacity. This should
// again cause us to instead go via the satoshi route.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
if firstHop == roasbeefSongoku {
@ -1752,7 +1752,7 @@ func TestWakeUpOnStaleBranch(t *testing.T) {
Graph: ctx.graph,
Chain: ctx.chain,
ChainView: ctx.chainView,
Payer: &mockPaymentAttemptDispatcher{},
Payer: &mockPaymentAttemptDispatcherOld{},
Control: makeMockControlTower(),
ChannelPruneExpiry: time.Hour * 24,
GraphPruneInterval: time.Hour * 2,
@ -2764,7 +2764,7 @@ func TestUnknownErrorSource(t *testing.T) {
// We'll modify the SendToSwitch method so that it simulates hop b as a
// node that returns an unparsable failure if approached via the a->b
// channel.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
// If channel a->b is used, return an error without
@ -2789,7 +2789,7 @@ func TestUnknownErrorSource(t *testing.T) {
}
// Next we modify payment result to return an unknown failure.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
// If channel a->b is used, simulate that the failure
@ -2891,7 +2891,7 @@ func TestSendToRouteStructuredError(t *testing.T) {
// Set up an init channel for the control tower, such that we can make
// sure the payment is initiated correctly.
init := make(chan initArgs, 1)
ctx.router.cfg.Control.(*mockControlTower).init = init
ctx.router.cfg.Control.(*mockControlTowerOld).init = init
// Setup a route from source a to destination c. The route will be used
// in a call to SendToRoute. SendToRoute also applies channel updates,
@ -2922,7 +2922,7 @@ func TestSendToRouteStructuredError(t *testing.T) {
// We'll modify the SendToSwitch method so that it simulates a failed
// payment with an error originating from the first hop of the route.
// The unsigned channel update is attached to the failure message.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
return [32]byte{}, htlcswitch.NewForwardingError(
&lnwire.FailFeeInsufficient{
@ -2999,7 +2999,7 @@ func TestSendToRouteMultiShardSend(t *testing.T) {
// The first shard we send we'll fail immediately, to check that we are
// still allowed to retry with other shards after a failed one.
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
return [32]byte{}, htlcswitch.NewForwardingError(
&lnwire.FailFeeInsufficient{
@ -3026,7 +3026,7 @@ func TestSendToRouteMultiShardSend(t *testing.T) {
waitForResultSignal := make(chan struct{}, numShards)
results := make(chan lntypes.Preimage, numShards)
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcher).setPaymentResult(
ctx.router.cfg.Payer.(*mockPaymentAttemptDispatcherOld).setPaymentResult(
func(firstHop lnwire.ShortChannelID) ([32]byte, error) {
// Signal that the shard has been initiated and is