Skip to content

Commit

Permalink
Fix/initial sync prune issue (#150)
Browse files Browse the repository at this point in the history
* fix: initial sync prune issue

* fix: optimize initial sync

* chore: upgrade version

* chore: update doc

* chore: update doc

* chore: upgrade manta-rs lib version
  • Loading branch information
DanielZhangReal authored Sep 2, 2023
1 parent c999f08 commit 3de6be4
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion manta-js/package/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion manta-js/package/src/PrivateWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
19 changes: 13 additions & 6 deletions manta-js/package/src/ledger-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()),
Expand Down
27 changes: 27 additions & 0 deletions manta-js/package/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,30 @@ export async function getSignedTransaction(
txs,
};
}

export async function requestData(
func: () => Promise<any>,
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 };
}
8 changes: 4 additions & 4 deletions manta-js/package/src/wallet/crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down

0 comments on commit 3de6be4

Please sign in to comment.