闪电网络

闪电网络阅读

创建通道

sequenceDiagram
    participant A as Alice
    participant B as Bob

    A->>B: lnwire.OpenChannel
    B->>A: lnwire.AcceptChannel
   A->>B:lnwire.FundingCreated
   B->>A:lnwire.FundingSigned
   A->>B:lnwire.FundingLocked
   B->>A:lnwire.FundingLocked

==Bob就是等待通道创建事件,然后再等待6个确认以后就认为通道可以使用了.==

数据库中 channel 的存储

graph BT
    open-chan-bucket-->node1-pubkey
    open-chan-bucket-->node2-pubkey
    open-chan-bucket-->node3-pubkey
    node1-pubkey-->mainnet
    node1-pubkey-->testnet
    mainnet-->FundingOutPoint1[channel1]
    mainnet-->FundingOutPoint2[channel2]
    FundingOutPoint1---channelInformation(ChanType,TotalMSatRecived...)
    FundingOutPoint1---commitKey(localCommitment,remoteCommitment)
    FundingOutPoint1---revocationStateKey(RemoteRevocation...)

矩形表示 bucket
圆角矩形表示存储的序列化 Data

转发数据库结构

graph BT
fwd-packages--> short-channel-ID1
fwd-packages--> short-channel-ID2
fwd-packages--> short-channel-ID3
short-channel-ID1-->height1
short-channel-ID1-->height2
height1-->add-updates
height1-->fail-settle-updates
height1-->fwd-filter-key(fwd-filter-key)
height1-->ack-filter-key(ack-filter-key)
height1-->settle-fail-filter-key(settle-fail-filter-key)
add-updates-->idx:logupdate(idx:LogUpdate)
fail-settle-updates-->idx:LogUpdate

height1,height2这些指的是foward HTLC的 height
short-channel-ID 是一个8字节的通道 ID, 由 blocknumber,block 中的哪个 tx(tx 在通道中的Index),tx的哪个输出 (output index)

数据库中的 graph

graph BT
nodeBucket[graph-node]-->sourceKey(source->node public key)
nodeBucket[graph-node]-->nodePub1_node(node public key1->LightningNode1)
nodeBucket[graph-node]-->nodePub2_node(node public key2->LightningNode2)

