lntypes: return a value from constructors

Returning pointers proved inconvenient in almost all cases. This commmit
converts the constructors to returning values.
This commit is contained in:
Joost Jager 2019-02-09 20:39:43 +01:00
parent 449c3d533e
commit c4263e7061
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
2 changed files with 13 additions and 13 deletions

@ -163,12 +163,12 @@ func (s *Server) RegisterWithRootServer(grpcServer *grpc.Server) error {
func (s *Server) SubscribeSingleInvoice(req *lnrpc.PaymentHash, func (s *Server) SubscribeSingleInvoice(req *lnrpc.PaymentHash,
updateStream Invoices_SubscribeSingleInvoiceServer) error { updateStream Invoices_SubscribeSingleInvoiceServer) error {
hash, err := lntypes.NewHash(req.RHash) hash, err := lntypes.MakeHash(req.RHash)
if err != nil { if err != nil {
return err return err
} }
invoiceClient := s.cfg.InvoiceRegistry.SubscribeSingleInvoice(*hash) invoiceClient := s.cfg.InvoiceRegistry.SubscribeSingleInvoice(hash)
defer invoiceClient.Cancel() defer invoiceClient.Cancel()
for { for {
@ -197,12 +197,12 @@ func (s *Server) SubscribeSingleInvoice(req *lnrpc.PaymentHash,
func (s *Server) CancelInvoice(ctx context.Context, func (s *Server) CancelInvoice(ctx context.Context,
in *CancelInvoiceMsg) (*CancelInvoiceResp, error) { in *CancelInvoiceMsg) (*CancelInvoiceResp, error) {
paymentHash, err := lntypes.NewHash(in.PaymentHash) paymentHash, err := lntypes.MakeHash(in.PaymentHash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = s.cfg.InvoiceRegistry.CancelInvoice(*paymentHash) err = s.cfg.InvoiceRegistry.CancelInvoice(paymentHash)
if err != nil { if err != nil {
return nil, err return nil, err
} }

@ -17,33 +17,33 @@ func (hash Hash) String() string {
return hex.EncodeToString(hash[:]) return hex.EncodeToString(hash[:])
} }
// NewHash returns a new Hash from a byte slice. An error is returned if // MakeHash returns a new Hash from a byte slice. An error is returned if
// the number of bytes passed in is not HashSize. // the number of bytes passed in is not HashSize.
func NewHash(newHash []byte) (*Hash, error) { func MakeHash(newHash []byte) (Hash, error) {
nhlen := len(newHash) nhlen := len(newHash)
if nhlen != HashSize { if nhlen != HashSize {
return nil, fmt.Errorf("invalid hash length of %v, want %v", return Hash{}, fmt.Errorf("invalid hash length of %v, want %v",
nhlen, HashSize) nhlen, HashSize)
} }
var hash Hash var hash Hash
copy(hash[:], newHash) copy(hash[:], newHash)
return &hash, nil return hash, nil
} }
// NewHashFromStr creates a Hash from a hex hash string. // MakeHashFromStr creates a Hash from a hex hash string.
func NewHashFromStr(newHash string) (*Hash, error) { func MakeHashFromStr(newHash string) (Hash, error) {
// Return error if hash string is of incorrect length. // Return error if hash string is of incorrect length.
if len(newHash) != HashSize*2 { if len(newHash) != HashSize*2 {
return nil, fmt.Errorf("invalid hash string length of %v, "+ return Hash{}, fmt.Errorf("invalid hash string length of %v, "+
"want %v", len(newHash), HashSize*2) "want %v", len(newHash), HashSize*2)
} }
hash, err := hex.DecodeString(newHash) hash, err := hex.DecodeString(newHash)
if err != nil { if err != nil {
return nil, err return Hash{}, err
} }
return NewHash(hash) return MakeHash(hash)
} }