Skip to content

Commit

Permalink
Refactor/factories (#24)
Browse files Browse the repository at this point in the history
* refactored factories

* added poc storage provider lighthouse

* finished lighthouse poc

* cleaned branc

* removed dep
  • Loading branch information
troykessler authored Mar 3, 2023
1 parent a9e0e8a commit 5314bbd
Show file tree
Hide file tree
Showing 25 changed files with 192 additions and 136 deletions.
14 changes: 8 additions & 6 deletions common/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
canPropose,
canVote,
claimUploaderRole,
compressionFactory,
continueRound,
createBundleProposal,
getBalances,
Expand All @@ -33,7 +32,6 @@ import {
setupSDK,
setupValidator,
skipUploaderRole,
storageProviderFactory,
submitBundleProposal,
syncPoolConfig,
syncPoolState,
Expand All @@ -53,6 +51,9 @@ import {
import { ICacheProvider, IMetrics, IRuntime } from "./types";
import { standardizeJSON } from "./utils";
import { SupportedChains } from "@kyvejs/sdk/dist/constants";
import { storageProviderFactory } from "./reactors/storageProviders";
import { compressionFactory } from "./reactors/compression";
import { cacheProviderFactory } from "./reactors/cacheProvider";

/**
* Main class of KYVE protocol nodes representing a validator node.
Expand Down Expand Up @@ -122,10 +123,6 @@ export class Validator {
protected saveGetTransformDataItem = saveGetTransformDataItem;
public getProxyAuth = getProxyAuth;

// factories
protected storageProviderFactory = storageProviderFactory;
protected compressionFactory = compressionFactory;

// txs
protected claimUploaderRole = claimUploaderRole;
protected skipUploaderRole = skipUploaderRole;
Expand Down Expand Up @@ -153,6 +150,11 @@ export class Validator {
protected runNode = runNode;
protected runCache = runCache;

// factories
public static cacheProviderFactory = cacheProviderFactory;
public static storageProviderFactory = storageProviderFactory;
public static compressionFactory = compressionFactory;

/**
* Constructor for the validator class. It is required to provide the
* runtime class here in order to run the
Expand Down
5 changes: 3 additions & 2 deletions common/protocol/src/methods/checks/validateStorageBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { Validator, standardizeJSON } from "../..";
*/
export async function validateStorageBalance(this: Validator): Promise<void> {
try {
const storageProvider = await this.storageProviderFactory(
this.pool.data?.current_storage_provider_id ?? 0
const storageProvider = Validator.storageProviderFactory(
this.pool.data?.current_storage_provider_id ?? 0,
this.storagePriv
);

this.logger.info(
Expand Down
33 changes: 0 additions & 33 deletions common/protocol/src/methods/factories/storageProviderFactory.ts

This file was deleted.

4 changes: 0 additions & 4 deletions common/protocol/src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ export * from "./helpers/continueRound";
export * from "./helpers/saveGetTransformDataItem";
export * from "./helpers/getProxyAuth";

// factories
export * from "./factories/compressionFactory";
export * from "./factories/storageProviderFactory";

// txs
export * from "./txs/claimUploaderRole";
export * from "./txs/skipUploaderRole";
Expand Down
5 changes: 3 additions & 2 deletions common/protocol/src/methods/queries/getBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export async function getBalances(this: Validator): Promise<void> {
this.pool.data?.current_storage_provider_id ?? 0
}, $STORAGE_PRIV)`
);
const storageProvider = await this.storageProviderFactory(
this.pool.data?.current_storage_provider_id ?? 0
const storageProvider = Validator.storageProviderFactory(
this.pool.data?.current_storage_provider_id ?? 0,
this.storagePriv
);

this.logger.debug(`this.storageProvider.getBalance()`);
Expand Down
15 changes: 2 additions & 13 deletions common/protocol/src/methods/setups/setupCacheProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import path from "path";

import { Validator, standardizeJSON } from "../..";
import * as cacheProvider from "../../reactors/cacheProvider";
import fse from "fs-extra";

/**
Expand All @@ -19,18 +18,8 @@ export async function setupCacheProvider(this: Validator): Promise<void> {

this.logger.debug(`Initializing cache provider with path ${cachePath}`);

// create cache provider depending on chosen cache type.
// Default is leveldb cache
switch (this.cache) {
case "memory":
this.cacheProvider = new cacheProvider.MemoryCache();
break;
case "jsonfile":
this.cacheProvider = new cacheProvider.JsonFileCache();
break;
default:
this.cacheProvider = new cacheProvider.JsonFileCache();
}
// create cache provider depending on chosen cache type
this.cacheProvider = Validator.cacheProviderFactory(this.cache);

// delete all contents of cache directory
await fse.emptyDir(`${cachePath}/`);
Expand Down
7 changes: 4 additions & 3 deletions common/protocol/src/methods/upload/createBundleProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export async function createBundleProposal(this: Validator): Promise<void> {
this.logger.debug(
`compressionFactory(${this.pool.data?.current_compression_id ?? 0})`
);
const compression = this.compressionFactory(
const compression = Validator.compressionFactory(
this.pool.data?.current_compression_id ?? 0
);

Expand Down Expand Up @@ -213,8 +213,9 @@ export async function createBundleProposal(this: Validator): Promise<void> {
this.pool.data?.current_storage_provider_id ?? 0
}, $STORAGE_PRIV)`
);
const storageProvider = await this.storageProviderFactory(
this.pool.data?.current_storage_provider_id ?? 0
const storageProvider = Validator.storageProviderFactory(
this.pool.data?.current_storage_provider_id ?? 0,
this.storagePriv
);

// upload the bundle proposal to the storage provider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function saveBundleDecompress(
this.logger.debug(
`compressionFactory(${this.pool.bundle_proposal?.compression_id ?? 0})`
);
const compression = this.compressionFactory(
const compression = Validator.compressionFactory(
this.pool.bundle_proposal?.compression_id ?? 0
);

Expand Down
5 changes: 3 additions & 2 deletions common/protocol/src/methods/validate/saveBundleDownload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ export async function saveBundleDownload(
this.pool.bundle_proposal?.storage_provider_id ?? 0
}, $STORAGE_PRIV)`
);
const storageProvider = await this.storageProviderFactory(
this.pool.bundle_proposal?.storage_provider_id ?? 0
const storageProvider = Validator.storageProviderFactory(
this.pool.bundle_proposal?.storage_provider_id ?? 0,
this.storagePriv
);

// calculate download timeout for storage provider
Expand Down
25 changes: 23 additions & 2 deletions common/protocol/src/reactors/cacheProvider/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
export * from "./JsonFileCache";
export * from "./MemoryCache";
import { ICacheProvider } from "../..";
import { JsonFileCache } from "./JsonFileCache";
import { MemoryCache } from "./MemoryCache";

/**
* cacheProviderFactory creates the correct cache class
* from the specified id. Current cache types are:
*
* 0 - JsonFile
* x - Memory (default)
*
* @method cacheProviderFactory
* @param {string} cacheId the id of the compression
* @return {ICacheProvider}
*/
export const cacheProviderFactory = (cacheId: string): ICacheProvider => {
switch (cacheId) {
case "jsonfile":
return new JsonFileCache();
default:
return new MemoryCache();
}
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Validator } from "../..";
import { ICompression } from "../..";
import { Gzip } from "../../reactors/compression/Gzip";
import { NoCompression } from "../../reactors/compression/NoCompression";
import { Gzip } from "./Gzip";
import { NoCompression } from "./NoCompression";

/**
* compressionFactory creates the correct compression class
Expand All @@ -12,18 +11,14 @@ import { NoCompression } from "../../reactors/compression/NoCompression";
* x - NoCompression (default)
*
* @method compressionFactory
* @param {Validator} this
* @param {number} compressionId the id of the compression
* @return {ICompression}
*/
export function compressionFactory(
this: Validator,
compressionId: number
): ICompression {
export const compressionFactory = (compressionId: number): ICompression => {
switch (compressionId) {
case 1:
return new Gzip();
default:
return new NoCompression();
}
}
};
37 changes: 23 additions & 14 deletions common/protocol/src/reactors/storageProviders/Arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,45 @@ export class Arweave implements IStorageProvider {
public name = "Arweave";
public decimals = 12;

private jwk!: JWKInterface;
private client = new ArweaveClient({
host: "arweave.net",
protocol: "https",
});

async init(storagePriv: string) {
this.jwk = JSON.parse(storagePriv);
return this;
private readonly storagePriv: string;

constructor(storagePriv: string) {
this.storagePriv = storagePriv;
}

private get arweaveKeyfile(): JWKInterface {
return JSON.parse(this.storagePriv);
}

private get arweaveClient(): ArweaveClient {
return new ArweaveClient({
host: "arweave.net",
protocol: "https",
});
}

async getAddress() {
return await this.client.wallets.getAddress(this.jwk);
return await this.arweaveClient.wallets.getAddress(this.arweaveKeyfile);
}

async getBalance() {
const account = await this.getAddress();
return await this.client.wallets.getBalance(account);
return await this.arweaveClient.wallets.getBalance(account);
}

async saveBundle(bundle: Buffer, tags: BundleTag[]) {
const transaction = await this.client.createTransaction({
const transaction = await this.arweaveClient.createTransaction({
data: bundle,
});

for (const tag of tags) {
transaction.addTag(tag.name, tag.value);
}

await this.client.transactions.sign(transaction, this.jwk);
await this.arweaveClient.transactions.sign(
transaction,
this.arweaveKeyfile
);

const balance = await this.getBalance();

Expand All @@ -47,7 +56,7 @@ export class Arweave implements IStorageProvider {
);
}

await this.client.transactions.post(transaction);
await this.arweaveClient.transactions.post(transaction);

return {
storageId: transaction.id,
Expand Down
25 changes: 14 additions & 11 deletions common/protocol/src/reactors/storageProviders/Bundlr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,30 @@ export class Bundlr implements IStorageProvider {
public name = "Bundlr";
public decimals = 12;

private jwk!: JWKInterface;
private client!: BundlrClient;
private readonly storagePriv: string;

async init(storagePriv: string) {
this.jwk = JSON.parse(storagePriv);
constructor(storagePriv: string) {
this.storagePriv = storagePriv;
}

private get bundlrKeyfile(): JWKInterface {
return JSON.parse(this.storagePriv);
}

this.client = new BundlrClient(
private get bundlrClient(): BundlrClient {
return new BundlrClient(
"http://node1.bundlr.network",
"arweave",
this.jwk
this.bundlrKeyfile
);

return this;
}

async getAddress() {
return this.client.address;
return this.bundlrClient.address;
}

async getBalance() {
const atomicUnits = await this.client.getLoadedBalance();
const atomicUnits = await this.bundlrClient.getLoadedBalance();
return atomicUnits.toString();
}

Expand All @@ -43,7 +46,7 @@ export class Bundlr implements IStorageProvider {
],
};

const transaction = this.client.createTransaction(
const transaction = this.bundlrClient.createTransaction(
bundle,
transactionOptions
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ export class NoStorageProvider implements IStorageProvider {
public name = "NoStorageProvider";
public decimals = 0;

async init(_storagePriv: string) {
return this;
}

async getAddress() {
return "";
}
Expand Down
Loading

0 comments on commit 5314bbd

Please sign in to comment.