nodeBucket[graph-node]-->aliasIndexBucket[alias]
nodeBucket[graph-node]-->nodeUpdateIndexBucket[graph-node-update-index]
aliasIndexBucket[alias]-->nodePub1_nickName(node public key1->node1's nick name)

source 是一个星型拓扑图的中心,实际上就是节点自身

graph BT
edgeBucket[graph-edge]-->edgeIndexBucket[edge-index]
edgeBucket[graph-edge]-->channelPointBucket[chan-index]
edgeIndexBucket-->shortChannelID(shortChannelID->ChannelEdgeInfo)
channelPointBucket[chan-index]-->ChannelPoint(FundingOutPoint->ShortChannelID)
edgeBucket[graph-edge]-->edgeKey(nodePub+ShortChannelID->ChannelEdgePolicy)

剪辑 Graph

graph BT
graphMetaBucket[graph-meta]-->pruneLogBucket[prune-log]
pruneLogBucket[prune-log]-->blockHeight_blockHash(block Height-> block Hash)

注意:
这里的 block Height 和 block Hash 就是指的链上某一块的高度(块号)和这个块的 hash 值

  1. 存储在数据库中的一条通道,这个是没有方向的
    ```go
    // ChannelEdgeInfo represents a fully authenticated channel along with all its
    // unique attributes. Once an authenticated channel announcement has been
    // processed on the network, then an instance of ChannelEdgeInfo encapsulating
    // the channels attributes is stored. The other portions relevant to routing
    // policy of a channel are stored within a ChannelEdgePolicy for each direction
    // of the channel.
    type ChannelEdgeInfo struct {
    // ChannelID is the unique channel ID for the channel. The first 3
    // bytes are the block height, the next 3 the index within the block,
    // and the last 2 bytes are the output index for the channel.
    ChannelID uint64

// ChainHash is the hash that uniquely identifies the chain that this
// channel was opened within.
//
// TODO(roasbeef): need to modify db keying for multi-chain
// * must add chain hash to prefix as well
ChainHash chainhash.Hash

// NodeKey1Bytes is the raw public key of the first node.
NodeKey1Bytes [33]byte
nodeKey1 *btcec.PublicKey

// NodeKey2Bytes is the raw public key of the first node.
NodeKey2Bytes [33]byte
nodeKey2 *btcec.PublicKey

// BitcoinKey1Bytes is the raw public key of the first node.
BitcoinKey1Bytes [33]byte
bitcoinKey1 *btcec.PublicKey

// BitcoinKey2Bytes is the raw public key of the first node.
BitcoinKey2Bytes [33]byte
bitcoinKey2 *btcec.PublicKey

// Features is an opaque byte slice that encodes the set of channel
// specific features that this channel edge supports.
Features []byte

// AuthProof is the authentication proof for this channel. This proof
// contains a set of signatures binding four identities, which attests
// to the legitimacy of the advertised channel.
AuthProof *ChannelAuthProof

// ChannelPoint is the funding outpoint of the channel. This can be
// used to uniquely identify the channel within the channel graph.
ChannelPoint wire.OutPoint

// Capacity is the total capacity of the channel, this is determined by
// the value output in the outpoint that created this channel.
Capacity btcutil.Amount
}
// ChannelAuthProof is the authentication proof (the signature portion) for a
// channel. Using the four signatures contained in the struct, and some
// auxiliary knowledge (the funding script, node identities, and outpoint) nodes
// on the network are able to validate the authenticity and existence of a
// channel. Each of these signatures signs the following digest: chanID ||
// nodeID1 || nodeID2 || bitcoinKey1|| bitcoinKey2 || 2-byte-feature-len ||
// features.
这个是双方广播通道时候的信息
type ChannelAuthProof struct {
// nodeSig1 is a cached instance of the first node signature.
nodeSig1 *btcec.Signature

// NodeSig1Bytes are the raw bytes of the first node signature encoded
// in DER format.
NodeSig1Bytes []byte

// nodeSig2 is a cached instance of the second node signature.
nodeSig2 *btcec.Signature

// NodeSig2Bytes are the raw bytes of the second node signature
// encoded in DER format.
NodeSig2Bytes []byte

// bitcoinSig1 is a cached instance of the first bitcoin signature.
bitcoinSig1 *btcec.Signature

// BitcoinSig1Bytes are the raw bytes of the first bitcoin signature
// encoded in DER format.
BitcoinSig1Bytes []byte

// bitcoinSig2 is a cached instance of the second bitcoin signature.
bitcoinSig2 *btcec.Signature

// BitcoinSig2Bytes are the raw bytes of the second bitcoin signature
// encoded in DER format.
BitcoinSig2Bytes []byte
}
// ChannelEdgePolicy represents a directed edge within the channel graph. For
// each channel in the database, there are two distinct edges: one for each
// possible direction of travel along the channel. The edges themselves hold
// information concerning fees, and minimum time-lock information which is
// utilized during path finding.
这个是通道参与一方定义的如何处理从我这里路过交易
type ChannelEdgePolicy struct {
// SigBytes is the raw bytes of the signature of the channel edge
// policy. We'll only parse these if the caller needs to access the
// signature for validation purposes.
SigBytes []byte

// sig is a cached fully parsed signature.
sig *btcec.Signature

// ChannelID is the unique channel ID for the channel. The first 3
// bytes are the block height, the next 3 the index within the block,
// and the last 2 bytes are the output index for the channel.
ChannelID uint64

// LastUpdate is the last time an authenticated edge for this channel
// was received.
LastUpdate time.Time

// Flags is a bitfield which signals the capabilities of the channel as
// well as the directed edge this update applies to.
Flags lnwire.ChanUpdateFlag

// TimeLockDelta is the number of blocks this node will subtract from
// the expiry of an incoming HTLC. This value expresses the time buffer
// the node would like to HTLC exchanges.
TimeLockDelta uint16

// MinHTLC is the smallest value HTLC this node will accept, expressed
// in millisatoshi.
MinHTLC lnwire.MilliSatoshi

// FeeBaseMSat is the base HTLC fee that will be charged for forwarding
// ANY HTLC, expressed in mSAT's.
FeeBaseMSat lnwire.MilliSatoshi

// FeeProportionalMillionths is the rate that the node will charge for
// HTLCs for each millionth of a satoshi forwarded.
FeeProportionalMillionths lnwire.MilliSatoshi

// Node is the LightningNode that this directed edge leads to. Using
// this pointer the channel graph can further be traversed.
Node *LightningNode

db *DB
}

// LightningNode represents an individual vertex/node within the channel graph.
// A node is connected to other nodes by one or more channel edges emanating
// from it. As the graph is directed, a node will also have an incoming edge
// attached to it for each outgoing edge.
type LightningNode struct {
// PubKeyBytes is the raw bytes of the public key of the target node.
PubKeyBytes [33]byte
pubKey *btcec.PublicKey

// HaveNodeAnnouncement indicates whether we received a node
// announcement for this particular node. If true, the remaining fields
// will be set, if false only the PubKey is known for this node.
HaveNodeAnnouncement bool

// LastUpdate is the last time the vertex information for this node has
// been updated.
LastUpdate time.Time

// Address is the TCP address this node is reachable over.
Addresses []net.Addr

// Color is the selected color for the node.
Color color.RGBA

// Alias is a nick-name for the node. The alias can be used to confirm
// a node's identity or to serve as a short ID for an address book.
Alias string

// AuthSigBytes is the raw signature under the advertised public key
// which serves to authenticate the attributes announced by this node.
AuthSigBytes []byte

// Features is the list of protocol features supported by this node.
Features *lnwire.FeatureVector

db *DB

// TODO(roasbeef): discovery will need storage to keep it's last IP
// address and re-announce if interface changes?

// TODO(roasbeef): add update method and fetch?
}

```

术语

settle 类似于 raiden 中的 unlock 消息,结束一笔 HTLC
PublicKey 32字节的公钥