2016-12-22 23:24:48 +03:00
|
|
|
package lnwire
|
|
|
|
|
2017-03-27 18:22:37 +03:00
|
|
|
// ShortChannelID represent the set of data which is needed to retrieve all
|
2016-12-22 23:24:48 +03:00
|
|
|
// necessary data to validate the channel existence.
|
2017-03-27 18:22:37 +03:00
|
|
|
type ShortChannelID struct {
|
2016-12-22 23:24:48 +03:00
|
|
|
// BlockHeight is the height of the block where funding transaction
|
|
|
|
// located.
|
|
|
|
//
|
|
|
|
// NOTE: This field is limited to 3 bytes.
|
|
|
|
BlockHeight uint32
|
|
|
|
|
|
|
|
// TxIndex is a position of funding transaction within a block.
|
|
|
|
//
|
|
|
|
// NOTE: This field is limited to 3 bytes.
|
|
|
|
TxIndex uint32
|
|
|
|
|
2017-01-15 04:52:05 +03:00
|
|
|
// TxPosition indicating transaction output which pays to the channel.
|
2016-12-22 23:24:48 +03:00
|
|
|
TxPosition uint16
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:22:37 +03:00
|
|
|
// NewShortChanIDFromInt returns a new ShortChannelID which is the decoded
|
|
|
|
// version of the compact channel ID encoded within the uint64. The format of
|
|
|
|
// the compact channel ID is as follows: 3 bytes for the block height, 3 bytes
|
|
|
|
// for the transaction index, and 2 bytes for the output index.
|
|
|
|
func NewShortChanIDFromInt(chanID uint64) ShortChannelID {
|
|
|
|
return ShortChannelID{
|
2016-12-22 23:24:48 +03:00
|
|
|
BlockHeight: uint32(chanID >> 40),
|
|
|
|
TxIndex: uint32(chanID>>16) & 0xFFFFFF,
|
|
|
|
TxPosition: uint16(chanID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:22:37 +03:00
|
|
|
// ToUint64 converts the ShortChannelID into a compact format encoded within a
|
2016-12-22 23:24:48 +03:00
|
|
|
// uint64 (8 bytes).
|
2017-03-27 18:22:37 +03:00
|
|
|
func (c *ShortChannelID) ToUint64() uint64 {
|
2016-12-22 23:24:48 +03:00
|
|
|
// TODO(roasbeef): explicit error on overflow?
|
|
|
|
return ((uint64(c.BlockHeight) << 40) | (uint64(c.TxIndex) << 16) |
|
|
|
|
(uint64(c.TxPosition)))
|
|
|
|
}
|