mirror of
https://github.com/btcsuite/btcd.git
synced 2024-11-19 01:40:07 +01:00
Import btcwire repo into wire directory.
This commit contains the entire btcwire repository along with several changes needed to move all of the files into the wire directory in order to prepare it for merging. This does NOT update btcd or any of the other packages to use the new location as that will be done separately. - All import paths in the old btcwire test files have been changed to the new location - All references to btcwire as the package name have been chagned to wire - The coveralls badge has been removed since it unfortunately doesn't support coverage of sub-packages This is ongoing work toward #214.
This commit is contained in:
parent
cb858fd107
commit
2eef3720a9
34
.gitignore
vendored
34
.gitignore
vendored
@ -1,34 +0,0 @@
|
||||
# Temp files
|
||||
*~
|
||||
btcwire.test
|
||||
|
||||
# Databases
|
||||
btcd.db
|
||||
*-shm
|
||||
*-wal
|
||||
|
||||
# Log files
|
||||
*.log
|
||||
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
18
.travis.yml
18
.travis.yml
@ -1,18 +0,0 @@
|
||||
language: go
|
||||
go:
|
||||
- release
|
||||
- tip
|
||||
sudo: false
|
||||
before_install:
|
||||
- gocleandeps=c16c849abae90c23419d
|
||||
- git clone https://gist.github.com/$gocleandeps.git
|
||||
- goclean=71d0380287747d956a26
|
||||
- git clone https://gist.github.com/$goclean.git
|
||||
install:
|
||||
- go get -d -t -v ./...
|
||||
- bash $gocleandeps/gocleandeps.sh
|
||||
script:
|
||||
- export PATH=$PATH:$HOME/gopath/bin
|
||||
- bash $goclean/goclean.sh
|
||||
after_success:
|
||||
- goveralls -coverprofile=profile.cov -service=travis-ci
|
13
LICENSE
13
LICENSE
@ -1,13 +0,0 @@
|
||||
Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
|
||||
Permission to use, copy, modify, and distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
@ -1,17 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This script uses gocov to generate a test coverage report.
|
||||
# The gocov tool my be obtained with the following command:
|
||||
# go get github.com/axw/gocov/gocov
|
||||
#
|
||||
# It will be installed to $GOPATH/bin, so ensure that location is in your $PATH.
|
||||
|
||||
# Check for gocov.
|
||||
type gocov >/dev/null 2>&1
|
||||
if [ $? -ne 0 ]; then
|
||||
echo >&2 "This script requires the gocov tool."
|
||||
echo >&2 "You may obtain it with the following command:"
|
||||
echo >&2 "go get github.com/axw/gocov/gocov"
|
||||
exit 1
|
||||
fi
|
||||
gocov test | gocov report
|
@ -1,42 +1,37 @@
|
||||
btcwire
|
||||
=======
|
||||
wire
|
||||
====
|
||||
|
||||
[![Build Status](http://img.shields.io/travis/btcsuite/btcwire.svg)]
|
||||
(https://travis-ci.org/btcsuite/btcwire) [![Coverage Status]
|
||||
(https://img.shields.io/coveralls/btcsuite/btcwire.svg)]
|
||||
(https://coveralls.io/r/btcsuite/btcwire?branch=master) [![ISC License]
|
||||
[![Build Status](http://img.shields.io/travis/btcsuite/btcd.svg)]
|
||||
(https://travis-ci.org/btcsuite/btcd) [![ISC License]
|
||||
(http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
|
||||
|
||||
Package btcwire implements the bitcoin wire protocol. A comprehensive suite of
|
||||
Package wire implements the bitcoin wire protocol. A comprehensive suite of
|
||||
tests with 100% test coverage is provided to ensure proper functionality.
|
||||
Package btcwire is licensed under the liberal ISC license.
|
||||
|
||||
There is an associated blog post about the release of this package
|
||||
[here](https://blog.conformal.com/btcwire-the-bitcoin-wire-protocol-package-from-btcd/).
|
||||
|
||||
This package is one of the core packages from btcd, an alternative full-node
|
||||
implementation of bitcoin which is under active development by Conformal.
|
||||
Although it was primarily written for btcd, this package has intentionally been
|
||||
designed so it can be used as a standalone package for any projects needing to
|
||||
interface with bitcoin peers at the wire protocol level.
|
||||
This package has intentionally been designed so it can be used as a standalone
|
||||
package for any projects needing to interface with bitcoin peers at the wire
|
||||
protocol level.
|
||||
|
||||
## Documentation
|
||||
|
||||
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)]
|
||||
(http://godoc.org/github.com/btcsuite/btcwire)
|
||||
(http://godoc.org/github.com/btcsuite/btcd/wire)
|
||||
|
||||
Full `go doc` style documentation for the project can be viewed online without
|
||||
installing this package by using the GoDoc site here:
|
||||
http://godoc.org/github.com/btcsuite/btcwire
|
||||
http://godoc.org/github.com/btcsuite/btcd/wire
|
||||
|
||||
You can also view the documentation locally once the package is installed with
|
||||
the `godoc` tool by running `godoc -http=":6060"` and pointing your browser to
|
||||
http://localhost:6060/pkg/github.com/btcsuite/btcwire
|
||||
http://localhost:6060/pkg/github.com/btcsuite/btcd/wire
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
$ go get github.com/btcsuite/btcwire
|
||||
$ go get github.com/btcsuite/btcd/wire
|
||||
```
|
||||
|
||||
## Bitcoin Message Overview
|
||||
@ -64,14 +59,14 @@ to a remote node running a bitcoin peer. Example syntax is:
|
||||
```Go
|
||||
// Use the most recent protocol version supported by the package and the
|
||||
// main bitcoin network.
|
||||
pver := btcwire.ProtocolVersion
|
||||
btcnet := btcwire.MainNet
|
||||
pver := wire.ProtocolVersion
|
||||
btcnet := wire.MainNet
|
||||
|
||||
// Reads and validates the next bitcoin message from conn using the
|
||||
// protocol version pver and the bitcoin network btcnet. The returns
|
||||
// are a btcwire.Message, a []byte which contains the unmarshalled
|
||||
// are a wire.Message, a []byte which contains the unmarshalled
|
||||
// raw payload, and a possible error.
|
||||
msg, rawPayload, err := btcwire.ReadMessage(conn, pver, btcnet)
|
||||
msg, rawPayload, err := wire.ReadMessage(conn, pver, btcnet)
|
||||
if err != nil {
|
||||
// Log and handle the error
|
||||
}
|
||||
@ -89,16 +84,16 @@ from a remote peer is:
|
||||
```Go
|
||||
// Use the most recent protocol version supported by the package and the
|
||||
// main bitcoin network.
|
||||
pver := btcwire.ProtocolVersion
|
||||
btcnet := btcwire.MainNet
|
||||
pver := wire.ProtocolVersion
|
||||
btcnet := wire.MainNet
|
||||
|
||||
// Create a new getaddr bitcoin message.
|
||||
msg := btcwire.NewMsgGetAddr()
|
||||
msg := wire.NewMsgGetAddr()
|
||||
|
||||
// Writes a bitcoin message msg to conn using the protocol version
|
||||
// pver, and the bitcoin network btcnet. The return is a possible
|
||||
// error.
|
||||
err := btcwire.WriteMessage(conn, msg, pver, btcnet)
|
||||
err := wire.WriteMessage(conn, msg, pver, btcnet)
|
||||
if err != nil {
|
||||
// Log and handle the error
|
||||
}
|
||||
@ -126,5 +121,5 @@ signature perform the following:
|
||||
|
||||
## License
|
||||
|
||||
Package btcwire is licensed under the [copyfree](http://copyfree.org) ISC
|
||||
Package wire is licensed under the [copyfree](http://copyfree.org) ISC
|
||||
License.
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,13 +10,13 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestBlockHeader tests the BlockHeader API.
|
||||
func TestBlockHeader(t *testing.T) {
|
||||
nonce64, err := btcwire.RandomUint64()
|
||||
nonce64, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("RandomUint64: Error generating nonce: %v", err)
|
||||
}
|
||||
@ -25,7 +25,7 @@ func TestBlockHeader(t *testing.T) {
|
||||
hash := mainNetGenesisHash
|
||||
merkleHash := mainNetGenesisMerkleRoot
|
||||
bits := uint32(0x1d00ffff)
|
||||
bh := btcwire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
|
||||
bh := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
|
||||
|
||||
// Ensure we get the same data back out.
|
||||
if !bh.PrevBlock.IsEqual(&hash) {
|
||||
@ -53,7 +53,7 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
|
||||
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
|
||||
bits := uint32(0x1d00ffff)
|
||||
baseBlockHdr := &btcwire.BlockHeader{
|
||||
baseBlockHdr := &wire.BlockHeader{
|
||||
Version: 1,
|
||||
PrevBlock: mainNetGenesisHash,
|
||||
MerkleRoot: mainNetGenesisMerkleRoot,
|
||||
@ -79,17 +79,17 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.BlockHeader // Data to encode
|
||||
out *btcwire.BlockHeader // Expected decoded data
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.BlockHeader // Data to encode
|
||||
out *wire.BlockHeader // Expected decoded data
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
baseBlockHdr,
|
||||
baseBlockHdr,
|
||||
baseBlockHdrEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version.
|
||||
@ -97,7 +97,7 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
baseBlockHdr,
|
||||
baseBlockHdr,
|
||||
baseBlockHdrEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version.
|
||||
@ -105,7 +105,7 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
baseBlockHdr,
|
||||
baseBlockHdr,
|
||||
baseBlockHdrEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion.
|
||||
@ -113,7 +113,7 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
baseBlockHdr,
|
||||
baseBlockHdr,
|
||||
baseBlockHdrEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion.
|
||||
@ -121,7 +121,7 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
baseBlockHdr,
|
||||
baseBlockHdr,
|
||||
baseBlockHdrEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -129,7 +129,7 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := btcwire.TstWriteBlockHeader(&buf, test.pver, test.in)
|
||||
err := wire.TstWriteBlockHeader(&buf, test.pver, test.in)
|
||||
if err != nil {
|
||||
t.Errorf("writeBlockHeader #%d error %v", i, err)
|
||||
continue
|
||||
@ -141,9 +141,9 @@ func TestBlockHeaderWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the block header from wire format.
|
||||
var bh btcwire.BlockHeader
|
||||
var bh wire.BlockHeader
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = btcwire.TstReadBlockHeader(rbuf, test.pver, &bh)
|
||||
err = wire.TstReadBlockHeader(rbuf, test.pver, &bh)
|
||||
if err != nil {
|
||||
t.Errorf("readBlockHeader #%d error %v", i, err)
|
||||
continue
|
||||
@ -162,7 +162,7 @@ func TestBlockHeaderSerialize(t *testing.T) {
|
||||
|
||||
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
|
||||
bits := uint32(0x1d00ffff)
|
||||
baseBlockHdr := &btcwire.BlockHeader{
|
||||
baseBlockHdr := &wire.BlockHeader{
|
||||
Version: 1,
|
||||
PrevBlock: mainNetGenesisHash,
|
||||
MerkleRoot: mainNetGenesisMerkleRoot,
|
||||
@ -188,9 +188,9 @@ func TestBlockHeaderSerialize(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.BlockHeader // Data to encode
|
||||
out *btcwire.BlockHeader // Expected decoded data
|
||||
buf []byte // Serialized data
|
||||
in *wire.BlockHeader // Data to encode
|
||||
out *wire.BlockHeader // Expected decoded data
|
||||
buf []byte // Serialized data
|
||||
}{
|
||||
{
|
||||
baseBlockHdr,
|
||||
@ -215,7 +215,7 @@ func TestBlockHeaderSerialize(t *testing.T) {
|
||||
}
|
||||
|
||||
// Deserialize the block header.
|
||||
var bh btcwire.BlockHeader
|
||||
var bh wire.BlockHeader
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = bh.Deserialize(rbuf)
|
||||
if err != nil {
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -12,13 +12,13 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// mainNetGenesisHash is the hash of the first block in the block chain for the
|
||||
// main network (genesis block).
|
||||
var mainNetGenesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
var mainNetGenesisHash = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
||||
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
||||
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
||||
@ -27,7 +27,7 @@ var mainNetGenesisHash = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet
|
||||
|
||||
// mainNetGenesisMerkleRoot is the hash of the first transaction in the genesis
|
||||
// block for the main network.
|
||||
var mainNetGenesisMerkleRoot = btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
var mainNetGenesisMerkleRoot = wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
|
||||
0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
|
||||
0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
|
||||
@ -84,7 +84,7 @@ func TestElementWire(t *testing.T) {
|
||||
[]byte{0x01, 0x02, 0x03, 0x04},
|
||||
},
|
||||
{
|
||||
[btcwire.CommandSize]byte{
|
||||
[wire.CommandSize]byte{
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
},
|
||||
@ -104,7 +104,7 @@ func TestElementWire(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
(*btcwire.ShaHash)(&[btcwire.HashSize]byte{ // Make go vet happy.
|
||||
(*wire.ShaHash)(&[wire.HashSize]byte{ // Make go vet happy.
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
|
||||
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
||||
@ -118,15 +118,15 @@ func TestElementWire(t *testing.T) {
|
||||
},
|
||||
},
|
||||
{
|
||||
btcwire.ServiceFlag(btcwire.SFNodeNetwork),
|
||||
wire.ServiceFlag(wire.SFNodeNetwork),
|
||||
[]byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
},
|
||||
{
|
||||
btcwire.InvType(btcwire.InvTypeTx),
|
||||
wire.InvType(wire.InvTypeTx),
|
||||
[]byte{0x01, 0x00, 0x00, 0x00},
|
||||
},
|
||||
{
|
||||
btcwire.BitcoinNet(btcwire.MainNet),
|
||||
wire.BitcoinNet(wire.MainNet),
|
||||
[]byte{0xf9, 0xbe, 0xb4, 0xd9},
|
||||
},
|
||||
// Type not supported by the "fast" path and requires reflection.
|
||||
@ -140,7 +140,7 @@ func TestElementWire(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Write to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := btcwire.TstWriteElement(&buf, test.in)
|
||||
err := wire.TstWriteElement(&buf, test.in)
|
||||
if err != nil {
|
||||
t.Errorf("writeElement #%d error %v", i, err)
|
||||
continue
|
||||
@ -157,7 +157,7 @@ func TestElementWire(t *testing.T) {
|
||||
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
|
||||
val = reflect.New(reflect.TypeOf(test.in)).Interface()
|
||||
}
|
||||
err = btcwire.TstReadElement(rbuf, val)
|
||||
err = wire.TstReadElement(rbuf, val)
|
||||
if err != nil {
|
||||
t.Errorf("readElement #%d error %v", i, err)
|
||||
continue
|
||||
@ -189,7 +189,7 @@ func TestElementWireErrors(t *testing.T) {
|
||||
{true, 0, io.ErrShortWrite, io.EOF},
|
||||
{[4]byte{0x01, 0x02, 0x03, 0x04}, 0, io.ErrShortWrite, io.EOF},
|
||||
{
|
||||
[btcwire.CommandSize]byte{
|
||||
[wire.CommandSize]byte{
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
},
|
||||
@ -203,7 +203,7 @@ func TestElementWireErrors(t *testing.T) {
|
||||
0, io.ErrShortWrite, io.EOF,
|
||||
},
|
||||
{
|
||||
(*btcwire.ShaHash)(&[btcwire.HashSize]byte{ // Make go vet happy.
|
||||
(*wire.ShaHash)(&[wire.HashSize]byte{ // Make go vet happy.
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
|
||||
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
||||
@ -211,16 +211,16 @@ func TestElementWireErrors(t *testing.T) {
|
||||
}),
|
||||
0, io.ErrShortWrite, io.EOF,
|
||||
},
|
||||
{btcwire.ServiceFlag(btcwire.SFNodeNetwork), 0, io.ErrShortWrite, io.EOF},
|
||||
{btcwire.InvType(btcwire.InvTypeTx), 0, io.ErrShortWrite, io.EOF},
|
||||
{btcwire.BitcoinNet(btcwire.MainNet), 0, io.ErrShortWrite, io.EOF},
|
||||
{wire.ServiceFlag(wire.SFNodeNetwork), 0, io.ErrShortWrite, io.EOF},
|
||||
{wire.InvType(wire.InvTypeTx), 0, io.ErrShortWrite, io.EOF},
|
||||
{wire.BitcoinNet(wire.MainNet), 0, io.ErrShortWrite, io.EOF},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
w := newFixedWriter(test.max)
|
||||
err := btcwire.TstWriteElement(w, test.in)
|
||||
err := wire.TstWriteElement(w, test.in)
|
||||
if err != test.writeErr {
|
||||
t.Errorf("writeElement #%d wrong error got: %v, want: %v",
|
||||
i, err, test.writeErr)
|
||||
@ -233,7 +233,7 @@ func TestElementWireErrors(t *testing.T) {
|
||||
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
|
||||
val = reflect.New(reflect.TypeOf(test.in)).Interface()
|
||||
}
|
||||
err = btcwire.TstReadElement(r, val)
|
||||
err = wire.TstReadElement(r, val)
|
||||
if err != test.readErr {
|
||||
t.Errorf("readElement #%d wrong error got: %v, want: %v",
|
||||
i, err, test.readErr)
|
||||
@ -244,7 +244,7 @@ func TestElementWireErrors(t *testing.T) {
|
||||
|
||||
// TestVarIntWire tests wire encode and decode for variable length integers.
|
||||
func TestVarIntWire(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
tests := []struct {
|
||||
in uint64 // Value to encode
|
||||
@ -283,7 +283,7 @@ func TestVarIntWire(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := btcwire.TstWriteVarInt(&buf, test.pver, test.in)
|
||||
err := wire.TstWriteVarInt(&buf, test.pver, test.in)
|
||||
if err != nil {
|
||||
t.Errorf("writeVarInt #%d error %v", i, err)
|
||||
continue
|
||||
@ -296,7 +296,7 @@ func TestVarIntWire(t *testing.T) {
|
||||
|
||||
// Decode from wire format.
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
val, err := btcwire.TstReadVarInt(rbuf, test.pver)
|
||||
val, err := wire.TstReadVarInt(rbuf, test.pver)
|
||||
if err != nil {
|
||||
t.Errorf("readVarInt #%d error %v", i, err)
|
||||
continue
|
||||
@ -312,7 +312,7 @@ func TestVarIntWire(t *testing.T) {
|
||||
// TestVarIntWireErrors performs negative tests against wire encode and decode
|
||||
// of variable length integers to confirm error paths work correctly.
|
||||
func TestVarIntWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
tests := []struct {
|
||||
in uint64 // Value to encode
|
||||
@ -336,7 +336,7 @@ func TestVarIntWireErrors(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
w := newFixedWriter(test.max)
|
||||
err := btcwire.TstWriteVarInt(w, test.pver, test.in)
|
||||
err := wire.TstWriteVarInt(w, test.pver, test.in)
|
||||
if err != test.writeErr {
|
||||
t.Errorf("writeVarInt #%d wrong error got: %v, want: %v",
|
||||
i, err, test.writeErr)
|
||||
@ -345,7 +345,7 @@ func TestVarIntWireErrors(t *testing.T) {
|
||||
|
||||
// Decode from wire format.
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
_, err = btcwire.TstReadVarInt(r, test.pver)
|
||||
_, err = wire.TstReadVarInt(r, test.pver)
|
||||
if err != test.readErr {
|
||||
t.Errorf("readVarInt #%d wrong error got: %v, want: %v",
|
||||
i, err, test.readErr)
|
||||
@ -380,7 +380,7 @@ func TestVarIntSerializeSize(t *testing.T) {
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
serializedSize := btcwire.VarIntSerializeSize(test.val)
|
||||
serializedSize := wire.VarIntSerializeSize(test.val)
|
||||
if serializedSize != test.size {
|
||||
t.Errorf("VarIntSerializeSize #%d got: %d, want: %d", i,
|
||||
serializedSize, test.size)
|
||||
@ -391,7 +391,7 @@ func TestVarIntSerializeSize(t *testing.T) {
|
||||
|
||||
// TestVarStringWire tests wire encode and decode for variable length strings.
|
||||
func TestVarStringWire(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// str256 is a string that takes a 2-byte varint to encode.
|
||||
str256 := strings.Repeat("test", 64)
|
||||
@ -415,7 +415,7 @@ func TestVarStringWire(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := btcwire.TstWriteVarString(&buf, test.pver, test.in)
|
||||
err := wire.TstWriteVarString(&buf, test.pver, test.in)
|
||||
if err != nil {
|
||||
t.Errorf("writeVarString #%d error %v", i, err)
|
||||
continue
|
||||
@ -428,7 +428,7 @@ func TestVarStringWire(t *testing.T) {
|
||||
|
||||
// Decode from wire format.
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
val, err := btcwire.TstReadVarString(rbuf, test.pver)
|
||||
val, err := wire.TstReadVarString(rbuf, test.pver)
|
||||
if err != nil {
|
||||
t.Errorf("readVarString #%d error %v", i, err)
|
||||
continue
|
||||
@ -444,7 +444,7 @@ func TestVarStringWire(t *testing.T) {
|
||||
// TestVarStringWireErrors performs negative tests against wire encode and
|
||||
// decode of variable length strings to confirm error paths work correctly.
|
||||
func TestVarStringWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// str256 is a string that takes a 2-byte varint to encode.
|
||||
str256 := strings.Repeat("test", 64)
|
||||
@ -470,7 +470,7 @@ func TestVarStringWireErrors(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
w := newFixedWriter(test.max)
|
||||
err := btcwire.TstWriteVarString(w, test.pver, test.in)
|
||||
err := wire.TstWriteVarString(w, test.pver, test.in)
|
||||
if err != test.writeErr {
|
||||
t.Errorf("writeVarString #%d wrong error got: %v, want: %v",
|
||||
i, err, test.writeErr)
|
||||
@ -479,7 +479,7 @@ func TestVarStringWireErrors(t *testing.T) {
|
||||
|
||||
// Decode from wire format.
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
_, err = btcwire.TstReadVarString(r, test.pver)
|
||||
_, err = wire.TstReadVarString(r, test.pver)
|
||||
if err != test.readErr {
|
||||
t.Errorf("readVarString #%d wrong error got: %v, want: %v",
|
||||
i, err, test.readErr)
|
||||
@ -493,7 +493,7 @@ func TestVarStringWireErrors(t *testing.T) {
|
||||
// length are handled properly. This could otherwise potentially be used as an
|
||||
// attack vector.
|
||||
func TestVarStringOverflowErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
tests := []struct {
|
||||
buf []byte // Wire encoding
|
||||
@ -501,16 +501,16 @@ func TestVarStringOverflowErrors(t *testing.T) {
|
||||
err error // Expected error
|
||||
}{
|
||||
{[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||
pver, &btcwire.MessageError{}},
|
||||
pver, &wire.MessageError{}},
|
||||
{[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
|
||||
pver, &btcwire.MessageError{}},
|
||||
pver, &wire.MessageError{}},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
// Decode from wire format.
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
_, err := btcwire.TstReadVarString(rbuf, test.pver)
|
||||
_, err := wire.TstReadVarString(rbuf, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
||||
t.Errorf("readVarString #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, reflect.TypeOf(test.err))
|
||||
@ -522,7 +522,7 @@ func TestVarStringOverflowErrors(t *testing.T) {
|
||||
|
||||
// TestVarBytesWire tests wire encode and decode for variable length byte array.
|
||||
func TestVarBytesWire(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// bytes256 is a byte array that takes a 2-byte varint to encode.
|
||||
bytes256 := bytes.Repeat([]byte{0x01}, 256)
|
||||
@ -545,7 +545,7 @@ func TestVarBytesWire(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := btcwire.TstWriteVarBytes(&buf, test.pver, test.in)
|
||||
err := wire.TstWriteVarBytes(&buf, test.pver, test.in)
|
||||
if err != nil {
|
||||
t.Errorf("writeVarBytes #%d error %v", i, err)
|
||||
continue
|
||||
@ -558,8 +558,8 @@ func TestVarBytesWire(t *testing.T) {
|
||||
|
||||
// Decode from wire format.
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
val, err := btcwire.TstReadVarBytes(rbuf, test.pver,
|
||||
btcwire.MaxMessagePayload, "test payload")
|
||||
val, err := wire.TstReadVarBytes(rbuf, test.pver,
|
||||
wire.MaxMessagePayload, "test payload")
|
||||
if err != nil {
|
||||
t.Errorf("readVarBytes #%d error %v", i, err)
|
||||
continue
|
||||
@ -575,7 +575,7 @@ func TestVarBytesWire(t *testing.T) {
|
||||
// TestVarBytesWireErrors performs negative tests against wire encode and
|
||||
// decode of variable length byte arrays to confirm error paths work correctly.
|
||||
func TestVarBytesWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// bytes256 is a byte array that takes a 2-byte varint to encode.
|
||||
bytes256 := bytes.Repeat([]byte{0x01}, 256)
|
||||
@ -601,7 +601,7 @@ func TestVarBytesWireErrors(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
w := newFixedWriter(test.max)
|
||||
err := btcwire.TstWriteVarBytes(w, test.pver, test.in)
|
||||
err := wire.TstWriteVarBytes(w, test.pver, test.in)
|
||||
if err != test.writeErr {
|
||||
t.Errorf("writeVarBytes #%d wrong error got: %v, want: %v",
|
||||
i, err, test.writeErr)
|
||||
@ -610,8 +610,8 @@ func TestVarBytesWireErrors(t *testing.T) {
|
||||
|
||||
// Decode from wire format.
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
_, err = btcwire.TstReadVarBytes(r, test.pver,
|
||||
btcwire.MaxMessagePayload, "test payload")
|
||||
_, err = wire.TstReadVarBytes(r, test.pver,
|
||||
wire.MaxMessagePayload, "test payload")
|
||||
if err != test.readErr {
|
||||
t.Errorf("readVarBytes #%d wrong error got: %v, want: %v",
|
||||
i, err, test.readErr)
|
||||
@ -625,7 +625,7 @@ func TestVarBytesWireErrors(t *testing.T) {
|
||||
// length are handled properly. This could otherwise potentially be used as an
|
||||
// attack vector.
|
||||
func TestVarBytesOverflowErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
tests := []struct {
|
||||
buf []byte // Wire encoding
|
||||
@ -633,17 +633,17 @@ func TestVarBytesOverflowErrors(t *testing.T) {
|
||||
err error // Expected error
|
||||
}{
|
||||
{[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
||||
pver, &btcwire.MessageError{}},
|
||||
pver, &wire.MessageError{}},
|
||||
{[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
|
||||
pver, &btcwire.MessageError{}},
|
||||
pver, &wire.MessageError{}},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
// Decode from wire format.
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
_, err := btcwire.TstReadVarBytes(rbuf, test.pver,
|
||||
btcwire.MaxMessagePayload, "test payload")
|
||||
_, 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, "+
|
||||
"want: %v", i, err, reflect.TypeOf(test.err))
|
||||
@ -669,7 +669,7 @@ func TestRandomUint64(t *testing.T) {
|
||||
|
||||
numHits := 0
|
||||
for i := 0; i < tries; i++ {
|
||||
nonce, err := btcwire.RandomUint64()
|
||||
nonce, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("RandomUint64 iteration %d failed - err %v",
|
||||
i, err)
|
||||
@ -692,7 +692,7 @@ func TestRandomUint64(t *testing.T) {
|
||||
func TestRandomUint64Errors(t *testing.T) {
|
||||
// Test short reads.
|
||||
fr := &fakeRandReader{n: 2, err: io.EOF}
|
||||
nonce, err := btcwire.TstRandomUint64(fr)
|
||||
nonce, err := wire.TstRandomUint64(fr)
|
||||
if err != io.ErrUnexpectedEOF {
|
||||
t.Errorf("TestRandomUint64Fails: Error not expected value of %v [%v]",
|
||||
io.ErrShortBuffer, err)
|
@ -1,9 +1,9 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
/*
|
||||
Package btcwire implements the bitcoin wire protocol.
|
||||
Package wire implements the bitcoin wire protocol.
|
||||
|
||||
For the complete details of the bitcoin protocol, see the official wiki entry
|
||||
at https://en.bitcoin.it/wiki/Protocol_specification. The following only serves
|
||||
@ -72,7 +72,7 @@ Protocol Version
|
||||
|
||||
The protocol version should be negotiated with the remote peer at a higher
|
||||
level than this package via the version (MsgVersion) message exchange, however,
|
||||
this package provides the btcwire.ProtocolVersion constant which indicates the
|
||||
this package provides the wire.ProtocolVersion constant which indicates the
|
||||
latest protocol version this package supports and is typically the value to use
|
||||
for all outbound connections before a potentially lower protocol version is
|
||||
negotiated.
|
||||
@ -83,10 +83,10 @@ The bitcoin network is a magic number which is used to identify the start of a
|
||||
message and which bitcoin network the message applies to. This package provides
|
||||
the following constants:
|
||||
|
||||
btcwire.MainNet
|
||||
btcwire.TestNet (Regression test network)
|
||||
btcwire.TestNet3 (Test network version 3)
|
||||
btcwire.SimNet (Simulation test network)
|
||||
wire.MainNet
|
||||
wire.TestNet (Regression test network)
|
||||
wire.TestNet3 (Test network version 3)
|
||||
wire.SimNet (Simulation test network)
|
||||
|
||||
Determining Message Type
|
||||
|
||||
@ -98,10 +98,10 @@ switch or type assertion. An example of a type switch follows:
|
||||
// Assumes msg is already a valid concrete message such as one created
|
||||
// via NewMsgVersion or read via ReadMessage.
|
||||
switch msg := msg.(type) {
|
||||
case *btcwire.MsgVersion:
|
||||
case *wire.MsgVersion:
|
||||
// The message is a pointer to a MsgVersion struct.
|
||||
fmt.Printf("Protocol version: %v", msg.ProtocolVersion)
|
||||
case *btcwire.MsgBlock:
|
||||
case *wire.MsgBlock:
|
||||
// The message is a pointer to a MsgBlock struct.
|
||||
fmt.Printf("Number of tx in block: %v", msg.Header.TxnCount)
|
||||
}
|
||||
@ -114,9 +114,9 @@ a remote node running a bitcoin peer. Example syntax is:
|
||||
|
||||
// Reads and validates the next bitcoin message from conn using the
|
||||
// protocol version pver and the bitcoin network btcnet. The returns
|
||||
// are a btcwire.Message, a []byte which contains the unmarshalled
|
||||
// are a wire.Message, a []byte which contains the unmarshalled
|
||||
// raw payload, and a possible error.
|
||||
msg, rawPayload, err := btcwire.ReadMessage(conn, pver, btcnet)
|
||||
msg, rawPayload, err := wire.ReadMessage(conn, pver, btcnet)
|
||||
if err != nil {
|
||||
// Log and handle the error
|
||||
}
|
||||
@ -129,12 +129,12 @@ a remote node running a bitcoin peer. Example syntax to request addresses
|
||||
from a remote peer is:
|
||||
|
||||
// Create a new getaddr bitcoin message.
|
||||
msg := btcwire.NewMsgGetAddr()
|
||||
msg := wire.NewMsgGetAddr()
|
||||
|
||||
// Writes a bitcoin message msg to conn using the protocol version
|
||||
// pver, and the bitcoin network btcnet. The return is a possible
|
||||
// error.
|
||||
err := btcwire.WriteMessage(conn, msg, pver, btcnet)
|
||||
err := wire.WriteMessage(conn, msg, pver, btcnet)
|
||||
if err != nil {
|
||||
// Log and handle the error
|
||||
}
|
||||
@ -143,7 +143,7 @@ Errors
|
||||
|
||||
Errors returned by this package are either the raw errors provided by underlying
|
||||
calls to read/write from streams such as io.EOF, io.ErrUnexpectedEOF, and
|
||||
io.ErrShortWrite, or of type btcwire.MessageError. This allows the caller to
|
||||
io.ErrShortWrite, or of type wire.MessageError. This allows the caller to
|
||||
differentiate between general IO errors and malformed messages through type
|
||||
assertions.
|
||||
|
||||
@ -156,4 +156,4 @@ This package includes spec changes outlined by the following BIPs:
|
||||
BIP0035 (https://en.bitcoin.it/wiki/BIP_0035)
|
||||
BIP0037 (https://en.bitcoin.it/wiki/BIP_0037)
|
||||
*/
|
||||
package btcwire
|
||||
package wire
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"net"
|
@ -1,16 +1,16 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// fakeMessage implements the btcwire.Message interface and is used to force
|
||||
// fakeMessage implements the wire.Message interface and is used to force
|
||||
// encode errors in messages.
|
||||
type fakeMessage struct {
|
||||
command string
|
||||
@ -19,7 +19,7 @@ type fakeMessage struct {
|
||||
forceLenErr bool
|
||||
}
|
||||
|
||||
// BtcDecode doesn't do anything. It just satisfies the btcwire.Message
|
||||
// BtcDecode doesn't do anything. It just satisfies the wire.Message
|
||||
// interface.
|
||||
func (msg *fakeMessage) BtcDecode(r io.Reader, pver uint32) error {
|
||||
return nil
|
||||
@ -27,10 +27,10 @@ func (msg *fakeMessage) BtcDecode(r io.Reader, pver uint32) error {
|
||||
|
||||
// BtcEncode writes the payload field of the fake message or forces an error
|
||||
// if the forceEncodeErr flag of the fake message is set. It also satisfies the
|
||||
// btcwire.Message interface.
|
||||
// wire.Message interface.
|
||||
func (msg *fakeMessage) BtcEncode(w io.Writer, pver uint32) error {
|
||||
if msg.forceEncodeErr {
|
||||
err := &btcwire.MessageError{
|
||||
err := &wire.MessageError{
|
||||
Func: "fakeMessage.BtcEncode",
|
||||
Description: "intentional error",
|
||||
}
|
||||
@ -42,14 +42,14 @@ func (msg *fakeMessage) BtcEncode(w io.Writer, pver uint32) error {
|
||||
}
|
||||
|
||||
// Command returns the command field of the fake message and satisfies the
|
||||
// btcwire.Message interface.
|
||||
// wire.Message interface.
|
||||
func (msg *fakeMessage) Command() string {
|
||||
return msg.command
|
||||
}
|
||||
|
||||
// MaxPayloadLength returns the length of the payload field of fake message
|
||||
// or a smaller value if the forceLenErr flag of the fake message is set. It
|
||||
// satisfies the btcwire.Message interface.
|
||||
// satisfies the wire.Message interface.
|
||||
func (msg *fakeMessage) MaxPayloadLength(pver uint32) uint32 {
|
||||
lenp := uint32(len(msg.payload))
|
||||
if msg.forceLenErr {
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,15 +1,15 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
/*
|
||||
This test file is part of the btcwire package rather than than the
|
||||
btcwire_test package so it can bridge access to the internals to properly test
|
||||
cases which are either not possible or can't reliably be tested via the public
|
||||
interface. The functions are only exported while the tests are being run.
|
||||
This test file is part of the wire package rather than than the wire_test
|
||||
package so it can bridge access to the internals to properly test cases which
|
||||
are either not possible or can't reliably be tested via the public interface.
|
||||
The functions are only exported while the tests are being run.
|
||||
*/
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"io"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,27 +1,27 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestInvVectStringer tests the stringized output for inventory vector types.
|
||||
func TestInvTypeStringer(t *testing.T) {
|
||||
tests := []struct {
|
||||
in btcwire.InvType
|
||||
in wire.InvType
|
||||
want string
|
||||
}{
|
||||
{btcwire.InvTypeError, "ERROR"},
|
||||
{btcwire.InvTypeTx, "MSG_TX"},
|
||||
{btcwire.InvTypeBlock, "MSG_BLOCK"},
|
||||
{wire.InvTypeError, "ERROR"},
|
||||
{wire.InvTypeTx, "MSG_TX"},
|
||||
{wire.InvTypeBlock, "MSG_BLOCK"},
|
||||
{0xffffffff, "Unknown InvType (4294967295)"},
|
||||
}
|
||||
|
||||
@ -39,11 +39,11 @@ func TestInvTypeStringer(t *testing.T) {
|
||||
|
||||
// TestInvVect tests the InvVect API.
|
||||
func TestInvVect(t *testing.T) {
|
||||
ivType := btcwire.InvTypeBlock
|
||||
hash := btcwire.ShaHash{}
|
||||
ivType := wire.InvTypeBlock
|
||||
hash := wire.ShaHash{}
|
||||
|
||||
// Ensure we get the same payload and signature back out.
|
||||
iv := btcwire.NewInvVect(ivType, &hash)
|
||||
iv := wire.NewInvVect(ivType, &hash)
|
||||
if iv.Type != ivType {
|
||||
t.Errorf("NewInvVect: wrong type - got %v, want %v",
|
||||
iv.Type, ivType)
|
||||
@ -60,15 +60,15 @@ func TestInvVect(t *testing.T) {
|
||||
func TestInvVectWire(t *testing.T) {
|
||||
// Block 203707 hash.
|
||||
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
|
||||
baseHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
baseHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// errInvVect is an inventory vector with an error.
|
||||
errInvVect := btcwire.InvVect{
|
||||
Type: btcwire.InvTypeError,
|
||||
Hash: btcwire.ShaHash{},
|
||||
errInvVect := wire.InvVect{
|
||||
Type: wire.InvTypeError,
|
||||
Hash: wire.ShaHash{},
|
||||
}
|
||||
|
||||
// errInvVectEncoded is the wire encoded bytes of errInvVect.
|
||||
@ -81,8 +81,8 @@ func TestInvVectWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// txInvVect is an inventory vector representing a transaction.
|
||||
txInvVect := btcwire.InvVect{
|
||||
Type: btcwire.InvTypeTx,
|
||||
txInvVect := wire.InvVect{
|
||||
Type: wire.InvTypeTx,
|
||||
Hash: *baseHash,
|
||||
}
|
||||
|
||||
@ -96,8 +96,8 @@ func TestInvVectWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// blockInvVect is an inventory vector representing a block.
|
||||
blockInvVect := btcwire.InvVect{
|
||||
Type: btcwire.InvTypeBlock,
|
||||
blockInvVect := wire.InvVect{
|
||||
Type: wire.InvTypeBlock,
|
||||
Hash: *baseHash,
|
||||
}
|
||||
|
||||
@ -111,17 +111,17 @@ func TestInvVectWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in btcwire.InvVect // NetAddress to encode
|
||||
out btcwire.InvVect // Expected decoded NetAddress
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in wire.InvVect // NetAddress to encode
|
||||
out wire.InvVect // Expected decoded NetAddress
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version error inventory vector.
|
||||
{
|
||||
errInvVect,
|
||||
errInvVect,
|
||||
errInvVectEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version tx inventory vector.
|
||||
@ -129,7 +129,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
txInvVect,
|
||||
txInvVect,
|
||||
txInvVectEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version block inventory vector.
|
||||
@ -137,7 +137,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
blockInvVect,
|
||||
blockInvVect,
|
||||
blockInvVectEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version error inventory vector.
|
||||
@ -145,7 +145,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
errInvVect,
|
||||
errInvVect,
|
||||
errInvVectEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version tx inventory vector.
|
||||
@ -153,7 +153,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
txInvVect,
|
||||
txInvVect,
|
||||
txInvVectEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version block inventory vector.
|
||||
@ -161,7 +161,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
blockInvVect,
|
||||
blockInvVect,
|
||||
blockInvVectEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version error inventory vector.
|
||||
@ -169,7 +169,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
errInvVect,
|
||||
errInvVect,
|
||||
errInvVectEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version tx inventory vector.
|
||||
@ -177,7 +177,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
txInvVect,
|
||||
txInvVect,
|
||||
txInvVectEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version block inventory vector.
|
||||
@ -185,7 +185,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
blockInvVect,
|
||||
blockInvVect,
|
||||
blockInvVectEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion error inventory vector.
|
||||
@ -193,7 +193,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
errInvVect,
|
||||
errInvVect,
|
||||
errInvVectEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion tx inventory vector.
|
||||
@ -201,7 +201,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
txInvVect,
|
||||
txInvVect,
|
||||
txInvVectEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion block inventory vector.
|
||||
@ -209,7 +209,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
blockInvVect,
|
||||
blockInvVect,
|
||||
blockInvVectEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion error inventory vector.
|
||||
@ -217,7 +217,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
errInvVect,
|
||||
errInvVect,
|
||||
errInvVectEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion tx inventory vector.
|
||||
@ -225,7 +225,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
txInvVect,
|
||||
txInvVect,
|
||||
txInvVectEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion block inventory vector.
|
||||
@ -233,7 +233,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
blockInvVect,
|
||||
blockInvVect,
|
||||
blockInvVectEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -241,7 +241,7 @@ func TestInvVectWire(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := btcwire.TstWriteInvVect(&buf, test.pver, &test.in)
|
||||
err := wire.TstWriteInvVect(&buf, test.pver, &test.in)
|
||||
if err != nil {
|
||||
t.Errorf("writeInvVect #%d error %v", i, err)
|
||||
continue
|
||||
@ -253,9 +253,9 @@ func TestInvVectWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var iv btcwire.InvVect
|
||||
var iv wire.InvVect
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = btcwire.TstReadInvVect(rbuf, test.pver, &iv)
|
||||
err = wire.TstReadInvVect(rbuf, test.pver, &iv)
|
||||
if err != nil {
|
||||
t.Errorf("readInvVect #%d error %v", i, err)
|
||||
continue
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -13,13 +13,13 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// makeHeader is a convenience function to make a message header in the form of
|
||||
// a byte slice. It is used to force errors when reading messages.
|
||||
func makeHeader(btcnet btcwire.BitcoinNet, command string,
|
||||
func makeHeader(btcnet wire.BitcoinNet, command string,
|
||||
payloadLen uint32, checksum uint32) []byte {
|
||||
|
||||
// The length of a bitcoin message header is 24 bytes.
|
||||
@ -35,82 +35,82 @@ func makeHeader(btcnet btcwire.BitcoinNet, command string,
|
||||
|
||||
// TestMessage tests the Read/WriteMessage and Read/WriteMessageN API.
|
||||
func TestMessage(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Create the various types of messages to test.
|
||||
|
||||
// MsgVersion.
|
||||
addrYou := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
|
||||
you, err := btcwire.NewNetAddress(addrYou, btcwire.SFNodeNetwork)
|
||||
you, err := wire.NewNetAddress(addrYou, wire.SFNodeNetwork)
|
||||
if err != nil {
|
||||
t.Errorf("NewNetAddress: %v", err)
|
||||
}
|
||||
you.Timestamp = time.Time{} // Version message has zero value timestamp.
|
||||
addrMe := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
|
||||
me, err := btcwire.NewNetAddress(addrMe, btcwire.SFNodeNetwork)
|
||||
me, err := wire.NewNetAddress(addrMe, wire.SFNodeNetwork)
|
||||
if err != nil {
|
||||
t.Errorf("NewNetAddress: %v", err)
|
||||
}
|
||||
me.Timestamp = time.Time{} // Version message has zero value timestamp.
|
||||
msgVersion := btcwire.NewMsgVersion(me, you, 123123, 0)
|
||||
msgVersion := wire.NewMsgVersion(me, you, 123123, 0)
|
||||
|
||||
msgVerack := btcwire.NewMsgVerAck()
|
||||
msgGetAddr := btcwire.NewMsgGetAddr()
|
||||
msgAddr := btcwire.NewMsgAddr()
|
||||
msgGetBlocks := btcwire.NewMsgGetBlocks(&btcwire.ShaHash{})
|
||||
msgVerack := wire.NewMsgVerAck()
|
||||
msgGetAddr := wire.NewMsgGetAddr()
|
||||
msgAddr := wire.NewMsgAddr()
|
||||
msgGetBlocks := wire.NewMsgGetBlocks(&wire.ShaHash{})
|
||||
msgBlock := &blockOne
|
||||
msgInv := btcwire.NewMsgInv()
|
||||
msgGetData := btcwire.NewMsgGetData()
|
||||
msgNotFound := btcwire.NewMsgNotFound()
|
||||
msgTx := btcwire.NewMsgTx()
|
||||
msgPing := btcwire.NewMsgPing(123123)
|
||||
msgPong := btcwire.NewMsgPong(123123)
|
||||
msgGetHeaders := btcwire.NewMsgGetHeaders()
|
||||
msgHeaders := btcwire.NewMsgHeaders()
|
||||
msgAlert := btcwire.NewMsgAlert([]byte("payload"), []byte("signature"))
|
||||
msgMemPool := btcwire.NewMsgMemPool()
|
||||
msgFilterAdd := btcwire.NewMsgFilterAdd([]byte{0x01})
|
||||
msgFilterClear := btcwire.NewMsgFilterClear()
|
||||
msgFilterLoad := btcwire.NewMsgFilterLoad([]byte{0x01}, 10, 0, btcwire.BloomUpdateNone)
|
||||
bh := btcwire.NewBlockHeader(&btcwire.ShaHash{}, &btcwire.ShaHash{}, 0, 0)
|
||||
msgMerkleBlock := btcwire.NewMsgMerkleBlock(bh)
|
||||
msgReject := btcwire.NewMsgReject("block", btcwire.RejectDuplicate, "duplicate block")
|
||||
msgInv := wire.NewMsgInv()
|
||||
msgGetData := wire.NewMsgGetData()
|
||||
msgNotFound := wire.NewMsgNotFound()
|
||||
msgTx := wire.NewMsgTx()
|
||||
msgPing := wire.NewMsgPing(123123)
|
||||
msgPong := wire.NewMsgPong(123123)
|
||||
msgGetHeaders := wire.NewMsgGetHeaders()
|
||||
msgHeaders := wire.NewMsgHeaders()
|
||||
msgAlert := wire.NewMsgAlert([]byte("payload"), []byte("signature"))
|
||||
msgMemPool := wire.NewMsgMemPool()
|
||||
msgFilterAdd := wire.NewMsgFilterAdd([]byte{0x01})
|
||||
msgFilterClear := wire.NewMsgFilterClear()
|
||||
msgFilterLoad := wire.NewMsgFilterLoad([]byte{0x01}, 10, 0, wire.BloomUpdateNone)
|
||||
bh := wire.NewBlockHeader(&wire.ShaHash{}, &wire.ShaHash{}, 0, 0)
|
||||
msgMerkleBlock := wire.NewMsgMerkleBlock(bh)
|
||||
msgReject := wire.NewMsgReject("block", wire.RejectDuplicate, "duplicate block")
|
||||
|
||||
tests := []struct {
|
||||
in btcwire.Message // Value to encode
|
||||
out btcwire.Message // Expected decoded value
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
btcnet btcwire.BitcoinNet // Network to use for wire encoding
|
||||
bytes int // Expected num bytes read/written
|
||||
in wire.Message // Value to encode
|
||||
out wire.Message // Expected decoded value
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
btcnet wire.BitcoinNet // Network to use for wire encoding
|
||||
bytes int // Expected num bytes read/written
|
||||
}{
|
||||
{msgVersion, msgVersion, pver, btcwire.MainNet, 125},
|
||||
{msgVerack, msgVerack, pver, btcwire.MainNet, 24},
|
||||
{msgGetAddr, msgGetAddr, pver, btcwire.MainNet, 24},
|
||||
{msgAddr, msgAddr, pver, btcwire.MainNet, 25},
|
||||
{msgGetBlocks, msgGetBlocks, pver, btcwire.MainNet, 61},
|
||||
{msgBlock, msgBlock, pver, btcwire.MainNet, 239},
|
||||
{msgInv, msgInv, pver, btcwire.MainNet, 25},
|
||||
{msgGetData, msgGetData, pver, btcwire.MainNet, 25},
|
||||
{msgNotFound, msgNotFound, pver, btcwire.MainNet, 25},
|
||||
{msgTx, msgTx, pver, btcwire.MainNet, 34},
|
||||
{msgPing, msgPing, pver, btcwire.MainNet, 32},
|
||||
{msgPong, msgPong, pver, btcwire.MainNet, 32},
|
||||
{msgGetHeaders, msgGetHeaders, pver, btcwire.MainNet, 61},
|
||||
{msgHeaders, msgHeaders, pver, btcwire.MainNet, 25},
|
||||
{msgAlert, msgAlert, pver, btcwire.MainNet, 42},
|
||||
{msgMemPool, msgMemPool, pver, btcwire.MainNet, 24},
|
||||
{msgFilterAdd, msgFilterAdd, pver, btcwire.MainNet, 26},
|
||||
{msgFilterClear, msgFilterClear, pver, btcwire.MainNet, 24},
|
||||
{msgFilterLoad, msgFilterLoad, pver, btcwire.MainNet, 35},
|
||||
{msgMerkleBlock, msgMerkleBlock, pver, btcwire.MainNet, 110},
|
||||
{msgReject, msgReject, pver, btcwire.MainNet, 79},
|
||||
{msgVersion, msgVersion, pver, wire.MainNet, 125},
|
||||
{msgVerack, msgVerack, pver, wire.MainNet, 24},
|
||||
{msgGetAddr, msgGetAddr, pver, wire.MainNet, 24},
|
||||
{msgAddr, msgAddr, pver, wire.MainNet, 25},
|
||||
{msgGetBlocks, msgGetBlocks, pver, wire.MainNet, 61},
|
||||
{msgBlock, msgBlock, pver, wire.MainNet, 239},
|
||||
{msgInv, msgInv, pver, wire.MainNet, 25},
|
||||
{msgGetData, msgGetData, pver, wire.MainNet, 25},
|
||||
{msgNotFound, msgNotFound, pver, wire.MainNet, 25},
|
||||
{msgTx, msgTx, pver, wire.MainNet, 34},
|
||||
{msgPing, msgPing, pver, wire.MainNet, 32},
|
||||
{msgPong, msgPong, pver, wire.MainNet, 32},
|
||||
{msgGetHeaders, msgGetHeaders, pver, wire.MainNet, 61},
|
||||
{msgHeaders, msgHeaders, pver, wire.MainNet, 25},
|
||||
{msgAlert, msgAlert, pver, wire.MainNet, 42},
|
||||
{msgMemPool, msgMemPool, pver, wire.MainNet, 24},
|
||||
{msgFilterAdd, msgFilterAdd, pver, wire.MainNet, 26},
|
||||
{msgFilterClear, msgFilterClear, pver, wire.MainNet, 24},
|
||||
{msgFilterLoad, msgFilterLoad, pver, wire.MainNet, 35},
|
||||
{msgMerkleBlock, msgMerkleBlock, pver, wire.MainNet, 110},
|
||||
{msgReject, msgReject, pver, wire.MainNet, 79},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
nw, err := btcwire.WriteMessageN(&buf, test.in, test.pver, test.btcnet)
|
||||
nw, err := wire.WriteMessageN(&buf, test.in, test.pver, test.btcnet)
|
||||
if err != nil {
|
||||
t.Errorf("WriteMessage #%d error %v", i, err)
|
||||
continue
|
||||
@ -124,7 +124,7 @@ func TestMessage(t *testing.T) {
|
||||
|
||||
// Decode from wire format.
|
||||
rbuf := bytes.NewReader(buf.Bytes())
|
||||
nr, msg, _, err := btcwire.ReadMessageN(rbuf, test.pver, test.btcnet)
|
||||
nr, msg, _, err := wire.ReadMessageN(rbuf, test.pver, test.btcnet)
|
||||
if err != nil {
|
||||
t.Errorf("ReadMessage #%d error %v, msg %v", i, err,
|
||||
spew.Sdump(msg))
|
||||
@ -149,7 +149,7 @@ func TestMessage(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := btcwire.WriteMessage(&buf, test.in, test.pver, test.btcnet)
|
||||
err := wire.WriteMessage(&buf, test.in, test.pver, test.btcnet)
|
||||
if err != nil {
|
||||
t.Errorf("WriteMessage #%d error %v", i, err)
|
||||
continue
|
||||
@ -157,7 +157,7 @@ func TestMessage(t *testing.T) {
|
||||
|
||||
// Decode from wire format.
|
||||
rbuf := bytes.NewReader(buf.Bytes())
|
||||
msg, _, err := btcwire.ReadMessage(rbuf, test.pver, test.btcnet)
|
||||
msg, _, err := wire.ReadMessage(rbuf, test.pver, test.btcnet)
|
||||
if err != nil {
|
||||
t.Errorf("ReadMessage #%d error %v, msg %v", i, err,
|
||||
spew.Sdump(msg))
|
||||
@ -174,12 +174,12 @@ func TestMessage(t *testing.T) {
|
||||
// TestReadMessageWireErrors performs negative tests against wire decoding into
|
||||
// concrete messages to confirm error paths work correctly.
|
||||
func TestReadMessageWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
btcnet := btcwire.MainNet
|
||||
pver := wire.ProtocolVersion
|
||||
btcnet := wire.MainNet
|
||||
|
||||
// Ensure message errors are as expected with no function specified.
|
||||
wantErr := "something bad happened"
|
||||
testErr := btcwire.MessageError{Description: wantErr}
|
||||
testErr := wire.MessageError{Description: wantErr}
|
||||
if testErr.Error() != wantErr {
|
||||
t.Errorf("MessageError: wrong error - got %v, want %v",
|
||||
testErr.Error(), wantErr)
|
||||
@ -187,18 +187,18 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
|
||||
// Ensure message errors are as expected with a function specified.
|
||||
wantFunc := "foo"
|
||||
testErr = btcwire.MessageError{Func: wantFunc, Description: wantErr}
|
||||
testErr = wire.MessageError{Func: wantFunc, Description: wantErr}
|
||||
if testErr.Error() != wantFunc+": "+wantErr {
|
||||
t.Errorf("MessageError: wrong error - got %v, want %v",
|
||||
testErr.Error(), wantErr)
|
||||
}
|
||||
|
||||
// Wire encoded bytes for main and testnet3 networks magic identifiers.
|
||||
testNet3Bytes := makeHeader(btcwire.TestNet3, "", 0, 0)
|
||||
testNet3Bytes := makeHeader(wire.TestNet3, "", 0, 0)
|
||||
|
||||
// Wire encoded bytes for a message that exceeds max overall message
|
||||
// length.
|
||||
mpl := uint32(btcwire.MaxMessagePayload)
|
||||
mpl := uint32(wire.MaxMessagePayload)
|
||||
exceedMaxPayloadBytes := makeHeader(btcnet, "getaddr", mpl+1, 0)
|
||||
|
||||
// Wire encoded bytes for a command which is invalid utf-8.
|
||||
@ -233,12 +233,12 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
discardBytes := makeHeader(btcnet, "bogus", 15*1024, 0)
|
||||
|
||||
tests := []struct {
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
btcnet btcwire.BitcoinNet // Bitcoin network for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
readErr error // Expected read error
|
||||
bytes int // Expected num bytes read
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
btcnet wire.BitcoinNet // Bitcoin network for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
readErr error // Expected read error
|
||||
bytes int // Expected num bytes read
|
||||
}{
|
||||
// Latest protocol version with intentional read errors.
|
||||
|
||||
@ -258,7 +258,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
pver,
|
||||
btcnet,
|
||||
len(testNet3Bytes),
|
||||
&btcwire.MessageError{},
|
||||
&wire.MessageError{},
|
||||
24,
|
||||
},
|
||||
|
||||
@ -268,7 +268,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
pver,
|
||||
btcnet,
|
||||
len(exceedMaxPayloadBytes),
|
||||
&btcwire.MessageError{},
|
||||
&wire.MessageError{},
|
||||
24,
|
||||
},
|
||||
|
||||
@ -278,7 +278,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
pver,
|
||||
btcnet,
|
||||
len(badCommandBytes),
|
||||
&btcwire.MessageError{},
|
||||
&wire.MessageError{},
|
||||
24,
|
||||
},
|
||||
|
||||
@ -288,7 +288,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
pver,
|
||||
btcnet,
|
||||
len(unsupportedCommandBytes),
|
||||
&btcwire.MessageError{},
|
||||
&wire.MessageError{},
|
||||
24,
|
||||
},
|
||||
|
||||
@ -298,7 +298,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
pver,
|
||||
btcnet,
|
||||
len(exceedTypePayloadBytes),
|
||||
&btcwire.MessageError{},
|
||||
&wire.MessageError{},
|
||||
24,
|
||||
},
|
||||
|
||||
@ -318,7 +318,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
pver,
|
||||
btcnet,
|
||||
len(badChecksumBytes),
|
||||
&btcwire.MessageError{},
|
||||
&wire.MessageError{},
|
||||
26,
|
||||
},
|
||||
|
||||
@ -338,7 +338,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
pver,
|
||||
btcnet,
|
||||
len(discardBytes),
|
||||
&btcwire.MessageError{},
|
||||
&wire.MessageError{},
|
||||
24,
|
||||
},
|
||||
}
|
||||
@ -347,7 +347,7 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Decode from wire format.
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
nr, _, _, err := btcwire.ReadMessageN(r, test.pver, test.btcnet)
|
||||
nr, _, _, err := wire.ReadMessageN(r, test.pver, test.btcnet)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
t.Errorf("ReadMessage #%d wrong error got: %v <%T>, "+
|
||||
"want: %T", i, err, err, test.readErr)
|
||||
@ -360,9 +360,9 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
"got %d, want %d", i, nr, test.bytes)
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("ReadMessage #%d wrong error got: %v <%T>, "+
|
||||
"want: %v <%T>", i, err, err,
|
||||
@ -376,9 +376,9 @@ func TestReadMessageWireErrors(t *testing.T) {
|
||||
// TestWriteMessageWireErrors performs negative tests against wire encoding from
|
||||
// concrete messages to confirm error paths work correctly.
|
||||
func TestWriteMessageWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
btcnet := btcwire.MainNet
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
btcnet := wire.MainNet
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
// Fake message with a command that is too long.
|
||||
badCommandMsg := &fakeMessage{command: "somethingtoolong"}
|
||||
@ -387,7 +387,7 @@ func TestWriteMessageWireErrors(t *testing.T) {
|
||||
encodeErrMsg := &fakeMessage{forceEncodeErr: true}
|
||||
|
||||
// Fake message that has payload which exceeds max overall message size.
|
||||
exceedOverallPayload := make([]byte, btcwire.MaxMessagePayload+1)
|
||||
exceedOverallPayload := make([]byte, wire.MaxMessagePayload+1)
|
||||
exceedOverallPayloadErrMsg := &fakeMessage{payload: exceedOverallPayload}
|
||||
|
||||
// Fake message that has payload which exceeds max allowed per message.
|
||||
@ -400,21 +400,21 @@ func TestWriteMessageWireErrors(t *testing.T) {
|
||||
bogusMsg := &fakeMessage{command: "bogus", payload: bogusPayload}
|
||||
|
||||
tests := []struct {
|
||||
msg btcwire.Message // Message to encode
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
btcnet btcwire.BitcoinNet // Bitcoin network for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
err error // Expected error
|
||||
bytes int // Expected num bytes written
|
||||
msg wire.Message // Message to encode
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
btcnet wire.BitcoinNet // Bitcoin network for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
err error // Expected error
|
||||
bytes int // Expected num bytes written
|
||||
}{
|
||||
// Command too long.
|
||||
{badCommandMsg, pver, btcnet, 0, btcwireErr, 0},
|
||||
{badCommandMsg, pver, btcnet, 0, wireErr, 0},
|
||||
// Force error in payload encode.
|
||||
{encodeErrMsg, pver, btcnet, 0, btcwireErr, 0},
|
||||
{encodeErrMsg, pver, btcnet, 0, wireErr, 0},
|
||||
// Force error due to exceeding max overall message payload size.
|
||||
{exceedOverallPayloadErrMsg, pver, btcnet, 0, btcwireErr, 0},
|
||||
{exceedOverallPayloadErrMsg, pver, btcnet, 0, wireErr, 0},
|
||||
// Force error due to exceeding max payload for message type.
|
||||
{exceedPayloadErrMsg, pver, btcnet, 0, btcwireErr, 0},
|
||||
{exceedPayloadErrMsg, pver, btcnet, 0, wireErr, 0},
|
||||
// Force error in header write.
|
||||
{bogusMsg, pver, btcnet, 0, io.ErrShortWrite, 0},
|
||||
// Force error in payload write.
|
||||
@ -425,7 +425,7 @@ func TestWriteMessageWireErrors(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode wire format.
|
||||
w := newFixedWriter(test.max)
|
||||
nw, err := btcwire.WriteMessageN(w, test.msg, test.pver, test.btcnet)
|
||||
nw, err := wire.WriteMessageN(w, test.msg, test.pver, test.btcnet)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
||||
t.Errorf("WriteMessage #%d wrong error got: %v <%T>, "+
|
||||
"want: %T", i, err, err, test.err)
|
||||
@ -438,9 +438,9 @@ func TestWriteMessageWireErrors(t *testing.T) {
|
||||
"written - got %d, want %d", i, nw, test.bytes)
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.err {
|
||||
t.Errorf("ReadMessage #%d wrong error got: %v <%T>, "+
|
||||
"want: %v <%T>", i, err, err,
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -12,17 +12,17 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestAddr tests the MsgAddr API.
|
||||
func TestAddr(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "addr"
|
||||
msg := btcwire.NewMsgAddr()
|
||||
msg := wire.NewMsgAddr()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgAddr: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -40,7 +40,7 @@ func TestAddr(t *testing.T) {
|
||||
|
||||
// Ensure NetAddresses are added properly.
|
||||
tcpAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
|
||||
na, err := btcwire.NewNetAddress(tcpAddr, btcwire.SFNodeNetwork)
|
||||
na, err := wire.NewNetAddress(tcpAddr, wire.SFNodeNetwork)
|
||||
if err != nil {
|
||||
t.Errorf("NewNetAddress: %v", err)
|
||||
}
|
||||
@ -63,7 +63,7 @@ func TestAddr(t *testing.T) {
|
||||
|
||||
// Ensure adding more than the max allowed addresses per message returns
|
||||
// error.
|
||||
for i := 0; i < btcwire.MaxAddrPerMsg+1; i++ {
|
||||
for i := 0; i < wire.MaxAddrPerMsg+1; i++ {
|
||||
err = msg.AddAddress(na)
|
||||
}
|
||||
if err == nil {
|
||||
@ -79,7 +79,7 @@ func TestAddr(t *testing.T) {
|
||||
// Ensure max payload is expected value for protocol versions before
|
||||
// timestamp was added to NetAddress.
|
||||
// Num addresses (varInt) + max allowed addresses.
|
||||
pver = btcwire.NetAddressTimeVersion - 1
|
||||
pver = wire.NetAddressTimeVersion - 1
|
||||
wantPayload = uint32(26009)
|
||||
maxPayload = msg.MaxPayloadLength(pver)
|
||||
if maxPayload != wantPayload {
|
||||
@ -91,7 +91,7 @@ func TestAddr(t *testing.T) {
|
||||
// Ensure max payload is expected value for protocol versions before
|
||||
// multiple addresses were allowed.
|
||||
// Num addresses (varInt) + a single net addresses.
|
||||
pver = btcwire.MultipleAddressVersion - 1
|
||||
pver = wire.MultipleAddressVersion - 1
|
||||
wantPayload = uint32(35)
|
||||
maxPayload = msg.MaxPayloadLength(pver)
|
||||
if maxPayload != wantPayload {
|
||||
@ -107,27 +107,27 @@ func TestAddr(t *testing.T) {
|
||||
// of addreses and protocol versions.
|
||||
func TestAddrWire(t *testing.T) {
|
||||
// A couple of NetAddresses to use for testing.
|
||||
na := &btcwire.NetAddress{
|
||||
na := &wire.NetAddress{
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: 8333,
|
||||
}
|
||||
na2 := &btcwire.NetAddress{
|
||||
na2 := &wire.NetAddress{
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("192.168.0.1"),
|
||||
Port: 8334,
|
||||
}
|
||||
|
||||
// Empty address message.
|
||||
noAddr := btcwire.NewMsgAddr()
|
||||
noAddr := wire.NewMsgAddr()
|
||||
noAddrEncoded := []byte{
|
||||
0x00, // Varint for number of addresses
|
||||
}
|
||||
|
||||
// Address message with multiple addresses.
|
||||
multiAddr := btcwire.NewMsgAddr()
|
||||
multiAddr := wire.NewMsgAddr()
|
||||
multiAddr.AddAddresses(na, na2)
|
||||
multiAddrEncoded := []byte{
|
||||
0x02, // Varint for number of addresses
|
||||
@ -145,17 +145,17 @@ func TestAddrWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgAddr // Message to encode
|
||||
out *btcwire.MsgAddr // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgAddr // Message to encode
|
||||
out *wire.MsgAddr // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version with no addresses.
|
||||
{
|
||||
noAddr,
|
||||
noAddr,
|
||||
noAddrEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with multiple addresses.
|
||||
@ -163,7 +163,7 @@ func TestAddrWire(t *testing.T) {
|
||||
multiAddr,
|
||||
multiAddr,
|
||||
multiAddrEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion-1 with no addresses.
|
||||
@ -171,7 +171,7 @@ func TestAddrWire(t *testing.T) {
|
||||
noAddr,
|
||||
noAddr,
|
||||
noAddrEncoded,
|
||||
btcwire.MultipleAddressVersion - 1,
|
||||
wire.MultipleAddressVersion - 1,
|
||||
},
|
||||
}
|
||||
|
||||
@ -191,7 +191,7 @@ func TestAddrWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgAddr
|
||||
var msg wire.MsgAddr
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -209,26 +209,26 @@ func TestAddrWire(t *testing.T) {
|
||||
// TestAddrWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgAddr to confirm error paths work correctly.
|
||||
func TestAddrWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pverMA := btcwire.MultipleAddressVersion
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
pverMA := wire.MultipleAddressVersion
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
// A couple of NetAddresses to use for testing.
|
||||
na := &btcwire.NetAddress{
|
||||
na := &wire.NetAddress{
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: 8333,
|
||||
}
|
||||
na2 := &btcwire.NetAddress{
|
||||
na2 := &wire.NetAddress{
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("192.168.0.1"),
|
||||
Port: 8334,
|
||||
}
|
||||
|
||||
// Address message with multiple addresses.
|
||||
baseAddr := btcwire.NewMsgAddr()
|
||||
baseAddr := wire.NewMsgAddr()
|
||||
baseAddr.AddAddresses(na, na2)
|
||||
baseAddrEncoded := []byte{
|
||||
0x02, // Varint for number of addresses
|
||||
@ -247,8 +247,8 @@ func TestAddrWireErrors(t *testing.T) {
|
||||
|
||||
// Message that forces an error by having more than the max allowed
|
||||
// addresses.
|
||||
maxAddr := btcwire.NewMsgAddr()
|
||||
for i := 0; i < btcwire.MaxAddrPerMsg; i++ {
|
||||
maxAddr := wire.NewMsgAddr()
|
||||
for i := 0; i < wire.MaxAddrPerMsg; i++ {
|
||||
maxAddr.AddAddress(na)
|
||||
}
|
||||
maxAddr.AddrList = append(maxAddr.AddrList, na)
|
||||
@ -257,12 +257,12 @@ func TestAddrWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgAddr // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgAddr // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with intentional read/write errors.
|
||||
// Force error in addresses count
|
||||
@ -270,10 +270,10 @@ func TestAddrWireErrors(t *testing.T) {
|
||||
// Force error in address list.
|
||||
{baseAddr, baseAddrEncoded, pver, 1, io.ErrShortWrite, io.EOF},
|
||||
// Force error with greater than max inventory vectors.
|
||||
{maxAddr, maxAddrEncoded, pver, 3, btcwireErr, btcwireErr},
|
||||
{maxAddr, maxAddrEncoded, pver, 3, wireErr, wireErr},
|
||||
// Force error with greater than max inventory vectors for
|
||||
// protocol versions before multiple addresses were allowed.
|
||||
{maxAddr, maxAddrEncoded, pverMA - 1, 3, btcwireErr, btcwireErr},
|
||||
{maxAddr, maxAddrEncoded, pverMA - 1, 3, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -287,9 +287,9 @@ func TestAddrWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -298,7 +298,7 @@ func TestAddrWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgAddr
|
||||
var msg wire.MsgAddr
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -307,9 +307,9 @@ func TestAddrWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,18 +10,18 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestMsgAlert tests the MsgAlert API.
|
||||
func TestMsgAlert(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
serializedpayload := []byte("some message")
|
||||
signature := []byte("some sig")
|
||||
|
||||
// Ensure we get the same payload and signature back out.
|
||||
msg := btcwire.NewMsgAlert(serializedpayload, signature)
|
||||
msg := wire.NewMsgAlert(serializedpayload, signature)
|
||||
if !reflect.DeepEqual(msg.SerializedPayload, serializedpayload) {
|
||||
t.Errorf("NewMsgAlert: wrong serializedpayload - got %v, want %v",
|
||||
msg.SerializedPayload, serializedpayload)
|
||||
@ -64,7 +64,7 @@ func TestMsgAlert(t *testing.T) {
|
||||
|
||||
// Test BtcEncode with Payload != nil
|
||||
// note: Payload is an empty Alert but not nil
|
||||
msg.Payload = new(btcwire.Alert)
|
||||
msg.Payload = new(wire.Alert)
|
||||
buf = *new(bytes.Buffer)
|
||||
err = msg.BtcEncode(&buf, pver)
|
||||
if err != nil {
|
||||
@ -85,7 +85,7 @@ func TestMsgAlert(t *testing.T) {
|
||||
// TestMsgAlertWire tests the MsgAlert wire encode and decode for various protocol
|
||||
// versions.
|
||||
func TestMsgAlertWire(t *testing.T) {
|
||||
baseMsgAlert := btcwire.NewMsgAlert([]byte("some payload"), []byte("somesig"))
|
||||
baseMsgAlert := wire.NewMsgAlert([]byte("some payload"), []byte("somesig"))
|
||||
baseMsgAlertEncoded := []byte{
|
||||
0x0c, // Varint for payload length
|
||||
0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x61, 0x79,
|
||||
@ -95,17 +95,17 @@ func TestMsgAlertWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgAlert // Message to encode
|
||||
out *btcwire.MsgAlert // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgAlert // Message to encode
|
||||
out *wire.MsgAlert // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
baseMsgAlert,
|
||||
baseMsgAlert,
|
||||
baseMsgAlertEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version.
|
||||
@ -113,7 +113,7 @@ func TestMsgAlertWire(t *testing.T) {
|
||||
baseMsgAlert,
|
||||
baseMsgAlert,
|
||||
baseMsgAlertEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version.
|
||||
@ -121,7 +121,7 @@ func TestMsgAlertWire(t *testing.T) {
|
||||
baseMsgAlert,
|
||||
baseMsgAlert,
|
||||
baseMsgAlertEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion.
|
||||
@ -129,7 +129,7 @@ func TestMsgAlertWire(t *testing.T) {
|
||||
baseMsgAlert,
|
||||
baseMsgAlert,
|
||||
baseMsgAlertEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion.
|
||||
@ -137,7 +137,7 @@ func TestMsgAlertWire(t *testing.T) {
|
||||
baseMsgAlert,
|
||||
baseMsgAlert,
|
||||
baseMsgAlertEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -157,7 +157,7 @@ func TestMsgAlertWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgAlert
|
||||
var msg wire.MsgAlert
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -175,9 +175,9 @@ func TestMsgAlertWire(t *testing.T) {
|
||||
// TestMsgAlertWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgAlert to confirm error paths work correctly.
|
||||
func TestMsgAlertWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
baseMsgAlert := btcwire.NewMsgAlert([]byte("some payload"), []byte("somesig"))
|
||||
baseMsgAlert := wire.NewMsgAlert([]byte("some payload"), []byte("somesig"))
|
||||
baseMsgAlertEncoded := []byte{
|
||||
0x0c, // Varint for payload length
|
||||
0x73, 0x6f, 0x6d, 0x65, 0x20, 0x70, 0x61, 0x79,
|
||||
@ -187,12 +187,12 @@ func TestMsgAlertWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgAlert // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgAlert // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in payload length.
|
||||
{baseMsgAlert, baseMsgAlertEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -215,9 +215,9 @@ func TestMsgAlertWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -226,7 +226,7 @@ func TestMsgAlertWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgAlert
|
||||
var msg wire.MsgAlert
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -235,9 +235,9 @@ func TestMsgAlertWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
||||
@ -250,38 +250,38 @@ func TestMsgAlertWireErrors(t *testing.T) {
|
||||
baseMsgAlert.SerializedPayload = []byte{}
|
||||
w := new(bytes.Buffer)
|
||||
err := baseMsgAlert.BtcEncode(w, pver)
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
t.Errorf("MsgAlert.BtcEncode wrong error got: %T, want: %T",
|
||||
err, btcwire.MessageError{})
|
||||
err, wire.MessageError{})
|
||||
}
|
||||
|
||||
// Test Payload Serialize error
|
||||
// overflow the max number of elements in SetCancel
|
||||
baseMsgAlert.Payload = new(btcwire.Alert)
|
||||
baseMsgAlert.Payload.SetCancel = make([]int32, btcwire.MaxCountSetCancel+1)
|
||||
baseMsgAlert.Payload = new(wire.Alert)
|
||||
baseMsgAlert.Payload.SetCancel = make([]int32, wire.MaxCountSetCancel+1)
|
||||
buf := *new(bytes.Buffer)
|
||||
err = baseMsgAlert.BtcEncode(&buf, pver)
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
t.Errorf("MsgAlert.BtcEncode wrong error got: %T, want: %T",
|
||||
err, btcwire.MessageError{})
|
||||
err, wire.MessageError{})
|
||||
}
|
||||
|
||||
// overflow the max number of elements in SetSubVer
|
||||
baseMsgAlert.Payload = new(btcwire.Alert)
|
||||
baseMsgAlert.Payload.SetSubVer = make([]string, btcwire.MaxCountSetSubVer+1)
|
||||
baseMsgAlert.Payload = new(wire.Alert)
|
||||
baseMsgAlert.Payload.SetSubVer = make([]string, wire.MaxCountSetSubVer+1)
|
||||
buf = *new(bytes.Buffer)
|
||||
err = baseMsgAlert.BtcEncode(&buf, pver)
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
t.Errorf("MsgAlert.BtcEncode wrong error got: %T, want: %T",
|
||||
err, btcwire.MessageError{})
|
||||
err, wire.MessageError{})
|
||||
}
|
||||
}
|
||||
|
||||
// TestAlert tests serialization and deserialization
|
||||
// of the payload to Alert
|
||||
func TestAlert(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
alert := btcwire.NewAlert(
|
||||
pver := wire.ProtocolVersion
|
||||
alert := wire.NewAlert(
|
||||
1, 1337093712, 1368628812, 1015,
|
||||
1013, []int32{1014}, 0, 40599, []string{"/Satoshi:0.7.2/"}, 5000, "",
|
||||
"URGENT: upgrade required, see http://bitcoin.org/dos for details",
|
||||
@ -292,7 +292,7 @@ func TestAlert(t *testing.T) {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
serializedpayload := w.Bytes()
|
||||
newAlert, err := btcwire.NewAlertFromPayload(serializedpayload, pver)
|
||||
newAlert, err := wire.NewAlertFromPayload(serializedpayload, pver)
|
||||
if err != nil {
|
||||
t.Error(err.Error())
|
||||
}
|
||||
@ -366,9 +366,9 @@ func TestAlert(t *testing.T) {
|
||||
// TestAlertErrors performs negative tests against payload serialization,
|
||||
// deserialization of Alert to confirm error paths work correctly.
|
||||
func TestAlertErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
baseAlert := btcwire.NewAlert(
|
||||
baseAlert := wire.NewAlert(
|
||||
1, 1337093712, 1368628812, 1015,
|
||||
1013, []int32{1014}, 0, 40599, []string{"/Satoshi:0.7.2/"}, 5000, "",
|
||||
"URGENT",
|
||||
@ -381,12 +381,12 @@ func TestAlertErrors(t *testing.T) {
|
||||
0x55, 0x52, 0x47, 0x45, 0x4e, 0x54, 0x00, //|URGENT.|
|
||||
}
|
||||
tests := []struct {
|
||||
in *btcwire.Alert // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.Alert // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in Version
|
||||
{baseAlert, baseAlertEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -420,7 +420,7 @@ func TestAlertErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
var alert btcwire.Alert
|
||||
var alert wire.Alert
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = alert.Deserialize(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -440,12 +440,12 @@ func TestAlertErrors(t *testing.T) {
|
||||
0x73, 0x68, 0x69, 0x3a, 0x30, 0x2e, 0x37, 0x2e, 0x32, 0x2f, 0x88, 0x13, 0x00, 0x00, 0x00, 0x06, //|shi:0.7.2/......|
|
||||
0x55, 0x52, 0x47, 0x45, 0x4e, 0x54, 0x00, //|URGENT.|
|
||||
}
|
||||
var alert btcwire.Alert
|
||||
var alert wire.Alert
|
||||
r := bytes.NewReader(badAlertEncoded)
|
||||
err := alert.Deserialize(r, pver)
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
t.Errorf("Alert.Deserialize wrong error got: %T, want: %T",
|
||||
err, btcwire.MessageError{})
|
||||
err, wire.MessageError{})
|
||||
}
|
||||
|
||||
// overflow the max number of elements in SetSubVer
|
||||
@ -460,8 +460,8 @@ func TestAlertErrors(t *testing.T) {
|
||||
}
|
||||
r = bytes.NewReader(badAlertEncoded)
|
||||
err = alert.Deserialize(r, pver)
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
t.Errorf("Alert.Deserialize wrong error got: %T, want: %T",
|
||||
err, btcwire.MessageError{})
|
||||
err, wire.MessageError{})
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -11,24 +11,24 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestBlock tests the MsgBlock API.
|
||||
func TestBlock(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Block 1 header.
|
||||
prevHash := &blockOne.Header.PrevBlock
|
||||
merkleHash := &blockOne.Header.MerkleRoot
|
||||
bits := blockOne.Header.Bits
|
||||
nonce := blockOne.Header.Nonce
|
||||
bh := btcwire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
|
||||
bh := wire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "block"
|
||||
msg := btcwire.NewMsgBlock(bh)
|
||||
msg := wire.NewMsgBlock(bh)
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgBlock: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -74,13 +74,13 @@ func TestBlock(t *testing.T) {
|
||||
func TestBlockTxShas(t *testing.T) {
|
||||
// Block 1, transaction 1 hash.
|
||||
hashStr := "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098"
|
||||
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
wantHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
wantShas := []btcwire.ShaHash{*wantHash}
|
||||
wantShas := []wire.ShaHash{*wantHash}
|
||||
shas, err := blockOne.TxShas()
|
||||
if err != nil {
|
||||
t.Errorf("TxShas: %v", err)
|
||||
@ -95,7 +95,7 @@ func TestBlockTxShas(t *testing.T) {
|
||||
func TestBlockSha(t *testing.T) {
|
||||
// Block 1 hash.
|
||||
hashStr := "839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
|
||||
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
wantHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
@ -115,11 +115,11 @@ func TestBlockSha(t *testing.T) {
|
||||
// of transaction inputs and outputs and protocol versions.
|
||||
func TestBlockWire(t *testing.T) {
|
||||
tests := []struct {
|
||||
in *btcwire.MsgBlock // Message to encode
|
||||
out *btcwire.MsgBlock // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
txLocs []btcwire.TxLoc // Expected transaction locations
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgBlock // Message to encode
|
||||
out *wire.MsgBlock // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
txLocs []wire.TxLoc // Expected transaction locations
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
@ -127,7 +127,7 @@ func TestBlockWire(t *testing.T) {
|
||||
&blockOne,
|
||||
blockOneBytes,
|
||||
blockOneTxLocs,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version.
|
||||
@ -136,7 +136,7 @@ func TestBlockWire(t *testing.T) {
|
||||
&blockOne,
|
||||
blockOneBytes,
|
||||
blockOneTxLocs,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version.
|
||||
@ -145,7 +145,7 @@ func TestBlockWire(t *testing.T) {
|
||||
&blockOne,
|
||||
blockOneBytes,
|
||||
blockOneTxLocs,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion.
|
||||
@ -154,7 +154,7 @@ func TestBlockWire(t *testing.T) {
|
||||
&blockOne,
|
||||
blockOneBytes,
|
||||
blockOneTxLocs,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion.
|
||||
@ -163,7 +163,7 @@ func TestBlockWire(t *testing.T) {
|
||||
&blockOne,
|
||||
blockOneBytes,
|
||||
blockOneTxLocs,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -183,7 +183,7 @@ func TestBlockWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgBlock
|
||||
var msg wire.MsgBlock
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -207,12 +207,12 @@ func TestBlockWireErrors(t *testing.T) {
|
||||
pver := uint32(60002)
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgBlock // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgBlock // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in version.
|
||||
{&blockOne, blockOneBytes, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -244,7 +244,7 @@ func TestBlockWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgBlock
|
||||
var msg wire.MsgBlock
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if err != test.readErr {
|
||||
@ -258,10 +258,10 @@ func TestBlockWireErrors(t *testing.T) {
|
||||
// TestBlockSerialize tests MsgBlock serialize and deserialize.
|
||||
func TestBlockSerialize(t *testing.T) {
|
||||
tests := []struct {
|
||||
in *btcwire.MsgBlock // Message to encode
|
||||
out *btcwire.MsgBlock // Expected decoded message
|
||||
buf []byte // Serialized data
|
||||
txLocs []btcwire.TxLoc // Expected transaction locations
|
||||
in *wire.MsgBlock // Message to encode
|
||||
out *wire.MsgBlock // Expected decoded message
|
||||
buf []byte // Serialized data
|
||||
txLocs []wire.TxLoc // Expected transaction locations
|
||||
}{
|
||||
{
|
||||
&blockOne,
|
||||
@ -287,7 +287,7 @@ func TestBlockSerialize(t *testing.T) {
|
||||
}
|
||||
|
||||
// Deserialize the block.
|
||||
var block btcwire.MsgBlock
|
||||
var block wire.MsgBlock
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = block.Deserialize(rbuf)
|
||||
if err != nil {
|
||||
@ -302,7 +302,7 @@ func TestBlockSerialize(t *testing.T) {
|
||||
|
||||
// Deserialize the block while gathering transaction location
|
||||
// information.
|
||||
var txLocBlock btcwire.MsgBlock
|
||||
var txLocBlock wire.MsgBlock
|
||||
br := bytes.NewBuffer(test.buf)
|
||||
txLocs, err := txLocBlock.DeserializeTxLoc(br)
|
||||
if err != nil {
|
||||
@ -326,11 +326,11 @@ func TestBlockSerialize(t *testing.T) {
|
||||
// decode of MsgBlock to confirm error paths work correctly.
|
||||
func TestBlockSerializeErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
in *btcwire.MsgBlock // Value to encode
|
||||
buf []byte // Serialized data
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgBlock // Value to encode
|
||||
buf []byte // Serialized data
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in version.
|
||||
{&blockOne, blockOneBytes, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -362,7 +362,7 @@ func TestBlockSerializeErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Deserialize the block.
|
||||
var block btcwire.MsgBlock
|
||||
var block wire.MsgBlock
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = block.Deserialize(r)
|
||||
if err != test.readErr {
|
||||
@ -371,7 +371,7 @@ func TestBlockSerializeErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
var txLocBlock btcwire.MsgBlock
|
||||
var txLocBlock wire.MsgBlock
|
||||
br := bytes.NewBuffer(test.buf[0:test.max])
|
||||
_, err = txLocBlock.DeserializeTxLoc(br)
|
||||
if err != test.readErr {
|
||||
@ -414,14 +414,14 @@ func TestBlockOverflowErrors(t *testing.T) {
|
||||
0x01, 0xe3, 0x62, 0x99, // Nonce
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, // TxnCount
|
||||
}, pver, &btcwire.MessageError{},
|
||||
}, pver, &wire.MessageError{},
|
||||
},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgBlock
|
||||
var msg wire.MsgBlock
|
||||
r := bytes.NewReader(test.buf)
|
||||
err := msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
||||
@ -454,11 +454,11 @@ func TestBlockOverflowErrors(t *testing.T) {
|
||||
// various blocks is accurate.
|
||||
func TestBlockSerializeSize(t *testing.T) {
|
||||
// Block with no transactions.
|
||||
noTxBlock := btcwire.NewMsgBlock(&blockOne.Header)
|
||||
noTxBlock := wire.NewMsgBlock(&blockOne.Header)
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgBlock // Block to encode
|
||||
size int // Expected serialized size
|
||||
in *wire.MsgBlock // Block to encode
|
||||
size int // Expected serialized size
|
||||
}{
|
||||
// Block with no transactions.
|
||||
{noTxBlock, 81},
|
||||
@ -478,16 +478,16 @@ func TestBlockSerializeSize(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var blockOne = btcwire.MsgBlock{
|
||||
Header: btcwire.BlockHeader{
|
||||
var blockOne = wire.MsgBlock{
|
||||
Header: wire.BlockHeader{
|
||||
Version: 1,
|
||||
PrevBlock: btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
PrevBlock: wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
||||
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
||||
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
||||
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
}),
|
||||
MerkleRoot: btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
MerkleRoot: wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
|
||||
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
|
||||
0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
|
||||
@ -498,13 +498,13 @@ var blockOne = btcwire.MsgBlock{
|
||||
Bits: 0x1d00ffff, // 486604799
|
||||
Nonce: 0x9962e301, // 2573394689
|
||||
},
|
||||
Transactions: []*btcwire.MsgTx{
|
||||
Transactions: []*wire.MsgTx{
|
||||
{
|
||||
Version: 1,
|
||||
TxIn: []*btcwire.TxIn{
|
||||
TxIn: []*wire.TxIn{
|
||||
{
|
||||
PreviousOutPoint: btcwire.OutPoint{
|
||||
Hash: btcwire.ShaHash{},
|
||||
PreviousOutPoint: wire.OutPoint{
|
||||
Hash: wire.ShaHash{},
|
||||
Index: 0xffffffff,
|
||||
},
|
||||
SignatureScript: []byte{
|
||||
@ -513,7 +513,7 @@ var blockOne = btcwire.MsgBlock{
|
||||
Sequence: 0xffffffff,
|
||||
},
|
||||
},
|
||||
TxOut: []*btcwire.TxOut{
|
||||
TxOut: []*wire.TxOut{
|
||||
{
|
||||
Value: 0x12a05f200,
|
||||
PkScript: []byte{
|
||||
@ -579,6 +579,6 @@ var blockOneBytes = []byte{
|
||||
}
|
||||
|
||||
// Transaction location information for block one transactions.
|
||||
var blockOneTxLocs = []btcwire.TxLoc{
|
||||
var blockOneTxLocs = []wire.TxLoc{
|
||||
{TxStart: 81, TxLen: 134},
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,16 +10,16 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// TestFilterAddLatest tests the MsgFilterAdd API against the latest protocol
|
||||
// version.
|
||||
func TestFilterAddLatest(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
data := []byte{0x01, 0x02}
|
||||
msg := btcwire.NewMsgFilterAdd(data)
|
||||
msg := wire.NewMsgFilterAdd(data)
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "filteradd"
|
||||
@ -45,7 +45,7 @@ func TestFilterAddLatest(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test decode with latest protocol version.
|
||||
var readmsg btcwire.MsgFilterAdd
|
||||
var readmsg wire.MsgFilterAdd
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgFilterAdd failed [%v] err <%v>", buf, err)
|
||||
@ -58,21 +58,21 @@ func TestFilterAddLatest(t *testing.T) {
|
||||
// latest protocol version and decoding with BIP0031Version.
|
||||
func TestFilterAddCrossProtocol(t *testing.T) {
|
||||
data := []byte{0x01, 0x02}
|
||||
msg := btcwire.NewMsgFilterAdd(data)
|
||||
msg := wire.NewMsgFilterAdd(data)
|
||||
if !bytes.Equal(msg.Data, data) {
|
||||
t.Errorf("should get same data back out")
|
||||
}
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err != nil {
|
||||
t.Errorf("encode of MsgFilterAdd failed %v err <%v>", msg, err)
|
||||
}
|
||||
|
||||
// Decode with old protocol version.
|
||||
var readmsg btcwire.MsgFilterAdd
|
||||
err = readmsg.BtcDecode(&buf, btcwire.BIP0031Version)
|
||||
var readmsg wire.MsgFilterAdd
|
||||
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgFilterAdd succeeded when it shouldn't "+
|
||||
"have %v", msg)
|
||||
@ -89,11 +89,11 @@ func TestFilterAddCrossProtocol(t *testing.T) {
|
||||
// TestFilterAddMaxDataSize tests the MsgFilterAdd API maximum data size.
|
||||
func TestFilterAddMaxDataSize(t *testing.T) {
|
||||
data := bytes.Repeat([]byte{0xff}, 521)
|
||||
msg := btcwire.NewMsgFilterAdd(data)
|
||||
msg := wire.NewMsgFilterAdd(data)
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err == nil {
|
||||
t.Errorf("encode of MsgFilterAdd succeeded when it shouldn't "+
|
||||
"have %v", msg)
|
||||
@ -101,7 +101,7 @@ func TestFilterAddMaxDataSize(t *testing.T) {
|
||||
|
||||
// Decode with latest protocol version.
|
||||
readbuf := bytes.NewReader(data)
|
||||
err = msg.BtcDecode(readbuf, btcwire.ProtocolVersion)
|
||||
err = msg.BtcDecode(readbuf, wire.ProtocolVersion)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgFilterAdd succeeded when it shouldn't "+
|
||||
"have %v", msg)
|
||||
@ -111,21 +111,21 @@ func TestFilterAddMaxDataSize(t *testing.T) {
|
||||
// TestFilterAddWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgFilterAdd to confirm error paths work correctly.
|
||||
func TestFilterAddWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pverNoFilterAdd := btcwire.BIP0037Version - 1
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
pverNoFilterAdd := wire.BIP0037Version - 1
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
baseData := []byte{0x01, 0x02, 0x03, 0x04}
|
||||
baseFilterAdd := btcwire.NewMsgFilterAdd(baseData)
|
||||
baseFilterAdd := wire.NewMsgFilterAdd(baseData)
|
||||
baseFilterAddEncoded := append([]byte{0x04}, baseData...)
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgFilterAdd // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgFilterAdd // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with intentional read/write errors.
|
||||
// Force error in data size.
|
||||
@ -141,7 +141,7 @@ func TestFilterAddWireErrors(t *testing.T) {
|
||||
// Force error due to unsupported protocol version.
|
||||
{
|
||||
baseFilterAdd, baseFilterAddEncoded, pverNoFilterAdd, 5,
|
||||
btcwireErr, btcwireErr,
|
||||
wireErr, wireErr,
|
||||
},
|
||||
}
|
||||
|
||||
@ -156,9 +156,9 @@ func TestFilterAddWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -167,7 +167,7 @@ func TestFilterAddWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgFilterAdd
|
||||
var msg wire.MsgFilterAdd
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -176,9 +176,9 @@ func TestFilterAddWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,24 +1,24 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestFilterCLearLatest tests the MsgFilterClear API against the latest
|
||||
// protocol version.
|
||||
func TestFilterClearLatest(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
msg := btcwire.NewMsgFilterClear()
|
||||
msg := wire.NewMsgFilterClear()
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "filterclear"
|
||||
@ -42,18 +42,18 @@ func TestFilterClearLatest(t *testing.T) {
|
||||
// TestFilterClearCrossProtocol tests the MsgFilterClear API when encoding with
|
||||
// the latest protocol version and decoding with BIP0031Version.
|
||||
func TestFilterClearCrossProtocol(t *testing.T) {
|
||||
msg := btcwire.NewMsgFilterClear()
|
||||
msg := wire.NewMsgFilterClear()
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err != nil {
|
||||
t.Errorf("encode of MsgFilterClear failed %v err <%v>", msg, err)
|
||||
}
|
||||
|
||||
// Decode with old protocol version.
|
||||
var readmsg btcwire.MsgFilterClear
|
||||
err = readmsg.BtcDecode(&buf, btcwire.BIP0031Version)
|
||||
var readmsg wire.MsgFilterClear
|
||||
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgFilterClear succeeded when it "+
|
||||
"shouldn't have %v", msg)
|
||||
@ -63,21 +63,21 @@ func TestFilterClearCrossProtocol(t *testing.T) {
|
||||
// TestFilterClearWire tests the MsgFilterClear wire encode and decode for
|
||||
// various protocol versions.
|
||||
func TestFilterClearWire(t *testing.T) {
|
||||
msgFilterClear := btcwire.NewMsgFilterClear()
|
||||
msgFilterClear := wire.NewMsgFilterClear()
|
||||
msgFilterClearEncoded := []byte{}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgFilterClear // Message to encode
|
||||
out *btcwire.MsgFilterClear // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgFilterClear // Message to encode
|
||||
out *wire.MsgFilterClear // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
msgFilterClear,
|
||||
msgFilterClear,
|
||||
msgFilterClearEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0037Version + 1.
|
||||
@ -85,7 +85,7 @@ func TestFilterClearWire(t *testing.T) {
|
||||
msgFilterClear,
|
||||
msgFilterClear,
|
||||
msgFilterClearEncoded,
|
||||
btcwire.BIP0037Version + 1,
|
||||
wire.BIP0037Version + 1,
|
||||
},
|
||||
|
||||
// Protocol version BIP0037Version.
|
||||
@ -93,7 +93,7 @@ func TestFilterClearWire(t *testing.T) {
|
||||
msgFilterClear,
|
||||
msgFilterClear,
|
||||
msgFilterClearEncoded,
|
||||
btcwire.BIP0037Version,
|
||||
wire.BIP0037Version,
|
||||
},
|
||||
}
|
||||
|
||||
@ -113,7 +113,7 @@ func TestFilterClearWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgFilterClear
|
||||
var msg wire.MsgFilterClear
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -131,24 +131,24 @@ func TestFilterClearWire(t *testing.T) {
|
||||
// TestFilterClearWireErrors performs negative tests against wire encode and
|
||||
// decode of MsgFilterClear to confirm error paths work correctly.
|
||||
func TestFilterClearWireErrors(t *testing.T) {
|
||||
pverNoFilterClear := btcwire.BIP0037Version - 1
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pverNoFilterClear := wire.BIP0037Version - 1
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
baseFilterClear := btcwire.NewMsgFilterClear()
|
||||
baseFilterClear := wire.NewMsgFilterClear()
|
||||
baseFilterClearEncoded := []byte{}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgFilterClear // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgFilterClear // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error due to unsupported protocol version.
|
||||
{
|
||||
baseFilterClear, baseFilterClearEncoded,
|
||||
pverNoFilterClear, 4, btcwireErr, btcwireErr,
|
||||
pverNoFilterClear, 4, wireErr, wireErr,
|
||||
},
|
||||
}
|
||||
|
||||
@ -163,9 +163,9 @@ func TestFilterClearWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -174,7 +174,7 @@ func TestFilterClearWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgFilterClear
|
||||
var msg wire.MsgFilterClear
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -183,9 +183,9 @@ func TestFilterClearWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,16 +10,16 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// TestFilterCLearLatest tests the MsgFilterLoad API against the latest protocol
|
||||
// version.
|
||||
func TestFilterLoadLatest(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
data := []byte{0x01, 0x02}
|
||||
msg := btcwire.NewMsgFilterLoad(data, 10, 0, 0)
|
||||
msg := wire.NewMsgFilterLoad(data, 10, 0, 0)
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "filterload"
|
||||
@ -45,7 +45,7 @@ func TestFilterLoadLatest(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test decode with latest protocol version.
|
||||
readmsg := btcwire.MsgFilterLoad{}
|
||||
readmsg := wire.MsgFilterLoad{}
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgFilterLoad failed [%v] err <%v>", buf, err)
|
||||
@ -58,19 +58,19 @@ func TestFilterLoadLatest(t *testing.T) {
|
||||
// the latest protocol version and decoding with BIP0031Version.
|
||||
func TestFilterLoadCrossProtocol(t *testing.T) {
|
||||
data := []byte{0x01, 0x02}
|
||||
msg := btcwire.NewMsgFilterLoad(data, 10, 0, 0)
|
||||
msg := wire.NewMsgFilterLoad(data, 10, 0, 0)
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err != nil {
|
||||
t.Errorf("encode of NewMsgFilterLoad failed %v err <%v>", msg,
|
||||
err)
|
||||
}
|
||||
|
||||
// Decode with old protocol version.
|
||||
var readmsg btcwire.MsgFilterLoad
|
||||
err = readmsg.BtcDecode(&buf, btcwire.BIP0031Version)
|
||||
var readmsg wire.MsgFilterLoad
|
||||
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
|
||||
msg)
|
||||
@ -80,11 +80,11 @@ func TestFilterLoadCrossProtocol(t *testing.T) {
|
||||
// TestFilterLoadMaxFilterSize tests the MsgFilterLoad API maximum filter size.
|
||||
func TestFilterLoadMaxFilterSize(t *testing.T) {
|
||||
data := bytes.Repeat([]byte{0xff}, 36001)
|
||||
msg := btcwire.NewMsgFilterLoad(data, 10, 0, 0)
|
||||
msg := wire.NewMsgFilterLoad(data, 10, 0, 0)
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err == nil {
|
||||
t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't "+
|
||||
"have %v", msg)
|
||||
@ -92,7 +92,7 @@ func TestFilterLoadMaxFilterSize(t *testing.T) {
|
||||
|
||||
// Decode with latest protocol version.
|
||||
readbuf := bytes.NewReader(data)
|
||||
err = msg.BtcDecode(readbuf, btcwire.ProtocolVersion)
|
||||
err = msg.BtcDecode(readbuf, wire.ProtocolVersion)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't "+
|
||||
"have %v", msg)
|
||||
@ -102,11 +102,11 @@ func TestFilterLoadMaxFilterSize(t *testing.T) {
|
||||
// TestFilterLoadMaxHashFuncsSize tests the MsgFilterLoad API maximum hash functions.
|
||||
func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
|
||||
data := bytes.Repeat([]byte{0xff}, 10)
|
||||
msg := btcwire.NewMsgFilterLoad(data, 61, 0, 0)
|
||||
msg := wire.NewMsgFilterLoad(data, 61, 0, 0)
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err == nil {
|
||||
t.Errorf("encode of MsgFilterLoad succeeded when it shouldn't have %v",
|
||||
msg)
|
||||
@ -121,7 +121,7 @@ func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
|
||||
}
|
||||
// Decode with latest protocol version.
|
||||
readbuf := bytes.NewReader(newBuf)
|
||||
err = msg.BtcDecode(readbuf, btcwire.ProtocolVersion)
|
||||
err = msg.BtcDecode(readbuf, wire.ProtocolVersion)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
|
||||
msg)
|
||||
@ -131,13 +131,13 @@ func TestFilterLoadMaxHashFuncsSize(t *testing.T) {
|
||||
// TestFilterLoadWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgFilterLoad to confirm error paths work correctly.
|
||||
func TestFilterLoadWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pverNoFilterLoad := btcwire.BIP0037Version - 1
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
pverNoFilterLoad := wire.BIP0037Version - 1
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
baseFilter := []byte{0x01, 0x02, 0x03, 0x04}
|
||||
baseFilterLoad := btcwire.NewMsgFilterLoad(baseFilter, 10, 0,
|
||||
btcwire.BloomUpdateNone)
|
||||
baseFilterLoad := wire.NewMsgFilterLoad(baseFilter, 10, 0,
|
||||
wire.BloomUpdateNone)
|
||||
baseFilterLoadEncoded := append([]byte{0x04}, baseFilter...)
|
||||
baseFilterLoadEncoded = append(baseFilterLoadEncoded,
|
||||
0x00, 0x00, 0x00, 0x0a, // HashFuncs
|
||||
@ -145,7 +145,7 @@ func TestFilterLoadWireErrors(t *testing.T) {
|
||||
0x00) // Flags
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgFilterLoad // Value to encode
|
||||
in *wire.MsgFilterLoad // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
@ -181,7 +181,7 @@ func TestFilterLoadWireErrors(t *testing.T) {
|
||||
// Force error due to unsupported protocol version.
|
||||
{
|
||||
baseFilterLoad, baseFilterLoadEncoded, pverNoFilterLoad,
|
||||
10, btcwireErr, btcwireErr,
|
||||
10, wireErr, wireErr,
|
||||
},
|
||||
}
|
||||
|
||||
@ -196,9 +196,9 @@ func TestFilterLoadWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -207,7 +207,7 @@ func TestFilterLoadWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgFilterLoad
|
||||
var msg wire.MsgFilterLoad
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -216,9 +216,9 @@ func TestFilterLoadWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"io"
|
@ -1,25 +1,25 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestGetAddr tests the MsgGetAddr API.
|
||||
func TestGetAddr(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "getaddr"
|
||||
msg := btcwire.NewMsgGetAddr()
|
||||
msg := wire.NewMsgGetAddr()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgGetAddr: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -41,21 +41,21 @@ func TestGetAddr(t *testing.T) {
|
||||
// TestGetAddrWire tests the MsgGetAddr wire encode and decode for various
|
||||
// protocol versions.
|
||||
func TestGetAddrWire(t *testing.T) {
|
||||
msgGetAddr := btcwire.NewMsgGetAddr()
|
||||
msgGetAddr := wire.NewMsgGetAddr()
|
||||
msgGetAddrEncoded := []byte{}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgGetAddr // Message to encode
|
||||
out *btcwire.MsgGetAddr // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgGetAddr // Message to encode
|
||||
out *wire.MsgGetAddr // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
msgGetAddr,
|
||||
msgGetAddr,
|
||||
msgGetAddrEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version.
|
||||
@ -63,7 +63,7 @@ func TestGetAddrWire(t *testing.T) {
|
||||
msgGetAddr,
|
||||
msgGetAddr,
|
||||
msgGetAddrEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version.
|
||||
@ -71,7 +71,7 @@ func TestGetAddrWire(t *testing.T) {
|
||||
msgGetAddr,
|
||||
msgGetAddr,
|
||||
msgGetAddrEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion.
|
||||
@ -79,7 +79,7 @@ func TestGetAddrWire(t *testing.T) {
|
||||
msgGetAddr,
|
||||
msgGetAddr,
|
||||
msgGetAddrEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion.
|
||||
@ -87,7 +87,7 @@ func TestGetAddrWire(t *testing.T) {
|
||||
msgGetAddr,
|
||||
msgGetAddr,
|
||||
msgGetAddrEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -107,7 +107,7 @@ func TestGetAddrWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgGetAddr
|
||||
var msg wire.MsgGetAddr
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,30 +10,30 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestGetBlocks tests the MsgGetBlocks API.
|
||||
func TestGetBlocks(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Block 99500 hash.
|
||||
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||
locatorHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
locatorHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 100000 hash.
|
||||
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||
hashStop, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashStop, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Ensure we get the same data back out.
|
||||
msg := btcwire.NewMsgGetBlocks(hashStop)
|
||||
msg := wire.NewMsgGetBlocks(hashStop)
|
||||
if !msg.HashStop.IsEqual(hashStop) {
|
||||
t.Errorf("NewMsgGetBlocks: wrong stop hash - got %v, want %v",
|
||||
msg.HashStop, hashStop)
|
||||
@ -71,7 +71,7 @@ func TestGetBlocks(t *testing.T) {
|
||||
|
||||
// Ensure adding more than the max allowed block locator hashes per
|
||||
// message returns an error.
|
||||
for i := 0; i < btcwire.MaxBlockLocatorsPerMsg; i++ {
|
||||
for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ {
|
||||
err = msg.AddBlockLocatorHash(locatorHash)
|
||||
}
|
||||
if err == nil {
|
||||
@ -90,27 +90,27 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
|
||||
// Block 99499 hash.
|
||||
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
|
||||
hashLocator, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashLocator, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 99500 hash.
|
||||
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||
hashLocator2, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashLocator2, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 100000 hash.
|
||||
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||
hashStop, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashStop, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// MsgGetBlocks message with no block locators or stop hash.
|
||||
noLocators := btcwire.NewMsgGetBlocks(&btcwire.ShaHash{})
|
||||
noLocators := wire.NewMsgGetBlocks(&wire.ShaHash{})
|
||||
noLocators.ProtocolVersion = pver
|
||||
noLocatorsEncoded := []byte{
|
||||
0x62, 0xea, 0x00, 0x00, // Protocol version 60002
|
||||
@ -122,7 +122,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// MsgGetBlocks message with multiple block locators and a stop hash.
|
||||
multiLocators := btcwire.NewMsgGetBlocks(hashStop)
|
||||
multiLocators := wire.NewMsgGetBlocks(hashStop)
|
||||
multiLocators.AddBlockLocatorHash(hashLocator2)
|
||||
multiLocators.AddBlockLocatorHash(hashLocator)
|
||||
multiLocators.ProtocolVersion = pver
|
||||
@ -144,17 +144,17 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgGetBlocks // Message to encode
|
||||
out *btcwire.MsgGetBlocks // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgGetBlocks // Message to encode
|
||||
out *wire.MsgGetBlocks // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version with no block locators.
|
||||
{
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with multiple block locators.
|
||||
@ -162,7 +162,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with no block locators.
|
||||
@ -170,7 +170,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with multiple block locators.
|
||||
@ -178,7 +178,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with no block locators.
|
||||
@ -186,7 +186,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Versionwith multiple block locators.
|
||||
@ -194,7 +194,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with no block locators.
|
||||
@ -202,7 +202,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion multiple block locators.
|
||||
@ -210,7 +210,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with no block locators.
|
||||
@ -218,7 +218,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion multiple block locators.
|
||||
@ -226,7 +226,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -246,7 +246,7 @@ func TestGetBlocksWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgGetBlocks
|
||||
var msg wire.MsgGetBlocks
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -268,31 +268,31 @@ func TestGetBlocksWireErrors(t *testing.T) {
|
||||
// specifically here instead of the latest because the test data is
|
||||
// using bytes encoded with that protocol version.
|
||||
pver := uint32(60002)
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
// Block 99499 hash.
|
||||
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
|
||||
hashLocator, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashLocator, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 99500 hash.
|
||||
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||
hashLocator2, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashLocator2, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 100000 hash.
|
||||
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||
hashStop, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashStop, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// MsgGetBlocks message with multiple block locators and a stop hash.
|
||||
baseGetBlocks := btcwire.NewMsgGetBlocks(hashStop)
|
||||
baseGetBlocks := wire.NewMsgGetBlocks(hashStop)
|
||||
baseGetBlocks.ProtocolVersion = pver
|
||||
baseGetBlocks.AddBlockLocatorHash(hashLocator2)
|
||||
baseGetBlocks.AddBlockLocatorHash(hashLocator)
|
||||
@ -315,8 +315,8 @@ func TestGetBlocksWireErrors(t *testing.T) {
|
||||
|
||||
// Message that forces an error by having more than the max allowed
|
||||
// block locator hashes.
|
||||
maxGetBlocks := btcwire.NewMsgGetBlocks(hashStop)
|
||||
for i := 0; i < btcwire.MaxBlockLocatorsPerMsg; i++ {
|
||||
maxGetBlocks := wire.NewMsgGetBlocks(hashStop)
|
||||
for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ {
|
||||
maxGetBlocks.AddBlockLocatorHash(&mainNetGenesisHash)
|
||||
}
|
||||
maxGetBlocks.BlockLocatorHashes = append(maxGetBlocks.BlockLocatorHashes,
|
||||
@ -327,12 +327,12 @@ func TestGetBlocksWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgGetBlocks // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgGetBlocks // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in protocol version.
|
||||
{baseGetBlocks, baseGetBlocksEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -343,7 +343,7 @@ func TestGetBlocksWireErrors(t *testing.T) {
|
||||
// Force error in stop hash.
|
||||
{baseGetBlocks, baseGetBlocksEncoded, pver, 69, io.ErrShortWrite, io.EOF},
|
||||
// Force error with greater than max block locator hashes.
|
||||
{maxGetBlocks, maxGetBlocksEncoded, pver, 7, btcwireErr, btcwireErr},
|
||||
{maxGetBlocks, maxGetBlocksEncoded, pver, 7, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -357,9 +357,9 @@ func TestGetBlocksWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -368,7 +368,7 @@ func TestGetBlocksWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgGetBlocks
|
||||
var msg wire.MsgGetBlocks
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -377,9 +377,9 @@ func TestGetBlocksWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,17 +10,17 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestGetData tests the MsgGetData API.
|
||||
func TestGetData(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "getdata"
|
||||
msg := btcwire.NewMsgGetData()
|
||||
msg := wire.NewMsgGetData()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgGetData: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -37,8 +37,8 @@ func TestGetData(t *testing.T) {
|
||||
}
|
||||
|
||||
// Ensure inventory vectors are added properly.
|
||||
hash := btcwire.ShaHash{}
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, &hash)
|
||||
hash := wire.ShaHash{}
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, &hash)
|
||||
err := msg.AddInvVect(iv)
|
||||
if err != nil {
|
||||
t.Errorf("AddInvVect: %v", err)
|
||||
@ -50,7 +50,7 @@ func TestGetData(t *testing.T) {
|
||||
|
||||
// Ensure adding more than the max allowed inventory vectors per
|
||||
// message returns an error.
|
||||
for i := 0; i < btcwire.MaxInvPerMsg; i++ {
|
||||
for i := 0; i < wire.MaxInvPerMsg; i++ {
|
||||
err = msg.AddInvVect(iv)
|
||||
}
|
||||
if err == nil {
|
||||
@ -60,8 +60,8 @@ func TestGetData(t *testing.T) {
|
||||
|
||||
// Ensure creating the message with a size hint larger than the max
|
||||
// works as expected.
|
||||
msg = btcwire.NewMsgGetDataSizeHint(btcwire.MaxInvPerMsg + 1)
|
||||
wantCap := btcwire.MaxInvPerMsg
|
||||
msg = wire.NewMsgGetDataSizeHint(wire.MaxInvPerMsg + 1)
|
||||
wantCap := wire.MaxInvPerMsg
|
||||
if cap(msg.InvList) != wantCap {
|
||||
t.Errorf("NewMsgGetDataSizeHint: wrong cap for size hint - "+
|
||||
"got %v, want %v", cap(msg.InvList), wantCap)
|
||||
@ -75,29 +75,29 @@ func TestGetData(t *testing.T) {
|
||||
func TestGetDataWire(t *testing.T) {
|
||||
// Block 203707 hash.
|
||||
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
|
||||
blockHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
blockHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Transation 1 of Block 203707 hash.
|
||||
hashStr = "d28a3dc7392bf00a9855ee93dd9a81eff82a2c4fe57fbd42cfe71b487accfaf0"
|
||||
txHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
txHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, blockHash)
|
||||
iv2 := btcwire.NewInvVect(btcwire.InvTypeTx, txHash)
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
|
||||
iv2 := wire.NewInvVect(wire.InvTypeTx, txHash)
|
||||
|
||||
// Empty MsgGetData message.
|
||||
NoInv := btcwire.NewMsgGetData()
|
||||
NoInv := wire.NewMsgGetData()
|
||||
NoInvEncoded := []byte{
|
||||
0x00, // Varint for number of inventory vectors
|
||||
}
|
||||
|
||||
// MsgGetData message with multiple inventory vectors.
|
||||
MultiInv := btcwire.NewMsgGetData()
|
||||
MultiInv := wire.NewMsgGetData()
|
||||
MultiInv.AddInvVect(iv)
|
||||
MultiInv.AddInvVect(iv2)
|
||||
MultiInvEncoded := []byte{
|
||||
@ -115,17 +115,17 @@ func TestGetDataWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgGetData // Message to encode
|
||||
out *btcwire.MsgGetData // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgGetData // Message to encode
|
||||
out *wire.MsgGetData // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version with no inv vectors.
|
||||
{
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with multiple inv vectors.
|
||||
@ -133,7 +133,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version no inv vectors.
|
||||
@ -141,7 +141,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with multiple inv vectors.
|
||||
@ -149,7 +149,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version no inv vectors.
|
||||
@ -157,7 +157,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with multiple inv vectors.
|
||||
@ -165,7 +165,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion no inv vectors.
|
||||
@ -173,7 +173,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with multiple inv vectors.
|
||||
@ -181,7 +181,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion no inv vectors.
|
||||
@ -189,7 +189,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with multiple inv vectors.
|
||||
@ -197,7 +197,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -217,7 +217,7 @@ func TestGetDataWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgGetData
|
||||
var msg wire.MsgGetData
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -235,20 +235,20 @@ func TestGetDataWire(t *testing.T) {
|
||||
// TestGetDataWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgGetData to confirm error paths work correctly.
|
||||
func TestGetDataWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
// Block 203707 hash.
|
||||
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
|
||||
blockHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
blockHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, blockHash)
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
|
||||
|
||||
// Base message used to induce errors.
|
||||
baseGetData := btcwire.NewMsgGetData()
|
||||
baseGetData := wire.NewMsgGetData()
|
||||
baseGetData.AddInvVect(iv)
|
||||
baseGetDataEncoded := []byte{
|
||||
0x02, // Varint for number of inv vectors
|
||||
@ -261,8 +261,8 @@ func TestGetDataWireErrors(t *testing.T) {
|
||||
|
||||
// Message that forces an error by having more than the max allowed inv
|
||||
// vectors.
|
||||
maxGetData := btcwire.NewMsgGetData()
|
||||
for i := 0; i < btcwire.MaxInvPerMsg; i++ {
|
||||
maxGetData := wire.NewMsgGetData()
|
||||
for i := 0; i < wire.MaxInvPerMsg; i++ {
|
||||
maxGetData.AddInvVect(iv)
|
||||
}
|
||||
maxGetData.InvList = append(maxGetData.InvList, iv)
|
||||
@ -271,12 +271,12 @@ func TestGetDataWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgGetData // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgGetData // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with intentional read/write errors.
|
||||
// Force error in inventory vector count
|
||||
@ -284,7 +284,7 @@ func TestGetDataWireErrors(t *testing.T) {
|
||||
// Force error in inventory list.
|
||||
{baseGetData, baseGetDataEncoded, pver, 1, io.ErrShortWrite, io.EOF},
|
||||
// Force error with greater than max inventory vectors.
|
||||
{maxGetData, maxGetDataEncoded, pver, 3, btcwireErr, btcwireErr},
|
||||
{maxGetData, maxGetDataEncoded, pver, 3, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -298,9 +298,9 @@ func TestGetDataWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -309,7 +309,7 @@ func TestGetDataWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgGetData
|
||||
var msg wire.MsgGetData
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -318,9 +318,9 @@ func TestGetDataWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,24 +10,24 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestGetHeaders tests the MsgGetHeader API.
|
||||
func TestGetHeaders(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Block 99500 hash.
|
||||
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||
locatorHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
locatorHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "getheaders"
|
||||
msg := btcwire.NewMsgGetHeaders()
|
||||
msg := wire.NewMsgGetHeaders()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgGetHeaders: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -58,7 +58,7 @@ func TestGetHeaders(t *testing.T) {
|
||||
|
||||
// Ensure adding more than the max allowed block locator hashes per
|
||||
// message returns an error.
|
||||
for i := 0; i < btcwire.MaxBlockLocatorsPerMsg; i++ {
|
||||
for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ {
|
||||
err = msg.AddBlockLocatorHash(locatorHash)
|
||||
}
|
||||
if err == nil {
|
||||
@ -79,27 +79,27 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
|
||||
// Block 99499 hash.
|
||||
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
|
||||
hashLocator, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashLocator, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 99500 hash.
|
||||
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||
hashLocator2, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashLocator2, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 100000 hash.
|
||||
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||
hashStop, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashStop, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// MsgGetHeaders message with no block locators or stop hash.
|
||||
noLocators := btcwire.NewMsgGetHeaders()
|
||||
noLocators := wire.NewMsgGetHeaders()
|
||||
noLocators.ProtocolVersion = pver
|
||||
noLocatorsEncoded := []byte{
|
||||
0x62, 0xea, 0x00, 0x00, // Protocol version 60002
|
||||
@ -111,7 +111,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// MsgGetHeaders message with multiple block locators and a stop hash.
|
||||
multiLocators := btcwire.NewMsgGetHeaders()
|
||||
multiLocators := wire.NewMsgGetHeaders()
|
||||
multiLocators.ProtocolVersion = pver
|
||||
multiLocators.HashStop = *hashStop
|
||||
multiLocators.AddBlockLocatorHash(hashLocator2)
|
||||
@ -134,17 +134,17 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgGetHeaders // Message to encode
|
||||
out *btcwire.MsgGetHeaders // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgGetHeaders // Message to encode
|
||||
out *wire.MsgGetHeaders // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version with no block locators.
|
||||
{
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with multiple block locators.
|
||||
@ -152,7 +152,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with no block locators.
|
||||
@ -160,7 +160,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with multiple block locators.
|
||||
@ -168,7 +168,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with no block locators.
|
||||
@ -176,7 +176,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Versionwith multiple block locators.
|
||||
@ -184,7 +184,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with no block locators.
|
||||
@ -192,7 +192,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion multiple block locators.
|
||||
@ -200,7 +200,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with no block locators.
|
||||
@ -208,7 +208,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
noLocators,
|
||||
noLocators,
|
||||
noLocatorsEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion multiple block locators.
|
||||
@ -216,7 +216,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
multiLocators,
|
||||
multiLocators,
|
||||
multiLocatorsEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -236,7 +236,7 @@ func TestGetHeadersWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgGetHeaders
|
||||
var msg wire.MsgGetHeaders
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -258,31 +258,31 @@ func TestGetHeadersWireErrors(t *testing.T) {
|
||||
// specifically here instead of the latest because the test data is
|
||||
// using bytes encoded with that protocol version.
|
||||
pver := uint32(60002)
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
// Block 99499 hash.
|
||||
hashStr := "2710f40c87ec93d010a6fd95f42c59a2cbacc60b18cf6b7957535"
|
||||
hashLocator, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashLocator, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 99500 hash.
|
||||
hashStr = "2e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||
hashLocator2, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashLocator2, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Block 100000 hash.
|
||||
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||
hashStop, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hashStop, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// MsgGetHeaders message with multiple block locators and a stop hash.
|
||||
baseGetHeaders := btcwire.NewMsgGetHeaders()
|
||||
baseGetHeaders := wire.NewMsgGetHeaders()
|
||||
baseGetHeaders.ProtocolVersion = pver
|
||||
baseGetHeaders.HashStop = *hashStop
|
||||
baseGetHeaders.AddBlockLocatorHash(hashLocator2)
|
||||
@ -306,8 +306,8 @@ func TestGetHeadersWireErrors(t *testing.T) {
|
||||
|
||||
// Message that forces an error by having more than the max allowed
|
||||
// block locator hashes.
|
||||
maxGetHeaders := btcwire.NewMsgGetHeaders()
|
||||
for i := 0; i < btcwire.MaxBlockLocatorsPerMsg; i++ {
|
||||
maxGetHeaders := wire.NewMsgGetHeaders()
|
||||
for i := 0; i < wire.MaxBlockLocatorsPerMsg; i++ {
|
||||
maxGetHeaders.AddBlockLocatorHash(&mainNetGenesisHash)
|
||||
}
|
||||
maxGetHeaders.BlockLocatorHashes = append(maxGetHeaders.BlockLocatorHashes,
|
||||
@ -318,12 +318,12 @@ func TestGetHeadersWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgGetHeaders // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgGetHeaders // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in protocol version.
|
||||
{baseGetHeaders, baseGetHeadersEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -334,7 +334,7 @@ func TestGetHeadersWireErrors(t *testing.T) {
|
||||
// Force error in stop hash.
|
||||
{baseGetHeaders, baseGetHeadersEncoded, pver, 69, io.ErrShortWrite, io.EOF},
|
||||
// Force error with greater than max block locator hashes.
|
||||
{maxGetHeaders, maxGetHeadersEncoded, pver, 7, btcwireErr, btcwireErr},
|
||||
{maxGetHeaders, maxGetHeadersEncoded, pver, 7, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -348,9 +348,9 @@ func TestGetHeadersWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -359,7 +359,7 @@ func TestGetHeadersWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgGetHeaders
|
||||
var msg wire.MsgGetHeaders
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -368,9 +368,9 @@ func TestGetHeadersWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,7 +10,7 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
@ -20,7 +20,7 @@ func TestHeaders(t *testing.T) {
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "headers"
|
||||
msg := btcwire.NewMsgHeaders()
|
||||
msg := wire.NewMsgHeaders()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgHeaders: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -49,10 +49,10 @@ func TestHeaders(t *testing.T) {
|
||||
// Ensure adding more than the max allowed headers per message returns
|
||||
// error.
|
||||
var err error
|
||||
for i := 0; i < btcwire.MaxBlockHeadersPerMsg+1; i++ {
|
||||
for i := 0; i < wire.MaxBlockHeadersPerMsg+1; i++ {
|
||||
err = msg.AddBlockHeader(bh)
|
||||
}
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(&btcwire.MessageError{}) {
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(&wire.MessageError{}) {
|
||||
t.Errorf("AddBlockHeader: expected error on too many headers " +
|
||||
"not received")
|
||||
}
|
||||
@ -67,18 +67,18 @@ func TestHeadersWire(t *testing.T) {
|
||||
merkleHash := blockOne.Header.MerkleRoot
|
||||
bits := uint32(0x1d00ffff)
|
||||
nonce := uint32(0x9962e301)
|
||||
bh := btcwire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
|
||||
bh := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
|
||||
bh.Version = blockOne.Header.Version
|
||||
bh.Timestamp = blockOne.Header.Timestamp
|
||||
|
||||
// Empty headers message.
|
||||
noHeaders := btcwire.NewMsgHeaders()
|
||||
noHeaders := wire.NewMsgHeaders()
|
||||
noHeadersEncoded := []byte{
|
||||
0x00, // Varint for number of headers
|
||||
}
|
||||
|
||||
// Headers message with one header.
|
||||
oneHeader := btcwire.NewMsgHeaders()
|
||||
oneHeader := wire.NewMsgHeaders()
|
||||
oneHeader.AddBlockHeader(bh)
|
||||
oneHeaderEncoded := []byte{
|
||||
0x01, // VarInt for number of headers.
|
||||
@ -98,17 +98,17 @@ func TestHeadersWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgHeaders // Message to encode
|
||||
out *btcwire.MsgHeaders // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgHeaders // Message to encode
|
||||
out *wire.MsgHeaders // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version with no headers.
|
||||
{
|
||||
noHeaders,
|
||||
noHeaders,
|
||||
noHeadersEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with one header.
|
||||
@ -116,7 +116,7 @@ func TestHeadersWire(t *testing.T) {
|
||||
oneHeader,
|
||||
oneHeader,
|
||||
oneHeaderEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with no headers.
|
||||
@ -124,7 +124,7 @@ func TestHeadersWire(t *testing.T) {
|
||||
noHeaders,
|
||||
noHeaders,
|
||||
noHeadersEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with one header.
|
||||
@ -132,7 +132,7 @@ func TestHeadersWire(t *testing.T) {
|
||||
oneHeader,
|
||||
oneHeader,
|
||||
oneHeaderEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with no headers.
|
||||
@ -140,7 +140,7 @@ func TestHeadersWire(t *testing.T) {
|
||||
noHeaders,
|
||||
noHeaders,
|
||||
noHeadersEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with one header.
|
||||
@ -148,14 +148,14 @@ func TestHeadersWire(t *testing.T) {
|
||||
oneHeader,
|
||||
oneHeader,
|
||||
oneHeaderEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
// Protocol version NetAddressTimeVersion with no headers.
|
||||
{
|
||||
noHeaders,
|
||||
noHeaders,
|
||||
noHeadersEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with one header.
|
||||
@ -163,7 +163,7 @@ func TestHeadersWire(t *testing.T) {
|
||||
oneHeader,
|
||||
oneHeader,
|
||||
oneHeaderEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with no headers.
|
||||
@ -171,7 +171,7 @@ func TestHeadersWire(t *testing.T) {
|
||||
noHeaders,
|
||||
noHeaders,
|
||||
noHeadersEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with one header.
|
||||
@ -179,7 +179,7 @@ func TestHeadersWire(t *testing.T) {
|
||||
oneHeader,
|
||||
oneHeader,
|
||||
oneHeaderEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -199,7 +199,7 @@ func TestHeadersWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgHeaders
|
||||
var msg wire.MsgHeaders
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -217,19 +217,19 @@ func TestHeadersWire(t *testing.T) {
|
||||
// TestHeadersWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgHeaders to confirm error paths work correctly.
|
||||
func TestHeadersWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
hash := mainNetGenesisHash
|
||||
merkleHash := blockOne.Header.MerkleRoot
|
||||
bits := uint32(0x1d00ffff)
|
||||
nonce := uint32(0x9962e301)
|
||||
bh := btcwire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
|
||||
bh := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
|
||||
bh.Version = blockOne.Header.Version
|
||||
bh.Timestamp = blockOne.Header.Timestamp
|
||||
|
||||
// Headers message with one header.
|
||||
oneHeader := btcwire.NewMsgHeaders()
|
||||
oneHeader := wire.NewMsgHeaders()
|
||||
oneHeader.AddBlockHeader(bh)
|
||||
oneHeaderEncoded := []byte{
|
||||
0x01, // VarInt for number of headers.
|
||||
@ -250,8 +250,8 @@ func TestHeadersWireErrors(t *testing.T) {
|
||||
|
||||
// Message that forces an error by having more than the max allowed
|
||||
// headers.
|
||||
maxHeaders := btcwire.NewMsgHeaders()
|
||||
for i := 0; i < btcwire.MaxBlockHeadersPerMsg; i++ {
|
||||
maxHeaders := wire.NewMsgHeaders()
|
||||
for i := 0; i < wire.MaxBlockHeadersPerMsg; i++ {
|
||||
maxHeaders.AddBlockHeader(bh)
|
||||
}
|
||||
maxHeaders.Headers = append(maxHeaders.Headers, bh)
|
||||
@ -261,11 +261,11 @@ func TestHeadersWireErrors(t *testing.T) {
|
||||
|
||||
// Intentionally invalid block header that has a transaction count used
|
||||
// to force errors.
|
||||
bhTrans := btcwire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
|
||||
bhTrans := wire.NewBlockHeader(&hash, &merkleHash, bits, nonce)
|
||||
bhTrans.Version = blockOne.Header.Version
|
||||
bhTrans.Timestamp = blockOne.Header.Timestamp
|
||||
|
||||
transHeader := btcwire.NewMsgHeaders()
|
||||
transHeader := wire.NewMsgHeaders()
|
||||
transHeader.AddBlockHeader(bhTrans)
|
||||
transHeaderEncoded := []byte{
|
||||
0x01, // VarInt for number of headers.
|
||||
@ -285,12 +285,12 @@ func TestHeadersWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgHeaders // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgHeaders // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with intentional read/write errors.
|
||||
// Force error in header count.
|
||||
@ -298,11 +298,11 @@ func TestHeadersWireErrors(t *testing.T) {
|
||||
// Force error in block header.
|
||||
{oneHeader, oneHeaderEncoded, pver, 5, io.ErrShortWrite, io.EOF},
|
||||
// Force error with greater than max headers.
|
||||
{maxHeaders, maxHeadersEncoded, pver, 3, btcwireErr, btcwireErr},
|
||||
{maxHeaders, maxHeadersEncoded, pver, 3, wireErr, wireErr},
|
||||
// Force error with number of transactions.
|
||||
{transHeader, transHeaderEncoded, pver, 81, io.ErrShortWrite, io.EOF},
|
||||
// Force error with included transactions.
|
||||
{transHeader, transHeaderEncoded, pver, len(transHeaderEncoded), nil, btcwireErr},
|
||||
{transHeader, transHeaderEncoded, pver, len(transHeaderEncoded), nil, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -316,9 +316,9 @@ func TestHeadersWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -327,7 +327,7 @@ func TestHeadersWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgHeaders
|
||||
var msg wire.MsgHeaders
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -336,9 +336,9 @@ func TestHeadersWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,17 +10,17 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestInv tests the MsgInv API.
|
||||
func TestInv(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "inv"
|
||||
msg := btcwire.NewMsgInv()
|
||||
msg := wire.NewMsgInv()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgInv: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -37,8 +37,8 @@ func TestInv(t *testing.T) {
|
||||
}
|
||||
|
||||
// Ensure inventory vectors are added properly.
|
||||
hash := btcwire.ShaHash{}
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, &hash)
|
||||
hash := wire.ShaHash{}
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, &hash)
|
||||
err := msg.AddInvVect(iv)
|
||||
if err != nil {
|
||||
t.Errorf("AddInvVect: %v", err)
|
||||
@ -50,7 +50,7 @@ func TestInv(t *testing.T) {
|
||||
|
||||
// Ensure adding more than the max allowed inventory vectors per
|
||||
// message returns an error.
|
||||
for i := 0; i < btcwire.MaxInvPerMsg; i++ {
|
||||
for i := 0; i < wire.MaxInvPerMsg; i++ {
|
||||
err = msg.AddInvVect(iv)
|
||||
}
|
||||
if err == nil {
|
||||
@ -60,8 +60,8 @@ func TestInv(t *testing.T) {
|
||||
|
||||
// Ensure creating the message with a size hint larger than the max
|
||||
// works as expected.
|
||||
msg = btcwire.NewMsgInvSizeHint(btcwire.MaxInvPerMsg + 1)
|
||||
wantCap := btcwire.MaxInvPerMsg
|
||||
msg = wire.NewMsgInvSizeHint(wire.MaxInvPerMsg + 1)
|
||||
wantCap := wire.MaxInvPerMsg
|
||||
if cap(msg.InvList) != wantCap {
|
||||
t.Errorf("NewMsgInvSizeHint: wrong cap for size hint - "+
|
||||
"got %v, want %v", cap(msg.InvList), wantCap)
|
||||
@ -75,29 +75,29 @@ func TestInv(t *testing.T) {
|
||||
func TestInvWire(t *testing.T) {
|
||||
// Block 203707 hash.
|
||||
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
|
||||
blockHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
blockHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Transation 1 of Block 203707 hash.
|
||||
hashStr = "d28a3dc7392bf00a9855ee93dd9a81eff82a2c4fe57fbd42cfe71b487accfaf0"
|
||||
txHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
txHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, blockHash)
|
||||
iv2 := btcwire.NewInvVect(btcwire.InvTypeTx, txHash)
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
|
||||
iv2 := wire.NewInvVect(wire.InvTypeTx, txHash)
|
||||
|
||||
// Empty inv message.
|
||||
NoInv := btcwire.NewMsgInv()
|
||||
NoInv := wire.NewMsgInv()
|
||||
NoInvEncoded := []byte{
|
||||
0x00, // Varint for number of inventory vectors
|
||||
}
|
||||
|
||||
// Inv message with multiple inventory vectors.
|
||||
MultiInv := btcwire.NewMsgInv()
|
||||
MultiInv := wire.NewMsgInv()
|
||||
MultiInv.AddInvVect(iv)
|
||||
MultiInv.AddInvVect(iv2)
|
||||
MultiInvEncoded := []byte{
|
||||
@ -115,17 +115,17 @@ func TestInvWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgInv // Message to encode
|
||||
out *btcwire.MsgInv // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgInv // Message to encode
|
||||
out *wire.MsgInv // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version with no inv vectors.
|
||||
{
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with multiple inv vectors.
|
||||
@ -133,7 +133,7 @@ func TestInvWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version no inv vectors.
|
||||
@ -141,7 +141,7 @@ func TestInvWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with multiple inv vectors.
|
||||
@ -149,7 +149,7 @@ func TestInvWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version no inv vectors.
|
||||
@ -157,7 +157,7 @@ func TestInvWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with multiple inv vectors.
|
||||
@ -165,7 +165,7 @@ func TestInvWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion no inv vectors.
|
||||
@ -173,7 +173,7 @@ func TestInvWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with multiple inv vectors.
|
||||
@ -181,7 +181,7 @@ func TestInvWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion no inv vectors.
|
||||
@ -189,7 +189,7 @@ func TestInvWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with multiple inv vectors.
|
||||
@ -197,7 +197,7 @@ func TestInvWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -217,7 +217,7 @@ func TestInvWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgInv
|
||||
var msg wire.MsgInv
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -235,20 +235,20 @@ func TestInvWire(t *testing.T) {
|
||||
// TestInvWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgInv to confirm error paths work correctly.
|
||||
func TestInvWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
// Block 203707 hash.
|
||||
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
|
||||
blockHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
blockHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, blockHash)
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
|
||||
|
||||
// Base inv message used to induce errors.
|
||||
baseInv := btcwire.NewMsgInv()
|
||||
baseInv := wire.NewMsgInv()
|
||||
baseInv.AddInvVect(iv)
|
||||
baseInvEncoded := []byte{
|
||||
0x02, // Varint for number of inv vectors
|
||||
@ -261,8 +261,8 @@ func TestInvWireErrors(t *testing.T) {
|
||||
|
||||
// Inv message that forces an error by having more than the max allowed
|
||||
// inv vectors.
|
||||
maxInv := btcwire.NewMsgInv()
|
||||
for i := 0; i < btcwire.MaxInvPerMsg; i++ {
|
||||
maxInv := wire.NewMsgInv()
|
||||
for i := 0; i < wire.MaxInvPerMsg; i++ {
|
||||
maxInv.AddInvVect(iv)
|
||||
}
|
||||
maxInv.InvList = append(maxInv.InvList, iv)
|
||||
@ -271,12 +271,12 @@ func TestInvWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgInv // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgInv // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with intentional read/write errors.
|
||||
// Force error in inventory vector count
|
||||
@ -284,7 +284,7 @@ func TestInvWireErrors(t *testing.T) {
|
||||
// Force error in inventory list.
|
||||
{baseInv, baseInvEncoded, pver, 1, io.ErrShortWrite, io.EOF},
|
||||
// Force error with greater than max inventory vectors.
|
||||
{maxInv, maxInvEncoded, pver, 3, btcwireErr, btcwireErr},
|
||||
{maxInv, maxInvEncoded, pver, 3, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -298,9 +298,9 @@ func TestInvWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -309,7 +309,7 @@ func TestInvWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgInv
|
||||
var msg wire.MsgInv
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -318,9 +318,9 @@ func TestInvWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,22 +1,22 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
func TestMemPool(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "mempool"
|
||||
msg := btcwire.NewMsgMemPool()
|
||||
msg := wire.NewMsgMemPool()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgMemPool: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -40,7 +40,7 @@ func TestMemPool(t *testing.T) {
|
||||
|
||||
// Older protocol versions should fail encode since message didn't
|
||||
// exist yet.
|
||||
oldPver := btcwire.BIP0035Version - 1
|
||||
oldPver := wire.BIP0035Version - 1
|
||||
err = msg.BtcEncode(&buf, oldPver)
|
||||
if err == nil {
|
||||
s := "encode of MsgMemPool passed for old protocol version %v err <%v>"
|
||||
@ -48,7 +48,7 @@ func TestMemPool(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test decode with latest protocol version.
|
||||
readmsg := btcwire.NewMsgMemPool()
|
||||
readmsg := wire.NewMsgMemPool()
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgMemPool failed [%v] err <%v>", buf, err)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -12,24 +12,24 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestMerkleBlock tests the MsgMerkleBlock API.
|
||||
func TestMerkleBlock(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Block 1 header.
|
||||
prevHash := &blockOne.Header.PrevBlock
|
||||
merkleHash := &blockOne.Header.MerkleRoot
|
||||
bits := blockOne.Header.Bits
|
||||
nonce := blockOne.Header.Nonce
|
||||
bh := btcwire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
|
||||
bh := wire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "merkleblock"
|
||||
msg := btcwire.NewMsgMerkleBlock(bh)
|
||||
msg := wire.NewMsgMerkleBlock(bh)
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgBlock: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -47,9 +47,9 @@ func TestMerkleBlock(t *testing.T) {
|
||||
|
||||
// Load maxTxPerBlock hashes
|
||||
data := make([]byte, 32)
|
||||
for i := 0; i < btcwire.MaxTxPerBlock; i++ {
|
||||
for i := 0; i < wire.MaxTxPerBlock; i++ {
|
||||
rand.Read(data)
|
||||
hash, err := btcwire.NewShaHash(data)
|
||||
hash, err := wire.NewShaHash(data)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHash failed: %v\n", err)
|
||||
return
|
||||
@ -63,7 +63,7 @@ func TestMerkleBlock(t *testing.T) {
|
||||
|
||||
// Add one more Tx to test failure.
|
||||
rand.Read(data)
|
||||
hash, err := btcwire.NewShaHash(data)
|
||||
hash, err := wire.NewShaHash(data)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHash failed: %v\n", err)
|
||||
return
|
||||
@ -82,7 +82,7 @@ func TestMerkleBlock(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test decode with latest protocol version.
|
||||
readmsg := btcwire.MsgMerkleBlock{}
|
||||
readmsg := wire.MsgMerkleBlock{}
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgMerkleBlock failed [%v] err <%v>", buf, err)
|
||||
@ -100,7 +100,7 @@ func TestMerkleBlock(t *testing.T) {
|
||||
// Force too many flag bytes to test maxFlagsPerMerkleBlock.
|
||||
// Reset the number of hashes back to a valid value.
|
||||
msg.Hashes = msg.Hashes[len(msg.Hashes)-1:]
|
||||
msg.Flags = make([]byte, btcwire.MaxFlagsPerMerkleBlock+1)
|
||||
msg.Flags = make([]byte, wire.MaxFlagsPerMerkleBlock+1)
|
||||
err = msg.BtcEncode(&buf, pver)
|
||||
if err == nil {
|
||||
t.Errorf("encode of MsgMerkleBlock succeeded with too many " +
|
||||
@ -117,21 +117,21 @@ func TestMerkleBlockCrossProtocol(t *testing.T) {
|
||||
merkleHash := &blockOne.Header.MerkleRoot
|
||||
bits := blockOne.Header.Bits
|
||||
nonce := blockOne.Header.Nonce
|
||||
bh := btcwire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
|
||||
bh := wire.NewBlockHeader(prevHash, merkleHash, bits, nonce)
|
||||
|
||||
msg := btcwire.NewMsgMerkleBlock(bh)
|
||||
msg := wire.NewMsgMerkleBlock(bh)
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err != nil {
|
||||
t.Errorf("encode of NewMsgFilterLoad failed %v err <%v>", msg,
|
||||
err)
|
||||
}
|
||||
|
||||
// Decode with old protocol version.
|
||||
var readmsg btcwire.MsgFilterLoad
|
||||
err = readmsg.BtcDecode(&buf, btcwire.BIP0031Version)
|
||||
var readmsg wire.MsgFilterLoad
|
||||
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgFilterLoad succeeded when it shouldn't have %v",
|
||||
msg)
|
||||
@ -142,21 +142,21 @@ func TestMerkleBlockCrossProtocol(t *testing.T) {
|
||||
// various numbers of transaction hashes and protocol versions.
|
||||
func TestMerkleBlockWire(t *testing.T) {
|
||||
tests := []struct {
|
||||
in *btcwire.MsgMerkleBlock // Message to encode
|
||||
out *btcwire.MsgMerkleBlock // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgMerkleBlock // Message to encode
|
||||
out *wire.MsgMerkleBlock // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
&merkleBlockOne, &merkleBlockOne, merkleBlockOneBytes,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0037Version.
|
||||
{
|
||||
&merkleBlockOne, &merkleBlockOne, merkleBlockOneBytes,
|
||||
btcwire.BIP0037Version,
|
||||
wire.BIP0037Version,
|
||||
},
|
||||
}
|
||||
|
||||
@ -176,7 +176,7 @@ func TestMerkleBlockWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgMerkleBlock
|
||||
var msg wire.MsgMerkleBlock
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -198,16 +198,16 @@ func TestMerkleBlockWireErrors(t *testing.T) {
|
||||
// because the test data is using bytes encoded with that protocol
|
||||
// version.
|
||||
pver := uint32(70001)
|
||||
pverNoMerkleBlock := btcwire.BIP0037Version - 1
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pverNoMerkleBlock := wire.BIP0037Version - 1
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgMerkleBlock // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgMerkleBlock // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in version.
|
||||
{
|
||||
@ -267,7 +267,7 @@ func TestMerkleBlockWireErrors(t *testing.T) {
|
||||
// Force error due to unsupported protocol version.
|
||||
{
|
||||
&merkleBlockOne, merkleBlockOneBytes, pverNoMerkleBlock,
|
||||
119, btcwireErr, btcwireErr,
|
||||
119, wireErr, wireErr,
|
||||
},
|
||||
}
|
||||
|
||||
@ -282,9 +282,9 @@ func TestMerkleBlockWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -293,7 +293,7 @@ func TestMerkleBlockWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgMerkleBlock
|
||||
var msg wire.MsgMerkleBlock
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -302,9 +302,9 @@ func TestMerkleBlockWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
||||
@ -327,7 +327,7 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
|
||||
// Create bytes for a merkle block that claims to have more than the max
|
||||
// allowed tx hashes.
|
||||
var buf bytes.Buffer
|
||||
btcwire.TstWriteVarInt(&buf, pver, btcwire.MaxTxPerBlock+1)
|
||||
wire.TstWriteVarInt(&buf, pver, wire.MaxTxPerBlock+1)
|
||||
numHashesOffset := 84
|
||||
exceedMaxHashes := make([]byte, numHashesOffset)
|
||||
copy(exceedMaxHashes, merkleBlockOneBytes[:numHashesOffset])
|
||||
@ -336,7 +336,7 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
|
||||
// Create bytes for a merkle block that claims to have more than the max
|
||||
// allowed flag bytes.
|
||||
buf.Reset()
|
||||
btcwire.TstWriteVarInt(&buf, pver, btcwire.MaxFlagsPerMerkleBlock+1)
|
||||
wire.TstWriteVarInt(&buf, pver, wire.MaxFlagsPerMerkleBlock+1)
|
||||
numFlagBytesOffset := 117
|
||||
exceedMaxFlagBytes := make([]byte, numFlagBytesOffset)
|
||||
copy(exceedMaxFlagBytes, merkleBlockOneBytes[:numFlagBytesOffset])
|
||||
@ -348,15 +348,15 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
|
||||
err error // Expected error
|
||||
}{
|
||||
// Block that claims to have more than max allowed hashes.
|
||||
{exceedMaxHashes, pver, &btcwire.MessageError{}},
|
||||
{exceedMaxHashes, pver, &wire.MessageError{}},
|
||||
// Block that claims to have more than max allowed flag bytes.
|
||||
{exceedMaxFlagBytes, pver, &btcwire.MessageError{}},
|
||||
{exceedMaxFlagBytes, pver, &wire.MessageError{}},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgMerkleBlock
|
||||
var msg wire.MsgMerkleBlock
|
||||
r := bytes.NewReader(test.buf)
|
||||
err := msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
||||
@ -369,16 +369,16 @@ func TestMerkleBlockOverflowErrors(t *testing.T) {
|
||||
|
||||
// merkleBlockOne is a merkle block created from block one of the block chain
|
||||
// where the first transaction matches.
|
||||
var merkleBlockOne = btcwire.MsgMerkleBlock{
|
||||
Header: btcwire.BlockHeader{
|
||||
var merkleBlockOne = wire.MsgMerkleBlock{
|
||||
Header: wire.BlockHeader{
|
||||
Version: 1,
|
||||
PrevBlock: btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
PrevBlock: wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
||||
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
||||
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
||||
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
}),
|
||||
MerkleRoot: btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
MerkleRoot: wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
|
||||
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
|
||||
0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
|
||||
@ -389,8 +389,8 @@ var merkleBlockOne = btcwire.MsgMerkleBlock{
|
||||
Nonce: 0x9962e301, // 2573394689
|
||||
},
|
||||
Transactions: 1,
|
||||
Hashes: []*btcwire.ShaHash{
|
||||
(*btcwire.ShaHash)(&[btcwire.HashSize]byte{ // Make go vet happy.
|
||||
Hashes: []*wire.ShaHash{
|
||||
(*wire.ShaHash)(&[wire.HashSize]byte{ // Make go vet happy.
|
||||
0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44,
|
||||
0xbb, 0xbe, 0x68, 0x0e, 0x1f, 0xee, 0x14, 0x67,
|
||||
0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1,
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,17 +10,17 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestNotFound tests the MsgNotFound API.
|
||||
func TestNotFound(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "notfound"
|
||||
msg := btcwire.NewMsgNotFound()
|
||||
msg := wire.NewMsgNotFound()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgNotFound: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -37,8 +37,8 @@ func TestNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
// Ensure inventory vectors are added properly.
|
||||
hash := btcwire.ShaHash{}
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, &hash)
|
||||
hash := wire.ShaHash{}
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, &hash)
|
||||
err := msg.AddInvVect(iv)
|
||||
if err != nil {
|
||||
t.Errorf("AddInvVect: %v", err)
|
||||
@ -50,7 +50,7 @@ func TestNotFound(t *testing.T) {
|
||||
|
||||
// Ensure adding more than the max allowed inventory vectors per
|
||||
// message returns an error.
|
||||
for i := 0; i < btcwire.MaxInvPerMsg; i++ {
|
||||
for i := 0; i < wire.MaxInvPerMsg; i++ {
|
||||
err = msg.AddInvVect(iv)
|
||||
}
|
||||
if err == nil {
|
||||
@ -66,29 +66,29 @@ func TestNotFound(t *testing.T) {
|
||||
func TestNotFoundWire(t *testing.T) {
|
||||
// Block 203707 hash.
|
||||
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
|
||||
blockHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
blockHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Transation 1 of Block 203707 hash.
|
||||
hashStr = "d28a3dc7392bf00a9855ee93dd9a81eff82a2c4fe57fbd42cfe71b487accfaf0"
|
||||
txHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
txHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, blockHash)
|
||||
iv2 := btcwire.NewInvVect(btcwire.InvTypeTx, txHash)
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
|
||||
iv2 := wire.NewInvVect(wire.InvTypeTx, txHash)
|
||||
|
||||
// Empty notfound message.
|
||||
NoInv := btcwire.NewMsgNotFound()
|
||||
NoInv := wire.NewMsgNotFound()
|
||||
NoInvEncoded := []byte{
|
||||
0x00, // Varint for number of inventory vectors
|
||||
}
|
||||
|
||||
// NotFound message with multiple inventory vectors.
|
||||
MultiInv := btcwire.NewMsgNotFound()
|
||||
MultiInv := wire.NewMsgNotFound()
|
||||
MultiInv.AddInvVect(iv)
|
||||
MultiInv.AddInvVect(iv2)
|
||||
MultiInvEncoded := []byte{
|
||||
@ -106,17 +106,17 @@ func TestNotFoundWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgNotFound // Message to encode
|
||||
out *btcwire.MsgNotFound // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgNotFound // Message to encode
|
||||
out *wire.MsgNotFound // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version with no inv vectors.
|
||||
{
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with multiple inv vectors.
|
||||
@ -124,7 +124,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version no inv vectors.
|
||||
@ -132,7 +132,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with multiple inv vectors.
|
||||
@ -140,7 +140,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version no inv vectors.
|
||||
@ -148,7 +148,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with multiple inv vectors.
|
||||
@ -156,7 +156,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion no inv vectors.
|
||||
@ -164,7 +164,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with multiple inv vectors.
|
||||
@ -172,7 +172,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion no inv vectors.
|
||||
@ -180,7 +180,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
NoInv,
|
||||
NoInv,
|
||||
NoInvEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with multiple inv vectors.
|
||||
@ -188,7 +188,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
MultiInv,
|
||||
MultiInv,
|
||||
MultiInvEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -208,7 +208,7 @@ func TestNotFoundWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgNotFound
|
||||
var msg wire.MsgNotFound
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -226,20 +226,20 @@ func TestNotFoundWire(t *testing.T) {
|
||||
// TestNotFoundWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgNotFound to confirm error paths work correctly.
|
||||
func TestNotFoundWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
// Block 203707 hash.
|
||||
hashStr := "3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc"
|
||||
blockHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
blockHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
iv := btcwire.NewInvVect(btcwire.InvTypeBlock, blockHash)
|
||||
iv := wire.NewInvVect(wire.InvTypeBlock, blockHash)
|
||||
|
||||
// Base message used to induce errors.
|
||||
baseNotFound := btcwire.NewMsgNotFound()
|
||||
baseNotFound := wire.NewMsgNotFound()
|
||||
baseNotFound.AddInvVect(iv)
|
||||
baseNotFoundEncoded := []byte{
|
||||
0x02, // Varint for number of inv vectors
|
||||
@ -252,8 +252,8 @@ func TestNotFoundWireErrors(t *testing.T) {
|
||||
|
||||
// Message that forces an error by having more than the max allowed inv
|
||||
// vectors.
|
||||
maxNotFound := btcwire.NewMsgNotFound()
|
||||
for i := 0; i < btcwire.MaxInvPerMsg; i++ {
|
||||
maxNotFound := wire.NewMsgNotFound()
|
||||
for i := 0; i < wire.MaxInvPerMsg; i++ {
|
||||
maxNotFound.AddInvVect(iv)
|
||||
}
|
||||
maxNotFound.InvList = append(maxNotFound.InvList, iv)
|
||||
@ -262,19 +262,19 @@ func TestNotFoundWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgNotFound // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgNotFound // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in inventory vector count
|
||||
{baseNotFound, baseNotFoundEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
// Force error in inventory list.
|
||||
{baseNotFound, baseNotFoundEncoded, pver, 1, io.ErrShortWrite, io.EOF},
|
||||
// Force error with greater than max inventory vectors.
|
||||
{maxNotFound, maxNotFoundEncoded, pver, 3, btcwireErr, btcwireErr},
|
||||
{maxNotFound, maxNotFoundEncoded, pver, 3, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -288,9 +288,9 @@ func TestNotFoundWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -299,7 +299,7 @@ func TestNotFoundWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgNotFound
|
||||
var msg wire.MsgNotFound
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -308,9 +308,9 @@ func TestNotFoundWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"io"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,20 +10,20 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestPing tests the MsgPing API against the latest protocol version.
|
||||
func TestPing(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Ensure we get the same nonce back out.
|
||||
nonce, err := btcwire.RandomUint64()
|
||||
nonce, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("RandomUint64: Error generating nonce: %v", err)
|
||||
}
|
||||
msg := btcwire.NewMsgPing(nonce)
|
||||
msg := wire.NewMsgPing(nonce)
|
||||
if msg.Nonce != nonce {
|
||||
t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
|
||||
msg.Nonce, nonce)
|
||||
@ -52,13 +52,13 @@ func TestPing(t *testing.T) {
|
||||
// BIP0031Version.
|
||||
func TestPingBIP0031(t *testing.T) {
|
||||
// Use the protocol version just prior to BIP0031Version changes.
|
||||
pver := btcwire.BIP0031Version
|
||||
pver := wire.BIP0031Version
|
||||
|
||||
nonce, err := btcwire.RandomUint64()
|
||||
nonce, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("RandomUint64: Error generating nonce: %v", err)
|
||||
}
|
||||
msg := btcwire.NewMsgPing(nonce)
|
||||
msg := wire.NewMsgPing(nonce)
|
||||
if msg.Nonce != nonce {
|
||||
t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
|
||||
msg.Nonce, nonce)
|
||||
@ -81,7 +81,7 @@ func TestPingBIP0031(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test decode with old protocol version.
|
||||
readmsg := btcwire.NewMsgPing(0)
|
||||
readmsg := wire.NewMsgPing(0)
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgPing failed [%v] err <%v>", buf, err)
|
||||
@ -99,11 +99,11 @@ func TestPingBIP0031(t *testing.T) {
|
||||
// TestPingCrossProtocol tests the MsgPing API when encoding with the latest
|
||||
// protocol version and decoding with BIP0031Version.
|
||||
func TestPingCrossProtocol(t *testing.T) {
|
||||
nonce, err := btcwire.RandomUint64()
|
||||
nonce, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("RandomUint64: Error generating nonce: %v", err)
|
||||
}
|
||||
msg := btcwire.NewMsgPing(nonce)
|
||||
msg := wire.NewMsgPing(nonce)
|
||||
if msg.Nonce != nonce {
|
||||
t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
|
||||
msg.Nonce, nonce)
|
||||
@ -111,14 +111,14 @@ func TestPingCrossProtocol(t *testing.T) {
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err = msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err = msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err != nil {
|
||||
t.Errorf("encode of MsgPing failed %v err <%v>", msg, err)
|
||||
}
|
||||
|
||||
// Decode with old protocol version.
|
||||
readmsg := btcwire.NewMsgPing(0)
|
||||
err = readmsg.BtcDecode(&buf, btcwire.BIP0031Version)
|
||||
readmsg := wire.NewMsgPing(0)
|
||||
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgPing failed [%v] err <%v>", buf, err)
|
||||
}
|
||||
@ -134,33 +134,33 @@ func TestPingCrossProtocol(t *testing.T) {
|
||||
// versions.
|
||||
func TestPingWire(t *testing.T) {
|
||||
tests := []struct {
|
||||
in btcwire.MsgPing // Message to encode
|
||||
out btcwire.MsgPing // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in wire.MsgPing // Message to encode
|
||||
out wire.MsgPing // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
btcwire.MsgPing{Nonce: 123123}, // 0x1e0f3
|
||||
btcwire.MsgPing{Nonce: 123123}, // 0x1e0f3
|
||||
wire.MsgPing{Nonce: 123123}, // 0x1e0f3
|
||||
wire.MsgPing{Nonce: 123123}, // 0x1e0f3
|
||||
[]byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version+1
|
||||
{
|
||||
btcwire.MsgPing{Nonce: 456456}, // 0x6f708
|
||||
btcwire.MsgPing{Nonce: 456456}, // 0x6f708
|
||||
wire.MsgPing{Nonce: 456456}, // 0x6f708
|
||||
wire.MsgPing{Nonce: 456456}, // 0x6f708
|
||||
[]byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
btcwire.BIP0031Version + 1,
|
||||
wire.BIP0031Version + 1,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version
|
||||
{
|
||||
btcwire.MsgPing{Nonce: 789789}, // 0xc0d1d
|
||||
btcwire.MsgPing{Nonce: 0}, // No nonce for pver
|
||||
[]byte{}, // No nonce for pver
|
||||
btcwire.BIP0031Version,
|
||||
wire.MsgPing{Nonce: 789789}, // 0xc0d1d
|
||||
wire.MsgPing{Nonce: 0}, // No nonce for pver
|
||||
[]byte{}, // No nonce for pver
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
}
|
||||
|
||||
@ -180,7 +180,7 @@ func TestPingWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgPing
|
||||
var msg wire.MsgPing
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -198,19 +198,19 @@ func TestPingWire(t *testing.T) {
|
||||
// TestPingWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgPing to confirm error paths work correctly.
|
||||
func TestPingWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgPing // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgPing // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with intentional read/write errors.
|
||||
{
|
||||
&btcwire.MsgPing{Nonce: 123123}, // 0x1e0f3
|
||||
&wire.MsgPing{Nonce: 123123}, // 0x1e0f3
|
||||
[]byte{0xf3, 0xe0, 0x01, 0x00},
|
||||
pver,
|
||||
2,
|
||||
@ -231,7 +231,7 @@ func TestPingWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgPing
|
||||
var msg wire.MsgPing
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if err != test.readErr {
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,19 +10,19 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestPongLatest tests the MsgPong API against the latest protocol version.
|
||||
func TestPongLatest(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
nonce, err := btcwire.RandomUint64()
|
||||
nonce, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("RandomUint64: error generating nonce: %v", err)
|
||||
}
|
||||
msg := btcwire.NewMsgPong(nonce)
|
||||
msg := wire.NewMsgPong(nonce)
|
||||
if msg.Nonce != nonce {
|
||||
t.Errorf("NewMsgPong: wrong nonce - got %v, want %v",
|
||||
msg.Nonce, nonce)
|
||||
@ -52,7 +52,7 @@ func TestPongLatest(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test decode with latest protocol version.
|
||||
readmsg := btcwire.NewMsgPong(0)
|
||||
readmsg := wire.NewMsgPong(0)
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgPong failed [%v] err <%v>", buf, err)
|
||||
@ -70,13 +70,13 @@ func TestPongLatest(t *testing.T) {
|
||||
// BIP0031Version.
|
||||
func TestPongBIP0031(t *testing.T) {
|
||||
// Use the protocol version just prior to BIP0031Version changes.
|
||||
pver := btcwire.BIP0031Version
|
||||
pver := wire.BIP0031Version
|
||||
|
||||
nonce, err := btcwire.RandomUint64()
|
||||
nonce, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("Error generating nonce: %v", err)
|
||||
}
|
||||
msg := btcwire.NewMsgPong(nonce)
|
||||
msg := wire.NewMsgPong(nonce)
|
||||
if msg.Nonce != nonce {
|
||||
t.Errorf("Should get same nonce back out.")
|
||||
}
|
||||
@ -97,7 +97,7 @@ func TestPongBIP0031(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test decode with old protocol version.
|
||||
readmsg := btcwire.NewMsgPong(0)
|
||||
readmsg := wire.NewMsgPong(0)
|
||||
err = readmsg.BtcDecode(&buf, pver)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgPong succeeded when it shouldn't have %v",
|
||||
@ -116,25 +116,25 @@ func TestPongBIP0031(t *testing.T) {
|
||||
// TestPongCrossProtocol tests the MsgPong API when encoding with the latest
|
||||
// protocol version and decoding with BIP0031Version.
|
||||
func TestPongCrossProtocol(t *testing.T) {
|
||||
nonce, err := btcwire.RandomUint64()
|
||||
nonce, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("Error generating nonce: %v", err)
|
||||
}
|
||||
msg := btcwire.NewMsgPong(nonce)
|
||||
msg := wire.NewMsgPong(nonce)
|
||||
if msg.Nonce != nonce {
|
||||
t.Errorf("Should get same nonce back out.")
|
||||
}
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err = msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err = msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err != nil {
|
||||
t.Errorf("encode of MsgPong failed %v err <%v>", msg, err)
|
||||
}
|
||||
|
||||
// Decode with old protocol version.
|
||||
readmsg := btcwire.NewMsgPong(0)
|
||||
err = readmsg.BtcDecode(&buf, btcwire.BIP0031Version)
|
||||
readmsg := wire.NewMsgPong(0)
|
||||
err = readmsg.BtcDecode(&buf, wire.BIP0031Version)
|
||||
if err == nil {
|
||||
t.Errorf("encode of MsgPong succeeded when it shouldn't have %v",
|
||||
msg)
|
||||
@ -151,25 +151,25 @@ func TestPongCrossProtocol(t *testing.T) {
|
||||
// versions.
|
||||
func TestPongWire(t *testing.T) {
|
||||
tests := []struct {
|
||||
in btcwire.MsgPong // Message to encode
|
||||
out btcwire.MsgPong // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in wire.MsgPong // Message to encode
|
||||
out wire.MsgPong // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
btcwire.MsgPong{Nonce: 123123}, // 0x1e0f3
|
||||
btcwire.MsgPong{Nonce: 123123}, // 0x1e0f3
|
||||
wire.MsgPong{Nonce: 123123}, // 0x1e0f3
|
||||
wire.MsgPong{Nonce: 123123}, // 0x1e0f3
|
||||
[]byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version+1
|
||||
{
|
||||
btcwire.MsgPong{Nonce: 456456}, // 0x6f708
|
||||
btcwire.MsgPong{Nonce: 456456}, // 0x6f708
|
||||
wire.MsgPong{Nonce: 456456}, // 0x6f708
|
||||
wire.MsgPong{Nonce: 456456}, // 0x6f708
|
||||
[]byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
btcwire.BIP0031Version + 1,
|
||||
wire.BIP0031Version + 1,
|
||||
},
|
||||
}
|
||||
|
||||
@ -189,7 +189,7 @@ func TestPongWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgPong
|
||||
var msg wire.MsgPong
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -207,28 +207,28 @@ func TestPongWire(t *testing.T) {
|
||||
// TestPongWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgPong to confirm error paths work correctly.
|
||||
func TestPongWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pverNoPong := btcwire.BIP0031Version
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
pverNoPong := wire.BIP0031Version
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
basePong := btcwire.NewMsgPong(123123) // 0x1e0f3
|
||||
basePong := wire.NewMsgPong(123123) // 0x1e0f3
|
||||
basePongEncoded := []byte{
|
||||
0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgPong // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgPong // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with intentional read/write errors.
|
||||
// Force error in nonce.
|
||||
{basePong, basePongEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
// Force error due to unsupported protocol version.
|
||||
{basePong, basePongEncoded, pverNoPong, 4, btcwireErr, btcwireErr},
|
||||
{basePong, basePongEncoded, pverNoPong, 4, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -242,9 +242,9 @@ func TestPongWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -253,7 +253,7 @@ func TestPongWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgPong
|
||||
var msg wire.MsgPong
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -262,9 +262,9 @@ func TestPongWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2014-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -10,24 +10,24 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestRejectCodeStringer tests the stringized output for the reject code type.
|
||||
func TestRejectCodeStringer(t *testing.T) {
|
||||
tests := []struct {
|
||||
in btcwire.RejectCode
|
||||
in wire.RejectCode
|
||||
want string
|
||||
}{
|
||||
{btcwire.RejectMalformed, "REJECT_MALFORMED"},
|
||||
{btcwire.RejectInvalid, "REJECT_INVALID"},
|
||||
{btcwire.RejectObsolete, "REJECT_OBSOLETE"},
|
||||
{btcwire.RejectDuplicate, "REJECT_DUPLICATE"},
|
||||
{btcwire.RejectNonstandard, "REJECT_NONSTANDARD"},
|
||||
{btcwire.RejectDust, "REJECT_DUST"},
|
||||
{btcwire.RejectInsufficientFee, "REJECT_INSUFFICIENTFEE"},
|
||||
{btcwire.RejectCheckpoint, "REJECT_CHECKPOINT"},
|
||||
{wire.RejectMalformed, "REJECT_MALFORMED"},
|
||||
{wire.RejectInvalid, "REJECT_INVALID"},
|
||||
{wire.RejectObsolete, "REJECT_OBSOLETE"},
|
||||
{wire.RejectDuplicate, "REJECT_DUPLICATE"},
|
||||
{wire.RejectNonstandard, "REJECT_NONSTANDARD"},
|
||||
{wire.RejectDust, "REJECT_DUST"},
|
||||
{wire.RejectInsufficientFee, "REJECT_INSUFFICIENTFEE"},
|
||||
{wire.RejectCheckpoint, "REJECT_CHECKPOINT"},
|
||||
{0xff, "Unknown RejectCode (255)"},
|
||||
}
|
||||
|
||||
@ -45,16 +45,16 @@ func TestRejectCodeStringer(t *testing.T) {
|
||||
|
||||
// TestRejectLatest tests the MsgPong API against the latest protocol version.
|
||||
func TestRejectLatest(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Create reject message data.
|
||||
rejCommand := (&btcwire.MsgBlock{}).Command()
|
||||
rejCode := btcwire.RejectDuplicate
|
||||
rejCommand := (&wire.MsgBlock{}).Command()
|
||||
rejCode := wire.RejectDuplicate
|
||||
rejReason := "duplicate block"
|
||||
rejHash := mainNetGenesisHash
|
||||
|
||||
// Ensure we get the correct data back out.
|
||||
msg := btcwire.NewMsgReject(rejCommand, rejCode, rejReason)
|
||||
msg := wire.NewMsgReject(rejCommand, rejCode, rejReason)
|
||||
msg.Hash = rejHash
|
||||
if msg.Cmd != rejCommand {
|
||||
t.Errorf("NewMsgReject: wrong rejected command - got %v, "+
|
||||
@ -77,7 +77,7 @@ func TestRejectLatest(t *testing.T) {
|
||||
}
|
||||
|
||||
// Ensure max payload is expected value for latest protocol version.
|
||||
wantPayload := uint32(btcwire.MaxMessagePayload)
|
||||
wantPayload := uint32(wire.MaxMessagePayload)
|
||||
maxPayload := msg.MaxPayloadLength(pver)
|
||||
if maxPayload != wantPayload {
|
||||
t.Errorf("MaxPayloadLength: wrong max payload length for "+
|
||||
@ -93,7 +93,7 @@ func TestRejectLatest(t *testing.T) {
|
||||
}
|
||||
|
||||
// Test decode with latest protocol version.
|
||||
readMsg := btcwire.MsgReject{}
|
||||
readMsg := wire.MsgReject{}
|
||||
err = readMsg.BtcDecode(&buf, pver)
|
||||
if err != nil {
|
||||
t.Errorf("decode of MsgReject failed %v err <%v>", buf.Bytes(),
|
||||
@ -123,15 +123,15 @@ func TestRejectLatest(t *testing.T) {
|
||||
// before the version which introduced it (RejectVersion).
|
||||
func TestRejectBeforeAdded(t *testing.T) {
|
||||
// Use the protocol version just prior to RejectVersion.
|
||||
pver := btcwire.RejectVersion - 1
|
||||
pver := wire.RejectVersion - 1
|
||||
|
||||
// Create reject message data.
|
||||
rejCommand := (&btcwire.MsgBlock{}).Command()
|
||||
rejCode := btcwire.RejectDuplicate
|
||||
rejCommand := (&wire.MsgBlock{}).Command()
|
||||
rejCode := wire.RejectDuplicate
|
||||
rejReason := "duplicate block"
|
||||
rejHash := mainNetGenesisHash
|
||||
|
||||
msg := btcwire.NewMsgReject(rejCommand, rejCode, rejReason)
|
||||
msg := wire.NewMsgReject(rejCommand, rejCode, rejReason)
|
||||
msg.Hash = rejHash
|
||||
|
||||
// Ensure max payload is expected value for old protocol version.
|
||||
@ -150,7 +150,7 @@ func TestRejectBeforeAdded(t *testing.T) {
|
||||
}
|
||||
|
||||
// // Test decode with old protocol version.
|
||||
readMsg := btcwire.MsgReject{}
|
||||
readMsg := wire.MsgReject{}
|
||||
err = readMsg.BtcDecode(&buf, pver)
|
||||
if err == nil {
|
||||
t.Errorf("decode of MsgReject succeeded when it shouldn't "+
|
||||
@ -182,24 +182,24 @@ func TestRejectBeforeAdded(t *testing.T) {
|
||||
// introduced it (RejectVersion).
|
||||
func TestRejectCrossProtocol(t *testing.T) {
|
||||
// Create reject message data.
|
||||
rejCommand := (&btcwire.MsgBlock{}).Command()
|
||||
rejCode := btcwire.RejectDuplicate
|
||||
rejCommand := (&wire.MsgBlock{}).Command()
|
||||
rejCode := wire.RejectDuplicate
|
||||
rejReason := "duplicate block"
|
||||
rejHash := mainNetGenesisHash
|
||||
|
||||
msg := btcwire.NewMsgReject(rejCommand, rejCode, rejReason)
|
||||
msg := wire.NewMsgReject(rejCommand, rejCode, rejReason)
|
||||
msg.Hash = rejHash
|
||||
|
||||
// Encode with latest protocol version.
|
||||
var buf bytes.Buffer
|
||||
err := msg.BtcEncode(&buf, btcwire.ProtocolVersion)
|
||||
err := msg.BtcEncode(&buf, wire.ProtocolVersion)
|
||||
if err != nil {
|
||||
t.Errorf("encode of MsgReject failed %v err <%v>", msg, err)
|
||||
}
|
||||
|
||||
// Decode with old protocol version.
|
||||
readMsg := btcwire.MsgReject{}
|
||||
err = readMsg.BtcDecode(&buf, btcwire.RejectVersion-1)
|
||||
readMsg := wire.MsgReject{}
|
||||
err = readMsg.BtcDecode(&buf, wire.RejectVersion-1)
|
||||
if err == nil {
|
||||
t.Errorf("encode of MsgReject succeeded when it shouldn't "+
|
||||
"have %v", msg)
|
||||
@ -226,37 +226,37 @@ func TestRejectCrossProtocol(t *testing.T) {
|
||||
// protocol versions.
|
||||
func TestRejectWire(t *testing.T) {
|
||||
tests := []struct {
|
||||
msg btcwire.MsgReject // Message to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
msg wire.MsgReject // Message to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version rejected command version (no hash).
|
||||
{
|
||||
btcwire.MsgReject{
|
||||
wire.MsgReject{
|
||||
Cmd: "version",
|
||||
Code: btcwire.RejectDuplicate,
|
||||
Code: wire.RejectDuplicate,
|
||||
Reason: "duplicate version",
|
||||
},
|
||||
[]byte{
|
||||
0x07, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, // "version"
|
||||
0x12, // btcwire.RejectDuplicate
|
||||
0x12, // wire.RejectDuplicate
|
||||
0x11, 0x64, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61,
|
||||
0x74, 0x65, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69,
|
||||
0x6f, 0x6e, // "duplicate version"
|
||||
},
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
// Latest protocol version rejected command block (has hash).
|
||||
{
|
||||
btcwire.MsgReject{
|
||||
wire.MsgReject{
|
||||
Cmd: "block",
|
||||
Code: btcwire.RejectDuplicate,
|
||||
Code: wire.RejectDuplicate,
|
||||
Reason: "duplicate block",
|
||||
Hash: mainNetGenesisHash,
|
||||
},
|
||||
[]byte{
|
||||
0x05, 0x62, 0x6c, 0x6f, 0x63, 0x6b, // "block"
|
||||
0x12, // btcwire.RejectDuplicate
|
||||
0x12, // wire.RejectDuplicate
|
||||
0x0f, 0x64, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61,
|
||||
0x74, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, // "duplicate block"
|
||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
||||
@ -264,7 +264,7 @@ func TestRejectWire(t *testing.T) {
|
||||
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
||||
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // mainNetGenesisHash
|
||||
},
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -284,7 +284,7 @@ func TestRejectWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgReject
|
||||
var msg wire.MsgReject
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -302,16 +302,16 @@ func TestRejectWire(t *testing.T) {
|
||||
// TestRejectWireErrors performs negative tests against wire encode and decode
|
||||
// of MsgReject to confirm error paths work correctly.
|
||||
func TestRejectWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pverNoReject := btcwire.RejectVersion - 1
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
pver := wire.ProtocolVersion
|
||||
pverNoReject := wire.RejectVersion - 1
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
baseReject := btcwire.NewMsgReject("block", btcwire.RejectDuplicate,
|
||||
baseReject := wire.NewMsgReject("block", wire.RejectDuplicate,
|
||||
"duplicate block")
|
||||
baseReject.Hash = mainNetGenesisHash
|
||||
baseRejectEncoded := []byte{
|
||||
0x05, 0x62, 0x6c, 0x6f, 0x63, 0x6b, // "block"
|
||||
0x12, // btcwire.RejectDuplicate
|
||||
0x12, // wire.RejectDuplicate
|
||||
0x0f, 0x64, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61,
|
||||
0x74, 0x65, 0x20, 0x62, 0x6c, 0x6f, 0x63, 0x6b, // "duplicate block"
|
||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
||||
@ -321,12 +321,12 @@ func TestRejectWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgReject // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgReject // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with intentional read/write errors.
|
||||
// Force error in reject command.
|
||||
@ -338,7 +338,7 @@ func TestRejectWireErrors(t *testing.T) {
|
||||
// Force error in reject hash.
|
||||
{baseReject, baseRejectEncoded, pver, 23, io.ErrShortWrite, io.EOF},
|
||||
// Force error due to unsupported protocol version.
|
||||
{baseReject, baseRejectEncoded, pverNoReject, 6, btcwireErr, btcwireErr},
|
||||
{baseReject, baseRejectEncoded, pverNoReject, 6, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -352,9 +352,9 @@ func TestRejectWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -363,7 +363,7 @@ func TestRejectWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgReject
|
||||
var msg wire.MsgReject
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -372,9 +372,9 @@ func TestRejectWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -11,24 +11,24 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestTx tests the MsgTx API.
|
||||
func TestTx(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Block 100000 hash.
|
||||
hashStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||
hash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
hash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "tx"
|
||||
msg := btcwire.NewMsgTx()
|
||||
msg := wire.NewMsgTx()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgAddr: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -48,7 +48,7 @@ func TestTx(t *testing.T) {
|
||||
// NOTE: This is a block hash and made up index, but we're only
|
||||
// testing package functionality.
|
||||
prevOutIndex := uint32(1)
|
||||
prevOut := btcwire.NewOutPoint(hash, prevOutIndex)
|
||||
prevOut := wire.NewOutPoint(hash, prevOutIndex)
|
||||
if !prevOut.Hash.IsEqual(hash) {
|
||||
t.Errorf("NewOutPoint: wrong hash - got %v, want %v",
|
||||
spew.Sprint(&prevOut.Hash), spew.Sprint(hash))
|
||||
@ -65,7 +65,7 @@ func TestTx(t *testing.T) {
|
||||
|
||||
// Ensure we get the same transaction input back out.
|
||||
sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62}
|
||||
txIn := btcwire.NewTxIn(prevOut, sigScript)
|
||||
txIn := wire.NewTxIn(prevOut, sigScript)
|
||||
if !reflect.DeepEqual(&txIn.PreviousOutPoint, prevOut) {
|
||||
t.Errorf("NewTxIn: wrong prev outpoint - got %v, want %v",
|
||||
spew.Sprint(&txIn.PreviousOutPoint),
|
||||
@ -92,7 +92,7 @@ func TestTx(t *testing.T) {
|
||||
0xa6, // 65-byte signature
|
||||
0xac, // OP_CHECKSIG
|
||||
}
|
||||
txOut := btcwire.NewTxOut(txValue, pkScript)
|
||||
txOut := wire.NewTxOut(txValue, pkScript)
|
||||
if txOut.Value != txValue {
|
||||
t.Errorf("NewTxOut: wrong pk script - got %v, want %v",
|
||||
txOut.Value, txValue)
|
||||
@ -132,23 +132,23 @@ func TestTx(t *testing.T) {
|
||||
func TestTxSha(t *testing.T) {
|
||||
// Hash of first transaction from block 113875.
|
||||
hashStr := "f051e59b5e2503ac626d03aaeac8ab7be2d72ba4b7e97119c5852d70d52dcb86"
|
||||
wantHash, err := btcwire.NewShaHashFromStr(hashStr)
|
||||
wantHash, err := wire.NewShaHashFromStr(hashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// First transaction from block 113875.
|
||||
msgTx := btcwire.NewMsgTx()
|
||||
txIn := btcwire.TxIn{
|
||||
PreviousOutPoint: btcwire.OutPoint{
|
||||
Hash: btcwire.ShaHash{},
|
||||
msgTx := wire.NewMsgTx()
|
||||
txIn := wire.TxIn{
|
||||
PreviousOutPoint: wire.OutPoint{
|
||||
Hash: wire.ShaHash{},
|
||||
Index: 0xffffffff,
|
||||
},
|
||||
SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
|
||||
Sequence: 0xffffffff,
|
||||
}
|
||||
txOut := btcwire.TxOut{
|
||||
txOut := wire.TxOut{
|
||||
Value: 5000000000,
|
||||
PkScript: []byte{
|
||||
0x41, // OP_DATA_65
|
||||
@ -183,7 +183,7 @@ func TestTxSha(t *testing.T) {
|
||||
// of transaction inputs and outputs and protocol versions.
|
||||
func TestTxWire(t *testing.T) {
|
||||
// Empty tx message.
|
||||
noTx := btcwire.NewMsgTx()
|
||||
noTx := wire.NewMsgTx()
|
||||
noTx.Version = 1
|
||||
noTxEncoded := []byte{
|
||||
0x01, 0x00, 0x00, 0x00, // Version
|
||||
@ -193,17 +193,17 @@ func TestTxWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgTx // Message to encode
|
||||
out *btcwire.MsgTx // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgTx // Message to encode
|
||||
out *wire.MsgTx // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version with no transactions.
|
||||
{
|
||||
noTx,
|
||||
noTx,
|
||||
noTxEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with multiple transactions.
|
||||
@ -211,7 +211,7 @@ func TestTxWire(t *testing.T) {
|
||||
multiTx,
|
||||
multiTx,
|
||||
multiTxEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with no transactions.
|
||||
@ -219,7 +219,7 @@ func TestTxWire(t *testing.T) {
|
||||
noTx,
|
||||
noTx,
|
||||
noTxEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version with multiple transactions.
|
||||
@ -227,7 +227,7 @@ func TestTxWire(t *testing.T) {
|
||||
multiTx,
|
||||
multiTx,
|
||||
multiTxEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with no transactions.
|
||||
@ -235,7 +235,7 @@ func TestTxWire(t *testing.T) {
|
||||
noTx,
|
||||
noTx,
|
||||
noTxEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version with multiple transactions.
|
||||
@ -243,7 +243,7 @@ func TestTxWire(t *testing.T) {
|
||||
multiTx,
|
||||
multiTx,
|
||||
multiTxEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with no transactions.
|
||||
@ -251,7 +251,7 @@ func TestTxWire(t *testing.T) {
|
||||
noTx,
|
||||
noTx,
|
||||
noTxEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with multiple transactions.
|
||||
@ -259,7 +259,7 @@ func TestTxWire(t *testing.T) {
|
||||
multiTx,
|
||||
multiTx,
|
||||
multiTxEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with no transactions.
|
||||
@ -267,7 +267,7 @@ func TestTxWire(t *testing.T) {
|
||||
noTx,
|
||||
noTx,
|
||||
noTxEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion with multiple transactions.
|
||||
@ -275,7 +275,7 @@ func TestTxWire(t *testing.T) {
|
||||
multiTx,
|
||||
multiTx,
|
||||
multiTxEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -295,7 +295,7 @@ func TestTxWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgTx
|
||||
var msg wire.MsgTx
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -319,12 +319,12 @@ func TestTxWireErrors(t *testing.T) {
|
||||
pver := uint32(60002)
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgTx // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgTx // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in version.
|
||||
{multiTx, multiTxEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -366,7 +366,7 @@ func TestTxWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgTx
|
||||
var msg wire.MsgTx
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = msg.BtcDecode(r, test.pver)
|
||||
if err != test.readErr {
|
||||
@ -379,7 +379,7 @@ func TestTxWireErrors(t *testing.T) {
|
||||
|
||||
// TestTxSerialize tests MsgTx serialize and deserialize.
|
||||
func TestTxSerialize(t *testing.T) {
|
||||
noTx := btcwire.NewMsgTx()
|
||||
noTx := wire.NewMsgTx()
|
||||
noTx.Version = 1
|
||||
noTxEncoded := []byte{
|
||||
0x01, 0x00, 0x00, 0x00, // Version
|
||||
@ -389,9 +389,9 @@ func TestTxSerialize(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgTx // Message to encode
|
||||
out *btcwire.MsgTx // Expected decoded message
|
||||
buf []byte // Serialized data
|
||||
in *wire.MsgTx // Message to encode
|
||||
out *wire.MsgTx // Expected decoded message
|
||||
buf []byte // Serialized data
|
||||
}{
|
||||
// No transactions.
|
||||
{
|
||||
@ -424,7 +424,7 @@ func TestTxSerialize(t *testing.T) {
|
||||
}
|
||||
|
||||
// Deserialize the transaction.
|
||||
var tx btcwire.MsgTx
|
||||
var tx wire.MsgTx
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = tx.Deserialize(rbuf)
|
||||
if err != nil {
|
||||
@ -443,11 +443,11 @@ func TestTxSerialize(t *testing.T) {
|
||||
// of MsgTx to confirm error paths work correctly.
|
||||
func TestTxSerializeErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
in *btcwire.MsgTx // Value to encode
|
||||
buf []byte // Serialized data
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgTx // Value to encode
|
||||
buf []byte // Serialized data
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in version.
|
||||
{multiTx, multiTxEncoded, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -489,7 +489,7 @@ func TestTxSerializeErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Deserialize the transaction.
|
||||
var tx btcwire.MsgTx
|
||||
var tx wire.MsgTx
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = tx.Deserialize(r)
|
||||
if err != test.readErr {
|
||||
@ -523,7 +523,7 @@ func TestTxOverflowErrors(t *testing.T) {
|
||||
0x00, 0x00, 0x00, 0x01, // Version
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, // Varint for number of input transactions
|
||||
}, pver, txVer, &btcwire.MessageError{},
|
||||
}, pver, txVer, &wire.MessageError{},
|
||||
},
|
||||
|
||||
// Transaction that claims to have ~uint64(0) outputs.
|
||||
@ -533,7 +533,7 @@ func TestTxOverflowErrors(t *testing.T) {
|
||||
0x00, // Varint for number of input transactions
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, // Varint for number of output transactions
|
||||
}, pver, txVer, &btcwire.MessageError{},
|
||||
}, pver, txVer, &wire.MessageError{},
|
||||
},
|
||||
|
||||
// Transaction that has an input with a signature script that
|
||||
@ -549,7 +549,7 @@ func TestTxOverflowErrors(t *testing.T) {
|
||||
0xff, 0xff, 0xff, 0xff, // Prevous output index
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, // Varint for length of signature script
|
||||
}, pver, txVer, &btcwire.MessageError{},
|
||||
}, pver, txVer, &wire.MessageError{},
|
||||
},
|
||||
|
||||
// Transaction that has an output with a public key script
|
||||
@ -569,14 +569,14 @@ func TestTxOverflowErrors(t *testing.T) {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Transaction amount
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, // Varint for length of public key script
|
||||
}, pver, txVer, &btcwire.MessageError{},
|
||||
}, pver, txVer, &wire.MessageError{},
|
||||
},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgTx
|
||||
var msg wire.MsgTx
|
||||
r := bytes.NewReader(test.buf)
|
||||
err := msg.BtcDecode(r, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
||||
@ -600,12 +600,12 @@ func TestTxOverflowErrors(t *testing.T) {
|
||||
// transactions is accurate.
|
||||
func TestTxSerializeSize(t *testing.T) {
|
||||
// Empty tx message.
|
||||
noTx := btcwire.NewMsgTx()
|
||||
noTx := wire.NewMsgTx()
|
||||
noTx.Version = 1
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgTx // Tx to encode
|
||||
size int // Expected serialized size
|
||||
in *wire.MsgTx // Tx to encode
|
||||
size int // Expected serialized size
|
||||
}{
|
||||
// No inputs or outpus.
|
||||
{noTx, 10},
|
||||
@ -626,12 +626,12 @@ func TestTxSerializeSize(t *testing.T) {
|
||||
}
|
||||
|
||||
// multiTx is a MsgTx with an input and output and used in various tests.
|
||||
var multiTx = &btcwire.MsgTx{
|
||||
var multiTx = &wire.MsgTx{
|
||||
Version: 1,
|
||||
TxIn: []*btcwire.TxIn{
|
||||
TxIn: []*wire.TxIn{
|
||||
{
|
||||
PreviousOutPoint: btcwire.OutPoint{
|
||||
Hash: btcwire.ShaHash{},
|
||||
PreviousOutPoint: wire.OutPoint{
|
||||
Hash: wire.ShaHash{},
|
||||
Index: 0xffffffff,
|
||||
},
|
||||
SignatureScript: []byte{
|
||||
@ -640,7 +640,7 @@ var multiTx = &btcwire.MsgTx{
|
||||
Sequence: 0xffffffff,
|
||||
},
|
||||
},
|
||||
TxOut: []*btcwire.TxOut{
|
||||
TxOut: []*wire.TxOut{
|
||||
{
|
||||
Value: 0x12a05f200,
|
||||
PkScript: []byte{
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"io"
|
@ -1,25 +1,25 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestVerAck tests the MsgVerAck API.
|
||||
func TestVerAck(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Ensure the command is expected value.
|
||||
wantCmd := "verack"
|
||||
msg := btcwire.NewMsgVerAck()
|
||||
msg := wire.NewMsgVerAck()
|
||||
if cmd := msg.Command(); cmd != wantCmd {
|
||||
t.Errorf("NewMsgVerAck: wrong command - got %v want %v",
|
||||
cmd, wantCmd)
|
||||
@ -40,21 +40,21 @@ func TestVerAck(t *testing.T) {
|
||||
// TestVerAckWire tests the MsgVerAck wire encode and decode for various
|
||||
// protocol versions.
|
||||
func TestVerAckWire(t *testing.T) {
|
||||
msgVerAck := btcwire.NewMsgVerAck()
|
||||
msgVerAck := wire.NewMsgVerAck()
|
||||
msgVerAckEncoded := []byte{}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgVerAck // Message to encode
|
||||
out *btcwire.MsgVerAck // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgVerAck // Message to encode
|
||||
out *wire.MsgVerAck // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
msgVerAck,
|
||||
msgVerAck,
|
||||
msgVerAckEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version.
|
||||
@ -62,7 +62,7 @@ func TestVerAckWire(t *testing.T) {
|
||||
msgVerAck,
|
||||
msgVerAck,
|
||||
msgVerAckEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version.
|
||||
@ -70,7 +70,7 @@ func TestVerAckWire(t *testing.T) {
|
||||
msgVerAck,
|
||||
msgVerAck,
|
||||
msgVerAckEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion.
|
||||
@ -78,7 +78,7 @@ func TestVerAckWire(t *testing.T) {
|
||||
msgVerAck,
|
||||
msgVerAck,
|
||||
msgVerAckEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion.
|
||||
@ -86,7 +86,7 @@ func TestVerAckWire(t *testing.T) {
|
||||
msgVerAck,
|
||||
msgVerAck,
|
||||
msgVerAckEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -106,7 +106,7 @@ func TestVerAckWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgVerAck
|
||||
var msg wire.MsgVerAck
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -17,9 +17,10 @@ import (
|
||||
// version message (MsgVersion).
|
||||
const MaxUserAgentLen = 2000
|
||||
|
||||
// DefaultUserAgent for btcwire in the stack
|
||||
// DefaultUserAgent for wire in the stack
|
||||
const DefaultUserAgent = "/btcwire:0.2.0/"
|
||||
|
||||
|
||||
// MsgVersion implements the Message interface and represents a bitcoin version
|
||||
// message. It is used for a peer to advertise itself as soon as an outbound
|
||||
// connection is made. The remote peer then uses this information along with
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -13,33 +13,33 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
// TestVersion tests the MsgVersion API.
|
||||
func TestVersion(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
|
||||
// Create version message data.
|
||||
lastBlock := int32(234234)
|
||||
tcpAddrMe := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
|
||||
me, err := btcwire.NewNetAddress(tcpAddrMe, btcwire.SFNodeNetwork)
|
||||
me, err := wire.NewNetAddress(tcpAddrMe, wire.SFNodeNetwork)
|
||||
if err != nil {
|
||||
t.Errorf("NewNetAddress: %v", err)
|
||||
}
|
||||
tcpAddrYou := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
|
||||
you, err := btcwire.NewNetAddress(tcpAddrYou, btcwire.SFNodeNetwork)
|
||||
you, err := wire.NewNetAddress(tcpAddrYou, wire.SFNodeNetwork)
|
||||
if err != nil {
|
||||
t.Errorf("NewNetAddress: %v", err)
|
||||
}
|
||||
nonce, err := btcwire.RandomUint64()
|
||||
nonce, err := wire.RandomUint64()
|
||||
if err != nil {
|
||||
t.Errorf("RandomUint64: error generating nonce: %v", err)
|
||||
}
|
||||
|
||||
// Ensure we get the correct data back out.
|
||||
msg := btcwire.NewMsgVersion(me, you, nonce, lastBlock)
|
||||
msg := wire.NewMsgVersion(me, you, nonce, lastBlock)
|
||||
if msg.ProtocolVersion != int32(pver) {
|
||||
t.Errorf("NewMsgVersion: wrong protocol version - got %v, want %v",
|
||||
msg.ProtocolVersion, pver)
|
||||
@ -56,9 +56,9 @@ func TestVersion(t *testing.T) {
|
||||
t.Errorf("NewMsgVersion: wrong nonce - got %v, want %v",
|
||||
msg.Nonce, nonce)
|
||||
}
|
||||
if msg.UserAgent != btcwire.DefaultUserAgent {
|
||||
if msg.UserAgent != wire.DefaultUserAgent {
|
||||
t.Errorf("NewMsgVersion: wrong user agent - got %v, want %v",
|
||||
msg.UserAgent, btcwire.DefaultUserAgent)
|
||||
msg.UserAgent, wire.DefaultUserAgent)
|
||||
}
|
||||
if msg.LastBlock != lastBlock {
|
||||
t.Errorf("NewMsgVersion: wrong last block - got %v, want %v",
|
||||
@ -70,7 +70,7 @@ func TestVersion(t *testing.T) {
|
||||
}
|
||||
|
||||
msg.AddUserAgent("myclient", "1.2.3", "optional", "comments")
|
||||
customUserAgent := btcwire.DefaultUserAgent + "myclient:1.2.3(optional; comments)/"
|
||||
customUserAgent := wire.DefaultUserAgent + "myclient:1.2.3(optional; comments)/"
|
||||
if msg.UserAgent != customUserAgent {
|
||||
t.Errorf("AddUserAgent: wrong user agent - got %s, want %s",
|
||||
msg.UserAgent, customUserAgent)
|
||||
@ -85,10 +85,10 @@ func TestVersion(t *testing.T) {
|
||||
|
||||
// accounting for ":", "/"
|
||||
err = msg.AddUserAgent(strings.Repeat("t",
|
||||
btcwire.MaxUserAgentLen-len(customUserAgent)-2+1), "")
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
wire.MaxUserAgentLen-len(customUserAgent)-2+1), "")
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
t.Errorf("AddUserAgent: expected error not received "+
|
||||
"- got %v, want %T", err, btcwire.MessageError{})
|
||||
"- got %v, want %T", err, wire.MessageError{})
|
||||
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ func TestVersion(t *testing.T) {
|
||||
msg.Services, 0)
|
||||
|
||||
}
|
||||
if msg.HasService(btcwire.SFNodeNetwork) {
|
||||
if msg.HasService(wire.SFNodeNetwork) {
|
||||
t.Errorf("HasService: SFNodeNetwork service is set")
|
||||
}
|
||||
|
||||
@ -123,18 +123,18 @@ func TestVersion(t *testing.T) {
|
||||
}
|
||||
|
||||
// Ensure adding the full service node flag works.
|
||||
msg.AddService(btcwire.SFNodeNetwork)
|
||||
if msg.Services != btcwire.SFNodeNetwork {
|
||||
msg.AddService(wire.SFNodeNetwork)
|
||||
if msg.Services != wire.SFNodeNetwork {
|
||||
t.Errorf("AddService: wrong services - got %v, want %v",
|
||||
msg.Services, btcwire.SFNodeNetwork)
|
||||
msg.Services, wire.SFNodeNetwork)
|
||||
}
|
||||
if !msg.HasService(btcwire.SFNodeNetwork) {
|
||||
if !msg.HasService(wire.SFNodeNetwork) {
|
||||
t.Errorf("HasService: SFNodeNetwork service not set")
|
||||
}
|
||||
|
||||
// Use a fake connection.
|
||||
conn := &fakeConn{localAddr: tcpAddrMe, remoteAddr: tcpAddrYou}
|
||||
msg, err = btcwire.NewMsgVersionFromConn(conn, nonce, lastBlock)
|
||||
msg, err = wire.NewMsgVersionFromConn(conn, nonce, lastBlock)
|
||||
if err != nil {
|
||||
t.Errorf("NewMsgVersionFromConn: %v", err)
|
||||
}
|
||||
@ -154,10 +154,10 @@ func TestVersion(t *testing.T) {
|
||||
localAddr: &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333},
|
||||
remoteAddr: tcpAddrYou,
|
||||
}
|
||||
msg, err = btcwire.NewMsgVersionFromConn(conn, nonce, lastBlock)
|
||||
if err != btcwire.ErrInvalidNetAddr {
|
||||
msg, err = wire.NewMsgVersionFromConn(conn, nonce, lastBlock)
|
||||
if err != wire.ErrInvalidNetAddr {
|
||||
t.Errorf("NewMsgVersionFromConn: expected error not received "+
|
||||
"- got %v, want %v", err, btcwire.ErrInvalidNetAddr)
|
||||
"- got %v, want %v", err, wire.ErrInvalidNetAddr)
|
||||
}
|
||||
|
||||
// Use a fake connection with remote UDP addresses to force a failure.
|
||||
@ -165,10 +165,10 @@ func TestVersion(t *testing.T) {
|
||||
localAddr: tcpAddrMe,
|
||||
remoteAddr: &net.UDPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333},
|
||||
}
|
||||
msg, err = btcwire.NewMsgVersionFromConn(conn, nonce, lastBlock)
|
||||
if err != btcwire.ErrInvalidNetAddr {
|
||||
msg, err = wire.NewMsgVersionFromConn(conn, nonce, lastBlock)
|
||||
if err != wire.ErrInvalidNetAddr {
|
||||
t.Errorf("NewMsgVersionFromConn: expected error not received "+
|
||||
"- got %v, want %v", err, btcwire.ErrInvalidNetAddr)
|
||||
"- got %v, want %v", err, wire.ErrInvalidNetAddr)
|
||||
}
|
||||
|
||||
return
|
||||
@ -187,17 +187,17 @@ func TestVersionWire(t *testing.T) {
|
||||
verRelayTxFalseEncoded[len(verRelayTxFalseEncoded)-1] = 0
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgVersion // Message to encode
|
||||
out *btcwire.MsgVersion // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in *wire.MsgVersion // Message to encode
|
||||
out *wire.MsgVersion // Expected decoded message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version.
|
||||
{
|
||||
baseVersionBIP0037,
|
||||
baseVersionBIP0037,
|
||||
baseVersionBIP0037Encoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version BIP0037Version with relay transactions field
|
||||
@ -206,7 +206,7 @@ func TestVersionWire(t *testing.T) {
|
||||
baseVersionBIP0037,
|
||||
baseVersionBIP0037,
|
||||
baseVersionBIP0037Encoded,
|
||||
btcwire.BIP0037Version,
|
||||
wire.BIP0037Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0037Version with relay transactions field
|
||||
@ -215,7 +215,7 @@ func TestVersionWire(t *testing.T) {
|
||||
verRelayTxFalse,
|
||||
verRelayTxFalse,
|
||||
verRelayTxFalseEncoded,
|
||||
btcwire.BIP0037Version,
|
||||
wire.BIP0037Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0035Version.
|
||||
@ -223,7 +223,7 @@ func TestVersionWire(t *testing.T) {
|
||||
baseVersion,
|
||||
baseVersion,
|
||||
baseVersionEncoded,
|
||||
btcwire.BIP0035Version,
|
||||
wire.BIP0035Version,
|
||||
},
|
||||
|
||||
// Protocol version BIP0031Version.
|
||||
@ -231,7 +231,7 @@ func TestVersionWire(t *testing.T) {
|
||||
baseVersion,
|
||||
baseVersion,
|
||||
baseVersionEncoded,
|
||||
btcwire.BIP0031Version,
|
||||
wire.BIP0031Version,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion.
|
||||
@ -239,7 +239,7 @@ func TestVersionWire(t *testing.T) {
|
||||
baseVersion,
|
||||
baseVersion,
|
||||
baseVersionEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version MultipleAddressVersion.
|
||||
@ -247,7 +247,7 @@ func TestVersionWire(t *testing.T) {
|
||||
baseVersion,
|
||||
baseVersion,
|
||||
baseVersionEncoded,
|
||||
btcwire.MultipleAddressVersion,
|
||||
wire.MultipleAddressVersion,
|
||||
},
|
||||
}
|
||||
|
||||
@ -267,7 +267,7 @@ func TestVersionWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgVersion
|
||||
var msg wire.MsgVersion
|
||||
rbuf := bytes.NewBuffer(test.buf)
|
||||
err = msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -289,7 +289,7 @@ func TestVersionWireErrors(t *testing.T) {
|
||||
// because the test data is using bytes encoded with that protocol
|
||||
// version.
|
||||
pver := uint32(60002)
|
||||
btcwireErr := &btcwire.MessageError{}
|
||||
wireErr := &wire.MessageError{}
|
||||
|
||||
// Ensure calling MsgVersion.BtcDecode with a non *bytes.Buffer returns
|
||||
// error.
|
||||
@ -302,12 +302,12 @@ func TestVersionWireErrors(t *testing.T) {
|
||||
// Copy the base version and change the user agent to exceed max limits.
|
||||
bvc := *baseVersion
|
||||
exceedUAVer := &bvc
|
||||
newUA := "/" + strings.Repeat("t", btcwire.MaxUserAgentLen-8+1) + ":0.0.1/"
|
||||
newUA := "/" + strings.Repeat("t", wire.MaxUserAgentLen-8+1) + ":0.0.1/"
|
||||
exceedUAVer.UserAgent = newUA
|
||||
|
||||
// Encode the new UA length as a varint.
|
||||
var newUAVarIntBuf bytes.Buffer
|
||||
err := btcwire.TstWriteVarInt(&newUAVarIntBuf, pver, uint64(len(newUA)))
|
||||
err := wire.TstWriteVarInt(&newUAVarIntBuf, pver, uint64(len(newUA)))
|
||||
if err != nil {
|
||||
t.Errorf("writeVarInt: error %v", err)
|
||||
}
|
||||
@ -324,12 +324,12 @@ func TestVersionWireErrors(t *testing.T) {
|
||||
copy(exceedUAVerEncoded[83+len(newUA):], baseVersionEncoded[97:100])
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.MsgVersion // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.MsgVersion // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Force error in protocol version.
|
||||
{baseVersion, baseVersionEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
||||
@ -353,10 +353,10 @@ func TestVersionWireErrors(t *testing.T) {
|
||||
// it's optional.
|
||||
{
|
||||
baseVersionBIP0037, baseVersionBIP0037Encoded,
|
||||
btcwire.BIP0037Version, 101, io.ErrShortWrite, nil,
|
||||
wire.BIP0037Version, 101, io.ErrShortWrite, nil,
|
||||
},
|
||||
// Force error due to user agent too big
|
||||
{exceedUAVer, exceedUAVerEncoded, pver, newLen, btcwireErr, btcwireErr},
|
||||
{exceedUAVer, exceedUAVerEncoded, pver, newLen, wireErr, wireErr},
|
||||
}
|
||||
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
@ -370,9 +370,9 @@ func TestVersionWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.writeErr {
|
||||
t.Errorf("BtcEncode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.writeErr)
|
||||
@ -381,7 +381,7 @@ func TestVersionWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var msg btcwire.MsgVersion
|
||||
var msg wire.MsgVersion
|
||||
buf := bytes.NewBuffer(test.buf[0:test.max])
|
||||
err = msg.BtcDecode(buf, test.pver)
|
||||
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
|
||||
@ -390,9 +390,9 @@ func TestVersionWireErrors(t *testing.T) {
|
||||
continue
|
||||
}
|
||||
|
||||
// For errors which are not of type btcwire.MessageError, check
|
||||
// For errors which are not of type wire.MessageError, check
|
||||
// them for equality.
|
||||
if _, ok := err.(*btcwire.MessageError); !ok {
|
||||
if _, ok := err.(*wire.MessageError); !ok {
|
||||
if err != test.readErr {
|
||||
t.Errorf("BtcDecode #%d wrong error got: %v, "+
|
||||
"want: %v", i, err, test.readErr)
|
||||
@ -407,13 +407,13 @@ func TestVersionWireErrors(t *testing.T) {
|
||||
func TestVersionOptionalFields(t *testing.T) {
|
||||
// onlyRequiredVersion is a version message that only contains the
|
||||
// required versions and all other values set to their default values.
|
||||
onlyRequiredVersion := btcwire.MsgVersion{
|
||||
onlyRequiredVersion := wire.MsgVersion{
|
||||
ProtocolVersion: 60002,
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)
|
||||
AddrYou: btcwire.NetAddress{
|
||||
AddrYou: wire.NetAddress{
|
||||
Timestamp: time.Time{}, // Zero value -- no timestamp in version
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("192.168.0.1"),
|
||||
Port: 8333,
|
||||
},
|
||||
@ -424,9 +424,9 @@ func TestVersionOptionalFields(t *testing.T) {
|
||||
// addrMeVersion is a version message that contains all fields through
|
||||
// the AddrMe field.
|
||||
addrMeVersion := onlyRequiredVersion
|
||||
addrMeVersion.AddrMe = btcwire.NetAddress{
|
||||
addrMeVersion.AddrMe = wire.NetAddress{
|
||||
Timestamp: time.Time{}, // Zero value -- no timestamp in version
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: 8333,
|
||||
}
|
||||
@ -455,40 +455,40 @@ func TestVersionOptionalFields(t *testing.T) {
|
||||
copy(lastBlockVersionEncoded, baseVersionEncoded)
|
||||
|
||||
tests := []struct {
|
||||
msg *btcwire.MsgVersion // Expected message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
msg *wire.MsgVersion // Expected message
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
{
|
||||
&onlyRequiredVersion,
|
||||
onlyRequiredVersionEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
{
|
||||
&addrMeVersion,
|
||||
addrMeVersionEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
{
|
||||
&nonceVersion,
|
||||
nonceVersionEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
{
|
||||
&uaVersion,
|
||||
uaVersionEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
{
|
||||
&lastBlockVersion,
|
||||
lastBlockVersionEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
}
|
||||
|
||||
for i, test := range tests {
|
||||
// Decode the message from wire format.
|
||||
var msg btcwire.MsgVersion
|
||||
var msg wire.MsgVersion
|
||||
rbuf := bytes.NewBuffer(test.buf)
|
||||
err := msg.BtcDecode(rbuf, test.pver)
|
||||
if err != nil {
|
||||
@ -504,19 +504,19 @@ func TestVersionOptionalFields(t *testing.T) {
|
||||
}
|
||||
|
||||
// baseVersion is used in the various tests as a baseline MsgVersion.
|
||||
var baseVersion = &btcwire.MsgVersion{
|
||||
var baseVersion = &wire.MsgVersion{
|
||||
ProtocolVersion: 60002,
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)
|
||||
AddrYou: btcwire.NetAddress{
|
||||
AddrYou: wire.NetAddress{
|
||||
Timestamp: time.Time{}, // Zero value -- no timestamp in version
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("192.168.0.1"),
|
||||
Port: 8333,
|
||||
},
|
||||
AddrMe: btcwire.NetAddress{
|
||||
AddrMe: wire.NetAddress{
|
||||
Timestamp: time.Time{}, // Zero value -- no timestamp in version
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: 8333,
|
||||
},
|
||||
@ -550,19 +550,19 @@ var baseVersionEncoded = []byte{
|
||||
|
||||
// baseVersionBIP0037 is used in the various tests as a baseline MsgVersion for
|
||||
// BIP0037.
|
||||
var baseVersionBIP0037 = &btcwire.MsgVersion{
|
||||
var baseVersionBIP0037 = &wire.MsgVersion{
|
||||
ProtocolVersion: 70001,
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST)
|
||||
AddrYou: btcwire.NetAddress{
|
||||
AddrYou: wire.NetAddress{
|
||||
Timestamp: time.Time{}, // Zero value -- no timestamp in version
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("192.168.0.1"),
|
||||
Port: 8333,
|
||||
},
|
||||
AddrMe: btcwire.NetAddress{
|
||||
AddrMe: wire.NetAddress{
|
||||
Timestamp: time.Time{}, // Zero value -- no timestamp in version
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: 8333,
|
||||
},
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@ -12,7 +12,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
@ -26,7 +26,7 @@ func TestNetAddress(t *testing.T) {
|
||||
IP: ip,
|
||||
Port: port,
|
||||
}
|
||||
na, err := btcwire.NewNetAddress(tcpAddr, 0)
|
||||
na, err := wire.NewNetAddress(tcpAddr, 0)
|
||||
if err != nil {
|
||||
t.Errorf("NewNetAddress: %v", err)
|
||||
}
|
||||
@ -43,24 +43,24 @@ func TestNetAddress(t *testing.T) {
|
||||
t.Errorf("NetNetAddress: wrong services - got %v, want %v",
|
||||
na.Services, 0)
|
||||
}
|
||||
if na.HasService(btcwire.SFNodeNetwork) {
|
||||
if na.HasService(wire.SFNodeNetwork) {
|
||||
t.Errorf("HasService: SFNodeNetwork service is set")
|
||||
}
|
||||
|
||||
// Ensure adding the full service node flag works.
|
||||
na.AddService(btcwire.SFNodeNetwork)
|
||||
if na.Services != btcwire.SFNodeNetwork {
|
||||
na.AddService(wire.SFNodeNetwork)
|
||||
if na.Services != wire.SFNodeNetwork {
|
||||
t.Errorf("AddService: wrong services - got %v, want %v",
|
||||
na.Services, btcwire.SFNodeNetwork)
|
||||
na.Services, wire.SFNodeNetwork)
|
||||
}
|
||||
if !na.HasService(btcwire.SFNodeNetwork) {
|
||||
if !na.HasService(wire.SFNodeNetwork) {
|
||||
t.Errorf("HasService: SFNodeNetwork service not set")
|
||||
}
|
||||
|
||||
// Ensure max payload is expected value for latest protocol version.
|
||||
pver := btcwire.ProtocolVersion
|
||||
pver := wire.ProtocolVersion
|
||||
wantPayload := uint32(30)
|
||||
maxPayload := btcwire.TstMaxNetAddressPayload(btcwire.ProtocolVersion)
|
||||
maxPayload := wire.TstMaxNetAddressPayload(wire.ProtocolVersion)
|
||||
if maxPayload != wantPayload {
|
||||
t.Errorf("maxNetAddressPayload: wrong max payload length for "+
|
||||
"protocol version %d - got %v, want %v", pver,
|
||||
@ -69,9 +69,9 @@ func TestNetAddress(t *testing.T) {
|
||||
|
||||
// Protocol version before NetAddressTimeVersion when timestamp was
|
||||
// added. Ensure max payload is expected value for it.
|
||||
pver = btcwire.NetAddressTimeVersion - 1
|
||||
pver = wire.NetAddressTimeVersion - 1
|
||||
wantPayload = 26
|
||||
maxPayload = btcwire.TstMaxNetAddressPayload(pver)
|
||||
maxPayload = wire.TstMaxNetAddressPayload(pver)
|
||||
if maxPayload != wantPayload {
|
||||
t.Errorf("maxNetAddressPayload: wrong max payload length for "+
|
||||
"protocol version %d - got %v, want %v", pver,
|
||||
@ -80,10 +80,10 @@ func TestNetAddress(t *testing.T) {
|
||||
|
||||
// Check for expected failure on wrong address type.
|
||||
udpAddr := &net.UDPAddr{}
|
||||
_, err = btcwire.NewNetAddress(udpAddr, 0)
|
||||
if err != btcwire.ErrInvalidNetAddr {
|
||||
_, err = wire.NewNetAddress(udpAddr, 0)
|
||||
if err != wire.ErrInvalidNetAddr {
|
||||
t.Errorf("NewNetAddress: expected error not received - "+
|
||||
"got %v, want %v", err, btcwire.ErrInvalidNetAddr)
|
||||
"got %v, want %v", err, wire.ErrInvalidNetAddr)
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,9 +91,9 @@ func TestNetAddress(t *testing.T) {
|
||||
// protocol versions and timestamp flag combinations.
|
||||
func TestNetAddressWire(t *testing.T) {
|
||||
// baseNetAddr is used in the various tests as a baseline NetAddress.
|
||||
baseNetAddr := btcwire.NetAddress{
|
||||
baseNetAddr := wire.NetAddress{
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: 8333,
|
||||
}
|
||||
@ -121,11 +121,11 @@ func TestNetAddressWire(t *testing.T) {
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in btcwire.NetAddress // NetAddress to encode
|
||||
out btcwire.NetAddress // Expected decoded NetAddress
|
||||
ts bool // Include timestamp?
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
in wire.NetAddress // NetAddress to encode
|
||||
out wire.NetAddress // Expected decoded NetAddress
|
||||
ts bool // Include timestamp?
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
}{
|
||||
// Latest protocol version without ts flag.
|
||||
{
|
||||
@ -133,7 +133,7 @@ func TestNetAddressWire(t *testing.T) {
|
||||
baseNetAddrNoTS,
|
||||
false,
|
||||
baseNetAddrNoTSEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Latest protocol version with ts flag.
|
||||
@ -142,7 +142,7 @@ func TestNetAddressWire(t *testing.T) {
|
||||
baseNetAddr,
|
||||
true,
|
||||
baseNetAddrEncoded,
|
||||
btcwire.ProtocolVersion,
|
||||
wire.ProtocolVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion without ts flag.
|
||||
@ -151,7 +151,7 @@ func TestNetAddressWire(t *testing.T) {
|
||||
baseNetAddrNoTS,
|
||||
false,
|
||||
baseNetAddrNoTSEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion with ts flag.
|
||||
@ -160,7 +160,7 @@ func TestNetAddressWire(t *testing.T) {
|
||||
baseNetAddr,
|
||||
true,
|
||||
baseNetAddrEncoded,
|
||||
btcwire.NetAddressTimeVersion,
|
||||
wire.NetAddressTimeVersion,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion-1 without ts flag.
|
||||
@ -169,7 +169,7 @@ func TestNetAddressWire(t *testing.T) {
|
||||
baseNetAddrNoTS,
|
||||
false,
|
||||
baseNetAddrNoTSEncoded,
|
||||
btcwire.NetAddressTimeVersion - 1,
|
||||
wire.NetAddressTimeVersion - 1,
|
||||
},
|
||||
|
||||
// Protocol version NetAddressTimeVersion-1 with timestamp.
|
||||
@ -181,7 +181,7 @@ func TestNetAddressWire(t *testing.T) {
|
||||
baseNetAddrNoTS,
|
||||
true,
|
||||
baseNetAddrNoTSEncoded,
|
||||
btcwire.NetAddressTimeVersion - 1,
|
||||
wire.NetAddressTimeVersion - 1,
|
||||
},
|
||||
}
|
||||
|
||||
@ -189,7 +189,7 @@ func TestNetAddressWire(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
var buf bytes.Buffer
|
||||
err := btcwire.TstWriteNetAddress(&buf, test.pver, &test.in, test.ts)
|
||||
err := wire.TstWriteNetAddress(&buf, test.pver, &test.in, test.ts)
|
||||
if err != nil {
|
||||
t.Errorf("writeNetAddress #%d error %v", i, err)
|
||||
continue
|
||||
@ -201,9 +201,9 @@ func TestNetAddressWire(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode the message from wire format.
|
||||
var na btcwire.NetAddress
|
||||
var na wire.NetAddress
|
||||
rbuf := bytes.NewReader(test.buf)
|
||||
err = btcwire.TstReadNetAddress(rbuf, test.pver, &na, test.ts)
|
||||
err = wire.TstReadNetAddress(rbuf, test.pver, &na, test.ts)
|
||||
if err != nil {
|
||||
t.Errorf("readNetAddress #%d error %v", i, err)
|
||||
continue
|
||||
@ -219,25 +219,25 @@ func TestNetAddressWire(t *testing.T) {
|
||||
// TestNetAddressWireErrors performs negative tests against wire encode and
|
||||
// decode NetAddress to confirm error paths work correctly.
|
||||
func TestNetAddressWireErrors(t *testing.T) {
|
||||
pver := btcwire.ProtocolVersion
|
||||
pverNAT := btcwire.NetAddressTimeVersion - 1
|
||||
pver := wire.ProtocolVersion
|
||||
pverNAT := wire.NetAddressTimeVersion - 1
|
||||
|
||||
// baseNetAddr is used in the various tests as a baseline NetAddress.
|
||||
baseNetAddr := btcwire.NetAddress{
|
||||
baseNetAddr := wire.NetAddress{
|
||||
Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
|
||||
Services: btcwire.SFNodeNetwork,
|
||||
Services: wire.SFNodeNetwork,
|
||||
IP: net.ParseIP("127.0.0.1"),
|
||||
Port: 8333,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
in *btcwire.NetAddress // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
ts bool // Include timestamp flag
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
in *wire.NetAddress // Value to encode
|
||||
buf []byte // Wire encoding
|
||||
pver uint32 // Protocol version for wire encoding
|
||||
ts bool // Include timestamp flag
|
||||
max int // Max size of fixed buffer to induce errors
|
||||
writeErr error // Expected write error
|
||||
readErr error // Expected read error
|
||||
}{
|
||||
// Latest protocol version with timestamp and intentional
|
||||
// read/write errors.
|
||||
@ -274,7 +274,7 @@ func TestNetAddressWireErrors(t *testing.T) {
|
||||
for i, test := range tests {
|
||||
// Encode to wire format.
|
||||
w := newFixedWriter(test.max)
|
||||
err := btcwire.TstWriteNetAddress(w, test.pver, test.in, test.ts)
|
||||
err := wire.TstWriteNetAddress(w, test.pver, test.in, test.ts)
|
||||
if err != test.writeErr {
|
||||
t.Errorf("writeNetAddress #%d wrong error got: %v, want: %v",
|
||||
i, err, test.writeErr)
|
||||
@ -282,9 +282,9 @@ func TestNetAddressWireErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
// Decode from wire format.
|
||||
var na btcwire.NetAddress
|
||||
var na wire.NetAddress
|
||||
r := newFixedReader(test.max, test.buf)
|
||||
err = btcwire.TstReadNetAddress(r, test.pver, &na, test.ts)
|
||||
err = wire.TstReadNetAddress(r, test.pver, &na, test.ts)
|
||||
if err != test.readErr {
|
||||
t.Errorf("readNetAddress #%d wrong error got: %v, want: %v",
|
||||
i, err, test.readErr)
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -1,23 +1,23 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// TestServiceFlagStringer tests the stringized output for service flag types.
|
||||
func TestServiceFlagStringer(t *testing.T) {
|
||||
tests := []struct {
|
||||
in btcwire.ServiceFlag
|
||||
in wire.ServiceFlag
|
||||
want string
|
||||
}{
|
||||
{0, "0x0"},
|
||||
{btcwire.SFNodeNetwork, "SFNodeNetwork"},
|
||||
{wire.SFNodeNetwork, "SFNodeNetwork"},
|
||||
{0xffffffff, "SFNodeNetwork|0xfffffffe"},
|
||||
}
|
||||
|
||||
@ -35,13 +35,13 @@ func TestServiceFlagStringer(t *testing.T) {
|
||||
// TestBitcoinNetStringer tests the stringized output for bitcoin net types.
|
||||
func TestBitcoinNetStringer(t *testing.T) {
|
||||
tests := []struct {
|
||||
in btcwire.BitcoinNet
|
||||
in wire.BitcoinNet
|
||||
want string
|
||||
}{
|
||||
{btcwire.MainNet, "MainNet"},
|
||||
{btcwire.TestNet, "TestNet"},
|
||||
{btcwire.TestNet3, "TestNet3"},
|
||||
{btcwire.SimNet, "SimNet"},
|
||||
{wire.MainNet, "MainNet"},
|
||||
{wire.TestNet, "TestNet"},
|
||||
{wire.TestNet3, "TestNet3"},
|
||||
{wire.SimNet, "SimNet"},
|
||||
{0xffffffff, "Unknown BitcoinNet (4294967295)"},
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire
|
||||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,15 +1,15 @@
|
||||
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
||||
// Copyright (c) 2013-2015 Conformal Systems LLC.
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package btcwire_test
|
||||
package wire_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/btcsuite/btcwire"
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
)
|
||||
|
||||
// TestShaHash tests the ShaHash API.
|
||||
@ -17,7 +17,7 @@ func TestShaHash(t *testing.T) {
|
||||
|
||||
// Hash of block 234439.
|
||||
blockHashStr := "14a0810ac680a3eb3f82edc878cea25ec41d6b790744e5daeef"
|
||||
blockHash, err := btcwire.NewShaHashFromStr(blockHashStr)
|
||||
blockHash, err := wire.NewShaHashFromStr(blockHashStr)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHashFromStr: %v", err)
|
||||
}
|
||||
@ -30,15 +30,15 @@ func TestShaHash(t *testing.T) {
|
||||
0xa6, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
}
|
||||
|
||||
hash, err := btcwire.NewShaHash(buf)
|
||||
hash, err := wire.NewShaHash(buf)
|
||||
if err != nil {
|
||||
t.Errorf("NewShaHash: unexpected error %v", err)
|
||||
}
|
||||
|
||||
// Ensure proper size.
|
||||
if len(hash) != btcwire.HashSize {
|
||||
if len(hash) != wire.HashSize {
|
||||
t.Errorf("NewShaHash: hash length mismatch - got: %v, want: %v",
|
||||
len(hash), btcwire.HashSize)
|
||||
len(hash), wire.HashSize)
|
||||
}
|
||||
|
||||
// Ensure contents match.
|
||||
@ -70,8 +70,8 @@ func TestShaHash(t *testing.T) {
|
||||
}
|
||||
|
||||
// Invalid size for NewShaHash.
|
||||
invalidHash := make([]byte, btcwire.HashSize+1)
|
||||
_, err = btcwire.NewShaHash(invalidHash)
|
||||
invalidHash := make([]byte, wire.HashSize+1)
|
||||
_, err = wire.NewShaHash(invalidHash)
|
||||
if err == nil {
|
||||
t.Errorf("NewShaHash: failed to received expected err - got: nil")
|
||||
}
|
||||
@ -81,7 +81,7 @@ func TestShaHash(t *testing.T) {
|
||||
func TestShaHashString(t *testing.T) {
|
||||
// Block 100000 hash.
|
||||
wantStr := "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||
hash := btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
hash := wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0x06, 0xe5, 0x33, 0xfd, 0x1a, 0xda, 0x86, 0x39,
|
||||
0x1f, 0x3f, 0x6c, 0x34, 0x32, 0x04, 0xb0, 0xd2,
|
||||
0x78, 0xd4, 0xaa, 0xec, 0x1c, 0x0b, 0x20, 0xaa,
|
||||
@ -99,7 +99,7 @@ func TestShaHashString(t *testing.T) {
|
||||
func TestNewShaHashFromStr(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
want btcwire.ShaHash
|
||||
want wire.ShaHash
|
||||
err error
|
||||
}{
|
||||
// Genesis hash.
|
||||
@ -119,14 +119,14 @@ func TestNewShaHashFromStr(t *testing.T) {
|
||||
// Empty string.
|
||||
{
|
||||
"",
|
||||
btcwire.ShaHash{},
|
||||
wire.ShaHash{},
|
||||
nil,
|
||||
},
|
||||
|
||||
// Single digit hash.
|
||||
{
|
||||
"1",
|
||||
btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
@ -138,7 +138,7 @@ func TestNewShaHashFromStr(t *testing.T) {
|
||||
// Block 203707 with stripped leading zeros.
|
||||
{
|
||||
"3264bc2ac36a60840790ba1d475d01367e7c723da941069e9dc",
|
||||
btcwire.ShaHash([btcwire.HashSize]byte{ // Make go vet happy.
|
||||
wire.ShaHash([wire.HashSize]byte{ // Make go vet happy.
|
||||
0xdc, 0xe9, 0x69, 0x10, 0x94, 0xda, 0x23, 0xc7,
|
||||
0xe7, 0x67, 0x13, 0xd0, 0x75, 0xd4, 0xa1, 0x0b,
|
||||
0x79, 0x40, 0x08, 0xa6, 0x36, 0xac, 0xc2, 0x4b,
|
||||
@ -150,14 +150,14 @@ func TestNewShaHashFromStr(t *testing.T) {
|
||||
// Hash string that is too long.
|
||||
{
|
||||
"01234567890123456789012345678901234567890123456789012345678912345",
|
||||
btcwire.ShaHash{},
|
||||
btcwire.ErrHashStrSize,
|
||||
wire.ShaHash{},
|
||||
wire.ErrHashStrSize,
|
||||
},
|
||||
|
||||
// Hash string that is contains non-hex chars.
|
||||
{
|
||||
"abcdefg",
|
||||
btcwire.ShaHash{},
|
||||
wire.ShaHash{},
|
||||
hex.InvalidByteError('g'),
|
||||
},
|
||||
}
|
||||
@ -166,7 +166,7 @@ func TestNewShaHashFromStr(t *testing.T) {
|
||||
unexpectedResultStr := "NewShaHashFromStr #%d got: %v want: %v"
|
||||
t.Logf("Running %d tests", len(tests))
|
||||
for i, test := range tests {
|
||||
result, err := btcwire.NewShaHashFromStr(test.in)
|
||||
result, err := wire.NewShaHashFromStr(test.in)
|
||||
if err != test.err {
|
||||
t.Errorf(unexpectedErrStr, i, err, test.err)
|
||||
continue
|
Loading…
Reference in New Issue
Block a user