Skip to content

Commit

Permalink
wire: Export (read|write)(VarInt|VarBytes).
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouke Hofman authored and Jouke Hofman committed Feb 22, 2016
1 parent f4d551c commit c17ff82
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 91 deletions.
16 changes: 8 additions & 8 deletions wire/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,31 @@ var blockOne = MsgBlock{
// a single byte variable length integer.
func BenchmarkWriteVarInt1(b *testing.B) {
for i := 0; i < b.N; i++ {
writeVarInt(ioutil.Discard, 0, 1)
WriteVarInt(ioutil.Discard, 0, 1)
}
}

// BenchmarkWriteVarInt3 performs a benchmark on how long it takes to write
// a three byte variable length integer.
func BenchmarkWriteVarInt3(b *testing.B) {
for i := 0; i < b.N; i++ {
writeVarInt(ioutil.Discard, 0, 65535)
WriteVarInt(ioutil.Discard, 0, 65535)
}
}

// BenchmarkWriteVarInt5 performs a benchmark on how long it takes to write
// a five byte variable length integer.
func BenchmarkWriteVarInt5(b *testing.B) {
for i := 0; i < b.N; i++ {
writeVarInt(ioutil.Discard, 0, 4294967295)
WriteVarInt(ioutil.Discard, 0, 4294967295)
}
}

// BenchmarkWriteVarInt9 performs a benchmark on how long it takes to write
// a nine byte variable length integer.
func BenchmarkWriteVarInt9(b *testing.B) {
for i := 0; i < b.N; i++ {
writeVarInt(ioutil.Discard, 0, 18446744073709551615)
WriteVarInt(ioutil.Discard, 0, 18446744073709551615)
}
}

Expand All @@ -151,7 +151,7 @@ func BenchmarkWriteVarInt9(b *testing.B) {
func BenchmarkReadVarInt1(b *testing.B) {
buf := []byte{0x01}
for i := 0; i < b.N; i++ {
readVarInt(bytes.NewReader(buf), 0)
ReadVarInt(bytes.NewReader(buf), 0)
}
}

Expand All @@ -160,7 +160,7 @@ func BenchmarkReadVarInt1(b *testing.B) {
func BenchmarkReadVarInt3(b *testing.B) {
buf := []byte{0x0fd, 0xff, 0xff}
for i := 0; i < b.N; i++ {
readVarInt(bytes.NewReader(buf), 0)
ReadVarInt(bytes.NewReader(buf), 0)
}
}

Expand All @@ -169,7 +169,7 @@ func BenchmarkReadVarInt3(b *testing.B) {
func BenchmarkReadVarInt5(b *testing.B) {
buf := []byte{0xfe, 0xff, 0xff, 0xff, 0xff}
for i := 0; i < b.N; i++ {
readVarInt(bytes.NewReader(buf), 0)
ReadVarInt(bytes.NewReader(buf), 0)
}
}

Expand All @@ -178,7 +178,7 @@ func BenchmarkReadVarInt5(b *testing.B) {
func BenchmarkReadVarInt9(b *testing.B) {
buf := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
for i := 0; i < b.N; i++ {
readVarInt(bytes.NewReader(buf), 0)
ReadVarInt(bytes.NewReader(buf), 0)
}
}

Expand Down
32 changes: 16 additions & 16 deletions wire/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ func writeElements(w io.Writer, elements ...interface{}) error {
return nil
}

// readVarInt reads a variable length integer from r and returns it as a uint64.
func readVarInt(r io.Reader, pver uint32) (uint64, error) {
// ReadVarInt reads a variable length integer from r and returns it as a uint64.
func ReadVarInt(r io.Reader, pver uint32) (uint64, error) {
var b [8]byte
_, err := io.ReadFull(r, b[0:1])
if err != nil {
Expand All @@ -345,7 +345,7 @@ func readVarInt(r io.Reader, pver uint32) (uint64, error) {
// encoded using fewer bytes.
min := uint64(0x100000000)
if rv < min {
return 0, messageError("readVarInt", fmt.Sprintf(
return 0, messageError("ReadVarInt", fmt.Sprintf(
errNonCanonicalVarInt, rv, discriminant, min))
}

Expand All @@ -360,7 +360,7 @@ func readVarInt(r io.Reader, pver uint32) (uint64, error) {
// encoded using fewer bytes.
min := uint64(0x10000)
if rv < min {
return 0, messageError("readVarInt", fmt.Sprintf(
return 0, messageError("ReadVarInt", fmt.Sprintf(
errNonCanonicalVarInt, rv, discriminant, min))
}

Expand All @@ -375,7 +375,7 @@ func readVarInt(r io.Reader, pver uint32) (uint64, error) {
// encoded using fewer bytes.
min := uint64(0xfd)
if rv < min {
return 0, messageError("readVarInt", fmt.Sprintf(
return 0, messageError("ReadVarInt", fmt.Sprintf(
errNonCanonicalVarInt, rv, discriminant, min))
}

Expand All @@ -386,9 +386,9 @@ func readVarInt(r io.Reader, pver uint32) (uint64, error) {
return rv, nil
}

// writeVarInt serializes val to w using a variable number of bytes depending
// WriteVarInt serializes val to w using a variable number of bytes depending
// on its value.
func writeVarInt(w io.Writer, pver uint32, val uint64) error {
func WriteVarInt(w io.Writer, pver uint32, val uint64) error {
if val < 0xfd {
_, err := w.Write([]byte{uint8(val)})
return err
Expand Down Expand Up @@ -447,7 +447,7 @@ func VarIntSerializeSize(val uint64) int {
// maximum block payload size since it helps protect against memory exhaustion
// attacks and forced panics through malformed messages.
func ReadVarString(r io.Reader, pver uint32) (string, error) {
count, err := readVarInt(r, pver)
count, err := ReadVarInt(r, pver)
if err != nil {
return "", err
}
Expand All @@ -473,7 +473,7 @@ func ReadVarString(r io.Reader, pver uint32) (string, error) {
// the length of the string followed by the bytes that represent the string
// itself.
func WriteVarString(w io.Writer, pver uint32, str string) error {
err := writeVarInt(w, pver, uint64(len(str)))
err := WriteVarInt(w, pver, uint64(len(str)))
if err != nil {
return err
}
Expand All @@ -484,17 +484,17 @@ func WriteVarString(w io.Writer, pver uint32, str string) error {
return nil
}

// readVarBytes reads a variable length byte array. A byte array is encoded
// ReadVarBytes reads a variable length byte array. A byte array is encoded
// as a varInt containing the length of the array followed by the bytes
// themselves. An error is returned if the length is greater than the
// passed maxAllowed parameter which helps protect against memory exhuastion
// attacks and forced panics thorugh malformed messages. The fieldName
// parameter is only used for the error message so it provides more context in
// the error.
func readVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
fieldName string) ([]byte, error) {

count, err := readVarInt(r, pver)
count, err := ReadVarInt(r, pver)
if err != nil {
return nil, err
}
Expand All @@ -505,7 +505,7 @@ func readVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
if count > uint64(maxAllowed) {
str := fmt.Sprintf("%s is larger than the max allowed size "+
"[count %d, max %d]", fieldName, count, maxAllowed)
return nil, messageError("readVarBytes", str)
return nil, messageError("ReadVarBytes", str)
}

b := make([]byte, count)
Expand All @@ -516,11 +516,11 @@ func readVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
return b, nil
}

// writeVarInt serializes a variable length byte array to w as a varInt
// WriteVarBytes serializes a variable length byte array to w as a varInt
// containing the number of bytes, followed by the bytes themselves.
func writeVarBytes(w io.Writer, pver uint32, bytes []byte) error {
func WriteVarBytes(w io.Writer, pver uint32, bytes []byte) error {
slen := uint64(len(bytes))
err := writeVarInt(w, pver, slen)
err := WriteVarInt(w, pver, slen)
if err != nil {
return err
}
Expand Down
30 changes: 15 additions & 15 deletions wire/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,11 @@ func TestVarIntWire(t *testing.T) {
var buf bytes.Buffer
err := wire.TstWriteVarInt(&buf, test.pver, test.in)
if err != nil {
t.Errorf("writeVarInt #%d error %v", i, err)
t.Errorf("WriteVarInt #%d error %v", i, err)
continue
}
if !bytes.Equal(buf.Bytes(), test.buf) {
t.Errorf("writeVarInt #%d\n got: %s want: %s", i,
t.Errorf("WriteVarInt #%d\n got: %s want: %s", i,
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
continue
}
Expand All @@ -298,11 +298,11 @@ func TestVarIntWire(t *testing.T) {
rbuf := bytes.NewReader(test.buf)
val, err := wire.TstReadVarInt(rbuf, test.pver)
if err != nil {
t.Errorf("readVarInt #%d error %v", i, err)
t.Errorf("ReadVarInt #%d error %v", i, err)
continue
}
if val != test.out {
t.Errorf("readVarInt #%d\n got: %d want: %d", i,
t.Errorf("ReadVarInt #%d\n got: %d want: %d", i,
val, test.out)
continue
}
Expand Down Expand Up @@ -338,7 +338,7 @@ func TestVarIntWireErrors(t *testing.T) {
w := newFixedWriter(test.max)
err := wire.TstWriteVarInt(w, test.pver, test.in)
if err != test.writeErr {
t.Errorf("writeVarInt #%d wrong error got: %v, want: %v",
t.Errorf("WriteVarInt #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
continue
}
Expand All @@ -347,7 +347,7 @@ func TestVarIntWireErrors(t *testing.T) {
r := newFixedReader(test.max, test.buf)
_, err = wire.TstReadVarInt(r, test.pver)
if err != test.readErr {
t.Errorf("readVarInt #%d wrong error got: %v, want: %v",
t.Errorf("ReadVarInt #%d wrong error got: %v, want: %v",
i, err, test.readErr)
continue
}
Expand Down Expand Up @@ -398,12 +398,12 @@ func TestVarIntNonCanonical(t *testing.T) {
rbuf := bytes.NewReader(test.in)
val, err := wire.TstReadVarInt(rbuf, test.pver)
if _, ok := err.(*wire.MessageError); !ok {
t.Errorf("readVarInt #%d (%s) unexpected error %v", i,
t.Errorf("ReadVarInt #%d (%s) unexpected error %v", i,
test.name, err)
continue
}
if val != 0 {
t.Errorf("readVarInt #%d (%s)\n got: %d want: 0", i,
t.Errorf("ReadVarInt #%d (%s)\n got: %d want: 0", i,
test.name, val)
continue
}
Expand Down Expand Up @@ -603,11 +603,11 @@ func TestVarBytesWire(t *testing.T) {
var buf bytes.Buffer
err := wire.TstWriteVarBytes(&buf, test.pver, test.in)
if err != nil {
t.Errorf("writeVarBytes #%d error %v", i, err)
t.Errorf("WriteVarBytes #%d error %v", i, err)
continue
}
if !bytes.Equal(buf.Bytes(), test.buf) {
t.Errorf("writeVarBytes #%d\n got: %s want: %s", i,
t.Errorf("WriteVarBytes #%d\n got: %s want: %s", i,
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
continue
}
Expand All @@ -617,11 +617,11 @@ func TestVarBytesWire(t *testing.T) {
val, err := wire.TstReadVarBytes(rbuf, test.pver,
wire.MaxMessagePayload, "test payload")
if err != nil {
t.Errorf("readVarBytes #%d error %v", i, err)
t.Errorf("ReadVarBytes #%d error %v", i, err)
continue
}
if !bytes.Equal(buf.Bytes(), test.buf) {
t.Errorf("readVarBytes #%d\n got: %s want: %s", i,
t.Errorf("ReadVarBytes #%d\n got: %s want: %s", i,
val, test.buf)
continue
}
Expand Down Expand Up @@ -659,7 +659,7 @@ func TestVarBytesWireErrors(t *testing.T) {
w := newFixedWriter(test.max)
err := wire.TstWriteVarBytes(w, test.pver, test.in)
if err != test.writeErr {
t.Errorf("writeVarBytes #%d wrong error got: %v, want: %v",
t.Errorf("WriteVarBytes #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
continue
}
Expand All @@ -669,7 +669,7 @@ func TestVarBytesWireErrors(t *testing.T) {
_, err = wire.TstReadVarBytes(r, test.pver,
wire.MaxMessagePayload, "test payload")
if err != test.readErr {
t.Errorf("readVarBytes #%d wrong error got: %v, want: %v",
t.Errorf("ReadVarBytes #%d wrong error got: %v, want: %v",
i, err, test.readErr)
continue
}
Expand Down Expand Up @@ -701,7 +701,7 @@ func TestVarBytesOverflowErrors(t *testing.T) {
_, err := wire.TstReadVarBytes(rbuf, test.pver,
wire.MaxMessagePayload, "test payload")
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
t.Errorf("readVarBytes #%d wrong error got: %v, "+
t.Errorf("ReadVarBytes #%d wrong error got: %v, "+
"want: %v", i, err, reflect.TypeOf(test.err))
continue
}
Expand Down
16 changes: 8 additions & 8 deletions wire/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,28 @@ func TstWriteElement(w io.Writer, element interface{}) error {
return writeElement(w, element)
}

// TstReadVarInt makes the internal readVarInt function available to the
// TstReadVarInt makes the internal ReadVarInt function available to the
// test package.
func TstReadVarInt(r io.Reader, pver uint32) (uint64, error) {
return readVarInt(r, pver)
return ReadVarInt(r, pver)
}

// TstWriteVarInt makes the internal writeVarInt function available to the
// TstWriteVarInt makes the internal WriteVarInt function available to the
// test package.
func TstWriteVarInt(w io.Writer, pver uint32, val uint64) error {
return writeVarInt(w, pver, val)
return WriteVarInt(w, pver, val)
}

// TstReadVarBytes makes the internal readVarBytes function available to the
// TstReadVarBytes makes the internal ReadVarBytes function available to the
// test package.
func TstReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32, fieldName string) ([]byte, error) {
return readVarBytes(r, pver, maxAllowed, fieldName)
return ReadVarBytes(r, pver, maxAllowed, fieldName)
}

// TstWriteVarBytes makes the internal writeVarBytes function available to the
// TstWriteVarBytes makes the internal WriteVarBytes function available to the
// test package.
func TstWriteVarBytes(w io.Writer, pver uint32, bytes []byte) error {
return writeVarBytes(w, pver, bytes)
return WriteVarBytes(w, pver, bytes)
}

// TstReadNetAddress makes the internal readNetAddress function available to
Expand Down
4 changes: 2 additions & 2 deletions wire/msgaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (msg *MsgAddr) ClearAddresses() {
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgAddr) BtcDecode(r io.Reader, pver uint32) error {
count, err := readVarInt(r, pver)
count, err := ReadVarInt(r, pver)
if err != nil {
return err
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (msg *MsgAddr) BtcEncode(w io.Writer, pver uint32) error {
return messageError("MsgAddr.BtcEncode", str)
}

err := writeVarInt(w, pver, uint64(count))
err := WriteVarInt(w, pver, uint64(count))
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit c17ff82

Please sign in to comment.