Skip to content

Commit

Permalink
Merge pull request #414 from BoltzExchange/hold-fixes
Browse files Browse the repository at this point in the history
fix: hold invoice overpayment
  • Loading branch information
michael1011 committed Oct 30, 2023
2 parents a1a9751 + e0260d3 commit 383d494
Show file tree
Hide file tree
Showing 37 changed files with 1,168 additions and 779 deletions.
4 changes: 1 addition & 3 deletions docker/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,7 @@ def build_images(
)
else:
extra_tag = "" if no_latest else f"-t {name}:latest"
command = (
f"docker build -t {name}:{tag} {extra_tag} -f {dockerfile} {args} ."
)
command = f"docker build -t {name}:{tag} {extra_tag} -f {dockerfile} {args} ."

if no_cache:
command = command + " --no-cache"
Expand Down
9 changes: 9 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ mnemonicpath = "/home/boltz/.boltz/seed.dat"
# Possible values are: error, warning, info, verbose, debug, silly
loglevel = "debug"

# Logs can be sent to a Loki log aggregator
# lokiHost = "http://127.0.0.1:3100"
# lokiNetwork = "regtest"

# This value configures the type of the lockup address of normal Submarine Swaps:
# - false: P2SH nested P2WSH
# - true: P2WSH
Expand All @@ -51,6 +55,11 @@ prepayminerfee = false
host = "127.0.0.1"
port = 9_001

# The backend can expose a metrics endpoint about swap count, volume, etc
# [prometheus]
# host = "127.0.0.1"
# port = 9_092

# And this the gRPC API that is used by the boltz-cli executable
[grpc]
host = "127.0.0.1"
Expand Down
6 changes: 5 additions & 1 deletion lib/Boltz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ class Boltz {
new GrpcService(this.service),
);

this.prometheus = new Prometheus(this.logger, this.config.prometheus);
this.prometheus = new Prometheus(
this.logger,
this.config.prometheus,
this.config.pairs,
);

