From 3f007135ee756a1a676be30cad6aec9e4fa66c0f Mon Sep 17 00:00:00 2001 From: Nabarun Date: Wed, 31 May 2023 18:35:54 +0530 Subject: [PATCH] Add placeholder methods for porting virtual-fund command --- packages/nitro-client/src/channel/channel.ts | 6 +- .../src/channel/state/outcome/guarantee.ts | 6 ++ packages/nitro-client/src/channel/virtual.ts | 13 +++- packages/nitro-client/src/client/client.ts | 16 +++++ .../nitro-client/src/client/engine/engine.ts | 4 +- .../src/client/engine/store/memstore.ts | 9 ++- packages/nitro-client/src/payments/helpers.ts | 9 +++ .../src/payments/voucher-manager.ts | 10 +-- .../src/protocols/virtualfund/virtualfund.ts | 64 ++++++++++++++++++- 9 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 packages/nitro-client/src/payments/helpers.ts diff --git a/packages/nitro-client/src/channel/channel.ts b/packages/nitro-client/src/channel/channel.ts index 48a2f588..11ba2caf 100644 --- a/packages/nitro-client/src/channel/channel.ts +++ b/packages/nitro-client/src/channel/channel.ts @@ -1,3 +1,5 @@ +import assert from 'assert'; + import { Signature } from '../crypto/signatures'; import { Destination } from '../types/destination'; import { Address } from '../types/types'; @@ -112,9 +114,9 @@ export class Channel extends FixedPart { } // PostFundState() returns the post fund setup state for the channel. - // TODO: Implement postFundState(): State { - return {} as State; + assert(this.signedStateForTurnNum); + return this.signedStateForTurnNum.get(PostFundTurnNum)!.state(); } // SignedPostFundState() returns the SIGNED post fund setup state for the channel. diff --git a/packages/nitro-client/src/channel/state/outcome/guarantee.ts b/packages/nitro-client/src/channel/state/outcome/guarantee.ts index 1af311cd..8f30fa40 100644 --- a/packages/nitro-client/src/channel/state/outcome/guarantee.ts +++ b/packages/nitro-client/src/channel/state/outcome/guarantee.ts @@ -5,6 +5,12 @@ export class GuaranteeMetadata { right: Destination = new Destination(); + // encode returns the abi.encoded GuaranteeMetadata (suitable for packing in an Allocation.Metadata field) + // TODO: Implement + encode(): Buffer { + return Buffer.alloc(0); + } + // Decode returns a GuaranteeMetaData from an abi encoding static decodeIntoGuaranteeMetadata(m: Buffer): GuaranteeMetadata { // TODO: Implement and check util method from nitro-protocol diff --git a/packages/nitro-client/src/channel/virtual.ts b/packages/nitro-client/src/channel/virtual.ts index 73658116..fde7965a 100644 --- a/packages/nitro-client/src/channel/virtual.ts +++ b/packages/nitro-client/src/channel/virtual.ts @@ -1,4 +1,15 @@ import { Channel } from './channel'; +import { State } from './state/state'; // TODO: Implement -export class VirtualChannel extends Channel {} +export class VirtualChannel extends Channel { + // NewVirtualChannel returns a new VirtualChannel based on the supplied state. + // + // Virtual channel protocol currently presumes exactly two "active" participants, + // Alice and Bob (p[0] and p[last]). They should be the only destinations allocated + // to in the supplied state's Outcome. + // TODO: Implement + static newVirtualChannel(s: State, myIndex: number): VirtualChannel { + return new VirtualChannel({}); + } +} diff --git a/packages/nitro-client/src/client/client.ts b/packages/nitro-client/src/client/client.ts index f0f0b4e2..a0be30c7 100644 --- a/packages/nitro-client/src/client/client.ts +++ b/packages/nitro-client/src/client/client.ts @@ -22,6 +22,10 @@ import { ObjectiveResponse as DirectFundObjectiveResponse, ObjectiveRequest as DirectFundObjectiveRequest, } from '../protocols/directfund/directfund'; +import { + ObjectiveResponse as VirtualFundObjectiveResponse, + ObjectiveRequest as VirtualFundObjectiveRequest, +} from '../protocols/virtualfund/virtualfund'; const log = debug('ts-nitro:client'); @@ -115,6 +119,18 @@ export class Client { return objectiveRequest.response(this.address, this.chainId); } + // CreateVirtualChannel creates a virtual channel with the counterParty using ledger channels + // with the supplied intermediaries. + // TODO: Implement + createVirtualPaymentChannel( + intermediaries: Address[], + counterParty: Address, + challengeDuration: number, + outcome: Exit, + ): VirtualFundObjectiveResponse { + return new VirtualFundObjectiveResponse(); + } + // handleEngineEvents is responsible for monitoring the ToApi channel on the engine. // It parses events from the ToApi chan and then dispatches events to the necessary client chan. // TODO: Implement diff --git a/packages/nitro-client/src/client/engine/engine.ts b/packages/nitro-client/src/client/engine/engine.ts index bb5ea433..82ba5513 100644 --- a/packages/nitro-client/src/client/engine/engine.ts +++ b/packages/nitro-client/src/client/engine/engine.ts @@ -472,7 +472,9 @@ export class Engine { } // TODO: Can throw an error - private registerPaymentChannel(vfo: VirtualFundObjective): void {} + private registerPaymentChannel(vfo: VirtualFundObjective): void { + // TODO: Implement + } // spawnConsensusChannelIfDirectFundObjective will attempt to create and store a ConsensusChannel derived from // the supplied Objective if it is a directfund.Objective. diff --git a/packages/nitro-client/src/client/engine/store/memstore.ts b/packages/nitro-client/src/client/engine/store/memstore.ts index 10a3838f..a8bd4d71 100644 --- a/packages/nitro-client/src/client/engine/store/memstore.ts +++ b/packages/nitro-client/src/client/engine/store/memstore.ts @@ -205,15 +205,18 @@ export class MemStore implements Store { } // TODO: Implement - setVoucherInfo(channelId: string, v: VoucherInfo): void {} + setVoucherInfo(channelId: Destination, v: VoucherInfo): void { + // TODO: Implement + } // TODO: Implement - getVoucherInfo(channelId: string): VoucherInfo { + getVoucherInfo(channelId: Destination): VoucherInfo { + // TODO: Implement return {} as VoucherInfo; } // TODO: Implement - removeVoucherInfo(channelId: string): void {} + removeVoucherInfo(channelId: Destination): void {} } // decodeObjective is a helper which encapsulates the deserialization diff --git a/packages/nitro-client/src/payments/helpers.ts b/packages/nitro-client/src/payments/helpers.ts new file mode 100644 index 00000000..037693e0 --- /dev/null +++ b/packages/nitro-client/src/payments/helpers.ts @@ -0,0 +1,9 @@ +import { Address } from '../types/types'; + +const PAYER_INDEX = 0; + +// GetPayer returns the payer on a payment channel +export const getPayer = (participants: Address[]): Address => participants[PAYER_INDEX]; + +// GetPayee returns the payee on a payment channel +const getPayee = (participants: Address[]): Address => participants[participants.length - 1]; diff --git a/packages/nitro-client/src/payments/voucher-manager.ts b/packages/nitro-client/src/payments/voucher-manager.ts index 97702110..9c584196 100644 --- a/packages/nitro-client/src/payments/voucher-manager.ts +++ b/packages/nitro-client/src/payments/voucher-manager.ts @@ -6,13 +6,13 @@ import { Voucher, VoucherInfo } from './vouchers'; // To avoid import cycles, this interface is defined in the payments package, but implemented in the store package. export interface VoucherStore { // TODO: Can throw an error - setVoucherInfo (channelId: string, v: VoucherInfo): void + setVoucherInfo (channelId: Destination, v: VoucherInfo): void // TODO: Can throw an error - getVoucherInfo (channelId: string): VoucherInfo + getVoucherInfo (channelId: Destination): VoucherInfo // TODO: Can throw an error - removeVoucherInfo (channelId: string): void + removeVoucherInfo (channelId: Destination): void } // VoucherInfo stores the status of payments for a given payment channel. @@ -35,7 +35,9 @@ export class VoucherManager { // Register registers a channel for use, given the payer, payee and starting balance of the channel // TODO: Can throw an error - register(channelId: string, payer: string, payee: string, startingBalance: bigint): void {} + register(channelId: Destination, payer: string, payee: string, startingBalance: bigint): void { + // TODO: Implement + } // Remove deletes the channel's status // TODO: Can throw an error diff --git a/packages/nitro-client/src/protocols/virtualfund/virtualfund.ts b/packages/nitro-client/src/protocols/virtualfund/virtualfund.ts index 7c566145..0531c027 100644 --- a/packages/nitro-client/src/protocols/virtualfund/virtualfund.ts +++ b/packages/nitro-client/src/protocols/virtualfund/virtualfund.ts @@ -1,7 +1,67 @@ // Objective is a cache of data computed by reading from the store. It stores (potentially) infinite data. + +import { Destination } from '../../types/destination'; +import { ConsensusChannel } from '../../channel/consensus-channel/consensus-channel'; +import { Exit } from '../../channel/state/outcome/exit'; +import { State } from '../../channel/state/state'; +import { Funds } from '../../types/funds'; +import { Address } from '../../types/types'; + +// GetTwoPartyConsensusLedgerFuncion describes functions which return a ConsensusChannel ledger channel between +// the calling client and the given counterparty, if such a channel exists. +interface GetTwoPartyConsensusLedgerFunction { + (counterparty: Address): [ConsensusChannel, boolean] +} + +// TODO: Implement +export class Connection { + // insertGuaranteeInfo mutates the receiver Connection struct. + private insertGuaranteeInfo(a0: Funds, b0: Funds, vId: Destination, left: Destination, right: Destination) {} +} + // TODO: Implement -export class Objective {} +export class Objective { + // NewObjective creates a new virtual funding objective from a given request. + // TODO: Implement + static newObjective( + request: ObjectiveRequest, + preApprove: boolean, + myAddress: Address, + chainId: bigint, + getTwoPartyConsensusLedger: GetTwoPartyConsensusLedgerFunction, + ): Objective { + return new Objective(); + } + + // constructFromState initiates an Objective from an initial state and set of ledgers. + // TODO: Implement + static constructFromState( + preApprove: boolean, + initialStateOfV: State, + myAddress: Address, + consensusChannelToMyLeft: ConsensusChannel, + consensusChannelToMyRight: ConsensusChannel, + ): Objective { + return new Objective(); + } +} // ObjectiveRequest represents a request to create a new virtual funding objective. // TODO: Implement -export class ObjectiveRequest {} +export class ObjectiveRequest { + // NewObjectiveRequest creates a new ObjectiveRequest. + static newObjectiveRequest( + intermediaries: Address[], + counterparty: Address, + challengeDuration: number, + outcome: Exit, + nonce: string, + appDefinition: Address, + ): ObjectiveRequest { + return new ObjectiveRequest(); + } +} + +// ObjectiveResponse is the type returned across the API in response to the ObjectiveRequest. +// TODO: Implement +export class ObjectiveResponse {}