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

Port method implementations for get-payment-channel command #26

Merged
merged 1 commit into from
Jun 1, 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
10 changes: 9 additions & 1 deletion packages/nitro-client/src/channel/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ export class Channel extends FixedPart {
// json-encoded data
// TODO: Can throw an error
// TODO: Implement
UnmarshalJSON(data: Buffer): void {}
unmarshalJSON(data: Buffer): void {
try {
// TODO: Implement json.Unmarshal
const jsonCh = JSON.parse(data.toString());
Object.assign(this, jsonCh);
} catch (err) {
throw new Error('error unmarshaling channel data');
}
}

// MyDestination returns the client's destination
// TODO: Implement
Expand Down
30 changes: 25 additions & 5 deletions packages/nitro-client/src/client/engine/store/memstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { bytes2Hex } from '@cerc-io/nitro-util';

import { ethers } from 'ethers';
import assert from 'assert';
import { Store } from './store';
import { ErrNoSuchChannel, Store } from './store';
import { Objective, ObjectiveStatus } from '../../../protocols/interfaces';
import { Channel } from '../../../channel/channel';
import { ConsensusChannel } from '../../../channel/consensus-channel/consensus-channel';
Expand Down Expand Up @@ -150,13 +150,33 @@ export class MemStore implements Store {
destroyConsensusChannel(id: string): void {}

getChannelById(id: Destination): [Channel, boolean] {
// TODO: Implement
return [{} as Channel, false];
try {
const ch = this._getChannelById(id);

return [ch, true];
} catch (err) {
return [new Channel({}), false];
}
}

private _getChannelById(id: Destination): Channel {
// TODO: Implement
return {} as Channel;
const [chJSON, ok] = this.channels.load(id.toString());

if (!ok) {
throw ErrNoSuchChannel;
}

assert(chJSON);

const ch = new Channel({});

try {
ch.unmarshalJSON(chJSON);

return ch;
} catch (err) {
throw new Error(`error unmarshaling channel ${ch.id}`);
}
}

// TODO: Implement
Expand Down
2 changes: 2 additions & 0 deletions packages/nitro-client/src/client/engine/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { VoucherStore } from '../../../payments/voucher-manager';
import { Address } from '../../../types/types';
import { Destination } from '../../../types/destination';

export const ErrNoSuchChannel = new Error('store: failed to find required channel data');

// Store is responsible for persisting objectives, objective metadata, states, signatures, private keys and blockchain data
export interface Store extends ConsensusChannelStore, VoucherStore {
// Get a pointer to a secret key for signing channel updates
Expand Down
57 changes: 33 additions & 24 deletions packages/nitro-client/src/client/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ const getLatestSupportedOrPreFund = (channel: Channel): State => {
return channel.preFundState();
};

// GetPaymentChannelInfo returns the PaymentChannelInfo for the given channel
// It does this by querying the provided store and voucher manager
// TODO: Implement
export const getPaymentChannelInfo = (id: Destination, store: Store, vm: VoucherManager): PaymentChannelInfo => new PaymentChannelInfo({});

export const getVoucherBalance = (id: Destination, vm: VoucherManager): [bigint, bigint] => {
let paid: bigint = BigInt(0);
let remaining: bigint = BigInt(0);
Expand All @@ -87,25 +82,6 @@ export const getVoucherBalance = (id: Destination, vm: VoucherManager): [bigint,
return [paid, remaining];
};

export const constructLedgerInfoFromConsensus = (con: ConsensusChannel): LedgerChannelInfo => {
const latest = con.consensusVars().asState(con.fixedPart());
return new LedgerChannelInfo({
iD: con.id,
status: ChannelStatus.Open,
balance: getLedgerBalanceFromState(latest),
});
};

export const constructLedgerInfoFromChannel = (c: Channel): LedgerChannelInfo => {
const latest = getLatestSupportedOrPreFund(c);

return new LedgerChannelInfo({
iD: c.id,
status: getStatusFromChannel(c),
balance: getLedgerBalanceFromState(latest),
});
};

export const constructPaymentInfo = (c: Channel, paid: bigint, remaining: bigint): PaymentChannelInfo => {
let status = getStatusFromChannel(c);

Expand All @@ -129,3 +105,36 @@ export const constructPaymentInfo = (c: Channel, paid: bigint, remaining: bigint
balance,
});
};

// GetPaymentChannelInfo returns the PaymentChannelInfo for the given channel
// It does this by querying the provided store and voucher manager
export const getPaymentChannelInfo = (id: Destination, store: Store, vm: VoucherManager): PaymentChannelInfo => {
const [c, channelFound] = store.getChannelById(id);

if (channelFound) {
const [paid, remaining] = getVoucherBalance(id, vm);

return constructPaymentInfo(c, paid, remaining);
}

throw new Error(`Could not find channel with id ${id}`);
};

export const constructLedgerInfoFromConsensus = (con: ConsensusChannel): LedgerChannelInfo => {
const latest = con.consensusVars().asState(con.fixedPart());
return new LedgerChannelInfo({
iD: con.id,
status: ChannelStatus.Open,
balance: getLedgerBalanceFromState(latest),
});
};

export const constructLedgerInfoFromChannel = (c: Channel): LedgerChannelInfo => {
const latest = getLatestSupportedOrPreFund(c);

return new LedgerChannelInfo({
iD: c.id,
status: getStatusFromChannel(c),
balance: getLedgerBalanceFromState(latest),
});
};