fusion
This commit is contained in:
commit
e1ee152fef
@ -433,6 +433,37 @@ export default class Services {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async tryFetchDiffValue(diffs: UserDiff[]): Promise<[UserDiff[], Record<string, string>]>{
|
||||||
|
if (diffs.length === 0) {
|
||||||
|
return [[], {}];
|
||||||
|
}
|
||||||
|
|
||||||
|
// We check if we have the value in diffs
|
||||||
|
let retrievedValues: Record<string, string> = {};
|
||||||
|
for (const diff of diffs) {
|
||||||
|
// Check if `new_value` is missing
|
||||||
|
if (diff.new_value === null) {
|
||||||
|
const hash = diff.value_commitment;
|
||||||
|
if (!hash) {
|
||||||
|
console.error('No commitment for diff');
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const res = await this.fetchValueFromStorage(hash);
|
||||||
|
if (!res) {
|
||||||
|
console.error('Failed to fetch value for hash', hash);
|
||||||
|
} else {
|
||||||
|
diff.new_value = res['value'];
|
||||||
|
retrievedValues[hash] = res['value'];
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to fetch new_value for diff: ${JSON.stringify(diff)}`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [diffs, retrievedValues];
|
||||||
|
}
|
||||||
|
|
||||||
public async handleApiReturn(apiReturn: ApiReturn) {
|
public async handleApiReturn(apiReturn: ApiReturn) {
|
||||||
if (apiReturn.new_tx_to_send && apiReturn.new_tx_to_send.transaction.length != 0) {
|
if (apiReturn.new_tx_to_send && apiReturn.new_tx_to_send.transaction.length != 0) {
|
||||||
await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send));
|
await this.sendNewTxMessage(JSON.stringify(apiReturn.new_tx_to_send));
|
||||||
@ -481,48 +512,25 @@ export default class Services {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const isPaired = this.isPaired();
|
const isPaired = this.isPaired();
|
||||||
|
|
||||||
if (updatedProcess.new_diffs.length != 0) {
|
if (updatedProcess.new_diffs.length != 0) {
|
||||||
// We check if we have the value in diffs
|
const [updatedDiffs, retrievedValues] = await this.tryFetchDiffValue(updatedProcess.new_diffs);
|
||||||
const diffs = updatedProcess.new_diffs;
|
|
||||||
const stateId = diffs[0].new_state_merkle_root;
|
|
||||||
let retrievedValues: Record<string, string> = {};
|
|
||||||
for (const diff of diffs) {
|
|
||||||
// Check if `new_value` is missing
|
|
||||||
if (diff.new_value === null) {
|
|
||||||
const hash = diff.value_commitment;
|
|
||||||
if (!hash) {
|
|
||||||
console.error('No commitment for diff');
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
const res = await this.fetchValueFromStorage(hash);
|
|
||||||
if (!res) {
|
|
||||||
console.error('Failed to fetch value for hash', hash);
|
|
||||||
} else {
|
|
||||||
diff.new_value = res['value'];
|
|
||||||
retrievedValues[hash] = res['value'];
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Failed to fetch new_value for diff: ${JSON.stringify(diff)}`, error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Object.entries(retrievedValues).length != 0) {
|
if (Object.entries(retrievedValues).length != 0) {
|
||||||
|
const stateId = updatedDiffs[0].new_state_merkle_root;
|
||||||
|
const processId = updatedDiffs[0].process_id;
|
||||||
// We update the process with the value we retrieved
|
// We update the process with the value we retrieved
|
||||||
const hashToValues = JSON.stringify(retrievedValues);
|
const hashToValues = JSON.stringify(retrievedValues);
|
||||||
const apiReturn = this.sdkClient.update_process_state(processId, stateId, hashToValues);
|
const apiReturn = this.sdkClient.update_process_state(processId, stateId, hashToValues);
|
||||||
await this.handleApiReturn(apiReturn);
|
await this.handleApiReturn(apiReturn);
|
||||||
}
|
} else {
|
||||||
|
|
||||||
if (isPaired) {
|
|
||||||
try {
|
try {
|
||||||
await this.saveDiffs(diffs);
|
await this.saveDiffs(updatedDiffs);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
} else {
|
if (!isPaired) {
|
||||||
await this.openPairingConfirmationModal(diffs, updatedProcess.up_to_date_roles);
|
await this.openPairingConfirmationModal(updatedDiffs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -544,7 +552,7 @@ export default class Services {
|
|||||||
}, 0);
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async openPairingConfirmationModal(diffs: UserDiff[], roles: Record<string, RoleDefinition>) {
|
public async openPairingConfirmationModal(diffs: UserDiff[]) {
|
||||||
const rolesDiff = diffs.find((diff) => diff.field === 'roles');
|
const rolesDiff = diffs.find((diff) => diff.field === 'roles');
|
||||||
if (!rolesDiff) {
|
if (!rolesDiff) {
|
||||||
throw new Error('Pairing process must have roles');
|
throw new Error('Pairing process must have roles');
|
||||||
@ -552,7 +560,7 @@ export default class Services {
|
|||||||
const processId = rolesDiff.process_id;
|
const processId = rolesDiff.process_id;
|
||||||
const stateId = rolesDiff.new_state_merkle_root;
|
const stateId = rolesDiff.new_state_merkle_root;
|
||||||
try {
|
try {
|
||||||
await this.routingInstance.openPairingConfirmationModal(roles, processId, stateId);
|
await this.routingInstance.openPairingConfirmationModal(rolesDiff.new_value, processId, stateId);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error(`${e}`);
|
throw new Error(`${e}`);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user