Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add placeholder methods for porting virtual-fund command #20

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/nitro-client/src/channel/channel.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import assert from 'assert';

import { Signature } from '../crypto/signatures';
import { Destination } from '../types/destination';
import { Address } from '../types/types';
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions packages/nitro-client/src/channel/state/outcome/guarantee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion packages/nitro-client/src/channel/virtual.ts
Original file line number Diff line number Diff line change
@@ -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({});
}
}
16 changes: 16 additions & 0 deletions packages/nitro-client/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/nitro-client/src/client/engine/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions packages/nitro-client/src/client/engine/store/memstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions packages/nitro-client/src/payments/helpers.ts
Original file line number Diff line number Diff line change
@@ -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];
10 changes: 6 additions & 4 deletions packages/nitro-client/src/payments/voucher-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
64 changes: 62 additions & 2 deletions packages/nitro-client/src/protocols/virtualfund/virtualfund.ts
Original file line number Diff line number Diff line change
@@ -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 {}