From 3de6be443687ac7b17d1119ab2dcaf3a345e8d33 Mon Sep 17 00:00:00 2001 From: Daniel <110973578+DanielZhangReal@users.noreply.github.com> Date: Sat, 2 Sep 2023 10:07:21 +0800 Subject: [PATCH] Fix/initial sync prune issue (#150) * fix: initial sync prune issue * fix: optimize initial sync * chore: upgrade version * chore: update doc * chore: update doc * chore: upgrade manta-rs lib version --- CHANGELOG.md | 2 ++ manta-js/package/package.json | 2 +- manta-js/package/src/PrivateWallet.ts | 3 ++- manta-js/package/src/ledger-api/index.ts | 19 +++++++++----- manta-js/package/src/utils/index.ts | 27 ++++++++++++++++++++ manta-js/package/src/wallet/crate/Cargo.toml | 8 +++--- 6 files changed, 49 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5114bc0..002bcae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] ### Added +- [\#150](https://github.com/Manta-Network/sdk/pull/150) Add a retry mechanism to the initial_sync api - [\#148](https://github.com/Manta-Network/sdk/pull/148) Add caching to the getMetadata interface that takes up too much bandwidth - [\#139](https://github.com/Manta-Network/sdk/pull/139) Add sdk node example - [\#133](https://github.com/Manta-Network/sdk/pull/133) Restore pruning and add UTXO consolidation && add Api `estimateTransferPostsCount` && add Api `consolidateTransferSend` @@ -33,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Removed ### Fixed +- [\#150](https://github.com/Manta-Network/sdk/pull/150) Fixes a bug which was causing prune not to work on trees initialized by initial_sync. - [\#136](https://github.com/Manta-Network/sdk/pull/136) Asset selection fix - [\#129](https://github.com/Manta-Network/sdk/pull/129) Signer bug fix. diff --git a/manta-js/package/package.json b/manta-js/package/package.json index 6beda6b..67b4b77 100644 --- a/manta-js/package/package.json +++ b/manta-js/package/package.json @@ -1,6 +1,6 @@ { "name": "manta-extension-sdk", - "version": "1.2.1", + "version": "1.2.2", "description": "manta.js sdk", "main": "./dist/browser/index.js", "exports": { diff --git a/manta-js/package/src/PrivateWallet.ts b/manta-js/package/src/PrivateWallet.ts index 0a00fe9..b63c119 100644 --- a/manta-js/package/src/PrivateWallet.ts +++ b/manta-js/package/src/PrivateWallet.ts @@ -177,8 +177,9 @@ export default class PrivateWallet implements IPrivateWallet { this.log('Start initial new account'); await this.baseWallet.isApiReady(); await this.wasmWallet.initial_sync(this.getWasmNetWork()); - this.log('Initial new account completed'); + this.log('Initial new account sync completed'); await this.saveStateToLocal(); + this.log('Initial new account prune&save completed'); this.initialSyncIsFinished = true; return true; }); diff --git a/manta-js/package/src/ledger-api/index.ts b/manta-js/package/src/ledger-api/index.ts index b86a368..ee50472 100644 --- a/manta-js/package/src/ledger-api/index.ts +++ b/manta-js/package/src/ledger-api/index.ts @@ -13,7 +13,7 @@ import $, { currentPathToJson, } from './decodeUtils'; import type { ILedgerApi, LedgerSyncProgress, PalletName } from '../interfaces'; -import { getLedgerSyncedCount, log } from '../utils'; +import { getLedgerSyncedCount, log, requestData } from '../utils'; import { ApiPromise } from '@polkadot/api'; export default class LedgerApi implements ILedgerApi { @@ -47,11 +47,18 @@ export default class LedgerApi implements ILedgerApi { if (this.loggingEnabled) { this._log('checkpoint ' + JSON.stringify(checkpoint)); } - // @ts-ignore - const result = await this.api.rpc[this.palletName].dense_initial_pull( - checkpoint, - MAX_RECEIVERS_PULL_SIZE, - ); + + const { result, success, error } = await requestData(() => { + // @ts-ignore + return this.api.rpc[this.palletName].dense_initial_pull( + checkpoint, + MAX_RECEIVERS_PULL_SIZE, + ); + }); + + if (!success) { + throw error; + } const decodedUtxoData = $Utxos.decode( base64Decode(result.utxo_data.toString()), diff --git a/manta-js/package/src/utils/index.ts b/manta-js/package/src/utils/index.ts index 41bdfcd..c3ae8fe 100644 --- a/manta-js/package/src/utils/index.ts +++ b/manta-js/package/src/utils/index.ts @@ -305,3 +305,30 @@ export async function getSignedTransaction( txs, }; } + +export async function requestData( + func: () => Promise, + checkResult?: (value: any) => boolean, + maxTimes = 3, + intervalTime = 3000, +) { + let index = 0; + let result: any; + let success = false; + let error: any; + while (index < maxTimes) { + try { + result = await func(); + if (typeof checkResult === 'function' && !checkResult(result)) { + throw new Error('Invalid Result'); + } + success = true; + break; + } catch (ex) { + await new Promise((r) => setTimeout(r, intervalTime)); + error = ex; + index += 1; + } + } + return { success, result, error }; +} diff --git a/manta-js/package/src/wallet/crate/Cargo.toml b/manta-js/package/src/wallet/crate/Cargo.toml index 6def19b..533ac8e 100644 --- a/manta-js/package/src/wallet/crate/Cargo.toml +++ b/manta-js/package/src/wallet/crate/Cargo.toml @@ -35,10 +35,10 @@ disable-restart = [] console_error_panic_hook = { version = "0.1.7", default-features = false } getrandom = { version = "0.2.8", default-features = false, features = ["js"] } js-sys = { version = "0.3.60", default-features = false } -manta-accounting = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.6.1", default-features = false, features = ["serde"] } -manta-crypto = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.6.1", default-features = false, features = ["serde"] } -manta-pay = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.6.1", default-features = true, features = ["arkworks", "groth16", "scale", "serde", "wallet"] } -manta-util = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.6.1", default-features = false, features = ["serde", "reqwest"] } +manta-accounting = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.6.2", default-features = false, features = ["serde"] } +manta-crypto = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.6.2", default-features = false, features = ["serde"] } +manta-pay = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.6.2", default-features = true, features = ["arkworks", "groth16", "scale", "serde", "wallet"] } +manta-util = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.6.2", default-features = false, features = ["serde", "reqwest"] } scale-codec = { package = "parity-scale-codec", version = "3.2.1", default-features = false, features = ["derive", "max-encoded-len"] } serde-wasm-bindgen = { version = "0.4.5", default-features = false } serde_json = { version = "1.0.89", default-features = false, features = ["alloc", "arbitrary_precision"] }