lnwire: modify Alias to only track the non-zero portion of wire msg
This commit modifies the Alias type to only hold the non-zero portion of the alias as encoded on the wire. Previously the entire 32-bytes would be read and stored, including the zeroes at the end used as padding. Within the constructor, we now parse the alias properly, discarding the trailing zeroes within the passed byte slice. Additionally, the .String() method of Alias will now also only print the non-zero prefix of the decoded alias.
This commit is contained in:
parent
16388aba00
commit
e5490b55f4
@ -6,12 +6,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
"github.com/roasbeef/btcd/btcec"
|
"github.com/roasbeef/btcd/btcec"
|
||||||
"github.com/roasbeef/btcd/txscript"
|
"github.com/roasbeef/btcd/txscript"
|
||||||
"github.com/roasbeef/btcd/wire"
|
"github.com/roasbeef/btcd/wire"
|
||||||
"github.com/roasbeef/btcutil"
|
"github.com/roasbeef/btcutil"
|
||||||
"net"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxSliceLength is the maximum allowed lenth for any opaque byte slices in
|
// MaxSliceLength is the maximum allowed lenth for any opaque byte slices in
|
||||||
@ -379,7 +380,7 @@ func writeElement(w io.Writer, element interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case Alias:
|
case Alias:
|
||||||
if err := writeElements(w, ([32]byte)(e)); err != nil {
|
if err := writeElements(w, ([32]byte)(e.data)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -720,8 +721,11 @@ func readElement(r io.Reader, element interface{}) error {
|
|||||||
if err := readElements(r, &a); err != nil {
|
if err := readElements(r, &a); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*e = (Alias)(a)
|
|
||||||
|
|
||||||
|
*e, err = newAlias(a[:])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Unknown type in readElement: %T", e)
|
return fmt.Errorf("Unknown type in readElement: %T", e)
|
||||||
}
|
}
|
||||||
|
@ -24,39 +24,51 @@ type RGB struct {
|
|||||||
blue uint8
|
blue uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alias a hex encoded UTF-8 string that may be displayed
|
// Alias a hex encoded UTF-8 string that may be displayed as an alternative to
|
||||||
// as an alternative to the node's ID. Notice that aliases are not
|
// the node's ID. Notice that aliases are not unique and may be freely chosen
|
||||||
// unique and may be freely chosen by the node operators.
|
// by the node operators.
|
||||||
type Alias [32]byte
|
type Alias struct {
|
||||||
|
data [32]byte
|
||||||
|
aliasLen int
|
||||||
|
}
|
||||||
|
|
||||||
// NewAlias create the alias from string and also checks spec requirements.
|
// NewAlias create the alias from string and also checks spec requirements.
|
||||||
func NewAlias(s string) (Alias, error) {
|
func NewAlias(s string) (Alias, error) {
|
||||||
var a Alias
|
|
||||||
|
|
||||||
data := []byte(s)
|
data := []byte(s)
|
||||||
if len(data) > aliasSpecLen {
|
return newAlias(data)
|
||||||
return a, errors.Errorf("alias too long the size "+
|
}
|
||||||
"must be less than %v", aliasSpecLen)
|
|
||||||
|
func newAlias(data []byte) (Alias, error) {
|
||||||
|
var a [32]byte
|
||||||
|
|
||||||
|
aliasEnd := len(data)
|
||||||
|
for data[aliasEnd-1] == 0 && aliasEnd > 0 {
|
||||||
|
aliasEnd--
|
||||||
}
|
}
|
||||||
|
|
||||||
copy(a[:], data)
|
copy(a[:aliasEnd], data[:aliasEnd])
|
||||||
return a, nil
|
|
||||||
|
return Alias{
|
||||||
|
data: a,
|
||||||
|
aliasLen: aliasEnd,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Alias) String() string {
|
func (a *Alias) String() string {
|
||||||
return string(a[:])
|
return string(a.data[:a.aliasLen])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate check that alias data lenght is lower than spec size.
|
// Validate check that alias data length is lower than spec size.
|
||||||
func (a *Alias) Validate() error {
|
func (a *Alias) Validate() error {
|
||||||
nonzero := len(a)
|
nonzero := len(a.data)
|
||||||
for a[nonzero-1] == 0 && nonzero > 0 {
|
for a.data[nonzero-1] == 0 && nonzero > 0 {
|
||||||
nonzero--
|
nonzero--
|
||||||
}
|
}
|
||||||
|
|
||||||
if nonzero > aliasSpecLen {
|
if nonzero > aliasSpecLen {
|
||||||
return errors.New("alias should be less then 21 bytes")
|
return errors.New("alias should be less then 21 bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user