2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-13 13:10:30 +00:00
loop/loopdb/codec.go
Andras Banki-Horvath 86db43a2cb loopdb: store protocol version alongside with swaps
This commit adds the protocol version to each stored swap. This will be
used to ensure that when swaps are resumed after a restart, they're
correctly handled given any breaking protocol changes.
2020-09-09 19:54:01 +02:00

43 lines
1.0 KiB
Go

package loopdb
import (
"fmt"
)
// itob returns an 8-byte big endian representation of v.
func itob(v uint64) []byte {
b := make([]byte, 8)
byteOrder.PutUint64(b, v)
return b
}
// UnmarshalProtocolVersion attempts to unmarshal a byte slice to a
// ProtocolVersion value. If the unmarshal fails, ProtocolVersionUnrecorded is
// returned along with an error.
func UnmarshalProtocolVersion(b []byte) (ProtocolVersion, error) {
if b == nil {
return ProtocolVersionUnrecorded, nil
}
if len(b) != 4 {
return ProtocolVersionUnrecorded,
fmt.Errorf("invalid size: %v", len(b))
}
version := ProtocolVersion(byteOrder.Uint32(b))
if !version.Valid() {
return ProtocolVersionUnrecorded,
fmt.Errorf("invalid protocol version: %v", version)
}
return version, nil
}
// MarshalProtocolVersion marshals a ProtocolVersion value to a byte slice.
func MarshalProtocolVersion(version ProtocolVersion) []byte {
var versionBytes [4]byte
byteOrder.PutUint32(versionBytes[:], uint32(version))
return versionBytes[:]
}