Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Use bigint exclusively to represent balances. #147

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/IntermediaryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class IntermediaryCoordinator {
}

const fee = 0; // for example
const updatedState = targetClient.addHTLC(
const updatedState = await targetClient.addHTLC(
htlc.amount - BigInt(fee),
htlc.hashLock,
);
Expand Down
2 changes: 1 addition & 1 deletion clients/OwnerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class OwnerClient extends StateChannelWallet {
console.log("received invoice: ", invoice);

// create a state update with the hashlock
const signedUpdate = this.addHTLC(amount, invoice.hashLock);
const signedUpdate = await this.addHTLC(amount, invoice.hashLock);

// send the state update to the intermediary
const intermediaryAck = await this.sendPeerMessage({
Expand Down
10 changes: 5 additions & 5 deletions clients/StateChannelWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,18 +276,18 @@ export class StateChannelWallet {
}

// Craft an HTLC struct, put it inside a state, hash the state, sign and return it
addHTLC(amount: bigint, hash: string): SignedState {
async addHTLC(amount: bigint, hash: string): Promise<SignedState> {
const currentTimestamp: number = Math.floor(Date.now() / 1000); // Unix timestamp in seconds

if (this.myRole() === Participant.Intermediary) {
if (Number(this.currentState().intermediaryBalance) < BigInt(amount)) {
if (BigInt(this.currentState().intermediaryBalance) < BigInt(amount)) {
throw new Error("Insufficient balance");
}
}

if (
this.myRole() === Participant.Owner &&
Number(this.getOwnerBalance()) < BigInt(amount)
(await this.getOwnerBalance()) < amount
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we were actually casting a promise to a number, which results in nan < amount which is always false. So this check was completely broken.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yowsa. Good catch.

Because we're good wallet developers, I think there's at least a 60% chance that the intermediary would reject HTLCs that should have failed here.

) {
throw new Error("Insufficient balance");
}
Expand All @@ -307,7 +307,7 @@ export class StateChannelWallet {
const updated: StateStruct = {
owner: this.ownerAddress,
intermediary: this.intermediaryAddress,
turnNum: Number(this.currentState().turnNum) + 1,
turnNum: BigInt(this.currentState().turnNum) + 1n,
intermediaryBalance: BigInt(updatedIntermediaryBalance),
htlcs: [...this.currentState().htlcs, htlc],
};
Expand Down Expand Up @@ -347,7 +347,7 @@ export class StateChannelWallet {
const updated: StateStruct = {
intermediary: this.intermediaryAddress,
owner: this.ownerAddress,
turnNum: Number(this.currentState().turnNum) + 1,
turnNum: BigInt(this.currentState().turnNum) + 1n,
intermediaryBalance: newintermediaryBalance,
htlcs: this.currentState().htlcs.filter((h) => h !== unlockTarget),
};
Expand Down
6 changes: 2 additions & 4 deletions src/Intermediary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ export const Intermediary: React.FunctionComponent<{
sx={{ width: 24, height: 24 }}
/>
</Tooltip>
<Typography>
Owner balance: {formatEther(BigInt(ownerBalance))}
</Typography>
<Typography>Owner balance: {formatEther(ownerBalance)}</Typography>
</Stack>
<Stack
direction="row"
Expand All @@ -143,7 +141,7 @@ export const Intermediary: React.FunctionComponent<{
/>
</Tooltip>
<Typography>
IntermediaryBalance: {formatEther(BigInt(intermediaryBalance))}
IntermediaryBalance: {formatEther(intermediaryBalance)}
</Typography>
</Stack>
</>
Expand Down
4 changes: 2 additions & 2 deletions src/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ const Wallet: React.FunctionComponent<{ role: Role }> = (props: {
</Typography>
<Typography>
{" "}
<b> Balance:</b> {formatEther(BigInt(ownerBalance))}{" "}
<b> Balance:</b> {formatEther(ownerBalance)}{" "}
</Typography>
<Typography>
{" "}
<b> Inbound Capacity:</b> {formatEther(BigInt(intermediaryBalance))}{" "}
<b> Inbound Capacity:</b> {formatEther(intermediaryBalance)}{" "}
</Typography>
</Stack>
<br />
Expand Down
10 changes: 5 additions & 5 deletions src/useBalances.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { useState, useEffect } from "react";
import { type StateChannelWallet } from "../clients/StateChannelWallet";
import { UI_UPDATE_PERIOD } from "./constants";

export function useBalances(client: StateChannelWallet): [number, number] {
const [ownerBalance, setOwnerBalance] = useState(0);
const [intermediaryBalance, setIntermediaryBalance] = useState(0);
export function useBalances(client: StateChannelWallet): [bigint, bigint] {
const [ownerBalance, setOwnerBalance] = useState(0n);
const [intermediaryBalance, setIntermediaryBalance] = useState(0n);
useEffect(() => {
const interval = setInterval(() => {
client
.getOwnerBalance()
.then((b) => {
setOwnerBalance(Number(b));
setOwnerBalance(b);
})
.catch((e) => {
console.error(e);
});
setIntermediaryBalance(Number(client.intermediaryBalance));
setIntermediaryBalance(client.intermediaryBalance);
}, UI_UPDATE_PERIOD);
return () => {
clearInterval(interval);
Expand Down