import CommonBase from './CommonBase.mjs';
import * as bindings from '../bindings.mjs'
-import * as InternalUtils from '../InternalUtils.mjs'
+/**
+ * Represents the network as nodes and channels between them
+ */
export class NetworkGraph extends CommonBase {
/* @internal */
public constructor(_dummy: object, ptr: number) {
return ret;
}
+ /**
+ * Creates a copy of the NetworkGraph
+ */
public clone(): NetworkGraph {
const ret: number = bindings.NetworkGraph_clone(this.ptr);
const ret_hu_conv: NetworkGraph = new NetworkGraph(null, ret);
return ret_hu_conv;
}
+ /**
+ * Serialize the NetworkGraph object into a byte array which can be read by NetworkGraph_read
+ */
public write(): Uint8Array {
- const ret: Uint8Array = bindings.NetworkGraph_write(this.ptr);
- return ret;
+ const ret: number = bindings.NetworkGraph_write(this.ptr);
+ const ret_conv: Uint8Array = bindings.decodeUint8Array(ret);
+ return ret_conv;
}
+ /**
+ * Read a NetworkGraph from a byte array, created by NetworkGraph_write
+ */
public static constructor_read(ser: Uint8Array): Result_NetworkGraphDecodeErrorZ {
- const ret: number = bindings.NetworkGraph_read(ser);
+ const ret: number = bindings.NetworkGraph_read(bindings.encodeUint8Array(ser));
const ret_hu_conv: Result_NetworkGraphDecodeErrorZ = Result_NetworkGraphDecodeErrorZ.constr_from_ptr(ret);
return ret_hu_conv;
}
+ /**
+ * Creates a new, empty, network graph.
+ */
public static constructor_new(genesis_hash: Uint8Array): NetworkGraph {
- const ret: number = bindings.NetworkGraph_new(InternalUtils.check_arr_len(genesis_hash, 32));
+ const ret: number = bindings.NetworkGraph_new(bindings.encodeUint8Array(bindings.check_arr_len(genesis_hash, 32)));
const ret_hu_conv: NetworkGraph = new NetworkGraph(null, ret);
CommonBase.add_ref_from(ret_hu_conv, ret_hu_conv);
return ret_hu_conv;
}
+ /**
+ * Returns a read-only view of the network graph.
+ */
public read_only(): ReadOnlyNetworkGraph {
const ret: number = bindings.NetworkGraph_read_only(this.ptr);
const ret_hu_conv: ReadOnlyNetworkGraph = new ReadOnlyNetworkGraph(null, ret);
return ret_hu_conv;
}
+ /**
+ * For an already known node (from channel announcements), update its stored properties from a
+ * given node announcement.
+ *
+ * You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
+ * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
+ * routing messages from a source using a protocol other than the lightning P2P protocol.
+ */
public update_node_from_announcement(msg: NodeAnnouncement): Result_NoneLightningErrorZ {
const ret: number = bindings.NetworkGraph_update_node_from_announcement(this.ptr, msg == null ? 0 : CommonBase.get_ptr_of(msg) & ~1);
const ret_hu_conv: Result_NoneLightningErrorZ = Result_NoneLightningErrorZ.constr_from_ptr(ret);
return ret_hu_conv;
}
+ /**
+ * For an already known node (from channel announcements), update its stored properties from a
+ * given node announcement without verifying the associated signatures. Because we aren't
+ * given the associated signatures here we cannot relay the node announcement to any of our
+ * peers.
+ */
public update_node_from_unsigned_announcement(msg: UnsignedNodeAnnouncement): Result_NoneLightningErrorZ {
const ret: number = bindings.NetworkGraph_update_node_from_unsigned_announcement(this.ptr, msg == null ? 0 : CommonBase.get_ptr_of(msg) & ~1);
const ret_hu_conv: Result_NoneLightningErrorZ = Result_NoneLightningErrorZ.constr_from_ptr(ret);
return ret_hu_conv;
}
+ /**
+ * Store or update channel info from a channel announcement.
+ *
+ * You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
+ * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
+ * routing messages from a source using a protocol other than the lightning P2P protocol.
+ *
+ * If a `chain::Access` object is provided via `chain_access`, it will be called to verify
+ * the corresponding UTXO exists on chain and is correctly-formatted.
+ */
public update_channel_from_announcement(msg: ChannelAnnouncement, chain_access: Option_AccessZ): Result_NoneLightningErrorZ {
const ret: number = bindings.NetworkGraph_update_channel_from_announcement(this.ptr, msg == null ? 0 : CommonBase.get_ptr_of(msg) & ~1, CommonBase.get_ptr_of(chain_access));
const ret_hu_conv: Result_NoneLightningErrorZ = Result_NoneLightningErrorZ.constr_from_ptr(ret);
return ret_hu_conv;
}
+ /**
+ * Store or update channel info from a channel announcement without verifying the associated
+ * signatures. Because we aren't given the associated signatures here we cannot relay the
+ * channel announcement to any of our peers.
+ *
+ * If a `chain::Access` object is provided via `chain_access`, it will be called to verify
+ * the corresponding UTXO exists on chain and is correctly-formatted.
+ */
public update_channel_from_unsigned_announcement(msg: UnsignedChannelAnnouncement, chain_access: Option_AccessZ): Result_NoneLightningErrorZ {
const ret: number = bindings.NetworkGraph_update_channel_from_unsigned_announcement(this.ptr, msg == null ? 0 : CommonBase.get_ptr_of(msg) & ~1, CommonBase.get_ptr_of(chain_access));
const ret_hu_conv: Result_NoneLightningErrorZ = Result_NoneLightningErrorZ.constr_from_ptr(ret);
return ret_hu_conv;
}
- public close_channel_from_update(short_channel_id: number, is_permanent: boolean): void {
+ /**
+ * Close a channel if a corresponding HTLC fail was sent.
+ * If permanent, removes a channel from the local storage.
+ * May cause the removal of nodes too, if this was their last channel.
+ * If not permanent, makes channels unavailable for routing.
+ */
+ public close_channel_from_update(short_channel_id: bigint, is_permanent: boolean): void {
bindings.NetworkGraph_close_channel_from_update(this.ptr, short_channel_id, is_permanent);
}
+ /**
+ * Marks a node in the graph as failed.
+ */
public fail_node(_node_id: Uint8Array, is_permanent: boolean): void {
- bindings.NetworkGraph_fail_node(this.ptr, InternalUtils.check_arr_len(_node_id, 33), is_permanent);
+ bindings.NetworkGraph_fail_node(this.ptr, bindings.encodeUint8Array(bindings.check_arr_len(_node_id, 33)), is_permanent);
}
- public remove_stale_channels_with_time(current_time_unix: number): void {
+ /**
+ * Removes information about channels that we haven't heard any updates about in some time.
+ * This can be used regularly to prune the network graph of channels that likely no longer
+ * exist.
+ *
+ * While there is no formal requirement that nodes regularly re-broadcast their channel
+ * updates every two weeks, the non-normative section of BOLT 7 currently suggests that
+ * pruning occur for updates which are at least two weeks old, which we implement here.
+ *
+ * This function takes the current unix time as an argument. For users with the `std` feature
+ * enabled, [`NetworkGraph::remove_stale_channels`] may be preferable.
+ */
+ public remove_stale_channels_with_time(current_time_unix: bigint): void {
bindings.NetworkGraph_remove_stale_channels_with_time(this.ptr, current_time_unix);
}
+ /**
+ * For an already known (from announcement) channel, update info about one of the directions
+ * of the channel.
+ *
+ * You probably don't want to call this directly, instead relying on a NetGraphMsgHandler's
+ * RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
+ * routing messages from a source using a protocol other than the lightning P2P protocol.
+ *
+ * If built with `no-std`, any updates with a timestamp more than two weeks in the past or
+ * materially in the future will be rejected.
+ */
public update_channel(msg: ChannelUpdate): Result_NoneLightningErrorZ {
const ret: number = bindings.NetworkGraph_update_channel(this.ptr, msg == null ? 0 : CommonBase.get_ptr_of(msg) & ~1);
const ret_hu_conv: Result_NoneLightningErrorZ = Result_NoneLightningErrorZ.constr_from_ptr(ret);
return ret_hu_conv;
}
+ /**
+ * For an already known (from announcement) channel, update info about one of the directions
+ * of the channel without verifying the associated signatures. Because we aren't given the
+ * associated signatures here we cannot relay the channel update to any of our peers.
+ *
+ * If built with `no-std`, any updates with a timestamp more than two weeks in the past or
+ * materially in the future will be rejected.
+ */
public update_channel_unsigned(msg: UnsignedChannelUpdate): Result_NoneLightningErrorZ {
const ret: number = bindings.NetworkGraph_update_channel_unsigned(this.ptr, msg == null ? 0 : CommonBase.get_ptr_of(msg) & ~1);
const ret_hu_conv: Result_NoneLightningErrorZ = Result_NoneLightningErrorZ.constr_from_ptr(ret);