From 2953f3532a606ded97ea15ccd7cce8e1137565c4 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Mon, 26 Aug 2019 12:54:04 -0700 Subject: [PATCH] lnwire/features: add SerializeSize32 for base32 encodings --- lnwire/features.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lnwire/features.go b/lnwire/features.go index 4cdbd054..875ef401 100644 --- a/lnwire/features.go +++ b/lnwire/features.go @@ -127,6 +127,20 @@ func (fv *RawFeatureVector) Unset(feature FeatureBit) { // SerializeSize returns the number of bytes needed to represent feature vector // in byte format. func (fv *RawFeatureVector) SerializeSize() int { + // We calculate byte-length via the largest bit index. + return fv.serializeSize(8) +} + +// SerializeSize32 returns the number of bytes needed to represent feature +// vector in base32 format. +func (fv *RawFeatureVector) SerializeSize32() int { + // We calculate base32-length via the largest bit index. + return fv.serializeSize(5) +} + +// serializeSize returns the number of bytes required to encode the feature +// vector using at most width bits per encoded byte. +func (fv *RawFeatureVector) serializeSize(width int) int { // Find the largest feature bit index max := -1 for feature := range fv.features { @@ -139,8 +153,7 @@ func (fv *RawFeatureVector) SerializeSize() int { return 0 } - // We calculate byte-length via the largest bit index - return max/8 + 1 + return max/width + 1 } // Encode writes the feature vector in byte representation. Every feature