lnwire: properly fold at 80-columns within features.go
This commit is contained in:
parent
c21cbc5021
commit
0db384fd6b
@ -24,14 +24,14 @@ func (f featureFlag) String() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// featureName represent the name of the feature and needed in order to have the
|
// featureName represent the name of the feature and needed in order to have
|
||||||
// compile errors if we specify wrong feature name.
|
// the compile errors if we specify wrong feature name.
|
||||||
type featureName string
|
type featureName string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// OptionalFlag represent the feature which we already have but it isn't
|
// OptionalFlag represent the feature which we already have but it
|
||||||
// required yet, and if remote peer doesn't have this feature we may
|
// isn't required yet, and if remote peer doesn't have this feature we
|
||||||
// turn it off without disconnecting with peer.
|
// may turn it off without disconnecting with peer.
|
||||||
OptionalFlag featureFlag = 2 // 0b10
|
OptionalFlag featureFlag = 2 // 0b10
|
||||||
|
|
||||||
// RequiredFlag represent the features which is required for proper
|
// RequiredFlag represent the features which is required for proper
|
||||||
@ -47,16 +47,17 @@ const (
|
|||||||
flagBitsSize = 2
|
flagBitsSize = 2
|
||||||
|
|
||||||
// maxAllowedSize is a maximum allowed size of feature vector.
|
// maxAllowedSize is a maximum allowed size of feature vector.
|
||||||
|
//
|
||||||
// NOTE: Within the protocol, the maximum allowed message size is 65535
|
// NOTE: Within the protocol, the maximum allowed message size is 65535
|
||||||
// bytes. Adding the overhead from the crypto protocol (the 2-byte packet
|
// bytes. Adding the overhead from the crypto protocol (the 2-byte
|
||||||
// length and 16-byte MAC), we arrive at 65569 bytes. Accounting for the
|
// packet length and 16-byte MAC), we arrive at 65569 bytes. Accounting
|
||||||
// overhead within the feature message to signal the type of the message,
|
// for the overhead within the feature message to signal the type of
|
||||||
// that leaves 65567 bytes for the init message itself. Next, we reserve
|
// the message, that leaves 65567 bytes for the init message itself.
|
||||||
// 4-bytes to encode the lengths of both the local and global feature
|
// Next, we reserve 4-bytes to encode the lengths of both the local and
|
||||||
// vectors, so 65563 for the global and local features. Knocking off one
|
// global feature vectors, so 65563 for the global and local features.
|
||||||
// byte for the sake of the calculation, that leads to a max allowed
|
// Knocking off one byte for the sake of the calculation, that leads to
|
||||||
// size of 32781 bytes for each feature vector, or 131124 different
|
// a max allowed size of 32781 bytes for each feature vector, or 131124
|
||||||
// features.
|
// different features.
|
||||||
maxAllowedSize = 32781
|
maxAllowedSize = 32781
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -67,14 +68,14 @@ type Feature struct {
|
|||||||
Flag featureFlag
|
Flag featureFlag
|
||||||
}
|
}
|
||||||
|
|
||||||
// FeatureVector represents the global/local feature vector. With this structure
|
// FeatureVector represents the global/local feature vector. With this
|
||||||
// you may set/get the feature by name and compare feature vector with remote
|
// structure you may set/get the feature by name and compare feature vector
|
||||||
// one.
|
// with remote one.
|
||||||
type FeatureVector struct {
|
type FeatureVector struct {
|
||||||
// featuresMap is the map which stores the correspondence between
|
// featuresMap is the map which stores the correspondence between
|
||||||
// feature name and its index within feature vector. Index within
|
// feature name and its index within feature vector. Index within
|
||||||
// feature vector and actual binary position of feature
|
// feature vector and actual binary position of feature are different
|
||||||
// are different things)
|
// things)
|
||||||
featuresMap map[featureName]int // name -> index
|
featuresMap map[featureName]int // name -> index
|
||||||
|
|
||||||
// flags is the map which stores the correspondence between feature
|
// flags is the map which stores the correspondence between feature
|
||||||
@ -116,12 +117,11 @@ func (f *FeatureVector) serializedSize() uint16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewFeatureVectorFromReader decodes the feature vector from binary
|
// NewFeatureVectorFromReader decodes the feature vector from binary
|
||||||
// representation and creates the instance of it.
|
// representation and creates the instance of it. Every feature decoded as 2
|
||||||
// Every feature decoded as 2 bits where odd bit determine whether the feature
|
// bits where odd bit determine whether the feature is "optional" and even bit
|
||||||
// is "optional" and even bit told us whether the feature is "required". The
|
// told us whether the feature is "required". The even/odd semantic allows
|
||||||
// even/odd semantic allows future incompatible changes, or backward compatible
|
// future incompatible changes, or backward compatible changes. Bits generally
|
||||||
// changes. Bits generally assigned in pairs, so that optional features can
|
// assigned in pairs, so that optional features can later become compulsory.
|
||||||
// later become compulsory.
|
|
||||||
func NewFeatureVectorFromReader(r io.Reader) (*FeatureVector, error) {
|
func NewFeatureVectorFromReader(r io.Reader) (*FeatureVector, error) {
|
||||||
f := &FeatureVector{
|
f := &FeatureVector{
|
||||||
flags: make(map[int]featureFlag),
|
flags: make(map[int]featureFlag),
|
||||||
@ -166,12 +166,12 @@ func NewFeatureVectorFromReader(r io.Reader) (*FeatureVector, error) {
|
|||||||
return f, nil
|
return f, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode encodes the features vector into bytes representation, every
|
// Encode encodes the features vector into bytes representation, every feature
|
||||||
// feature encoded as 2 bits where odd bit determine whether the feature is
|
// encoded as 2 bits where odd bit determine whether the feature is "optional"
|
||||||
// "optional" and even bit told us whether the feature is "required". The
|
// and even bit told us whether the feature is "required". The even/odd
|
||||||
// even/odd semantic allows future incompatible changes, or backward compatible
|
// semantic allows future incompatible changes, or backward compatible changes.
|
||||||
// changes. Bits generally assigned in pairs, so that optional features can
|
// Bits generally assigned in pairs, so that optional features can later become
|
||||||
// later become compulsory.
|
// compulsory.
|
||||||
func (f *FeatureVector) Encode(w io.Writer) error {
|
func (f *FeatureVector) Encode(w io.Writer) error {
|
||||||
setFlag := func(data []byte, position int, flag featureFlag) {
|
setFlag := func(data []byte, position int, flag featureFlag) {
|
||||||
byteNumber := uint(position / 8)
|
byteNumber := uint(position / 8)
|
||||||
@ -191,8 +191,8 @@ func (f *FeatureVector) Encode(w io.Writer) error {
|
|||||||
// Generate the data and write it.
|
// Generate the data and write it.
|
||||||
data := make([]byte, length)
|
data := make([]byte, length)
|
||||||
for index, flag := range f.flags {
|
for index, flag := range f.flags {
|
||||||
// Every feature takes 2 bits, so in order to get the
|
// Every feature takes 2 bits, so in order to get the feature
|
||||||
// feature bits position we should multiply index by 2.
|
// bits position we should multiply index by 2.
|
||||||
position := index * flagBitsSize
|
position := index * flagBitsSize
|
||||||
setFlag(data, position, flag)
|
setFlag(data, position, flag)
|
||||||
}
|
}
|
||||||
@ -263,9 +263,9 @@ func (f *FeatureVector) Copy() *FeatureVector {
|
|||||||
return NewFeatureVector(features)
|
return NewFeatureVector(features)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SharedFeatures is a product of comparison of two features vector
|
// SharedFeatures is a product of comparison of two features vector which
|
||||||
// which consist of features which are present in both local and remote
|
// consist of features which are present in both local and remote features
|
||||||
// features vectors.
|
// vectors.
|
||||||
type SharedFeatures struct {
|
type SharedFeatures struct {
|
||||||
*FeatureVector
|
*FeatureVector
|
||||||
}
|
}
|
||||||
@ -276,8 +276,8 @@ func newSharedFeatures(f *FeatureVector) *SharedFeatures {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsActive checks is feature active or not, it might be disabled during
|
// IsActive checks is feature active or not, it might be disabled during
|
||||||
// comparision with remote feature vector if it was optional and
|
// comparision with remote feature vector if it was optional and remote peer
|
||||||
// remote peer doesn't support it.
|
// doesn't support it.
|
||||||
func (f *SharedFeatures) IsActive(name featureName) bool {
|
func (f *SharedFeatures) IsActive(name featureName) bool {
|
||||||
index, ok := f.featuresMap[name]
|
index, ok := f.featuresMap[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
Loading…
Reference in New Issue
Block a user