Skip to content

Commit

Permalink
Adjust submit storage proof code
Browse files Browse the repository at this point in the history
Signed-off-by: KayleCoder <[email protected]>
  • Loading branch information
KayleCoder committed Jul 30, 2024
1 parent c070c6f commit 066dc32
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 37 deletions.
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const configs = {
maxFileSize: BigInt(getEnvOrExit("TASK_MAX_FILE_SIZE", "10485760", false)),
providerMnemonic: getEnvOrExit("TASK_PROVIDER_MNEMONIC", ""),
providerMinBalance: getEnvOrExit("TASK_PROVIDER_MIN_BALANCE", "1000000000"),
submitStorageProofBefore: getEnvOrExit("TASK_SUBMIT_STORAGE_PROOF_BEFORE", "1800")
},
tonStorageUtilsApi: getEnvOrExit('TON_STORAGE_UTILS_API', 'http://localhost:8192'),
}
3 changes: 0 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {initDb} from "./db";
import {logger} from "./util/logger";
import {job} from "./service/job";
import {getTonProvider} from "./util/ton";
const node = require('./merkle/node');

async function main() {
await initDb();
await getTonProvider();
await job();
}

Expand Down
16 changes: 13 additions & 3 deletions src/service/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ export async function job() {
const uploadProof = uploadStorageProofs();
const updateProofState = updateStorageProviderState();
const claim = claimRewards();
const jobs = [queryTx, analysisTx,
updateOrder, generate, downloadHeaders, downloadChild,
fileState, register, uploadProof, updateProofState, claim];
const jobs = [
queryTx,
analysisTx,
updateOrder,
generate,
downloadHeaders,
downloadChild,
fileState,
register,
uploadProof,
updateProofState,
claim
];
return Promise.all(jobs).catch(e => {
logger.error(`Error in jobs: ${e.stack}`);
throw new Error(e);
Expand Down
4 changes: 2 additions & 2 deletions src/service/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function updateOrderState() {
[Op.and]: {
started: 1,
period_finish: {
[Op.lt]: Date.now()
[Op.gte]: now()
}
},
started: 0
Expand All @@ -69,7 +69,7 @@ export async function updateOrderState() {
}
for (const order of orders) {
const contract: OpenedContract<StorageContract> = provider.getStorageContract(order.address);
const orderState = contract.getOrderInfo();
const orderState = await contract.getOrderState();
if (orderState == null) {
await Order.model.update({
order_state: OrderState.invalid
Expand Down
8 changes: 5 additions & 3 deletions src/service/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ export async function downloadTorrentHeaders() {
const torrentHash = order.torrent_hash;

try {
const result = await checkHeaderDownloaded(torrentHash, task);
const result = await checkDownloadState(torrentHash, task);
if (result) {
continue;
}
await addTonBag(torrentHash);
} catch (e) {
logger.error(`Failed to download meta bag ${torrentHash}: ${e}`);
continue;
}
await Task.model.update({
task_state: TaskState.download_torrent_start
Expand All @@ -52,7 +53,7 @@ export async function downloadTorrentHeaders() {
}
}

async function checkHeaderDownloaded(torrentHash: string, task: any){
async function checkDownloadState(torrentHash: string, task: any){
const bagDetail = await getTonBagDetails(torrentHash);
if (bagDetail.downloaded === bagDetail.size) {
await Task.model.update({
Expand Down Expand Up @@ -96,13 +97,14 @@ export async function downloadChildFiles() {
}))[0];
const torrentHash = order.torrent_hash;
try {
const result = await checkHeaderDownloaded(torrentHash, task);
const result = await checkDownloadState(torrentHash, task);
if (result) {
continue;
}
await downloadChildTonBag(torrentHash);
} catch (e) {
logger.error(`Failed to download child bag ${torrentHash}: ${e}`);
continue;
}
await Task.model.update({
task_state: TaskState.download_torrent_child_file_start
Expand Down
1 change: 1 addition & 0 deletions src/service/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ async function generateTask(provider: TonProvider, order: any, transaction: DBTr
const lastProofTime = await contract.getStorageProviderLastProofTime(providerAddress);
await Task.model.create({
order_id: order.id,
provider_address: providerAddress.toString(),
last_proof_time: lastProofTime,
task_state: TaskState.unregister_storage_provider,
}, {
Expand Down
86 changes: 64 additions & 22 deletions src/service/tonbag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {getTonProvider} from "../util/ton";
import {mnemonicToWalletKey} from "@ton/crypto";
import {configs} from "../config";
import {Address, OpenedContract, WalletContractV4} from "@ton/ton";
import {StorageContract} from "../wrapper/StorageContract";
import {Op} from "sequelize";
const node = require('../merkle/node')

export async function registerStorageProvider() {
Expand All @@ -29,7 +29,12 @@ export async function registerStorageProvider() {
continue;
}
for (const task of tasks) {
await registerProvider(task);
try {
await registerProvider(task);
} catch (e) {
logger.error(`Register provider failed: ${e.message}`);
await sleep(1);
}
}
}
}
Expand All @@ -45,23 +50,41 @@ async function registerProvider(task: any) {
const wallet = WalletContractV4.create({ publicKey: key.publicKey, workchain: 0 });
const storageContract = provider.getStorageContract(order.address);
const walletContract = provider.getTonClient().open(wallet);
const providerAddress = wallet.address;
const sender = walletContract.sender(key.secretKey);
let registerSuccess = await checkRegisterSuccess(wallet.address, order.address, false);
let registerSuccess = await checkRegisterSuccess(providerAddress, order.address, false);
if (registerSuccess) {
const lastProofTime = await storageContract.getStorageProviderLastProofTime(providerAddress);
return await Task.model.update({
task_state: TaskState.submit_storage_proof,
last_proof_time: lastProofTime,
next_proof_time: (BigInt(lastProofTime) + BigInt(order.max_storage_proof_span_in_sec)).toString(),
provider_address: wallet.address.toString()
}, {
where: {
id: task.id
}
});
}
// check provider count
const providerCount = await storageContract.getResidueProviderCount();
if (providerCount <= 0) {
return await Task.model.update({
task_state: TaskState.more_than_max_storage_provider_count,
}, {
where: {
id: task.id
}
});
}
await storageContract.sendRegisterAsStorageProvider(sender);
registerSuccess = await checkRegisterSuccess(wallet.address, order.address);
if (registerSuccess) {
const lastProofTime = await storageContract.getStorageProviderLastProofTime(providerAddress);
await Task.model.update({
task_state: TaskState.submit_storage_proof,
last_proof_time: lastProofTime,
next_proof_time: (BigInt(lastProofTime) + BigInt(order.max_storage_proof_span_in_sec)).toString(),
provider_address: sender.address.toString()
}, {
where: {
Expand All @@ -77,7 +100,6 @@ async function registerProvider(task: any) {
async function checkRegisterSuccess(address: Address, orderAddress: string, retry: boolean = true): Promise<boolean> {
const provider = await getTonProvider();
const storageContract = provider.getStorageContract(orderAddress);
console.log(`userAddress: ${address.toString()}`)
let nextProof = await storageContract.getNextProof(address);
if (retry) {
let retryTimes = 0;
Expand All @@ -95,38 +117,55 @@ async function getProviderAddress(): Promise<Address> {
return wallet.address;
}

async function checkBalance(): Promise<boolean> {
const provider = await getTonProvider();
const address = await getProviderAddress();
if (!await provider.getTonClient().isContractDeployed(address)) {
logger.error("Provider wallet not deployed");
return false;
}
const balance = await provider.getTonClient().getBalance(address);
const providerMinBalance = BigInt(configs.task.providerMinBalance);
if (balance < providerMinBalance) {
logger.error(`Provider balance ${balance} is not enough: ${providerMinBalance}`);
async function checkBalance(address?: Address): Promise<boolean> {
try {
const provider = await getTonProvider();
let tonAddress: Address;
if (address) {
tonAddress = address;
} else {
tonAddress = await getProviderAddress();
}
if (!await provider.getTonClient().isContractDeployed(tonAddress)) {
logger.error("Provider wallet not deployed");
return false;
}
const balance = await provider.getTonClient().getBalance(tonAddress);
const providerMinBalance = BigInt(configs.task.providerMinBalance);
if (balance < providerMinBalance) {
logger.error(`Provider${address.toString()} balance ${balance} is not enough: ${providerMinBalance}`);
return false;
}
return true;
} catch (e) {
logger.error(`Check balance failed: ${e.message}`);
return false;
}
return true;

}

export async function uploadStorageProofs() {
while (true) {
const tasks = await Task.model.findAll({
where: {
task_state: TaskState.submit_storage_proof
task_state: TaskState.submit_storage_proof,
next_proof_time: {
[Op.lte]: (now() - Number(configs.task.submitStorageProofBefore))
}
},
order: [
['last_proof_time', 'ASC']
['next_proof_time', 'ASC']
],
limit: 100
limit: 10
});
if (tasks.length === 0) {
await sleep(10);
await sleep(5);
continue;
}
for (const task of tasks) {
if (!await checkBalance(Address.parse(task.provider_address))) {
continue;
}
try {
await submitStorageProof(task);
} catch (e) {
Expand Down Expand Up @@ -188,6 +227,7 @@ async function submitStorageProof(task: any) {
if (lastProofTime) {
await Task.model.update({
last_proof_time: lastProofTime,
next_proof_time: (BigInt(lastProofTime) + BigInt(order.max_storage_proof_span_in_sec)).toString(),
}, {
where: {
id: task.id
Expand Down Expand Up @@ -243,7 +283,9 @@ async function updateProviderState(task: any) {
const lastProofTime = await storageContract.getStorageProviderLastProofTime(Address.parse(task.provider_address));
if (lastProofTime != null && lastProofTime > task.last_proof_time) {
await Task.model.update({
task_state: TaskState.submit_storage_proof
task_state: TaskState.submit_storage_proof,
last_proof_time: lastProofTime,
next_proof_time: (BigInt(lastProofTime) + BigInt(order.max_storage_proof_span_in_sec)).toString()
}, {
where: {
id: task.id
Expand All @@ -255,7 +297,7 @@ async function updateProviderState(task: any) {
retry++;
}
await Task.model.update({
task_state: TaskState.submit_storage_proof
task_state: TaskState.submit_storage_proof,
}, {
where: {
id: task.id
Expand Down
3 changes: 2 additions & 1 deletion src/service/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function indexPlaceOrderTransactions() {
}
// no transaction waiting
if (state.lastTransaction == null) {
logger.info(`No transactions found for contract ${TON_BAG_ADDRESS}`);
logger.info(`No last transactions found for contract ${TON_BAG_ADDRESS}`);
await sleep(10);
continue;
}
Expand All @@ -51,6 +51,7 @@ export async function indexPlaceOrderTransactions() {
async function queryTransactions(address: Address, ton: TonProvider, lastTransaction: TonTransaction) {
const exist = await saveTransaction(lastTransaction);
if (exist) {
logger.info("Last transaction exist....");
return;
}
let {prevTransactionLt, prevTransactionHash} = lastTransaction;
Expand Down
3 changes: 2 additions & 1 deletion src/type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export enum TaskState {
submit_storage_proof = 5,
proof_success_wait_for_update_state = 6,
period_finish = 6,
task_finish = 7
task_finish = 7,
more_than_max_storage_provider_count = -1,
}
2 changes: 1 addition & 1 deletion src/util/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function sleep(s: number) {
});
}

export function now() {
export function now(): number {
return Math.round(new Date().getTime()/1000);
}

Expand Down
2 changes: 1 addition & 1 deletion src/wrapper/StorageContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ export class StorageContract implements Contract {
const orderInfo = this.loadOrderInfo(cell);
const rewardsParams = this.loadRewardsParams(cell);
return {
address: this.address,
address: this.address.toString(),
...orderInfo,
started: rewardsParams.started,
total_rewards: rewardsParams.total_rewards,
Expand Down

0 comments on commit 066dc32

Please sign in to comment.