Setup config for paid RPC methods and funding amounts with upstream ETH server (#423)

* Add config option for paid RPC methods

* Change rpcProviderNitroNode to nitro

* Add config option for funding amounts

* Handle review changes

* Refactor config and add a method to setup provider with payments

* Upgrade package versions

---------

Co-authored-by: Shreerang Kale <shreerangkale@gmail.com>
This commit is contained in:
prathamesh0 2023-10-04 16:10:54 +05:30 committed by GitHub
parent e203154ac7
commit ae60bbe1ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 153 additions and 59 deletions

View File

@ -2,7 +2,7 @@
"packages": [ "packages": [
"packages/*" "packages/*"
], ],
"version": "0.2.61", "version": "0.2.62",
"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.61", "version": "0.2.62",
"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.61", "version": "0.2.62",
"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.61", "@cerc-io/cache": "^0.2.62",
"@cerc-io/ipld-eth-client": "^0.2.61", "@cerc-io/ipld-eth-client": "^0.2.62",
"@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.61", "@cerc-io/peer": "^0.2.62",
"@cerc-io/rpc-eth-client": "^0.2.61", "@cerc-io/rpc-eth-client": "^0.2.62",
"@cerc-io/util": "^0.2.61", "@cerc-io/util": "^0.2.62",
"@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

@ -1,6 +1,6 @@
{ {
"name": "@cerc-io/codegen", "name": "@cerc-io/codegen",
"version": "0.2.61", "version": "0.2.62",
"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.61", "@cerc-io/util": "^0.2.62",
"@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.61", "@cerc-io/cli": "^0.2.62",
"@cerc-io/ipld-eth-client": "^0.2.61", "@cerc-io/ipld-eth-client": "^0.2.62",
"@cerc-io/solidity-mapper": "^0.2.61", "@cerc-io/solidity-mapper": "^0.2.62",
"@cerc-io/util": "^0.2.61", "@cerc-io/util": "^0.2.62",
{{#if (subgraphPath)}} {{#if (subgraphPath)}}
"@cerc-io/graph-node": "^0.2.61", "@cerc-io/graph-node": "^0.2.62",
{{/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.61", "version": "0.2.62",
"main": "dist/index.js", "main": "dist/index.js",
"license": "AGPL-3.0", "license": "AGPL-3.0",
"devDependencies": { "devDependencies": {
"@cerc-io/solidity-mapper": "^0.2.61", "@cerc-io/solidity-mapper": "^0.2.62",
"@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.61", "@cerc-io/cache": "^0.2.62",
"@cerc-io/ipld-eth-client": "^0.2.61", "@cerc-io/ipld-eth-client": "^0.2.62",
"@cerc-io/util": "^0.2.61", "@cerc-io/util": "^0.2.62",
"@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.61", "version": "0.2.62",
"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.61", "@cerc-io/cache": "^0.2.62",
"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.61", "version": "0.2.62",
"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.61", "version": "0.2.62",
"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.61", "@cerc-io/cache": "^0.2.62",
"@cerc-io/ipld-eth-client": "^0.2.61", "@cerc-io/ipld-eth-client": "^0.2.62",
"@cerc-io/util": "^0.2.61", "@cerc-io/util": "^0.2.62",
"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.61", "version": "0.2.62",
"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.61", "@cerc-io/ipld-eth-client": "^0.2.62",
"@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.61", "version": "0.2.62",
"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.61", "version": "0.2.62",
"description": "ETH VM tracing client", "description": "ETH VM tracing client",
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {

View File

@ -1,15 +1,17 @@
{ {
"name": "@cerc-io/util", "name": "@cerc-io/util",
"version": "0.2.61", "version": "0.2.62",
"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.61", "@cerc-io/peer": "^0.2.62",
"@cerc-io/solidity-mapper": "^0.2.61", "@cerc-io/solidity-mapper": "^0.2.62",
"@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/properties": "^5.7.0",
"@ethersproject/providers": "^5.4.4", "@ethersproject/providers": "^5.4.4",
"@ethersproject/web": "^5.7.1",
"@graphql-tools/schema": "^9.0.10", "@graphql-tools/schema": "^9.0.10",
"@graphql-tools/utils": "^9.1.1", "@graphql-tools/utils": "^9.1.1",
"@ipld/dag-cbor": "^6.0.12", "@ipld/dag-cbor": "^6.0.12",
@ -49,7 +51,7 @@
"yargs": "^17.0.1" "yargs": "^17.0.1"
}, },
"devDependencies": { "devDependencies": {
"@cerc-io/cache": "^0.2.61", "@cerc-io/cache": "^0.2.62",
"@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,9 +224,19 @@ export interface ServerConfig {
rpcSupportsBlockHashParam: boolean; rpcSupportsBlockHashParam: boolean;
} }
export interface FundingAmountsConfig {
directFund: string;
virtualFund: string;
}
export interface NitroPeerConfig { export interface NitroPeerConfig {
address: string; address: string;
multiAddr: string; multiAddr: string;
fundingAmounts: FundingAmountsConfig;
}
export interface EthServerPaymentsConfig {
nitro: NitroPeerConfig;
paidRPCMethods: string[];
amount: string; amount: string;
} }
@ -237,7 +247,7 @@ export interface UpstreamConfig {
rpcProviderEndpoint: string; rpcProviderEndpoint: string;
rpcProviderMutationEndpoint: string; rpcProviderMutationEndpoint: string;
rpcClient: boolean; rpcClient: boolean;
rpcProviderNitroNode: NitroPeerConfig; payments: EthServerPaymentsConfig;
} }
traceProviderEndpoint: string; traceProviderEndpoint: string;
} }

View File

@ -4,6 +4,7 @@
import debug from 'debug'; import debug from 'debug';
import assert from 'assert'; import assert from 'assert';
import { providers } from 'ethers';
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';
@ -13,6 +14,8 @@ 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 { Voucher } from '@cerc-io/nitro-node'; import type { Voucher } from '@cerc-io/nitro-node';
import { utils as nitroUtils, ChannelStatus, Destination } from '@cerc-io/nitro-node'; import { utils as nitroUtils, ChannelStatus, Destination } from '@cerc-io/nitro-node';
import { deepCopy } from '@ethersproject/properties';
import { fetchJson } from '@ethersproject/web';
import { BaseRatesConfig, NitroPeerConfig, PaymentsConfig } from './config'; import { BaseRatesConfig, NitroPeerConfig, PaymentsConfig } from './config';
@ -70,8 +73,6 @@ export class PaymentsManager {
private stopSubscriptionLoop: ReadWriteChannel<void>; private stopSubscriptionLoop: ReadWriteChannel<void>;
private paymentListeners: ReadWriteChannel<string>[] = []; private paymentListeners: ReadWriteChannel<string>[] = [];
private upstreamNodePaymentChannel?: string;
constructor (nitro: nitroUtils.Nitro, config: PaymentsConfig, baseRatesConfig: BaseRatesConfig) { constructor (nitro: nitroUtils.Nitro, config: PaymentsConfig, baseRatesConfig: BaseRatesConfig) {
this.nitro = nitro; this.nitro = nitro;
this.config = config; this.config = config;
@ -240,43 +241,37 @@ export class PaymentsManager {
} }
} }
async setupUpstreamPaymentChannel (rpcProviderNitroNode: NitroPeerConfig): Promise<void> { async setupPaymentChannel (nodeConfig: NitroPeerConfig): Promise<string> {
log(`Adding upstream Nitro node: ${rpcProviderNitroNode.address}`); log(`Adding Nitro node: ${nodeConfig.address}`);
await this.nitro.addPeerByMultiaddr(rpcProviderNitroNode.address, rpcProviderNitroNode.multiAddr); await this.nitro.addPeerByMultiaddr(nodeConfig.address, nodeConfig.multiAddr);
// Create a payment channel with upstream Nitro node // Create a payment channel with the given Nitro node
// if it doesn't already exist // if it doesn't already exist
const existingPaymentChannel = await this.getPaymentChannelWithPeer(rpcProviderNitroNode.address); const existingPaymentChannel = await this.getPaymentChannelWithPeer(nodeConfig.address);
if (existingPaymentChannel) { if (existingPaymentChannel) {
this.upstreamNodePaymentChannel = existingPaymentChannel; log(`Using existing payment channel ${existingPaymentChannel} with Nitro node ${nodeConfig.address}`);
log(`Using existing payment channel ${existingPaymentChannel} with upstream Nitro node`); return existingPaymentChannel;
return;
} }
await this.nitro.directFund( await this.nitro.directFund(
rpcProviderNitroNode.address, nodeConfig.address,
// TODO: Configure amount Number(nodeConfig.fundingAmounts?.directFund || 0)
1_000_000_000_000
); );
this.upstreamNodePaymentChannel = await this.nitro.virtualFund( return this.nitro.virtualFund(
rpcProviderNitroNode.address, nodeConfig.address,
// TODO: Configure amount Number(nodeConfig.fundingAmounts?.virtualFund || 0)
1_000_000_000
); );
// TODO: Handle closures // TODO: Handle closures
} }
async sendUpstreamPayment (amount: string): Promise<{ async sendPayment (destChannelId: string, amount: string): Promise<{
channelId: string, channelId: string,
amount: string, amount: string,
signature: string signature: string
}> { }> {
assert(this.upstreamNodePaymentChannel); const dest = new Destination(destChannelId);
const dest = new Destination(this.upstreamNodePaymentChannel);
const voucher = await this.nitro.node.createVoucher(dest, BigInt(amount ?? 0)); const voucher = await this.nitro.node.createVoucher(dest, BigInt(amount ?? 0));
assert(voucher.amount); assert(voucher.amount);
@ -431,3 +426,90 @@ export const paymentsPlugin = (paymentsManager?: PaymentsManager): ApolloServerP
} }
}; };
}; };
// Helper method to modify a given JsonRpcProvider to make payment for required methods
// and attach the voucher details in reqeust URL
export const setupProviderWithPayments = (
provider: providers.JsonRpcProvider,
paymentsManager: PaymentsManager,
paymentChannelId: string,
paidRPCMethods: string[],
paymentAmount: string
): void => {
// https://github.com/ethers-io/ethers.js/blob/v5.7.2/packages/providers/src.ts/json-rpc-provider.ts#L502
provider.send = async (method: string, params: Array<any>): Promise<any> => {
log(`Making RPC call: ${method}`);
const request = {
method: method,
params: params,
id: (provider._nextId++),
jsonrpc: '2.0'
};
provider.emit('debug', {
action: 'request',
request: deepCopy(request),
provider: provider
});
// We can expand this in the future to any call, but for now these
// are the biggest wins and do not require any serializing parameters.
const cache = (['eth_chainId', 'eth_blockNumber'].indexOf(method) >= 0);
// @ts-expect-error copied code
if (cache && provider._cache[method]) {
return provider._cache[method];
}
// Send a payment to upstream Nitro node and add details to the request URL
let updatedURL = `${provider.connection.url}?method=${method}`;
if (paidRPCMethods.includes(method)) {
const voucher = await paymentsManager.sendPayment(paymentChannelId, paymentAmount);
updatedURL = `${updatedURL}&channelId=${voucher.channelId}&amount=${voucher.amount}&signature=${voucher.signature}`;
}
const result = fetchJson({ ...provider.connection, url: updatedURL }, JSON.stringify(request), getResult).then((result) => {
provider.emit('debug', {
action: 'response',
request: request,
response: result,
provider: provider
});
return result;
}, (error) => {
provider.emit('debug', {
action: 'response',
error: error,
request: request,
provider: provider
});
throw error;
});
// Cache the fetch, but clear it on the next event loop
if (cache) {
provider._cache[method] = result;
setTimeout(() => {
// @ts-expect-error copied code
provider._cache[method] = null;
}, 0);
}
return result;
};
};
// https://github.com/ethers-io/ethers.js/blob/v5.7.2/packages/providers/src.ts/json-rpc-provider.ts#L139
function getResult (payload: { error?: { code?: number, data?: any, message?: string }, result?: any }): any {
if (payload.error) {
// @TODO: not any
const error: any = new Error(payload.error.message);
error.code = payload.error.code;
error.data = payload.error.data;
throw error;
}
return payload.result;
}

View File

@ -1451,7 +1451,7 @@
"@ethersproject/properties" "^5.6.0" "@ethersproject/properties" "^5.6.0"
"@ethersproject/strings" "^5.6.1" "@ethersproject/strings" "^5.6.1"
"@ethersproject/web@5.7.1", "@ethersproject/web@^5.6.1", "@ethersproject/web@^5.7.0": "@ethersproject/web@5.7.1", "@ethersproject/web@^5.6.1", "@ethersproject/web@^5.7.0", "@ethersproject/web@^5.7.1":
version "5.7.1" version "5.7.1"
resolved "https://registry.npmjs.org/@ethersproject/web/-/web-5.7.1.tgz" resolved "https://registry.npmjs.org/@ethersproject/web/-/web-5.7.1.tgz"
integrity sha512-Gueu8lSvyjBWL4cYsWsjh6MtMwM0+H4HvqFPZfB6dV8ctbP9zFAO73VG1cMWae0FLPCtz0peKPpZY8/ugJJX2w== integrity sha512-Gueu8lSvyjBWL4cYsWsjh6MtMwM0+H4HvqFPZfB6dV8ctbP9zFAO73VG1cMWae0FLPCtz0peKPpZY8/ugJJX2w==