lnd.xprv/lnwire/htlc_addrequest.go
Joseph Poon f3849f5c10 Structs for Wire Protocol HTLCs and Commitments
* Structs and wire messages for HTLCs
* Wire protocol for a state machine with no blocking(!!!)
  (I will write the state machine)
  TL;DR: Can do multiple HTLC modifications in-flight, dead simple wire
  protocol. Both sides can update their Commitments unliaterally without
  waiting for the other party's signature. Will have basic/preliminary
  notes in the README
* Added **swp to .gitignore because of vim annoyances
2016-01-14 23:56:10 -08:00

130 lines
2.9 KiB
Go

package lnwire
import (
"fmt"
"io"
)
//Multiple Clearing Requests are possible by putting this inside an array of
//clearing requests
type HTLCAddRequest struct {
//We can use a different data type for this if necessary...
ChannelID uint64
//ID of this request
StagingID uint64
//When the HTLC expires
Expiry uint32
//Amount to pay in the hop
//Difference between hop and first item in blob is the fee to complete
Amount CreditsAmount
//Hash160 address of the next hop.
NextHop [20]byte
//Contract Type
//first 4 bits is n, second for is m, in n-of-m "multisig"
ContractType uint8
//Redemption Hashes
RedemptionHashes []*[20]byte
//Data to parse&pass on to the next node
//Eventually, we need to make this into a group of 2 nested structs?
Blob []byte
}
func (c *HTLCAddRequest) Decode(r io.Reader, pver uint32) error {
//ChannelID(8)
//StagingID(8)
//Expiry(4)
//Amount(4)
//NextHop(20)
//ContractType(1)
//RedemptionHashes (numOfHashes * 20 + numOfHashes)
//Blob(2+blobsize)
err := readElements(r,
&c.ChannelID,
&c.StagingID,
&c.Expiry,
&c.Amount,
&c.NextHop,
&c.ContractType,
&c.RedemptionHashes,
&c.Blob,
)
if err != nil {
return err
}
return nil
}
//Creates a new HTLCAddRequest
func NewHTLCAddRequest() *HTLCAddRequest {
return &HTLCAddRequest{}
}
//Serializes the item from the HTLCAddRequest struct
//Writes the data to w
func (c *HTLCAddRequest) Encode(w io.Writer, pver uint32) error {
err := writeElements(w,
c.ChannelID,
c.StagingID,
c.Expiry,
c.Amount,
c.NextHop,
c.ContractType,
c.RedemptionHashes,
c.Blob,
)
if err != nil {
return err
}
return nil
}
func (c *HTLCAddRequest) Command() uint32 {
return CmdHTLCAddRequest
}
func (c *HTLCAddRequest) MaxPayloadLength(uint32) uint32 {
//base size ~110, but blob can be variable.
//shouldn't be bigger than 8K though...
return 8192
}
//Makes sure the struct data is valid (e.g. no negatives or invalid pkscripts)
func (c *HTLCAddRequest) Validate() error {
if c.Amount < 0 {
//While fees can be negative, it's too confusing to allow
//negative payments. Maybe for some wallets, but not this one!
return fmt.Errorf("Amount paid cannot be negative.")
}
//We're good!
return nil
}
func (c *HTLCAddRequest) String() string {
var redemptionHashes string
for i, rh := range c.RedemptionHashes {
redemptionHashes += fmt.Sprintf("\n\tSlice\t%d\n", i)
redemptionHashes += fmt.Sprintf("\t\tRedemption Hash: %x\n", *rh)
}
return fmt.Sprintf("\n--- Begin HTLCAddRequest ---\n") +
fmt.Sprintf("ChannelID:\t%d\n", c.ChannelID) +
fmt.Sprintf("StagingID:\t%d\n", c.StagingID) +
fmt.Sprintf("Expiry:\t\t%d\n", c.Expiry) +
fmt.Sprintf("Amount\t\t%d\n", c.Amount) +
fmt.Sprintf("NextHop\t\t%x\n", c.NextHop) +
fmt.Sprintf("ContractType:\t%d (%b)\n", c.ContractType, c.ContractType) +
fmt.Sprintf("RedemptionHashes:") +
redemptionHashes +
fmt.Sprintf("Blob:\t\t\t\t%x\n", c.Blob) +
fmt.Sprintf("--- End HTLCAddRequest ---\n")
}