this.api = new Api(this.logger, this.config.api, this.service);
} catch (error) {
Expand Down
30 changes: 26 additions & 4 deletions lib/Prometheus.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import express, { Response } from 'express';
import { collectDefaultMetrics, Gauge, Registry } from 'prom-client';
import Logger from './Logger';
import StatsRepository from './db/repositories/StatsRepository';
import { getPairId } from './Utils';
import { PairConfig } from './consts/Types';
import StatsRepository, { SwapType } from './db/repositories/StatsRepository';

type PrometheusConfig = {
host?: string;
Expand All @@ -11,13 +13,18 @@ type PrometheusConfig = {
class Prometheus {
private static readonly metric_prefix = 'boltz_';

private readonly pairs = new Set<string>();

private readonly nodeRegistry?: Registry;
private readonly swapRegistry?: Registry;

constructor(
private readonly logger: Logger,
private readonly config?: PrometheusConfig,
private readonly config: PrometheusConfig | undefined,
pairs: PairConfig[],
) {
pairs.forEach((pair) => this.pairs.add(getPairId(pair)));

if (
this.config === undefined ||
Object.values(this.config).some((value) => value === undefined)
Expand Down Expand Up @@ -71,6 +78,19 @@ class Prometheus {
register: this.nodeRegistry,
});

const defaults = Array.from(this.pairs.values()).flatMap((pair) =>
Object.values(SwapType).map((type) => ({
type,
pair,
})),
);

const setDefaults = (gauge: Gauge, defaultValue: number) => {
defaults.forEach((defaultLabels) => {
gauge.set(defaultLabels, defaultValue);
});
};

this.swapRegistry!.registerMetric(
new Gauge({
name: `${Prometheus.metric_prefix}swap_counts`,
Expand Down Expand Up @@ -112,8 +132,9 @@ class Prometheus {
collect: async function () {
const counts = await StatsRepository.getPendingSwapsCounts();

counts.forEach((volume) =>
this.set({ pair: volume.pair, type: volume.type }, volume.count),
setDefaults(this, 0);
counts.forEach((count) =>
this.set({ pair: count.pair, type: count.type }, count.count),
);
},
}),
Expand All @@ -127,6 +148,7 @@ class Prometheus {
collect: async function () {
const lockedFunds = await StatsRepository.getLockedFunds();

setDefaults(this, 0);
lockedFunds.forEach((locked) =>
this.set({ pair: locked.pair, type: 'reverse' }, locked.locked),
);
Expand Down
2 changes: 1 addition & 1 deletion lib/VersionCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class VersionCheck {
},
[ClnClient.serviceNameHold]: {
minimal: '0.0.3',
maximal: '0.0.3',
maximal: '0.0.4',
},
[LndClient.serviceName]: {
minimal: '0.16.0',
Expand Down
7 changes: 5 additions & 2 deletions lib/db/repositories/StatsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ type FailureRate = StatsDate & {
failureRate: number;
};

type SwapType = 'swap' | 'reverse';
enum SwapType {
Swap = 'swap',
Reverse = 'reverse',
}

type BaseMetric = {
pair: string;
Expand Down Expand Up @@ -320,4 +323,4 @@ GROUP BY pair;
}

export default StatsRepository;
export { StatsDate };
export { SwapType, StatsDate };
9 changes: 9 additions & 0 deletions lib/proto/hold/hold_pb.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ export class Htlc extends jspb.Message {
setMsat(value: number): Htlc;
getCreatedAt(): number;
setCreatedAt(value: number): Htlc;
getShortChannelId(): string;
setShortChannelId(value: string): Htlc;
getId(): number;
setId(value: number): Htlc;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): Htlc.AsObject;
Expand All @@ -349,6 +353,8 @@ export namespace Htlc {
state: HtlcState;
msat: number;
createdAt: number;
shortChannelId: string;
id: number;
};
}

Expand All @@ -370,6 +376,8 @@ export class Invoice extends jspb.Message {
getHtlcsList(): Array<Htlc>;
setHtlcsList(value: Array<Htlc>): Invoice;
addHtlcs(value?: Htlc, index?: number): Htlc;
getAmountMsat(): number;
setAmountMsat(value: number): Invoice;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): Invoice.AsObject;
Expand Down Expand Up @@ -397,6 +405,7 @@ export namespace Invoice {
bolt11: string;
createdAt: number;
htlcsList: Array<Htlc.AsObject>;
amountMsat: number;
};
}

Expand Down
94 changes: 92 additions & 2 deletions lib/proto/hold/hold_pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -2291,7 +2291,9 @@ proto.hold.Htlc.toObject = function(includeInstance, msg) {
var f, obj = {
state: jspb.Message.getFieldWithDefault(msg, 1, 0),
msat: jspb.Message.getFieldWithDefault(msg, 2, 0),
createdAt: jspb.Message.getFieldWithDefault(msg, 3, 0)
createdAt: jspb.Message.getFieldWithDefault(msg, 3, 0),
shortChannelId: jspb.Message.getFieldWithDefault(msg, 4, ""),
id: jspb.Message.getFieldWithDefault(msg, 5, 0)
};

if (includeInstance) {
Expand Down Expand Up @@ -2340,6 +2342,14 @@ proto.hold.Htlc.deserializeBinaryFromReader = function(msg, reader) {
var value = /** @type {number} */ (reader.readUint64());
msg.setCreatedAt(value);
break;
case 4:
var value = /** @type {string} */ (reader.readString());
msg.setShortChannelId(value);
break;
case 5:
var value = /** @type {number} */ (reader.readUint64());
msg.setId(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -2390,6 +2400,20 @@ proto.hold.Htlc.serializeBinaryToWriter = function(message, writer) {
f
);
}
f = message.getShortChannelId();
if (f.length > 0) {
writer.writeString(
4,
f
);
}
f = message.getId();
if (f !== 0) {
writer.writeUint64(
5,
f
);
}
};


Expand Down Expand Up @@ -2447,6 +2471,42 @@ proto.hold.Htlc.prototype.setCreatedAt = function(value) {
};


/**
* optional string short_channel_id = 4;
* @return {string}
*/
proto.hold.Htlc.prototype.getShortChannelId = function() {
return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 4, ""));
};


/**
* @param {string} value
* @return {!proto.hold.Htlc} returns this
*/
proto.hold.Htlc.prototype.setShortChannelId = function(value) {
return jspb.Message.setProto3StringField(this, 4, value);
};


/**
* optional uint64 id = 5;
* @return {number}
*/
proto.hold.Htlc.prototype.getId = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 5, 0));
};


/**
* @param {number} value
* @return {!proto.hold.Htlc} returns this
*/
proto.hold.Htlc.prototype.setId = function(value) {
return jspb.Message.setProto3IntField(this, 5, value);
};



/**
* List of repeated fields within this message type.
Expand Down Expand Up @@ -2492,7 +2552,8 @@ proto.hold.Invoice.toObject = function(includeInstance, msg) {
bolt11: jspb.Message.getFieldWithDefault(msg, 4, ""),
createdAt: jspb.Message.getFieldWithDefault(msg, 5, 0),
htlcsList: jspb.Message.toObjectList(msg.getHtlcsList(),
proto.hold.Htlc.toObject, includeInstance)
proto.hold.Htlc.toObject, includeInstance),
amountMsat: jspb.Message.getFieldWithDefault(msg, 7, 0)
};

if (includeInstance) {
Expand Down Expand Up @@ -2554,6 +2615,10 @@ proto.hold.Invoice.deserializeBinaryFromReader = function(msg, reader) {
reader.readMessage(value,proto.hold.Htlc.deserializeBinaryFromReader);
msg.addHtlcs(value);
break;
case 7:
var value = /** @type {number} */ (reader.readUint64());
msg.setAmountMsat(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -2626,6 +2691,13 @@ proto.hold.Invoice.serializeBinaryToWriter = function(message, writer) {
proto.hold.Htlc.serializeBinaryToWriter
);
}
f = message.getAmountMsat();
if (f !== 0) {
writer.writeUint64(
7,
f
);
}
};


Expand Down Expand Up @@ -2775,6 +2847,24 @@ proto.hold.Invoice.prototype.clearHtlcsList = function() {
};


/**
* optional uint64 amount_msat = 7;
* @return {number}
*/
proto.hold.Invoice.prototype.getAmountMsat = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 7, 0));
};


/**
* @param {number} value
* @return {!proto.hold.Invoice} returns this
*/
proto.hold.Invoice.prototype.setAmountMsat = function(value) {
return jspb.Message.setProto3IntField(this, 7, value);
};



/**
* List of repeated fields within this message type.
Expand Down
9 changes: 6 additions & 3 deletions lib/swap/SwapNursery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ class SwapNursery extends EventEmitter implements ISwapNursery {
if (
typeof error !== 'object' ||
((error as any).details !== 'unable to locate invoice' &&
(error as any).details !== 'there are no existing invoices')
(error as any).details !== 'there are no existing invoices' &&
(error as any).message !== 'hold invoice not found')
) {
this.logger.error(
`Could not cancel invoice${plural} of Reverse Swap ${
Expand All @@ -446,10 +447,12 @@ class SwapNursery extends EventEmitter implements ISwapNursery {
);
return;
} else {
this.logger.warn(
this.logger.silly(
`Cancelling invoice${plural} of Reverse Swap ${
reverseSwap.id
} failed although they could be found: ${formatError(error)}`,
} failed because they could not be found: ${formatError(
error,
)}`,
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/wallet/ethereum/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ErrorCodePrefix } from '../../consts/Enums';

export default {
NO_PROVIDER_SPECIFIED: (): Error => ({
message: 'no Web3 provider was specified',
message: 'no RPC provider was specified',
code: concatErrorCode(ErrorCodePrefix.Ethereum, 0),
}),
NO_LOCKUP_FOUND: (): Error => ({
Expand All @@ -23,7 +23,7 @@ export default {
);

return {
message: `not all web3 provider networks are equal: ${networkStrings.join(
message: `not all RPC provider networks are equal: ${networkStrings.join(
', ',
)}`,
code: concatErrorCode(ErrorCodePrefix.Ethereum, 3),
Expand Down
6 changes: 5 additions & 1 deletion lib/wallet/ethereum/EthereumManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ class EthereumManager {
this.networkDetails = isRsk ? Rsk : Ethereum;

this.config = config;
this.provider = new InjectedProvider(this.logger, this.config);
this.provider = new InjectedProvider(
this.logger,
this.networkDetails,
this.config,
);

this.logger.debug(
`Using ${this.networkDetails.name} EtherSwap contract: ${this.config.etherSwapAddress}`,
Expand Down

0 comments on commit 383d494

Please sign in to comment.