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/*"
],
"version": "0.2.60",
"version": "0.2.61",
"npmClient": "yarn",
"useWorkspaces": true,
"command": {

View File

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

View File

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

View File

@ -38,7 +38,7 @@ import type {
Peer
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1319854183
} 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)
import type { Libp2p } from '@cerc-io/libp2p';
@ -55,7 +55,7 @@ export class ServerCmd {
_argv?: Arguments;
_baseCmd: BaseCmd;
_peer?: Peer;
_nitro?: NitroNode;
_nitro?: utils.Nitro;
_consensus?: Consensus;
constructor () {
@ -82,7 +82,7 @@ export class ServerCmd {
return this._peer;
}
get nitro (): NitroNode | undefined {
get nitro (): utils.Nitro | undefined {
return this._nitro;
}
@ -235,7 +235,7 @@ export class ServerCmd {
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
const {
server: {
@ -246,7 +246,8 @@ export class ServerCmd {
},
upstream: {
ethServer: {
rpcProviderEndpoint
rpcProviderEndpoint,
rpcProviderMutationEndpoint
}
}
} = this._baseCmd.config;
@ -257,17 +258,26 @@ export class ServerCmd {
}
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,
rpcProviderEndpoint,
chainUrl,
nitroConfig.chainPrivateKey,
nitroContractAddresses,
this.peer,
path.resolve(nitroConfig.store)
);
this._nitro = nitro.node;
log(`Nitro node started with address: ${this._nitro.address}`);
log(`Nitro node started with address: ${this._nitro.node.address}`);
return this._nitro;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@
//
import debug from 'debug';
import assert from 'assert';
import { LRUCache } from 'lru-cache';
import { FieldNode } from 'graphql';
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 type { ReadWriteChannel } from '@cerc-io/ts-channel';
import type { Node, Voucher } from '@cerc-io/nitro-node';
import { utils as nitroUtils, ChannelStatus } 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 { BaseRatesConfig, PaymentsConfig } from './config';
import { BaseRatesConfig, NitroPeerConfig, PaymentsConfig } from './config';
const log = debug('laconic:payments');
@ -54,7 +55,7 @@ interface Payment {
}
export class PaymentsManager {
clientAddress?: string;
nitro: nitroUtils.Nitro;
private config: PaymentsConfig;
private ratesConfig: BaseRatesConfig;
@ -69,7 +70,10 @@ export class PaymentsManager {
private stopSubscriptionLoop: ReadWriteChannel<void>;
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.ratesConfig = baseRatesConfig;
@ -98,13 +102,11 @@ export class PaymentsManager {
return this.ratesConfig.mutations ?? {};
}
async subscribeToVouchers (node: Node): Promise<void> {
this.clientAddress = node.address;
async subscribeToVouchers (): Promise<void> {
// 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...');
while (true) {
@ -119,7 +121,7 @@ export class PaymentsManager {
return;
}
const associatedPaymentChannel = await node.getPaymentChannel(voucher.channelId);
const associatedPaymentChannel = await this.nitro.node.getPaymentChannel(voucher.channelId);
const payer = associatedPaymentChannel.balance.payer;
if (!voucher.amount) {
@ -158,7 +160,7 @@ export class PaymentsManager {
}
async unSubscribeVouchers (): Promise<void> {
await this.stopSubscriptionLoop.close();
this.stopSubscriptionLoop.close();
}
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
// Returns whether the voucher was found, whether it was of sufficient value
private acceptReceivedPayment (voucherHash:string, signerAddress: string, minRequiredValue: bigint): [boolean, boolean] {
@ -261,12 +310,12 @@ export class PaymentsManager {
return [true, true];
}
private async loadPaymentChannels (node: Node): Promise<void> {
const ledgerChannels = await node.getAllLedgerChannels();
private async loadPaymentChannels (): Promise<void> {
const ledgerChannels = await this.nitro.node.getAllLedgerChannels();
for await (const ledgerChannel of ledgerChannels) {
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) {
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 => {