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 implementation of Engine.handleObjectiveRequest method for direct-fund command #18

Merged
merged 17 commits 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
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"packages": [
"packages/*"
],
"nohoist": ["**/mocha", "**/mocha/**"]
"nohoist": [
"**/mocha",
"**/mocha/**"
]
},
"devDependencies": {
"lerna": "^6.6.2",
"husky": "^7.0.2"
"husky": "^7.0.2",
"lerna": "^6.6.2"
},
"scripts": {
"lint": "lerna run lint --stream --parallel",
Expand Down
13 changes: 13 additions & 0 deletions packages/nitro-client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
"project": "./tsconfig.eslint.json"
},
"rules": {
// Override airbnb eslint no-restricted-syntax
// https://github.com/airbnb/javascript/blob/master/packages/eslint-config-airbnb-base/rules/style.js
"no-restricted-syntax": [
"error",
{
"selector": "LabeledStatement",
"message": "Labels are a form of GOTO; using them makes code confusing and hard to maintain and understand."
},
{
"selector": "WithStatement",
"message": "`with` is disallowed in strict mode because it makes code impossible to predict and optimize."
}
],
"import/no-extraneous-dependencies": [
"error",
{
Expand Down
3 changes: 3 additions & 0 deletions packages/nitro-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"devDependencies": {
"@types/chai": "^4.3.5",
"@types/debug": "^4.1.7",
"@types/json-bigint": "^1.0.1",
"@types/mocha": "^10.0.1",
"@types/node": "^20.2.1",
"@types/webpack": "^5.28.1",
Expand Down Expand Up @@ -47,9 +48,11 @@
"@libp2p/mdns": "^8.0.0",
"@libp2p/tcp": "^7.0.1",
"@nodeguy/channel": "^1.0.2",
"@statechannels/nitro-protocol": "^2.0.0-alpha.3",
"assert": "^2.0.0",
"debug": "^4.3.4",
"ethers": "^5.7.2",
"json-bigint": "^1.0.0",
"libp2p": "^0.45.3"
}
}
3 changes: 3 additions & 0 deletions packages/nitro-client/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export { Client } from './client/client';
export { EthChainService } from './client/engine/chainservice/eth-chainservice';
export { MemStore } from './client/engine/store/memstore';
export { PermissivePolicy } from './client/engine/policy-maker';
export { SingleAssetExit, Exit } from './channel/state/outcome/exit';
export { Allocation } from './channel/state/outcome/allocation';
export { Destination } from './types/destination';

export const test = (): string => {
// eslint-disable-next-line no-console
Expand Down
59 changes: 39 additions & 20 deletions packages/nitro-client/src/channel/channel.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,77 @@
import { Signature } from '../crypto/signatures';
import { Address, Funds } from '../types/types';
import { Destination } from '../types/destination';
import { Address } from '../types/types';
import { Funds } from '../types/funds';
import { MaxTurnNum, PostFundTurnNum, PreFundTurnNum } from './constants';
import { Allocation } from './state/outcome/allocation';
import { SignedState } from './state/signedstate';
import { FixedPart, State } from './state/state';
import { FixedPart, State, ConstructorOptions as FixedPartConstructorOptions } from './state/state';

interface ConstructorOptions extends FixedPartConstructorOptions {
id?: Destination;
myIndex?: number;
onChainFunding?: Funds;
fixedPart?: FixedPart;
signedStateForTurnNum?: Map<number, SignedState>;
latestSupportedStateTurnNum?: number;
}

// Channel contains states and metadata and exposes convenience methods.
export class Channel {
id: string;
export class Channel extends FixedPart {
id: Destination = new Destination('');

// TODO: unit replacement
myIndex: number;
myIndex: number = 0;

onChainFunding: Funds;
onChainFunding: Funds = new Funds();

fixedPart: FixedPart;
fixedPart?: FixedPart;
// Support []uint64 // TODO: this property will be important, and allow the Channel to store the necessary data to close out the channel on chain
// It could be an array of turnNums, which can be used to slice into Channel.SignedStateForTurnNum

// TODO: unit64 replacement
signedStateForTurnNum: Map<number, SignedState>;
signedStateForTurnNum?: Map<number, SignedState>;
// Longer term, we should have a more efficient and smart mechanism to store states https://github.com/statechannels/go-nitro/issues/106

// largest uint64 value reserved for "no supported state"
// TODO: unit64 replacement
private latestSupportedStateTurnNum: number;
private latestSupportedStateTurnNum: number = 0;

// TODO: unit replacement
constructor(s: State, myIndex: number) {
constructor(params: ConstructorOptions) {
super(params);
Object.assign(this, params);
}

// new constructs a new Channel from the supplied state.
static new(s: State, myIndex: number): Channel {
const c = new Channel({});
// TODO: Use try-catch
s.validate();

this.id = s.channelId();
c.id = s.channelId();

this.myIndex = myIndex;
this.onChainFunding = new Map();
this.fixedPart = s.fixedPart().clone();
this.latestSupportedStateTurnNum = MaxTurnNum; // largest uint64 value reserved for "no supported state"
c.myIndex = myIndex;
c.onChainFunding = new Funds();
c.fixedPart = s.fixedPart().clone();
c.latestSupportedStateTurnNum = MaxTurnNum; // largest uint64 value reserved for "no supported state"
// c.Support = // TODO

// Store prefund
this.signedStateForTurnNum = new Map();
this.signedStateForTurnNum.set(PreFundTurnNum, new SignedState(s));
c.signedStateForTurnNum = new Map();
c.signedStateForTurnNum.set(PreFundTurnNum, new SignedState({ state: s }));

// Store postfund
const post = s.clone();
post.turnNum = PostFundTurnNum;
this.signedStateForTurnNum.set(PostFundTurnNum, new SignedState(post));
c.signedStateForTurnNum.set(PostFundTurnNum, new SignedState({ state: post }));

// TODO: Implement
// Set on chain holdings to zero for each asset
// for asset := range s.Outcome.TotalAllocated() {
// c.OnChainFunding[asset] = big.NewInt(0)
// }

return c;
}

// MarshalJSON returns a JSON representation of the Channel
Expand Down Expand Up @@ -164,7 +183,7 @@ export class Channel {
// Total() returns the total allocated of each asset allocated by the pre fund setup state of the Channel.
// TODO: Implement
total(): Funds {
return new Map();
return new Funds();
}

// Affords returns true if, for each asset keying the input variables, the channel can afford the allocation given the funding.
Expand Down
Loading