[NOD-262] Renamed all instances of GetBlocks to GetBlockInvs. (#359)

This commit is contained in:
stasatdaglabs 2019-08-11 12:25:29 +03:00 committed by GitHub
parent 594a209f83
commit e5485ac5e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 133 additions and 133 deletions

View File

@ -293,7 +293,7 @@ func (sm *SyncManager) startSync() {
"%d from peer %s", sm.dag.ChainHeight()+1,
sm.nextCheckpoint.ChainHeight, bestPeer.Addr()) //TODO: (Ori) This is probably wrong. Done only for compilation
} else {
bestPeer.PushGetBlocksMsg(locator, &daghash.ZeroHash)
bestPeer.PushGetBlockInvsMsg(locator, &daghash.ZeroHash)
}
sm.syncPeer = bestPeer
} else {
@ -674,9 +674,9 @@ func (sm *SyncManager) handleBlockMsg(bmsg *blockMsg) {
sm.headerList.Init()
log.Infof("Reached the final checkpoint -- switching to normal mode")
locator := blockdag.BlockLocator([]*daghash.Hash{blockHash})
err = peer.PushGetBlocksMsg(locator, &daghash.ZeroHash)
err = peer.PushGetBlockInvsMsg(locator, &daghash.ZeroHash)
if err != nil {
log.Warnf("Failed to send getblocks message to peer %s: %s",
log.Warnf("Failed to send getblockinvs message to peer %s: %s",
peer.Addr(), err)
return
}
@ -1018,7 +1018,7 @@ func (sm *SyncManager) handleInvMsg(imsg *invMsg) {
// final one the remote peer knows about (zero
// stop hash).
locator := sm.dag.BlockLocatorFromHash(iv.Hash)
peer.PushGetBlocksMsg(locator, &daghash.ZeroHash)
peer.PushGetBlockInvsMsg(locator, &daghash.ZeroHash)
}
}
}

View File

@ -47,7 +47,7 @@ A quick overview of the major features peer provides are as follows:
- Does not invoke the related callbacks for older protocol versions
- Snapshottable peer statistics such as the total number of bytes read and
written, the remote address, user agent, and negotiated protocol version
- Helper functions pushing addresses, getblocks, getheaders, and reject
- Helper functions pushing addresses, getblockinvs, getheaders, and reject
messages
- These could all be sent manually via the standard message output function,
but the helpers provide additional nice functionality such as duplicate

View File

@ -42,7 +42,7 @@ A quick overview of the major features peer provides are as follows:
- Does not invoke the related callbacks for older protocol versions
- Snapshottable peer statistics such as the total number of bytes read and
written, the remote address, user agent, and negotiated protocol version
- Helper functions pushing addresses, getblocks, getheaders, and reject
- Helper functions pushing addresses, getblockinvs, getheaders, and reject
messages
- These could all be sent manually via the standard message output function,
but the helpers provide additional nice functionality such as duplicate
@ -109,7 +109,7 @@ of a most-recently used algorithm.
Message Sending Helper Functions
In addition to the bare QueueMessage function previously described, the
PushAddrMsg, PushGetBlocksMsg, PushGetHeadersMsg, and PushRejectMsg functions
PushAddrMsg, PushGetBlockInvsMsg, PushGetHeadersMsg, and PushRejectMsg functions
are provided as a convenience. While it is of course possible to create and
send these message manually via QueueMessage, these helper functions provided
additional useful functionality that is typically desired.
@ -120,7 +120,7 @@ there are too many. This allows the caller to simply provide a slice of known
addresses, such as that returned by the addrmgr package, without having to worry
about the details.
Next, the PushGetBlocksMsg and PushGetHeadersMsg functions will construct proper
Next, the PushGetBlockInvsMsg and PushGetHeadersMsg functions will construct proper
messages using a block locator and ignore back to back duplicate requests.
Finally, the PushRejectMsg function can be used to easily create and send an

View File

@ -178,7 +178,7 @@ func messageSummary(msg wire.Message) string {
case *wire.MsgGetData:
return invSummary(msg.InvList)
case *wire.MsgGetBlocks:
case *wire.MsgGetBlockInvs:
return locatorSummary(msg.BlockLocatorHashes, msg.StopHash)
case *wire.MsgGetHeaders:

View File

@ -147,9 +147,9 @@ type MessageListeners struct {
// OnGetData is invoked when a peer receives a getdata bitcoin message.
OnGetData func(p *Peer, msg *wire.MsgGetData)
// OnGetBlocks is invoked when a peer receives a getblocks bitcoin
// OnGetBlockInvs is invoked when a peer receives a getblockinvs bitcoin
// message.
OnGetBlocks func(p *Peer, msg *wire.MsgGetBlocks)
OnGetBlockInvs func(p *Peer, msg *wire.MsgGetBlockInvs)
// OnGetHeaders is invoked when a peer receives a getheaders bitcoin
// message.
@ -443,13 +443,13 @@ type Peer struct {
sendHeadersPreferred bool // peer sent a sendheaders message
verAckReceived bool
knownInventory *mruInventoryMap
prevGetBlocksMtx sync.Mutex
prevGetBlocksBegin *daghash.Hash
prevGetBlocksStop *daghash.Hash
prevGetHdrsMtx sync.Mutex
prevGetHdrsBegin *daghash.Hash
prevGetHdrsStop *daghash.Hash
knownInventory *mruInventoryMap
prevGetBlockInvsMtx sync.Mutex
prevGetBlockInvsBegin *daghash.Hash
prevGetBlockInvsStop *daghash.Hash
prevGetHdrsMtx sync.Mutex
prevGetHdrsBegin *daghash.Hash
prevGetHdrsStop *daghash.Hash
// These fields keep track of statistics for the peer and are protected
// by the statsMtx mutex.
@ -858,33 +858,33 @@ func (p *Peer) PushAddrMsg(addresses []*wire.NetAddress, subnetworkID *subnetwor
return msg.AddrList, nil
}
// PushGetBlocksMsg sends a getblocks message for the provided block locator
// PushGetBlockInvsMsg sends a getblockinvs message for the provided block locator
// and stop hash. It will ignore back-to-back duplicate requests.
//
// This function is safe for concurrent access.
func (p *Peer) PushGetBlocksMsg(locator blockdag.BlockLocator, stopHash *daghash.Hash) error {
func (p *Peer) PushGetBlockInvsMsg(locator blockdag.BlockLocator, stopHash *daghash.Hash) error {
// Extract the begin hash from the block locator, if one was specified,
// to use for filtering duplicate getblocks requests.
// to use for filtering duplicate getblockinvs requests.
var beginHash *daghash.Hash
if len(locator) > 0 {
beginHash = locator[0]
}
// Filter duplicate getblocks requests.
p.prevGetBlocksMtx.Lock()
isDuplicate := p.prevGetBlocksStop != nil && p.prevGetBlocksBegin != nil &&
beginHash != nil && stopHash.IsEqual(p.prevGetBlocksStop) &&
beginHash.IsEqual(p.prevGetBlocksBegin)
p.prevGetBlocksMtx.Unlock()
// Filter duplicate getblockinvs requests.
p.prevGetBlockInvsMtx.Lock()
isDuplicate := p.prevGetBlockInvsStop != nil && p.prevGetBlockInvsBegin != nil &&
beginHash != nil && stopHash.IsEqual(p.prevGetBlockInvsStop) &&
beginHash.IsEqual(p.prevGetBlockInvsBegin)
p.prevGetBlockInvsMtx.Unlock()
if isDuplicate {
log.Tracef("Filtering duplicate [getblocks] with begin "+
log.Tracef("Filtering duplicate [getblockinvs] with begin "+
"hash %s, stop hash %s", beginHash, stopHash)
return nil
}
// Construct the getblocks request and queue it to be sent.
msg := wire.NewMsgGetBlocks(stopHash)
// Construct the getblockinvs request and queue it to be sent.
msg := wire.NewMsgGetBlockInvs(stopHash)
for _, hash := range locator {
err := msg.AddBlockLocatorHash(hash)
if err != nil {
@ -893,16 +893,16 @@ func (p *Peer) PushGetBlocksMsg(locator blockdag.BlockLocator, stopHash *daghash
}
p.QueueMessage(msg, nil)
// Update the previous getblocks request information for filtering
// Update the previous getblockinvs request information for filtering
// duplicates.
p.prevGetBlocksMtx.Lock()
p.prevGetBlocksBegin = beginHash
p.prevGetBlocksStop = stopHash
p.prevGetBlocksMtx.Unlock()
p.prevGetBlockInvsMtx.Lock()
p.prevGetBlockInvsBegin = beginHash
p.prevGetBlockInvsStop = stopHash
p.prevGetBlockInvsMtx.Unlock()
return nil
}
// PushGetHeadersMsg sends a getblocks message for the provided block locator
// PushGetHeadersMsg sends a getblockinvs message for the provided block locator
// and stop hash. It will ignore back-to-back duplicate requests.
//
// This function is safe for concurrent access.
@ -1211,7 +1211,7 @@ func (p *Peer) maybeAddDeadline(pendingResponses map[string]time.Time, msgCmd st
// Expects an inv message.
pendingResponses[wire.CmdInv] = deadline
case wire.CmdGetBlocks:
case wire.CmdGetBlockInvs:
// Expects an inv message.
pendingResponses[wire.CmdInv] = deadline
@ -1517,9 +1517,9 @@ out:
p.cfg.Listeners.OnGetData(p, msg)
}
case *wire.MsgGetBlocks:
if p.cfg.Listeners.OnGetBlocks != nil {
p.cfg.Listeners.OnGetBlocks(p, msg)
case *wire.MsgGetBlockInvs:
if p.cfg.Listeners.OnGetBlockInvs != nil {
p.cfg.Listeners.OnGetBlockInvs(p, msg)
}
case *wire.MsgGetHeaders:

View File

@ -375,7 +375,7 @@ func TestPeerListeners(t *testing.T) {
OnGetData: func(p *peer.Peer, msg *wire.MsgGetData) {
ok <- msg
},
OnGetBlocks: func(p *peer.Peer, msg *wire.MsgGetBlocks) {
OnGetBlockInvs: func(p *peer.Peer, msg *wire.MsgGetBlockInvs) {
ok <- msg
},
OnGetHeaders: func(p *peer.Peer, msg *wire.MsgGetHeaders) {
@ -513,8 +513,8 @@ func TestPeerListeners(t *testing.T) {
wire.NewMsgGetData(),
},
{
"OnGetBlocks",
wire.NewMsgGetBlocks(&daghash.Hash{}),
"OnGetBlockInvs",
wire.NewMsgGetBlockInvs(&daghash.Hash{}),
},
{
"OnGetHeaders",
@ -677,8 +677,8 @@ func TestOutboundPeer(t *testing.T) {
t.Errorf("PushAddrMsg: unexpected err %v\n", err)
return
}
if err := p2.PushGetBlocksMsg(nil, &daghash.Hash{}); err != nil {
t.Errorf("PushGetBlocksMsg: unexpected err %v\n", err)
if err := p2.PushGetBlockInvsMsg(nil, &daghash.Hash{}); err != nil {
t.Errorf("PushGetBlockInvsMsg: unexpected err %v\n", err)
return
}
if err := p2.PushGetHeadersMsg(nil, &daghash.Hash{}); err != nil {

View File

@ -682,9 +682,9 @@ func (sp *Peer) OnGetData(_ *peer.Peer, msg *wire.MsgGetData) {
}
}
// OnGetBlocks is invoked when a peer receives a getblocks bitcoin
// OnGetBlockInvs is invoked when a peer receives a getblockinvs bitcoin
// message.
func (sp *Peer) OnGetBlocks(_ *peer.Peer, msg *wire.MsgGetBlocks) {
func (sp *Peer) OnGetBlockInvs(_ *peer.Peer, msg *wire.MsgGetBlockInvs) {
// Find the most recent known block in the dag based on the block
// locator and fetch all of the block hashes after it until either
// wire.MaxBlocksPerMsg have been fetched or the provided stop hash is
@ -1778,7 +1778,7 @@ func newPeerConfig(sp *Peer) *peer.Config {
OnInv: sp.OnInv,
OnHeaders: sp.OnHeaders,
OnGetData: sp.OnGetData,
OnGetBlocks: sp.OnGetBlocks,
OnGetBlockInvs: sp.OnGetBlockInvs,
OnGetHeaders: sp.OnGetHeaders,
OnGetCFilters: sp.OnGetCFilters,
OnGetCFHeaders: sp.OnGetCFHeaders,

View File

@ -445,12 +445,12 @@ func BenchmarkDecodeHeaders(b *testing.B) {
}
}
// BenchmarkDecodeGetBlocks performs a benchmark on how long it takes to
// decode a getblocks message with the maximum number of block locator hashes.
func BenchmarkDecodeGetBlocks(b *testing.B) {
// BenchmarkDecodeGetBlockInvs performs a benchmark on how long it takes to
// decode a getblockinvs message with the maximum number of block locator hashes.
func BenchmarkDecodeGetBlockInvs(b *testing.B) {
// Create a message with the maximum number of block locators.
pver := ProtocolVersion
var m MsgGetBlocks
var m MsgGetBlockInvs
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
hash, err := daghash.NewHashFromStr(fmt.Sprintf("%x", i))
if err != nil {
@ -462,12 +462,12 @@ func BenchmarkDecodeGetBlocks(b *testing.B) {
// Serialize it so the bytes are available to test the decode below.
var bb bytes.Buffer
if err := m.BtcEncode(&bb, pver); err != nil {
b.Fatalf("MsgGetBlocks.BtcEncode: unexpected error: %v", err)
b.Fatalf("MsgGetBlockInvs.BtcEncode: unexpected error: %v", err)
}
buf := bb.Bytes()
r := bytes.NewReader(buf)
var msg MsgGetBlocks
var msg MsgGetBlockInvs
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Seek(0, 0)

View File

@ -45,17 +45,17 @@ things such as protocol version and supported services with each other. Once
the initial handshake is complete, the following chart indicates message
interactions in no particular order.
Peer A Sends Peer B Responds
Peer A Sends Peer B Responds
----------------------------------------------------------------------------
getaddr message (MsgGetAddr) addr message (MsgAddr)
getblocks message (MsgGetBlocks) inv message (MsgInv)
inv message (MsgInv) getdata message (MsgGetData)
getdata message (MsgGetData) block message (MsgBlock) -or-
tx message (MsgTx) -or-
notfound message (MsgNotFound)
getheaders message (MsgGetHeaders) headers message (MsgHeaders)
ping message (MsgPing) pong message (MsgHeaders)* -or-
(none -- Ability to send message is enough)
getaddr message (MsgGetAddr) addr message (MsgAddr)
getblockinvs message (MsgGetBlockInvs) inv message (MsgInv)
inv message (MsgInv) getdata message (MsgGetData)
getdata message (MsgGetData) block message (MsgBlock) -or-
tx message (MsgTx) -or-
notfound message (MsgNotFound)
getheaders message (MsgGetHeaders) headers message (MsgHeaders)
ping message (MsgPing) pong message (MsgHeaders)* -or-
(none -- Ability to send message is enough)
NOTES:
* The pong message was not added until later protocol versions as defined

View File

@ -32,7 +32,7 @@ const (
CmdVerAck = "verack"
CmdGetAddr = "getaddr"
CmdAddr = "addr"
CmdGetBlocks = "getblocks"
CmdGetBlockInvs = "getblockinvs"
CmdInv = "inv"
CmdGetData = "getdata"
CmdNotFound = "notfound"
@ -87,8 +87,8 @@ func makeEmptyMessage(command string) (Message, error) {
case CmdAddr:
msg = &MsgAddr{}
case CmdGetBlocks:
msg = &MsgGetBlocks{}
case CmdGetBlockInvs:
msg = &MsgGetBlockInvs{}
case CmdBlock:
msg = &MsgBlock{}

View File

@ -51,7 +51,7 @@ func TestMessage(t *testing.T) {
msgVerack := NewMsgVerAck()
msgGetAddr := NewMsgGetAddr(false, nil)
msgAddr := NewMsgAddr(false, nil)
msgGetBlocks := NewMsgGetBlocks(&daghash.Hash{})
msgGetBlockInvs := NewMsgGetBlockInvs(&daghash.Hash{})
msgBlock := &blockOne
msgInv := NewMsgInv()
msgGetData := NewMsgGetData()
@ -90,7 +90,7 @@ func TestMessage(t *testing.T) {
{msgVerack, msgVerack, pver, MainNet, 24},
{msgGetAddr, msgGetAddr, pver, MainNet, 26},
{msgAddr, msgAddr, pver, MainNet, 27},
{msgGetBlocks, msgGetBlocks, pver, MainNet, 61},
{msgGetBlockInvs, msgGetBlockInvs, pver, MainNet, 61},
{msgBlock, msgBlock, pver, MainNet, 372},
{msgInv, msgInv, pver, MainNet, 25},
{msgGetData, msgGetData, pver, MainNet, 25},

View File

@ -15,11 +15,11 @@ import (
// per message.
const MaxBlockLocatorsPerMsg = 500
// MsgGetBlocks implements the Message interface and represents a bitcoin
// getblocks message. It is used to request a list of blocks starting after the
// last known hash in the slice of block locator hashes. The list is returned
// via an inv message (MsgInv) and is limited by a specific hash to stop at or
// the maximum number of blocks per message, which is currently 500.
// MsgGetBlockInvs implements the Message interface and represents a bitcoin
// getblockinvss message. It is used to request a list of blocks starting after
// the last known hash in the slice of block locator hashes. The list is
// returned via an inv message (MsgInv) and is limited by a specific hash to
// stop at or the maximum number of blocks per message, which is currently 500.
//
// Set the StopHash field to the hash at which to stop and use
// AddBlockLocatorHash to build up the list of block locator hashes.
@ -30,18 +30,18 @@ const MaxBlockLocatorsPerMsg = 500
// most recent 10 block hashes, then double the step each loop iteration to
// exponentially decrease the number of hashes the further away from head and
// closer to the genesis block you get.
type MsgGetBlocks struct {
type MsgGetBlockInvs struct {
ProtocolVersion uint32
BlockLocatorHashes []*daghash.Hash
StopHash *daghash.Hash
}
// AddBlockLocatorHash adds a new block locator hash to the message.
func (msg *MsgGetBlocks) AddBlockLocatorHash(hash *daghash.Hash) error {
func (msg *MsgGetBlockInvs) AddBlockLocatorHash(hash *daghash.Hash) error {
if len(msg.BlockLocatorHashes)+1 > MaxBlockLocatorsPerMsg {
str := fmt.Sprintf("too many block locator hashes for message [max %d]",
MaxBlockLocatorsPerMsg)
return messageError("MsgGetBlocks.AddBlockLocatorHash", str)
return messageError("MsgGetBlockInvs.AddBlockLocatorHash", str)
}
msg.BlockLocatorHashes = append(msg.BlockLocatorHashes, hash)
@ -50,7 +50,7 @@ func (msg *MsgGetBlocks) AddBlockLocatorHash(hash *daghash.Hash) error {
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
// This is part of the Message interface implementation.
func (msg *MsgGetBlocks) BtcDecode(r io.Reader, pver uint32) error {
func (msg *MsgGetBlockInvs) BtcDecode(r io.Reader, pver uint32) error {
err := ReadElement(r, &msg.ProtocolVersion)
if err != nil {
return err
@ -64,7 +64,7 @@ func (msg *MsgGetBlocks) BtcDecode(r io.Reader, pver uint32) error {
if count > MaxBlockLocatorsPerMsg {
str := fmt.Sprintf("too many block locator hashes for message "+
"[count %d, max %d]", count, MaxBlockLocatorsPerMsg)
return messageError("MsgGetBlocks.BtcDecode", str)
return messageError("MsgGetBlockInvs.BtcDecode", str)
}
// Create a contiguous slice of hashes to deserialize into in order to
@ -86,12 +86,12 @@ func (msg *MsgGetBlocks) BtcDecode(r io.Reader, pver uint32) error {
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
// This is part of the Message interface implementation.
func (msg *MsgGetBlocks) BtcEncode(w io.Writer, pver uint32) error {
func (msg *MsgGetBlockInvs) BtcEncode(w io.Writer, pver uint32) error {
count := len(msg.BlockLocatorHashes)
if count > MaxBlockLocatorsPerMsg {
str := fmt.Sprintf("too many block locator hashes for message "+
"[count %d, max %d]", count, MaxBlockLocatorsPerMsg)
return messageError("MsgGetBlocks.BtcEncode", str)
return messageError("MsgGetBlockInvs.BtcEncode", str)
}
err := WriteElement(w, msg.ProtocolVersion)
@ -116,23 +116,23 @@ func (msg *MsgGetBlocks) BtcEncode(w io.Writer, pver uint32) error {
// Command returns the protocol command string for the message. This is part
// of the Message interface implementation.
func (msg *MsgGetBlocks) Command() string {
return CmdGetBlocks
func (msg *MsgGetBlockInvs) Command() string {
return CmdGetBlockInvs
}
// MaxPayloadLength returns the maximum length the payload can be for the
// receiver. This is part of the Message interface implementation.
func (msg *MsgGetBlocks) MaxPayloadLength(pver uint32) uint32 {
func (msg *MsgGetBlockInvs) MaxPayloadLength(pver uint32) uint32 {
// Protocol version 4 bytes + num hashes (varInt) + max block locator
// hashes + hash stop.
return 4 + MaxVarIntPayload + (MaxBlockLocatorsPerMsg * daghash.HashSize) + daghash.HashSize
}
// NewMsgGetBlocks returns a new bitcoin getblocks message that conforms to the
// Message interface using the passed parameters and defaults for the remaining
// fields.
func NewMsgGetBlocks(stopHash *daghash.Hash) *MsgGetBlocks {
return &MsgGetBlocks{
// NewMsgGetBlockInvs returns a new bitcoin getblockinvs message that conforms
// to the Message interface using the passed parameters and defaults for the
// remaining fields.
func NewMsgGetBlockInvs(stopHash *daghash.Hash) *MsgGetBlockInvs {
return &MsgGetBlockInvs{
ProtocolVersion: ProtocolVersion,
BlockLocatorHashes: make([]*daghash.Hash, 0, MaxBlockLocatorsPerMsg),
StopHash: stopHash,

View File

@ -14,8 +14,8 @@ import (
"github.com/davecgh/go-spew/spew"
)
// TestGetBlocks tests the MsgGetBlocks API.
func TestGetBlocks(t *testing.T) {
// TestGetBlockInvs tests the MsgGetBlockInvs API.
func TestGetBlockInvs(t *testing.T) {
pver := ProtocolVersion
// Block 99500 hash.
@ -33,16 +33,16 @@ func TestGetBlocks(t *testing.T) {
}
// Ensure we get the same data back out.
msg := NewMsgGetBlocks(stopHash)
msg := NewMsgGetBlockInvs(stopHash)
if !msg.StopHash.IsEqual(stopHash) {
t.Errorf("NewMsgGetBlocks: wrong stop hash - got %v, want %v",
t.Errorf("NewMsgGetBlockInvs: wrong stop hash - got %v, want %v",
msg.StopHash, stopHash)
}
// Ensure the command is expected value.
wantCmd := "getblocks"
wantCmd := "getblockinvs"
if cmd := msg.Command(); cmd != wantCmd {
t.Errorf("NewMsgGetBlocks: wrong command - got %v want %v",
t.Errorf("NewMsgGetBlockInvs: wrong command - got %v want %v",
cmd, wantCmd)
}
@ -80,10 +80,10 @@ func TestGetBlocks(t *testing.T) {
}
}
// TestGetBlocksWire tests the MsgGetBlocks wire encode and decode for various
// TestGetBlockInvsWire tests the MsgGetBlockInvs wire encode and decode for various
// numbers of block locator hashes and protocol versions.
func TestGetBlocksWire(t *testing.T) {
// Set protocol inside getblocks message.
func TestGetBlockInvsWire(t *testing.T) {
// Set protocol inside getblockinvs message.
pver := uint32(1)
// Block 99499 hash.
@ -107,8 +107,8 @@ func TestGetBlocksWire(t *testing.T) {
t.Errorf("NewHashFromStr: %v", err)
}
// MsgGetBlocks message with no block locators or stop hash.
noLocators := NewMsgGetBlocks(&daghash.Hash{})
// MsgGetBlockInvs message with no block locators or stop hash.
noLocators := NewMsgGetBlockInvs(&daghash.Hash{})
noLocators.ProtocolVersion = pver
noLocatorsEncoded := []byte{
0x01, 0x00, 0x00, 0x00, // Protocol version 1
@ -119,8 +119,8 @@ func TestGetBlocksWire(t *testing.T) {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Hash stop
}
// MsgGetBlocks message with multiple block locators and a stop hash.
multiLocators := NewMsgGetBlocks(stopHash)
// MsgGetBlockInvs message with multiple block locators and a stop hash.
multiLocators := NewMsgGetBlockInvs(stopHash)
multiLocators.AddBlockLocatorHash(hashLocator2)
multiLocators.AddBlockLocatorHash(hashLocator)
multiLocators.ProtocolVersion = pver
@ -142,10 +142,10 @@ func TestGetBlocksWire(t *testing.T) {
}
tests := []struct {
in *MsgGetBlocks // Message to encode
out *MsgGetBlocks // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
in *MsgGetBlockInvs // Message to encode
out *MsgGetBlockInvs // Expected decoded message
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version with no block locators.
{
@ -180,7 +180,7 @@ func TestGetBlocksWire(t *testing.T) {
}
// Decode the message from wire format.
var msg MsgGetBlocks
var msg MsgGetBlockInvs
rbuf := bytes.NewReader(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
@ -195,9 +195,9 @@ func TestGetBlocksWire(t *testing.T) {
}
}
// TestGetBlocksWireErrors performs negative tests against wire encode and
// decode of MsgGetBlocks to confirm error paths work correctly.
func TestGetBlocksWireErrors(t *testing.T) {
// TestGetBlockInvsWireErrors performs negative tests against wire encode and
// decode of MsgGetBlockInvs to confirm error paths work correctly.
func TestGetBlockInvsWireErrors(t *testing.T) {
// Set protocol inside getheaders message. Use protocol version 1
// specifically here instead of the latest because the test data is
// using bytes encoded with that protocol version.
@ -225,12 +225,12 @@ func TestGetBlocksWireErrors(t *testing.T) {
t.Errorf("NewHashFromStr: %v", err)
}
// MsgGetBlocks message with multiple block locators and a stop hash.
baseGetBlocks := NewMsgGetBlocks(stopHash)
baseGetBlocks.ProtocolVersion = pver
baseGetBlocks.AddBlockLocatorHash(hashLocator2)
baseGetBlocks.AddBlockLocatorHash(hashLocator)
baseGetBlocksEncoded := []byte{
// MsgGetBlockInvs message with multiple block locators and a stop hash.
baseGetBlockInvs := NewMsgGetBlockInvs(stopHash)
baseGetBlockInvs.ProtocolVersion = pver
baseGetBlockInvs.AddBlockLocatorHash(hashLocator2)
baseGetBlockInvs.AddBlockLocatorHash(hashLocator)
baseGetBlockInvsEncoded := []byte{
0x01, 0x00, 0x00, 0x00, // Protocol version 1
0x02, // Varint for number of block locator hashes
0xe0, 0xde, 0x06, 0x44, 0x68, 0x13, 0x2c, 0x63,
@ -249,35 +249,35 @@ func TestGetBlocksWireErrors(t *testing.T) {
// Message that forces an error by having more than the max allowed
// block locator hashes.
maxGetBlocks := NewMsgGetBlocks(stopHash)
maxGetBlockInvs := NewMsgGetBlockInvs(stopHash)
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
maxGetBlocks.AddBlockLocatorHash(mainNetGenesisHash)
maxGetBlockInvs.AddBlockLocatorHash(mainNetGenesisHash)
}
maxGetBlocks.BlockLocatorHashes = append(maxGetBlocks.BlockLocatorHashes,
maxGetBlockInvs.BlockLocatorHashes = append(maxGetBlockInvs.BlockLocatorHashes,
mainNetGenesisHash)
maxGetBlocksEncoded := []byte{
maxGetBlockInvsEncoded := []byte{
0x01, 0x00, 0x00, 0x00, // Protocol version 1
0xfd, 0xf5, 0x01, // Varint for number of block loc hashes (501)
}
tests := []struct {
in *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 *MsgGetBlockInvs // 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},
{baseGetBlockInvs, baseGetBlockInvsEncoded, pver, 0, io.ErrShortWrite, io.EOF},
// Force error in block locator hash count.
{baseGetBlocks, baseGetBlocksEncoded, pver, 4, io.ErrShortWrite, io.EOF},
{baseGetBlockInvs, baseGetBlockInvsEncoded, pver, 4, io.ErrShortWrite, io.EOF},
// Force error in block locator hashes.
{baseGetBlocks, baseGetBlocksEncoded, pver, 5, io.ErrShortWrite, io.EOF},
{baseGetBlockInvs, baseGetBlockInvsEncoded, pver, 5, io.ErrShortWrite, io.EOF},
// Force error in stop hash.
{baseGetBlocks, baseGetBlocksEncoded, pver, 69, io.ErrShortWrite, io.EOF},
{baseGetBlockInvs, baseGetBlockInvsEncoded, pver, 69, io.ErrShortWrite, io.EOF},
// Force error with greater than max block locator hashes.
{maxGetBlocks, maxGetBlocksEncoded, pver, 7, wireErr, wireErr},
{maxGetBlockInvs, maxGetBlockInvsEncoded, pver, 7, wireErr, wireErr},
}
t.Logf("Running %d tests", len(tests))
@ -302,7 +302,7 @@ func TestGetBlocksWireErrors(t *testing.T) {
}
// Decode from wire format.
var msg MsgGetBlocks
var msg MsgGetBlockInvs
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {

View File

@ -21,7 +21,7 @@ const defaultInvListAlloc = 1000
// MsgInv implements the Message interface and represents a bitcoin inv message.
// It is used to advertise a peer's known data such as blocks and transactions
// through inventory vectors. It may be sent unsolicited to inform other peers
// of the data or in response to a getblocks message (MsgGetBlocks). Each
// of the data or in response to a getblockinvs message (MsgGetBlockInvs). Each
// message is limited to a maximum number of inventory vectors, which is
// currently 50,000.
//