Skip to content

Commit

Permalink
made getMainAddress public
Browse files Browse the repository at this point in the history
  • Loading branch information
acedward committed Dec 15, 2023
1 parent e7a465e commit 0e633d5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 53 deletions.
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/engine/paima-funnel/src/paima-l2-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { toBN, hexToUtf8, sha3 } from 'web3-utils';
import { DelegateWallet } from '@paima/sm/src/delegate-wallet';
import type { PoolClient } from 'pg';
import { getMainAddress } from '@paima/db';

interface ValidatedSubmittedData extends SubmittedData {
validated: boolean;
Expand All @@ -38,7 +39,7 @@ async function eventMapper(
DBConn: PoolClient
): Promise<SubmittedData[]> {
const decodedData = decodeEventData(e.returnValues.data);
const address = await new DelegateWallet(DBConn).getMainAddress(e.returnValues.userAddress);
const address = await getMainAddress(e.returnValues.userAddress, DBConn);
return await processDataUnit(
web3,
{
Expand Down Expand Up @@ -150,7 +151,7 @@ async function processBatchedSubunit(

const inputNonce = createBatchNonce(millisecondTimestamp, userAddress, inputData);

const address = await new DelegateWallet(DBConn).getMainAddress(userAddress);
const address = await getMainAddress(userAddress, DBConn);
return {
inputData,
realAddress: userAddress,
Expand Down
3 changes: 1 addition & 2 deletions packages/engine/paima-sm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"author": "Paima Studios",
"dependencies": {
"@sinclair/typebox": "^0.31.28",
"assert-never": "^1.2.1",
"lru-cache": "^10.1.0"
"assert-never": "^1.2.1"
}
}
48 changes: 4 additions & 44 deletions packages/engine/paima-sm/src/delegate-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { LRUCache } from 'lru-cache';
import Web3 from 'web3';
import type { PoolClient } from 'pg';

Expand All @@ -7,20 +6,18 @@ import { ENV, doLog } from '@paima/utils';
import { CryptoManager } from '@paima/crypto';
import type { IGetAddressFromAddressResult } from '@paima/db';
import {
addressCache,
deleteAddress,
deleteDelegationsFrom,
getAddressFromAddress,
getAddressFromId,
getDelegation,
getDelegationsFrom,
getDelegationsTo,
newAddress,
newDelegation,
updateAddress,
} from '@paima/db';

export type WalletDelegate = { address: string; id: number };

type ParsedDelegateWalletCommand =
| {
command: 'delegate';
Expand All @@ -33,8 +30,6 @@ type ParsedDelegateWalletCommand =
| { command: 'cancelDelegations'; args: { to: string; to_signature: string } };

export class DelegateWallet {
public static readonly NO_USER_ID = -1;

private static readonly DELEGATE_WALLET_PREFIX = 'DELEGATE-WALLET';

public static readonly INTERNAL_COMMAND_PREFIX = '&';
Expand Down Expand Up @@ -71,10 +66,6 @@ export class DelegateWallet {
DelegateWallet.parserCommands
);

private static addressCache = new LRUCache<string, WalletDelegate>({
max: 2000,
});

constructor(private DBConn: PoolClient) {}

/* Generate Plaintext Message */
Expand Down Expand Up @@ -202,7 +193,7 @@ export class DelegateWallet {
this.DBConn
);

DelegateWallet.addressCache.clear();
addressCache.clear();
}

// Migrate.
Expand All @@ -213,7 +204,7 @@ export class DelegateWallet {

const toAddress = await this.getOrCreateNewAddress(to);
await this.swap(fromAddress.address, toAddress.address.address);
DelegateWallet.addressCache.clear();
addressCache.clear();
}

// Cancel Delegations.
Expand All @@ -222,38 +213,7 @@ export class DelegateWallet {
const [toAddress] = await getAddressFromAddress.run({ address: to }, this.DBConn);
if (!toAddress) throw new Error('Invalid Address');
await deleteDelegationsFrom.run({ from_id: toAddress.id }, this.DBConn);
DelegateWallet.addressCache.clear();
}

// Get Main Wallet and ID for address.
// If wallet does not exist, It will NOT be created in address, table.
public async getMainAddress(address: string): Promise<WalletDelegate> {
let addressMapping: WalletDelegate | undefined = DelegateWallet.addressCache.get(address);
if (addressMapping) return addressMapping;

// get main address.
// const addressResult = await this.getOrCreateNewAddress(address);
const [addressResult] = await getAddressFromAddress.run({ address }, this.DBConn);
if (!addressResult) {
// This wallet has never been used before.
// This value will get updated before sent to the STF.
return { address, id: DelegateWallet.NO_USER_ID };
}

// if exists we have to check if it is a delegation.
const [delegate] = await getDelegationsTo.run({ set_id: addressResult.id }, this.DBConn);
if (!delegate) {
// is main address or has no delegations.
addressMapping = { address: addressResult.address, id: addressResult.id };
DelegateWallet.addressCache.set(address, addressMapping);
return addressMapping;
}

// if is delegation, get main address.
const [mainAddress] = await getAddressFromId.run({ id: delegate.from_id }, this.DBConn);
addressMapping = { address: mainAddress.address, id: mainAddress.id };
DelegateWallet.addressCache.set(address, addressMapping);
return addressMapping;
addressCache.clear();
}

/*
Expand Down
8 changes: 5 additions & 3 deletions packages/engine/paima-sm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import {
getLatestProcessedCdeBlockheight,
getCardanoLatestProcessedCdeSlot,
markCardanoCdeSlotProcessed,
getMainAddress,
NO_USER_ID,
} from '@paima/db';
import Prando from '@paima/prando';

import { randomnessRouter } from './randomness.js';
import { cdeTransitionFunction } from './cde-processing.js';
import type { WalletDelegate } from './delegate-wallet.js';
import { DelegateWallet } from './delegate-wallet.js';
import type {
SubmittedData,
Expand All @@ -40,6 +41,7 @@ import type {
GameStateTransitionFunction,
GameStateMachineInitializer,
} from './types.js';

export * from './types.js';
export type * from './types.js';

Expand Down Expand Up @@ -130,7 +132,7 @@ const SM: GameStateMachineInitializer = {
const [b] = await getLatestProcessedBlockHeight.run(undefined, dbTx);
const blockHeight = b?.block_height ?? startBlockHeight ?? 0;
const gameStateTransition = gameStateTransitionRouter(blockHeight);
const address = await new DelegateWallet(dbTx).getMainAddress(userAddress);
const address = await getMainAddress(userAddress, dbTx);

const data = await gameStateTransition(
{
Expand Down Expand Up @@ -340,7 +342,7 @@ async function processUserInputs(
await delegateWallet.process(inputData.inputData);
} else {
// If wallet does not exist in address table: create it.
if (inputData.userId === DelegateWallet.NO_USER_ID) {
if (inputData.userId === NO_USER_ID) {
const newAddress = await delegateWallet.getOrCreateNewAddress(inputData.userAddress);
inputData.userId = newAddress.address.id;
}
Expand Down
47 changes: 47 additions & 0 deletions packages/node-sdk/paima-db/src/delegate-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
getAddressFromAddress,
getAddressFromId,
getDelegationsTo,
} from './sql/wallet-delegation.queries.js';
import type { PoolClient } from 'pg';

export type WalletDelegate = { address: string; id: number };
export const NO_USER_ID = -1;

/**
* TODO
* This is a temporal fix as lru-cache module was
* not correctly packaged into a single file.
*/
export const addressCache = new Map<string, WalletDelegate>();

// Get Main Wallet and ID for address.
// If wallet does not exist, It will NOT be created in address, table.
export async function getMainAddress(address: string, DBConn: PoolClient): Promise<WalletDelegate> {
let addressMapping: WalletDelegate | undefined = addressCache.get(address);
if (addressMapping) return addressMapping;

// get main address.
// const addressResult = await this.getOrCreateNewAddress(address);
const [addressResult] = await getAddressFromAddress.run({ address }, DBConn);
if (!addressResult) {
// This wallet has never been used before.
// This value will get updated before sent to the STF.
return { address, id: NO_USER_ID };
}

// if exists we have to check if it is a delegation.
const [delegate] = await getDelegationsTo.run({ set_id: addressResult.id }, DBConn);
if (!delegate) {
// is main address or has no delegations.
addressMapping = { address: addressResult.address, id: addressResult.id };
addressCache.set(address, addressMapping);
return addressMapping;
}

// if is delegation, get main address.
const [mainAddress] = await getAddressFromId.run({ id: delegate.from_id }, DBConn);
addressMapping = { address: mainAddress.address, id: mainAddress.id };
addressCache.set(address, addressMapping);
return addressMapping;
}
2 changes: 2 additions & 0 deletions packages/node-sdk/paima-db/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { createScheduledData, deleteScheduledData } from './scheduled-constructo
import { initializePaimaTables } from './database-validation.js';
import { DataMigrations } from './data-migrations.js';

export * from './delegate-wallet.js';

export * from './sql/block-heights.queries.js';
export type * from './sql/block-heights.queries.js';
export * from './sql/scheduled.queries.js';
Expand Down

0 comments on commit 0e633d5

Please sign in to comment.