mit-dci / lit

Lightning Network node software
MIT License
551 stars 119 forks source link

Structure IO is messy and prone to errors due to using magic offsets #294

Closed delbonis closed 5 years ago

delbonis commented 6 years ago

A lot of structures are (de)serialized via offsets like this:

copy(q.TheirPub[:], b[:33])
copy(q.TheirRefundPub[:], b[33:66])
copy(q.TheirHAKDBase[:], b[66:99])

tsbuf := make([]byte, 8)
copy(tsbuf, b[99:107])
q.LastUpdate = binary.BigEndian.Uint64(tsbuf)

u, err := portxo.PorTxoFromBytes(b[107:])
if err != nil {
    return q, err
}

It would be much better to use a Reader and Writer that lets you read a set number of bytes at a time either from a buffer or from a fd instead of blindly copying a buffer of some size and then taking chunks of that into memory.

Example the BinaryComponent system from Jiyunet where subtypes (like outpoints in our case) have there IO routines called out to by their owners:

delbonis commented 5 years ago

Partially addressed in #379, still some problems though, but important parts have been dealt with.