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
This commit is contained in:
Joseph Poon authored and Olaoluwa Osuntokun committed 2016-01-14 23:56:10 -08:00
1 parent 58f0bfe252
commit f3849f5c10
23 files changed
+1286 -5

No files matched your search

+128 -5
View File
@@ -11,11 +11,25 @@ import (
"io/ioutil"
)
var MAX_SLICE_LENGTH = 65535
//Actual pkScript, not redeemScript
type PkScript []byte
//Subsatoshi amount
type MicroSatoshi int32
//Subsatoshi amount (Micro-Satoshi, 1/1000th)
//Should be a signed int to account for negative fees
//
//"In any science-fiction movie, anywhere in the galaxy, currency is referred
//to as 'credits.'"
// --Sam Humphries. Ebert, Roger (1999). Ebert's bigger little movie
// glossary. Andrews McMeel. p. 172.
//
//https://en.wikipedia.org/wiki/List_of_fictional_currencies
//https://en.wikipedia.org/wiki/Fictional_currency#Trends_in_the_use_of_fictional_currencies
//http://tvtropes.org/pmwiki/pmwiki.php/Main/WeWillSpendCreditsInTheFuture
type CreditsAmount int32 //Credits (XCB, accountants should use XCB :^)
//US Display format: 1 BTC = 100,000,000'000 XCB
//Or in BTC = 1.00000000'000
//Writes the big endian representation of element
//Unified function to call when writing different types
@@ -32,6 +46,20 @@ func writeElement(w io.Writer, element interface{}) error {
return err
}
return nil
case uint16:
var b [2]byte
binary.BigEndian.PutUint16(b[:], uint16(e))
_, err = w.Write(b[:])
if err != nil {
return err
}
return nil
case CreditsAmount:
err = binary.Write(w, binary.BigEndian, int32(e))
if err != nil {
return err
}
return nil
case uint32:
var b [4]byte
binary.BigEndian.PutUint32(b[:], uint32(e))
@@ -107,6 +135,26 @@ func writeElement(w io.Writer, element interface{}) error {
return err
}
return nil
case []*[20]byte:
//Get size of slice and dump in slice
sliceSize := len(e)
err = writeElement(w, uint16(sliceSize))
if err != nil {
return err
}
//Write in each sequentially
for _, element := range e {
err = writeElement(w, &element)
if err != nil {
return err
}
}
return nil
case **[20]byte:
_, err = w.Write((*e)[:])
if err != nil {
return err
}
case [20]byte:
_, err = w.Write(e[:])
if err != nil {
@@ -121,6 +169,22 @@ func writeElement(w io.Writer, element interface{}) error {
return err
}
return nil
case []byte:
sliceLength := len(e)
if sliceLength > MAX_SLICE_LENGTH {
return fmt.Errorf("Slice length too long!")
}
//Write the size
err = writeElement(w, uint16(sliceLength))
if err != nil {
return err
}
//Write the data
_, err = w.Write(e)
if err != nil {
return err
}
return nil
case PkScript:
scriptLength := len(e)
//Make sure it's P2PKH or P2SH size or less
@@ -202,6 +266,22 @@ func readElement(r io.Reader, element interface{}) error {
}
*e = b[0]
return nil
case *uint16:
var b [2]byte
_, err = io.ReadFull(r, b[:])
if err != nil {
return err
}
*e = binary.BigEndian.Uint16(b[:])
return nil
case *CreditsAmount:
var b [4]byte
_, err = io.ReadFull(r, b[:])
if err != nil {
return err
}
*e = CreditsAmount(int32(binary.BigEndian.Uint32(b[:])))
return nil
case *uint32:
var b [4]byte
_, err = io.ReadFull(r, b[:])
@@ -294,6 +374,25 @@ func readElement(r io.Reader, element interface{}) error {
}
*e = &*btcecSig
return nil
case *[]*[20]byte:
//How many to read
var sliceSize uint16
err = readElement(r, &sliceSize)
if err != nil {
return err
}
var data []*[20]byte
//Append the actual
for i := uint16(0); i < sliceSize; i++ {
var element [20]byte
err = readElement(r, &element)
if err != nil {
return err
}
data = append(data, &element)
}
*e = data
return nil
case *[20]byte:
_, err = io.ReadFull(r, e[:])
if err != nil {
@@ -308,6 +407,30 @@ func readElement(r io.Reader, element interface{}) error {
}
*e = wire.BitcoinNet(binary.BigEndian.Uint32(b[:]))
return nil
case *[]byte:
//Get the blob length first
var blobLength uint16
err = readElement(r, &blobLength)
if err != nil {
return err
}
//Shouldn't need to do this, since it's uint16, but we
//might have a different value for MAX_SLICE_LENGTH...
if int(blobLength) > MAX_SLICE_LENGTH {
return fmt.Errorf("Slice length too long!")
}
//Read the slice length
l := io.LimitReader(r, int64(blobLength))
*e, err = ioutil.ReadAll(l)
if err != nil {
return err
}
if len(*e) != int(blobLength) {
return fmt.Errorf("EOF: Slice length mismatch.")
}
return nil
case *PkScript:
//Get the script length first
var scriptLength uint8
@@ -323,12 +446,12 @@ func readElement(r io.Reader, element interface{}) error {
//Read the script length
l := io.LimitReader(r, int64(scriptLength))
*e, err = ioutil.ReadAll(l)
if len(*e) != int(scriptLength) {
return fmt.Errorf("EOF: Signature length mismatch.")
}
if err != nil {
return err
}
if len(*e) != int(scriptLength) {
return fmt.Errorf("EOF: Signature length mismatch.")
}
return nil
case *[]*wire.TxIn:
//Read the size (1-byte number of txins)