Skip to content

Commit

Permalink
refactor(node): calculate endpoint account paths on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
justmoon committed Oct 9, 2024
1 parent c28bb91 commit 784edf7
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import { createActor, createSignal } from "@dassie/lib-reactive"
import { isFailure } from "@dassie/lib-type-utils"

import { OwnerLedgerIdSignal } from "../accounting/signals/owner-ledger-id"
import { BtpTokensStore } from "../api-keys/database-stores/btp-tokens"
import type { BtpToken } from "../api-keys/types/btp-token"
import type { DassieReactor } from "../base/types/dassie-base"
Expand All @@ -30,7 +29,6 @@ export const RegisterBtpHttpUpgradeActor = (reactor: DassieReactor) => {
const processPacket = reactor.use(ProcessPacket)
const btpTokensStore = reactor.use(BtpTokensStore)
const routingTableSignal = reactor.use(RoutingTableSignal)
const ownerLedgerIdSignal = reactor.use(OwnerLedgerIdSignal)
const nextConnectionIdSignal = reactor.use(NextConnectionIdSignal)
const websocketRouter = reactor.use(HttpsWebSocketRouter)

Expand Down Expand Up @@ -62,7 +60,6 @@ export const RegisterBtpHttpUpgradeActor = (reactor: DassieReactor) => {
const endpointInfo: BtpEndpointInfo = {
type: "btp",
connectionId,
accountPath: `${ownerLedgerIdSignal.read()}:equity/owner`,
}

logger.debug?.("handle BTP websocket connection", { connectionId })
Expand Down
3 changes: 0 additions & 3 deletions packages/app-dassie/src/ildcp-server/handle-ildcp-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { IlpType, serializeIlpPacket } from "@dassie/lib-protocol-ilp"
import { createActor } from "@dassie/lib-reactive"
import { isFailure } from "@dassie/lib-type-utils"

import type { AccountPath } from "../accounting/types/account-paths"
import type { DassieActorContext } from "../base/types/dassie-base"
import { ProcessPacket } from "../ilp-connector/functions/process-packet"
import type { IldcpEndpointInfo } from "../ilp-connector/senders/send-ildcp-packets"
Expand All @@ -22,8 +21,6 @@ export const HandleIldcpRequestsActor = () =>

const ildcpDestinationInfo: IldcpEndpointInfo = {
type: "ildcp",
// ILDCP cannot send or receive money so we set the account path to an impossible value
accountPath: "unreachable" as AccountPath,
}

ilpRoutingTable.set(ILDCP_ADDRESS, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { InsufficientTimeoutIlpFailure } from "../failures/insufficient-timeout-
import { InternalErrorIlpFailure } from "../failures/internal-error-ilp-failure"
import { InvalidPacketIlpFailure } from "../failures/invalid-packet-ilp-failure"
import { UnreachableIlpFailure } from "../failures/unreachable-ilp-failure"
import { GetEndpointAccountPath } from "./get-endpoint-account-path"
import type { EndpointInfo } from "./send-packet"

export interface CalculatePreparePacketOutcomeParameters {
Expand Down Expand Up @@ -57,6 +58,7 @@ export const CalculatePreparePacketOutcome = (reactor: DassieReactor) => {
const ledgerStore = reactor.use(LedgerStore)
const resolveIlpAddress = reactor.use(ResolveIlpAddress)
const calculateOutgoingAmount = reactor.use(CalculateOutgoingAmount)
const getEndpointAccountPath = reactor.use(GetEndpointAccountPath)

function calculatePacketOutcome({
sourceEndpointInfo,
Expand Down Expand Up @@ -100,14 +102,14 @@ export const CalculatePreparePacketOutcome = (reactor: DassieReactor) => {
}

outgoingAmount = calculateOutgoingAmount(
sourceEndpointInfo.accountPath,
destinationEndpointInfo.accountPath,
getEndpointAccountPath(sourceEndpointInfo),
getEndpointAccountPath(destinationEndpointInfo),
parsedPacket.data.amount,
)

const transferParameters = applyPacketPrepareToLedger(
sourceEndpointInfo.accountPath,
destinationEndpointInfo.accountPath,
getEndpointAccountPath(sourceEndpointInfo),
getEndpointAccountPath(destinationEndpointInfo),
parsedPacket.data.amount,
outgoingAmount,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Reactor } from "@dassie/lib-reactive"
import { UnreachableCaseError } from "@dassie/lib-type-utils"

import { OwnerLedgerIdSignal } from "../../accounting/signals/owner-ledger-id"
import type { AccountPath } from "../../accounting/types/account-paths"
import type { EndpointInfo } from "./send-packet"

export function GetEndpointAccountPath(reactor: Reactor) {
const ownerLedgerIdSignal = reactor.use(OwnerLedgerIdSignal)

return function getEndpointAccountPath(
endpointInfo: EndpointInfo,
): AccountPath {
switch (endpointInfo.type) {
case "peer": {
return endpointInfo.accountPath
}
case "ildcp": {
// ILDCP cannot send or receive money so we set the account path to an impossible value
return "unreachable" as AccountPath
}
case "btp": {
return `${ownerLedgerIdSignal.read()}:equity/owner`
}
case "local": {
return `${ownerLedgerIdSignal.read()}:equity/owner`
}
case "http": {
return `${ownerLedgerIdSignal.read()}:equity/owner`
}
default: {
throw new UnreachableCaseError(endpointInfo)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type IlpPacket, IlpType } from "@dassie/lib-protocol-ilp"

import type { AccountPath } from "../../accounting/types/account-paths"
import type { DassieReactor } from "../../base/types/dassie-base"
import {
type BtpEndpointInfo,
Expand All @@ -24,9 +23,7 @@ import {
} from "../senders/send-peer-packets"
import type { PreparedIlpPacketEvent } from "../topics/prepared-ilp-packet"

export interface CommonEndpointInfo {
readonly accountPath: AccountPath
}
export interface CommonEndpointInfo {}

export interface PreparedPacketParameters<
TType extends EndpointInfo["type"] = EndpointInfo["type"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { isFailure } from "@dassie/lib-type-utils"
import type { DassieReactor } from "../../base/types/dassie-base"
import { RegisterBtpHttpUpgradeActor } from "../../btp-server/register-btp-http-upgrade"
import { connector as logger } from "../../logger/instances"
import type { CommonEndpointInfo, PacketSender } from "../functions/send-packet"
import type { PacketSender } from "../functions/send-packet"

export interface BtpEndpointInfo extends CommonEndpointInfo {
export interface BtpEndpointInfo {
readonly type: "btp"
readonly connectionId: number
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { Reactor } from "@dassie/lib-reactive"

import { HandleIldcpRequestsActor } from "../../ildcp-server/handle-ildcp-requests"
import { GetEndpointIlpAddress } from "../functions/get-endpoint-ilp-address"
import type { CommonEndpointInfo, PacketSender } from "../functions/send-packet"
import type { PacketSender } from "../functions/send-packet"

export interface IldcpEndpointInfo extends CommonEndpointInfo {
export interface IldcpEndpointInfo {
readonly type: "ildcp"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { tell } from "@dassie/lib-type-utils"
import { SendAsyncPrepare } from "../../ilp-http/functions/send-async-prepare"
import { SendAsyncResult } from "../../ilp-http/functions/send-async-result"
import { IncomingRequestIdMap } from "../../ilp-http/values/incoming-request-id-map"
import type { CommonEndpointInfo, PacketSender } from "../functions/send-packet"
import type { PacketSender } from "../functions/send-packet"

export interface IlpHttpEndpointInfo extends CommonEndpointInfo {
export interface IlpHttpEndpointInfo {
readonly type: "http"
readonly id: string
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import type { DassieReactor } from "../../base/types/dassie-base"
import { LocalEndpointsStore } from "../../local-ilp/stores/local-endpoints"
import { OutstandingRequestsStore } from "../../local-ilp/stores/outstanding-requests"
import { ProcessPacket } from "../functions/process-packet"
import type { CommonEndpointInfo, PacketSender } from "../functions/send-packet"
import type { PacketSender } from "../functions/send-packet"

export interface LocalEndpointInfo extends CommonEndpointInfo {
export interface LocalEndpointInfo {
readonly type: "local"
readonly hint: string
readonly localIlpAddressPart: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { assert } from "@dassie/lib-logger"
import { tell } from "@dassie/lib-type-utils"

import type { AccountPath } from "../../accounting/types/account-paths"
import type { DassieReactor } from "../../base/types/dassie-base"
import { connector as logger } from "../../logger/instances"
import { SendPeerMessage } from "../../peer-protocol/functions/send-peer-message"
import type { NodeId } from "../../peer-protocol/types/node-id"
import type { CommonEndpointInfo, PacketSender } from "../functions/send-packet"
import type { PacketSender } from "../functions/send-packet"

export interface PeerEndpointInfo extends CommonEndpointInfo {
export interface PeerEndpointInfo {
readonly type: "peer"
readonly nodeId: NodeId
readonly accountPath: AccountPath
}

export const SendPeerPackets = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { parseIlpPacket } from "@dassie/lib-protocol-ilp"
import { createActor } from "@dassie/lib-reactive"

import { OwnerLedgerIdSignal } from "../accounting/signals/owner-ledger-id"
import type { DassieReactor } from "../base/types/dassie-base"
import { HttpsRouter } from "../http-server/values/https-router"
import { ProcessPacket } from "../ilp-connector/functions/process-packet"
Expand All @@ -17,7 +16,6 @@ import { ILP_OVER_HTTP_CONTENT_TYPE } from "./constants/content-type"

export const RegisterIlpHttpCallbackHandlerActor = (reactor: DassieReactor) => {
const processPacket = reactor.use(ProcessPacket)
const ownerLedgerIdSignal = reactor.use(OwnerLedgerIdSignal)

return createActor((sig) => {
const http = sig.reactor.use(HttpsRouter)
Expand All @@ -39,7 +37,6 @@ export const RegisterIlpHttpCallbackHandlerActor = (reactor: DassieReactor) => {

const endpointInfo: IlpHttpEndpointInfo = {
type: "http",
accountPath: `${ownerLedgerIdSignal.read()}:equity/owner`,
id: "TODO",
}

Expand Down
3 changes: 0 additions & 3 deletions packages/app-dassie/src/ilp-http/register-ilp-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { parseIlpPacket } from "@dassie/lib-protocol-ilp"
import { createActor } from "@dassie/lib-reactive"

import { OwnerLedgerIdSignal } from "../accounting/signals/owner-ledger-id"
import type { DassieReactor } from "../base/types/dassie-base"
import { HttpsRouter } from "../http-server/values/https-router"
import { ProcessPacket } from "../ilp-connector/functions/process-packet"
Expand All @@ -19,7 +18,6 @@ import { IncomingRequestIdMap } from "./values/incoming-request-id-map"
export const RegisterIlpHttpHandlerActor = (reactor: DassieReactor) => {
const processPacket = reactor.use(ProcessPacket)
const incomingRequestIdMap = reactor.use(IncomingRequestIdMap)
const ownerLedgerIdSignal = reactor.use(OwnerLedgerIdSignal)

return createActor((sig) => {
const http = sig.reactor.use(HttpsRouter)
Expand Down Expand Up @@ -53,7 +51,6 @@ export const RegisterIlpHttpHandlerActor = (reactor: DassieReactor) => {

const endpointInfo: IlpHttpEndpointInfo = {
type: "http",
accountPath: `${ownerLedgerIdSignal.read()}:equity/owner`,
id: "TODO",
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from "@dassie/lib-protocol-ilp"
import { type Scope, createDeferred } from "@dassie/lib-reactive"

import { OwnerLedgerIdSignal } from "../../accounting/signals/owner-ledger-id"
import type { DassieReactor } from "../../base/types/dassie-base"
import { ProcessPacket } from "../../ilp-connector/functions/process-packet"
import type { LocalEndpointInfo } from "../../ilp-connector/senders/send-local-packets"
Expand All @@ -26,7 +25,6 @@ export interface LocalIlpEndpoint extends IlpEndpoint {

export const CreateLocalEndpoint = (reactor: DassieReactor) => {
const localEndpointsStore = reactor.use(LocalEndpointsStore)
const ownerLedgerIdSignal = reactor.use(OwnerLedgerIdSignal)
const processPacket = reactor.use(ProcessPacket)
const outstandingRequestsStore = reactor.use(OutstandingRequestsStore)

Expand All @@ -43,7 +41,6 @@ export const CreateLocalEndpoint = (reactor: DassieReactor) => {
type: "local",
hint,
localIlpAddressPart: id,
accountPath: `${ownerLedgerIdSignal.read()}:equity/owner`,
}

localEndpointsStore.act.registerLocalEndpoint(localEndpointInfo)
Expand Down

0 comments on commit 784edf7

Please sign in to comment.