Skip to content

Commit

Permalink
Port ObjectiveRequest.newObjectiveRequest method implementation req…
Browse files Browse the repository at this point in the history
…uired for virtual-fund command (#21)

* Implement ObjectiveRequest.newObjectiveRequest required for virtual-fund command

* Set Go uint64 type zero value to string 0 in TS
  • Loading branch information
nikugogoi authored May 31, 2023
1 parent 099ca1a commit bcb5cc0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/nitro-client/src/channel/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class FixedPart {
participants: Address[] = [];

// TODO: unit64 replacement
channelNonce: string = '';
channelNonce: string = '0';

appDefinition: Address = ethers.constants.AddressZero;

Expand Down Expand Up @@ -70,7 +70,7 @@ export class VariablePart {
export class State {
participants: Address[] = [];

channelNonce: string = '';
channelNonce: string = '0';

appDefinition: Address = '';

Expand Down
31 changes: 24 additions & 7 deletions packages/nitro-client/src/client/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import debug from 'debug';
import assert from 'assert';
import { ethers } from 'ethers';

import type { ReadWriteChannel } from '@nodeguy/channel';
import Channel from '@nodeguy/channel';
Expand Down Expand Up @@ -33,7 +34,7 @@ export class Client {
// The core business logic of the client
private engine?: Engine;

address?: Address;
address: Address = ethers.constants.AddressZero;

private channelNotifier?: ChannelNotifier;

Expand Down Expand Up @@ -104,13 +105,13 @@ export class Client {
assert(this.address);
assert(this.chainId);

const objectiveRequest = new DirectFundObjectiveRequest({
counterParty: counterparty,
const objectiveRequest = DirectFundObjectiveRequest.newObjectiveRequest(
counterparty,
challengeDuration,
outcome,
nonce: randUint64(),
appDefinition: this.engine.getConsensusAppAddress(),
});
randUint64(),
this.engine.getConsensusAppAddress(),
);

assert(this.engine.objectiveRequestsFromAPI);
// Send the event to the engine
Expand All @@ -128,7 +129,23 @@ export class Client {
challengeDuration: number,
outcome: Exit,
): VirtualFundObjectiveResponse {
return new VirtualFundObjectiveResponse();
assert(this.engine);

const objectiveRequest = VirtualFundObjectiveRequest.newObjectiveRequest(
intermediaries,
counterParty,
challengeDuration,
outcome,
randUint64(),
this.engine.getVirtualPaymentAppAddress(),
);

// Send the event to the engine
assert(this.engine.objectiveRequestsFromAPI);
this.engine.objectiveRequestsFromAPI.push(objectiveRequest);

objectiveRequest.waitForObjectiveToStart();
return objectiveRequest.response(this.address);
}

// handleEngineEvents is responsible for monitoring the ToApi channel on the engine.
Expand Down
20 changes: 19 additions & 1 deletion packages/nitro-client/src/protocols/directfund/directfund.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ export class ObjectiveRequest implements ObjectiveRequestInterface {

appData: Buffer = Buffer.alloc(0);

nonce: string = '';
nonce: string = '0';

private objectiveStarted?: ReadWriteChannel<void>;

Expand All @@ -510,6 +510,24 @@ export class ObjectiveRequest implements ObjectiveRequestInterface {
Object.assign(this, params);
}

// newObjectiveRequest creates a new ObjectiveRequest.
static newObjectiveRequest(
counterparty: Address,
challengeDuration: number,
outcome: Exit,
nonce: string,
appDefinition: Address,
): ObjectiveRequest {
return new ObjectiveRequest({
counterParty: counterparty,
challengeDuration,
outcome,
nonce,
appDefinition,
objectiveStarted: Channel(),
});
}

// SignalObjectiveStarted is used by the engine to signal the objective has been started.
signalObjectiveStarted(): void { }

Expand Down
73 changes: 65 additions & 8 deletions packages/nitro-client/src/protocols/virtualfund/virtualfund.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Objective is a cache of data computed by reading from the store. It stores (potentially) infinite data.
import Channel from '@nodeguy/channel';
import type { ReadWriteChannel } from '@nodeguy/channel';

import { ethers } from 'ethers';
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';
import { ObjectiveRequest as ObjectiveRequestInterface } from '../interfaces';
import { ObjectiveId } from '../messages';

// GetTwoPartyConsensusLedgerFuncion describes functions which return a ConsensusChannel ledger channel between
// the calling client and the given counterparty, if such a channel exists.
Expand All @@ -19,7 +23,7 @@ export class Connection {
private insertGuaranteeInfo(a0: Funds, b0: Funds, vId: Destination, left: Destination, right: Destination) {}
}

// TODO: Implement
// Objective is a cache of data computed by reading from the store. It stores (potentially) infinite data.
export class Objective {
// NewObjective creates a new virtual funding objective from a given request.
// TODO: Implement
Expand All @@ -46,9 +50,39 @@ export class Objective {
}
}

// ObjectiveResponse is the type returned across the API in response to the ObjectiveRequest.
// TODO: Implement
export class ObjectiveResponse {}

// ObjectiveRequest represents a request to create a new virtual funding objective.
// TODO: Implement
export class ObjectiveRequest {
export class ObjectiveRequest implements ObjectiveRequestInterface {
intermediaries: Address[] = [];

counterParty: Address = ethers.constants.AddressZero;

challengeDuration: number = 0;

outcome?: Exit;

nonce: string = '0';

appDefinition: Address = ethers.constants.AddressZero;

private objectiveStarted?: ReadWriteChannel<null>;

constructor(params: {
intermediaries: Address[];
counterParty: Address;
challengeDuration: number;
outcome?: Exit;
nonce: string;
appDefinition: Address;
objectiveStarted?: ReadWriteChannel<null>;
}) {
Object.assign(this, params);
}

// NewObjectiveRequest creates a new ObjectiveRequest.
static newObjectiveRequest(
intermediaries: Address[],
Expand All @@ -58,10 +92,33 @@ export class ObjectiveRequest {
nonce: string,
appDefinition: Address,
): ObjectiveRequest {
return new ObjectiveRequest();
return new ObjectiveRequest({
intermediaries,
counterParty: counterparty,
challengeDuration,
outcome,
nonce,
appDefinition,
objectiveStarted: Channel(),
});
}
}

// ObjectiveResponse is the type returned across the API in response to the ObjectiveRequest.
// TODO: Implement
export class ObjectiveResponse {}
id(address: Address, chainId: bigint): ObjectiveId {
// TODO: Implement
return '';
}

waitForObjectiveToStart(): void {
// TODO: Implement
}

signalObjectiveStarted(): void {
// TODO: Implement
}

// response computes and returns the appropriate response from the request.
response(myAddress: Address): ObjectiveResponse {
// TODO: Implement
return new ObjectiveResponse();
}
}

0 comments on commit bcb5cc0

Please sign in to comment.