RichΛrd 0babdad17b
chore: upgrade go-waku to v0.5 (#3213)
* chore: upgrade go-waku to v0.5
* chore: add println and logs to check what's being stored in the enr, and preemptively delete the multiaddr field (#3219)
* feat: add wakuv2 test (#3218)
2023-02-22 17:58:17 -04:00

35 lines
637 B
Go

package msgio
import (
"encoding/binary"
"io"
)
// NBO is NetworkByteOrder
var NBO = binary.BigEndian
// WriteLen writes a length to the given writer.
func WriteLen(w io.Writer, l int) error {
ul := uint32(l)
return binary.Write(w, NBO, &ul)
}
// ReadLen reads a length from the given reader.
// if buf is non-nil, it reuses the buffer. Ex:
//
// l, err := ReadLen(r, nil)
// _, err := ReadLen(r, buf)
func ReadLen(r io.Reader, buf []byte) (int, error) {
if len(buf) < 4 {
buf = make([]byte, 4)
}
buf = buf[:4]
if _, err := io.ReadFull(r, buf); err != nil {
return 0, err
}
n := int(NBO.Uint32(buf))
return n, nil
}