Support payments to upstream ETH server for RPC requests (#418)

* Add config option for upstream RPC mutation endpoint

* Use mutation endpoint as chain URL for Nitro node

* Add a method to setup an upstream payment channel

* Add a method to send payments to upstream Nitro node

* Upgrade package versions
This commit is contained in:
prathamesh0 2023-09-28 15:24:45 +05:30 committed by GitHub
parent 32c78a907c
commit e203154ac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 145 additions and 59 deletions

View File

@ -2,7 +2,7 @@
"packages": [ "packages": [
"packages/*" "packages/*"
], ],
"version": "0.2.60", "version": "0.2.61",
"npmClient": "yarn", "npmClient": "yarn",
"useWorkspaces": true, "useWorkspaces": true,
"command": { "command": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/cache", "name": "@cerc-io/cache",
"version": "0.2.60", "version": "0.2.61",
"description": "Generic object cache", "description": "Generic object cache",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/cli", "name": "@cerc-io/cli",
"version": "0.2.60", "version": "0.2.61",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"scripts": { "scripts": {
@ -12,13 +12,13 @@
}, },
"dependencies": { "dependencies": {
"@apollo/client": "^3.7.1", "@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.60", "@cerc-io/cache": "^0.2.61",
"@cerc-io/ipld-eth-client": "^0.2.60", "@cerc-io/ipld-eth-client": "^0.2.61",
"@cerc-io/libp2p": "^0.42.2-laconic-0.1.4", "@cerc-io/libp2p": "^0.42.2-laconic-0.1.4",
"@cerc-io/nitro-node": "^0.1.11", "@cerc-io/nitro-node": "^0.1.11",
"@cerc-io/peer": "^0.2.60", "@cerc-io/peer": "^0.2.61",
"@cerc-io/rpc-eth-client": "^0.2.60", "@cerc-io/rpc-eth-client": "^0.2.61",
"@cerc-io/util": "^0.2.60", "@cerc-io/util": "^0.2.61",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@graphql-tools/utils": "^9.1.1", "@graphql-tools/utils": "^9.1.1",
"@ipld/dag-cbor": "^8.0.0", "@ipld/dag-cbor": "^8.0.0",

View File

@ -38,7 +38,7 @@ import type {
Peer Peer
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183 // @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
} from '@cerc-io/peer'; } from '@cerc-io/peer';
import { Node as NitroNode, utils } from '@cerc-io/nitro-node'; import { utils } from '@cerc-io/nitro-node';
// @ts-expect-error TODO: Resolve (Not able to find the type declarations) // @ts-expect-error TODO: Resolve (Not able to find the type declarations)
import type { Libp2p } from '@cerc-io/libp2p'; import type { Libp2p } from '@cerc-io/libp2p';
@ -55,7 +55,7 @@ export class ServerCmd {
_argv?: Arguments; _argv?: Arguments;
_baseCmd: BaseCmd; _baseCmd: BaseCmd;
_peer?: Peer; _peer?: Peer;
_nitro?: NitroNode; _nitro?: utils.Nitro;
_consensus?: Consensus; _consensus?: Consensus;
constructor () { constructor () {
@ -82,7 +82,7 @@ export class ServerCmd {
return this._peer; return this._peer;
} }
get nitro (): NitroNode | undefined { get nitro (): utils.Nitro | undefined {
return this._nitro; return this._nitro;
} }
@ -235,7 +235,7 @@ export class ServerCmd {
return this._consensus; return this._consensus;
} }
async initNitro (nitroContractAddresses: { [key: string]: string }): Promise<NitroNode | undefined> { async initNitro (nitroContractAddresses: { [key: string]: string }): Promise<utils.Nitro | undefined> {
// Start a Nitro node // Start a Nitro node
const { const {
server: { server: {
@ -246,7 +246,8 @@ export class ServerCmd {
}, },
upstream: { upstream: {
ethServer: { ethServer: {
rpcProviderEndpoint rpcProviderEndpoint,
rpcProviderMutationEndpoint
} }
} }
} = this._baseCmd.config; } = this._baseCmd.config;
@ -257,17 +258,26 @@ export class ServerCmd {
} }
assert(this.peer); assert(this.peer);
const nitro = await utils.Nitro.setupNode(
let chainUrl: string;
if (rpcProviderMutationEndpoint) {
log('Using rpcProviderMutationEndpoint as chain URL for Nitro node');
chainUrl = rpcProviderMutationEndpoint;
} else {
log('Using rpcProviderEndpoint as chain URL for Nitro node');
chainUrl = rpcProviderEndpoint;
}
this._nitro = await utils.Nitro.setupNode(
nitroConfig.privateKey, nitroConfig.privateKey,
rpcProviderEndpoint, chainUrl,
nitroConfig.chainPrivateKey, nitroConfig.chainPrivateKey,
nitroContractAddresses, nitroContractAddresses,
this.peer, this.peer,
path.resolve(nitroConfig.store) path.resolve(nitroConfig.store)
); );
this._nitro = nitro.node; log(`Nitro node started with address: ${this._nitro.node.address}`);
log(`Nitro node started with address: ${this._nitro.address}`);
return this._nitro; return this._nitro;
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/codegen", "name": "@cerc-io/codegen",
"version": "0.2.60", "version": "0.2.61",
"description": "Code generator", "description": "Code generator",
"private": true, "private": true,
"main": "index.js", "main": "index.js",
@ -20,7 +20,7 @@
}, },
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@cerc-io/util": "^0.2.60", "@cerc-io/util": "^0.2.61",
"@graphql-tools/load-files": "^6.5.2", "@graphql-tools/load-files": "^6.5.2",
"@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git", "@poanet/solidity-flattener": "https://github.com/vulcanize/solidity-flattener.git",
"@solidity-parser/parser": "^0.13.2", "@solidity-parser/parser": "^0.13.2",

View File

@ -41,12 +41,12 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/cli": "^0.2.60", "@cerc-io/cli": "^0.2.61",
"@cerc-io/ipld-eth-client": "^0.2.60", "@cerc-io/ipld-eth-client": "^0.2.61",
"@cerc-io/solidity-mapper": "^0.2.60", "@cerc-io/solidity-mapper": "^0.2.61",
"@cerc-io/util": "^0.2.60", "@cerc-io/util": "^0.2.61",
{{#if (subgraphPath)}} {{#if (subgraphPath)}}
"@cerc-io/graph-node": "^0.2.60", "@cerc-io/graph-node": "^0.2.61",
{{/if}} {{/if}}
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"apollo-type-bigint": "^0.1.3", "apollo-type-bigint": "^0.1.3",

View File

@ -1,10 +1,10 @@
{ {
"name": "@cerc-io/graph-node", "name": "@cerc-io/graph-node",
"version": "0.2.60", "version": "0.2.61",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"devDependencies": { "devDependencies": {
"@cerc-io/solidity-mapper": "^0.2.60", "@cerc-io/solidity-mapper": "^0.2.61",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@graphprotocol/graph-ts": "^0.22.0", "@graphprotocol/graph-ts": "^0.22.0",
"@nomiclabs/hardhat-ethers": "^2.0.2", "@nomiclabs/hardhat-ethers": "^2.0.2",
@ -51,9 +51,9 @@
"dependencies": { "dependencies": {
"@apollo/client": "^3.3.19", "@apollo/client": "^3.3.19",
"@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2", "@cerc-io/assemblyscript": "0.19.10-watcher-ts-0.1.2",
"@cerc-io/cache": "^0.2.60", "@cerc-io/cache": "^0.2.61",
"@cerc-io/ipld-eth-client": "^0.2.60", "@cerc-io/ipld-eth-client": "^0.2.61",
"@cerc-io/util": "^0.2.60", "@cerc-io/util": "^0.2.61",
"@types/json-diff": "^0.5.2", "@types/json-diff": "^0.5.2",
"@types/yargs": "^17.0.0", "@types/yargs": "^17.0.0",
"bn.js": "^4.11.9", "bn.js": "^4.11.9",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/ipld-eth-client", "name": "@cerc-io/ipld-eth-client",
"version": "0.2.60", "version": "0.2.61",
"description": "IPLD ETH Client", "description": "IPLD ETH Client",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
@ -20,7 +20,7 @@
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@apollo/client": "^3.7.1", "@apollo/client": "^3.7.1",
"@cerc-io/cache": "^0.2.60", "@cerc-io/cache": "^0.2.61",
"cross-fetch": "^3.1.4", "cross-fetch": "^3.1.4",
"debug": "^4.3.1", "debug": "^4.3.1",
"ethers": "^5.4.4", "ethers": "^5.4.4",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/peer", "name": "@cerc-io/peer",
"version": "0.2.60", "version": "0.2.61",
"description": "libp2p module", "description": "libp2p module",
"main": "dist/index.js", "main": "dist/index.js",
"exports": "./dist/index.js", "exports": "./dist/index.js",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/rpc-eth-client", "name": "@cerc-io/rpc-eth-client",
"version": "0.2.60", "version": "0.2.61",
"description": "RPC ETH Client", "description": "RPC ETH Client",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
@ -19,9 +19,9 @@
}, },
"homepage": "https://github.com/cerc-io/watcher-ts#readme", "homepage": "https://github.com/cerc-io/watcher-ts#readme",
"dependencies": { "dependencies": {
"@cerc-io/cache": "^0.2.60", "@cerc-io/cache": "^0.2.61",
"@cerc-io/ipld-eth-client": "^0.2.60", "@cerc-io/ipld-eth-client": "^0.2.61",
"@cerc-io/util": "^0.2.60", "@cerc-io/util": "^0.2.61",
"chai": "^4.3.4", "chai": "^4.3.4",
"ethers": "^5.4.4", "ethers": "^5.4.4",
"left-pad": "^1.3.0", "left-pad": "^1.3.0",

View File

@ -1,10 +1,10 @@
{ {
"name": "@cerc-io/solidity-mapper", "name": "@cerc-io/solidity-mapper",
"version": "0.2.60", "version": "0.2.61",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"devDependencies": { "devDependencies": {
"@cerc-io/ipld-eth-client": "^0.2.60", "@cerc-io/ipld-eth-client": "^0.2.61",
"@ethersproject/abi": "^5.3.0", "@ethersproject/abi": "^5.3.0",
"@nomiclabs/hardhat-ethers": "^2.0.2", "@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1", "@nomiclabs/hardhat-waffle": "^2.0.1",

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/test", "name": "@cerc-io/test",
"version": "0.2.60", "version": "0.2.61",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"private": true, "private": true,

View File

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/tracing-client", "name": "@cerc-io/tracing-client",
"version": "0.2.60", "version": "0.2.61",
"description": "ETH VM tracing client", "description": "ETH VM tracing client",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {

View File

@ -1,13 +1,13 @@
{ {
"name": "@cerc-io/util", "name": "@cerc-io/util",
"version": "0.2.60", "version": "0.2.61",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"dependencies": { "dependencies": {
"@apollo/utils.keyvaluecache": "^1.0.1", "@apollo/utils.keyvaluecache": "^1.0.1",
"@cerc-io/nitro-node": "^0.1.11", "@cerc-io/nitro-node": "^0.1.11",
"@cerc-io/peer": "^0.2.60", "@cerc-io/peer": "^0.2.61",
"@cerc-io/solidity-mapper": "^0.2.60", "@cerc-io/solidity-mapper": "^0.2.61",
"@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1", "@cerc-io/ts-channel": "1.0.3-ts-nitro-0.1.1",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@graphql-tools/schema": "^9.0.10", "@graphql-tools/schema": "^9.0.10",
@ -49,7 +49,7 @@
"yargs": "^17.0.1" "yargs": "^17.0.1"
}, },
"devDependencies": { "devDependencies": {
"@cerc-io/cache": "^0.2.60", "@cerc-io/cache": "^0.2.61",
"@nomiclabs/hardhat-waffle": "^2.0.1", "@nomiclabs/hardhat-waffle": "^2.0.1",
"@types/bunyan": "^1.8.8", "@types/bunyan": "^1.8.8",
"@types/express": "^4.17.14", "@types/express": "^4.17.14",

View File

@ -224,12 +224,20 @@ export interface ServerConfig {
rpcSupportsBlockHashParam: boolean; rpcSupportsBlockHashParam: boolean;
} }
export interface NitroPeerConfig {
address: string;
multiAddr: string;
amount: string;
}
export interface UpstreamConfig { export interface UpstreamConfig {
cache: CacheConfig; cache: CacheConfig;
ethServer: { ethServer: {
gqlApiEndpoint: string; gqlApiEndpoint: string;
rpcProviderEndpoint: string; rpcProviderEndpoint: string;
rpcProviderMutationEndpoint: string;
rpcClient: boolean; rpcClient: boolean;
rpcProviderNitroNode: NitroPeerConfig;
} }
traceProviderEndpoint: string; traceProviderEndpoint: string;
} }

View File

@ -3,6 +3,7 @@
// //
import debug from 'debug'; import debug from 'debug';
import assert from 'assert';
import { LRUCache } from 'lru-cache'; import { LRUCache } from 'lru-cache';
import { FieldNode } from 'graphql'; import { FieldNode } from 'graphql';
import { ApolloServerPlugin, GraphQLResponse, GraphQLRequestContext } from 'apollo-server-plugin-base'; import { ApolloServerPlugin, GraphQLResponse, GraphQLRequestContext } from 'apollo-server-plugin-base';
@ -10,10 +11,10 @@ import { Response as HTTPResponse } from 'apollo-server-env';
import Channel from '@cerc-io/ts-channel'; import Channel from '@cerc-io/ts-channel';
import type { ReadWriteChannel } from '@cerc-io/ts-channel'; import type { ReadWriteChannel } from '@cerc-io/ts-channel';
import type { Node, Voucher } from '@cerc-io/nitro-node'; import type { Voucher } from '@cerc-io/nitro-node';
import { utils as nitroUtils, ChannelStatus } from '@cerc-io/nitro-node'; import { utils as nitroUtils, ChannelStatus, Destination } from '@cerc-io/nitro-node';
import { BaseRatesConfig, PaymentsConfig } from './config'; import { BaseRatesConfig, NitroPeerConfig, PaymentsConfig } from './config';
const log = debug('laconic:payments'); const log = debug('laconic:payments');
@ -54,7 +55,7 @@ interface Payment {
} }
export class PaymentsManager { export class PaymentsManager {
clientAddress?: string; nitro: nitroUtils.Nitro;
private config: PaymentsConfig; private config: PaymentsConfig;
private ratesConfig: BaseRatesConfig; private ratesConfig: BaseRatesConfig;
@ -69,7 +70,10 @@ export class PaymentsManager {
private stopSubscriptionLoop: ReadWriteChannel<void>; private stopSubscriptionLoop: ReadWriteChannel<void>;
private paymentListeners: ReadWriteChannel<string>[] = []; private paymentListeners: ReadWriteChannel<string>[] = [];
constructor (config: PaymentsConfig, baseRatesConfig: BaseRatesConfig) { private upstreamNodePaymentChannel?: string;
constructor (nitro: nitroUtils.Nitro, config: PaymentsConfig, baseRatesConfig: BaseRatesConfig) {
this.nitro = nitro;
this.config = config; this.config = config;
this.ratesConfig = baseRatesConfig; this.ratesConfig = baseRatesConfig;
@ -98,13 +102,11 @@ export class PaymentsManager {
return this.ratesConfig.mutations ?? {}; return this.ratesConfig.mutations ?? {};
} }
async subscribeToVouchers (node: Node): Promise<void> { async subscribeToVouchers (): Promise<void> {
this.clientAddress = node.address;
// Load existing open payment channels with amount paid so far from the stored state // Load existing open payment channels with amount paid so far from the stored state
await this.loadPaymentChannels(node); await this.loadPaymentChannels();
const receivedVouchersChannel = node.receivedVouchers(); const receivedVouchersChannel = this.nitro.node.receivedVouchers();
log('Starting voucher subscription...'); log('Starting voucher subscription...');
while (true) { while (true) {
@ -119,7 +121,7 @@ export class PaymentsManager {
return; return;
} }
const associatedPaymentChannel = await node.getPaymentChannel(voucher.channelId); const associatedPaymentChannel = await this.nitro.node.getPaymentChannel(voucher.channelId);
const payer = associatedPaymentChannel.balance.payer; const payer = associatedPaymentChannel.balance.payer;
if (!voucher.amount) { if (!voucher.amount) {
@ -158,7 +160,7 @@ export class PaymentsManager {
} }
async unSubscribeVouchers (): Promise<void> { async unSubscribeVouchers (): Promise<void> {
await this.stopSubscriptionLoop.close(); this.stopSubscriptionLoop.close();
} }
async allowRequest (voucherHash: string, signerAddress: string, querySelection: string): Promise<[false, string] | [true, null]> { async allowRequest (voucherHash: string, signerAddress: string, querySelection: string): Promise<[false, string] | [true, null]> {
@ -238,6 +240,53 @@ export class PaymentsManager {
} }
} }
async setupUpstreamPaymentChannel (rpcProviderNitroNode: NitroPeerConfig): Promise<void> {
log(`Adding upstream Nitro node: ${rpcProviderNitroNode.address}`);
await this.nitro.addPeerByMultiaddr(rpcProviderNitroNode.address, rpcProviderNitroNode.multiAddr);
// Create a payment channel with upstream Nitro node
// if it doesn't already exist
const existingPaymentChannel = await this.getPaymentChannelWithPeer(rpcProviderNitroNode.address);
if (existingPaymentChannel) {
this.upstreamNodePaymentChannel = existingPaymentChannel;
log(`Using existing payment channel ${existingPaymentChannel} with upstream Nitro node`);
return;
}
await this.nitro.directFund(
rpcProviderNitroNode.address,
// TODO: Configure amount
1_000_000_000_000
);
this.upstreamNodePaymentChannel = await this.nitro.virtualFund(
rpcProviderNitroNode.address,
// TODO: Configure amount
1_000_000_000
);
// TODO: Handle closures
}
async sendUpstreamPayment (amount: string): Promise<{
channelId: string,
amount: string,
signature: string
}> {
assert(this.upstreamNodePaymentChannel);
const dest = new Destination(this.upstreamNodePaymentChannel);
const voucher = await this.nitro.node.createVoucher(dest, BigInt(amount ?? 0));
assert(voucher.amount);
return {
channelId: voucher.channelId.string(),
amount: voucher.amount.toString(),
signature: voucher.signature.toHexString()
};
}
// Check for a given payment voucher in LRU cache map // Check for a given payment voucher in LRU cache map
// Returns whether the voucher was found, whether it was of sufficient value // Returns whether the voucher was found, whether it was of sufficient value
private acceptReceivedPayment (voucherHash:string, signerAddress: string, minRequiredValue: bigint): [boolean, boolean] { private acceptReceivedPayment (voucherHash:string, signerAddress: string, minRequiredValue: bigint): [boolean, boolean] {
@ -261,12 +310,12 @@ export class PaymentsManager {
return [true, true]; return [true, true];
} }
private async loadPaymentChannels (node: Node): Promise<void> { private async loadPaymentChannels (): Promise<void> {
const ledgerChannels = await node.getAllLedgerChannels(); const ledgerChannels = await this.nitro.node.getAllLedgerChannels();
for await (const ledgerChannel of ledgerChannels) { for await (const ledgerChannel of ledgerChannels) {
if (ledgerChannel.status === ChannelStatus.Open) { if (ledgerChannel.status === ChannelStatus.Open) {
const paymentChannels = await node.getPaymentChannelsByLedger(ledgerChannel.iD); const paymentChannels = await this.nitro.node.getPaymentChannelsByLedger(ledgerChannel.iD);
for (const paymentChannel of paymentChannels) { for (const paymentChannel of paymentChannels) {
if (paymentChannel.status === ChannelStatus.Open) { if (paymentChannel.status === ChannelStatus.Open) {
@ -276,6 +325,25 @@ export class PaymentsManager {
} }
} }
} }
private async getPaymentChannelWithPeer (nitroPeer: string): Promise<string | undefined> {
const ledgerChannels = await this.nitro.node.getAllLedgerChannels();
for await (const ledgerChannel of ledgerChannels) {
if (
ledgerChannel.balance.them !== nitroPeer ||
ledgerChannel.status !== ChannelStatus.Open
) {
continue;
}
const paymentChannels = await this.nitro.node.getPaymentChannelsByLedger(ledgerChannel.iD);
for (const paymentChannel of paymentChannels) {
if (paymentChannel.status === ChannelStatus.Open) {
return paymentChannel.iD.string();
}
}
}
}
} }
export const paymentsPlugin = (paymentsManager?: PaymentsManager): ApolloServerPlugin => { export const paymentsPlugin = (paymentsManager?: PaymentsManager): ApolloServerPlugin => {