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

New batcher payment primitive #421

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
5,744 changes: 2,812 additions & 2,932 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.9.1",
"@nx/esbuild": "19.5.7",
"@nx/js": "19.5.7",
"@nx/vite": "19.5.7",
"@nx/linter": "19.5.7",
"@nx/esbuild": "^19.7.3",
"@nx/js": "^19.7.3",
"@nx/linter": "^19.7.3",
"@nx/vite": "^19.7.3",
"@types/eslint-plugin-prettier": "^3.1.3",
"@types/node": "^20.11.0",
"@typescript-eslint/eslint-plugin": "^7.17.0",
Expand All @@ -39,7 +39,7 @@
"eslint-plugin-require-extensions": "^0.1.3",
"husky": "^9.1.5",
"json5": "^2.2.3",
"nx": "19.5.7",
"nx": "^19.7.3",
"prettier": "^3.2.3",
"prettier-plugin-organize-imports": "^4.0.0",
"prettier-plugin-solidity": "^1.4.1",
Expand Down
25 changes: 21 additions & 4 deletions packages/batcher/batcher-transaction-poster/src/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@ class EvmBatchedTransactionPoster extends BatchedTransactionPosterBase {
this.provider = newProvider;
};

public override postMessage = async (
msg: string,
hashes: string[]
): Promise<[number, string]> => {
public estimateGasLimit = async (msg: string): Promise<bigint> => {
const { populatedTx } = await this.buildTransaction(msg);
const estimate = await this.provider.estimateGasLimit(populatedTx);
return estimate;
};

private buildTransaction = async (
msg: string
): Promise<{
txRequest: ethers.TransactionRequest;
populatedTx: ethers.TransactionRequest;
serializedSignedTx: ethers.Transaction;
}> => {
const hexMsg = utf8ToHex(msg);
// todo: unify with buildDirectTx
const iface = new ethers.Interface([
Expand All @@ -66,6 +75,14 @@ class EvmBatchedTransactionPoster extends BatchedTransactionPosterBase {
const serializedSignedTx = ethers.Transaction.from(
await this.provider.getConnection().api.signTransaction(populatedTx)
);
return { txRequest, populatedTx, serializedSignedTx };
};

public override postMessage = async (
msg: string,
hashes: string[]
): Promise<[number, string]> => {
const { txRequest, serializedSignedTx } = await this.buildTransaction(msg);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.provider.finalizeTransaction actually makes network calls, so it's best not to repeat this work if possible


const [transaction] = await Promise.all([
this.provider.sendTransaction(txRequest),
Expand Down
4 changes: 4 additions & 0 deletions packages/batcher/batcher-transaction-poster/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ import EvmBatchedTransactionPoster from './evm.js';
import BatchedTransactionPoster from './transactionPoster.js';

export { AvailBatchedTransactionPoster, EvmBatchedTransactionPoster, BatchedTransactionPoster };

export class BatchedTransactionPosterStore {
static reference: BatchedTransactionPoster | null = null;
}
9 changes: 4 additions & 5 deletions packages/batcher/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { Pool } from 'pg';
import {
AvailBatchedTransactionPoster,
EvmBatchedTransactionPoster,
BatchedTransactionPosterStore,
} from '@paima/batcher-transaction-poster';
import type { BatchedTransactionPoster } from '@paima/batcher-transaction-poster';
import { server, startServer } from '@paima/batcher-webserver';
Expand Down Expand Up @@ -43,8 +44,6 @@ setLogger(s => {
});

const MINIMUM_INTER_CATCH_PERIOD = 1000;
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
let batchedTransactionPosterReference: BatchedTransactionPoster | null = null;
let lastCatchDate = Date.now();
let reinitializingWeb3 = false;

Expand All @@ -68,11 +67,11 @@ process.on('uncaughtException', async err => {
}
lastCatchDate = Date.now();
console.error(err);
if (!reinitializingWeb3 && batchedTransactionPosterReference) {
if (!reinitializingWeb3 && BatchedTransactionPosterStore.reference) {
reinitializingWeb3 = true;
const walletWeb3 = await getAndConfirmWeb3(ENV.CHAIN_URI, ENV.BATCHER_PRIVATE_KEY, 1000);
reinitializingWeb3 = false;
batchedTransactionPosterReference.updateProvider(walletWeb3);
BatchedTransactionPosterStore.reference.updateProvider(walletWeb3);
}
});

Expand Down Expand Up @@ -248,7 +247,7 @@ async function main(): Promise<void> {

const gameInputValidatorCore = await getValidatorCore(ENV.GAME_INPUT_VALIDATION_TYPE);
const gameInputValidator = new GameInputValidator(gameInputValidatorCore, pool);
batchedTransactionPosterReference = batchedTransactionPoster;
BatchedTransactionPosterStore.reference = batchedTransactionPoster;
await batchedTransactionPoster.initialize();

const runtime = BatcherRuntime.initialize(pool);
Expand Down
5 changes: 5 additions & 0 deletions packages/batcher/utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export class ENV {
return process.env.MAX_GAS_PER_BYTE ? parseInt(process.env.MAX_GAS_PER_BYTE, 10) : 32;
}

// Batcher Payment Contract
static get BATCHER_PAYMENT_ENABLED(): boolean {
return ENV.isTrue(process.env.BATCHER_PAYMENT_ENABLED);
}

// Webserver:
static get BATCHER_PORT(): number {
return parseInt(process.env.BATCHER_PORT || '0', 10);
Expand Down
3 changes: 2 additions & 1 deletion packages/batcher/webserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"axios": "^1.7.2",
"cors": "^2.8.5",
"express": "^4.18.1",
"pg": "^8.11.3"
"pg": "^8.11.3",
"web3": "1.10.0"
},
"devDependencies": {
"@types/cors": "^2.8.12",
Expand Down
84 changes: 51 additions & 33 deletions packages/batcher/webserver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { createMessageForBatcher } from '@paima/concise';
import { AddressType, getWriteNamespace, wait } from '@paima/utils';
import { hashBatchSubunit } from '@paima/concise';
import { RecaptchaError, reCaptchaValidation } from './recaptcha.js';
import { BatcherPayment, BatcherPaymentError } from './payment.js';

const port = ENV.BATCHER_PORT;

Expand Down Expand Up @@ -405,49 +406,66 @@ async function initializeServer(
return;
}

if (validity === 0) {
if (validity !== 0) {
res.status(400).json({
success: false,
message: errorCodeToMessage(validity),
code: validity,
});
return;
}
const payload = {
address_type: addressType,
user_address: userAddress,
game_input: gameInput,
millisecond_timestamp: millisecondTimestamp,
user_signature: userSignature,
};

let weiUsed = BigInt(0);
if (ENV.BATCHER_PAYMENT_ENABLED) {
try {
await reCaptchaValidation(captcha);
unsetWebserverClosed();
await insertStateValidating.run({ input_hash: hash }, pool);
await insertUnvalidatedInput.run(
{
address_type: addressType,
user_address: userAddress,
game_input: gameInput,
millisecond_timestamp: millisecondTimestamp,
user_signature: userSignature,
},
pool
);
setWebserverClosed();
weiUsed = await BatcherPayment.estimateGasFee(JSON.stringify(payload));
await BatcherPayment.hasEnoughBalance(userAddress, weiUsed);
BatcherPayment.addTemporalGasFee(userAddress, weiUsed);
} catch (err) {
if (err instanceof RecaptchaError) {
console.log('[webserver] Recaptcha validation failed.');
console.log({ addressType, userAddress, gameInput, millisecondTimestamp });
} else {
console.log('[webserver] Error while setting input as validated.');
}
res.status(500).json({
console.log('[webserver] Not enough funds.');
res.status(400).json({
success: false,
message: 'Internal server error',
message: 'Insufficient funds',
});
return;
}
console.log('[webserver] Input has been accepted!');
res.status(200).json({
success: true,
hash: hash,
});
return;
} else {
res.status(400).json({
}

try {
await reCaptchaValidation(captcha);
unsetWebserverClosed();
await insertStateValidating.run({ input_hash: hash }, pool);
await insertUnvalidatedInput.run(payload, pool);
setWebserverClosed();
} catch (err) {
if (err instanceof RecaptchaError) {
console.log('[webserver] Recaptcha validation failed.');
console.log({ addressType, userAddress, gameInput, millisecondTimestamp });
} else {
console.log('[webserver] Error while setting input as validated.');
}
if (ENV.BATCHER_PAYMENT_ENABLED) {
BatcherPayment.revertTemporalGasFee(userAddress, weiUsed);
}
res.status(500).json({
success: false,
message: errorCodeToMessage(validity),
code: validity,
message: 'Internal server error',
});
return;
}
console.log('[webserver] Input has been accepted!');
res.status(200).json({
success: true,
hash: hash,
});
return;
} catch (err) {
console.log('[webserver/submit_user_input] error:', err);
res.status(500).json({
Expand Down
127 changes: 127 additions & 0 deletions packages/batcher/webserver/src/payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { ENV } from '@paima/batcher-utils';
import axios from 'axios';
import Web3 from 'web3';
import {
EvmBatchedTransactionPoster,
BatchedTransactionPosterStore,
AvailBatchedTransactionPoster,
} from '@paima/batcher-transaction-poster';
import { BuiltinEvents, PaimaEventManager } from '@paima/events';
export class BatcherPaymentError extends Error {}

type TemporalFees = bigint[];
type Address = string;

export class BatcherPayment {
private static isInitialized = false;

/** In memory storage for user balances */
private static userAddressBalance: Map<Address, TemporalFees> = new Map();

/** Subtract funds temporally from account until updated */
public static addTemporalGasFee(user_address: string, fee: bigint): void {
if (fee < 0) throw new BatcherPaymentError('Expected Positive Value');
let accountBalance = BatcherPayment.userAddressBalance.get(user_address);
if (!accountBalance) {
accountBalance = [];
BatcherPayment.userAddressBalance.set(user_address, accountBalance);
}
accountBalance.push(fee);
}

/** Revert funds that where temporally subtracted from account */
public static revertTemporalGasFee(user_address: string, fee: bigint): void {
if (fee < 0) throw new BatcherPaymentError('Expected Positive Value');
const accountBalance = BatcherPayment.userAddressBalance.get(user_address);
if (!accountBalance) return;

const index = accountBalance.indexOf(fee);
if (index >= 0) accountBalance.splice(index, 1);
if (accountBalance.length === 0) BatcherPayment.userAddressBalance.delete(user_address);
}

/** Removes best temporal match */
private static chargeGasFee(user_address: string, fee: bigint): void {
if (fee < 0) throw new BatcherPaymentError('Expected Positive Value');
const accountBalance = BatcherPayment.userAddressBalance.get(user_address);
if (!accountBalance) return;

const bestMatch = accountBalance
.map(f => ({ error: f - fee, value: f }))
.sort((a, b) => Number(a.error - b.error)) // we want to remove the closest match
.map(f => f.value);
bestMatch.shift();
if (bestMatch.length === 0) BatcherPayment.userAddressBalance.delete(user_address);
else BatcherPayment.userAddressBalance.set(user_address, bestMatch);
}

/** Get estimation of gas used for message */
public static async estimateGasFee(message: string): Promise<bigint> {
if (BatchedTransactionPosterStore.reference instanceof EvmBatchedTransactionPoster) {
const gasUsed = await BatchedTransactionPosterStore.reference.estimateGasLimit(message);
const weiUsed = 1000000000n * gasUsed;
return weiUsed;
} else if (BatchedTransactionPosterStore.reference instanceof AvailBatchedTransactionPoster) {
console.log('NYI: estimateGasLimit is not implemented for AvailBatchedTransactionPoster');
throw new BatcherPaymentError();
} else {
console.log('NYI: estimateGaLimit is not implemented for BatchedTransactionPoster');
throw new BatcherPaymentError();
}
}

/** Check if user has enough balance to pay for gas */
public static async hasEnoughBalance(user_address: string, gasInWei: bigint): Promise<void> {
if (!BatcherPayment.isInitialized)
throw new BatcherPaymentError('BatcherPayment not initialized');

const balance = await BatcherPayment.getBalance(user_address);
if (balance <= gasInWei) throw new BatcherPaymentError();
}

/** Fetch current balance from game-node and subtract temporal fees */
public static async getBalance(user_address: string): Promise<bigint> {
// Fetch balance from Game Node
const address = new Web3().eth.accounts.privateKeyToAccount(ENV.BATCHER_PRIVATE_KEY);
const url = `${ENV.GAME_NODE_URI}/batcher_payment`;
const result = await axios.get<{ result: { balance: string } }>(url, {
params: { batcher_address: address.address, user_address },
});
if (result.status < 200 || result.status >= 300)
throw new BatcherPaymentError('Error fetching balance');

const temporalFees = BatcherPayment.userAddressBalance.get(user_address) ?? [];
return BigInt(result.data.result.balance) - temporalFees.reduce((a, b) => a + b, 0n);
}

/** Initialize BatcherPayment System. Read Paima-Events that contain real fees and added funds. */
public static async init(): Promise<void> {
if (BatcherPayment.isInitialized) return;
BatcherPayment.isInitialized = true;
const batcherAddress = new Web3().eth.accounts
.privateKeyToAccount(ENV.BATCHER_PRIVATE_KEY)
.address.toLocaleLowerCase();
console.log(`Starting BatcherPayment System For Batcher Address ${batcherAddress}`);
await PaimaEventManager.Instance.subscribe(
{ topic: BuiltinEvents.BatcherPayment, filter: { batcherAddress } },
data => {
const { userAddress, operation, value } = data;
switch (operation) {
case 'payGas':
BatcherPayment.chargeGasFee(userAddress, BigInt(value));
break;
case 'addFunds':
// Do nothing, as current balance is fetched from game-node
break;
}
}
);
}
}

// Listen to MQTT events and update storage.
if (ENV.BATCHER_PAYMENT_ENABLED) {
BatcherPayment.init().catch(err =>
console.log('CRITICAL ERROR: Could not start BatcherPayment System', err)
);
}
2 changes: 2 additions & 0 deletions packages/batcher/webserver/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
{ "path": "../address-validator/" },
{ "path": "../db/" },
{ "path": "../utils" },
{ "path": "../batcher-transaction-poster" },
{ "path": "../../paima-sdk/paima-providers/tsconfig.build.json" },
{ "path": "../../paima-sdk/paima-concise/tsconfig.build.json" },
{ "path": "../../paima-sdk/paima-utils/tsconfig.build.json" },
{ "path": "../../paima-sdk/paima-events/tsconfig.build.json" },
]
}
Loading