@emurgo/yoroi-lib
Advanced tools
Comparing version
import { Axios } from 'axios'; | ||
import { UtxoApiContract } from './api'; | ||
import { Asset, DiffPoint, DiffType, TipStatusReference, Utxo, UtxoApiResponse, UtxoAtPointRequest, UtxoDiff, UtxoDiffSincePointRequest } from './models'; | ||
import { Asset, DiffType, TipStatusReference, Utxo, UtxoApiResponse, UtxoAtPointRequest, UtxoDiff, UtxoDiffSincePointRequest } from './models'; | ||
export declare type UtxoAtPointItemResponse = { | ||
@@ -24,4 +24,6 @@ utxo_id: string; | ||
export declare type UtxoDiffSincePointResponse = { | ||
lastDiffPointSelected: DiffPoint; | ||
lastDiffPointSelected: any; | ||
diffItems: UtxoDiffSincePointItemResponse[]; | ||
lastFoundSafeblock?: string; | ||
lastFoundBestblock?: string; | ||
}; | ||
@@ -28,0 +30,0 @@ export declare type TipStatusResponse = { |
@@ -104,3 +104,3 @@ "use strict"; | ||
return yield this._base.getUtxoDiffSincePoint({ | ||
afterBestBlock: req.afterBestBlock, | ||
afterBestBlocks: req.afterBestBlocks, | ||
untilBlockHash: req.untilBlockHash, | ||
@@ -111,6 +111,21 @@ addresses: addresses | ||
const values = (yield Promise.all(promises)).map((x) => x.value); | ||
const uniqueMatchedBestBlocks = new Set(values.map(e => e.reference.lastFoundBestBlock)); | ||
if (uniqueMatchedBestBlocks.size > 1) { | ||
return { result: models_1.UtxoApiResult.BESTBLOCK_ROLLBACK }; | ||
} | ||
const latestMatchedSafeBlockIndex = Math.max(...(values.map(e => { | ||
const v = e.reference.lastFoundSafeBlock; | ||
return v == null ? -1 : req.afterBestBlocks.indexOf(v); | ||
}))); | ||
if (latestMatchedSafeBlockIndex < 0) { | ||
return { result: models_1.UtxoApiResult.SAFEBLOCK_ROLLBACK }; | ||
} | ||
return { | ||
result: models_1.UtxoApiResult.SUCCESS, | ||
value: { | ||
diffItems: (0, js_1.flatten)(values.map((x) => x.diffItems)) | ||
diffItems: (0, js_1.flatten)(values.map((x) => x.diffItems)), | ||
reference: { | ||
lastFoundBestBlock: values[0].reference.lastFoundBestBlock, | ||
lastFoundSafeBlock: req.afterBestBlocks[latestMatchedSafeBlockIndex], | ||
} | ||
} | ||
@@ -126,2 +141,4 @@ }; | ||
exports.BatchedEmurgoUtxoApi = BatchedEmurgoUtxoApi; | ||
const UTXO_AT_POINT_PAGE_SIZE = 10; | ||
const UTXO_DIFF_SINCE_POINT_PAGE_SIZE = 10; | ||
class EmurgoUtxoApi { | ||
@@ -194,3 +211,3 @@ constructor(axios, apiUrl, throwRequestErrors) { | ||
allUtxos = allUtxos.concat(utxosAtPointPage); | ||
while (utxosAtPointPage.length > 0) { | ||
while (utxosAtPointPage.length === UTXO_AT_POINT_PAGE_SIZE) { | ||
page++; | ||
@@ -230,10 +247,14 @@ utxosAtPointPage = yield this.getUtxoAtPointPage(req, page); | ||
untilBlockHash: req.untilBlockHash, | ||
afterPoint: { | ||
blockHash: req.afterBestBlock | ||
}, | ||
diffLimit: 10 | ||
afterBestblocks: req.afterBestBlocks, | ||
diffLimit: UTXO_DIFF_SINCE_POINT_PAGE_SIZE | ||
}); | ||
let allDiffItems = []; | ||
while (response.data.diffItems.length > 0) { | ||
allDiffItems = allDiffItems.concat(response.data.diffItems); | ||
if (response.data.lastFoundBestblock == null) { | ||
throw new Error('Unexpected state: no bestblock match is found in the request reference!'); | ||
} | ||
const reference = { | ||
lastFoundBestBlock: response.data.lastFoundBestblock, | ||
lastFoundSafeBlock: response.data.lastFoundSafeblock, | ||
}; | ||
let allDiffItems = [...response.data.diffItems]; | ||
while (response.data.diffItems.length === UTXO_DIFF_SINCE_POINT_PAGE_SIZE) { | ||
response = yield this._axios.post(url, { | ||
@@ -243,4 +264,5 @@ addresses: req.addresses, | ||
afterPoint: response.data.lastDiffPointSelected, | ||
diffLimit: 10 | ||
diffLimit: UTXO_DIFF_SINCE_POINT_PAGE_SIZE | ||
}); | ||
allDiffItems = allDiffItems.concat(response.data.diffItems); | ||
} | ||
@@ -274,3 +296,4 @@ return { | ||
} | ||
}) | ||
}), | ||
reference: reference, | ||
} | ||
@@ -294,3 +317,3 @@ }; | ||
page: page, | ||
pageSize: 10 | ||
pageSize: UTXO_AT_POINT_PAGE_SIZE, | ||
}); | ||
@@ -297,0 +320,0 @@ return resp.data; |
@@ -109,9 +109,9 @@ "use strict"; | ||
yield this._utxoStorage.clearUtxoState(); | ||
return yield this.syncSafeStateAndGetDiff(addresses); | ||
return this.syncSafeStateAndGetDiff(addresses); | ||
} | ||
const tipStatus = tipStatusResponse.value; | ||
const { safeBlockRollback, value } = yield this.getUtxoDiffSincePoint(addresses, tipStatus.reference.lastFoundBestBlock); | ||
const { safeBlockRollback, value } = yield this.getUtxoDiffSincePoint(addresses, referenceBlocks); | ||
if (safeBlockRollback) { | ||
yield this._utxoStorage.clearUtxoState(); | ||
return yield this.syncSafeStateAndGetDiff(addresses); | ||
return this.syncSafeStateAndGetDiff(addresses); | ||
} | ||
@@ -128,3 +128,8 @@ else { | ||
localDiff, | ||
tipStatus | ||
tipStatus: { | ||
reference: { | ||
lastFoundBestBlock: value.diff.reference.lastFoundBestBlock, | ||
lastFoundSafeBlock: value.diff.reference.lastFoundSafeBlock || tipStatus.reference.lastFoundSafeBlock, | ||
} | ||
} | ||
}; | ||
@@ -134,3 +139,3 @@ } | ||
} | ||
getUtxoDiffSincePoint(addresses, afterBestBlock) { | ||
getUtxoDiffSincePoint(addresses, afterBestBlocks) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -140,7 +145,7 @@ const bestBlock = yield this._api.getBestBlock(); | ||
addresses: addresses, | ||
afterBestBlock: afterBestBlock, | ||
afterBestBlocks: afterBestBlocks, | ||
untilBlockHash: bestBlock | ||
}); | ||
if (diffResult.result === models_1.UtxoApiResult.BESTBLOCK_ROLLBACK) { | ||
return yield this.getUtxoDiffSincePoint(addresses, afterBestBlock); | ||
return this.getUtxoDiffSincePoint(addresses, afterBestBlocks); | ||
} | ||
@@ -174,8 +179,8 @@ else if (diffResult.result === models_1.UtxoApiResult.SAFEBLOCK_ROLLBACK) { | ||
for (const diffToMerge of diffsToMerge) { | ||
for (const newUtxo of diffToMerge.newUtxos) { | ||
utxoMap[newUtxo.utxoId] = newUtxo; | ||
} | ||
for (const spentUtxoId of diffToMerge.spentUtxoIds) { | ||
delete utxoMap[spentUtxoId]; | ||
} | ||
for (const newUtxo of diffToMerge.newUtxos) { | ||
utxoMap[newUtxo.utxoId] = newUtxo; | ||
} | ||
yield this._utxoStorage.removeDiffWithBestBlock(diffToMerge.lastBestBlockHash); | ||
@@ -192,3 +197,3 @@ } | ||
const { safeBlockHash, utxos } = yield this.getUtxoAtSafePointFromApi(addresses); | ||
yield this._utxoStorage.replaceUtxoAtSafePoint(utxos, safeBlockHash); | ||
this._utxoStorage.replaceUtxoAtSafePoint(utxos, safeBlockHash); | ||
return { | ||
@@ -195,0 +200,0 @@ safeBlockHash: safeBlockHash, |
@@ -20,3 +20,3 @@ import BigNumber from 'bignumber.js'; | ||
untilBlockHash: string; | ||
afterBestBlock: string; | ||
afterBestBlocks: string[]; | ||
}; | ||
@@ -50,9 +50,8 @@ export declare type Asset = { | ||
}; | ||
export declare type DiffPoint = { | ||
blockHash: string; | ||
txHash: string; | ||
itemIndex: number; | ||
}; | ||
export declare type UtxoDiff = { | ||
diffItems: Array<UtxoDiffItem | UtxoDiffItemOutput>; | ||
reference: { | ||
lastFoundBestBlock: string; | ||
lastFoundSafeBlock?: string; | ||
}; | ||
}; | ||
@@ -59,0 +58,0 @@ export declare type UtxoDiffToBestBlock = { |
{ | ||
"name": "@emurgo/yoroi-lib", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
228250
0.84%4085
0.71%