Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

Commit 275b05f

Browse files
committed
Implement Grade Of Finality Support
1 parent 3d37fa1 commit 275b05f

File tree

10 files changed

+65
-48
lines changed

10 files changed

+65
-48
lines changed

src/app/components/Settings.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,22 @@ class Settings extends Component<SettingsProps, SettingsState> {
4242
password: settings.password,
4343
accessManaPledgeID: settings.accessManaPledgeID,
4444
consensusManaPledgeID: settings.consensusManaPledgeID,
45+
gofConfThreshold: settings.gofConfThreshold,
4546
isBusy: false
4647
});
4748
}
4849

50+
private static imposeMinMax(el: HTMLInputElement): void {
51+
if (el.value !== "") {
52+
if (parseInt(el.value) < parseInt(el.min)) {
53+
el.value = el.min;
54+
}
55+
if (parseInt(el.value) > parseInt(el.max)) {
56+
el.value = el.max;
57+
}
58+
}
59+
}
60+
4961
/**
5062
* Render the component.
5163
* @returns The node to render.
@@ -139,6 +151,23 @@ class Settings extends Component<SettingsProps, SettingsState> {
139151
onChange={e => this.setState({ consensusManaPledgeID: e.target.value })}
140152
/>
141153
</div>
154+
<div className="card--label">
155+
Grade Of Finality for confirmation
156+
</div>
157+
<div className="card--value">
158+
<input
159+
className="fill"
160+
type="number"
161+
min="1"
162+
max="3"
163+
value={this.state.gofConfThreshold}
164+
onChange={e => {
165+
Settings.imposeMinMax(e.target);
166+
this.setState({ gofConfThreshold: e.target.valueAsNumber });
167+
}
168+
}
169+
/>
170+
</div>
142171
<div className="margin-t-s">
143172
<button
144173
onClick={() => this.save()}
@@ -178,7 +207,8 @@ class Settings extends Component<SettingsProps, SettingsState> {
178207
user: this.state.user ?? "",
179208
password: this.state.password ?? "",
180209
accessManaPledgeID: this.state.accessManaPledgeID ?? "",
181-
consensusManaPledgeID: this.state.consensusManaPledgeID ?? ""
210+
consensusManaPledgeID: this.state.consensusManaPledgeID ?? "",
211+
gofConfThreshold: this.state.gofConfThreshold ?? 0
182212
};
183213
await this._settingsService.set(newSettings);
184214

src/app/components/SettingsState.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ export interface SettingsState {
4040
* The nodeID of the consensus Mana pledge.
4141
*/
4242
consensusManaPledgeID?: string;
43+
44+
/**
45+
* The minimal grade of finality needed for an output to be confirmed
46+
*/
47+
gofConfThreshold?: number
4348
}

src/app/components/Wallet.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,6 @@ class Wallet extends Component<WalletProps, WalletState> {
255255
<th>Color</th>
256256
<th>Confirmed</th>
257257
<th>Pending</th>
258-
<th>Rejected</th>
259258
<th>&nbsp;</th>
260259
</tr>
261260
</thead>
@@ -283,9 +282,6 @@ class Wallet extends Component<WalletProps, WalletState> {
283282
<td className="warning">
284283
{balance.unConfirmed.toString()}
285284
</td>
286-
<td className="danger">
287-
{balance.rejected.toString()}
288-
</td>
289285
<td>
290286
<button
291287
disabled={balance.confirmed <= 0}

src/iota/api/models/IUnspentOutputsResponse.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,10 @@ export interface IUnspentOutputsResponse extends IResponse {
4949
};
5050

5151
/**
52-
* InclusionState represents the different states of an OutputID.
52+
* GradeOfFinality represents the confirmation level. 0 is not confirmed.
53+
* 3 is the maximum confirmation level.
5354
*/
54-
inclusionState: {
55-
/**
56-
* Is the output confirmed.
57-
*/
58-
confirmed?: boolean;
59-
/**
60-
* Is the output rejected.
61-
*/
62-
rejected?: boolean;
63-
/**
64-
* Is the output conflicting.
65-
*/
66-
conflicting?: boolean;
67-
};
55+
gradeOfFinality: number;
6856
}[];
6957
}[];
7058
}

src/models/ISettings.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ export interface ISettings {
2828
* Access Mana Pledge ID.
2929
*/
3030
consensusManaPledgeID: string;
31+
32+
/**
33+
* Grade of Finality Confirmation threshold
34+
*/
35+
gofConfThreshold: number;
3136
}

src/models/IWalletBalance.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,4 @@ export interface IWalletBalance {
1515
* Unconfirmed Balance.
1616
*/
1717
unConfirmed: bigint;
18-
19-
/**
20-
* Rejected Balance.
21-
*/
22-
rejected: bigint;
2318
}

src/models/IWalletOutput.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface IWalletOutput {
1313
balances: IWalletOutputBalance[];
1414

1515
/**
16-
* Inclusion state.
16+
* Grade of finality.
1717
*/
18-
inclusionState: IWalletOutputInclusionState;
18+
gof: number;
1919
}

