-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the
findroutebetweennodes
command
- Loading branch information
Mohit Kumar
committed
Aug 18, 2023
1 parent
c860eaa
commit 6f6807c
Showing
6 changed files
with
230 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package commands | ||
|
||
import IResultWriter | ||
import api.IEclairClientBuilder | ||
import arrow.core.flatMap | ||
import kotlinx.cli.ArgType | ||
import kotlinx.coroutines.runBlocking | ||
import types.FindRouteResponse | ||
import types.Serialization | ||
|
||
class FindRouteBetweenNodesCommand( | ||
private val resultWriter: IResultWriter, | ||
private val eclairClientBuilder: IEclairClientBuilder | ||
) : BaseCommand( | ||
"findroutebetweennodes", | ||
"Finds a route between two nodes." | ||
) { | ||
private val sourceNodeId by option( | ||
ArgType.String, | ||
description = "The destination of the route" | ||
) | ||
private val targetNodeId by option( | ||
ArgType.String, | ||
description = "The destination of the route" | ||
) | ||
private val amountMsat by option( | ||
ArgType.Int, | ||
description = "The amount that should go through the route" | ||
) | ||
private val ignoreNodeIds by option( | ||
ArgType.String, | ||
description = "A list of nodes to exclude from path-finding" | ||
) | ||
private val ignoreShortChannelIds by option( | ||
ArgType.String, | ||
description = "A list of channels to exclude from path-finding" | ||
) | ||
private val format by option( | ||
ArgType.String, | ||
description = "Format that will be used for the resulting route" | ||
) | ||
private val maxFeeMsat by option( | ||
ArgType.Int, | ||
description = "Maximum fee allowed for this payment" | ||
) | ||
private val includeLocalChannelCost by option( | ||
ArgType.Boolean, | ||
description = "If true, the relay fees of local channels will be counted" | ||
) | ||
private val pathFindingExperimentName by option( | ||
ArgType.String, | ||
description = "Name of the path-finding configuration that should be used" | ||
) | ||
|
||
override fun execute() = runBlocking { | ||
val eclairClient = eclairClientBuilder.build(host, password) | ||
val result = eclairClient.findroutebetweennodes( | ||
sourceNodeId!!, | ||
targetNodeId!!, | ||
amountMsat!!, | ||
ignoreNodeIds?.split(","), | ||
ignoreShortChannelIds?.split(","), | ||
format, | ||
maxFeeMsat, | ||
includeLocalChannelCost, | ||
pathFindingExperimentName | ||
).flatMap { apiResponse -> Serialization.decode<FindRouteResponse>(apiResponse) } | ||
.map { decoded -> Serialization.encode(decoded) } | ||
resultWriter.write(result) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/nativeTest/kotlin/commands/FindRouteBetweenNodesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package commands | ||
|
||
import api.IEclairClientBuilder | ||
import kotlinx.cli.ArgParser | ||
import kotlinx.cli.ExperimentalCli | ||
import kotlinx.serialization.json.Json | ||
import mocks.DummyEclairClient | ||
import mocks.DummyResultWriter | ||
import mocks.FailingEclairClient | ||
import types.ApiError | ||
import types.FindRouteResponse | ||
import kotlin.test.* | ||
|
||
@OptIn(ExperimentalCli::class) | ||
class FindRouteBetweenNodesCommandTest { | ||
private fun runTest(eclairClient: IEclairClientBuilder): DummyResultWriter { | ||
val resultWriter = DummyResultWriter() | ||
val command = FindRouteBetweenNodesCommand(resultWriter, eclairClient) | ||
val parser = ArgParser("test") | ||
parser.subcommands(command) | ||
parser.parse( | ||
arrayOf( | ||
"findroutebetweennodes", | ||
"-p", | ||
"password", | ||
"--sourceNodeId", | ||
"03c5b161c16e9f8ef3f3bccfb74a6e9a3b423dd41fe2848174b7209f1c2ea25dad", | ||
"--targetNodeId", | ||
"02f666711319435b7905dd77d10c269d8d50c02668b975f526577167d370b50a3e", | ||
"--amountMsat", | ||
"1000" | ||
) | ||
) | ||
return resultWriter | ||
} | ||
|
||
@Test | ||
fun `successful request`() { | ||
val resultWriter = | ||
runTest(DummyEclairClient(findroutebetweennodesResponse = DummyEclairClient.validFindRouteBetweenNodesResponse)) | ||
assertNull(resultWriter.lastError) | ||
assertNotNull(resultWriter.lastResult) | ||
val format = Json { ignoreUnknownKeys = true } | ||
assertEquals( | ||
format.decodeFromString( | ||
FindRouteResponse.serializer(), | ||
DummyEclairClient.validFindRouteBetweenNodesResponse | ||
), | ||
format.decodeFromString(FindRouteResponse.serializer(), resultWriter.lastResult!!), | ||
) | ||
} | ||
|
||
@Test | ||
fun `api error`() { | ||
val error = ApiError(42, "test failure message") | ||
val resultWriter = runTest(FailingEclairClient(error)) | ||
assertNull(resultWriter.lastResult) | ||
assertEquals(error, resultWriter.lastError) | ||
} | ||
|
||
@Test | ||
fun `serialization error`() { | ||
val resultWriter = runTest(DummyEclairClient(findroutebetweennodesResponse = "{invalidJson}")) | ||
assertNull(resultWriter.lastResult) | ||
assertNotNull(resultWriter.lastError) | ||
assertTrue(resultWriter.lastError!!.message.contains("api response could not be parsed")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters