From 88c019141734a556838fdef22b0169481571dd81 Mon Sep 17 00:00:00 2001 From: neerajvijay1997 <111040298+neerajvijay1997@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:48:07 +0530 Subject: [PATCH] Port latest changes from go-nitro till commit `ff84d606` on November 1 (#4) * Upgrade state channel nitro protocol version * Generate latest contract bindings * Add steps to generate bindings * Update DEVELOPMENT.md * Use wrapped error in handle objective request * Update README and DEVELOPMENT.md --- DEVELOPMENT.md | 2 +- README.md | 4 +++ packages/nitro-node/package.json | 2 +- packages/nitro-node/src/node/engine/engine.ts | 23 ++++++++++----- packages/nitro-util/DEVELOPMENT.md | 28 +++++++++++++++++++ packages/nitro-util/README.md | 5 ++++ .../contracts/NitroAdjudicator.json | 19 +++++++++++++ packages/nitro-util/package.json | 2 +- .../src/contract-bindings/NitroAdjudicator.ts | 22 +++++++++++++++ .../factories/NitroAdjudicator__factory.ts | 19 +++++++++++++ packages/server/src/index.ts | 2 -- yarn.lock | 8 +++--- 12 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 packages/nitro-util/DEVELOPMENT.md create mode 100644 packages/nitro-util/README.md diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index 993d7ec9..dae7b314 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -1,4 +1,4 @@ -# Porting go-nitro latest commits +# Development ## Skipped go-nitro Commits / Features diff --git a/README.md b/README.md index 86ad3cf1..009c2941 100644 --- a/README.md +++ b/README.md @@ -182,3 +182,7 @@ Run relay node using v2 watcher ```bash clearNodeStorage() ``` + +### Development + +* [README](./DEVELOPMENT.md) diff --git a/packages/nitro-node/package.json b/packages/nitro-node/package.json index 98acdd4f..e3c840e9 100644 --- a/packages/nitro-node/package.json +++ b/packages/nitro-node/package.json @@ -61,7 +61,7 @@ "@libp2p/crypto": "^1.0.4", "@libp2p/tcp": "^6.0.0", "@multiformats/multiaddr": "^11.1.4", - "@statechannels/nitro-protocol": "^2.0.1-alpha.5", + "@statechannels/nitro-protocol": "^2.0.1-alpha.6", "assert": "^2.0.0", "async-mutex": "^0.4.0", "debug": "^4.3.4", diff --git a/packages/nitro-node/src/node/engine/engine.ts b/packages/nitro-node/src/node/engine/engine.ts index 5a845365..7531f893 100644 --- a/packages/nitro-node/src/node/engine/engine.ts +++ b/packages/nitro-node/src/node/engine/engine.ts @@ -719,7 +719,7 @@ export class Engine { try { chainId = await this.chain.getChainId(); } catch (err) { - return [new EngineEvent({}), new Error(`could not get chain id from chain service: ${err}`)]; + return [new EngineEvent({}), new WrappedError(`could not get chain id from chain service: ${err}`, err as Error)]; } const objectiveId = or.id(myAddress, chainId); @@ -746,7 +746,7 @@ export class Engine { this.store.getConsensusChannel.bind(this.store), ); } catch (err) { - return [failedEngineEvent, new Error(`handleAPIEvent: Could not create virtualfund objective for ${or}: ${err}`)]; + return [failedEngineEvent, new WrappedError(`handleAPIEvent: Could not create virtualfund objective for ${or}: ${err}`, err as Error)]; } if (METRICS_ENABLED) { @@ -784,7 +784,10 @@ export class Engine { } catch (err) { return [ failedEngineEvent, - new Error(`handleAPIEvent: Could not create virtualdefund objective for ${JSONbigNative.stringify(request)}: ${err}`), + new WrappedError( + `handleAPIEvent: Could not create virtualdefund objective for ${JSONbigNative.stringify(request)}: ${err}`, + err as Error, + ), ]; } } @@ -806,7 +809,10 @@ export class Engine { } catch (err) { return [ failedEngineEvent, - new Error(`handleAPIEvent: Could not create virtualdefund objective for ${JSONbigNative.stringify(request)}: ${err}`), + new WrappedError( + `handleAPIEvent: Could not create virtualdefund objective for ${JSONbigNative.stringify(request)}: ${err}`, + err as Error, + ), ]; } @@ -831,7 +837,7 @@ export class Engine { } catch (err) { return [ failedEngineEvent, - new Error(`handleAPIEvent: Could not create directfund objective for ${JSONbigNative.stringify(or)}: ${err}`), + new WrappedError(`handleAPIEvent: Could not create directfund objective for ${JSONbigNative.stringify(or)}: ${err}`, err as Error), ]; } @@ -850,7 +856,10 @@ export class Engine { } catch (err) { return [ failedEngineEvent, - new Error(`handleAPIEvent: Could not create directdefund objective for ${JSONbigNative.stringify(request)}: ${err}`), + new WrappedError( + `handleAPIEvent: Could not create directdefund objective for ${JSONbigNative.stringify(request)}: ${err}`, + err as Error, + ), ]; } @@ -863,7 +872,7 @@ export class Engine { } catch (err) { return [ failedEngineEvent, - new Error(`handleAPIEvent: Could not destroy consensus channel for ${JSONbigNative.stringify(request)}: ${err}`), + new WrappedError(`handleAPIEvent: Could not destroy consensus channel for ${JSONbigNative.stringify(request)}: ${err}`, err as Error), ]; } diff --git a/packages/nitro-util/DEVELOPMENT.md b/packages/nitro-util/DEVELOPMENT.md new file mode 100644 index 00000000..a4c99833 --- /dev/null +++ b/packages/nitro-util/DEVELOPMENT.md @@ -0,0 +1,28 @@ +# Development + +## Generate contract bindings + +* Clone the go-nitro repo () and run `yarn` in root of repo. + +* Move to `go-nitro/packages/nitro-protocol/` and run `yarn hardhat compile` to compile the contract + + ```bash + $ yarn hardhat compile + Generating typings for: 43 artifacts in dir: typechain-types for target: ethers-v5 + Successfully generated 67 typings! + Compiled 42 Solidity files successfully + ``` + +* Copy files `ConsensusApp.json` `NitroAdjudicator.json` `VirtualPaymentApp.json` from go-nitro `packages/nitro-protocol/artifacts` to ts-nitro `packages/nitro-util/contracts` + +* In ts-nitro `packages/nitro-util` run `yarn generate-bindings` to generate the contract bindings + + ```bash + $ yarn generate-bindings + + yarn run v1.22.19 + $ ./scripts/generate-bindings.sh + $ /ts-nitro/node_modules/.bin/typechain --target ethers-v5 --out-dir ./src/contract-bindings ./contracts/NitroAdjudicator.json ./contracts/ConsensusApp.json ./contracts/VirtualPaymentApp.json ./contracts/Token.json + Successfully generated 11 typings! + Done in 1.27s. + ``` diff --git a/packages/nitro-util/README.md b/packages/nitro-util/README.md new file mode 100644 index 00000000..71f3412e --- /dev/null +++ b/packages/nitro-util/README.md @@ -0,0 +1,5 @@ +# nitro-util + +## Development + +* [README](./DEVELOPMENT.md) diff --git a/packages/nitro-util/contracts/NitroAdjudicator.json b/packages/nitro-util/contracts/NitroAdjudicator.json index 1d8539bb..1e7c6d43 100644 --- a/packages/nitro-util/contracts/NitroAdjudicator.json +++ b/packages/nitro-util/contracts/NitroAdjudicator.json @@ -288,6 +288,25 @@ "name": "ChallengeRegistered", "type": "event" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "bytes32", + "name": "channelId", + "type": "bytes32" + }, + { + "indexed": false, + "internalType": "uint48", + "name": "newTurnNumRecord", + "type": "uint48" + } + ], + "name": "Checkpointed", + "type": "event" + }, { "anonymous": false, "inputs": [ diff --git a/packages/nitro-util/package.json b/packages/nitro-util/package.json index 6e0879ae..ce7fb91b 100644 --- a/packages/nitro-util/package.json +++ b/packages/nitro-util/package.json @@ -39,7 +39,7 @@ }, "dependencies": { "@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1", - "@statechannels/nitro-protocol": "^2.0.1-alpha.5", + "@statechannels/nitro-protocol": "^2.0.1-alpha.6", "assert": "^2.0.0", "debug": "^4.3.4", "ethers": "^5.7.2", diff --git a/packages/nitro-util/src/contract-bindings/NitroAdjudicator.ts b/packages/nitro-util/src/contract-bindings/NitroAdjudicator.ts index f80468ee..46b8006a 100644 --- a/packages/nitro-util/src/contract-bindings/NitroAdjudicator.ts +++ b/packages/nitro-util/src/contract-bindings/NitroAdjudicator.ts @@ -302,6 +302,7 @@ export interface NitroAdjudicatorInterface extends utils.Interface { "AllocationUpdated(bytes32,uint256,uint256,uint256)": EventFragment; "ChallengeCleared(bytes32,uint48)": EventFragment; "ChallengeRegistered(bytes32,uint48,tuple[],tuple)": EventFragment; + "Checkpointed(bytes32,uint48)": EventFragment; "Concluded(bytes32,uint48)": EventFragment; "Deposited(bytes32,address,uint256)": EventFragment; "Reclaimed(bytes32,uint256)": EventFragment; @@ -310,6 +311,7 @@ export interface NitroAdjudicatorInterface extends utils.Interface { getEvent(nameOrSignatureOrTopic: "AllocationUpdated"): EventFragment; getEvent(nameOrSignatureOrTopic: "ChallengeCleared"): EventFragment; getEvent(nameOrSignatureOrTopic: "ChallengeRegistered"): EventFragment; + getEvent(nameOrSignatureOrTopic: "Checkpointed"): EventFragment; getEvent(nameOrSignatureOrTopic: "Concluded"): EventFragment; getEvent(nameOrSignatureOrTopic: "Deposited"): EventFragment; getEvent(nameOrSignatureOrTopic: "Reclaimed"): EventFragment; @@ -360,6 +362,17 @@ export type ChallengeRegisteredEvent = TypedEvent< export type ChallengeRegisteredEventFilter = TypedEventFilter; +export interface CheckpointedEventObject { + channelId: string; + newTurnNumRecord: number; +} +export type CheckpointedEvent = TypedEvent< + [string, number], + CheckpointedEventObject +>; + +export type CheckpointedEventFilter = TypedEventFilter; + export interface ConcludedEventObject { channelId: string; finalizesAt: number; @@ -781,6 +794,15 @@ export interface NitroAdjudicator extends BaseContract { candidate?: null ): ChallengeRegisteredEventFilter; + "Checkpointed(bytes32,uint48)"( + channelId?: BytesLike | null, + newTurnNumRecord?: null + ): CheckpointedEventFilter; + Checkpointed( + channelId?: BytesLike | null, + newTurnNumRecord?: null + ): CheckpointedEventFilter; + "Concluded(bytes32,uint48)"( channelId?: BytesLike | null, finalizesAt?: null diff --git a/packages/nitro-util/src/contract-bindings/factories/NitroAdjudicator__factory.ts b/packages/nitro-util/src/contract-bindings/factories/NitroAdjudicator__factory.ts index e38a633e..e4259b11 100644 --- a/packages/nitro-util/src/contract-bindings/factories/NitroAdjudicator__factory.ts +++ b/packages/nitro-util/src/contract-bindings/factories/NitroAdjudicator__factory.ts @@ -295,6 +295,25 @@ const _abi = [ name: "ChallengeRegistered", type: "event", }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: "bytes32", + name: "channelId", + type: "bytes32", + }, + { + indexed: false, + internalType: "uint48", + name: "newTurnNumRecord", + type: "uint48", + }, + ], + name: "Checkpointed", + type: "event", + }, { anonymous: false, inputs: [ diff --git a/packages/server/src/index.ts b/packages/server/src/index.ts index bb6be336..a8be87fc 100644 --- a/packages/server/src/index.ts +++ b/packages/server/src/index.ts @@ -279,8 +279,6 @@ const main = async () => { await new Promise((resolve) => { setTimeout(() => resolve(), 1000); }); await nitro.close(); - - process.exit(0); } }; diff --git a/yarn.lock b/yarn.lock index 8056888b..a20e85ba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3949,10 +3949,10 @@ ethers "^5.1.4" lodash "^4.17.21" -"@statechannels/nitro-protocol@^2.0.1-alpha.5": - version "2.0.1-alpha.5" - resolved "https://registry.yarnpkg.com/@statechannels/nitro-protocol/-/nitro-protocol-2.0.1-alpha.5.tgz#3aa09dacf6780a47ff415de23123f9aafe2534ec" - integrity sha512-CT32i5ZlZ0Jz8mAOmywALCnS/+Cl7ntrRlJr2r3jGBkkzNBWxHWR4qXg6HTUh7xxm5tY/g/TSmsGH8/z54K/Sw== +"@statechannels/nitro-protocol@^2.0.1-alpha.6": + version "2.0.1-alpha.6" + resolved "https://registry.yarnpkg.com/@statechannels/nitro-protocol/-/nitro-protocol-2.0.1-alpha.6.tgz#1bb59365eb78489eef2dfa73733bc1cad9890685" + integrity sha512-VehCgq3AOVTGCvGGIoF23YyQX+x1IbLzByFWbqPovucsm9vW0udR7r8Okw9hC0ZHanHOjQH5KxJL6aKUnrXinw== dependencies: "@openzeppelin/contracts" "^4.7.3" "@statechannels/exit-format" "^0.2.0"