src/models/IWalletOutputInclusionState.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ export interface IWalletOutputInclusionState {
66
/**
77
* Is the output confirmed.
88
*/
9-
confirmed?: boolean;
10-
/**
11-
* Is the output rejected.
12-
*/
13-
rejected?: boolean;
9+
gof?: number;
1410
/**
1511
* Is the output liked.
1612
*/

src/services/settingsService.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { ISettings } from "../models/ISettings";
33
import { IJsonStorageService } from "../models/services/IJsonStorageService";
44
import { ISettingsService } from "../models/services/ISettingsService";
55

6+
const defaultGofConfThreshold = 3;
7+
68
/**
79
* Service to manage settings.
810
*/
@@ -40,7 +42,8 @@ export class SettingsService implements ISettingsService {
4042
user: "",
4143
password: "",
4244
accessManaPledgeID: "",
43-
consensusManaPledgeID: ""
45+
consensusManaPledgeID: "",
46+
gofConfThreshold: defaultGofConfThreshold
4447
};
4548
}
4649

src/services/walletService.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -358,10 +358,10 @@ export class WalletService implements IWalletService {
358358

359359
const version = 0;
360360
const time = Date.now();
361-
const timestamp = BigInt(time * 1000000);
361+
const timestamp = BigInt(time * 1_000_000);
362362

363363
// Calculate the spending requirements
364-
const consumedOutputs = this.determineOutputsToConsume(sendFundsOptions);
364+
const consumedOutputs = this.determineOutputsToConsume(sendFundsOptions, settings.gofConfThreshold);
365365

366366
const { inputs, consumedFunds } = this.buildInputs(consumedOutputs);
367367
const outputs = this.buildOutputs(sendFundsOptions, consumedFunds);
@@ -575,7 +575,7 @@ export class WalletService implements IWalletService {
575575
outputs: uo.outputs.filter(o => o.output.type === "SigLockedColoredOutputType").map(uid => ({
576576
id: uid.output.outputID.base58,
577577
balances: this.mapToArray(uid.output.output.balances),
578-
inclusionState: uid.inclusionState
578+
gof: uid.gradeOfFinality
579579
}))
580580
})));
581581
} while (spentAddresses.length > BLOCK_COUNT - 2);
@@ -607,10 +607,11 @@ export class WalletService implements IWalletService {
607607

608608
/**
609609
* From all the inputs determine which ones we need to consume.
610-
* @param sendFundOptions The request funds.
610+
* @param sendFundOptions The request funds options.
611+
* @param gofConfThreshold the grade of finality needed to consider the outputs as approved
611612
* @returns The output that we need to consume.
612613
*/
613-
private determineOutputsToConsume(sendFundOptions: ISendFundsOptions): {
614+
private determineOutputsToConsume(sendFundOptions: ISendFundsOptions, gofConfThreshold : number): {
614615
[address: string]: { [outputID: string]: IWalletOutput };
615616
} {
616617
const outputsToConsume: { [address: string]: { [outputID: string]: IWalletOutput } } = {};
@@ -637,9 +638,7 @@ export class WalletService implements IWalletService {
637638
const confirmedUnspentOutputs = unspentOutput.outputs.filter(o =>
638639
(!this._spentOutputTransactions ||
639640
!this._spentOutputTransactions.includes(o.id)) &&
640-
o.inclusionState.confirmed);
641-
642-
641+
o.gof >= gofConfThreshold);
643642
// scan the outputs on this address for required funds
644643
for (const output of confirmedUnspentOutputs) {
645644
// keeps track if the output contains any usable funds
@@ -895,14 +894,16 @@ export class WalletService implements IWalletService {
895894
const assetsMap: { [id: string]: IWalletAsset } = {};
896895
const addedAssets: IWalletAsset[] = [];
897896
const apiRegistryClient = await this.buildApiRegistryClient();
897+
const settingsService = ServiceFactory.get<SettingsService>("settings");
898+
const settings = await settingsService.get();
898899

899900
for (let i = 0; i <= this._wallet.lastAddressIndex; i++) {
900901
const addr = Seed.generateAddress(Base58.decode(this._wallet.seed), BigInt(i));
901902
const address: IWalletAddress = {
902903
index: BigInt(i),
903904
address: addr,
904905
isSpent: this._wallet &&
905-
this._wallet.spentAddresses.includes(addr) ? true : false
906+
this._wallet.spentAddresses.includes(addr) ? true : false
906907
};
907908
addressMap[address.address] = address;
908909
this._addresses.push(address);
@@ -948,15 +949,13 @@ export class WalletService implements IWalletService {
948949
colorMap[balance.color] = {
949950
asset: assetsMap[balance.color],
950951
confirmed: BigInt(0),
951-
unConfirmed: BigInt(0),
952-
rejected: BigInt(0)
952+
unConfirmed: BigInt(0)
953953
};
954954
this._balances.push(colorMap[balance.color]);
955955
}
956-
if (output.inclusionState.confirmed) {
956+
957+
if (output.gof >= settings.gofConfThreshold) {
957958
colorMap[balance.color].confirmed += balance.value;
958-
} else if (output.inclusionState.rejected) {
959-
colorMap[balance.color].rejected += balance.value;
960959
} else {
961960
colorMap[balance.color].unConfirmed += balance.value;
962961
}
@@ -1040,4 +1039,4 @@ export class WalletService implements IWalletService {
10401039
const settings = await settingsService.get();
10411040
return new ApiRegistryClient(settings.apiRegistryEndpoint);
10421041
}
1043-
}
1042+
}

0 commit comments

Comments
 (0)