lnd.xprv/htlcswitch/iterator.go
Andrey Samokhvalov de01721aed htlcswitch: add hop iterator
Short: such abstraction give as ability to test the channel link in the
future.

Long: hop iterator represents the entity which is be able to give payment
route hop by hop. This interface will be used to have an abstraction
over the algorithm which we use to determine the next hope in htlc route
and also helps the unit test to create mock representation of such
algorithm which uses simple array of hops.
2017-05-31 11:06:08 -07:00

44 lines
1.2 KiB
Go

package htlcswitch
import (
"bytes"
"encoding/hex"
"io"
"github.com/btcsuite/golangcrypto/ripemd160"
"github.com/roasbeef/btcutil"
)
// HopID represents the id which is used by propagation subsystem in order to
// identify lightning network node.
// TODO(andrew.shvv) remove after switching to the using channel id.
type HopID [ripemd160.Size]byte
// NewHopID creates new instance of hop form node public key.
func NewHopID(pubKey []byte) HopID {
var routeID HopID
copy(routeID[:], btcutil.Hash160(pubKey))
return routeID
}
// String returns string representation of hop id.
func (h HopID) String() string {
return hex.EncodeToString(h[:])
}
// IsEqual checks does the two hop ids are equal.
func (h HopID) IsEqual(h2 HopID) bool {
return bytes.Equal(h[:], h2[:])
}
// HopIterator interface represent the entity which is able to give route
// hops one by one. This interface is used to have an abstraction over the
// algorithm which we use to determine the next hope in htlc route.
type HopIterator interface {
// Next returns next hop if exist and nil if route is ended.
Next() *HopID
// Encode encodes iterator and writes it to the writer.
Encode(w io.Writer) error
}